aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/sam/sam.h
blob: 6e018156f7942bda08e92093a08e7c7048251c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <u.h>
#include <libc.h>
#include <plumb.h>
#include "errors.h"

#undef waitfor
#define waitfor samwaitfor

#undef warn
#define warn samwarn

#undef class
#define class samclass

/*
 * BLOCKSIZE is relatively small to keep memory consumption down.
 */

#define	BLOCKSIZE	2048
#define	RUNESIZE	sizeof(Rune)
#define	NDISC		5
#define	NBUFFILES	3+2*NDISC	/* plan 9+undo+snarf+NDISC*(transcript+buf) */
#define NSUBEXP	10

#define	TRUE		1
#define	FALSE		0

#undef INFINITY	/* Darwin declares this as HUGE_VAL */
#define	INFINITY	0x7FFFFFFFL
#define	INCR		25
#define	STRSIZE		(2*BLOCKSIZE)

typedef long		Posn;		/* file position or address */
typedef	ushort		Mod;		/* modification number */

typedef struct Address	Address;
typedef struct Block	Block;
typedef struct Buffer	Buffer;
typedef struct Disk	Disk;
typedef struct File	File;
typedef struct List	List;
typedef struct Range	Range;
typedef struct Rangeset	Rangeset;
typedef struct String	String;

enum State
{
	Clean =		' ',
	Dirty =		'\'',
	Unread =	'-'
};

struct Range
{
	Posn	p1, p2;
};

struct Rangeset
{
	Range	p[NSUBEXP];
};

struct Address
{
	Range	r;
	File	*f;
};

struct String
{
	short	n;
	short	size;
	Rune	*s;
};

struct List	/* code depends on a long being able to hold a pointer */
{
	int	type;	/* 'p' for pointer, 'P' for Posn */
	int	nalloc;
	int	nused;
	union{
		void*	listp;
		void**	voidp;
		Posn*	posnp;
		String**stringp;
		File**	filep;
	}g;
};

#define	listptr		g.listp
#define	voidpptr	g.voidp
#define	posnptr		g.posnp
#define	stringpptr	g.stringp
#define	filepptr	g.filep

enum
{
	Blockincr =	256,
	Maxblock = 	8*1024,

	BUFSIZE = Maxblock,	/* size from fbufalloc() */
	RBUFSIZE = BUFSIZE/sizeof(Rune)
};


enum
{
	Null		= '-',
	Delete		= 'd',
	Insert		= 'i',
	Filename	= 'f',
	Dot		= 'D',
	Mark		= 'm'
};

struct Block
{
	vlong		addr;	/* disk address in bytes */
	union {
		uint	n;	/* number of used runes in block */
		Block	*next;	/* pointer to next in free list */
	} u;
};

struct Disk
{
	int		fd;
	vlong		addr;	/* length of temp file */
	Block		*free[Maxblock/Blockincr+1];
};

Disk*		diskinit(void);
Block*		disknewblock(Disk*, uint);
void		diskrelease(Disk*, Block*);
void		diskread(Disk*, Block*, Rune*, uint);
void		diskwrite(Disk*, Block**, Rune*, uint);

struct Buffer
{
	uint		nc;
	Rune		*c;	/* cache */
	uint		cnc;	/* bytes in cache */
	uint		cmax;	/* size of allocated cache */
	uint		cq;	/* position of cache */
	int		cdirty;	/* cache needs to be written */
	uint		cbi;	/* index of cache Block */
	Block		**bl;	/* array of blocks */
	uint		nbl;	/* number of blocks */
};
void		bufinsert(Buffer*, uint, Rune*, uint);
void		bufdelete(Buffer*, uint, uint);
uint		bufload(Buffer*, uint, int, int*);
void		bufread(Buffer*, uint, Rune*, uint);
void		bufclose(Buffer*);
void		bufreset(Buffer*);

struct File
{
	Buffer 	b;				/* the data */
	Buffer		delta;		/* transcript of changes */
	Buffer		epsilon;	/* inversion of delta for redo */
	String		name;		/* name of associated file */
	uvlong		qidpath;	/* of file when read */
	uint		mtime;		/* of file when read */
	ulong	dev;		/* of file when read */
	int		unread;		/* file has not been read from disk */

	long		seq;		/* if seq==0, File acts like Buffer */
	long		cleanseq;	/* f->seq at last read/write of file */
	int		mod;		/* file appears modified in menu */
	char		rescuing;	/* sam exiting; this file unusable */

#if 0
//	Text		*curtext;	/* most recently used associated text */
//	Text		**text;		/* list of associated texts */
//	int		ntext;
//	int		dumpid;		/* used in dumping zeroxed windows */
#endif

	Posn		hiposn;		/* highest address touched this Mod */
	Address		dot;		/* current position */
	Address		ndot;		/* new current position after update */
	Range		tdot;		/* what terminal thinks is current range */
	Range		mark;		/* tagged spot in text (don't confuse with Mark) */
	List		*rasp;		/* map of what terminal's got */
	short		tag;		/* for communicating with terminal */
	char		closeok;	/* ok to close file? */
	char		deleted;	/* delete at completion of command */
	Range		prevdot;	/* state before start of change */
	Range		prevmark;
	long		prevseq;
	int		prevmod;
};
/*File*		fileaddtext(File*, Text*); */
void		fileclose(File*);
void		filedelete(File*, uint, uint);
/*void		filedeltext(File*, Text*); */
void		fileinsert(File*, uint, Rune*, uint);
uint		fileload(File*, uint, int, int*);
void		filemark(File*);
void		filereset(File*);
void		filesetname(File*, String*);
void		fileundelete(File*, Buffer*, uint, uint);
void		fileuninsert(File*, Buffer*, uint, uint);
void		fileunsetname(File*, Buffer*);
void		fileundo(File*, int, int, uint*, uint*, int);
int		fileupdate(File*, int, int);

int		filereadc(File*, uint);
File		*fileopen(void);
void		loginsert(File*, uint, Rune*, uint);
void		logdelete(File*, uint, uint);
void		logsetname(File*, String*);
int		fileisdirty(File*);
long		undoseq(File*, int);
long		prevseq(Buffer*);

void		raspload(File*);
void		raspstart(File*);
void		raspdelete(File*, uint, uint, int);
void		raspinsert(File*, uint, Rune*, uint, int);
void		raspdone(File*, int);
void		raspflush(File*);

/*
 * acme fns
 */
void*	fbufalloc(void);
void	fbuffree(void*);
uint	min(uint, uint);
void	cvttorunes(char*, int, Rune*, int*, int*, int*);

#define	runemalloc(a)		(Rune*)emalloc((a)*sizeof(Rune))
#define	runerealloc(a, b)	(Rune*)realloc((a), (b)*sizeof(Rune))
#define	runemove(a, b, c)	memmove((a), (b), (c)*sizeof(Rune))

int	alnum(int);
int	Read(int, void*, int);
void	Seek(int, long, int);
int	plan9(File*, int, String*, int);
int	Write(int, void*, int);
void	Close(int);
int	bexecute(File*, Posn);
void	cd(String*);
void	closefiles(File*, String*);
void	closeio(Posn);
void	cmdloop(void);
void	cmdupdate(void);
void	compile(String*);
void	copy(File*, Address);
File	*current(File*);
void	delete(File*);
void	delfile(File*);
void	dellist(List*, int);
void	doubleclick(File*, Posn);
void	dprint(char*, ...);
void	edit(File*, int);
void	*emalloc(ulong);
void	*erealloc(void*, ulong);
void	error(Err);
void	error_c(Err, int);
void	error_r(Err, char*);
void	error_s(Err, char*);
int	execute(File*, Posn, Posn);
int	filematch(File*, String*);
void	filename(File*);
void	fixname(String*);
void	fullname(String*);
void	getcurwd(void);
File	*getfile(String*);
int	getname(File*, String*, int);
long	getnum(int);
void	hiccough(char*);
void	inslist(List*, int, ...);
Address	lineaddr(Posn, Address, int);
List	*listalloc(int);
void	listfree(List*);
void	load(File*);
File	*lookfile(String*);
void	lookorigin(File*, Posn, Posn);
int	lookup(int);
void	move(File*, Address);
void	moveto(File*, Range);
File	*newfile(void);
void	nextmatch(File*, String*, Posn, int);
int	newtmp(int);
void	notifyf(void*, char*);
void	panic(char*);
void	printposn(File*, int);
void	print_ss(char*, String*, String*);
void	print_s(char*, String*);
int	rcv(void);
Range	rdata(List*, Posn, Posn);
Posn	readio(File*, int*, int, int);
void	rescue(void);
void	resetcmd(void);
void	resetsys(void);
void	resetxec(void);
void	rgrow(List*, Posn, Posn);
void	samerr(char*);
void	settempfile(void);
int	skipbl(void);
void	snarf(File*, Posn, Posn, Buffer*, int);
void	sortname(File*);
void	startup(char*, int, char**, char**);
void	state(File*, int);
int	statfd(int, ulong*, uvlong*, long*, long*, long*);
int	statfile(char*, ulong*, uvlong*, long*, long*, long*);
void	Straddc(String*, int);
void	Strclose(String*);
int	Strcmp(String*, String*);
void	Strdelete(String*, Posn, Posn);
void	Strdupl(String*, Rune*);
void	Strduplstr(String*, String*);
void	Strinit(String*);
void	Strinit0(String*);
void	Strinsert(String*, String*, Posn);
void	Strinsure(String*, ulong);
int	Strispre(String*, String*);
void	Strzero(String*);
int	Strlen(Rune*);
char	*Strtoc(String*);
void	syserror(char*);
void	telldot(File*);
void	tellpat(void);
String	*tmpcstr(char*);
String	*tmprstr(Rune*, int);
void	freetmpstr(String*);
void	termcommand(void);
void	termwrite(char*);
File	*tofile(String*);
void	trytoclose(File*);
void	trytoquit(void);
int	undo(int);
void	update(void);
int	waitfor(int);
void	warn(Warn);
void	warn_s(Warn, char*);
void	warn_SS(Warn, String*, String*);
void	warn_S(Warn, String*);
int	whichmenu(File*);
void	writef(File*);
Posn	writeio(File*);

extern Rune	samname[];	/* compiler dependent */
extern Rune	*left[];
extern Rune	*right[];

extern char	RSAM[];		/* system dependent */
extern char	SAMTERM[];
extern char	HOME[];
extern char	TMPDIR[];
extern char	SH[];
extern char	SHPATH[];
extern char	RX[];
extern char	RXPATH[];

/*
 * acme globals
 */
extern long		seq;
extern Disk		*disk;

extern char	*rsamname;	/* globals */
extern char	*samterm;
extern Rune	genbuf[];
extern char	*genc;
extern int	io;
extern int	patset;
extern int	quitok;
extern Address	addr;
extern Buffer	snarfbuf;
extern Buffer	plan9buf;
extern List	file;
extern List	tempfile;
extern File	*cmd;
extern File	*curfile;
extern File	*lastfile;
extern Mod	modnum;
extern Posn	cmdpt;
extern Posn	cmdptadv;
extern Rangeset	sel;
extern String	curwd;
extern String	cmdstr;
extern String	genstr;
extern String	lastpat;
extern String	lastregexp;
extern String	plan9cmd;
extern int	downloaded;
extern int	eof;
extern int	bpipeok;
extern int	panicking;
extern Rune	empty[];
extern int	termlocked;
extern int	outbuffered;

#include "mesg.h"

void	outTs(Hmesg, int);
void	outT0(Hmesg);
void	outTl(Hmesg, long);
void	outTslS(Hmesg, int, long, String*);
void	outTS(Hmesg, String*);
void	outTsS(Hmesg, int, String*);
void	outTsllS(Hmesg, int, long, long, String*);
void	outTsll(Hmesg, int, long, long);
void	outTsl(Hmesg, int, long);
void	outTsv(Hmesg, int, vlong);
void	outflush(void);
int needoutflush(void);