Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <ctype.h>
5
#include <errno.h>
6
#include "pic.h"
7
#include "y.tab.h"
8
 
9
Infile	infile[10];
10
Infile	*curfile = infile;
11
 
12
#define	MAXSRC	50
13
Src	src[MAXSRC];	/* input source stack */
14
Src	*srcp	= src;
15
 
16
void	do_thru(void);
17
int	nextchar(void);
18
int	getarg(char *);
19
void	freedef(char *);
20
int	baldelim(int, char *);
21
 
22
void pushsrc(int type, char *ptr)	/* new input source */
23
{
24
	if (++srcp >= src + MAXSRC)
25
		ERROR "inputs nested too deep" FATAL;
26
	srcp->type = type;
27
	srcp->sp = ptr;
28
	if (dbg > 1) {
29
		printf("\n%3d ", srcp - src);
30
		switch (srcp->type) {
31
		case File:
32
			printf("push file %s\n", ((Infile *)ptr)->fname);
33
			break;
34
		case Macro:
35
			printf("push macro <%s>\n", ptr);
36
			break;
37
		case Char:
38
			printf("push char <%c>\n", *ptr);
39
			break;
40
		case Thru:
41
			printf("push thru\n");
42
			break;
43
		case String:
44
			printf("push string <%s>\n", ptr);
45
			break;
46
		case Free:
47
			printf("push free <%s>\n", ptr);
48
			break;
49
		default:
50
			ERROR "pushed bad type %d", srcp->type FATAL;
51
		}
52
	}
53
}
54
 
55
void popsrc(void)	/* restore an old one */
56
{
57
	if (srcp <= src)
58
		ERROR "too many inputs popped" FATAL;
59
	if (dbg > 1) {
60
		printf("%3d ", srcp - src);
61
		switch (srcp->type) {
62
		case File:
63
			printf("pop file\n");
64
			break;
65
		case Macro:
66
			printf("pop macro\n");
67
			break;
68
		case Char:
69
			printf("pop char <%c>\n", *srcp->sp);
70
			break;
71
		case Thru:
72
			printf("pop thru\n");
73
			break;
74
		case String:
75
			printf("pop string\n");
76
			break;
77
		case Free:
78
			printf("pop free\n");
79
			break;
80
		default:
81
			ERROR "pop weird input %d", srcp->type FATAL;
82
		}
83
	}
84
	srcp--;
85
}
86
 
87
void definition(char *s)	/* collect definition for s and install */
88
				/* definitions picked up lexically */
89
{
90
	char *p;
91
	struct symtab *stp;
92
 
93
	p = delimstr("definition");
94
	stp = lookup(s);
95
	if (stp != NULL) {	/* it's there before */
96
		if (stp->s_type != DEFNAME) {
97
			ERROR "%s used as variable and definition", s WARNING;
98
			return;
99
		}
100
		free(stp->s_val.p);
101
		stp->s_val.p = p;
102
	} else {
103
		YYSTYPE u;
104
		u.p = p;
105
		makevar(tostring(s), DEFNAME, u);
106
	}
107
	dprintf("installing %s as `%s'\n", s, p);
108
}
109
 
110
char *delimstr(char *s)	/* get body of X ... X */
111
				/* message if too big */
112
{
113
	int c, delim, rdelim, n, deep;
114
	static char *buf = NULL;
115
	static int nbuf = 0;
116
	char *p;
117
 
118
	if (buf == NULL)
119
		buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
120
	while ((delim = input()) == ' ' || delim == '\t' || delim == '\n')
121
		;
122
	rdelim = baldelim(delim, "{}");		/* could be "(){}[]`'" */
123
	deep = 1;
124
	for (p = buf; ; ) {
125
		c = input();
126
		if (c == rdelim)
127
			if (--deep == 0)
128
				break;
129
		if (c == delim)
130
			deep++;
131
		if (p >= buf + nbuf) {
132
			n = p - buf;
133
			buf = grow(buf, "buf", nbuf += 1000, sizeof(buf[0]));
134
			p = buf + n;
135
		}
136
		if (c == EOF)
137
			ERROR "end of file in %s %c %.20s... %c", s, delim, buf, delim FATAL;
138
		*p++ = c;
139
	}
140
	*p = '\0';
141
	dprintf("delimstr %s %c <%s> %c\n", s, delim, buf, delim);
142
	return tostring(buf);
143
}
144
 
145
baldelim(int c, char *s)	/* replace c by balancing entry in s */
146
{
147
	for ( ; *s; s += 2)
148
		if (*s == c)
149
			return s[1];
150
	return c;
151
}
152
 
153
void undefine(char *s)	/* undefine macro */
154
{
155
	while (*s != ' ' && *s != '\t')		/* skip "undef..." */
156
		s++;
157
	while (*s == ' ' || *s == '\t')
158
		s++;
159
	freedef(s);
160
}
161
 
162
 
163
Arg	args[10];	/* argument frames */
164
Arg	*argfp = args;	/* frame pointer */
165
int	argcnt;		/* number of arguments seen so far */
166
 
167
void dodef(struct symtab *stp)	/* collect args and switch input to defn */
168
{
169
	int i, len;
170
	char *p;
171
	Arg *ap;
172
 
173
	ap = argfp+1;
174
	if (ap >= args+10)
175
		ERROR "arguments too deep" FATAL;
176
	argcnt = 0;
177
	if (input() != '(')
178
		ERROR "disaster in dodef" FATAL;
179
	if (ap->argval == 0)
180
		ap->argval = malloc(1000);
181
	for (p = ap->argval; (len = getarg(p)) != -1; p += len) {
182
		ap->argstk[argcnt++] = p;
183
		if (input() == ')')
184
			break;
185
	}
186
	for (i = argcnt; i < MAXARGS; i++)
187
		ap->argstk[i] = "";
188
	if (dbg)
189
		for (i = 0; i < argcnt; i++)
190
			printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
191
	argfp = ap;
192
	pushsrc(Macro, stp->s_val.p);
193
}
194
 
195
getarg(char *p)	/* pick up single argument, store in p, return length */
196
{
197
	int n, c, npar;
198
 
199
	n = npar = 0;
200
	for ( ;; ) {
201
		c = input();
202
		if (c == EOF)
203
			ERROR "end of file in getarg" FATAL;
204
		if (npar == 0 && (c == ',' || c == ')'))
205
			break;
206
		if (c == '"')	/* copy quoted stuff intact */
207
			do {
208
				*p++ = c;
209
				n++;
210
			} while ((c = input()) != '"' && c != EOF);
211
		else if (c == '(')
212
			npar++;
213
		else if (c == ')')
214
			npar--;
215
		n++;
216
		*p++ = c;
217
	}
218
	*p = 0;
219
	unput(c);
220
	return(n + 1);
221
}
222
 
223
#define	PBSIZE	2000
224
char	pbuf[PBSIZE];		/* pushback buffer */
225
char	*pb	= pbuf-1;	/* next pushed back character */
226
 
227
char	ebuf[200];		/* collect input here for error reporting */
228
char	*ep	= ebuf;
229
 
230
int	begin	= 0;
231
extern	int	thru;
232
extern	struct symtab	*thrudef;
233
extern	char	*untilstr;
234
 
235
input(void)
236
{
237
	register int c;
238
 
239
	if (thru && begin) {
240
		do_thru();
241
		begin = 0;
242
	}
243
	c = nextchar();
244
	if (dbg > 1)
245
		printf(" <%c>", c);
246
	if (ep >= ebuf + sizeof ebuf)
247
		ep = ebuf;
248
	return *(unsigned char *)ep++ = c;
249
}
250
 
251
nextchar(void)
252
{
253
	register int c;
254
 
255
	c = 0;
256
  loop:
257
	switch (srcp->type) {
258
	case Free:	/* free string */
259
		free(srcp->sp);
260
		popsrc();
261
		goto loop;
262
	case Thru:	/* end of pushed back line */
263
		begin = 1;
264
		popsrc();
265
		c = '\n';
266
		break;
267
	case Char:
268
		if (pb >= pbuf) {
269
			c = *pb--;
270
			popsrc();
271
			break;
272
		} else {	/* can't happen? */
273
			popsrc();
274
			goto loop;
275
		}
276
	case String:
277
		c = *srcp->sp++;
278
		if (c == '\0') {
279
			popsrc();
280
			goto loop;
281
		} else {
282
			if (*srcp->sp == '\0')	/* empty, so pop */
283
				popsrc();
284
			break;
285
		}
286
	case Macro:
287
		c = *srcp->sp++;
288
		if (c == '\0') {
289
			if (--argfp < args)
290
				ERROR "argfp underflow" FATAL;
291
			popsrc();
292
			goto loop;
293
		} else if (c == '$' && isdigit(*srcp->sp)) {
294
			int n = 0;
295
			while (isdigit(*srcp->sp))
296
				n = 10 * n + *srcp->sp++ - '0';
297
			if (n > 0 && n <= MAXARGS)
298
				pushsrc(String, argfp->argstk[n-1]);
299
			goto loop;
300
		}
301
		break;
302
	case File:
303
		c = getc(curfile->fin);
304
		if (c == EOF) {
305
			if (curfile == infile)
306
				ERROR "end of file inside .PS/.PE" FATAL;
307
			if (curfile->fin != stdin) {
308
				fclose(curfile->fin);
309
				free(curfile->fname);	/* assumes allocated */
310
			}
311
			curfile--;
312
			printlf(curfile->lineno, curfile->fname);
313
			popsrc();
314
			thru = 0;	/* chicken out */
315
			thrudef = 0;
316
			if (untilstr) {
317
				free(untilstr);
318
				untilstr = 0;
319
			}
320
			goto loop;
321
		}
322
		if (c == '\n')
323
			curfile->lineno++;
324
		break;
325
	}
326
	return c;
327
}
328
 
329
void do_thru(void)	/* read one line, make into a macro expansion */
330
{
331
	int c, i;
332
	char *p;
333
	Arg *ap;
334
 
335
	ap = argfp+1;
336
	if (ap >= args+10)
337
		ERROR "arguments too deep" FATAL;
338
	if (ap->argval == NULL)
339
		ap->argval = malloc(1000);
340
	p = ap->argval;
341
	argcnt = 0;
342
	c = nextchar();
343
	if (thru == 0) {	/* end of file was seen, so thru is done */
344
		unput(c);
345
		return;
346
	}
347
	for ( ; c != '\n' && c != EOF; ) {
348
		if (c == ' ' || c == '\t') {
349
			c = nextchar();
350
			continue;
351
		}
352
		ap->argstk[argcnt++] = p;
353
		if (c == '"') {
354
			do {
355
				*p++ = c;
356
				if ((c = nextchar()) == '\\') {
357
					*p++ = c;
358
					*p++ = nextchar();
359
					c = nextchar();
360
				}
361
			} while (c != '"' && c != '\n' && c != EOF);
362
			*p++ = '"';
363
			if (c == '"')
364
				c = nextchar();
365
		} else {
366
			do {
367
				*p++ = c;
368
			} while ((c = nextchar())!=' ' && c!='\t' && c!='\n' && c!=',' && c!=EOF);
369
			if (c == ',')
370
				c = nextchar();
371
		}
372
		*p++ = '\0';
373
	}
374
	if (c == EOF)
375
		ERROR "unexpected end of file in do_thru" FATAL;
376
	if (argcnt == 0) {	/* ignore blank line */
377
		pushsrc(Thru, (char *) 0);
378
		return;
379
	}
380
	for (i = argcnt; i < MAXARGS; i++)
381
		ap->argstk[i] = "";
382
	if (dbg)
383
		for (i = 0; i < argcnt; i++)
384
			printf("arg %d.%d = <%s>\n", ap-args, i+1, ap->argstk[i]);
385
	if (strcmp(ap->argstk[0], ".PE") == 0) {
386
		thru = 0;
387
		thrudef = 0;
388
		pushsrc(String, "\n.PE\n");
389
		return;
390
	}
391
	if (untilstr && strcmp(ap->argstk[0], untilstr) == 0) {
392
		thru = 0;
393
		thrudef = 0;
394
		free(untilstr);
395
		untilstr = 0;
396
		return;
397
	}
398
	pushsrc(Thru, (char *) 0);
399
	dprintf("do_thru pushing back <%s>\n", thrudef->s_val.p);
400
	argfp = ap;
401
	pushsrc(Macro, thrudef->s_val.p);
402
}
403
 
404
unput(int c)
405
{
406
	if (++pb >= pbuf + sizeof pbuf)
407
		ERROR "pushback overflow" FATAL;
408
	if (--ep < ebuf)
409
		ep = ebuf + sizeof(ebuf) - 1;
410
	*pb = c;
411
	pushsrc(Char, pb);
412
	return c;
413
}
414
 
415
void pbstr(char *s)
416
{
417
	pushsrc(String, s);
418
}
419
 
420
double errcheck(double x, char  *s)
421
{
422
	extern int errno;
423
 
424
	if (errno == EDOM) {
425
		errno = 0;
426
		ERROR "%s argument out of domain", s WARNING;
427
	} else if (errno == ERANGE) {
428
		errno = 0;
429
		ERROR "%s result out of range", s WARNING;
430
	}
431
	return x;
432
}
433
 
434
char	errbuf[200];
435
 
436
void	eprint(void);
437
 
438
void yyerror(char *s)
439
{
440
	extern char *cmdname;
441
	int ern = errno;	/* cause some libraries clobber it */
442
 
443
	if (synerr)
444
		return;
445
	fflush(stdout);
446
	fprintf(stderr, "%s: %s", cmdname, s);
447
	if (ern > 0) {
448
		errno = ern;
449
		perror("???");
450
	}
451
	fprintf(stderr, " near %s:%d\n",
452
		curfile->fname, curfile->lineno+1);
453
	eprint();
454
	synerr = 1;
455
	errno = 0;
456
}
457
 
458
void eprint(void)	/* try to print context around error */
459
{
460
	char *p, *q;
461
 
462
	p = ep - 1;
463
	if (p > ebuf && *p == '\n')
464
		p--;
465
	for ( ; p >= ebuf && *p != '\n'; p--)
466
		;
467
	while (*p == '\n')
468
		p++;
469
	fprintf(stderr, " context is\n\t");
470
	for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
471
		;
472
	while (p < q)
473
		putc(*p++, stderr);
474
	fprintf(stderr, " >>> ");
475
	while (p < ep)
476
		putc(*p++, stderr);
477
	fprintf(stderr, " <<< ");
478
	while (pb >= pbuf)
479
		putc(*pb--, stderr);
480
	fgets(ebuf, sizeof ebuf, curfile->fin);
481
	fprintf(stderr, "%s", ebuf);
482
	pbstr("\n.PE\n");	/* safety first */
483
	ep = ebuf;
484
}
485
 
486
int
487
yywrap(void)
488
{
489
	return 1;		/* read eof; did not switch inputs */
490
}
491
 
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 */
496
 
497
void copyfile(char *s)	/* remember file to start reading from */
498
{
499
	newfile = s;
500
}
501
 
502
void copydef(struct symtab *p)	/* remember macro symtab ptr */
503
{
504
	thrudef = p;
505
}
506
 
507
struct symtab *copythru(char *s)	/* collect the macro name or body for thru */
508
{
509
	struct symtab *p;
510
	char *q, *addnewline(char *);
511
 
512
	p = lookup(s);
513
	if (p != NULL) {
514
		if (p->s_type == DEFNAME) {
515
			p->s_val.p = addnewline(p->s_val.p);
516
			return p;
517
		} else
518
			ERROR "%s used as define and name", s FATAL;
519
	}
520
	/* have to collect the definition */
521
	pbstr(s);	/* first char is the delimiter */
522
	q = delimstr("thru body");
523
	s = "nameless";
524
	p = lookup(s);
525
	if (p != NULL) {
526
		if (p->s_val.p)
527
			free(p->s_val.p);
528
		p->s_val.p = q;
529
	} else {
530
		YYSTYPE u;
531
		u.p = q;
532
		p = makevar(tostring(s), DEFNAME, u);
533
	}
534
	p->s_val.p = addnewline(p->s_val.p);
535
	dprintf("installing %s as `%s'\n", s, p->s_val.p);
536
	return p;
537
}
538
 
539
char *addnewline(char *p)	/* add newline to end of p */
540
{
541
	int n;
542
 
543
	n = strlen(p);
544
	if (p[n-1] != '\n') {
545
		p = realloc(p, n+2);
546
		p[n] = '\n';
547
		p[n+1] = '\0';
548
	}
549
	return p;
550
}
551
 
552
void copyuntil(char *s)	/* string that terminates a thru */
553
{
554
	untilstr = s;
555
}
556
 
557
void copy(void)	/* begin input from file, etc. */
558
{
559
	FILE *fin;
560
 
561
	if (newfile) {
562
		if ((fin = fopen(newfile, "r")) == NULL)
563
			ERROR "can't open file %s", newfile FATAL;
564
		curfile++;
565
		curfile->fin = fin;
566
		curfile->fname = newfile;
567
		curfile->lineno = 0;
568
		printlf(1, curfile->fname);
569
		pushsrc(File, curfile->fname);
570
		newfile = 0;
571
	}
572
	if (thrudef) {
573
		thru = 1;
574
		begin = 1;	/* wrong place */
575
	}
576
}
577
 
578
char	shellbuf[1000], *shellp;
579
 
580
void shell_init(void)	/* set up to interpret a shell command */
581
{
582
	sprintf(shellbuf, "rc -c '");
583
	shellp = shellbuf + strlen(shellbuf);
584
}
585
 
586
void shell_text(char *s)	/* add string to command being collected */
587
{
588
	while (*shellp++ = *s++)
589
		;
590
	shellp--;
591
}
592
 
593
void shell_exec(void)	/* do it */
594
{
595
	*shellp++ = '\'';
596
	*shellp = '\0';
597
	system(shellbuf);
598
}