]> Zhao Yanbai Git Server - minix.git/commitdiff
Exit status propagation fix from freebsd's sh
authorBen Gras <ben@minix3.org>
Fri, 17 Mar 2006 17:33:20 +0000 (17:33 +0000)
committerBen Gras <ben@minix3.org>
Fri, 17 Mar 2006 17:33:20 +0000 (17:33 +0000)
commands/ash/eval.c
commands/ash/expand.c
commands/ash/main.c

index ce0eb1773177f9e1bb912dd410d39de0227a0704..388a8000db4a8a5d90f54c51dde056dadd911574 100755 (executable)
@@ -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) {
index 1b6d2a81636450dfe8209322b908cf0cd442bbf6..28ef32ab02030ad552b355914ddaa1eb8e3df97f 100755 (executable)
@@ -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;
index 7393cf4d3943df7a2637524888dabcdeff14b376..2461b7412b58e5c404bed979a6921351e7b55192 100755 (executable)
@@ -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);
 }