input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. c = 0;
  232. loop:
  233. switch (srcp->type) {
  234. case Free: /* free string */
  235. free(srcp->sp);
  236. popsrc();
  237. goto loop;
  238. case Thru: /* end of pushed back line */
  239. begin = 1;
  240. popsrc();
  241. c = '\n';
  242. break;
  243. case Char:
  244. if (pb >= pbuf) {
  245. c = *pb--;
  246. popsrc();
  247. break;
  248. } else { /* can't happen? */
  249. popsrc();
  250. goto loop;
  251. }
  252. case String:
  253. c = *srcp->sp++;
  254. if (c == '\0') {
  255. popsrc();
  256. goto loop;
  257. } else {
  258. if (*srcp->sp == '\0') /* empty, so pop */
  259. popsrc();
  260. break;
  261. }
  262. case Macro:
  263. c = *srcp->sp++;
  264. if (c == '\0') {
  265. if (--argfp < args)
  266. ERROR "argfp underflow" FATAL;
  267. popsrc();
  268. goto loop;
  269. } else if (c == '$' && isdigit(*srcp->sp)) {
  270. int n = 0;
  271. while (isdigit(*srcp->sp))
  272. n = 10 * n + *srcp->sp++ - '0';
  273. if (n > 0 && n <= MAXARGS)
  274. pushsrc(String, argfp->argstk[n-1]);
  275. goto loop;
  276. }
  277. break;
  278. case File:
  279. c = getc(curfile->fin);
  280. if (c == EOF) {
  281. if (curfile == infile)
  282. ERROR "end of file inside .PS/.PE" FATAL;
  283. if (curfile->fin != stdin) {
  284. fclose(curfile->fin);
  285. free(curfile->fname); /* assumes allocated */
  286. }
  287. curfile--;
  288. printlf(curfile->lineno, curfile->fname);
  289. popsrc();
  290. thru = 0; /* chicken out */
  291. thrudef = 0;
  292. if (untilstr) {
  293. free(untilstr);
  294. untilstr = 0;
  295. }
  296. goto loop;
  297. }
  298. if (c == '\n')
  299. curfile->lineno++;
  300. break;
  301. }
  302. return c;
  303. }
  304. void do_thru(void) /* read one line, make into a macro expansion */
  305. {
  306. int c, i;
  307. char *p;
  308. Arg *ap;
  309. ap = argfp+1;
  310. if (ap >= args+10)
  311. ERROR "arguments too deep" FATAL;
  312. if (ap->argval == NULL)
  313. ap->argval = malloc(1000);
  314. p = ap->argval;
  315. argcnt = 0;
  316. c = nextchar();
  317. if (thru == 0) { /* end of file was seen, so thru is done */
  318. unput(c);
  319. return;
  320. }
  321. for ( ; c != '\n' && c != EOF; ) {
  322. if (c == ' ' || c == '\t') {
  323. c = nextchar();
  324. continue;
  325. }
  326. ap->argstk[argcnt++] = p;
  327. if (c == '"') {
  328. do {
  329. *p++ = c;
  330. if ((c = nextchar()) == '\\') {
  331. *p++ = c;
  332. *p++ = nextchar();
  333. c = nextchar();
  334. }
  335. } while (c != '"' && c != '\n' && c != EOF);
  336. *p++ = '"';
  337. if (c == '"')
  338. c = nextchar();
  339. } else {
  340. do {
  341. *p++ = c;
  342. } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
  343. if (c == ',')
  344. c = nextchar();
  345. }
  346. *p++ = '\0';
  347. }
  348. if (c == EOF)
  349. ERROR "unexpected end of file in do_thru" FATAL;
  350. if (argcnt == 0) { /* ignore blank line */
  351. pushsrc(Thru, (char *) 0);
  352. return;
  353. }
  354. for (i = argcnt; i < MAXARGS; i++)
  355. ap->argstk[i] = "";
  356. if (dbg)
  357. for (i = 0; i < argcnt; i++)
  358. printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
  359. if (strcmp(ap->argstk[0], ".PE") == 0) {
  360. thru = 0;
  361. thrudef = 0;
  362. pushsrc(String, "\n.PE\n");
  363. return;
  364. }
  365. if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
  366. thru = 0;
  367. thrudef = 0;
  368. free(untilstr);
  369. untilstr = 0;
  370. return;
  371. }
  372. pushsrc(Thru, (char *) 0);
  373. dprintf("do_thru pushing back <%s>\n", thrudef->s_val.p);
  374. argfp = ap;
  375. pushsrc(Macro, thrudef->s_val.p);
  376. }
  377. unput(int c)
  378. {
  379. if (++pb >= pbuf + sizeof pbuf)
  380. ERROR "pushback overflow" FATAL;
  381. if (--ep < ebuf)
  382. ep = ebuf + sizeof(ebuf) - 1;
  383. *pb = c;
  384. pushsrc(Char, pb);
  385. return c;
  386. }
  387. void pbstr(char *s)
  388. {
  389. pushsrc(String, s);
  390. }
  391. double errcheck(double x, char *s)
  392. {
  393. extern int errno;
  394. if (errno == EDOM) {
  395. errno = 0;
  396. ERROR "%s argument out of domain", s WARNING;
  397. } else if (errno == ERANGE) {
  398. errno = 0;
  399. ERROR "%s result out of range", s WARNING;
  400. }
  401. return x;
  402. }
  403. char errbuf[200];
  404. void eprint(void);
  405. void yyerror(char *s)
  406. {
  407. extern char *cmdname;
  408. int ern = errno; /* cause some libraries clobber it */
  409. if (synerr)
  410. return;
  411. fflush(stdout);
  412. fprintf(stderr, "%s: %s", cmdname, s);
  413. if (ern > 0) {
  414. errno = ern;
  415. perror("???");
  416. }
  417. fprintf(stderr, " near %s:%d\n",
  418. curfile->fname, curfile->lineno+1);
  419. eprint();
  420. synerr = 1;
  421. errno = 0;
  422. }
  423. void eprint(void) /* try to print context around error */
  424. {
  425. char *p, *q;
  426. p = ep - 1;
  427. if (p > ebuf && *p == '\n')
  428. p--;
  429. for ( ; p >= ebuf && *p != '\n'; p--)
  430. ;
  431. while (*p == '\n')
  432. p++;
  433. fprintf(stderr, " context is\n\t");
  434. for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
  435. ;
  436. while (p < q)
  437. putc(*p++, stderr);
  438. fprintf(stderr, " >>> ");
  439. while (p < ep)
  440. putc(*p++, stderr);
  441. fprintf(stderr, " <<< ");
  442. while (pb >= pbuf)
  443. putc(*pb--, stderr);
  444. fgets(ebuf, sizeof ebuf, curfile->fin);
  445. fprintf(stderr, "%s", ebuf);
  446. pbstr("\n.PE\n"); /* safety first */
  447. ep = ebuf;
  448. }
  449. int
  450. yywrap(void)
  451. {
  452. return 1; /* read eof; did not switch inputs */
  453. }
  454. char *newfile = 0; /* filename for file copy */
  455. char *untilstr = 0; /* string that terminates a thru */
  456. int thru = 0; /* 1 if copying thru macro */
  457. struct symtab *thrudef = 0; /* macro being used */
  458. void copyfile(char *s) /* remember file to start reading from */
  459. {
  460. newfile = s;
  461. }
  462. void copydef(struct symtab *p) /* remember macro symtab ptr */
  463. {
  464. thrudef = p;
  465. }
  466. struct symtab *copythru(char *s) /* collect the macro name or body for thru */
  467. {
  468. struct symtab *p;
  469. char *q, *addnewline(char *);
  470. p = lookup(s);
  471. if (p != NULL) {
  472. if (p->s_type == DEFNAME) {
  473. p->s_val.p = addnewline(p->s_val.p);
  474. return p;
  475. } else
  476. ERROR "%s used as define and name", s FATAL;
  477. }
  478. /* have to collect the definition */
  479. pbstr(s); /* first char is the delimiter */
  480. q = delimstr("thru body");
  481. s = "nameless";
  482. p = lookup(s);
  483. if (p != NULL) {
  484. if (p->s_val.p)
  485. free(p->s_val.p);
  486. p->s_val.p = q;
  487. } else {
  488. YYSTYPE u;
  489. u.p = q;
  490. p = makevar(tostring(s), DEFNAME, u);
  491. }
  492. p->s_val.p = addnewline(p->s_val.p);
  493. dprintf("installing %s as `%s'\n", s, p->s_val.p);
  494. return p;
  495. }
  496. char *addnewline(char *p) /* add newline to end of p */
  497. {
  498. int n;
  499. n = strlen(p);
  500. if (p[n-1] != '\n') {
  501. p = realloc(p, n+2);
  502. p[n] = '\n';
  503. p[n+1] = '\0';
  504. }
  505. return p;
  506. }
  507. void copyuntil(char *s) /* string that terminates a thru */
  508. {
  509. untilstr = s;
  510. }
  511. void copy(void) /* begin input from file, etc. */
  512. {
  513. FILE *fin;
  514. if (newfile) {
  515. if ((fin = fopen(newfile, "r")) == NULL)
  516. ERROR "can't open file %s", newfile FATAL;
  517. curfile++;
  518. curfile->fin = fin;
  519. curfile->fname = newfile;
  520. curfile->lineno = 0;
  521. printlf(1, curfile->fname);
  522. pushsrc(File, curfile->fname);
  523. newfile = 0;
  524. }
  525. if (thrudef) {
  526. thru = 1;
  527. begin = 1; /* wrong place */
  528. }
  529. }
  530. char shellbuf[1000], *shellp;
  531. void shell_init(void) /* set up to interpret a shell command */
  532. {
  533. sprintf(shellbuf, "rc -c '");
  534. shellp = shellbuf + strlen(shellbuf);
  535. }
  536. void shell_text(char *s) /* add string to command being collected */
  537. {
  538. while (*shellp++ = *s++)
  539. ;
  540. shellp--;
  541. }
  542. void shell_exec(void) /* do it */
  543. {
  544. *shellp++ = '\'';
  545. *shellp = '\0';
  546. system(shellbuf);
  547. }