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