blob: 335e3fba37f4df144cf1ceed98079cb47517e332 [file] [log] [blame]
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001/* $OpenBSD: tree.c,v 1.21 2015/09/01 13:12:31 tedu Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002
3/*-
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005 * 2011, 2012, 2013, 2015, 2016, 2017
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006 * mirabilos <m@mirbsd.org>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07007 *
8 * Provided that these terms and disclaimer and all copyright notices
9 * are retained or reproduced in an accompanying document, permission
10 * is granted to deal in this work without restriction, including un-
11 * limited rights to use, publicly perform, distribute, sell, modify,
12 * merge, give away, or sublicence.
13 *
14 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 * the utmost extent permitted by applicable law, neither express nor
16 * implied; without malicious intent or gross negligence. In no event
17 * may a licensor, author or contributor be held liable for indirect,
18 * direct, other damage, loss, or other issues arising in any way out
19 * of dealing in the work, even if advised of the possibility of such
20 * damage or existence of a defect, except proven that it results out
21 * of said person's immediate fault when using the work as intended.
22 */
23
24#include "sh.h"
25
Elliott Hughes47086262019-03-26 12:34:31 -070026__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.97 2018/10/20 18:46:00 tg Exp $");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070027
Geremy Condra03ebf062011-10-12 18:17:24 -070028#define INDENT 8
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070029
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070030static void ptree(struct op *, int, struct shf *);
Thorsten Glaser811a5752013-07-25 14:24:45 +000031static void pioact(struct shf *, struct ioword *);
Geremy Condra03ebf062011-10-12 18:17:24 -070032static const char *wdvarput(struct shf *, const char *, int, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070033static void vfptreef(struct shf *, int, const char *, va_list);
34static struct ioword **iocopy(struct ioword **, Area *);
35static void iofree(struct ioword **, Area *);
36
Geremy Condra03ebf062011-10-12 18:17:24 -070037/* "foo& ; bar" and "foo |& ; bar" are invalid */
38static bool prevent_semicolon;
39
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000040static const char Telif_pT[] = "elif %T";
41
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070042/*
43 * print a command tree
44 */
45static void
46ptree(struct op *t, int indent, struct shf *shf)
47{
48 const char **w;
49 struct ioword **ioact;
50 struct op *t1;
Geremy Condra03ebf062011-10-12 18:17:24 -070051 int i;
Elliott Hughes77740fc2016-08-12 15:06:53 -070052 const char *ccp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070053
54 Chain:
55 if (t == NULL)
56 return;
57 switch (t->type) {
58 case TCOM:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000059 prevent_semicolon = false;
Elliott Hughes77740fc2016-08-12 15:06:53 -070060 /* special-case 'var=<<EOF' (cf. exec.c:execute) */
Elliott Hughesa3c3f962017-04-12 16:52:30 -070061 if (t->args &&
Elliott Hughes77740fc2016-08-12 15:06:53 -070062 /* we have zero arguments, i.e. no program to run */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000063 t->args[0] == NULL &&
64 /* we have exactly one variable assignment */
65 t->vars[0] != NULL && t->vars[1] == NULL &&
66 /* we have exactly one I/O redirection */
67 t->ioact != NULL && t->ioact[0] != NULL &&
68 t->ioact[1] == NULL &&
69 /* of type "here document" (or "here string") */
Elliott Hughes77740fc2016-08-12 15:06:53 -070070 (t->ioact[0]->ioflag & IOTYPE) == IOHERE &&
71 /* the variable assignment begins with a valid varname */
72 (ccp = skip_wdvarname(t->vars[0], true)) != t->vars[0] &&
73 /* and has no right-hand side (i.e. "varname=") */
74 ccp[0] == CHAR && ((ccp[1] == '=' && ccp[2] == EOS) ||
75 /* or "varname+=" */ (ccp[1] == '+' && ccp[2] == CHAR &&
76 ccp[3] == '=' && ccp[4] == EOS))) {
77 fptreef(shf, indent, Tf_S, t->vars[0]);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000078 break;
79 }
80
Geremy Condra03ebf062011-10-12 18:17:24 -070081 if (t->vars) {
82 w = (const char **)t->vars;
83 while (*w)
Elliott Hughes77740fc2016-08-12 15:06:53 -070084 fptreef(shf, indent, Tf_S_, *w++);
Geremy Condra03ebf062011-10-12 18:17:24 -070085 } else
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070086 shf_puts("#no-vars# ", shf);
Geremy Condra03ebf062011-10-12 18:17:24 -070087 if (t->args) {
88 w = t->args;
Elliott Hughesa3c3f962017-04-12 16:52:30 -070089 if (*w && **w == CHAR) {
90 char *cp = wdstrip(*w++, WDS_TPUTS);
91
92 if (valid_alias_name(cp))
93 shf_putc('\\', shf);
94 shf_puts(cp, shf);
95 shf_putc(' ', shf);
96 afree(cp, ATEMP);
97 }
Geremy Condra03ebf062011-10-12 18:17:24 -070098 while (*w)
Elliott Hughes77740fc2016-08-12 15:06:53 -070099 fptreef(shf, indent, Tf_S_, *w++);
Geremy Condra03ebf062011-10-12 18:17:24 -0700100 } else
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700101 shf_puts("#no-args# ", shf);
102 break;
103 case TEXEC:
104 t = t->left;
105 goto Chain;
106 case TPAREN:
107 fptreef(shf, indent + 2, "( %T) ", t->left);
108 break;
109 case TPIPE:
110 fptreef(shf, indent, "%T| ", t->left);
111 t = t->right;
112 goto Chain;
113 case TLIST:
114 fptreef(shf, indent, "%T%;", t->left);
115 t = t->right;
116 goto Chain;
117 case TOR:
118 case TAND:
119 fptreef(shf, indent, "%T%s %T",
Geremy Condra03ebf062011-10-12 18:17:24 -0700120 t->left, (t->type == TOR) ? "||" : "&&", t->right);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700121 break;
122 case TBANG:
123 shf_puts("! ", shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700124 prevent_semicolon = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700125 t = t->right;
126 goto Chain;
Geremy Condra03ebf062011-10-12 18:17:24 -0700127 case TDBRACKET:
128 w = t->args;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700129 shf_puts("[[", shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700130 while (*w)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700131 fptreef(shf, indent, Tf__S, *w++);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700132 shf_puts(" ]] ", shf);
133 break;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700134 case TSELECT:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700135 case TFOR:
Geremy Condra03ebf062011-10-12 18:17:24 -0700136 fptreef(shf, indent, "%s %s ",
137 (t->type == TFOR) ? "for" : Tselect, t->str);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700138 if (t->vars != NULL) {
139 shf_puts("in ", shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700140 w = (const char **)t->vars;
141 while (*w)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700142 fptreef(shf, indent, Tf_S_, *w++);
143 fptreef(shf, indent, Tft_end);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700144 }
145 fptreef(shf, indent + INDENT, "do%N%T", t->left);
146 fptreef(shf, indent, "%;done ");
147 break;
148 case TCASE:
149 fptreef(shf, indent, "case %S in", t->str);
150 for (t1 = t->left; t1 != NULL; t1 = t1->right) {
151 fptreef(shf, indent, "%N(");
Geremy Condra03ebf062011-10-12 18:17:24 -0700152 w = (const char **)t1->vars;
153 while (*w) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700154 fptreef(shf, indent, "%S%c", *w,
155 (w[1] != NULL) ? '|' : ')');
Geremy Condra03ebf062011-10-12 18:17:24 -0700156 ++w;
157 }
158 fptreef(shf, indent + INDENT, "%N%T%N;%c", t1->left,
159 t1->u.charflag);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700160 }
161 fptreef(shf, indent, "%Nesac ");
162 break;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700163 case TELIF:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700164 internal_errorf(TELIF_unexpected);
Geremy Condra03ebf062011-10-12 18:17:24 -0700165 /* FALLTHROUGH */
Geremy Condra03ebf062011-10-12 18:17:24 -0700166 case TIF:
167 i = 2;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000168 t1 = t;
Geremy Condra03ebf062011-10-12 18:17:24 -0700169 goto process_TIF;
170 do {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000171 t1 = t1->right;
Geremy Condra03ebf062011-10-12 18:17:24 -0700172 i = 0;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700173 fptreef(shf, indent, Tft_end);
Geremy Condra03ebf062011-10-12 18:17:24 -0700174 process_TIF:
175 /* 5 == strlen("elif ") */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000176 fptreef(shf, indent + 5 - i, Telif_pT + i, t1->left);
177 t1 = t1->right;
178 if (t1->left != NULL) {
Elliott Hughes77740fc2016-08-12 15:06:53 -0700179 fptreef(shf, indent, Tft_end);
Geremy Condra03ebf062011-10-12 18:17:24 -0700180 fptreef(shf, indent + INDENT, "%s%N%T",
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000181 "then", t1->left);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700182 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000183 } while (t1->right && t1->right->type == TELIF);
184 if (t1->right != NULL) {
Elliott Hughes77740fc2016-08-12 15:06:53 -0700185 fptreef(shf, indent, Tft_end);
Geremy Condra03ebf062011-10-12 18:17:24 -0700186 fptreef(shf, indent + INDENT, "%s%N%T",
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000187 "else", t1->right);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700188 }
189 fptreef(shf, indent, "%;fi ");
190 break;
191 case TWHILE:
192 case TUNTIL:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000193 /* 6 == strlen("while "/"until ") */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700194 fptreef(shf, indent + 6, Tf_s_T,
Geremy Condra03ebf062011-10-12 18:17:24 -0700195 (t->type == TWHILE) ? "while" : "until",
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700196 t->left);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700197 fptreef(shf, indent, Tft_end);
Geremy Condra03ebf062011-10-12 18:17:24 -0700198 fptreef(shf, indent + INDENT, "do%N%T", t->right);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700199 fptreef(shf, indent, "%;done ");
200 break;
201 case TBRACE:
Geremy Condra03ebf062011-10-12 18:17:24 -0700202 fptreef(shf, indent + INDENT, "{%N%T", t->left);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700203 fptreef(shf, indent, "%;} ");
204 break;
205 case TCOPROC:
206 fptreef(shf, indent, "%T|& ", t->left);
Geremy Condra03ebf062011-10-12 18:17:24 -0700207 prevent_semicolon = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700208 break;
209 case TASYNC:
210 fptreef(shf, indent, "%T& ", t->left);
Geremy Condra03ebf062011-10-12 18:17:24 -0700211 prevent_semicolon = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700212 break;
213 case TFUNCT:
Geremy Condra03ebf062011-10-12 18:17:24 -0700214 fpFUNCTf(shf, indent, tobool(t->u.ksh_func), t->str, t->left);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700215 break;
216 case TTIME:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700217 fptreef(shf, indent, Tf_s_T, Ttime, t->left);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700218 break;
219 default:
220 shf_puts("<botch>", shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700221 prevent_semicolon = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700222 break;
223 }
224 if ((ioact = t->ioact) != NULL) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700225 bool need_nl = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700226
227 while (*ioact != NULL)
Thorsten Glaser811a5752013-07-25 14:24:45 +0000228 pioact(shf, *ioact++);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700229 /* Print here documents after everything else... */
Geremy Condra03ebf062011-10-12 18:17:24 -0700230 ioact = t->ioact;
231 while (*ioact != NULL) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700232 struct ioword *iop = *ioact++;
233
Geremy Condra03ebf062011-10-12 18:17:24 -0700234 /* heredoc is NULL when tracing (set -x) */
Elliott Hughesb27ce952015-04-21 13:39:18 -0700235 if ((iop->ioflag & (IOTYPE | IOHERESTR)) == IOHERE &&
Geremy Condra03ebf062011-10-12 18:17:24 -0700236 iop->heredoc) {
237 shf_putc('\n', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700238 shf_puts(iop->heredoc, shf);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700239 fptreef(shf, indent, Tf_s,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700240 evalstr(iop->delim, 0));
Geremy Condra03ebf062011-10-12 18:17:24 -0700241 need_nl = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700242 }
243 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700244 /*
245 * Last delimiter must be followed by a newline (this
246 * often leads to an extra blank line, but it's not
247 * worth worrying about)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700248 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000249 if (need_nl) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700250 shf_putc('\n', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000251 prevent_semicolon = true;
252 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700253 }
254}
255
256static void
Thorsten Glaser811a5752013-07-25 14:24:45 +0000257pioact(struct shf *shf, struct ioword *iop)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700258{
Elliott Hughesb27ce952015-04-21 13:39:18 -0700259 unsigned short flag = iop->ioflag;
260 unsigned short type = flag & IOTYPE;
261 short expected;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700262
263 expected = (type == IOREAD || type == IORDWR || type == IOHERE) ? 0 :
264 (type == IOCAT || type == IOWRITE) ? 1 :
265 (type == IODUP && (iop->unit == !(flag & IORDUP))) ? iop->unit :
266 iop->unit + 1;
267 if (iop->unit != expected)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700268 shf_fprintf(shf, Tf_d, (int)iop->unit);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700269
270 switch (type) {
271 case IOREAD:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000272 shf_putc('<', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700273 break;
274 case IOHERE:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000275 shf_puts("<<", shf);
276 if (flag & IOSKIP)
277 shf_putc('-', shf);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800278 else if (flag & IOHERESTR)
279 shf_putc('<', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700280 break;
281 case IOCAT:
Geremy Condra03ebf062011-10-12 18:17:24 -0700282 shf_puts(">>", shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700283 break;
284 case IOWRITE:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000285 shf_putc('>', shf);
286 if (flag & IOCLOB)
287 shf_putc('|', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700288 break;
289 case IORDWR:
Geremy Condra03ebf062011-10-12 18:17:24 -0700290 shf_puts("<>", shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700291 break;
292 case IODUP:
293 shf_puts(flag & IORDUP ? "<&" : ">&", shf);
294 break;
295 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700296 /* name/delim are NULL when printing syntax errors */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700297 if (type == IOHERE) {
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800298 if (iop->delim && !(iop->ioflag & IONDELIM))
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000299 wdvarput(shf, iop->delim, 0, WDS_TPUTS);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800300 } else if (iop->ioname) {
Elliott Hughesb27ce952015-04-21 13:39:18 -0700301 if (flag & IONAMEXP)
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800302 print_value_quoted(shf, iop->ioname);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000303 else
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800304 wdvarput(shf, iop->ioname, 0, WDS_TPUTS);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000305 }
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800306 shf_putc(' ', shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700307 prevent_semicolon = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700308}
309
Geremy Condra03ebf062011-10-12 18:17:24 -0700310/* variant of fputs for ptreef and wdstrip */
311static const char *
312wdvarput(struct shf *shf, const char *wp, int quotelevel, int opmode)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700313{
Geremy Condra03ebf062011-10-12 18:17:24 -0700314 int c;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000315 const char *cs;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700316
Geremy Condra03ebf062011-10-12 18:17:24 -0700317 /*-
318 * problems:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700319 * `...` -> $(...)
320 * 'foo' -> "foo"
Geremy Condra03ebf062011-10-12 18:17:24 -0700321 * x${foo:-"hi"} -> x${foo:-hi} unless WDS_TPUTS
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800322 * x${foo:-'hi'} -> x${foo:-hi}
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700323 * could change encoding to:
324 * OQUOTE ["'] ... CQUOTE ["']
325 * COMSUB [(`] ...\0 (handle $ ` \ and maybe " in `...` case)
326 */
Geremy Condra03ebf062011-10-12 18:17:24 -0700327 while (/* CONSTCOND */ 1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700328 switch (*wp++) {
329 case EOS:
Geremy Condra03ebf062011-10-12 18:17:24 -0700330 return (--wp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700331 case ADELIM:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800332 if (ord(*wp) == ORD(/*{*/ '}')) {
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800333 ++wp;
334 goto wdvarput_csubst;
335 }
Elliott Hughes77740fc2016-08-12 15:06:53 -0700336 /* FALLTHROUGH */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700337 case CHAR:
Elliott Hughes23925bb2017-09-22 16:04:20 -0700338 c = ord(*wp++);
Geremy Condra03ebf062011-10-12 18:17:24 -0700339 shf_putc(c, shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700340 break;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700341 case QCHAR:
Elliott Hughes23925bb2017-09-22 16:04:20 -0700342 c = ord(*wp++);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700343 if (opmode & WDS_TPUTS)
344 switch (c) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800345 case ORD('\n'):
Elliott Hughes77740fc2016-08-12 15:06:53 -0700346 if (quotelevel == 0) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800347 c = ORD('\'');
Elliott Hughes77740fc2016-08-12 15:06:53 -0700348 shf_putc(c, shf);
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800349 shf_putc(ORD('\n'), shf);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700350 }
351 break;
352 default:
353 if (quotelevel == 0)
354 /* FALLTHROUGH */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800355 case ORD('"'):
356 case ORD('`'):
357 case ORD('$'):
358 case ORD('\\'):
359 shf_putc(ORD('\\'), shf);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700360 break;
361 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700362 shf_putc(c, shf);
363 break;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700364 case COMASUB:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700365 case COMSUB:
366 shf_puts("$(", shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000367 cs = ")";
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800368 if (ord(*wp) == ORD('(' /*)*/))
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700369 shf_putc(' ', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000370 pSUB:
Geremy Condra03ebf062011-10-12 18:17:24 -0700371 while ((c = *wp++) != 0)
372 shf_putc(c, shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000373 shf_puts(cs, shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700374 break;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700375 case FUNASUB:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000376 case FUNSUB:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800377 c = ORD(' ');
Thorsten Glaser811a5752013-07-25 14:24:45 +0000378 if (0)
379 /* FALLTHROUGH */
380 case VALSUB:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800381 c = ORD('|');
Thorsten Glaser811a5752013-07-25 14:24:45 +0000382 shf_putc('$', shf);
383 shf_putc('{', shf);
384 shf_putc(c, shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000385 cs = ";}";
386 goto pSUB;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700387 case EXPRSUB:
388 shf_puts("$((", shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000389 cs = "))";
390 goto pSUB;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700391 case OQUOTE:
Geremy Condra03ebf062011-10-12 18:17:24 -0700392 if (opmode & WDS_TPUTS) {
393 quotelevel++;
394 shf_putc('"', shf);
395 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700396 break;
397 case CQUOTE:
Geremy Condra03ebf062011-10-12 18:17:24 -0700398 if (opmode & WDS_TPUTS) {
399 if (quotelevel)
400 quotelevel--;
401 shf_putc('"', shf);
402 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700403 break;
404 case OSUBST:
Geremy Condra03ebf062011-10-12 18:17:24 -0700405 shf_putc('$', shf);
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800406 if (ord(*wp++) == ORD('{'))
Geremy Condra03ebf062011-10-12 18:17:24 -0700407 shf_putc('{', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700408 while ((c = *wp++) != 0)
Geremy Condra03ebf062011-10-12 18:17:24 -0700409 shf_putc(c, shf);
410 wp = wdvarput(shf, wp, 0, opmode);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700411 break;
412 case CSUBST:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800413 if (ord(*wp++) == ORD('}')) {
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800414 wdvarput_csubst:
Geremy Condra03ebf062011-10-12 18:17:24 -0700415 shf_putc('}', shf);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800416 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700417 return (wp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700418 case OPAT:
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800419 shf_putchar(*wp++, shf);
420 shf_putc('(', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700421 break;
422 case SPAT:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800423 c = ORD('|');
Geremy Condra03ebf062011-10-12 18:17:24 -0700424 if (0)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700425 /* FALLTHROUGH */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700426 case CPAT:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800427 c = ORD(/*(*/ ')');
Geremy Condra03ebf062011-10-12 18:17:24 -0700428 shf_putc(c, shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700429 break;
430 }
431}
432
433/*
434 * this is the _only_ way to reliably handle
435 * variable args with an ANSI compiler
436 */
437/* VARARGS */
Geremy Condra03ebf062011-10-12 18:17:24 -0700438void
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700439fptreef(struct shf *shf, int indent, const char *fmt, ...)
440{
441 va_list va;
442
443 va_start(va, fmt);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700444 vfptreef(shf, indent, fmt, va);
445 va_end(va);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700446}
447
448/* VARARGS */
449char *
Geremy Condra03ebf062011-10-12 18:17:24 -0700450snptreef(char *s, ssize_t n, const char *fmt, ...)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700451{
452 va_list va;
453 struct shf shf;
454
455 shf_sopen(s, n, SHF_WR | (s ? 0 : SHF_DYNAMIC), &shf);
456
457 va_start(va, fmt);
458 vfptreef(&shf, 0, fmt, va);
459 va_end(va);
460
Geremy Condra03ebf062011-10-12 18:17:24 -0700461 /* shf_sclose NUL terminates */
462 return (shf_sclose(&shf));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700463}
464
465static void
466vfptreef(struct shf *shf, int indent, const char *fmt, va_list va)
467{
468 int c;
469
Elliott Hughes23925bb2017-09-22 16:04:20 -0700470 while ((c = ord(*fmt++))) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700471 if (c == '%') {
Elliott Hughes23925bb2017-09-22 16:04:20 -0700472 switch ((c = ord(*fmt++))) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800473 case ORD('c'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700474 /* character (octet, probably) */
475 shf_putchar(va_arg(va, int), shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700476 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800477 case ORD('s'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700478 /* string */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700479 shf_puts(va_arg(va, char *), shf);
480 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800481 case ORD('S'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700482 /* word */
483 wdvarput(shf, va_arg(va, char *), 0, WDS_TPUTS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700484 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800485 case ORD('d'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700486 /* signed decimal */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700487 shf_fprintf(shf, Tf_d, va_arg(va, int));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700488 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800489 case ORD('u'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700490 /* unsigned decimal */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700491 shf_fprintf(shf, "%u", va_arg(va, unsigned int));
492 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800493 case ORD('T'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700494 /* format tree */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700495 ptree(va_arg(va, struct op *), indent, shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700496 goto dont_trash_prevent_semicolon;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800497 case ORD(';'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700498 /* newline or ; */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800499 case ORD('N'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700500 /* newline or space */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700501 if (shf->flags & SHF_STRING) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800502 if ((unsigned int)c == ORD(';') &&
503 !prevent_semicolon)
Geremy Condra03ebf062011-10-12 18:17:24 -0700504 shf_putc(';', shf);
505 shf_putc(' ', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700506 } else {
507 int i;
508
Geremy Condra03ebf062011-10-12 18:17:24 -0700509 shf_putc('\n', shf);
510 i = indent;
511 while (i >= 8) {
512 shf_putc('\t', shf);
513 i -= 8;
514 }
515 while (i--)
516 shf_putc(' ', shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700517 }
518 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800519 case ORD('R'):
Geremy Condra03ebf062011-10-12 18:17:24 -0700520 /* I/O redirection */
Thorsten Glaser811a5752013-07-25 14:24:45 +0000521 pioact(shf, va_arg(va, struct ioword *));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700522 break;
523 default:
Geremy Condra03ebf062011-10-12 18:17:24 -0700524 shf_putc(c, shf);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700525 break;
526 }
527 } else
Geremy Condra03ebf062011-10-12 18:17:24 -0700528 shf_putc(c, shf);
529 prevent_semicolon = false;
530 dont_trash_prevent_semicolon:
531 ;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700532 }
533}
534
535/*
536 * copy tree (for function definition)
537 */
538struct op *
539tcopy(struct op *t, Area *ap)
540{
541 struct op *r;
542 const char **tw;
543 char **rw;
544
545 if (t == NULL)
546 return (NULL);
547
548 r = alloc(sizeof(struct op), ap);
549
550 r->type = t->type;
551 r->u.evalflags = t->u.evalflags;
552
553 if (t->type == TCASE)
554 r->str = wdcopy(t->str, ap);
555 else
556 strdupx(r->str, t->str, ap);
557
558 if (t->vars == NULL)
559 r->vars = NULL;
560 else {
Geremy Condra03ebf062011-10-12 18:17:24 -0700561 tw = (const char **)t->vars;
562 while (*tw)
563 ++tw;
564 rw = r->vars = alloc2(tw - (const char **)t->vars + 1,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700565 sizeof(*tw), ap);
Geremy Condra03ebf062011-10-12 18:17:24 -0700566 tw = (const char **)t->vars;
567 while (*tw)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700568 *rw++ = wdcopy(*tw++, ap);
569 *rw = NULL;
570 }
571
572 if (t->args == NULL)
573 r->args = NULL;
574 else {
Geremy Condra03ebf062011-10-12 18:17:24 -0700575 tw = t->args;
576 while (*tw)
577 ++tw;
578 r->args = (const char **)(rw = alloc2(tw - t->args + 1,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700579 sizeof(*tw), ap));
Geremy Condra03ebf062011-10-12 18:17:24 -0700580 tw = t->args;
581 while (*tw)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700582 *rw++ = wdcopy(*tw++, ap);
583 *rw = NULL;
584 }
585
586 r->ioact = (t->ioact == NULL) ? NULL : iocopy(t->ioact, ap);
587
588 r->left = tcopy(t->left, ap);
589 r->right = tcopy(t->right, ap);
590 r->lineno = t->lineno;
591
592 return (r);
593}
594
595char *
596wdcopy(const char *wp, Area *ap)
597{
Geremy Condra03ebf062011-10-12 18:17:24 -0700598 size_t len;
599
600 len = wdscan(wp, EOS) - wp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700601 return (memcpy(alloc(len, ap), wp, len));
602}
603
604/* return the position of prefix c in wp plus 1 */
605const char *
606wdscan(const char *wp, int c)
607{
608 int nest = 0;
609
Geremy Condra03ebf062011-10-12 18:17:24 -0700610 while (/* CONSTCOND */ 1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700611 switch (*wp++) {
612 case EOS:
613 return (wp);
614 case ADELIM:
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800615 if (c == ADELIM && nest == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700616 return (wp + 1);
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800617 if (ord(*wp) == ORD(/*{*/ '}'))
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800618 goto wdscan_csubst;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700619 /* FALLTHROUGH */
620 case CHAR:
621 case QCHAR:
622 wp++;
623 break;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700624 case COMASUB:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700625 case COMSUB:
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700626 case FUNASUB:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000627 case FUNSUB:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000628 case VALSUB:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700629 case EXPRSUB:
630 while (*wp++ != 0)
631 ;
632 break;
633 case OQUOTE:
634 case CQUOTE:
635 break;
636 case OSUBST:
637 nest++;
638 while (*wp++ != '\0')
639 ;
640 break;
641 case CSUBST:
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800642 wdscan_csubst:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700643 wp++;
644 if (c == CSUBST && nest == 0)
645 return (wp);
646 nest--;
647 break;
648 case OPAT:
649 nest++;
650 wp++;
651 break;
652 case SPAT:
653 case CPAT:
654 if (c == wp[-1] && nest == 0)
655 return (wp);
656 if (wp[-1] == CPAT)
657 nest--;
658 break;
659 default:
660 internal_warningf(
Elliott Hughes77740fc2016-08-12 15:06:53 -0700661 "wdscan: unknown char 0x%X (carrying on)",
662 (unsigned char)wp[-1]);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700663 }
664}
665
Geremy Condra03ebf062011-10-12 18:17:24 -0700666/*
667 * return a copy of wp without any of the mark up characters and with
668 * quote characters (" ' \) stripped. (string is allocated from ATEMP)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700669 */
670char *
Geremy Condra03ebf062011-10-12 18:17:24 -0700671wdstrip(const char *wp, int opmode)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700672{
673 struct shf shf;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700674
675 shf_sopen(NULL, 32, SHF_WR | SHF_DYNAMIC, &shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700676 wdvarput(&shf, wp, 0, opmode);
677 /* shf_sclose NUL terminates */
678 return (shf_sclose(&shf));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700679}
680
681static struct ioword **
682iocopy(struct ioword **iow, Area *ap)
683{
684 struct ioword **ior;
685 int i;
686
Geremy Condra03ebf062011-10-12 18:17:24 -0700687 ior = iow;
688 while (*ior)
689 ++ior;
690 ior = alloc2(ior - iow + 1, sizeof(struct ioword *), ap);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700691
692 for (i = 0; iow[i] != NULL; i++) {
693 struct ioword *p, *q;
694
695 p = iow[i];
696 q = alloc(sizeof(struct ioword), ap);
697 ior[i] = q;
698 *q = *p;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800699 if (p->ioname != NULL)
700 q->ioname = wdcopy(p->ioname, ap);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700701 if (p->delim != NULL)
702 q->delim = wdcopy(p->delim, ap);
703 if (p->heredoc != NULL)
704 strdupx(q->heredoc, p->heredoc, ap);
705 }
706 ior[i] = NULL;
707
708 return (ior);
709}
710
711/*
712 * free tree (for function definition)
713 */
714void
715tfree(struct op *t, Area *ap)
716{
717 char **w;
718
719 if (t == NULL)
720 return;
721
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800722 afree(t->str, ap);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700723
724 if (t->vars != NULL) {
725 for (w = t->vars; *w != NULL; w++)
726 afree(*w, ap);
727 afree(t->vars, ap);
728 }
729
730 if (t->args != NULL) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700731 /*XXX we assume the caller is right */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700732 union mksh_ccphack cw;
Geremy Condra03ebf062011-10-12 18:17:24 -0700733
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700734 cw.ro = t->args;
735 for (w = cw.rw; *w != NULL; w++)
736 afree(*w, ap);
737 afree(t->args, ap);
738 }
739
740 if (t->ioact != NULL)
741 iofree(t->ioact, ap);
742
743 tfree(t->left, ap);
744 tfree(t->right, ap);
745
746 afree(t, ap);
747}
748
749static void
750iofree(struct ioword **iow, Area *ap)
751{
752 struct ioword **iop;
753 struct ioword *p;
754
Geremy Condra03ebf062011-10-12 18:17:24 -0700755 iop = iow;
756 while ((p = *iop++) != NULL) {
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800757 afree(p->ioname, ap);
758 afree(p->delim, ap);
759 afree(p->heredoc, ap);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700760 afree(p, ap);
761 }
762 afree(iow, ap);
763}
Geremy Condra03ebf062011-10-12 18:17:24 -0700764
765void
766fpFUNCTf(struct shf *shf, int i, bool isksh, const char *k, struct op *v)
767{
768 if (isksh)
769 fptreef(shf, i, "%s %s %T", Tfunction, k, v);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800770 else if (ktsearch(&keywords, k, hash(k)))
771 fptreef(shf, i, "%s %s() %T", Tfunction, k, v);
Geremy Condra03ebf062011-10-12 18:17:24 -0700772 else
773 fptreef(shf, i, "%s() %T", k, v);
774}
775
776
777/* for jobs.c */
778void
779vistree(char *dst, size_t sz, struct op *t)
780{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000781 unsigned int c;
Geremy Condra03ebf062011-10-12 18:17:24 -0700782 char *cp, *buf;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000783 size_t n;
Geremy Condra03ebf062011-10-12 18:17:24 -0700784
Thorsten Glaser811a5752013-07-25 14:24:45 +0000785 buf = alloc(sz + 16, ATEMP);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700786 snptreef(buf, sz + 16, Tf_T, t);
Geremy Condra03ebf062011-10-12 18:17:24 -0700787 cp = buf;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000788 vist_loop:
789 if (UTFMODE && (n = utf_mbtowc(&c, cp)) != (size_t)-1) {
790 if (c == 0 || n >= sz)
791 /* NUL or not enough free space */
792 goto vist_out;
793 /* copy multibyte char */
794 sz -= n;
795 while (n--)
796 *dst++ = *cp++;
797 goto vist_loop;
Geremy Condra03ebf062011-10-12 18:17:24 -0700798 }
Elliott Hughes23925bb2017-09-22 16:04:20 -0700799 if (--sz == 0 || (c = ord(*cp++)) == 0)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000800 /* NUL or not enough free space */
801 goto vist_out;
Elliott Hughes23925bb2017-09-22 16:04:20 -0700802 if (ksh_isctrl(c)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000803 /* C0 or C1 control character or DEL */
804 if (--sz == 0)
805 /* not enough free space for two chars */
806 goto vist_out;
Elliott Hughes23925bb2017-09-22 16:04:20 -0700807 *dst++ = '^';
808 c = ksh_unctrl(c);
809 } else if (UTFMODE && rtt2asc(c) > 0x7F) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000810 /* better not try to display broken multibyte chars */
Elliott Hughes47086262019-03-26 12:34:31 -0700811 /* also go easy on the UCS: no U+FFFD here */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800812 c = ORD('?');
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000813 }
814 *dst++ = c;
815 goto vist_loop;
816
817 vist_out:
Geremy Condra03ebf062011-10-12 18:17:24 -0700818 *dst = '\0';
819 afree(buf, ATEMP);
820}
821
822#ifdef DEBUG
823void
Elliott Hughes47086262019-03-26 12:34:31 -0700824dumpchar(struct shf *shf, unsigned char c)
Geremy Condra03ebf062011-10-12 18:17:24 -0700825{
Elliott Hughes23925bb2017-09-22 16:04:20 -0700826 if (ksh_isctrl(c)) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700827 /* C0 or C1 control character or DEL */
Elliott Hughes23925bb2017-09-22 16:04:20 -0700828 shf_putc('^', shf);
829 c = ksh_unctrl(c);
Geremy Condra03ebf062011-10-12 18:17:24 -0700830 }
831 shf_putc(c, shf);
832}
833
834/* see: wdvarput */
835static const char *
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000836dumpwdvar_i(struct shf *shf, const char *wp, int quotelevel)
Geremy Condra03ebf062011-10-12 18:17:24 -0700837{
838 int c;
839
840 while (/* CONSTCOND */ 1) {
841 switch(*wp++) {
842 case EOS:
843 shf_puts("EOS", shf);
844 return (--wp);
845 case ADELIM:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800846 if (ord(*wp) == ORD(/*{*/ '}')) {
Elliott Hughes77740fc2016-08-12 15:06:53 -0700847 shf_puts(/*{*/ "]ADELIM(})", shf);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800848 return (wp + 1);
849 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700850 shf_puts("ADELIM=", shf);
851 if (0)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700852 /* FALLTHROUGH */
Geremy Condra03ebf062011-10-12 18:17:24 -0700853 case CHAR:
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700854 shf_puts("CHAR=", shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700855 dumpchar(shf, *wp++);
856 break;
857 case QCHAR:
858 shf_puts("QCHAR<", shf);
Elliott Hughes23925bb2017-09-22 16:04:20 -0700859 c = ord(*wp++);
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800860 if (quotelevel == 0 || c == ORD('"') ||
861 c == ORD('\\') || ctype(c, C_DOLAR | C_GRAVE))
Geremy Condra03ebf062011-10-12 18:17:24 -0700862 shf_putc('\\', shf);
863 dumpchar(shf, c);
864 goto closeandout;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700865 case COMASUB:
866 shf_puts("COMASUB<", shf);
867 goto dumpsub;
Geremy Condra03ebf062011-10-12 18:17:24 -0700868 case COMSUB:
869 shf_puts("COMSUB<", shf);
870 dumpsub:
871 while ((c = *wp++) != 0)
872 dumpchar(shf, c);
873 closeandout:
874 shf_putc('>', shf);
875 break;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700876 case FUNASUB:
877 shf_puts("FUNASUB<", shf);
878 goto dumpsub;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000879 case FUNSUB:
880 shf_puts("FUNSUB<", shf);
881 goto dumpsub;
Thorsten Glaser811a5752013-07-25 14:24:45 +0000882 case VALSUB:
883 shf_puts("VALSUB<", shf);
884 goto dumpsub;
Geremy Condra03ebf062011-10-12 18:17:24 -0700885 case EXPRSUB:
886 shf_puts("EXPRSUB<", shf);
887 goto dumpsub;
888 case OQUOTE:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700889 shf_fprintf(shf, "OQUOTE{%d" /*}*/, ++quotelevel);
Geremy Condra03ebf062011-10-12 18:17:24 -0700890 break;
891 case CQUOTE:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700892 shf_fprintf(shf, /*{*/ "%d}CQUOTE", quotelevel);
Geremy Condra03ebf062011-10-12 18:17:24 -0700893 if (quotelevel)
894 quotelevel--;
895 else
896 shf_puts("(err)", shf);
897 break;
898 case OSUBST:
899 shf_puts("OSUBST(", shf);
900 dumpchar(shf, *wp++);
901 shf_puts(")[", shf);
902 while ((c = *wp++) != 0)
903 dumpchar(shf, c);
904 shf_putc('|', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000905 wp = dumpwdvar_i(shf, wp, 0);
Geremy Condra03ebf062011-10-12 18:17:24 -0700906 break;
907 case CSUBST:
908 shf_puts("]CSUBST(", shf);
909 dumpchar(shf, *wp++);
910 shf_putc(')', shf);
911 return (wp);
912 case OPAT:
913 shf_puts("OPAT=", shf);
914 dumpchar(shf, *wp++);
915 break;
916 case SPAT:
917 shf_puts("SPAT", shf);
918 break;
919 case CPAT:
920 shf_puts("CPAT", shf);
921 break;
922 default:
923 shf_fprintf(shf, "INVAL<%u>", (uint8_t)wp[-1]);
924 break;
925 }
926 shf_putc(' ', shf);
927 }
928}
929void
930dumpwdvar(struct shf *shf, const char *wp)
931{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000932 dumpwdvar_i(shf, wp, 0);
933}
934
935void
936dumpioact(struct shf *shf, struct op *t)
937{
938 struct ioword **ioact, *iop;
939
940 if ((ioact = t->ioact) == NULL)
941 return;
942
943 shf_puts("{IOACT", shf);
944 while ((iop = *ioact++) != NULL) {
Elliott Hughesb27ce952015-04-21 13:39:18 -0700945 unsigned short type = iop->ioflag & IOTYPE;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000946#define DT(x) case x: shf_puts(#x, shf); break;
Elliott Hughesb27ce952015-04-21 13:39:18 -0700947#define DB(x) if (iop->ioflag & x) shf_puts("|" #x, shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000948
949 shf_putc(';', shf);
950 switch (type) {
951 DT(IOREAD)
952 DT(IOWRITE)
953 DT(IORDWR)
954 DT(IOHERE)
955 DT(IOCAT)
956 DT(IODUP)
957 default:
958 shf_fprintf(shf, "unk%d", type);
959 }
960 DB(IOEVAL)
961 DB(IOSKIP)
962 DB(IOCLOB)
963 DB(IORDUP)
964 DB(IONAMEXP)
965 DB(IOBASH)
966 DB(IOHERESTR)
967 DB(IONDELIM)
Elliott Hughesb27ce952015-04-21 13:39:18 -0700968 shf_fprintf(shf, ",unit=%d", (int)iop->unit);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800969 if (iop->delim && !(iop->ioflag & IONDELIM)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000970 shf_puts(",delim<", shf);
971 dumpwdvar(shf, iop->delim);
972 shf_putc('>', shf);
973 }
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800974 if (iop->ioname) {
Elliott Hughesb27ce952015-04-21 13:39:18 -0700975 if (iop->ioflag & IONAMEXP) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000976 shf_puts(",name=", shf);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800977 print_value_quoted(shf, iop->ioname);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000978 } else {
979 shf_puts(",name<", shf);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800980 dumpwdvar(shf, iop->ioname);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000981 shf_putc('>', shf);
982 }
983 }
984 if (iop->heredoc) {
985 shf_puts(",heredoc=", shf);
986 print_value_quoted(shf, iop->heredoc);
987 }
988#undef DT
989#undef DB
990 }
991 shf_putc('}', shf);
Geremy Condra03ebf062011-10-12 18:17:24 -0700992}
993
994void
995dumptree(struct shf *shf, struct op *t)
996{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000997 int i, j;
Geremy Condra03ebf062011-10-12 18:17:24 -0700998 const char **w, *name;
999 struct op *t1;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001000 static int nesting;
Geremy Condra03ebf062011-10-12 18:17:24 -07001001
1002 for (i = 0; i < nesting; ++i)
1003 shf_putc('\t', shf);
1004 ++nesting;
1005 shf_puts("{tree:" /*}*/, shf);
1006 if (t == NULL) {
1007 name = "(null)";
1008 goto out;
1009 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001010 dumpioact(shf, t);
Geremy Condra03ebf062011-10-12 18:17:24 -07001011 switch (t->type) {
1012#define OPEN(x) case x: name = #x; shf_puts(" {" #x ":", shf); /*}*/
1013
1014 OPEN(TCOM)
1015 if (t->vars) {
1016 i = 0;
1017 w = (const char **)t->vars;
1018 while (*w) {
1019 shf_putc('\n', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001020 for (j = 0; j < nesting; ++j)
Geremy Condra03ebf062011-10-12 18:17:24 -07001021 shf_putc('\t', shf);
1022 shf_fprintf(shf, " var%d<", i++);
1023 dumpwdvar(shf, *w++);
1024 shf_putc('>', shf);
1025 }
1026 } else
1027 shf_puts(" #no-vars#", shf);
1028 if (t->args) {
1029 i = 0;
1030 w = t->args;
1031 while (*w) {
1032 shf_putc('\n', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001033 for (j = 0; j < nesting; ++j)
Geremy Condra03ebf062011-10-12 18:17:24 -07001034 shf_putc('\t', shf);
1035 shf_fprintf(shf, " arg%d<", i++);
1036 dumpwdvar(shf, *w++);
1037 shf_putc('>', shf);
1038 }
1039 } else
1040 shf_puts(" #no-args#", shf);
1041 break;
1042 OPEN(TEXEC)
1043 dumpleftandout:
1044 t = t->left;
1045 dumpandout:
1046 shf_putc('\n', shf);
1047 dumptree(shf, t);
1048 break;
1049 OPEN(TPAREN)
1050 goto dumpleftandout;
1051 OPEN(TPIPE)
1052 dumpleftmidrightandout:
1053 shf_putc('\n', shf);
1054 dumptree(shf, t->left);
1055/* middumprightandout: (unused) */
1056 shf_fprintf(shf, "/%s:", name);
1057 dumprightandout:
1058 t = t->right;
1059 goto dumpandout;
1060 OPEN(TLIST)
1061 goto dumpleftmidrightandout;
1062 OPEN(TOR)
1063 goto dumpleftmidrightandout;
1064 OPEN(TAND)
1065 goto dumpleftmidrightandout;
1066 OPEN(TBANG)
1067 goto dumprightandout;
1068 OPEN(TDBRACKET)
1069 i = 0;
1070 w = t->args;
1071 while (*w) {
1072 shf_putc('\n', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001073 for (j = 0; j < nesting; ++j)
Geremy Condra03ebf062011-10-12 18:17:24 -07001074 shf_putc('\t', shf);
1075 shf_fprintf(shf, " arg%d<", i++);
1076 dumpwdvar(shf, *w++);
1077 shf_putc('>', shf);
1078 }
1079 break;
1080 OPEN(TFOR)
1081 dumpfor:
1082 shf_fprintf(shf, " str<%s>", t->str);
1083 if (t->vars != NULL) {
1084 i = 0;
1085 w = (const char **)t->vars;
1086 while (*w) {
1087 shf_putc('\n', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001088 for (j = 0; j < nesting; ++j)
Geremy Condra03ebf062011-10-12 18:17:24 -07001089 shf_putc('\t', shf);
1090 shf_fprintf(shf, " var%d<", i++);
1091 dumpwdvar(shf, *w++);
1092 shf_putc('>', shf);
1093 }
1094 }
1095 goto dumpleftandout;
1096 OPEN(TSELECT)
1097 goto dumpfor;
1098 OPEN(TCASE)
1099 shf_fprintf(shf, " str<%s>", t->str);
1100 i = 0;
1101 for (t1 = t->left; t1 != NULL; t1 = t1->right) {
1102 shf_putc('\n', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001103 for (j = 0; j < nesting; ++j)
Geremy Condra03ebf062011-10-12 18:17:24 -07001104 shf_putc('\t', shf);
1105 shf_fprintf(shf, " sub%d[(", i);
1106 w = (const char **)t1->vars;
1107 while (*w) {
1108 dumpwdvar(shf, *w);
1109 if (w[1] != NULL)
1110 shf_putc('|', shf);
1111 ++w;
1112 }
1113 shf_putc(')', shf);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001114 dumpioact(shf, t);
Geremy Condra03ebf062011-10-12 18:17:24 -07001115 shf_putc('\n', shf);
1116 dumptree(shf, t1->left);
1117 shf_fprintf(shf, " ;%c/%d]", t1->u.charflag, i++);
1118 }
1119 break;
1120 OPEN(TWHILE)
1121 goto dumpleftmidrightandout;
1122 OPEN(TUNTIL)
1123 goto dumpleftmidrightandout;
1124 OPEN(TBRACE)
1125 goto dumpleftandout;
1126 OPEN(TCOPROC)
1127 goto dumpleftandout;
1128 OPEN(TASYNC)
1129 goto dumpleftandout;
1130 OPEN(TFUNCT)
1131 shf_fprintf(shf, " str<%s> ksh<%s>", t->str,
Elliott Hughes77740fc2016-08-12 15:06:53 -07001132 t->u.ksh_func ? Ttrue : Tfalse);
Geremy Condra03ebf062011-10-12 18:17:24 -07001133 goto dumpleftandout;
1134 OPEN(TTIME)
1135 goto dumpleftandout;
1136 OPEN(TIF)
1137 dumpif:
1138 shf_putc('\n', shf);
1139 dumptree(shf, t->left);
1140 t = t->right;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001141 dumpioact(shf, t);
Geremy Condra03ebf062011-10-12 18:17:24 -07001142 if (t->left != NULL) {
1143 shf_puts(" /TTHEN:\n", shf);
1144 dumptree(shf, t->left);
1145 }
1146 if (t->right && t->right->type == TELIF) {
1147 shf_puts(" /TELIF:", shf);
1148 t = t->right;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001149 dumpioact(shf, t);
Geremy Condra03ebf062011-10-12 18:17:24 -07001150 goto dumpif;
1151 }
1152 if (t->right != NULL) {
1153 shf_puts(" /TELSE:\n", shf);
1154 dumptree(shf, t->right);
1155 }
1156 break;
1157 OPEN(TEOF)
1158 dumpunexpected:
Elliott Hughes77740fc2016-08-12 15:06:53 -07001159 shf_puts(Tunexpected, shf);
Geremy Condra03ebf062011-10-12 18:17:24 -07001160 break;
1161 OPEN(TELIF)
1162 goto dumpunexpected;
1163 OPEN(TPAT)
1164 goto dumpunexpected;
1165 default:
1166 name = "TINVALID";
1167 shf_fprintf(shf, "{T<%d>:" /*}*/, t->type);
1168 goto dumpunexpected;
1169
1170#undef OPEN
1171 }
1172 out:
1173 shf_fprintf(shf, /*{*/ " /%s}\n", name);
1174 --nesting;
1175}
1176#endif