diff options
Diffstat (limited to 'src/exec.c')
| -rw-r--r-- | src/exec.c | 42 |
1 files changed, 34 insertions, 8 deletions
@@ -358,7 +358,7 @@ char *path_search(const char *name, const char *path, bool executable) { } struct stat st; if (stat(full, &st) == 0 && S_ISREG(st.st_mode) && - (!executable || (st.st_mode & 0111))) + (!executable || access(full, X_OK) == 0)) return full; if (!colon) return nullptr; @@ -744,11 +744,26 @@ typedef struct simple_cmd { } simple_cmd; /** + * @brief Expand PS4 for `set -x` without recursively tracing the expansion. + */ +static const char *xtrace_prefix(void) { + const char *ps4 = var_get("PS4"); + if (!ps4) + return "+ "; + if (!strpbrk(ps4, "$`\\")) + return ps4; + bool saved = opt.xtrace; + opt.xtrace = false; + const char *out = expand_quietly(ps4, false); + opt.xtrace = saved; + return out; +} + +/** * @brief Print a command for `set -x`. */ static void xtrace(const simple_cmd *sc) { - const char *ps4 = var_get("PS4"); - out_puts(&out2, ps4 ? ps4 : "+ "); + out_puts(&out2, xtrace_prefix()); for (int i = 0; i < sc->argc; i++) { if (i) out_putc(&out2, ' '); @@ -1217,7 +1232,8 @@ static int run_pure(simple_cmd *sc, output *o) { jmploc *prev = exc_handler; int status; - out1 = *o; + if (o != &out1) + out1 = *o; if (setjmp(jl.buf) == 0) { exc_catch(&jl); status = sc->cmd.bi->fn(sc->argc, sc->argv); @@ -1225,6 +1241,8 @@ static int run_pure(simple_cmd *sc, output *o) { status = exitstatus ? exitstatus : 2; } exc_handler = prev; + if (o == &out1) + return finish_builtin(status); out_flush(&out1); *o = out1; out1 = saved; @@ -1502,6 +1520,9 @@ static int eval_pipe(node *n, unsigned flags, strbuf *cap) { */ jmploc jl; jmploc *prev = exc_handler; + int saved_loop = loop_depth; + int saved_dot = dot_depth; + int saved_func = func_depth; volatile bool raised = false; if (setjmp(jl.buf) == 0) { exc_catch(&jl); @@ -1543,10 +1564,15 @@ static int eval_pipe(node *n, unsigned flags, strbuf *cap) { * and dash both run on after `echo x | echo "${unset?}"`. Only * `exit` in the last stage ends the shell. */ - if (exc_code == EXC_EXIT) + if (exc_code == EXC_EXIT) { raised = true; - else + } else { last->status = exitstatus; + loop_depth = saved_loop; + dot_depth = saved_dot; + func_depth = saved_func; + skip_kind = SKIP_NONE; + } } exc_handler = prev; @@ -1922,6 +1948,7 @@ static int eval_subshell(node *n, unsigned flags) { if (ok && (sc.cmd.bi->flags & BI_CD)) { int status; builtin_cd_isolated(sc.argc, sc.argv, &status); + status = finish_builtin(status); scope_pop(); return status; } @@ -2055,8 +2082,7 @@ int eval_node(node *n, unsigned flags) { cur_lineno = n->lineno; if (opt.xtrace) { char *text = node_text(n); - out_printf(&out2, "%s%s\n", var_get("PS4") ? var_get("PS4") : "+ ", - text); + out_printf(&out2, "%s%s\n", xtrace_prefix(), text); out_flush(&out2); free(text); } |
