sam.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. void raspflush(File*);
  187. /*
  188. * acme fns
  189. */
  190. void* fbufalloc(void);
  191. void fbuffree(void*);
  192. uint min(uint, uint);
  193. void cvttorunes(char*, int, Rune*, int*, int*, int*);
  194. #define runemalloc(a) (Rune*)emalloc((a)*sizeof(Rune))
  195. #define runerealloc(a, b) (Rune*)realloc((a), (b)*sizeof(Rune))
  196. #define runemove(a, b, c) memmove((a), (b), (c)*sizeof(Rune))
  197. int alnum(int);
  198. int Read(int, void*, int);
  199. void Seek(int, long, int);
  200. int plan9(File*, int, String*, int);
  201. int Write(int, void*, int);
  202. int bexecute(File*, Posn);
  203. void cd(String*);
  204. void closefiles(File*, String*);
  205. void closeio(Posn);
  206. void cmdloop(void);
  207. void cmdupdate(void);
  208. void compile(String*);
  209. void copy(File*, Address);
  210. File *current(File*);
  211. void delete(File*);
  212. void delfile(File*);
  213. void dellist(List*, int);
  214. void doubleclick(File*, Posn);
  215. void dprint(char*, ...);
  216. void edit(File*, int);
  217. void *emalloc(ulong);
  218. void *erealloc(void*, ulong);
  219. void error(Err);
  220. void error_c(Err, int);
  221. void error_r(Err, char*);
  222. void error_s(Err, char*);
  223. int execute(File*, Posn, Posn);
  224. int filematch(File*, String*);
  225. void filename(File*);
  226. void fixname(String*);
  227. void fullname(String*);
  228. void getcurwd(void);
  229. File *getfile(String*);
  230. int getname(File*, String*, int);
  231. long getnum(int);
  232. void hiccough(char*);
  233. void inslist(List*, int, ...);
  234. Address lineaddr(Posn, Address, int);
  235. List *listalloc(int);
  236. void listfree(List*);
  237. void load(File*);
  238. File *lookfile(String*);
  239. void lookorigin(File*, Posn, Posn);
  240. int lookup(int);
  241. void move(File*, Address);
  242. void moveto(File*, Range);
  243. File *newfile(void);
  244. void nextmatch(File*, String*, Posn, int);
  245. void notifyf(void*, char*);
  246. void panic(char*);
  247. void printposn(File*, int);
  248. void print_ss(char*, String*, String*);
  249. void print_s(char*, String*);
  250. int rcv(void);
  251. Range rdata(List*, Posn, Posn);
  252. Posn readio(File*, int*, int, int);
  253. void rescue(void);
  254. void resetcmd(void);
  255. void resetsys(void);
  256. void resetxec(void);
  257. void rgrow(List*, Posn, Posn);
  258. void samerr(char*);
  259. void settempfile(void);
  260. int skipbl(void);
  261. void snarf(File*, Posn, Posn, Buffer*, int);
  262. void sortname(File*);
  263. void startup(char*, int, char**, char**);
  264. void state(File*, int);
  265. int statfd(int, ulong*, uvlong*, long*, long*, long*);
  266. int statfile(char*, ulong*, uvlong*, long*, long*, long*);
  267. void Straddc(String*, int);
  268. void Strclose(String*);
  269. int Strcmp(String*, String*);
  270. void Strdelete(String*, Posn, Posn);
  271. void Strdupl(String*, Rune*);
  272. void Strduplstr(String*, String*);
  273. void Strinit(String*);
  274. void Strinit0(String*);
  275. void Strinsert(String*, String*, Posn);
  276. void Strinsure(String*, ulong);
  277. int Strispre(String*, String*);
  278. void Strzero(String*);
  279. int Strlen(Rune*);
  280. char *Strtoc(String*);
  281. void syserror(char*);
  282. void telldot(File*);
  283. void tellpat(void);
  284. String *tmpcstr(char*);
  285. String *tmprstr(Rune*, int);
  286. void freetmpstr(String*);
  287. void termcommand(void);
  288. void termwrite(char*);
  289. File *tofile(String*);
  290. void trytoclose(File*);
  291. void trytoquit(void);
  292. int undo(int);
  293. void update(void);
  294. char *waitfor(int);
  295. void warn(Warn);
  296. void warn_s(Warn, char*);
  297. void warn_SS(Warn, String*, String*);
  298. void warn_S(Warn, String*);
  299. int whichmenu(File*);
  300. void writef(File*);
  301. Posn writeio(File*);
  302. Discdesc *Dstart(void);
  303. extern Rune samname[]; /* compiler dependent */
  304. extern Rune *left[];
  305. extern Rune *right[];
  306. extern char RSAM[]; /* system dependent */
  307. extern char SAMTERM[];
  308. extern char HOME[];
  309. extern char TMPDIR[];
  310. extern char SH[];
  311. extern char SHPATH[];
  312. extern char RX[];
  313. extern char RXPATH[];
  314. extern char SAMSAVECMD[];
  315. /*
  316. * acme globals
  317. */
  318. extern long seq;
  319. extern Disk *disk;
  320. extern char *rsamname; /* globals */
  321. extern char *samterm;
  322. extern Rune genbuf[];
  323. extern char *genc;
  324. extern int io;
  325. extern int patset;
  326. extern int quitok;
  327. extern Address addr;
  328. extern Buffer snarfbuf;
  329. extern Buffer plan9buf;
  330. extern List file;
  331. extern List tempfile;
  332. extern File *cmd;
  333. extern File *curfile;
  334. extern File *lastfile;
  335. extern Mod modnum;
  336. extern Posn cmdpt;
  337. extern Posn cmdptadv;
  338. extern Rangeset sel;
  339. extern String curwd;
  340. extern String cmdstr;
  341. extern String genstr;
  342. extern String lastpat;
  343. extern String lastregexp;
  344. extern String plan9cmd;
  345. extern int downloaded;
  346. extern int eof;
  347. extern int bpipeok;
  348. extern int panicking;
  349. extern Rune empty[];
  350. extern int termlocked;
  351. extern int outbuffered;
  352. #include "mesg.h"
  353. void outTs(Hmesg, int);
  354. void outT0(Hmesg);
  355. void outTl(Hmesg, long);
  356. void outTslS(Hmesg, int, long, String*);
  357. void outTS(Hmesg, String*);
  358. void outTsS(Hmesg, int, String*);
  359. void outTsllS(Hmesg, int, long, long, String*);
  360. void outTsll(Hmesg, int, long, long);
  361. void outTsl(Hmesg, int, long);
  362. void outTsv(Hmesg, int, vlong);
  363. void outflush(void);
  364. int needoutflush(void);