11 Infile *curfile = infile;
14 Src src[MAXSRC]; /* input source stack */
17 void pushsrc(int type, char *ptr) /* new input source */
19 if (++srcp >= src + MAXSRC)
20 ERROR "inputs nested too deep" FATAL;
24 printf("\n%3ld ", (long)(srcp - src));
27 printf("push file %s\n", ptr);
30 printf("push macro <%s>\n", ptr);
33 printf("push char <%c>\n", *ptr);
36 printf("push thru\n");
39 printf("push string <%s>\n", ptr);
42 printf("push free <%s>\n", ptr);
45 ERROR "pushed bad type %d", srcp->type FATAL;
50 void 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;
82 void definition(char *s) /* collect definition for s and install */
83 /* definitions picked up lexically */
88 p = delimstr("definition");
90 if (stp != NULL) { /* it's there before */
91 if (stp->type != DEFNAME) {
92 ERROR "%s used as variable and definition", s WARNING;
101 dprintf("installing %s as `%s'\n", s, p);
104 char *delimstr(char *s) /* get body of X ... X */
105 /* message if too big */
107 int c, delim, rdelim, n, deep;
108 static char *buf = NULL;
113 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
114 while ((delim = input()) == ' ' || delim == '\t' || delim == '\n')
116 rdelim = baldelim(delim, "{}"); /* could be "(){}[]`'" */
125 if (p >= buf + nbuf) {
127 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
131 ERROR "end of file in %s %c %.20s... %c", s, delim, buf, delim FATAL;
135 dprintf("delimstr %s %c <%s> %c\n", s, delim, buf, delim);
136 return tostring(buf);
140 baldelim(int c, char *s) /* replace c by balancing entry in s */
148 Arg args[10]; /* argument frames */
149 Arg *argfp = args; /* frame pointer */
150 int argcnt; /* number of arguments seen so far */
152 void dodef(Obj *stp) /* collect args and switch input to defn */
160 ERROR "arguments too deep" FATAL;
163 ERROR "disaster in dodef" FATAL;
165 ap->argval = malloc(1000);
166 for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
167 ap->argstk[argcnt++] = p;
171 for (i = argcnt; i < MAXARGS; i++)
174 for (i = 0; i < argcnt; i++)
175 printf("arg %ld.%d = <%s>\n", (long)(ap-args), i+1, ap->argstk[i]);
177 pushsrc(Macro, stp->val);
181 getarg(char *p) /* pick up single argument, store in p, return length */
189 ERROR "end of file in getarg!" FATAL;
190 if (npar == 0 && (c == ',' || c == ')'))
192 if (c == '"') /* copy quoted stuff intact */
196 } while ((c = input()) != '"' && c != EOF);
210 char pbuf[PBSIZE]; /* pushback buffer */
211 char *pb = pbuf-1; /* next pushed back character */
213 char ebuf[200]; /* collect input here for error reporting */
219 extern char *untilstr;
232 if (ep >= ebuf + sizeof ebuf)
245 switch (srcp->type) {
246 case Free: /* free string */
250 case Thru: /* end of pushed back line */
260 } else { /* can't happen? */
270 if (*srcp->sp == '\0') /* empty, so pop */
278 ERROR "argfp underflow" FATAL;
281 } else if (c == '$' && isdigit((uchar)*srcp->sp)) { /* $3 */
283 while (isdigit((uchar)*srcp->sp))
284 n = 10 * n + *srcp->sp++ - '0';
285 if (n > 0 && n <= MAXARGS)
286 pushsrc(String, argfp->argstk[n-1]);
291 c = getc(curfile->fin);
293 if (curfile == infile)
294 ERROR "end of file inside .G1/.G2" FATAL;
295 if (curfile->fin != stdin) {
296 fclose(curfile->fin);
297 free(curfile->fname); /* assumes allocated */
300 printf(".lf %d %s\n", curfile->lineno, curfile->fname);
302 thru = 0; /* chicken out */
317 void do_thru(void) /* read one line, make into a macro expansion */
325 ERROR "arguments too deep" FATAL;
326 if (ap->argval == NULL)
327 ap->argval = malloc(1000);
331 if (thru == 0) { /* end of file was seen, so thru is done */
335 for ( ; c != '\n' && c != EOF; ) {
336 if (c == ' ' || c == '\t') {
340 if (argcnt >= MAXARGS)
341 ERROR "too many fields on input line" FATAL;
342 ap->argstk[argcnt++] = p;
346 if ((c = nextchar()) == '\\') {
351 } while (c != '"' && c != '\n' && c != EOF);
358 } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
365 ERROR "unexpected end of file in do_thru" FATAL;
366 if (argcnt == 0) { /* ignore blank line */
367 pushsrc(Thru, (char *) 0);
370 for (i = argcnt; i < MAXARGS; i++)
373 for (i = 0; i < argcnt; i++)
374 printf("arg %ld.%d = <%s>\n", (long)(ap-args), i+1, ap->argstk[i]);
375 if (strcmp(ap->argstk[0], ".G2") == 0) {
378 pushsrc(String, "\n.G2\n");
381 if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
388 pushsrc(Thru, (char *) 0);
389 dprintf("do_thru pushing back <%s>\n", thrudef->val);
391 pushsrc(Macro, thrudef->val);
397 if (++pb >= pbuf + sizeof pbuf)
398 ERROR "pushback overflow" FATAL;
400 ep = ebuf + sizeof(ebuf) - 1;
411 double errcheck(double x, char *s)
415 ERROR "%s argument out of domain", s WARNING;
416 } else if (errno == ERANGE) {
418 ERROR "%s result out of range", s WARNING;
425 void yyerror(char *s)
427 extern char *cmdname;
428 int ern = errno; /* cause some libraries clobber it */
433 fprintf(stderr, "%s: %s", cmdname, s);
438 fprintf(stderr, " near %s:%d\n",
439 curfile->fname, curfile->lineno+1);
445 void eprint(void) /* try to print context around error */
450 if (p > ebuf && *p == '\n')
452 for ( ; p >= ebuf && *p != '\n'; p--)
456 fprintf(stderr, " context is\n\t");
457 for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
460 if (isprint((uchar)*p))
462 fprintf(stderr, " >>> ");
464 if (isprint((uchar)*p))
466 fprintf(stderr, " <<< ");
469 fgets(ebuf, sizeof ebuf, curfile->fin);
470 fprintf(stderr, "%s", ebuf);
471 pbstr("\n.G2\n"); /* safety first */
475 int yywrap(void) {return 1;}
477 char *newfile = 0; /* filename for file copy */
478 char *untilstr = 0; /* string that terminates a thru */
479 int thru = 0; /* 1 if copying thru macro */
480 Obj *thrudef = 0; /* macro being used */
482 void copyfile(char *s) /* remember file to start reading from */
487 void copydef(Obj *p) /* remember macro Obj */
492 Obj *copythru(char *s) /* collect the macro name or body for thru */
499 if (p->type == DEFNAME) {
500 p->val = addnewline(p->val);
503 ERROR "%s used as define and name", s FATAL;
505 /* have to collect the definition */
506 pbstr(s); /* first char is the delimiter */
507 q = delimstr("thru body");
508 p = lookup("nameless", 1);
514 p->val = addnewline(p->val);
515 dprintf("installing nameless as `%s'\n", p->val);
519 char *addnewline(char *p) /* add newline to end of p */
524 if (p[n-1] != '\n') {
532 void copyuntil(char *s) /* string that terminates a thru */
537 void copy(void) /* begin input from file, etc. */
542 if ((fin = fopen(newfile, "r")) == NULL)
543 ERROR "can't open file %s", newfile FATAL;
546 curfile->fname = tostring(newfile);
548 printf(".lf 1 %s\n", curfile->fname);
549 pushsrc(File, curfile->fname);
554 begin = 1; /* wrong place */
558 char shellbuf[1000], *shellp;
560 void shell_init(void) /* set up to interpret a shell command */
562 fprintf(tfd, "# shell cmd...\n");
563 sprintf(shellbuf, "rc -c '");
564 shellp = shellbuf + strlen(shellbuf);
567 void shell_text(char *s) /* add string to command being collected */
569 /* fprintf(tfd, "#add <%s> to <%s>\n", s, shellbuf); */
571 if (*s == '\'') { /* protect interior quotes */
580 void shell_exec(void) /* do it */
582 /* fprintf(tfd, "# run <%s>\n", shellbuf); */