/** * @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 #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; }