1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/**
* @file unparse.c
* @brief Turn a parse tree back into command text.
*
* The shell keeps commands as trees, not as the text the user typed, so
* the `jobs` listing and the "[1] + Stopped ..." notices need the text
* rebuilt. The result is meant to be read by a person: it is equivalent
* to the original command but not necessarily spelled the same way (for
* example `"a b"` comes back as `'a b'`).
*
* This runs only when a job is recorded in an interactive shell. Scripts
* never pay for it.
*/
#include <string.h>
#include "exec.h"
static void put_node(strbuf *sb, const node *n);
/**
* @brief Append a literal, quoting it if that is needed to read it back.
* @param sb Destination.
* @param s Literal text.
* @param len Its length.
* @param quoted Whether the literal was quoted in the source.
*/
static void put_lit(strbuf *sb, const char *s, size_t len, bool quoted) {
bool need = quoted && !len;
for (size_t i = 0; i < len && quoted && !need; i++)
need = strchr(" \t\n;&|<>()$`\\\"'*?[#~=%", s[i]) != NULL;
if (!need) {
sb_put(sb, s, len);
return;
}
sb_putc(sb, '\'');
for (size_t i = 0; i < len; i++) {
if (s[i] == '\'')
sb_puts(sb, "'\\''");
else
sb_putc(sb, s[i]);
}
sb_putc(sb, '\'');
}
static void put_parts(strbuf *sb, const wpart *p, bool raw);
/**
* @brief Append a parameter expansion, e.g. `$x` or `${x:-word}`.
* @param sb Destination.
* @param pm The parameter.
*/
static void put_param(strbuf *sb, const param *pm) {
static const char *const ops[] = {
[PS_MINUS] = "-", [PS_EQUAL] = "=", [PS_QUESTION] = "?",
[PS_PLUS] = "+", [PS_TRIML] = "#", [PS_TRIMLL] = "##",
[PS_TRIMR] = "%", [PS_TRIMRR] = "%%",
};
sb_putc(sb, '$');
if (pm->op == PS_NORMAL && (pm->type != PT_POS || pm->nlen == 1)) {
sb_put(sb, pm->name, pm->nlen);
return;
}
sb_putc(sb, '{');
if (pm->op == PS_LENGTH)
sb_putc(sb, '#');
sb_put(sb, pm->name, pm->nlen);
if (pm->op != PS_NORMAL && pm->op != PS_LENGTH) {
if (pm->colon)
sb_putc(sb, ':');
sb_puts(sb, ops[pm->op]);
put_parts(sb, pm->arg, false);
}
sb_putc(sb, '}');
}
/**
* @brief Append the parts of one word.
* @param sb Destination.
* @param p First part, or NULL.
* @param raw The parts are an arithmetic expression: literal text is
* written as is (it was never shell-quoted).
*/
static void put_parts(strbuf *sb, const wpart *p, bool raw) {
for (; p; p = p->next) {
switch (p->kind) {
case P_LIT:
if (raw)
sb_put(sb, p->u.lit.s, p->u.lit.len);
else
put_lit(sb, p->u.lit.s, p->u.lit.len, p->quoted);
break;
case P_PARAM:
if (p->quoted && !raw)
sb_putc(sb, '"');
put_param(sb, p->u.param);
if (p->quoted && !raw)
sb_putc(sb, '"');
break;
case P_CMDSUB:
sb_puts(sb, p->quoted ? "\"$(" : "$(");
if (p->u.cmd)
put_node(sb, p->u.cmd);
sb_puts(sb, p->quoted ? ")\"" : ")");
break;
case P_ARITH:
sb_puts(sb, "$((");
put_parts(sb, p->u.arith, true);
sb_puts(sb, "))");
break;
}
}
}
/**
* @brief Append a list of words separated by spaces.
* @param sb Destination.
* @param w First word, or NULL.
* @param first Whether nothing precedes the list on this command (no
* leading space is wanted then).
* @return false if anything was written, else `first` unchanged.
*/
static bool put_words(strbuf *sb, const word *w, bool first) {
for (; w; w = w->next) {
if (!first)
sb_putc(sb, ' ');
first = false;
if (w->plain && !w->parts->quoted)
sb_puts(sb, w->plain);
else
put_parts(sb, w->parts, false);
}
return first;
}
/**
* @brief Append a command's redirections.
* @param sb Destination.
* @param r First redirection, or NULL.
*/
static void put_redirs(strbuf *sb, const redir *r) {
static const char *const ops[] = {
[R_IN] = "<", [R_OUT] = ">", [R_CLOBBER] = ">|",
[R_APPEND] = ">>", [R_RDWR] = "<>", [R_DUPIN] = "<&",
[R_DUPOUT] = ">&", [R_HERE] = "<<",
};
for (; r; r = r->next) {
char buf[INT_BUFSZ];
int dflt = (r->op == R_IN || r->op == R_RDWR || r->op == R_DUPIN ||
r->op == R_HERE)
? 0
: 1;
sb_putc(sb, ' ');
if (r->fd != dflt)
sb_puts(sb, fmt_int(r->fd, buf));
sb_puts(sb, ops[r->op]);
if (r->op == R_HERE)
sb_puts(sb, "...");
else
put_parts(sb, r->target->parts, false);
}
}
/**
* @brief Append a command list that sits inside a compound command.
* @param sb Destination.
* @param n The list, or NULL for an empty one.
*/
static void put_body(strbuf *sb, const node *n) {
if (n)
put_node(sb, n);
else
sb_putc(sb, ':');
}
/**
* @brief Append a `[[ ]]` expression.
* @param sb Destination.
* @param c The expression.
*/
static void put_cond(strbuf *sb, const cond *c) {
static const char *const ops[] = {
[CO_MATCH] = "==", [CO_NOMATCH] = "!=", [CO_REGEX] = "=~",
[CO_LESS] = "<", [CO_GREATER] = ">", [CO_EQ] = "-eq",
[CO_NE] = "-ne", [CO_LT] = "-lt", [CO_LE] = "-le",
[CO_GT] = "-gt", [CO_GE] = "-ge", [CO_NEWER] = "-nt",
[CO_OLDER] = "-ot", [CO_SAMEFILE] = "-ef",
};
switch (c->kind) {
case CN_AND:
case CN_OR:
sb_puts(sb, "( ");
put_cond(sb, c->left);
sb_puts(sb, c->kind == CN_AND ? " && " : " || ");
put_cond(sb, c->right);
sb_puts(sb, " )");
break;
case CN_NOT:
sb_puts(sb, "! ");
put_cond(sb, c->left);
break;
case CN_UNARY:
sb_putc(sb, '-');
sb_putc(sb, c->uop);
sb_putc(sb, ' ');
put_parts(sb, c->a->parts, false);
break;
case CN_BINARY:
put_parts(sb, c->a->parts, false);
sb_putc(sb, ' ');
sb_puts(sb, ops[c->op]);
sb_putc(sb, ' ');
put_parts(sb, c->b->parts, false);
break;
case CN_STRING:
put_parts(sb, c->a->parts, false);
break;
}
}
/**
* @brief Append the text of one command.
* @param sb Destination.
* @param n The command. Must not be NULL.
*/
static void put_node(strbuf *sb, const node *n) {
switch (n->kind) {
case N_SIMPLE: {
bool first = put_words(sb, n->u.simple.assigns, true);
put_words(sb, n->u.simple.args, first);
break;
}
case N_PIPE:
for (int i = 0; i < n->u.pipe.n; i++) {
if (i)
sb_puts(sb, " | ");
put_node(sb, n->u.pipe.cmds[i]);
}
break;
case N_AND:
case N_OR:
case N_SEQ:
put_node(sb, n->u.bin.left);
if (n->u.bin.left->kind == N_BG)
sb_putc(sb, ' ');
else
sb_puts(sb, n->kind == N_AND ? " && "
: n->kind == N_OR ? " || "
: "; ");
put_node(sb, n->u.bin.right);
break;
case N_NOT:
sb_puts(sb, "! ");
put_node(sb, n->u.body);
break;
case N_TIME:
sb_puts(sb, n->u.time.posix ? "time -p" : "time");
if (n->u.time.body) {
sb_putc(sb, ' ');
put_node(sb, n->u.time.body);
}
break;
case N_BG:
put_node(sb, n->u.body);
sb_puts(sb, " &");
break;
case N_SUBSHELL:
sb_puts(sb, "( ");
put_body(sb, n->u.body);
sb_puts(sb, " )");
break;
case N_GROUP:
sb_puts(sb, "{ ");
put_body(sb, n->u.body);
sb_puts(sb, "; }");
break;
case N_IF:
sb_puts(sb, "if ");
put_body(sb, n->u.if_.cond);
sb_puts(sb, "; then ");
put_body(sb, n->u.if_.then_part);
if (n->u.if_.else_part) {
sb_puts(sb, "; else ");
put_node(sb, n->u.if_.else_part);
}
sb_puts(sb, "; fi");
break;
case N_WHILE:
case N_UNTIL:
sb_puts(sb, n->kind == N_WHILE ? "while " : "until ");
put_body(sb, n->u.loop.cond);
sb_puts(sb, "; do ");
put_body(sb, n->u.loop.body);
sb_puts(sb, "; done");
break;
case N_FOR:
sb_puts(sb, "for ");
sb_puts(sb, n->u.for_.var);
if (n->u.for_.has_in) {
sb_puts(sb, " in");
put_words(sb, n->u.for_.words, false);
}
sb_puts(sb, "; do ");
put_body(sb, n->u.for_.body);
sb_puts(sb, "; done");
break;
case N_CASE:
sb_puts(sb, "case ");
put_parts(sb, n->u.case_.subject->parts, false);
sb_puts(sb, " in ");
for (const caseitem *it = n->u.case_.items; it; it = it->next) {
for (const word *w = it->patterns; w; w = w->next) {
put_parts(sb, w->parts, false);
sb_puts(sb, w->next ? "|" : ") ");
}
put_body(sb, it->body);
sb_puts(sb, ";; ");
}
sb_puts(sb, "esac");
break;
case N_FUNC:
sb_puts(sb, n->u.func.name);
sb_puts(sb, "() ");
put_node(sb, n->u.func.body);
break;
case N_COND:
sb_puts(sb, "[[ ");
put_cond(sb, n->u.cond);
sb_puts(sb, " ]]");
break;
case N_ARITH:
sb_puts(sb, "((");
put_parts(sb, n->u.arith->u.arith, true);
sb_puts(sb, "))");
break;
}
put_redirs(sb, n->redirs);
}
/**
* @brief Rebuild the text of a command.
* @param n The command, or NULL.
* @return Heap-allocated text. The caller frees it.
*/
char *node_text(const node *n) {
arena_marker mark = arena_mark(&scratch);
strbuf sb;
sb_init(&sb, &scratch);
if (n)
put_node(&sb, n);
char *text = xstrdup(sb_cstr(&sb));
arena_release(&scratch, mark);
return text;
}
|