xref: /plan9-contrib/sys/src/cmd/sam/sam.h (revision ec59a3ddbfceee0efe34584c2c9981a5e5ff1ec4)
1 #include <u.h>
2 #include <libc.h>
3 #include <plumb.h>
4 #include "errors.h"
5 
6 /*
7  * BLOCKSIZE is relatively small to keep memory consumption down.
8  */
9 
10 #define	BLOCKSIZE	2048
11 #define	RUNESIZE	sizeof(Rune)
12 #define	NDISC		5
13 #define	NBUFFILES	3+2*NDISC	/* plan 9+undo+snarf+NDISC*(transcript+buf) */
14 #define NSUBEXP	10
15 
16 #define	TRUE		1
17 #define	FALSE		0
18 
19 #define	INFINITY	0x7FFFFFFFL
20 #define	INCR		25
21 #define	STRSIZE		(2*BLOCKSIZE)
22 
23 typedef long		Posn;		/* file position or address */
24 typedef	ushort		Mod;		/* modification number */
25 
26 typedef struct Address	Address;
27 typedef struct Block	Block;
28 typedef struct Buffer	Buffer;
29 typedef struct Disk	Disk;
30 typedef struct Discdesc	Discdesc;
31 typedef struct File	File;
32 typedef struct List	List;
33 typedef struct Range	Range;
34 typedef struct Rangeset	Rangeset;
35 typedef struct String	String;
36 
37 enum State
38 {
39 	Clean =		' ',
40 	Dirty =		'\'',
41 	Unread =	'-',
42 };
43 
44 struct Range
45 {
46 	Posn	p1, p2;
47 };
48 
49 struct Rangeset
50 {
51 	Range	p[NSUBEXP];
52 };
53 
54 struct Address
55 {
56 	Range	r;
57 	File	*f;
58 };
59 
60 struct String
61 {
62 	short	n;
63 	short	size;
64 	Rune	*s;
65 };
66 
67 struct List
68 {
69 	int	type;	/* 'p' for pointer, 'P' for Posn */
70 	int	nalloc;
71 	int	nused;
72 	union{
73 		void*	listp;
74 		void**	voidp;
75 		Posn*	posnp;
76 		String**stringp;
77 		File**	filep;
78 	}g;
79 };
80 
81 #define	listptr		g.listp
82 #define	voidpptr	g.voidp
83 #define	posnptr		g.posnp
84 #define	stringpptr	g.stringp
85 #define	filepptr	g.filep
86 
87 enum
88 {
89 	Blockincr =	256,
90 	Maxblock = 	8*1024,
91 
92 	BUFSIZE = Maxblock,	/* size from fbufalloc() */
93 	RBUFSIZE = BUFSIZE/sizeof(Rune),
94 };
95 
96 
97 enum
98 {
99 	Null		= '-',
100 	Delete		= 'd',
101 	Insert		= 'i',
102 	Filename	= 'f',
103 	Dot		= 'D',
104 	Mark		= 'm',
105 };
106 
107 struct Block
108 {
109 	uint		addr;	/* disk address in bytes */
110 	union
111 	{
112 		uint	n;	/* number of used runes in block */
113 		Block	*next;	/* pointer to next in free list */
114 	};
115 };
116 
117 struct Disk
118 {
119 	int		fd;
120 	uint		addr;	/* length of temp file */
121 	Block		*free[Maxblock/Blockincr+1];
122 };
123 
124 Disk*		diskinit(void);
125 Block*		disknewblock(Disk*, uint);
126 void		diskrelease(Disk*, Block*);
127 void		diskread(Disk*, Block*, Rune*, uint);
128 void		diskwrite(Disk*, Block**, Rune*, uint);
129 
130 struct Buffer
131 {
132 	uint		nc;
133 	Rune		*c;	/* cache */
134 	uint		cnc;	/* bytes in cache */
135 	uint		cmax;	/* size of allocated cache */
136 	uint		cq;	/* position of cache */
137 	int		cdirty;	/* cache needs to be written */
138 	uint		cbi;	/* index of cache Block */
139 	Block		**bl;	/* array of blocks */
140 	uint		nbl;	/* number of blocks */
141 };
142 void		bufinsert(Buffer*, uint, Rune*, uint);
143 void		bufdelete(Buffer*, uint, uint);
144 uint		bufload(Buffer*, uint, int, int*);
145 void		bufread(Buffer*, uint, Rune*, uint);
146 void		bufclose(Buffer*);
147 void		bufreset(Buffer*);
148 
149 struct File
150 {
151 	Buffer;				/* the data */
152 	Buffer		delta;		/* transcript of changes */
153 	Buffer		epsilon;	/* inversion of delta for redo */
154 	String		name;		/* name of associated file */
155 	uvlong		qidpath;	/* of file when read */
156 	uint		mtime;		/* of file when read */
157 	int		dev;		/* of file when read */
158 	int		unread;		/* file has not been read from disk */
159 
160 	long		seq;		/* if seq==0, File acts like Buffer */
161 	long		cleanseq;	/* f->seq at last read/write of file */
162 	int		mod;		/* file appears modified in menu */
163 	char		rescuing;	/* sam exiting; this file unusable */
164 
165 //	Text		*curtext;	/* most recently used associated text */
166 //	Text		**text;		/* list of associated texts */
167 //	int		ntext;
168 //	int		dumpid;		/* used in dumping zeroxed windows */
169 
170 	Posn		hiposn;		/* highest address touched this Mod */
171 	Address		dot;		/* current position */
172 	Address		ndot;		/* new current position after update */
173 	Range		tdot;		/* what terminal thinks is current range */
174 	Range		mark;		/* tagged spot in text (don't confuse with Mark) */
175 	List		*rasp;		/* map of what terminal's got */
176 	short		tag;		/* for communicating with terminal */
177 	char		closeok;	/* ok to close file? */
178 	char		deleted;	/* delete at completion of command */
179 	Range		prevdot;	/* state before start of change */
180 	Range		prevmark;
181 	long		prevseq;
182 	int		prevmod;
183 };
184 //File*		fileaddtext(File*, Text*);
185 void		fileclose(File*);
186 void		filedelete(File*, uint, uint);
187 //void		filedeltext(File*, Text*);
188 void		fileinsert(File*, uint, Rune*, uint);
189 uint		fileload(File*, uint, int, int*);
190 void		filemark(File*);
191 void		filereset(File*);
192 void		filesetname(File*, String*);
193 void		fileundelete(File*, Buffer*, uint, uint);
194 void		fileuninsert(File*, Buffer*, uint, uint);
195 void		fileunsetname(File*, Buffer*);
196 void		fileundo(File*, int, int, uint*, uint*, int);
197 int		fileupdate(File*, int, int);
198 
199 int		filereadc(File*, uint);
200 File		*fileopen(void);
201 void		loginsert(File*, uint, Rune*, uint);
202 void		logdelete(File*, uint, uint);
203 void		logsetname(File*, String*);
204 int		fileisdirty(File*);
205 long		undoseq(File*, int);
206 long		prevseq(Buffer*);
207 
208 void		raspload(File*);
209 void		raspstart(File*);
210 void		raspdelete(File*, uint, uint, int);
211 void		raspinsert(File*, uint, Rune*, uint, int);
212 void		raspdone(File*, int);
213 
214 /*
215  * acme fns
216  */
217 void*	fbufalloc(void);
218 void	fbuffree(void*);
219 uint	min(uint, uint);
220 void	cvttorunes(char*, int, Rune*, int*, int*, int*);
221 
222 #define	runemalloc(a)		(Rune*)emalloc((a)*sizeof(Rune))
223 #define	runerealloc(a, b)	(Rune*)realloc((a), (b)*sizeof(Rune))
224 #define	runemove(a, b, c)	memmove((a), (b), (c)*sizeof(Rune))
225 
226 int	alnum(int);
227 int	Read(int, void*, int);
228 void	Seek(int, long, int);
229 int	plan9(File*, int, String*, int);
230 int	Write(int, void*, int);
231 int	bexecute(File*, Posn);
232 void	cd(String*);
233 void	closefiles(File*, String*);
234 void	closeio(Posn);
235 void	cmdloop(void);
236 void	cmdupdate(void);
237 void	compile(String*);
238 void	copy(File*, Address);
239 File	*current(File*);
240 void	delete(File*);
241 void	delfile(File*);
242 void	dellist(List*, int);
243 void	doubleclick(File*, Posn);
244 void	dprint(char*, ...);
245 void	edit(File*, int);
246 void	*emalloc(ulong);
247 void	*erealloc(void*, ulong);
248 void	error(Err);
249 void	error_c(Err, int);
250 void	error_r(Err, char*);
251 void	error_s(Err, char*);
252 int	execute(File*, Posn, Posn);
253 int	filematch(File*, String*);
254 void	filename(File*);
255 void	fixname(String*);
256 void	fullname(String*);
257 void	getcurwd(void);
258 File	*getfile(String*);
259 int	getname(File*, String*, int);
260 long	getnum(int);
261 void	hiccough(char*);
262 void	inslist(List*, int, ...);
263 Address	lineaddr(Posn, Address, int);
264 List	*listalloc(int);
265 void	listfree(List*);
266 void	load(File*);
267 File	*lookfile(String*);
268 void	lookorigin(File*, Posn, Posn);
269 int	lookup(int);
270 void	move(File*, Address);
271 void	moveto(File*, Range);
272 File	*newfile(void);
273 void	nextmatch(File*, String*, Posn, int);
274 void	notifyf(void*, char*);
275 void	panic(char*);
276 void	printposn(File*, int);
277 void	print_ss(char*, String*, String*);
278 void	print_s(char*, String*);
279 int	rcv(void);
280 Range	rdata(List*, Posn, Posn);
281 Posn	readio(File*, int*, int, int);
282 void	rescue(void);
283 void	resetcmd(void);
284 void	resetsys(void);
285 void	resetxec(void);
286 void	rgrow(List*, Posn, Posn);
287 void	samerr(char*);
288 void	settempfile(void);
289 int	skipbl(void);
290 void	snarf(File*, Posn, Posn, Buffer*, int);
291 void	sortname(File*);
292 void	startup(char*, int, char**, char**);
293 void	state(File*, int);
294 int	statfd(int, ulong*, uvlong*, long*, long*, long*);
295 int	statfile(char*, ulong*, uvlong*, long*, long*, long*);
296 void	Straddc(String*, int);
297 void	Strclose(String*);
298 int	Strcmp(String*, String*);
299 void	Strdelete(String*, Posn, Posn);
300 void	Strdupl(String*, Rune*);
301 void	Strduplstr(String*, String*);
302 void	Strinit(String*);
303 void	Strinit0(String*);
304 void	Strinsert(String*, String*, Posn);
305 void	Strinsure(String*, ulong);
306 int	Strispre(String*, String*);
307 void	Strzero(String*);
308 int	Strlen(Rune*);
309 char	*Strtoc(String*);
310 void	syserror(char*);
311 void	telldot(File*);
312 void	tellpat(void);
313 String	*tmpcstr(char*);
314 String	*tmprstr(Rune*, int);
315 void	freetmpstr(String*);
316 void	termcommand(void);
317 void	termwrite(char*);
318 File	*tofile(String*);
319 void	trytoclose(File*);
320 void	trytoquit(void);
321 int	undo(int);
322 void	update(void);
323 char	*waitfor(int);
324 void	warn(Warn);
325 void	warn_s(Warn, char*);
326 void	warn_SS(Warn, String*, String*);
327 void	warn_S(Warn, String*);
328 int	whichmenu(File*);
329 void	writef(File*);
330 Posn	writeio(File*);
331 Discdesc *Dstart(void);
332 
333 extern Rune	samname[];	/* compiler dependent */
334 extern Rune	*left[];
335 extern Rune	*right[];
336 
337 extern char	RSAM[];		/* system dependent */
338 extern char	SAMTERM[];
339 extern char	HOME[];
340 extern char	TMPDIR[];
341 extern char	SH[];
342 extern char	SHPATH[];
343 extern char	RX[];
344 extern char	RXPATH[];
345 extern char	SAMSAVECMD[];
346 
347 /*
348  * acme globals
349  */
350 extern long		seq;
351 extern Disk		*disk;
352 
353 extern char	*rsamname;	/* globals */
354 extern char	*samterm;
355 extern Rune	genbuf[];
356 extern char	*genc;
357 extern int	io;
358 extern int	patset;
359 extern int	quitok;
360 extern Address	addr;
361 extern Buffer	snarfbuf;
362 extern Buffer	plan9buf;
363 extern List	file;
364 extern List	tempfile;
365 extern File	*cmd;
366 extern File	*curfile;
367 extern File	*lastfile;
368 extern Mod	modnum;
369 extern Posn	cmdpt;
370 extern Posn	cmdptadv;
371 extern Rangeset	sel;
372 extern String	curwd;
373 extern String	cmdstr;
374 extern String	genstr;
375 extern String	lastpat;
376 extern String	lastregexp;
377 extern String	plan9cmd;
378 extern int	downloaded;
379 extern int	eof;
380 extern int	bpipeok;
381 extern int	panicking;
382 extern Rune	empty[];
383 extern int	termlocked;
384 extern int	noflush;
385 
386 #include "mesg.h"
387 
388 void	outTs(Hmesg, int);
389 void	outT0(Hmesg);
390 void	outTl(Hmesg, long);
391 void	outTslS(Hmesg, int, long, String*);
392 void	outTS(Hmesg, String*);
393 void	outTsS(Hmesg, int, String*);
394 void	outTsllS(Hmesg, int, long, long, String*);
395 void	outTsll(Hmesg, int, long, long);
396 void	outTsl(Hmesg, int, long);
397 void	outTsv(Hmesg, int, vlong);
398 void	outflush(void);
399