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