xref: /minix3/bin/ksh/tree.c (revision 2718b5688b1550d32bf379153192626eee37752d)
1*2718b568SThomas Cort /*	$NetBSD: tree.c,v 1.6 2005/06/26 19:09:00 christos Exp $	*/
2*2718b568SThomas Cort 
3*2718b568SThomas Cort /*
4*2718b568SThomas Cort  * command tree climbing
5*2718b568SThomas Cort  */
6*2718b568SThomas Cort #include <sys/cdefs.h>
7*2718b568SThomas Cort 
8*2718b568SThomas Cort #ifndef lint
9*2718b568SThomas Cort __RCSID("$NetBSD: tree.c,v 1.6 2005/06/26 19:09:00 christos Exp $");
10*2718b568SThomas Cort #endif
11*2718b568SThomas Cort 
12*2718b568SThomas Cort 
13*2718b568SThomas Cort #include "sh.h"
14*2718b568SThomas Cort 
15*2718b568SThomas Cort #define INDENT	4
16*2718b568SThomas Cort 
17*2718b568SThomas Cort #define tputc(c, shf)	shf_putchar(c, shf);
18*2718b568SThomas Cort static void 	ptree ARGS((struct op *t, int indent, struct shf *f));
19*2718b568SThomas Cort static void 	pioact ARGS((struct shf *f, int indent, struct ioword *iop));
20*2718b568SThomas Cort static void	tputC ARGS((int c, struct shf *shf));
21*2718b568SThomas Cort static void	tputS ARGS((char *wp, struct shf *shf));
22*2718b568SThomas Cort static void	vfptreef ARGS((struct shf *shf, int indent, const char *fmt, va_list va));
23*2718b568SThomas Cort static struct ioword **iocopy ARGS((struct ioword **iow, Area *ap));
24*2718b568SThomas Cort static void     iofree ARGS((struct ioword **iow, Area *ap));
25*2718b568SThomas Cort 
26*2718b568SThomas Cort /*
27*2718b568SThomas Cort  * print a command tree
28*2718b568SThomas Cort  */
29*2718b568SThomas Cort 
30*2718b568SThomas Cort static void
ptree(t,indent,shf)31*2718b568SThomas Cort ptree(t, indent, shf)
32*2718b568SThomas Cort 	register struct op *t;
33*2718b568SThomas Cort 	int indent;
34*2718b568SThomas Cort 	register struct shf *shf;
35*2718b568SThomas Cort {
36*2718b568SThomas Cort 	register char **w;
37*2718b568SThomas Cort 	struct ioword **ioact;
38*2718b568SThomas Cort 	struct op *t1;
39*2718b568SThomas Cort 
40*2718b568SThomas Cort  Chain:
41*2718b568SThomas Cort 	if (t == NULL)
42*2718b568SThomas Cort 		return;
43*2718b568SThomas Cort 	switch (t->type) {
44*2718b568SThomas Cort 	  case TCOM:
45*2718b568SThomas Cort 		if (t->vars)
46*2718b568SThomas Cort 			for (w = t->vars; *w != NULL; )
47*2718b568SThomas Cort 				fptreef(shf, indent, "%S ", *w++);
48*2718b568SThomas Cort 		else
49*2718b568SThomas Cort 			fptreef(shf, indent, "#no-vars# ");
50*2718b568SThomas Cort 		if (t->args)
51*2718b568SThomas Cort 			for (w = t->args; *w != NULL; )
52*2718b568SThomas Cort 				fptreef(shf, indent, "%S ", *w++);
53*2718b568SThomas Cort 		else
54*2718b568SThomas Cort 			fptreef(shf, indent, "#no-args# ");
55*2718b568SThomas Cort 		break;
56*2718b568SThomas Cort 	  case TEXEC:
57*2718b568SThomas Cort #if 0 /* ?not useful - can't be called? */
58*2718b568SThomas Cort 		/* Print original vars */
59*2718b568SThomas Cort 		if (t->left->vars)
60*2718b568SThomas Cort 			for (w = t->left->vars; *w != NULL; )
61*2718b568SThomas Cort 				fptreef(shf, indent, "%S ", *w++);
62*2718b568SThomas Cort 		else
63*2718b568SThomas Cort 			fptreef(shf, indent, "#no-vars# ");
64*2718b568SThomas Cort 		/* Print expanded vars */
65*2718b568SThomas Cort 		if (t->args)
66*2718b568SThomas Cort 			for (w = t->args; *w != NULL; )
67*2718b568SThomas Cort 				fptreef(shf, indent, "%s ", *w++);
68*2718b568SThomas Cort 		else
69*2718b568SThomas Cort 			fptreef(shf, indent, "#no-args# ");
70*2718b568SThomas Cort 		/* Print original io */
71*2718b568SThomas Cort 		t = t->left;
72*2718b568SThomas Cort #else
73*2718b568SThomas Cort 		t = t->left;
74*2718b568SThomas Cort 		goto Chain;
75*2718b568SThomas Cort #endif
76*2718b568SThomas Cort 	  case TPAREN:
77*2718b568SThomas Cort 		fptreef(shf, indent + 2, "( %T) ", t->left);
78*2718b568SThomas Cort 		break;
79*2718b568SThomas Cort 	  case TPIPE:
80*2718b568SThomas Cort 		fptreef(shf, indent, "%T| ", t->left);
81*2718b568SThomas Cort 		t = t->right;
82*2718b568SThomas Cort 		goto Chain;
83*2718b568SThomas Cort 	  case TLIST:
84*2718b568SThomas Cort 		fptreef(shf, indent, "%T%;", t->left);
85*2718b568SThomas Cort 		t = t->right;
86*2718b568SThomas Cort 		goto Chain;
87*2718b568SThomas Cort 	  case TOR:
88*2718b568SThomas Cort 	  case TAND:
89*2718b568SThomas Cort 		fptreef(shf, indent, "%T%s %T",
90*2718b568SThomas Cort 			t->left, (t->type==TOR) ? "||" : "&&", t->right);
91*2718b568SThomas Cort 		break;
92*2718b568SThomas Cort 	  case TBANG:
93*2718b568SThomas Cort 		fptreef(shf, indent, "! ");
94*2718b568SThomas Cort 		t = t->right;
95*2718b568SThomas Cort 		goto Chain;
96*2718b568SThomas Cort 	  case TDBRACKET:
97*2718b568SThomas Cort 	  {
98*2718b568SThomas Cort 		int i;
99*2718b568SThomas Cort 
100*2718b568SThomas Cort 		fptreef(shf, indent, "[[");
101*2718b568SThomas Cort 		for (i = 0; t->args[i]; i++)
102*2718b568SThomas Cort 			fptreef(shf, indent, " %S", t->args[i]);
103*2718b568SThomas Cort 		fptreef(shf, indent, " ]] ");
104*2718b568SThomas Cort 		break;
105*2718b568SThomas Cort 	  }
106*2718b568SThomas Cort #ifdef KSH
107*2718b568SThomas Cort 	  case TSELECT:
108*2718b568SThomas Cort 		fptreef(shf, indent, "select %s ", t->str);
109*2718b568SThomas Cort 		/* fall through */
110*2718b568SThomas Cort #endif /* KSH */
111*2718b568SThomas Cort 	  case TFOR:
112*2718b568SThomas Cort 		if (t->type == TFOR)
113*2718b568SThomas Cort 			fptreef(shf, indent, "for %s ", t->str);
114*2718b568SThomas Cort 		if (t->vars != NULL) {
115*2718b568SThomas Cort 			fptreef(shf, indent, "in ");
116*2718b568SThomas Cort 			for (w = t->vars; *w; )
117*2718b568SThomas Cort 				fptreef(shf, indent, "%S ", *w++);
118*2718b568SThomas Cort 			fptreef(shf, indent, "%;");
119*2718b568SThomas Cort 		}
120*2718b568SThomas Cort 		fptreef(shf, indent + INDENT, "do%N%T", t->left);
121*2718b568SThomas Cort 		fptreef(shf, indent, "%;done ");
122*2718b568SThomas Cort 		break;
123*2718b568SThomas Cort 	  case TCASE:
124*2718b568SThomas Cort 		fptreef(shf, indent, "case %S in", t->str);
125*2718b568SThomas Cort 		for (t1 = t->left; t1 != NULL; t1 = t1->right) {
126*2718b568SThomas Cort 			fptreef(shf, indent, "%N(");
127*2718b568SThomas Cort 			for (w = t1->vars; *w != NULL; w++)
128*2718b568SThomas Cort 				fptreef(shf, indent, "%S%c", *w,
129*2718b568SThomas Cort 					(w[1] != NULL) ? '|' : ')');
130*2718b568SThomas Cort 			fptreef(shf, indent + INDENT, "%;%T%N;;", t1->left);
131*2718b568SThomas Cort 		}
132*2718b568SThomas Cort 		fptreef(shf, indent, "%Nesac ");
133*2718b568SThomas Cort 		break;
134*2718b568SThomas Cort 	  case TIF:
135*2718b568SThomas Cort 	  case TELIF:
136*2718b568SThomas Cort 		/* 3 == strlen("if ") */
137*2718b568SThomas Cort 		fptreef(shf, indent + 3, "if %T", t->left);
138*2718b568SThomas Cort 		for (;;) {
139*2718b568SThomas Cort 			t = t->right;
140*2718b568SThomas Cort 			if (t->left != NULL) {
141*2718b568SThomas Cort 				fptreef(shf, indent, "%;");
142*2718b568SThomas Cort 				fptreef(shf, indent + INDENT, "then%N%T",
143*2718b568SThomas Cort 					t->left);
144*2718b568SThomas Cort 			}
145*2718b568SThomas Cort 			if (t->right == NULL || t->right->type != TELIF)
146*2718b568SThomas Cort 				break;
147*2718b568SThomas Cort 			t = t->right;
148*2718b568SThomas Cort 			fptreef(shf, indent, "%;");
149*2718b568SThomas Cort 			/* 5 == strlen("elif ") */
150*2718b568SThomas Cort 			fptreef(shf, indent + 5, "elif %T", t->left);
151*2718b568SThomas Cort 		}
152*2718b568SThomas Cort 		if (t->right != NULL) {
153*2718b568SThomas Cort 			fptreef(shf, indent, "%;");
154*2718b568SThomas Cort 			fptreef(shf, indent + INDENT, "else%;%T", t->right);
155*2718b568SThomas Cort 		}
156*2718b568SThomas Cort 		fptreef(shf, indent, "%;fi ");
157*2718b568SThomas Cort 		break;
158*2718b568SThomas Cort 	  case TWHILE:
159*2718b568SThomas Cort 	  case TUNTIL:
160*2718b568SThomas Cort 		/* 6 == strlen("while"/"until") */
161*2718b568SThomas Cort 		fptreef(shf, indent + 6, "%s %T",
162*2718b568SThomas Cort 			(t->type==TWHILE) ? "while" : "until",
163*2718b568SThomas Cort 			t->left);
164*2718b568SThomas Cort 		fptreef(shf, indent, "%;do");
165*2718b568SThomas Cort 		fptreef(shf, indent + INDENT, "%;%T", t->right);
166*2718b568SThomas Cort 		fptreef(shf, indent, "%;done ");
167*2718b568SThomas Cort 		break;
168*2718b568SThomas Cort 	  case TBRACE:
169*2718b568SThomas Cort 		fptreef(shf, indent + INDENT, "{%;%T", t->left);
170*2718b568SThomas Cort 		fptreef(shf, indent, "%;} ");
171*2718b568SThomas Cort 		break;
172*2718b568SThomas Cort 	  case TCOPROC:
173*2718b568SThomas Cort 		fptreef(shf, indent, "%T|& ", t->left);
174*2718b568SThomas Cort 		break;
175*2718b568SThomas Cort 	  case TASYNC:
176*2718b568SThomas Cort 		fptreef(shf, indent, "%T& ", t->left);
177*2718b568SThomas Cort 		break;
178*2718b568SThomas Cort 	  case TFUNCT:
179*2718b568SThomas Cort 		fptreef(shf, indent,
180*2718b568SThomas Cort 			t->u.ksh_func ? "function %s %T" : "%s() %T",
181*2718b568SThomas Cort 				t->str, t->left);
182*2718b568SThomas Cort 		break;
183*2718b568SThomas Cort 	  case TTIME:
184*2718b568SThomas Cort 		fptreef(shf, indent, "time %T", t->left);
185*2718b568SThomas Cort 		break;
186*2718b568SThomas Cort 	  default:
187*2718b568SThomas Cort 		fptreef(shf, indent, "<botch>");
188*2718b568SThomas Cort 		break;
189*2718b568SThomas Cort 	}
190*2718b568SThomas Cort 	if ((ioact = t->ioact) != NULL) {
191*2718b568SThomas Cort 		int	need_nl = 0;
192*2718b568SThomas Cort 
193*2718b568SThomas Cort 		while (*ioact != NULL)
194*2718b568SThomas Cort 			pioact(shf, indent, *ioact++);
195*2718b568SThomas Cort 		/* Print here documents after everything else... */
196*2718b568SThomas Cort 		for (ioact = t->ioact; *ioact != NULL; ) {
197*2718b568SThomas Cort 			struct ioword *iop = *ioact++;
198*2718b568SThomas Cort 
199*2718b568SThomas Cort 			/* heredoc is 0 when tracing (set -x) */
200*2718b568SThomas Cort 			if ((iop->flag & IOTYPE) == IOHERE && iop->heredoc) {
201*2718b568SThomas Cort 				tputc('\n', shf);
202*2718b568SThomas Cort 				shf_puts(iop->heredoc, shf);
203*2718b568SThomas Cort 				fptreef(shf, indent, "%s",
204*2718b568SThomas Cort 					evalstr(iop->delim, 0));
205*2718b568SThomas Cort 				need_nl = 1;
206*2718b568SThomas Cort 			}
207*2718b568SThomas Cort 		}
208*2718b568SThomas Cort 		/* Last delimiter must be followed by a newline (this often
209*2718b568SThomas Cort 		 * leads to an extra blank line, but its not worth worrying
210*2718b568SThomas Cort 		 * about)
211*2718b568SThomas Cort 		 */
212*2718b568SThomas Cort 		if (need_nl)
213*2718b568SThomas Cort 			tputc('\n', shf);
214*2718b568SThomas Cort 	}
215*2718b568SThomas Cort }
216*2718b568SThomas Cort 
217*2718b568SThomas Cort static void
pioact(shf,indent,iop)218*2718b568SThomas Cort pioact(shf, indent, iop)
219*2718b568SThomas Cort 	register struct shf *shf;
220*2718b568SThomas Cort 	int indent;
221*2718b568SThomas Cort 	register struct ioword *iop;
222*2718b568SThomas Cort {
223*2718b568SThomas Cort 	int flag = iop->flag;
224*2718b568SThomas Cort 	int type = flag & IOTYPE;
225*2718b568SThomas Cort 	int expected;
226*2718b568SThomas Cort 
227*2718b568SThomas Cort 	expected = (type == IOREAD || type == IORDWR || type == IOHERE) ? 0
228*2718b568SThomas Cort 		    : (type == IOCAT || type == IOWRITE) ? 1
229*2718b568SThomas Cort 		    : (type == IODUP && (iop->unit == !(flag & IORDUP))) ?
230*2718b568SThomas Cort 			iop->unit
231*2718b568SThomas Cort 		    : iop->unit + 1;
232*2718b568SThomas Cort 	if (iop->unit != expected)
233*2718b568SThomas Cort 		tputc('0' + iop->unit, shf);
234*2718b568SThomas Cort 
235*2718b568SThomas Cort 	switch (type) {
236*2718b568SThomas Cort 	case IOREAD:
237*2718b568SThomas Cort 		fptreef(shf, indent, "< ");
238*2718b568SThomas Cort 		break;
239*2718b568SThomas Cort 	case IOHERE:
240*2718b568SThomas Cort 		if (flag&IOSKIP)
241*2718b568SThomas Cort 			fptreef(shf, indent, "<<- ");
242*2718b568SThomas Cort 		else
243*2718b568SThomas Cort 			fptreef(shf, indent, "<< ");
244*2718b568SThomas Cort 		break;
245*2718b568SThomas Cort 	case IOCAT:
246*2718b568SThomas Cort 		fptreef(shf, indent, ">> ");
247*2718b568SThomas Cort 		break;
248*2718b568SThomas Cort 	case IOWRITE:
249*2718b568SThomas Cort 		if (flag&IOCLOB)
250*2718b568SThomas Cort 			fptreef(shf, indent, ">| ");
251*2718b568SThomas Cort 		else
252*2718b568SThomas Cort 			fptreef(shf, indent, "> ");
253*2718b568SThomas Cort 		break;
254*2718b568SThomas Cort 	case IORDWR:
255*2718b568SThomas Cort 		fptreef(shf, indent, "<> ");
256*2718b568SThomas Cort 		break;
257*2718b568SThomas Cort 	case IODUP:
258*2718b568SThomas Cort 		if (flag & IORDUP)
259*2718b568SThomas Cort 			fptreef(shf, indent, "<&");
260*2718b568SThomas Cort 		else
261*2718b568SThomas Cort 			fptreef(shf, indent, ">&");
262*2718b568SThomas Cort 		break;
263*2718b568SThomas Cort 	}
264*2718b568SThomas Cort 	/* name/delim are 0 when printing syntax errors */
265*2718b568SThomas Cort 	if (type == IOHERE) {
266*2718b568SThomas Cort 		if (iop->delim)
267*2718b568SThomas Cort 			fptreef(shf, indent, "%S ", iop->delim);
268*2718b568SThomas Cort 	} else if (iop->name)
269*2718b568SThomas Cort 		fptreef(shf, indent, (iop->flag & IONAMEXP) ? "%s " : "%S ",
270*2718b568SThomas Cort 			iop->name);
271*2718b568SThomas Cort }
272*2718b568SThomas Cort 
273*2718b568SThomas Cort 
274*2718b568SThomas Cort /*
275*2718b568SThomas Cort  * variants of fputc, fputs for ptreef and snptreef
276*2718b568SThomas Cort  */
277*2718b568SThomas Cort 
278*2718b568SThomas Cort static void
tputC(c,shf)279*2718b568SThomas Cort tputC(c, shf)
280*2718b568SThomas Cort 	register int c;
281*2718b568SThomas Cort 	register struct shf *shf;
282*2718b568SThomas Cort {
283*2718b568SThomas Cort 	if ((c&0x60) == 0) {		/* C0|C1 */
284*2718b568SThomas Cort 		tputc((c&0x80) ? '$' : '^', shf);
285*2718b568SThomas Cort 		tputc(((c&0x7F)|0x40), shf);
286*2718b568SThomas Cort 	} else if ((c&0x7F) == 0x7F) {	/* DEL */
287*2718b568SThomas Cort 		tputc((c&0x80) ? '$' : '^', shf);
288*2718b568SThomas Cort 		tputc('?', shf);
289*2718b568SThomas Cort 	} else
290*2718b568SThomas Cort 		tputc(c, shf);
291*2718b568SThomas Cort }
292*2718b568SThomas Cort 
293*2718b568SThomas Cort static void
tputS(wp,shf)294*2718b568SThomas Cort tputS(wp, shf)
295*2718b568SThomas Cort 	register char *wp;
296*2718b568SThomas Cort 	register struct shf *shf;
297*2718b568SThomas Cort {
298*2718b568SThomas Cort 	register int c, quoted=0;
299*2718b568SThomas Cort 
300*2718b568SThomas Cort 	/* problems:
301*2718b568SThomas Cort 	 *	`...` -> $(...)
302*2718b568SThomas Cort 	 *	'foo' -> "foo"
303*2718b568SThomas Cort 	 * could change encoding to:
304*2718b568SThomas Cort 	 *	OQUOTE ["'] ... CQUOTE ["']
305*2718b568SThomas Cort 	 * 	COMSUB [(`] ...\0	(handle $ ` \ and maybe " in `...` case)
306*2718b568SThomas Cort 	 */
307*2718b568SThomas Cort 	while (1)
308*2718b568SThomas Cort 		switch ((c = *wp++)) {
309*2718b568SThomas Cort 		  case EOS:
310*2718b568SThomas Cort 			return;
311*2718b568SThomas Cort 		  case CHAR:
312*2718b568SThomas Cort 			tputC(*wp++, shf);
313*2718b568SThomas Cort 			break;
314*2718b568SThomas Cort 		  case QCHAR:
315*2718b568SThomas Cort 			c = *wp++;
316*2718b568SThomas Cort 			if (!quoted || (c == '"' || c == '`' || c == '$'))
317*2718b568SThomas Cort 				tputc('\\', shf);
318*2718b568SThomas Cort 			tputC(c, shf);
319*2718b568SThomas Cort 			break;
320*2718b568SThomas Cort 		  case COMSUB:
321*2718b568SThomas Cort 			tputc('$', shf);
322*2718b568SThomas Cort 			tputc('(', shf);
323*2718b568SThomas Cort 			while (*wp != 0)
324*2718b568SThomas Cort 				tputC(*wp++, shf);
325*2718b568SThomas Cort 			tputc(')', shf);
326*2718b568SThomas Cort 			wp++;
327*2718b568SThomas Cort 			break;
328*2718b568SThomas Cort 		  case EXPRSUB:
329*2718b568SThomas Cort 			tputc('$', shf);
330*2718b568SThomas Cort 			tputc('(', shf);
331*2718b568SThomas Cort 			tputc('(', shf);
332*2718b568SThomas Cort 			while (*wp != 0)
333*2718b568SThomas Cort 				tputC(*wp++, shf);
334*2718b568SThomas Cort 			tputc(')', shf);
335*2718b568SThomas Cort 			tputc(')', shf);
336*2718b568SThomas Cort 			wp++;
337*2718b568SThomas Cort 			break;
338*2718b568SThomas Cort 		  case OQUOTE:
339*2718b568SThomas Cort 		  	quoted = 1;
340*2718b568SThomas Cort 			tputc('"', shf);
341*2718b568SThomas Cort 			break;
342*2718b568SThomas Cort 		  case CQUOTE:
343*2718b568SThomas Cort 			quoted = 0;
344*2718b568SThomas Cort 			tputc('"', shf);
345*2718b568SThomas Cort 			break;
346*2718b568SThomas Cort 		  case OSUBST:
347*2718b568SThomas Cort 			tputc('$', shf);
348*2718b568SThomas Cort 			if (*wp++ == '{')
349*2718b568SThomas Cort 				tputc('{', shf);
350*2718b568SThomas Cort 			while ((c = *wp++) != 0)
351*2718b568SThomas Cort 				tputC(c, shf);
352*2718b568SThomas Cort 			break;
353*2718b568SThomas Cort 		  case CSUBST:
354*2718b568SThomas Cort 			if (*wp++ == '}')
355*2718b568SThomas Cort 				tputc('}', shf);
356*2718b568SThomas Cort 			break;
357*2718b568SThomas Cort #ifdef KSH
358*2718b568SThomas Cort 		  case OPAT:
359*2718b568SThomas Cort 			tputc(*wp++, shf);
360*2718b568SThomas Cort 			tputc('(', shf);
361*2718b568SThomas Cort 			break;
362*2718b568SThomas Cort 		  case SPAT:
363*2718b568SThomas Cort 			tputc('|', shf);
364*2718b568SThomas Cort 			break;
365*2718b568SThomas Cort 		  case CPAT:
366*2718b568SThomas Cort 			tputc(')', shf);
367*2718b568SThomas Cort 			break;
368*2718b568SThomas Cort #endif /* KSH */
369*2718b568SThomas Cort 		}
370*2718b568SThomas Cort }
371*2718b568SThomas Cort 
372*2718b568SThomas Cort /*
373*2718b568SThomas Cort  * this is the _only_ way to reliably handle
374*2718b568SThomas Cort  * variable args with an ANSI compiler
375*2718b568SThomas Cort  */
376*2718b568SThomas Cort /* VARARGS */
377*2718b568SThomas Cort int
378*2718b568SThomas Cort #ifdef HAVE_PROTOTYPES
fptreef(struct shf * shf,int indent,const char * fmt,...)379*2718b568SThomas Cort fptreef(struct shf *shf, int indent, const char *fmt, ...)
380*2718b568SThomas Cort #else
381*2718b568SThomas Cort fptreef(shf, indent, fmt, va_alist)
382*2718b568SThomas Cort   struct shf *shf;
383*2718b568SThomas Cort   int indent;
384*2718b568SThomas Cort   const char *fmt;
385*2718b568SThomas Cort   va_dcl
386*2718b568SThomas Cort #endif
387*2718b568SThomas Cort {
388*2718b568SThomas Cort   va_list	va;
389*2718b568SThomas Cort 
390*2718b568SThomas Cort   SH_VA_START(va, fmt);
391*2718b568SThomas Cort 
392*2718b568SThomas Cort   vfptreef(shf, indent, fmt, va);
393*2718b568SThomas Cort   va_end(va);
394*2718b568SThomas Cort   return 0;
395*2718b568SThomas Cort }
396*2718b568SThomas Cort 
397*2718b568SThomas Cort /* VARARGS */
398*2718b568SThomas Cort char *
399*2718b568SThomas Cort #ifdef HAVE_PROTOTYPES
snptreef(char * s,int n,const char * fmt,...)400*2718b568SThomas Cort snptreef(char *s, int n, const char *fmt, ...)
401*2718b568SThomas Cort #else
402*2718b568SThomas Cort snptreef(s, n, fmt, va_alist)
403*2718b568SThomas Cort   char *s;
404*2718b568SThomas Cort   int n;
405*2718b568SThomas Cort   const char *fmt;
406*2718b568SThomas Cort   va_dcl
407*2718b568SThomas Cort #endif
408*2718b568SThomas Cort {
409*2718b568SThomas Cort   va_list va;
410*2718b568SThomas Cort   struct shf shf;
411*2718b568SThomas Cort 
412*2718b568SThomas Cort   shf_sopen(s, n, SHF_WR | (s ? 0 : SHF_DYNAMIC), &shf);
413*2718b568SThomas Cort 
414*2718b568SThomas Cort   SH_VA_START(va, fmt);
415*2718b568SThomas Cort   vfptreef(&shf, 0, fmt, va);
416*2718b568SThomas Cort   va_end(va);
417*2718b568SThomas Cort 
418*2718b568SThomas Cort   return shf_sclose(&shf); /* null terminates */
419*2718b568SThomas Cort }
420*2718b568SThomas Cort 
421*2718b568SThomas Cort static void
vfptreef(shf,indent,fmt,va)422*2718b568SThomas Cort vfptreef(shf, indent, fmt, va)
423*2718b568SThomas Cort 	register struct shf *shf;
424*2718b568SThomas Cort 	int indent;
425*2718b568SThomas Cort 	const char *fmt;
426*2718b568SThomas Cort 	register va_list va;
427*2718b568SThomas Cort {
428*2718b568SThomas Cort 	register int c;
429*2718b568SThomas Cort 
430*2718b568SThomas Cort 	while ((c = *fmt++))
431*2718b568SThomas Cort 	    if (c == '%') {
432*2718b568SThomas Cort 		register long n;
433*2718b568SThomas Cort 		register char *p;
434*2718b568SThomas Cort 		int neg;
435*2718b568SThomas Cort 
436*2718b568SThomas Cort 		switch ((c = *fmt++)) {
437*2718b568SThomas Cort 		  case 'c':
438*2718b568SThomas Cort 			tputc(va_arg(va, int), shf);
439*2718b568SThomas Cort 			break;
440*2718b568SThomas Cort 		  case 's':
441*2718b568SThomas Cort 			p = va_arg(va, char *);
442*2718b568SThomas Cort 			while (*p)
443*2718b568SThomas Cort 				tputc(*p++, shf);
444*2718b568SThomas Cort 			break;
445*2718b568SThomas Cort 		  case 'S':	/* word */
446*2718b568SThomas Cort 			p = va_arg(va, char *);
447*2718b568SThomas Cort 			tputS(p, shf);
448*2718b568SThomas Cort 			break;
449*2718b568SThomas Cort 		  case 'd': case 'u': /* decimal */
450*2718b568SThomas Cort 			n = (c == 'd') ? va_arg(va, int)
451*2718b568SThomas Cort 				       : va_arg(va, unsigned int);
452*2718b568SThomas Cort 			neg = c=='d' && n<0;
453*2718b568SThomas Cort 			p = ulton((neg) ? -n : n, 10);
454*2718b568SThomas Cort 			if (neg)
455*2718b568SThomas Cort 				*--p = '-';
456*2718b568SThomas Cort 			while (*p)
457*2718b568SThomas Cort 				tputc(*p++, shf);
458*2718b568SThomas Cort 			break;
459*2718b568SThomas Cort 		  case 'T':	/* format tree */
460*2718b568SThomas Cort 			ptree(va_arg(va, struct op *), indent, shf);
461*2718b568SThomas Cort 			break;
462*2718b568SThomas Cort 		  case ';':	/* newline or ; */
463*2718b568SThomas Cort 		  case 'N':	/* newline or space */
464*2718b568SThomas Cort 			if (shf->flags & SHF_STRING) {
465*2718b568SThomas Cort 				if (c == ';')
466*2718b568SThomas Cort 					tputc(';', shf);
467*2718b568SThomas Cort 				tputc(' ', shf);
468*2718b568SThomas Cort 			} else {
469*2718b568SThomas Cort 				int i;
470*2718b568SThomas Cort 
471*2718b568SThomas Cort 				tputc('\n', shf);
472*2718b568SThomas Cort 				for (i = indent; i >= 8; i -= 8)
473*2718b568SThomas Cort 					tputc('\t', shf);
474*2718b568SThomas Cort 				for (; i > 0; --i)
475*2718b568SThomas Cort 					tputc(' ', shf);
476*2718b568SThomas Cort 			}
477*2718b568SThomas Cort 			break;
478*2718b568SThomas Cort 		  case 'R':
479*2718b568SThomas Cort 			pioact(shf, indent, va_arg(va, struct ioword *));
480*2718b568SThomas Cort 			break;
481*2718b568SThomas Cort 		  default:
482*2718b568SThomas Cort 			tputc(c, shf);
483*2718b568SThomas Cort 			break;
484*2718b568SThomas Cort 		}
485*2718b568SThomas Cort 	    } else
486*2718b568SThomas Cort 		tputc(c, shf);
487*2718b568SThomas Cort }
488*2718b568SThomas Cort 
489*2718b568SThomas Cort /*
490*2718b568SThomas Cort  * copy tree (for function definition)
491*2718b568SThomas Cort  */
492*2718b568SThomas Cort 
493*2718b568SThomas Cort struct op *
tcopy(t,ap)494*2718b568SThomas Cort tcopy(t, ap)
495*2718b568SThomas Cort 	register struct op *t;
496*2718b568SThomas Cort 	Area *ap;
497*2718b568SThomas Cort {
498*2718b568SThomas Cort 	register struct op *r;
499*2718b568SThomas Cort 	register char **tw, **rw;
500*2718b568SThomas Cort 
501*2718b568SThomas Cort 	if (t == NULL)
502*2718b568SThomas Cort 		return NULL;
503*2718b568SThomas Cort 
504*2718b568SThomas Cort 	r = (struct op *) alloc(sizeof(struct op), ap);
505*2718b568SThomas Cort 
506*2718b568SThomas Cort 	r->type = t->type;
507*2718b568SThomas Cort 	r->u.evalflags = t->u.evalflags;
508*2718b568SThomas Cort 
509*2718b568SThomas Cort 	r->str = t->type == TCASE ? wdcopy(t->str, ap) : str_save(t->str, ap);
510*2718b568SThomas Cort 
511*2718b568SThomas Cort 	if (t->vars == NULL)
512*2718b568SThomas Cort 		r->vars = NULL;
513*2718b568SThomas Cort 	else {
514*2718b568SThomas Cort 		for (tw = t->vars; *tw++ != NULL; )
515*2718b568SThomas Cort 			;
516*2718b568SThomas Cort 		rw = r->vars = (char **)
517*2718b568SThomas Cort 			alloc((tw - t->vars + 1) * sizeof(*tw), ap);
518*2718b568SThomas Cort 		for (tw = t->vars; *tw != NULL; )
519*2718b568SThomas Cort 			*rw++ = wdcopy(*tw++, ap);
520*2718b568SThomas Cort 		*rw = NULL;
521*2718b568SThomas Cort 	}
522*2718b568SThomas Cort 
523*2718b568SThomas Cort 	if (t->args == NULL)
524*2718b568SThomas Cort 		r->args = NULL;
525*2718b568SThomas Cort 	else {
526*2718b568SThomas Cort 		for (tw = t->args; *tw++ != NULL; )
527*2718b568SThomas Cort 			;
528*2718b568SThomas Cort 		rw = r->args = (char **)
529*2718b568SThomas Cort 			alloc((tw - t->args + 1) * sizeof(*tw), ap);
530*2718b568SThomas Cort 		for (tw = t->args; *tw != NULL; )
531*2718b568SThomas Cort 			*rw++ = wdcopy(*tw++, ap);
532*2718b568SThomas Cort 		*rw = NULL;
533*2718b568SThomas Cort 	}
534*2718b568SThomas Cort 
535*2718b568SThomas Cort 	r->ioact = (t->ioact == NULL) ? NULL : iocopy(t->ioact, ap);
536*2718b568SThomas Cort 
537*2718b568SThomas Cort 	r->left = tcopy(t->left, ap);
538*2718b568SThomas Cort 	r->right = tcopy(t->right, ap);
539*2718b568SThomas Cort 	r->lineno = t->lineno;
540*2718b568SThomas Cort 
541*2718b568SThomas Cort 	return r;
542*2718b568SThomas Cort }
543*2718b568SThomas Cort 
544*2718b568SThomas Cort char *
wdcopy(wp,ap)545*2718b568SThomas Cort wdcopy(wp, ap)
546*2718b568SThomas Cort 	const char *wp;
547*2718b568SThomas Cort 	Area *ap;
548*2718b568SThomas Cort {
549*2718b568SThomas Cort 	size_t len = wdscan(wp, EOS) - wp;
550*2718b568SThomas Cort 	return memcpy(alloc(len, ap), wp, len);
551*2718b568SThomas Cort }
552*2718b568SThomas Cort 
553*2718b568SThomas Cort /* return the position of prefix c in wp plus 1 */
554*2718b568SThomas Cort char *
wdscan(wp,c)555*2718b568SThomas Cort wdscan(wp, c)
556*2718b568SThomas Cort 	register const char *wp;
557*2718b568SThomas Cort 	register int c;
558*2718b568SThomas Cort {
559*2718b568SThomas Cort 	register int nest = 0;
560*2718b568SThomas Cort 
561*2718b568SThomas Cort 	while (1)
562*2718b568SThomas Cort 		switch (*wp++) {
563*2718b568SThomas Cort 		  case EOS:
564*2718b568SThomas Cort 			return (char *) __UNCONST(wp);
565*2718b568SThomas Cort 		  case CHAR:
566*2718b568SThomas Cort 		  case QCHAR:
567*2718b568SThomas Cort 			wp++;
568*2718b568SThomas Cort 			break;
569*2718b568SThomas Cort 		  case COMSUB:
570*2718b568SThomas Cort 		  case EXPRSUB:
571*2718b568SThomas Cort 			while (*wp++ != 0)
572*2718b568SThomas Cort 				;
573*2718b568SThomas Cort 			break;
574*2718b568SThomas Cort 		  case OQUOTE:
575*2718b568SThomas Cort 		  case CQUOTE:
576*2718b568SThomas Cort 			break;
577*2718b568SThomas Cort 		  case OSUBST:
578*2718b568SThomas Cort 			nest++;
579*2718b568SThomas Cort 			while (*wp++ != '\0')
580*2718b568SThomas Cort 				;
581*2718b568SThomas Cort 			break;
582*2718b568SThomas Cort 		  case CSUBST:
583*2718b568SThomas Cort 			wp++;
584*2718b568SThomas Cort 			if (c == CSUBST && nest == 0)
585*2718b568SThomas Cort 				return (char *) __UNCONST(wp);
586*2718b568SThomas Cort 			nest--;
587*2718b568SThomas Cort 			break;
588*2718b568SThomas Cort #ifdef KSH
589*2718b568SThomas Cort 		  case OPAT:
590*2718b568SThomas Cort 			nest++;
591*2718b568SThomas Cort 			wp++;
592*2718b568SThomas Cort 			break;
593*2718b568SThomas Cort 		  case SPAT:
594*2718b568SThomas Cort 		  case CPAT:
595*2718b568SThomas Cort 			if (c == wp[-1] && nest == 0)
596*2718b568SThomas Cort 				return (char *) __UNCONST(wp);
597*2718b568SThomas Cort 			if (wp[-1] == CPAT)
598*2718b568SThomas Cort 				nest--;
599*2718b568SThomas Cort 			break;
600*2718b568SThomas Cort #endif /* KSH */
601*2718b568SThomas Cort 		  default:
602*2718b568SThomas Cort 			internal_errorf(0,
603*2718b568SThomas Cort 				"wdscan: unknown char 0x%x (carrying on)",
604*2718b568SThomas Cort 				wp[-1]);
605*2718b568SThomas Cort 		}
606*2718b568SThomas Cort }
607*2718b568SThomas Cort 
608*2718b568SThomas Cort /* return a copy of wp without any of the mark up characters and
609*2718b568SThomas Cort  * with quote characters (" ' \) stripped.
610*2718b568SThomas Cort  * (string is allocated from ATEMP)
611*2718b568SThomas Cort  */
612*2718b568SThomas Cort char *
wdstrip(wp)613*2718b568SThomas Cort wdstrip(wp)
614*2718b568SThomas Cort 	const char *wp;
615*2718b568SThomas Cort {
616*2718b568SThomas Cort 	struct shf shf;
617*2718b568SThomas Cort 	int c;
618*2718b568SThomas Cort 
619*2718b568SThomas Cort 	shf_sopen((char *) 0, 32, SHF_WR | SHF_DYNAMIC, &shf);
620*2718b568SThomas Cort 
621*2718b568SThomas Cort 	/* problems:
622*2718b568SThomas Cort 	 *	`...` -> $(...)
623*2718b568SThomas Cort 	 *	x${foo:-"hi"} -> x${foo:-hi}
624*2718b568SThomas Cort 	 *	x${foo:-'hi'} -> x${foo:-hi}
625*2718b568SThomas Cort 	 */
626*2718b568SThomas Cort 	while (1)
627*2718b568SThomas Cort 		switch ((c = *wp++)) {
628*2718b568SThomas Cort 		  case EOS:
629*2718b568SThomas Cort 			return shf_sclose(&shf); /* null terminates */
630*2718b568SThomas Cort 		  case CHAR:
631*2718b568SThomas Cort 		  case QCHAR:
632*2718b568SThomas Cort 			shf_putchar(*wp++, &shf);
633*2718b568SThomas Cort 			break;
634*2718b568SThomas Cort 		  case COMSUB:
635*2718b568SThomas Cort 			shf_putchar('$', &shf);
636*2718b568SThomas Cort 			shf_putchar('(', &shf);
637*2718b568SThomas Cort 			while (*wp != 0)
638*2718b568SThomas Cort 				shf_putchar(*wp++, &shf);
639*2718b568SThomas Cort 			shf_putchar(')', &shf);
640*2718b568SThomas Cort 			break;
641*2718b568SThomas Cort 		  case EXPRSUB:
642*2718b568SThomas Cort 			shf_putchar('$', &shf);
643*2718b568SThomas Cort 			shf_putchar('(', &shf);
644*2718b568SThomas Cort 			shf_putchar('(', &shf);
645*2718b568SThomas Cort 			while (*wp != 0)
646*2718b568SThomas Cort 				shf_putchar(*wp++, &shf);
647*2718b568SThomas Cort 			shf_putchar(')', &shf);
648*2718b568SThomas Cort 			shf_putchar(')', &shf);
649*2718b568SThomas Cort 			break;
650*2718b568SThomas Cort 		  case OQUOTE:
651*2718b568SThomas Cort 			break;
652*2718b568SThomas Cort 		  case CQUOTE:
653*2718b568SThomas Cort 			break;
654*2718b568SThomas Cort 		  case OSUBST:
655*2718b568SThomas Cort 			shf_putchar('$', &shf);
656*2718b568SThomas Cort 			if (*wp++ == '{')
657*2718b568SThomas Cort 			    shf_putchar('{', &shf);
658*2718b568SThomas Cort 			while ((c = *wp++) != 0)
659*2718b568SThomas Cort 				shf_putchar(c, &shf);
660*2718b568SThomas Cort 			break;
661*2718b568SThomas Cort 		  case CSUBST:
662*2718b568SThomas Cort 			if (*wp++ == '}')
663*2718b568SThomas Cort 				shf_putchar('}', &shf);
664*2718b568SThomas Cort 			break;
665*2718b568SThomas Cort #ifdef KSH
666*2718b568SThomas Cort 		  case OPAT:
667*2718b568SThomas Cort 			shf_putchar(*wp++, &shf);
668*2718b568SThomas Cort 			shf_putchar('(', &shf);
669*2718b568SThomas Cort 			break;
670*2718b568SThomas Cort 		  case SPAT:
671*2718b568SThomas Cort 			shf_putchar('|', &shf);
672*2718b568SThomas Cort 			break;
673*2718b568SThomas Cort 		  case CPAT:
674*2718b568SThomas Cort 			shf_putchar(')', &shf);
675*2718b568SThomas Cort 			break;
676*2718b568SThomas Cort #endif /* KSH */
677*2718b568SThomas Cort 		}
678*2718b568SThomas Cort }
679*2718b568SThomas Cort 
680*2718b568SThomas Cort static	struct ioword **
iocopy(iow,ap)681*2718b568SThomas Cort iocopy(iow, ap)
682*2718b568SThomas Cort 	register struct ioword **iow;
683*2718b568SThomas Cort 	Area *ap;
684*2718b568SThomas Cort {
685*2718b568SThomas Cort 	register struct ioword **ior;
686*2718b568SThomas Cort 	register int i;
687*2718b568SThomas Cort 
688*2718b568SThomas Cort 	for (ior = iow; *ior++ != NULL; )
689*2718b568SThomas Cort 		;
690*2718b568SThomas Cort 	ior = (struct ioword **) alloc((ior - iow + 1) * sizeof(*ior), ap);
691*2718b568SThomas Cort 
692*2718b568SThomas Cort 	for (i = 0; iow[i] != NULL; i++) {
693*2718b568SThomas Cort 		register struct ioword *p, *q;
694*2718b568SThomas Cort 
695*2718b568SThomas Cort 		p = iow[i];
696*2718b568SThomas Cort 		q = (struct ioword *) alloc(sizeof(*p), ap);
697*2718b568SThomas Cort 		ior[i] = q;
698*2718b568SThomas Cort 		*q = *p;
699*2718b568SThomas Cort 		if (p->name != (char *) 0)
700*2718b568SThomas Cort 			q->name = wdcopy(p->name, ap);
701*2718b568SThomas Cort 		if (p->delim != (char *) 0)
702*2718b568SThomas Cort 			q->delim = wdcopy(p->delim, ap);
703*2718b568SThomas Cort 		if (p->heredoc != (char *) 0)
704*2718b568SThomas Cort 			q->heredoc = str_save(p->heredoc, ap);
705*2718b568SThomas Cort 	}
706*2718b568SThomas Cort 	ior[i] = NULL;
707*2718b568SThomas Cort 
708*2718b568SThomas Cort 	return ior;
709*2718b568SThomas Cort }
710*2718b568SThomas Cort 
711*2718b568SThomas Cort /*
712*2718b568SThomas Cort  * free tree (for function definition)
713*2718b568SThomas Cort  */
714*2718b568SThomas Cort 
715*2718b568SThomas Cort void
tfree(t,ap)716*2718b568SThomas Cort tfree(t, ap)
717*2718b568SThomas Cort 	register struct op *t;
718*2718b568SThomas Cort 	Area *ap;
719*2718b568SThomas Cort {
720*2718b568SThomas Cort 	register char **w;
721*2718b568SThomas Cort 
722*2718b568SThomas Cort 	if (t == NULL)
723*2718b568SThomas Cort 		return;
724*2718b568SThomas Cort 
725*2718b568SThomas Cort 	if (t->str != NULL)
726*2718b568SThomas Cort 		afree((void*)t->str, ap);
727*2718b568SThomas Cort 
728*2718b568SThomas Cort 	if (t->vars != NULL) {
729*2718b568SThomas Cort 		for (w = t->vars; *w != NULL; w++)
730*2718b568SThomas Cort 			afree((void*)*w, ap);
731*2718b568SThomas Cort 		afree((void*)t->vars, ap);
732*2718b568SThomas Cort 	}
733*2718b568SThomas Cort 
734*2718b568SThomas Cort 	if (t->args != NULL) {
735*2718b568SThomas Cort 		for (w = t->args; *w != NULL; w++)
736*2718b568SThomas Cort 			afree((void*)*w, ap);
737*2718b568SThomas Cort 		afree((void*)t->args, ap);
738*2718b568SThomas Cort 	}
739*2718b568SThomas Cort 
740*2718b568SThomas Cort 	if (t->ioact != NULL)
741*2718b568SThomas Cort 		iofree(t->ioact, ap);
742*2718b568SThomas Cort 
743*2718b568SThomas Cort 	tfree(t->left, ap);
744*2718b568SThomas Cort 	tfree(t->right, ap);
745*2718b568SThomas Cort 
746*2718b568SThomas Cort 	afree((void*)t, ap);
747*2718b568SThomas Cort }
748*2718b568SThomas Cort 
749*2718b568SThomas Cort static	void
iofree(iow,ap)750*2718b568SThomas Cort iofree(iow, ap)
751*2718b568SThomas Cort 	struct ioword **iow;
752*2718b568SThomas Cort 	Area *ap;
753*2718b568SThomas Cort {
754*2718b568SThomas Cort 	register struct ioword **iop;
755*2718b568SThomas Cort 	register struct ioword *p;
756*2718b568SThomas Cort 
757*2718b568SThomas Cort 	for (iop = iow; (p = *iop++) != NULL; ) {
758*2718b568SThomas Cort 		if (p->name != NULL)
759*2718b568SThomas Cort 			afree((void*)p->name, ap);
760*2718b568SThomas Cort 		if (p->delim != NULL)
761*2718b568SThomas Cort 			afree((void*)p->delim, ap);
762*2718b568SThomas Cort 		if (p->heredoc != NULL)
763*2718b568SThomas Cort 			afree((void*)p->heredoc, ap);
764*2718b568SThomas Cort 		afree((void*)p, ap);
765*2718b568SThomas Cort 	}
766*2718b568SThomas Cort }
767