diff options
author | Jilles Tjoelker <jilles@FreeBSD.org> | 2010-01-01 18:17:46 +0000 |
---|---|---|
committer | Jilles Tjoelker <jilles@FreeBSD.org> | 2010-01-01 18:17:46 +0000 |
commit | f7cc73afc8f467e61c0288744e916faff4231731 (patch) | |
tree | 6198da26b9a8cb29d4142aca530e1a661605a331 /bin/sh | |
parent | c8054a6197b15c27646553e9f34b83719b628848 (diff) | |
download | src-f7cc73afc8f467e61c0288744e916faff4231731.tar.gz src-f7cc73afc8f467e61c0288744e916faff4231731.zip |
sh: Fix some bugs with backquoted builtins:
- correctly handle error output in $(builtin 2>&1), clarify out1/out2 vs
output/errout in the code
- treat all builtins as regular builtins so errors do not abort the shell
and variable assignments do not persist
- respect the caller's INTOFF
Some bugs still exist:
- expansion errors may still abort the shell
- some side effects of expansions and builtins persist
Notes
Notes:
svn path=/head/; revision=201366
Diffstat (limited to 'bin/sh')
-rw-r--r-- | bin/sh/error.c | 4 | ||||
-rw-r--r-- | bin/sh/eval.c | 23 | ||||
-rw-r--r-- | bin/sh/exec.c | 2 | ||||
-rw-r--r-- | bin/sh/expand.c | 2 | ||||
-rw-r--r-- | bin/sh/output.h | 9 | ||||
-rw-r--r-- | bin/sh/parser.c | 4 |
6 files changed, 23 insertions, 21 deletions
diff --git a/bin/sh/error.c b/bin/sh/error.c index 3fc7101f8d31..1f981f061ae6 100644 --- a/bin/sh/error.c +++ b/bin/sh/error.c @@ -160,8 +160,8 @@ exverror(int cond, const char *msg, va_list ap) #endif if (msg) { if (commandname) - outfmt(&errout, "%s: ", commandname); - doformat(&errout, msg, ap); + outfmt(out2, "%s: ", commandname); + doformat(out2, msg, ap); out2c('\n'); } flushall(); diff --git a/bin/sh/eval.c b/bin/sh/eval.c index e54d19a794ff..f2caafb0659e 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -646,7 +646,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) out2str(ps4val()); for (sp = varlist.list ; sp ; sp = sp->next) { if (sep != 0) - outc(' ', &errout); + out2c(' '); p = sp->text; while (*p != '=' && *p != '\0') out2c(*p++); @@ -658,7 +658,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) } for (sp = arglist.list ; sp ; sp = sp->next) { if (sep != 0) - outc(' ', &errout); + out2c(' '); /* Disambiguate command looking like assignment. */ if (sp == arglist.list && strchr(sp->text, '=') != NULL && @@ -670,7 +670,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) out2qstr(sp->text); sep = ' '; } - outc('\n', &errout); + out2c('\n'); flushout(&errout); } @@ -722,9 +722,8 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) break; if ((cmdentry.u.index = find_builtin(*argv, &cmdentry.special)) < 0) { - outfmt(&errout, "%s: not found\n", *argv); + out2fmt_flush("%s: not found\n", *argv); exitstatus = 127; - flushout(&errout); return; } if (cmdentry.u.index != BLTINCMD) @@ -832,6 +831,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) memout.nextc = memout.buf; memout.bufsize = 64; mode |= REDIR_BACKQ; + cmdentry.special = 0; } savecmdname = commandname; savetopfile = getcurrentfile(); @@ -865,20 +865,21 @@ cmddone: } } handler = savehandler; + if (flags == EV_BACKCMD) { + backcmd->buf = memout.buf; + backcmd->nleft = memout.nextc - memout.buf; + memout.buf = NULL; + } if (e != -1) { if ((e != EXERROR && e != EXEXEC) || cmdentry.special) exraise(e); popfilesupto(savetopfile); - FORCEINTON; + if (flags != EV_BACKCMD) + FORCEINTON; } if (cmdentry.u.index != EXECCMD) popredir(); - if (flags == EV_BACKCMD) { - backcmd->buf = memout.buf; - backcmd->nleft = memout.nextc - memout.buf; - memout.buf = NULL; - } } else { #ifdef DEBUG trputs("normal command: "); trargs(argv); diff --git a/bin/sh/exec.c b/bin/sh/exec.c index c9f9a2634756..a28ab0364a50 100644 --- a/bin/sh/exec.c +++ b/bin/sh/exec.c @@ -255,7 +255,7 @@ hashcmd(int argc __unused, char **argv __unused) if (cmdp != NULL) printentry(cmdp, verbose); else - outfmt(&errout, "%s: not found\n", name); + outfmt(out2, "%s: not found\n", name); } flushall(); } diff --git a/bin/sh/expand.c b/bin/sh/expand.c index bb781516c08e..876cde163f60 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -526,7 +526,7 @@ subevalvar(char *p, char *str, int strloc, int subtype, int startloc, case VSQUESTION: if (*p != CTLENDVAR) { - outfmt(&errout, "%s\n", startp); + outfmt(out2, "%s\n", startp); error((char *)NULL); } error("%.*s: parameter %snot set", (int)(p - str - 1), diff --git a/bin/sh/output.h b/bin/sh/output.h index 91167c092733..345fe531c400 100644 --- a/bin/sh/output.h +++ b/bin/sh/output.h @@ -46,11 +46,12 @@ struct output { short flags; }; -extern struct output output; -extern struct output errout; +extern struct output output; /* to fd 1 */ +extern struct output errout; /* to fd 2 */ extern struct output memout; -extern struct output *out1; -extern struct output *out2; +extern struct output *out1; /* &memout if backquote, otherwise &output */ +extern struct output *out2; /* &memout if backquote with 2>&1, otherwise + &errout */ void out1str(const char *); void out1qstr(const char *); diff --git a/bin/sh/parser.c b/bin/sh/parser.c index b85d10ad2e6f..f8fd0edd3761 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -1560,8 +1560,8 @@ STATIC void synerror(const char *msg) { if (commandname) - outfmt(&errout, "%s: %d: ", commandname, startlinno); - outfmt(&errout, "Syntax error: %s\n", msg); + outfmt(out2, "%s: %d: ", commandname, startlinno); + outfmt(out2, "Syntax error: %s\n", msg); error((char *)NULL); } |