sam.h 9.7 KB

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