10 Infile *curfile = infile;
13 Src src[MAXSRC]; /* input source stack */
20 int baldelim(int, char *);
22 void pushsrc(int type, char *ptr) /* new input source */
24 if (++srcp >= src + MAXSRC)
25 ERROR "inputs nested too deep" FATAL;
29 printf("\n%3d ", (int)(srcp - src));
32 printf("push file %s\n", ((Infile *)ptr)->fname);
35 printf("push macro <%s>\n", ptr);
38 printf("push char <%c>\n", *ptr);
41 printf("push thru\n");
44 printf("push string <%s>\n", ptr);
47 printf("push free <%s>\n", ptr);
50 ERROR "pushed bad type %d", srcp->type FATAL;
55 void popsrc(void) /* restore an old one */
58 ERROR "too many inputs popped" FATAL;
60 printf("%3d ", (int) (srcp - src));
66 printf("pop macro\n");
69 printf("pop char <%c>\n", *srcp->sp);
75 printf("pop string\n");
81 ERROR "pop weird input %d", srcp->type FATAL;
87 void definition(char *s) /* collect definition for s and install */
88 /* definitions picked up lexically */
93 p = delimstr("definition");
95 if (stp != NULL) { /* it's there before */
96 if (stp->s_type != DEFNAME) {
97 ERROR "%s used as variable and definition", s WARNING;
105 makevar(tostring(s), DEFNAME, u);
107 dprintf("installing %s as `%s'\n", s, p);
110 char *delimstr(char *s) /* get body of X ... X */
111 /* message if too big */
113 int c, delim, rdelim, n, deep;
114 static char *buf = NULL;
119 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
120 while ((delim = input()) == ' ' || delim == '\t' || delim == '\n')
122 rdelim = baldelim(delim, "{}"); /* could be "(){}[]`'" */
131 if (p >= buf + nbuf) {
133 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
137 ERROR "end of file in %s %c %.20s... %c", s, delim, buf, delim FATAL;
141 dprintf("delimstr %s %c <%s> %c\n", s, delim, buf, delim);
142 return tostring(buf);
146 baldelim(int c, char *s) /* replace c by balancing entry in s */
154 void undefine(char *s) /* undefine macro */
156 while (*s != ' ' && *s != '\t') /* skip "undef..." */
158 while (*s == ' ' || *s == '\t')
164 Arg args[10]; /* argument frames */
165 Arg *argfp = args; /* frame pointer */
166 int argcnt; /* number of arguments seen so far */
168 void dodef(struct symtab *stp) /* collect args and switch input to defn */
176 ERROR "arguments too deep" FATAL;
179 ERROR "disaster in dodef" FATAL;
181 ap->argval = malloc(1000);
182 for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
183 ap->argstk[argcnt++] = p;
187 for (i = argcnt; i < MAXARGS; i++)
190 for (i = 0; i < argcnt; i++)
191 printf("arg %d.%d = <%s>\n", (int) (ap-args), i+1, ap->argstk[i]);
193 pushsrc(Macro, stp->s_val.p);
197 getarg(char *p) /* pick up single argument, store in p, return length */
205 ERROR "end of file in getarg" FATAL;
206 if (npar == 0 && (c == ',' || c == ')'))
208 if (c == '"') /* copy quoted stuff intact */
212 } while ((c = input()) != '"' && c != EOF);
226 char pbuf[PBSIZE]; /* pushback buffer */
227 char *pb = pbuf-1; /* next pushed back character */
229 char ebuf[200]; /* collect input here for error reporting */
234 extern struct symtab *thrudef;
235 extern char *untilstr;
249 if (ep >= ebuf + sizeof ebuf)
259 c = 0; /* Botch: gcc */
262 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((unsigned char) *srcp->sp)) {
300 while (isdigit((unsigned char) *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 */
334 void do_thru(void) /* read one line, make into a macro expansion */
342 ERROR "arguments too deep" FATAL;
343 if (ap->argval == NULL)
344 ap->argval = malloc(1000);
348 if (thru == 0) { /* end of file was seen, so thru is done */
352 for ( ; c != '\n' && c != EOF; ) {
353 if (c == ' ' || c == '\t') {
357 ap->argstk[argcnt++] = p;
361 if ((c = nextchar()) == '\\') {
366 } while (c != '"' && c != '\n' && c != EOF);
373 } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
380 ERROR "unexpected end of file in do_thru" FATAL;
381 if (argcnt == 0) { /* ignore blank line */
382 pushsrc(Thru, (char *) 0);
385 for (i = argcnt; i < MAXARGS; i++)
388 for (i = 0; i < argcnt; i++)
389 printf("arg %d.%d = <%s>\n", (int) (ap-args), i+1, ap->argstk[i]);
390 if (strcmp(ap->argstk[0], ".PE") == 0) {
393 pushsrc(String, "\n.PE\n");
396 if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
403 pushsrc(Thru, (char *) 0);
404 dprintf("do_thru pushing back <%s>\n", thrudef->s_val.p);
406 pushsrc(Macro, thrudef->s_val.p);
412 if (++pb >= pbuf + sizeof pbuf)
413 ERROR "pushback overflow" FATAL;
415 ep = ebuf + sizeof(ebuf) - 1;
426 double errcheck(double x, char *s)
430 ERROR "%s argument out of domain", s WARNING;
431 } else if (errno == ERANGE) {
433 ERROR "%s result out of range", s WARNING;
442 void yyerror(char *s)
444 extern char *cmdname;
445 int ern = errno; /* cause some libraries clobber it */
450 fprintf(stderr, "%s: %s", cmdname, s);
455 fprintf(stderr, " near %s:%d\n",
456 curfile->fname, curfile->lineno+1);
462 void 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 */
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 */
497 void copyfile(char *s) /* remember file to start reading from */
502 void copydef(struct symtab *p) /* remember macro symtab ptr */
507 struct symtab *copythru(char *s) /* collect the macro name or body for thru */
510 char *q, *addnewline(char *);
514 if (p->s_type == DEFNAME) {
515 p->s_val.p = addnewline(p->s_val.p);
518 ERROR "%s used as define and name", s FATAL;
520 /* have to collect the definition */
521 pbstr(s); /* first char is the delimiter */
522 q = delimstr("thru body");
532 p = makevar(tostring(s), DEFNAME, u);
534 p->s_val.p = addnewline(p->s_val.p);
535 dprintf("installing %s as `%s'\n", s, p->s_val.p);
539 char *addnewline(char *p) /* add newline to end of p */
544 if (p[n-1] != '\n') {
552 void copyuntil(char *s) /* string that terminates a thru */
557 void copy(void) /* begin input from file, etc. */
562 if ((fin = fopen(newfile, "r")) == NULL)
563 ERROR "can't open file %s", newfile FATAL;
566 curfile->fname = newfile;
568 printlf(1, curfile->fname);
569 pushsrc(File, curfile->fname);
574 begin = 1; /* wrong place */
578 char shellbuf[1000], *shellp;
580 void shell_init(void) /* set up to interpret a shell command */
582 sprintf(shellbuf, "rc -c '");
583 shellp = shellbuf + strlen(shellbuf);
586 void shell_text(char *s) /* add string to command being collected */
588 while ((*shellp++ = *s++))
593 void shell_exec(void) /* do it */