tcsh-org / tcsh

This is a read-only mirror of the tcsh code repository.
https://www.tcsh.org/
Other
232 stars 42 forks source link

command list does not exit immediately #45

Open ajschorr opened 2 years ago

ajschorr commented 2 years ago

This looks like a bug to me. This is using tcsh 6.20.00:

bash-4.4$ csh -c 'false || exit; echo hello' hello bash-4.4$

Why didn't the shell exit prior to executing the 'echo hello' command?

It seems as if the exit has a delayed effect and happens only after the entire list has been executed.

In an interactive shell, one sees:

bash-4.4$ csh schorr@ti129: false || exit; echo hello hello exit bash-4.4$

Krush206 commented 2 months ago

I got used to this behavior, thus no longer saw it as a bug. I found it useful for one-line procedures.

( exit ( -e myfile ) && ( ( echo mytext > myfile ) >& /dev/null || echo Cannot create file. ) || echo File exists.

Anyhow, this behavior can be avoided with the following patch.

--- a/sh.sem.c
+++ b/sh.sem.c
@@ -658,13 +658,15 @@ execute(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
        if (setexit() == 0)
            func(t, bifunc);
        resexit(oldexit);
-       haderr = ohaderr;

        if (adrof(STRprintexitvalue)) {
            int rv = getstatus();
            if (rv != 0)
            xprintf(CGETS(17, 2, "Exit %d\n"), rv);
        }
+       if (bifunc->bfunct == doexit && !haderr)
+           reset();
+       haderr = ohaderr;
        }
        break;
    }

Should be noted this doesn't work for one-line ifs, and I don't think would make a problem, since exit inherits the same expressiveness of if.