input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <errno.h>
  14. #include "grap.h"
  15. #include "y.tab.h"
  16. Infile infile[10];
  17. Infile *curfile = infile;
  18. #define MAXSRC 50
  19. Src src[MAXSRC]; /* input source stack */
  20. Src *srcp = src;
  21. void pushsrc(int type, char *ptr) /* new input source */
  22. {
  23. if (++srcp >= src + MAXSRC)
  24. ERROR "inputs nested too deep" FATAL;
  25. srcp->type = type;
  26. srcp->sp = ptr;
  27. if (dbg) {
  28. printf("\n%3d ", srcp - src);
  29. switch (srcp->type) {
  30. case File:
  31. printf("push file %s\n", ptr);
  32. break;
  33. case Macro:
  34. printf("push macro <%s>\n", ptr);
  35. break;
  36. case Char:
  37. printf("push char <%c>\n", *ptr);
  38. break;
  39. case Thru:
  40. printf("push thru\n");
  41. break;
  42. case String:
  43. printf("push string <%s>\n", ptr);
  44. break;
  45. case Free:
  46. printf("push free <%s>\n", ptr);
  47. break;
  48. default:
  49. ERROR "pushed bad type %d", srcp->type FATAL;
  50. }
  51. }
  52. }
  53. void popsrc(void) /* restore an old one */
  54. {
  55. if (srcp <= src)
  56. ERROR "too many inputs popped" FATAL;
  57. if (dbg) {
  58. printf("%3d ", srcp - src);
  59. switch (srcp->type) {
  60. case File:
  61. printf("pop file\n");
  62. break;
  63. case Macro:
  64. printf("pop macro\n");
  65. break;
  66. case Char:
  67. printf("pop char <%c>\n", *srcp->sp);
  68. break;
  69. case Thru:
  70. printf("pop thru\n");
  71. break;
  72. case String:
  73. printf("pop string\n");
  74. break;
  75. case Free:
  76. printf("pop free\n");
  77. break;
  78. default:
  79. ERROR "pop weird input %d", srcp->type FATAL;
  80. }
  81. }
  82. srcp--;
  83. }
  84. void definition(char *s) /* collect definition for s and install */
  85. /* definitions picked up lexically */
  86. {
  87. char *p;
  88. Obj *stp;
  89. p = delimstr("definition");
  90. stp = lookup(s, 0);
  91. if (stp != NULL) { /* it's there before */
  92. if (stp->type != DEFNAME) {
  93. ERROR "%s used as variable and definition", s WARNING;
  94. return;
  95. }
  96. free(stp->val);
  97. } else {
  98. stp = lookup(s, 1);
  99. stp->type = DEFNAME;
  100. }
  101. stp->val = p;
  102. dprintf("installing %s as `%s'\n", s, p);
  103. }
  104. char *delimstr(char *s) /* get body of X ... X */
  105. /* message if too big */
  106. {
  107. int c, delim, rdelim, n, deep;
  108. static char *buf = NULL;
  109. static int nbuf = 0;
  110. char *p;
  111. if (buf == NULL)
  112. buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
  113. while ((delim = input()) == ' ' || delim == '\t' || delim == '\n')
  114. ;
  115. rdelim = baldelim(delim, "{}"); /* could be "(){}[]`'" */
  116. deep = 1;
  117. for (p = buf; ; ) {
  118. c = input();
  119. if (c == rdelim)
  120. if (--deep == 0)
  121. break;
  122. if (c == delim)
  123. deep++;
  124. if (p >= buf + nbuf) {
  125. n = p - buf;
  126. buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
  127. p = buf + n;
  128. }
  129. if (c == EOF)
  130. ERROR "end of file in %s %c %.20s... %c", s, delim, buf, delim FATAL;
  131. *p++ = c;
  132. }
  133. *p = '\0';
  134. dprintf("delimstr %s %c <%s> %c\n", s, delim, buf, delim);
  135. return tostring(buf);
  136. }
  137. baldelim(int c, char *s) /* replace c by balancing entry in s */
  138. {
  139. for ( ; *s; s += 2)
  140. if (*s == c)
  141. return s[1];
  142. return c;
  143. }
  144. Arg args[10]; /* argument frames */
  145. Arg *argfp = args; /* frame pointer */
  146. int argcnt; /* number of arguments seen so far */
  147. void dodef(Obj *stp) /* collect args and switch input to defn */
  148. {
  149. int i, len;
  150. char *p;
  151. Arg *ap;
  152. ap = argfp+1;
  153. if (ap >= args+10)
  154. ERROR "arguments too deep" FATAL;
  155. argcnt = 0;
  156. if (input() != '(')
  157. ERROR "disaster in dodef" FATAL;
  158. if (ap->argval == 0)
  159. ap->argval = malloc(1000);
  160. for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
  161. ap->argstk[argcnt++] = p;
  162. if (input() == ')')
  163. break;
  164. }
  165. for (i = argcnt; i < MAXARGS; i++)
  166. ap->argstk[i] = "";
  167. if (dbg)
  168. for (i = 0; i < argcnt; i++)
  169. printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
  170. argfp = ap;
  171. pushsrc(Macro, stp->val);
  172. }
  173. getarg(char *p) /* pick up single argument, store in p, return length */
  174. {
  175. int n, c, npar;
  176. n = npar = 0;
  177. for ( ;; ) {
  178. c = input();
  179. if (c == EOF)
  180. ERROR "end of file in getarg!" FATAL;
  181. if (npar == 0 && (c == ',' || c == ')'))
  182. break;
  183. if (c == '"') /* copy quoted stuff intact */
  184. do {
  185. *p++ = c;
  186. n++;
  187. } while ((c = input()) != '"' && c != EOF);
  188. else if (c == '(')
  189. npar++;
  190. else if (c == ')')
  191. npar--;
  192. n++;
  193. *p++ = c;
  194. }
  195. *p = 0;
  196. unput(c);
  197. return(n + 1);
  198. }
  199. #define PBSIZE 2000
  200. char pbuf[PBSIZE]; /* pushback buffer */
  201. char *pb = pbuf-1; /* next pushed back character */
  202. char ebuf[200]; /* collect input here for error reporting */
  203. char *ep = ebuf;
  204. int begin = 0;
  205. extern int thru;
  206. extern Obj *thrudef;
  207. extern char *untilstr;
  208. input(void)
  209. {
  210. register int c;
  211. if (thru && begin) {
  212. do_thru();
  213. begin = 0;
  214. }
  215. c = nextchar();
  216. dprintf(" <%c>", c);
  217. if (ep >= ebuf + sizeof ebuf)
  218. ep = ebuf;
  219. return *ep++ = c;
  220. }
  221. nextchar(void)
  222. {
  223. register int c;
  224. loop:
  225. switch (srcp->type) {
  226. case Free: /* free string */
  227. free(srcp->sp);
  228. popsrc();
  229. goto loop;
  230. case Thru: /* end of pushed back line */
  231. begin = 1;
  232. popsrc();
  233. c = '\n';
  234. break;
  235. case Char:
  236. if (pb >= pbuf) {
  237. c = *pb--;
  238. popsrc();
  239. break;
  240. } else { /* can't happen? */
  241. popsrc();
  242. goto loop;
  243. }
  244. case String:
  245. c = *srcp->sp++;
  246. if (c == '\0') {
  247. popsrc();
  248. goto loop;
  249. } else {
  250. if (*srcp->sp == '\0') /* empty, so pop */
  251. popsrc();
  252. break;
  253. }
  254. case Macro:
  255. c = *srcp->sp++;
  256. if (c == '\0') {
  257. if (--argfp < args)
  258. ERROR "argfp underflow" FATAL;
  259. popsrc();
  260. goto loop;
  261. } else if (c == '$' && isdigit(*srcp->sp)) { /* $3 */
  262. int n = 0;
  263. while (isdigit(*srcp->sp))
  264. n = 10 * n + *srcp->sp++ - '0';
  265. if (n > 0 && n <= MAXARGS)
  266. pushsrc(String, argfp->argstk[n-1]);
  267. goto loop;
  268. }
  269. break;
  270. case File:
  271. c = getc(curfile->fin);
  272. if (c == EOF) {
  273. if (curfile == infile)
  274. ERROR "end of file inside .G1/.G2" FATAL;
  275. if (curfile->fin != stdin) {
  276. fclose(curfile->fin);
  277. free(curfile->fname); /* assumes allocated */
  278. }
  279. curfile--;
  280. printf(".lf %d %s\n", curfile->lineno, curfile->fname);
  281. popsrc();
  282. thru = 0; /* chicken out */
  283. thrudef = 0;
  284. if (untilstr) {
  285. free(untilstr);
  286. untilstr = 0;
  287. }
  288. goto loop;
  289. }
  290. if (c == '\n')
  291. curfile->lineno++;
  292. break;
  293. }
  294. return c;
  295. }
  296. void do_thru(void) /* read one line, make into a macro expansion */
  297. {
  298. int c, i;
  299. char *p;
  300. Arg *ap;
  301. ap = argfp+1;
  302. if (ap >= args+10)
  303. ERROR "arguments too deep" FATAL;
  304. if (ap->argval == NULL)
  305. ap->argval = malloc(1000);
  306. p = ap->argval;
  307. argcnt = 0;
  308. c = nextchar();
  309. if (thru == 0) { /* end of file was seen, so thru is done */
  310. unput(c);
  311. return;
  312. }
  313. for ( ; c != '\n' && c != EOF; ) {
  314. if (c == ' ' || c == '\t') {
  315. c = nextchar();
  316. continue;
  317. }
  318. if (argcnt >= MAXARGS)
  319. ERROR "too many fields on input line" FATAL;
  320. ap->argstk[argcnt++] = p;
  321. if (c == '"') {
  322. do {
  323. *p++ = c;
  324. if ((c = nextchar()) == '\\') {
  325. *p++ = c;
  326. *p++ = nextchar();
  327. c = nextchar();
  328. }
  329. } while (c != '"' && c != '\n' && c != EOF);
  330. *p++ = '"';
  331. if (c == '"')
  332. c = nextchar();
  333. } else {
  334. do {
  335. *p++ = c;
  336. } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
  337. if (c == ',')
  338. c = nextchar();
  339. }
  340. *p++ = '\0';
  341. }
  342. if (c == EOF)
  343. ERROR "unexpected end of file in do_thru" FATAL;
  344. if (argcnt == 0) { /* ignore blank line */
  345. pushsrc(Thru, (char *) 0);
  346. return;
  347. }
  348. for (i = argcnt; i < MAXARGS; i++)
  349. ap->argstk[i] = "";
  350. if (dbg)
  351. for (i = 0; i < argcnt; i++)
  352. printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
  353. if (strcmp(ap->argstk[0], ".G2") == 0) {
  354. thru = 0;
  355. thrudef = 0;
  356. pushsrc(String, "\n.G2\n");
  357. return;
  358. }
  359. if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
  360. thru = 0;
  361. thrudef = 0;
  362. free(untilstr);
  363. untilstr = 0;
  364. return;
  365. }
  366. pushsrc(Thru, (char *) 0);
  367. dprintf("do_thru pushing back <%s>\n", thrudef->val);
  368. argfp = ap;
  369. pushsrc(Macro, thrudef->val);
  370. }
  371. unput(int c)
  372. {
  373. if (++pb >= pbuf + sizeof pbuf)
  374. ERROR "pushback overflow" FATAL;
  375. if (--ep < ebuf)
  376. ep = ebuf + sizeof(ebuf) - 1;
  377. *pb = c;
  378. pushsrc(Char, pb);
  379. return c;
  380. }
  381. void pbstr(char *s)
  382. {
  383. pushsrc(String, s);
  384. }
  385. double errcheck(double x, char *s)
  386. {
  387. extern int errno;
  388. if (errno == EDOM) {
  389. errno = 0;
  390. ERROR "%s argument out of domain", s WARNING;
  391. } else if (errno == ERANGE) {
  392. errno = 0;
  393. ERROR "%s result out of range", s WARNING;
  394. }
  395. return x;
  396. }
  397. char errbuf[200];
  398. void yyerror(char *s)
  399. {
  400. extern char *cmdname;
  401. int ern = errno; /* cause some libraries clobber it */
  402. if (synerr)
  403. return;
  404. fflush(stdout);
  405. fprintf(stderr, "%s: %s", cmdname, s);
  406. if (ern > 0) {
  407. errno = ern;
  408. perror("???");
  409. }
  410. fprintf(stderr, " near %s:%d\n",
  411. curfile->fname, curfile->lineno+1);
  412. eprint();
  413. synerr = 1;
  414. errno = 0;
  415. }
  416. void eprint(void) /* try to print context around error */
  417. {
  418. char *p, *q;
  419. p = ep - 1;
  420. if (p > ebuf && *p == '\n')
  421. p--;
  422. for ( ; p >= ebuf && *p != '\n'; p--)
  423. ;
  424. while (*p == '\n')
  425. p++;
  426. fprintf(stderr, " context is\n\t");
  427. for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
  428. ;
  429. for (; p < q; p++)
  430. if (isprint(*p))
  431. putc(*p, stderr);
  432. fprintf(stderr, " >>> ");
  433. for (; p < ep; p++)
  434. if (isprint(*p))
  435. putc(*p, stderr);
  436. fprintf(stderr, " <<< ");
  437. while (pb >= pbuf)
  438. putc(*pb--, stderr);
  439. fgets(ebuf, sizeof ebuf, curfile->fin);
  440. fprintf(stderr, "%s", ebuf);
  441. pbstr("\n.G2\n"); /* safety first */
  442. ep = ebuf;
  443. }
  444. int yywrap(void) {return 1;}
  445. char *newfile = 0; /* filename for file copy */
  446. char *untilstr = 0; /* string that terminates a thru */
  447. int thru = 0; /* 1 if copying thru macro */
  448. Obj *thrudef = 0; /* macro being used */
  449. void copyfile(char *s) /* remember file to start reading from */
  450. {
  451. newfile = s;
  452. }
  453. void copydef(Obj *p) /* remember macro Obj */
  454. {
  455. thrudef = p;
  456. }
  457. Obj *copythru(char *s) /* collect the macro name or body for thru */
  458. {
  459. Obj *p;
  460. char *q;
  461. p = lookup(s, 0);
  462. if (p != NULL) {
  463. if (p->type == DEFNAME) {
  464. p->val = addnewline(p->val);
  465. return p;
  466. } else
  467. ERROR "%s used as define and name", s FATAL;
  468. }
  469. /* have to collect the definition */
  470. pbstr(s); /* first char is the delimiter */
  471. q = delimstr("thru body");
  472. p = lookup("nameless", 1);
  473. if (p != NULL)
  474. if (p->val)
  475. free(p->val);
  476. p->type = DEFNAME;
  477. p->val = q;
  478. p->val = addnewline(p->val);
  479. dprintf("installing nameless as `%s'\n", p->val);
  480. return p;
  481. }
  482. char *addnewline(char *p) /* add newline to end of p */
  483. {
  484. int n;
  485. n = strlen(p);
  486. if (p[n-1] != '\n') {
  487. p = realloc(p, n+2);
  488. p[n] = '\n';
  489. p[n+1] = '\0';
  490. }
  491. return p;
  492. }
  493. void copyuntil(char *s) /* string that terminates a thru */
  494. {
  495. untilstr = s;
  496. }
  497. void copy(void) /* begin input from file, etc. */
  498. {
  499. FILE *fin;
  500. if (newfile) {
  501. if ((fin = fopen(newfile, "r")) == NULL)
  502. ERROR "can't open file %s", newfile FATAL;
  503. curfile++;
  504. curfile->fin = fin;
  505. curfile->fname = tostring(newfile);
  506. curfile->lineno = 0;
  507. printf(".lf 1 %s\n", curfile->fname);
  508. pushsrc(File, curfile->fname);
  509. newfile = 0;
  510. }
  511. if (thrudef) {
  512. thru = 1;
  513. begin = 1; /* wrong place */
  514. }
  515. }
  516. char shellbuf[1000], *shellp;
  517. void shell_init(void) /* set up to interpret a shell command */
  518. {
  519. fprintf(tfd, "# shell cmd...\n");
  520. sprintf(shellbuf, "rc -c '");
  521. shellp = shellbuf + strlen(shellbuf);
  522. }
  523. void shell_text(char *s) /* add string to command being collected */
  524. {
  525. /* fprintf(tfd, "#add <%s> to <%s>\n", s, shellbuf); */
  526. while (*s) {
  527. if (*s == '\'') { /* protect interior quotes */
  528. *shellp++ = '\'';
  529. *shellp++ = '\\';
  530. *shellp++ = '\'';
  531. }
  532. *shellp++ = *s++;
  533. }
  534. }
  535. void shell_exec(void) /* do it */
  536. {
  537. /* fprintf(tfd, "# run <%s>\n", shellbuf); */
  538. *shellp++ = '\'';
  539. *shellp = '\0';
  540. system(shellbuf);
  541. }