Blob


1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <errno.h>
6 #include "grap.h"
7 #include "y.tab.h"
9 Infile infile[10];
10 Infile *curfile = infile;
12 #define MAXSRC 50
13 Src src[MAXSRC]; /* input source stack */
14 Src *srcp = src;
16 void pushsrc(int type, char *ptr) /* new input source */
17 {
18 if (++srcp >= src + MAXSRC)
19 ERROR "inputs nested too deep" FATAL;
20 srcp->type = type;
21 srcp->sp = ptr;
22 if (dbg) {
23 printf("\n%3d ", srcp - src);
24 switch (srcp->type) {
25 case File:
26 printf("push file %s\n", ptr);
27 break;
28 case Macro:
29 printf("push macro <%s>\n", ptr);
30 break;
31 case Char:
32 printf("push char <%c>\n", *ptr);
33 break;
34 case Thru:
35 printf("push thru\n");
36 break;
37 case String:
38 printf("push string <%s>\n", ptr);
39 break;
40 case Free:
41 printf("push free <%s>\n", ptr);
42 break;
43 default:
44 ERROR "pushed bad type %d", srcp->type FATAL;
45 }
46 }
47 }
49 void popsrc(void) /* restore an old one */
50 {
51 if (srcp <= src)
52 ERROR "too many inputs popped" FATAL;
53 if (dbg) {
54 printf("%3d ", srcp - src);
55 switch (srcp->type) {
56 case File:
57 printf("pop file\n");
58 break;
59 case Macro:
60 printf("pop macro\n");
61 break;
62 case Char:
63 printf("pop char <%c>\n", *srcp->sp);
64 break;
65 case Thru:
66 printf("pop thru\n");
67 break;
68 case String:
69 printf("pop string\n");
70 break;
71 case Free:
72 printf("pop free\n");
73 break;
74 default:
75 ERROR "pop weird input %d", srcp->type FATAL;
76 }
77 }
78 srcp--;
79 }
81 void definition(char *s) /* collect definition for s and install */
82 /* definitions picked up lexically */
83 {
84 char *p;
85 Obj *stp;
87 p = delimstr("definition");
88 stp = lookup(s, 0);
89 if (stp != NULL) { /* it's there before */
90 if (stp->type != DEFNAME) {
91 ERROR "%s used as variable and definition", s WARNING;
92 return;
93 }
94 free(stp->val);
95 } else {
96 stp = lookup(s, 1);
97 stp->type = DEFNAME;
98 }
99 stp->val = p;
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;
108 static int nbuf = 0;
109 char *p;
111 if (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 "(){}[]`'" */
116 deep = 1;
117 for (p = buf; ; ) {
118 c = input();
119 if (c == rdelim)
120 if (--deep == 0)
121 break;
122 if (c == delim)
123 deep++;
124 if (p >= buf + nbuf) {
125 n = p - buf;
126 buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
127 p = buf + n;
129 if (c == EOF)
130 ERROR "end of file in %s %c %.20s... %c", s, delim, buf, delim FATAL;
131 *p++ = c;
133 *p = '\0';
134 dprintf("delimstr %s %c <%s> %c\n", s, delim, buf, delim);
135 return tostring(buf);
138 int
139 baldelim(int c, char *s) /* replace c by balancing entry in s */
141 for ( ; *s; s += 2)
142 if (*s == c)
143 return s[1];
144 return c;
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 */
153 int i, len;
154 char *p;
155 Arg *ap;
157 ap = argfp+1;
158 if (ap >= args+10)
159 ERROR "arguments too deep" FATAL;
160 argcnt = 0;
161 if (input() != '(')
162 ERROR "disaster in dodef" FATAL;
163 if (ap->argval == 0)
164 ap->argval = malloc(1000);
165 for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
166 ap->argstk[argcnt++] = p;
167 if (input() == ')')
168 break;
170 for (i = argcnt; i < MAXARGS; i++)
171 ap->argstk[i] = "";
172 if (dbg)
173 for (i = 0; i < argcnt; i++)
174 printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
175 argfp = ap;
176 pushsrc(Macro, stp->val);
179 int
180 getarg(char *p) /* pick up single argument, store in p, return length */
182 int n, c, npar;
184 n = npar = 0;
185 for ( ;; ) {
186 c = input();
187 if (c == EOF)
188 ERROR "end of file in getarg!" FATAL;
189 if (npar == 0 && (c == ',' || c == ')'))
190 break;
191 if (c == '"') /* copy quoted stuff intact */
192 do {
193 *p++ = c;
194 n++;
195 } while ((c = input()) != '"' && c != EOF);
196 else if (c == '(')
197 npar++;
198 else if (c == ')')
199 npar--;
200 n++;
201 *p++ = c;
203 *p = 0;
204 unput(c);
205 return(n + 1);
208 #define PBSIZE 2000
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 */
213 char *ep = ebuf;
215 int begin = 0;
216 extern int thru;
217 extern Obj *thrudef;
218 extern char *untilstr;
220 int
221 input(void)
223 register int c;
225 if (thru && begin) {
226 do_thru();
227 begin = 0;
229 c = nextchar();
230 dprintf(" <%c>", c);
231 if (ep >= ebuf + sizeof ebuf)
232 ep = ebuf;
233 return *ep++ = c;
236 int
237 nextchar(void)
239 register int c;
241 c = 0; /* gcc */
243 loop:
244 switch (srcp->type) {
245 case Free: /* free string */
246 free(srcp->sp);
247 popsrc();
248 goto loop;
249 case Thru: /* end of pushed back line */
250 begin = 1;
251 popsrc();
252 c = '\n';
253 break;
254 case Char:
255 if (pb >= pbuf) {
256 c = *pb--;
257 popsrc();
258 break;
259 } else { /* can't happen? */
260 popsrc();
261 goto loop;
263 case String:
264 c = *srcp->sp++;
265 if (c == '\0') {
266 popsrc();
267 goto loop;
268 } else {
269 if (*srcp->sp == '\0') /* empty, so pop */
270 popsrc();
271 break;
273 case Macro:
274 c = *srcp->sp++;
275 if (c == '\0') {
276 if (--argfp < args)
277 ERROR "argfp underflow" FATAL;
278 popsrc();
279 goto loop;
280 } else if (c == '$' && isdigit(*srcp->sp)) { /* $3 */
281 int n = 0;
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]);
286 goto loop;
288 break;
289 case File:
290 c = getc(curfile->fin);
291 if (c == EOF) {
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 */
298 curfile--;
299 printf(".lf %d %s\n", curfile->lineno, curfile->fname);
300 popsrc();
301 thru = 0; /* chicken out */
302 thrudef = 0;
303 if (untilstr) {
304 free(untilstr);
305 untilstr = 0;
307 goto loop;
309 if (c == '\n')
310 curfile->lineno++;
311 break;
313 return c;
316 void do_thru(void) /* read one line, make into a macro expansion */
318 int c, i;
319 char *p;
320 Arg *ap;
322 ap = argfp+1;
323 if (ap >= args+10)
324 ERROR "arguments too deep" FATAL;
325 if (ap->argval == NULL)
326 ap->argval = malloc(1000);
327 p = ap->argval;
328 argcnt = 0;
329 c = nextchar();
330 if (thru == 0) { /* end of file was seen, so thru is done */
331 unput(c);
332 return;
334 for ( ; c != '\n' && c != EOF; ) {
335 if (c == ' ' || c == '\t') {
336 c = nextchar();
337 continue;
339 if (argcnt >= MAXARGS)
340 ERROR "too many fields on input line" FATAL;
341 ap->argstk[argcnt++] = p;
342 if (c == '"') {
343 do {
344 *p++ = c;
345 if ((c = nextchar()) == '\\') {
346 *p++ = c;
347 *p++ = nextchar();
348 c = nextchar();
350 } while (c != '"' && c != '\n' && c != EOF);
351 *p++ = '"';
352 if (c == '"')
353 c = nextchar();
354 } else {
355 do {
356 *p++ = c;
357 } while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
358 if (c == ',')
359 c = nextchar();
361 *p++ = '\0';
363 if (c == EOF)
364 ERROR "unexpected end of file in do_thru" FATAL;
365 if (argcnt == 0) { /* ignore blank line */
366 pushsrc(Thru, (char *) 0);
367 return;
369 for (i = argcnt; i < MAXARGS; i++)
370 ap->argstk[i] = "";
371 if (dbg)
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) {
375 thru = 0;
376 thrudef = 0;
377 pushsrc(String, "\n.G2\n");
378 return;
380 if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
381 thru = 0;
382 thrudef = 0;
383 free(untilstr);
384 untilstr = 0;
385 return;
387 pushsrc(Thru, (char *) 0);
388 dprintf("do_thru pushing back <%s>\n", thrudef->val);
389 argfp = ap;
390 pushsrc(Macro, thrudef->val);
393 int
394 unput(int c)
396 if (++pb >= pbuf + sizeof pbuf)
397 ERROR "pushback overflow" FATAL;
398 if (--ep < ebuf)
399 ep = ebuf + sizeof(ebuf) - 1;
400 *pb = c;
401 pushsrc(Char, pb);
402 return c;
405 void pbstr(char *s)
407 pushsrc(String, s);
410 double errcheck(double x, char *s)
412 if (errno == EDOM) {
413 errno = 0;
414 ERROR "%s argument out of domain", s WARNING;
415 } else if (errno == ERANGE) {
416 errno = 0;
417 ERROR "%s result out of range", s WARNING;
419 return x;
422 char errbuf[200];
424 void yyerror(char *s)
426 extern char *cmdname;
427 int ern = errno; /* cause some libraries clobber it */
429 if (synerr)
430 return;
431 fflush(stdout);
432 fprintf(stderr, "%s: %s", cmdname, s);
433 if (ern > 0) {
434 errno = ern;
435 perror("???");
437 fprintf(stderr, " near %s:%d\n",
438 curfile->fname, curfile->lineno+1);
439 eprint();
440 synerr = 1;
441 errno = 0;
444 void eprint(void) /* try to print context around error */
446 char *p, *q;
448 p = ep - 1;
449 if (p > ebuf && *p == '\n')
450 p--;
451 for ( ; p >= ebuf && *p != '\n'; p--)
453 while (*p == '\n')
454 p++;
455 fprintf(stderr, " context is\n\t");
456 for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
458 for (; p < q; p++)
459 if (isprint(*p))
460 putc(*p, stderr);
461 fprintf(stderr, " >>> ");
462 for (; p < q; p++)
463 if (isprint(*p))
464 putc(*p, stderr);
465 fprintf(stderr, " <<< ");
466 while (pb >= pbuf)
467 putc(*pb--, stderr);
468 fgets(ebuf, sizeof ebuf, curfile->fin);
469 fprintf(stderr, "%s", ebuf);
470 pbstr("\n.G2\n"); /* safety first */
471 ep = ebuf;
474 int yywrap(void) {return 1;}
476 char *newfile = 0; /* filename for file copy */
477 char *untilstr = 0; /* string that terminates a thru */
478 int thru = 0; /* 1 if copying thru macro */
479 Obj *thrudef = 0; /* macro being used */
481 void copyfile(char *s) /* remember file to start reading from */
483 newfile = s;
486 void copydef(Obj *p) /* remember macro Obj */
488 thrudef = p;
491 Obj *copythru(char *s) /* collect the macro name or body for thru */
493 Obj *p;
494 char *q;
496 p = lookup(s, 0);
497 if (p != NULL) {
498 if (p->type == DEFNAME) {
499 p->val = addnewline(p->val);
500 return p;
501 } else
502 ERROR "%s used as define and name", s FATAL;
504 /* have to collect the definition */
505 pbstr(s); /* first char is the delimiter */
506 q = delimstr("thru body");
507 p = lookup("nameless", 1);
508 if (p != NULL)
509 if (p->val)
510 free(p->val);
511 p->type = DEFNAME;
512 p->val = q;
513 p->val = addnewline(p->val);
514 dprintf("installing nameless as `%s'\n", p->val);
515 return p;
518 char *addnewline(char *p) /* add newline to end of p */
520 int n;
522 n = strlen(p);
523 if (p[n-1] != '\n') {
524 p = realloc(p, n+2);
525 p[n] = '\n';
526 p[n+1] = '\0';
528 return p;
531 void copyuntil(char *s) /* string that terminates a thru */
533 untilstr = s;
536 void copy(void) /* begin input from file, etc. */
538 FILE *fin;
540 if (newfile) {
541 if ((fin = fopen(newfile, "r")) == NULL)
542 ERROR "can't open file %s", newfile FATAL;
543 curfile++;
544 curfile->fin = fin;
545 curfile->fname = tostring(newfile);
546 curfile->lineno = 0;
547 printf(".lf 1 %s\n", curfile->fname);
548 pushsrc(File, curfile->fname);
549 newfile = 0;
551 if (thrudef) {
552 thru = 1;
553 begin = 1; /* wrong place */
557 char shellbuf[1000], *shellp;
559 void shell_init(void) /* set up to interpret a shell command */
561 fprintf(tfd, "# shell cmd...\n");
562 sprintf(shellbuf, "rc -c '");
563 shellp = shellbuf + strlen(shellbuf);
566 void shell_text(char *s) /* add string to command being collected */
568 /* fprintf(tfd, "#add <%s> to <%s>\n", s, shellbuf); */
569 while (*s) {
570 if (*s == '\'') { /* protect interior quotes */
571 *shellp++ = '\'';
572 *shellp++ = '\\';
573 *shellp++ = '\'';
575 *shellp++ = *s++;
579 void shell_exec(void) /* do it */
581 /* fprintf(tfd, "# run <%s>\n", shellbuf); */
582 *shellp++ = '\'';
583 *shellp = '\0';
584 system(shellbuf);