input.c 11 KB

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