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