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