sam.h 9.4 KB

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