10 Infile *curfile = infile;
13 Src src[MAXSRC]; /* input source stack */
16 void pushsrc(int type, char *ptr) /* new input source */
18 if (++srcp >= src + MAXSRC)
19 ERROR "inputs nested too deep" FATAL;
23 printf("\n%3d ", srcp - src);
26 printf("push file %s\n", ptr);
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;
49 void popsrc(void) /* restore an old one */
52 ERROR "too many inputs popped" FATAL;
54 printf("%3d ", srcp - src);
60 printf("pop macro\n");
63 printf("pop char <%c>\n", *srcp->sp);
69 printf("pop string\n");
75 ERROR "pop weird input %d", srcp->type FATAL;
81 void definition(char *s) /* collect definition for s and install */
82 /* definitions picked up lexically */
87 p = delimstr("definition");
89 if (stp != NULL) { /* it's there before */
90 if (stp->type != DEFNAME) {
91 ERROR "%s used as variable and definition", s WARNING;
100 dprintf("installing %s as `%s'\n", s, p);
103 char *delimstr(char *s) /* get body of X ... X */
104 /* message if too big */
106 int c, delim, rdelim, n, deep;
107 static char *buf = NULL;
112 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
113 while ((delim = input()) == ' ' || delim == '\t' || delim == '\n')
115 rdelim = baldelim(delim, "{}"); /* could be "(){}[]`'" */
124 if (p >= buf + nbuf) {
126 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
130 ERROR "end of file in %s %c %.20s... %c", s, delim, buf, delim FATAL;
134 dprintf("delimstr %s %c <%s> %c\n", s, delim, buf, delim);
135 return tostring(buf);
139 baldelim(int c, char *s) /* replace c by balancing entry in s */
147 Arg args[10]; /* argument frames */
148 Arg *argfp = args; /* frame pointer */
149 int argcnt; /* number of arguments seen so far */
151 void dodef(Obj *stp) /* collect args and switch input to defn */
159 ERROR "arguments too deep" FATAL;
162 ERROR "disaster in dodef" FATAL;
164 ap->argval = malloc(1000);
165 for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
166 ap->argstk[argcnt++] = p;
170 for (i = argcnt; i < MAXARGS; i++)
173 for (i = 0; i < argcnt; i++)
174 printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
176 pushsrc(Macro, stp->val);
180 getarg(char *p) /* pick up single argument, store in p, return length */
188 ERROR "end of file in getarg!" FATAL;
189 if (npar == 0 && (c == ',' || c == ')'))
191 if (c == '"') /* copy quoted stuff intact */
195 } while ((c = input()) != '"' && c != EOF);
209 char pbuf[PBSIZE]; /* pushback buffer */
210 char *pb = pbuf-1; /* next pushed back character */
212 char ebuf[200]; /* collect input here for error reporting */
218 extern char *untilstr;
231 if (ep >= ebuf + sizeof ebuf)
244 switch (srcp->type) {
245 case Free: /* free string */
249 case Thru: /* end of pushed back line */
259 } else { /* can't happen? */
269 if (*srcp->sp == '\0') /* empty, so pop */
277 ERROR "argfp underflow" FATAL;
280 } else if (c == '$' && isdigit(*srcp->sp)) { /* $3 */
282 while (isdigit(*srcp->sp))
283 n = 10 * n + *srcp->sp++ - '0';
284 if (n > 0 && n <= MAXARGS)
285 pushsrc(String, argfp->argstk[n-1]);
290 c = getc(curfile->fin);
292 if (curfile == infile)
293 ERROR "end of file inside .G1/.G2" FATAL;
294 if (curfile->fin != stdin) {
295 fclose(curfile->fin);
296 free(curfile->fname); /* assumes allocated */
299 printf(".lf %d %s\n", curfile->lineno, curfile->fname);
301 thru = 0; /* chicken out */
316 void do_thru(void) /* read one line, make into a macro expansion */
324 ERROR "arguments too deep" FATAL;
325 if (ap->argval == NULL)
326 ap->argval = malloc(1000);
330 if (thru == 0) { /* end of file was seen, so thru is done */
334 for ( ; c != '\n' && c != EOF; ) {
335 if (c == ' ' || c == '\t') {
339 if (argcnt >= MAXARGS)
340 ERROR "too many fields on input line" FATAL;
341 ap->argstk[argcnt++] = p;
345 if ((c = nextchar()) == '\\') {
350 } while (c != '"' && c != '\n' && c != EOF);
357 } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
364 ERROR "unexpected end of file in do_thru" FATAL;
365 if (argcnt == 0) { /* ignore blank line */
366 pushsrc(Thru, (char *) 0);
369 for (i = argcnt; i < MAXARGS; i++)
372 for (i = 0; i < argcnt; i++)
373 printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
374 if (strcmp(ap->argstk[0], ".G2") == 0) {
377 pushsrc(String, "\n.G2\n");
380 if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
387 pushsrc(Thru, (char *) 0);
388 dprintf("do_thru pushing back <%s>\n", thrudef->val);
390 pushsrc(Macro, thrudef->val);
396 if (++pb >= pbuf + sizeof pbuf)
397 ERROR "pushback overflow" FATAL;
399 ep = ebuf + sizeof(ebuf) - 1;
410 double errcheck(double x, char *s)
416 ERROR "%s argument out of domain", s WARNING;
417 } else if (errno == ERANGE) {
419 ERROR "%s result out of range", s WARNING;
426 void yyerror(char *s)
428 extern char *cmdname;
429 int ern = errno; /* cause some libraries clobber it */
434 fprintf(stderr, "%s: %s", cmdname, s);
439 fprintf(stderr, " near %s:%d\n",
440 curfile->fname, curfile->lineno+1);
446 void eprint(void) /* try to print context around error */
451 if (p > ebuf && *p == '\n')
453 for ( ; p >= ebuf && *p != '\n'; p--)
457 fprintf(stderr, " context is\n\t");
458 for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
463 fprintf(stderr, " >>> ");
467 fprintf(stderr, " <<< ");
470 fgets(ebuf, sizeof ebuf, curfile->fin);
471 fprintf(stderr, "%s", ebuf);
472 pbstr("\n.G2\n"); /* safety first */
476 int yywrap(void) {return 1;}
478 char *newfile = 0; /* filename for file copy */
479 char *untilstr = 0; /* string that terminates a thru */
480 int thru = 0; /* 1 if copying thru macro */
481 Obj *thrudef = 0; /* macro being used */
483 void copyfile(char *s) /* remember file to start reading from */
488 void copydef(Obj *p) /* remember macro Obj */
493 Obj *copythru(char *s) /* collect the macro name or body for thru */
500 if (p->type == DEFNAME) {
501 p->val = addnewline(p->val);
504 ERROR "%s used as define and name", s FATAL;
506 /* have to collect the definition */
507 pbstr(s); /* first char is the delimiter */
508 q = delimstr("thru body");
509 p = lookup("nameless", 1);
515 p->val = addnewline(p->val);
516 dprintf("installing nameless as `%s'\n", p->val);
520 char *addnewline(char *p) /* add newline to end of p */
525 if (p[n-1] != '\n') {
533 void copyuntil(char *s) /* string that terminates a thru */
538 void copy(void) /* begin input from file, etc. */
543 if ((fin = fopen(newfile, "r")) == NULL)
544 ERROR "can't open file %s", newfile FATAL;
547 curfile->fname = tostring(newfile);
549 printf(".lf 1 %s\n", curfile->fname);
550 pushsrc(File, curfile->fname);
555 begin = 1; /* wrong place */
559 char shellbuf[1000], *shellp;
561 void shell_init(void) /* set up to interpret a shell command */
563 fprintf(tfd, "# shell cmd...\n");
564 sprintf(shellbuf, "rc -c '");
565 shellp = shellbuf + strlen(shellbuf);
568 void shell_text(char *s) /* add string to command being collected */
570 /* fprintf(tfd, "#add <%s> to <%s>\n", s, shellbuf); */
572 if (*s == '\'') { /* protect interior quotes */
581 void shell_exec(void) /* do it */
583 /* fprintf(tfd, "# run <%s>\n", shellbuf); */