From 9e1428fb91b383f5b1cc826373e97cd6fd8b652a Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Fri, 17 Mar 2006 17:33:20 +0000 Subject: [PATCH] Exit status propagation fix from freebsd's sh --- commands/ash/eval.c | 19 ++++++++++++++++--- commands/ash/expand.c | 4 ++-- commands/ash/main.c | 3 +++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/commands/ash/eval.c b/commands/ash/eval.c index ce0eb1773..388a8000d 100755 --- a/commands/ash/eval.c +++ b/commands/ash/eval.c @@ -84,6 +84,7 @@ int funcnest; /* depth of function calls */ char *commandname; struct strlist *cmdenviron; int exitstatus; /* exit status of last command */ +int oexitstatus; /* saved exit status */ #ifdef __STDC__ @@ -272,8 +273,11 @@ evaltree(n, flags) out: if (pendingsigs) dotrap(); - if ((flags & EV_EXIT) || (eflag == 1 && exitstatus && !(flags & EV_TESTED))) + if ((flags & EV_EXIT) || (eflag && exitstatus + && !(flags & EV_TESTED) && (n->type == NCMD || + n->type == NSUBSHELL))) { exitshell(exitstatus); + } } @@ -326,6 +330,7 @@ evalfor(n) setstackmark(&smark); arglist.lastp = &arglist.list; for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { + oexitstatus = exitstatus; expandarg(argp, &arglist, 1); if (evalskip) goto out; @@ -365,6 +370,7 @@ evalcase(n, flags) setstackmark(&smark); arglist.lastp = &arglist.list; + oexitstatus = exitstatus; expandarg(n->ncase.expr, &arglist, 0); for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { @@ -421,6 +427,7 @@ expredir(n) register union node *redir; for (redir = n ; redir ; redir = redir->nfile.next) { + oexitstatus = exitstatus; if (redir->type == NFROM || redir->type == NTO || redir->type == NAPPEND) { @@ -525,6 +532,7 @@ evalbackcmd(n, result) /* `` */ } else if (n->type == NCMD) { + exitstatus = oexitstatus; evalcommand(n, EV_BACKCMD, result); } else { if (pipe(pip) < 0) @@ -588,6 +596,8 @@ evalcommand(cmd, flags, backcmd) arglist.lastp = &arglist.list; varlist.lastp = &varlist.list; varflag = 1; + oexitstatus = exitstatus; + exitstatus = 0; for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { p = argp->narg.text; if (varflag && is_name(*p)) { @@ -722,7 +732,10 @@ evalcommand(cmd, flags, backcmd) for (sp = varlist.list ; sp ; sp = sp->next) mklocal(sp->text); funcnest++; - evaltree(cmdentry.u.func, 0); + if (flags & EV_TESTED) + evaltree(cmdentry.u.func, EV_TESTED); + else + evaltree(cmdentry.u.func, 0); funcnest--; INTOFF; poplocalvars(); @@ -895,7 +908,7 @@ breakcmd(argc, argv) char **argv; { returncmd(argc, argv) char **argv; { int ret; - ret = exitstatus; + ret = oexitstatus; if (argc > 1) ret = number(argv[1]); if (funcnest) { diff --git a/commands/ash/expand.c b/commands/ash/expand.c index 1b6d2a816..28ef32ab0 100755 --- a/commands/ash/expand.c +++ b/commands/ash/expand.c @@ -463,7 +463,7 @@ varvalue(name, quoted, allow_split) char temp[32]; char *p; int i; - extern int exitstatus; + extern int oexitstatus; char sep; char **ap; char const *syntax; @@ -473,7 +473,7 @@ varvalue(name, quoted, allow_split) num = rootpid; goto numvar; case '?': - num = exitstatus; + num = oexitstatus; goto numvar; case '#': num = shellparam.nparam; diff --git a/commands/ash/main.c b/commands/ash/main.c index 7393cf4d3..2461b7412 100755 --- a/commands/ash/main.c +++ b/commands/ash/main.c @@ -321,8 +321,11 @@ dotcmd(argc, argv) char **argv; { exitcmd(argc, argv) char **argv; { + extern int oexitstatus; if (argc > 1) exitstatus = number(argv[1]); + else + exitstatus = oexitstatus; exitshell(exitstatus); } -- 2.44.0