9 Infile *curfile = infile;
12 Src src[MAXSRC]; /* input source stack */
16 pushsrc(int type, char *ptr) /* new input source */
18 if (++srcp >= src + MAXSRC)
19 ERROR "inputs nested too deep" FATAL;
23 printf("\n%3ld ", (long)(srcp - src));
26 printf("push file %s\n", ((Infile *)ptr)->fname);
29 printf("push macro <%s>\n", ptr);
32 printf("push char <%c>\n", *ptr);
35 printf("push thru\n");
38 printf("push string <%s>\n", ptr);
41 printf("push free <%s>\n", ptr);
44 ERROR "pushed bad type %d", srcp->type FATAL;
50 popsrc(void) /* restore an old one */
53 ERROR "too many inputs popped" FATAL;
55 printf("%3ld ", (long)(srcp - src));
61 printf("pop macro\n");
64 printf("pop char <%c>\n", *srcp->sp);
70 printf("pop string\n");
76 ERROR "pop weird input %d", srcp->type FATAL;
83 definition(char *s) /* collect definition for s and install */
84 /* definitions picked up lexically */
89 p = delimstr("definition");
91 if (stp != NULL) { /* it's there before */
92 if (stp->s_type != DEFNAME) {
93 ERROR "%s used as variable and definition", s WARNING;
101 makevar(tostring(s), DEFNAME, u);
103 dprintf("installing %s as `%s'\n", s, p);
107 delimstr(char *s) /* get body of X ... X */
108 /* message if too big */
110 int c, delim, rdelim, n, deep;
111 static char *buf = NULL;
116 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
117 while ((delim = input()) == ' ' || delim == '\t' || delim == '\n')
119 rdelim = baldelim(delim, "{}"); /* could be "(){}[]`'" */
128 if (p >= buf + nbuf) {
130 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
134 ERROR "end of file in %s %c %.20s... %c", s, delim, buf, delim FATAL;
138 dprintf("delimstr %s %c <%s> %c\n", s, delim, buf, delim);
139 return tostring(buf);
143 baldelim(int c, char *s) /* replace c by balancing entry in s */
152 undefine(char *s) /* undefine macro */
154 while (*s != ' ' && *s != '\t') /* skip "undef..." */
156 while (*s == ' ' || *s == '\t')
162 Arg args[10]; /* argument frames */
163 Arg *argfp = args; /* frame pointer */
164 int argcnt; /* number of arguments seen so far */
167 dodef(struct symtab *stp) /* collect args and switch input to defn */
175 ERROR "arguments too deep" FATAL;
178 ERROR "disaster in dodef" FATAL;
180 ap->argval = malloc(1000);
181 for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
182 ap->argstk[argcnt++] = p;
186 for (i = argcnt; i < MAXARGS; i++)
189 for (i = 0; i < argcnt; i++)
190 printf("arg %ld.%d = <%s>\n", (long)(ap-args), i+1, ap->argstk[i]);
192 pushsrc(Macro, stp->s_val.p);
196 getarg(char *p) /* pick up single argument, store in p, return length */
204 ERROR "end of file in getarg" FATAL;
205 if (npar == 0 && (c == ',' || c == ')'))
207 if (c == '"') /* copy quoted stuff intact */
211 } while ((c = input()) != '"' && c != EOF);
225 char pbuf[PBSIZE]; /* pushback buffer */
226 char *pb = pbuf-1; /* next pushed back character */
228 char ebuf[200]; /* collect input here for error reporting */
233 extern struct symtab *thrudef;
234 extern char *untilstr;
248 if (ep >= ebuf + sizeof ebuf)
259 switch (srcp->type) {
263 case Free: /* free string */
267 case Thru: /* end of pushed back line */
277 } else { /* can't happen? */
287 if (*srcp->sp == '\0') /* empty, so pop */
295 ERROR "argfp underflow" FATAL;
298 } else if (c == '$' && isdigit(*srcp->sp)) {
300 while (isdigit(*srcp->sp))
301 n = 10 * n + *srcp->sp++ - '0';
302 if (n > 0 && n <= MAXARGS)
303 pushsrc(String, argfp->argstk[n-1]);
308 c = getc(curfile->fin);
310 if (curfile == infile)
311 ERROR "end of file inside .PS/.PE" FATAL;
312 if (curfile->fin != stdin) {
313 fclose(curfile->fin);
314 free(curfile->fname); /* assumes allocated */
317 printlf(curfile->lineno, curfile->fname);
319 thru = 0; /* chicken out */
335 do_thru(void) /* read one line, make into a macro expansion */
343 ERROR "arguments too deep" FATAL;
344 if (ap->argval == NULL)
345 ap->argval = malloc(1000);
349 if (thru == 0) { /* end of file was seen, so thru is done */
353 for ( ; c != '\n' && c != EOF; ) {
354 if (c == ' ' || c == '\t') {
358 ap->argstk[argcnt++] = p;
362 if ((c = nextchar()) == '\\') {
367 } while (c != '"' && c != '\n' && c != EOF);
374 } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
381 ERROR "unexpected end of file in do_thru" FATAL;
382 if (argcnt == 0) { /* ignore blank line */
383 pushsrc(Thru, (char *) 0);
386 for (i = argcnt; i < MAXARGS; i++)
389 for (i = 0; i < argcnt; i++)
390 printf("arg %ld.%d = <%s>\n", (long)(ap-args), i+1, ap->argstk[i]);
391 if (strcmp(ap->argstk[0], ".PE") == 0) {
394 pushsrc(String, "\n.PE\n");
397 if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
404 pushsrc(Thru, (char *) 0);
405 dprintf("do_thru pushing back <%s>\n", thrudef->s_val.p);
407 pushsrc(Macro, thrudef->s_val.p);
413 if (++pb >= pbuf + sizeof pbuf)
414 ERROR "pushback overflow" FATAL;
416 ep = ebuf + sizeof(ebuf) - 1;
429 errcheck(double x, char *s)
433 ERROR "%s argument out of domain", s WARNING;
434 } else if (errno == ERANGE) {
436 ERROR "%s result out of range", s WARNING;
446 extern char *cmdname;
451 fprintf(stderr, "%s: %s", cmdname, s);
453 fprintf(stderr, " (%s)", strerror(errno));
454 fprintf(stderr, " near line %d, file %s\n",
455 curfile->lineno, curfile->fname);
462 eprint(void) /* try to print context around error */
467 if (p > ebuf && *p == '\n')
469 for ( ; p >= ebuf && *p != '\n'; p--)
473 fprintf(stderr, " context is\n\t");
474 for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
478 fprintf(stderr, " >>> ");
481 fprintf(stderr, " <<< ");
484 fgets(ebuf, sizeof ebuf, curfile->fin);
485 fprintf(stderr, "%s", ebuf);
486 pbstr("\n.PE\n"); /* safety first */
490 void yywrap(void) {;}
492 char *newfile = 0; /* filename for file copy */
493 char *untilstr = 0; /* string that terminates a thru */
494 int thru = 0; /* 1 if copying thru macro */
495 struct symtab *thrudef = 0; /* macro being used */
498 copyfile(char *s) /* remember file to start reading from */
504 copydef(struct symtab *p) /* remember macro symtab ptr */
510 copythru(char *s) /* collect the macro name or body for thru */
517 if (p->s_type == DEFNAME) {
518 p->s_val.p = addnewline(p->s_val.p);
521 ERROR "%s used as define and name", s FATAL;
523 /* have to collect the definition */
524 pbstr(s); /* first char is the delimiter */
525 q = delimstr("thru body");
535 p = makevar(tostring(s), DEFNAME, u);
537 p->s_val.p = addnewline(p->s_val.p);
538 dprintf("installing %s as `%s'\n", s, p->s_val.p);
543 addnewline(char *p) /* add newline to end of p */
548 if (p[n-1] != '\n') {
557 copyuntil(char *s) /* string that terminates a thru */
563 copy(void) /* begin input from file, etc. */
568 if ((fin = fopen(newfile, "r")) == NULL)
569 ERROR "can't open file %s", newfile FATAL;
572 curfile->fname = newfile;
574 printlf(1, curfile->fname);
575 pushsrc(File, curfile->fname);
580 begin = 1; /* wrong place */
584 char shellbuf[1000], *shellp;
587 shell_init(void) /* set up to interpret a shell command */
589 sprintf(shellbuf, "sh -c '");
590 shellp = shellbuf + strlen(shellbuf);
594 shell_text(char *s) /* add string to command being collected */
596 while (*shellp++ = *s++)
602 shell_exec(void) /* do it */