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);
138 baldelim(int c, char *s) /* replace c by balancing entry in s */
146 Arg args[10]; /* argument frames */
147 Arg *argfp = args; /* frame pointer */
148 int argcnt; /* number of arguments seen so far */
150 void dodef(Obj *stp) /* collect args and switch input to defn */
158 ERROR "arguments too deep" FATAL;
161 ERROR "disaster in dodef" FATAL;
163 ap->argval = malloc(1000);
164 for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
165 ap->argstk[argcnt++] = p;
169 for (i = argcnt; i < MAXARGS; i++)
172 for (i = 0; i < argcnt; i++)
173 printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
175 pushsrc(Macro, stp->val);
178 getarg(char *p) /* pick up single argument, store in p, return length */
186 ERROR "end of file in getarg!" FATAL;
187 if (npar == 0 && (c == ',' || c == ')'))
189 if (c == '"') /* copy quoted stuff intact */
193 } while ((c = input()) != '"' && c != EOF);
207 char pbuf[PBSIZE]; /* pushback buffer */
208 char *pb = pbuf-1; /* next pushed back character */
210 char ebuf[200]; /* collect input here for error reporting */
216 extern char *untilstr;
228 if (ep >= ebuf + sizeof ebuf)
238 switch (srcp->type) {
239 case Free: /* free string */
243 case Thru: /* end of pushed back line */
253 } else { /* can't happen? */
263 if (*srcp->sp == '\0') /* empty, so pop */
271 ERROR "argfp underflow" FATAL;
274 } else if (c == '$' && isdigit(*srcp->sp)) { /* $3 */
276 while (isdigit(*srcp->sp))
277 n = 10 * n + *srcp->sp++ - '0';
278 if (n > 0 && n <= MAXARGS)
279 pushsrc(String, argfp->argstk[n-1]);
284 c = getc(curfile->fin);
286 if (curfile == infile)
287 ERROR "end of file inside .G1/.G2" FATAL;
288 if (curfile->fin != stdin) {
289 fclose(curfile->fin);
290 free(curfile->fname); /* assumes allocated */
293 printf(".lf %d %s\n", curfile->lineno, curfile->fname);
295 thru = 0; /* chicken out */
310 void do_thru(void) /* read one line, make into a macro expansion */
318 ERROR "arguments too deep" FATAL;
319 if (ap->argval == NULL)
320 ap->argval = malloc(1000);
324 if (thru == 0) { /* end of file was seen, so thru is done */
328 for ( ; c != '\n' && c != EOF; ) {
329 if (c == ' ' || c == '\t') {
333 if (argcnt >= MAXARGS)
334 ERROR "too many fields on input line" FATAL;
335 ap->argstk[argcnt++] = p;
339 if ((c = nextchar()) == '\\') {
344 } while (c != '"' && c != '\n' && c != EOF);
351 } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
358 ERROR "unexpected end of file in do_thru" FATAL;
359 if (argcnt == 0) { /* ignore blank line */
360 pushsrc(Thru, (char *) 0);
363 for (i = argcnt; i < MAXARGS; i++)
366 for (i = 0; i < argcnt; i++)
367 printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
368 if (strcmp(ap->argstk[0], ".G2") == 0) {
371 pushsrc(String, "\n.G2\n");
374 if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
381 pushsrc(Thru, (char *) 0);
382 dprintf("do_thru pushing back <%s>\n", thrudef->val);
384 pushsrc(Macro, thrudef->val);
389 if (++pb >= pbuf + sizeof pbuf)
390 ERROR "pushback overflow" FATAL;
392 ep = ebuf + sizeof(ebuf) - 1;
403 double errcheck(double x, char *s)
409 ERROR "%s argument out of domain", s WARNING;
410 } else if (errno == ERANGE) {
412 ERROR "%s result out of range", s WARNING;
419 void yyerror(char *s)
421 extern char *cmdname;
422 int ern = errno; /* cause some libraries clobber it */
427 fprintf(stderr, "%s: %s", cmdname, s);
432 fprintf(stderr, " near %s:%d\n",
433 curfile->fname, curfile->lineno+1);
439 void eprint(void) /* try to print context around error */
444 if (p > ebuf && *p == '\n')
446 for ( ; p >= ebuf && *p != '\n'; p--)
450 fprintf(stderr, " context is\n\t");
451 for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
456 fprintf(stderr, " >>> ");
460 fprintf(stderr, " <<< ");
463 fgets(ebuf, sizeof ebuf, curfile->fin);
464 fprintf(stderr, "%s", ebuf);
465 pbstr("\n.G2\n"); /* safety first */
469 int yywrap(void) {return 1;}
471 char *newfile = 0; /* filename for file copy */
472 char *untilstr = 0; /* string that terminates a thru */
473 int thru = 0; /* 1 if copying thru macro */
474 Obj *thrudef = 0; /* macro being used */
476 void copyfile(char *s) /* remember file to start reading from */
481 void copydef(Obj *p) /* remember macro Obj */
486 Obj *copythru(char *s) /* collect the macro name or body for thru */
493 if (p->type == DEFNAME) {
494 p->val = addnewline(p->val);
497 ERROR "%s used as define and name", s FATAL;
499 /* have to collect the definition */
500 pbstr(s); /* first char is the delimiter */
501 q = delimstr("thru body");
502 p = lookup("nameless", 1);
508 p->val = addnewline(p->val);
509 dprintf("installing nameless as `%s'\n", p->val);
513 char *addnewline(char *p) /* add newline to end of p */
518 if (p[n-1] != '\n') {
526 void copyuntil(char *s) /* string that terminates a thru */
531 void copy(void) /* begin input from file, etc. */
536 if ((fin = fopen(newfile, "r")) == NULL)
537 ERROR "can't open file %s", newfile FATAL;
540 curfile->fname = tostring(newfile);
542 printf(".lf 1 %s\n", curfile->fname);
543 pushsrc(File, curfile->fname);
548 begin = 1; /* wrong place */
552 char shellbuf[1000], *shellp;
554 void shell_init(void) /* set up to interpret a shell command */
556 fprintf(tfd, "# shell cmd...\n");
557 sprintf(shellbuf, "rc -c '");
558 shellp = shellbuf + strlen(shellbuf);
561 void shell_text(char *s) /* add string to command being collected */
563 /* fprintf(tfd, "#add <%s> to <%s>\n", s, shellbuf); */
565 if (*s == '\'') { /* protect interior quotes */
574 void shell_exec(void) /* do it */
576 /* fprintf(tfd, "# run <%s>\n", shellbuf); */