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