input.c 11 KB

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