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 /*
10 * BLOCKSIZE is relatively small to keep memory consumption down.
11 */
13 #define BLOCKSIZE 2048
14 #define RUNESIZE sizeof(Rune)
15 #define NDISC 5
16 #define NBUFFILES 3+2*NDISC /* plan 9+undo+snarf+NDISC*(transcript+buf) */
17 #define NSUBEXP 10
19 #define TRUE 1
20 #define FALSE 0
22 #undef INFINITY /* Darwin declares this as HUGE_VAL */
23 #define INFINITY 0x7FFFFFFFL
24 #define INCR 25
25 #define STRSIZE (2*BLOCKSIZE)
27 typedef long Posn; /* file position or address */
28 typedef ushort Mod; /* modification number */
30 typedef struct Address Address;
31 typedef struct Block Block;
32 typedef struct Buffer Buffer;
33 typedef struct Disk Disk;
34 typedef struct Discdesc Discdesc;
35 typedef struct File File;
36 typedef struct List List;
37 typedef struct Range Range;
38 typedef struct Rangeset Rangeset;
39 typedef struct String String;
41 enum State
42 {
43 Clean = ' ',
44 Dirty = '\'',
45 Unread = '-',
46 };
48 struct Range
49 {
50 Posn p1, p2;
51 };
53 struct Rangeset
54 {
55 Range p[NSUBEXP];
56 };
58 struct Address
59 {
60 Range r;
61 File *f;
62 };
64 struct String
65 {
66 short n;
67 short size;
68 Rune *s;
69 };
71 struct List /* code depends on a long being able to hold a pointer */
72 {
73 int nalloc;
74 int nused;
75 union{
76 void *listp;
77 Block *blkp;
78 long *longp;
79 uchar* *ucharp;
80 String* *stringp;
81 File* *filep;
82 long listv;
83 }g;
84 };
86 #define listptr g.listp
87 #define blkptr g.blkp
88 #define longptr g.longp
89 #define ucharpptr g.ucharp
90 #define stringpptr g.stringp
91 #define filepptr g.filep
92 #define listval g.listv
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 // Text *curtext; /* most recently used associated text */
172 // Text **text; /* list of associated texts */
173 // int ntext;
174 // int dumpid; /* used in dumping zeroxed windows */
176 Posn hiposn; /* highest address touched this Mod */
177 Address dot; /* current position */
178 Address ndot; /* new current position after update */
179 Range tdot; /* what terminal thinks is current range */
180 Range mark; /* tagged spot in text (don't confuse with Mark) */
181 List *rasp; /* map of what terminal's got */
182 short tag; /* for communicating with terminal */
183 char closeok; /* ok to close file? */
184 char deleted; /* delete at completion of command */
185 Range prevdot; /* state before start of change */
186 Range prevmark;
187 long prevseq;
188 int prevmod;
189 };
190 //File* fileaddtext(File*, Text*);
191 void fileclose(File*);
192 void filedelete(File*, uint, uint);
193 //void filedeltext(File*, Text*);
194 void fileinsert(File*, uint, Rune*, uint);
195 uint fileload(File*, uint, int, int*);
196 void filemark(File*);
197 void filereset(File*);
198 void filesetname(File*, String*);
199 void fileundelete(File*, Buffer*, uint, uint);
200 void fileuninsert(File*, Buffer*, uint, uint);
201 void fileunsetname(File*, Buffer*);
202 void fileundo(File*, int, int, uint*, uint*, int);
203 int fileupdate(File*, int, int);
205 int filereadc(File*, uint);
206 File *fileopen(void);
207 void loginsert(File*, uint, Rune*, uint);
208 void logdelete(File*, uint, uint);
209 void logsetname(File*, String*);
210 int fileisdirty(File*);
211 long undoseq(File*, int);
212 long prevseq(Buffer*);
214 void raspload(File*);
215 void raspstart(File*);
216 void raspdelete(File*, uint, uint, int);
217 void raspinsert(File*, uint, Rune*, uint, int);
218 void raspdone(File*, int);
220 /*
221 * acme fns
222 */
223 void* fbufalloc(void);
224 void fbuffree(void*);
225 uint min(uint, uint);
226 void cvttorunes(char*, int, Rune*, int*, int*, int*);
228 #define runemalloc(a) (Rune*)emalloc((a)*sizeof(Rune))
229 #define runerealloc(a, b) (Rune*)realloc((a), (b)*sizeof(Rune))
230 #define runemove(a, b, c) memmove((a), (b), (c)*sizeof(Rune))
232 int alnum(int);
233 int Read(int, void*, int);
234 void Seek(int, long, int);
235 int plan9(File*, int, String*, int);
236 int Write(int, void*, int);
237 int bexecute(File*, Posn);
238 void cd(String*);
239 void closefiles(File*, String*);
240 void closeio(Posn);
241 void cmdloop(void);
242 void cmdupdate(void);
243 void compile(String*);
244 void copy(File*, Address);
245 File *current(File*);
246 void delete(File*);
247 void delfile(File*);
248 void dellist(List*, int);
249 void doubleclick(File*, Posn);
250 void dprint(char*, ...);
251 void edit(File*, int);
252 void *emalloc(ulong);
253 void *erealloc(void*, ulong);
254 void error(Err);
255 void error_c(Err, int);
256 void error_r(Err, char*);
257 void error_s(Err, char*);
258 int execute(File*, Posn, Posn);
259 int filematch(File*, String*);
260 void filename(File*);
261 void fixname(String*);
262 void fullname(String*);
263 void getcurwd(void);
264 File *getfile(String*);
265 int getname(File*, String*, int);
266 long getnum(int);
267 void hiccough(char*);
268 void inslist(List*, int, long);
269 Address lineaddr(Posn, Address, int);
270 void listfree(List*);
271 void load(File*);
272 File *lookfile(String*);
273 void lookorigin(File*, Posn, Posn);
274 int lookup(int);
275 void move(File*, Address);
276 void moveto(File*, Range);
277 File *newfile(void);
278 void nextmatch(File*, String*, Posn, int);
279 int newtmp(int);
280 void notifyf(void*, char*);
281 void panic(char*);
282 void printposn(File*, int);
283 void print_ss(char*, String*, String*);
284 void print_s(char*, String*);
285 int rcv(void);
286 Range rdata(List*, Posn, Posn);
287 Posn readio(File*, int*, int, int);
288 void rescue(void);
289 void resetcmd(void);
290 void resetsys(void);
291 void resetxec(void);
292 void rgrow(List*, Posn, Posn);
293 void samerr(char*);
294 void settempfile(void);
295 int skipbl(void);
296 void snarf(File*, Posn, Posn, Buffer*, int);
297 void sortname(File*);
298 void startup(char*, int, char**, char**);
299 void state(File*, int);
300 int statfd(int, ulong*, uvlong*, long*, long*, long*);
301 int statfile(char*, ulong*, uvlong*, long*, long*, long*);
302 void Straddc(String*, int);
303 void Strclose(String*);
304 int Strcmp(String*, String*);
305 void Strdelete(String*, Posn, Posn);
306 void Strdupl(String*, Rune*);
307 void Strduplstr(String*, String*);
308 void Strinit(String*);
309 void Strinit0(String*);
310 void Strinsert(String*, String*, Posn);
311 void Strinsure(String*, ulong);
312 int Strispre(String*, String*);
313 void Strzero(String*);
314 int Strlen(Rune*);
315 char *Strtoc(String*);
316 void syserror(char*);
317 void telldot(File*);
318 void tellpat(void);
319 String *tmpcstr(char*);
320 String *tmprstr(Rune*, int);
321 void freetmpstr(String*);
322 void termcommand(void);
323 void termwrite(char*);
324 File *tofile(String*);
325 void trytoclose(File*);
326 void trytoquit(void);
327 int undo(int);
328 void update(void);
329 int waitfor(int);
330 void warn(Warn);
331 void warn_s(Warn, char*);
332 void warn_SS(Warn, String*, String*);
333 void warn_S(Warn, String*);
334 int whichmenu(File*);
335 void writef(File*);
336 Posn writeio(File*);
337 Discdesc *Dstart(void);
339 extern Rune samname[]; /* compiler dependent */
340 extern Rune *left[];
341 extern Rune *right[];
343 extern char RSAM[]; /* system dependent */
344 extern char SAMTERM[];
345 extern char HOME[];
346 extern char TMPDIR[];
347 extern char SH[];
348 extern char SHPATH[];
349 extern char RX[];
350 extern char RXPATH[];
352 /*
353 * acme globals
354 */
355 extern long seq;
356 extern Disk *disk;
358 extern char *rsamname; /* globals */
359 extern char *samterm;
360 extern Rune genbuf[];
361 extern char *genc;
362 extern int io;
363 extern int patset;
364 extern int quitok;
365 extern Address addr;
366 extern Buffer snarfbuf;
367 extern Buffer plan9buf;
368 extern List file;
369 extern List tempfile;
370 extern File *cmd;
371 extern File *curfile;
372 extern File *lastfile;
373 extern Mod modnum;
374 extern Posn cmdpt;
375 extern Posn cmdptadv;
376 extern Rangeset sel;
377 extern String curwd;
378 extern String cmdstr;
379 extern String genstr;
380 extern String lastpat;
381 extern String lastregexp;
382 extern String plan9cmd;
383 extern int downloaded;
384 extern int eof;
385 extern int bpipeok;
386 extern int panicking;
387 extern Rune empty[];
388 extern int termlocked;
389 extern int noflush;
391 #include "mesg.h"
393 void outTs(Hmesg, int);
394 void outT0(Hmesg);
395 void outTl(Hmesg, long);
396 void outTslS(Hmesg, int, long, String*);
397 void outTS(Hmesg, String*);
398 void outTsS(Hmesg, int, String*);
399 void outTsllS(Hmesg, int, long, long, String*);
400 void outTsll(Hmesg, int, long, long);
401 void outTsl(Hmesg, int, long);
402 void outTsv(Hmesg, int, long);
403 void outstart(Hmesg);
404 void outcopy(int, void*);
405 void outshort(int);
406 void outlong(long);
407 void outvlong(void*);
408 void outsend(void);
409 void outflush(void);