sam.h 9.1 KB

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