patch from oksh (except manpage, I'll merge that later):

pass "xerrok" status across the execution call stack to more closely
match what both POSIX and [18]ksh.1 already describe in regards to set
-e/errexit's behavior in determining when to exit from nonzero return
values.

specifically, the truth values tested as operands to &&' and ||', as
well as the resulting compound expression itself, along with the truth
value resulting from a negated command (i.e. a pipeline prefixed !'),
should not make the shell exit when -e is in effect.

issue reported by matthieu.
testing matthieu, naddy.
ok miod (earlier version), otto.
man page ok jmc.
This commit is contained in:
tg
2009-03-22 17:47:38 +00:00
parent 0fe20ab25f
commit b20f49adae
6 changed files with 66 additions and 54 deletions

6
eval.c
View File

@ -1,8 +1,8 @@
/* $OpenBSD: eval.c,v 1.33 2007/08/02 11:05:54 fgsch Exp $ */ /* $OpenBSD: eval.c,v 1.34 2009/01/29 23:27:26 jaredy Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.52 2009/03/14 18:12:51 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/eval.c,v 1.53 2009/03/22 17:47:35 tg Exp $");
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
#define MKSH_NOPWNAM #define MKSH_NOPWNAM
@ -1069,7 +1069,7 @@ comsub(Expand *xp, const char *cp)
ksh_dup2(pv[1], 1, false); ksh_dup2(pv[1], 1, false);
close(pv[1]); close(pv[1]);
} }
execute(t, XFORK|XXCOM|XPIPEO); execute(t, XFORK|XXCOM|XPIPEO, NULL);
restfd(1, ofd1); restfd(1, ofd1);
startlast(); startlast();
xp->split = 1; /* waitlast() */ xp->split = 1; /* waitlast() */

77
exec.c
View File

@ -1,11 +1,11 @@
/* $OpenBSD: exec.c,v 1.48 2007/09/05 19:02:01 otto Exp $ */ /* $OpenBSD: exec.c,v 1.49 2009/01/29 23:27:26 jaredy Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.50 2009/03/16 15:50:12 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/exec.c,v 1.51 2009/03/22 17:47:36 tg Exp $");
static int comexec(struct op *, struct tbl *volatile, const char **, static int comexec(struct op *, struct tbl *volatile, const char **,
int volatile); int volatile, volatile int *);
static void scriptexec(struct op *, const char **) static void scriptexec(struct op *, const char **)
__attribute__((noreturn)); __attribute__((noreturn));
static int call_builtin(struct tbl *, const char **); static int call_builtin(struct tbl *, const char **);
@ -21,7 +21,8 @@ static void dbteste_error(Test_env *, int, const char *);
*/ */
int int
execute(struct op *volatile t, execute(struct op *volatile t,
volatile int flags) /* if XEXEC don't fork */ volatile int flags, /* if XEXEC don't fork */
volatile int *xerrok)
{ {
int i; int i;
volatile int rv = 0; volatile int rv = 0;
@ -36,7 +37,8 @@ execute(struct op *volatile t,
return 0; return 0;
if ((flags&XFORK) && !(flags&XEXEC) && t->type != TPIPE) if ((flags&XFORK) && !(flags&XEXEC) && t->type != TPIPE)
return exchild(t, flags & ~XTIME, -1); /* run in sub-process */ /* run in sub-process */
return exchild(t, flags & ~XTIME, xerrok, -1);
newenv(E_EXEC); newenv(E_EXEC);
if (trap) if (trap)
@ -95,11 +97,11 @@ execute(struct op *volatile t,
switch (t->type) { switch (t->type) {
case TCOM: case TCOM:
rv = comexec(t, tp, (const char **)ap, flags); rv = comexec(t, tp, (const char **)ap, flags, xerrok);
break; break;
case TPAREN: case TPAREN:
rv = execute(t->left, flags|XFORK); rv = execute(t->left, flags | XFORK, xerrok);
break; break;
case TPIPE: case TPIPE:
@ -115,7 +117,8 @@ execute(struct op *volatile t,
* (: ; cat /etc/termcap) | sleep 1 * (: ; cat /etc/termcap) | sleep 1
* will hang forever). * will hang forever).
*/ */
exchild(t->left, flags|XPIPEO|XCCLOSE, pv[0]); exchild(t->left, flags | XPIPEO | XCCLOSE,
NULL, pv[0]);
ksh_dup2(pv[0], 0, false); /* stdin of next */ ksh_dup2(pv[0], 0, false); /* stdin of next */
closepipe(pv); closepipe(pv);
flags |= XPIPEI; flags |= XPIPEI;
@ -124,17 +127,17 @@ execute(struct op *volatile t,
restfd(1, e->savefd[1]); /* stdout of last */ restfd(1, e->savefd[1]); /* stdout of last */
e->savefd[1] = 0; /* no need to re-restore this */ e->savefd[1] = 0; /* no need to re-restore this */
/* Let exchild() close 0 in parent, after fork, before wait */ /* Let exchild() close 0 in parent, after fork, before wait */
i = exchild(t, flags|XPCLOSE, 0); i = exchild(t, flags | XPCLOSE, xerrok, 0);
if (!(flags&XBGND) && !(flags&XXCOM)) if (!(flags&XBGND) && !(flags&XXCOM))
rv = i; rv = i;
break; break;
case TLIST: case TLIST:
while (t->type == TLIST) { while (t->type == TLIST) {
execute(t->left, flags & XERROK); execute(t->left, flags & XERROK, NULL);
t = t->right; t = t->right;
} }
rv = execute(t, flags & XERROK); rv = execute(t, flags & XERROK, xerrok);
break; break;
case TCOPROC: case TCOPROC:
@ -191,8 +194,8 @@ execute(struct op *volatile t,
* job is actually created. * job is actually created.
*/ */
flags &= ~XEXEC; flags &= ~XEXEC;
exchild(t->left, flags|XBGND|XFORK|XCOPROC|XCCLOSE, exchild(t->left, flags | XBGND | XFORK | XCOPROC | XCCLOSE,
coproc.readw); NULL, coproc.readw);
break; break;
} }
@ -201,20 +204,24 @@ execute(struct op *volatile t,
* forks again for async... parent should optimise * forks again for async... parent should optimise
* this to "foo &"... * this to "foo &"...
*/ */
rv = execute(t->left, (flags&~XEXEC)|XBGND|XFORK); rv = execute(t->left, (flags&~XEXEC)|XBGND|XFORK, xerrok);
break; break;
case TOR: case TOR:
case TAND: case TAND:
rv = execute(t->left, XERROK); rv = execute(t->left, XERROK, xerrok);
if (t->right != NULL && (rv == 0) == (t->type == TAND)) if ((rv == 0) == (t->type == TAND))
rv = execute(t->right, flags & XERROK); rv = execute(t->right, XERROK, xerrok);
else flags |= XERROK;
flags |= XERROK; if (xerrok)
*xerrok = 1;
break; break;
case TBANG: case TBANG:
rv = !execute(t->right, XERROK); rv = !execute(t->right, XERROK, xerrok);
flags |= XERROK;
if (xerrok)
*xerrok = 1;
break; break;
case TDBRACKET: case TDBRACKET:
@ -257,7 +264,7 @@ execute(struct op *volatile t,
if (t->type == TFOR) { if (t->type == TFOR) {
while (*ap != NULL) { while (*ap != NULL) {
setstr(global(t->str), *ap++, KSH_UNWIND_ERROR); setstr(global(t->str), *ap++, KSH_UNWIND_ERROR);
rv = execute(t->left, flags & XERROK); rv = execute(t->left, flags & XERROK, xerrok);
} }
} else { /* TSELECT */ } else { /* TSELECT */
for (;;) { for (;;) {
@ -267,7 +274,7 @@ execute(struct op *volatile t,
} }
is_first = false; is_first = false;
setstr(global(t->str), cp, KSH_UNWIND_ERROR); setstr(global(t->str), cp, KSH_UNWIND_ERROR);
rv = execute(t->left, flags & XERROK); rv = execute(t->left, flags & XERROK, xerrok);
} }
} }
} }
@ -290,17 +297,18 @@ execute(struct op *volatile t,
} }
} }
rv = 0; /* in case of a continue */ rv = 0; /* in case of a continue */
while ((execute(t->left, XERROK) == 0) == (t->type == TWHILE)) while ((execute(t->left, XERROK, NULL) == 0) ==
rv = execute(t->right, flags & XERROK); (t->type == TWHILE))
rv = execute(t->right, flags & XERROK, xerrok);
break; break;
case TIF: case TIF:
case TELIF: case TELIF:
if (t->right == NULL) if (t->right == NULL)
break; /* should be error */ break; /* should be error */
rv = execute(t->left, XERROK) == 0 ? rv = execute(t->left, XERROK, NULL) == 0 ?
execute(t->right->left, flags & XERROK) : execute(t->right->left, flags & XERROK, xerrok) :
execute(t->right->right, flags & XERROK); execute(t->right->right, flags & XERROK, xerrok);
break; break;
case TCASE: case TCASE:
@ -312,11 +320,11 @@ execute(struct op *volatile t,
goto Found; goto Found;
break; break;
Found: Found:
rv = execute(t->left, flags & XERROK); rv = execute(t->left, flags & XERROK, xerrok);
break; break;
case TBRACE: case TBRACE:
rv = execute(t->left, flags & XERROK); rv = execute(t->left, flags & XERROK, xerrok);
break; break;
case TFUNCT: case TFUNCT:
@ -327,7 +335,7 @@ execute(struct op *volatile t,
/* Clear XEXEC so nested execute() call doesn't exit /* Clear XEXEC so nested execute() call doesn't exit
* (allows "ls -l | time grep foo"). * (allows "ls -l | time grep foo").
*/ */
rv = timex(t, flags & ~XEXEC); rv = timex(t, flags & ~XEXEC, xerrok);
break; break;
case TEXEC: /* an eval'd TCOM */ case TEXEC: /* an eval'd TCOM */
@ -353,7 +361,8 @@ execute(struct op *volatile t,
quitenv(NULL); /* restores IO */ quitenv(NULL); /* restores IO */
if ((flags&XEXEC)) if ((flags&XEXEC))
unwind(LEXIT); /* exit child */ unwind(LEXIT); /* exit child */
if (rv != 0 && !(flags & XERROK)) { if (rv != 0 && !(flags & XERROK) &&
(xerrok == NULL || !*xerrok)) {
trapsig(SIGERR_); trapsig(SIGERR_);
if (Flag(FERREXIT)) if (Flag(FERREXIT))
unwind(LERROR); unwind(LERROR);
@ -367,7 +376,7 @@ execute(struct op *volatile t,
static int static int
comexec(struct op *t, struct tbl *volatile tp, const char **ap, comexec(struct op *t, struct tbl *volatile tp, const char **ap,
volatile int flags) volatile int flags, volatile int *xerrok)
{ {
int i; int i;
volatile int rv = 0; volatile int rv = 0;
@ -579,7 +588,7 @@ comexec(struct op *t, struct tbl *volatile tp, const char **ap,
i = sigsetjmp(e->jbuf, 0); i = sigsetjmp(e->jbuf, 0);
if (i == 0) { if (i == 0) {
/* seems odd to pass XERROK here, but at&t ksh does */ /* seems odd to pass XERROK here, but at&t ksh does */
exstat = execute(tp->val.t, flags & XERROK); exstat = execute(tp->val.t, flags & XERROK, xerrok);
i = LRETURN; i = LRETURN;
} }
kshname = old_kshname; kshname = old_kshname;
@ -652,7 +661,7 @@ comexec(struct op *t, struct tbl *volatile tp, const char **ap,
texec.left = t; /* for tprint */ texec.left = t; /* for tprint */
texec.str = tp->val.s; texec.str = tp->val.s;
texec.args = ap; texec.args = ap;
rv = exchild(&texec, flags, -1); rv = exchild(&texec, flags, xerrok, -1);
break; break;
} }
Leave: Leave:

View File

@ -1,11 +1,11 @@
/* $OpenBSD: c_ksh.c,v 1.31 2008/05/17 23:31:52 sobrado Exp $ */ /* $OpenBSD: c_ksh.c,v 1.31 2008/05/17 23:31:52 sobrado Exp $ */
/* $OpenBSD: c_sh.c,v 1.38 2008/07/23 16:34:38 jaredy Exp $ */ /* $OpenBSD: c_sh.c,v 1.39 2009/01/29 23:27:26 jaredy Exp $ */
/* $OpenBSD: c_test.c,v 1.17 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: c_test.c,v 1.17 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: c_ulimit.c,v 1.17 2008/03/21 12:51:19 millert Exp $ */ /* $OpenBSD: c_ulimit.c,v 1.17 2008/03/21 12:51:19 millert Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.95 2009/03/15 16:13:39 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.96 2009/03/22 17:47:36 tg Exp $");
/* A leading = means assignments before command are kept; /* A leading = means assignments before command are kept;
* a leading * means a POSIX special builtin; * a leading * means a POSIX special builtin;
@ -2196,7 +2196,7 @@ c_times(const char **wp __unused)
* time pipeline (really a statement, not a built-in command) * time pipeline (really a statement, not a built-in command)
*/ */
int int
timex(struct op *t, int f) timex(struct op *t, int f, volatile int *xerrok)
{ {
#define TF_NOARGS BIT(0) #define TF_NOARGS BIT(0)
#define TF_NOREAL BIT(1) /* don't report real time */ #define TF_NOREAL BIT(1) /* don't report real time */
@ -2222,7 +2222,7 @@ timex(struct op *t, int f)
*/ */
timerclear(&j_usrtime); timerclear(&j_usrtime);
timerclear(&j_systime); timerclear(&j_systime);
rv = execute(t->left, f | XTIME); rv = execute(t->left, f | XTIME, xerrok);
if (t->left->type == TCOM) if (t->left->type == TCOM)
tf |= t->left->str[0]; tf |= t->left->str[0];
gettimeofday(&tv1, NULL); gettimeofday(&tv1, NULL);

13
jobs.c
View File

@ -1,8 +1,8 @@
/* $OpenBSD: jobs.c,v 1.36 2007/09/06 19:57:47 otto Exp $ */ /* $OpenBSD: jobs.c,v 1.37 2009/01/29 23:27:26 jaredy Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.44 2009/02/23 16:17:44 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.45 2009/03/22 17:47:37 tg Exp $");
/* Order important! */ /* Order important! */
#define PRUNNING 0 #define PRUNNING 0
@ -289,7 +289,9 @@ j_change(void)
/* execute tree in child subprocess */ /* execute tree in child subprocess */
int int
exchild(struct op *t, int flags, /* used if XPCLOSE or XCCLOSE */ int close_fd) exchild(struct op *t, int flags,
volatile int *xerrok,
/* used if XPCLOSE or XCCLOSE */ int close_fd)
{ {
static Proc *last_proc; /* for pipelines */ static Proc *last_proc; /* for pipelines */
@ -305,7 +307,7 @@ exchild(struct op *t, int flags, /* used if XPCLOSE or XCCLOSE */ int close_fd)
/* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND /* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
* (also done in another execute() below) * (also done in another execute() below)
*/ */
return execute(t, flags & (XEXEC | XERROK)); return execute(t, flags & (XEXEC | XERROK), xerrok);
/* no SIGCHLDs while messing with job and process lists */ /* no SIGCHLDs while messing with job and process lists */
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask); sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
@ -430,7 +432,8 @@ exchild(struct op *t, int flags, /* used if XPCLOSE or XCCLOSE */ int close_fd)
Flag(FTALKING) = 0; Flag(FTALKING) = 0;
tty_close(); tty_close();
cleartraps(); cleartraps();
execute(t, (flags & XERROK) | XEXEC); /* no return */ /* no return */
execute(t, (flags & XERROK) | XEXEC, NULL);
#ifndef MKSH_SMALL #ifndef MKSH_SMALL
if (t->type == TPIPE) if (t->type == TPIPE)
unwind(LLEAVE); unwind(LLEAVE);

6
main.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: main.c,v 1.44 2008/07/05 07:25:18 djm Exp $ */ /* $OpenBSD: main.c,v 1.45 2009/01/29 23:27:26 jaredy Exp $ */
/* $OpenBSD: tty.c,v 1.9 2006/03/14 22:08:01 deraadt Exp $ */ /* $OpenBSD: tty.c,v 1.9 2006/03/14 22:08:01 deraadt Exp $ */
/* $OpenBSD: io.c,v 1.22 2006/03/17 16:30:13 millert Exp $ */ /* $OpenBSD: io.c,v 1.22 2006/03/17 16:30:13 millert Exp $ */
/* $OpenBSD: table.c,v 1.13 2009/01/17 22:06:44 millert Exp $ */ /* $OpenBSD: table.c,v 1.13 2009/01/17 22:06:44 millert Exp $ */
@ -13,7 +13,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.121 2009/03/22 17:31:16 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.122 2009/03/22 17:47:37 tg Exp $");
extern char **environ; extern char **environ;
@ -566,7 +566,7 @@ shell(Source * volatile s, volatile int toplevel)
} }
} }
if (t && (!Flag(FNOEXEC) || (s->flags & SF_TTY))) if (t && (!Flag(FNOEXEC) || (s->flags & SF_TTY)))
exstat = execute(t, 0); exstat = execute(t, 0, NULL);
if (t != NULL && t->type != TEOF && interactive && really_exit) if (t != NULL && t->type != TEOF && interactive && really_exit)
really_exit = 0; really_exit = 0;

10
sh.h
View File

@ -4,7 +4,7 @@
/* $OpenBSD: tree.h,v 1.10 2005/03/28 21:28:22 deraadt Exp $ */ /* $OpenBSD: tree.h,v 1.10 2005/03/28 21:28:22 deraadt Exp $ */
/* $OpenBSD: expand.h,v 1.6 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: expand.h,v 1.6 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: lex.h,v 1.11 2006/05/29 18:22:24 otto Exp $ */ /* $OpenBSD: lex.h,v 1.11 2006/05/29 18:22:24 otto Exp $ */
/* $OpenBSD: proto.h,v 1.31 2009/01/17 22:06:44 millert Exp $ */ /* $OpenBSD: proto.h,v 1.32 2009/01/29 23:27:26 jaredy Exp $ */
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */ /* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */ /* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
@ -102,7 +102,7 @@
#define __SCCSID(x) __IDSTRING(sccsid,x) #define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.281 2009/03/22 17:31:17 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.282 2009/03/22 17:47:38 tg Exp $");
#endif #endif
#define MKSH_VERSION "R36 2009/03/17" #define MKSH_VERSION "R36 2009/03/17"
@ -1289,7 +1289,7 @@ char *debunk(char *, const char *, size_t);
void expand(const char *, XPtrV *, int); void expand(const char *, XPtrV *, int);
int glob_str(char *, XPtrV *, int); int glob_str(char *, XPtrV *, int);
/* exec.c */ /* exec.c */
int execute(struct op * volatile, volatile int); int execute(struct op * volatile, volatile int, volatile int *);
int shcomexec(const char **); int shcomexec(const char **);
struct tbl *findfunc(const char *, unsigned int, int); struct tbl *findfunc(const char *, unsigned int, int);
int define(const char *, struct op *); int define(const char *, struct op *);
@ -1334,7 +1334,7 @@ int c_set(const char **);
int c_unset(const char **); int c_unset(const char **);
int c_ulimit(const char **); int c_ulimit(const char **);
int c_times(const char **); int c_times(const char **);
int timex(struct op *, int); int timex(struct op *, int, volatile int *);
void timex_hook(struct op *, char ** volatile *); void timex_hook(struct op *, char ** volatile *);
int c_exec(const char **); int c_exec(const char **);
int c_builtin(const char **); int c_builtin(const char **);
@ -1383,7 +1383,7 @@ void setexecsig(Trap *, int);
void j_init(int); void j_init(int);
void j_exit(void); void j_exit(void);
void j_change(void); void j_change(void);
int exchild(struct op *, int, int); int exchild(struct op *, int, volatile int *, int);
void startlast(void); void startlast(void);
int waitlast(void); int waitlast(void);
int waitfor(const char *, int *); int waitfor(const char *, int *);