Blob


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