hush: fix exit code propagation from `cmd`. +45 bytes
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 16 Nov 2009 01:00:03 +0000 (02:00 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 16 Nov 2009 01:00:03 +0000 (02:00 +0100)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/hush.c
shell/hush_test/hush-psubst/emptytick.right
shell/hush_test/hush-psubst/emptytick.tests

index ede8d680e00b9596bd4502fe6ada5879bd0d8792..6dd6a0d035f7bb695a64211f9bb84722b67804f3 100644 (file)
@@ -3904,7 +3904,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
                /* if someone gives us an empty string: `cmd with empty output` */
                if (!argv_expanded[0]) {
                        debug_leave();
-                       return 0; // or G.last_exitcode? see emptytick.tests
+                       return G.last_exitcode;
                }
 
                x = find_builtin(argv_expanded[0]);
@@ -6380,15 +6380,26 @@ static struct pipe *parse_stream(char **pstring,
  */
 static void parse_and_run_stream(struct in_str *inp, int end_trigger)
 {
+       /* Why we need empty flag?
+        * An obscure corner case "false; ``; echo $?":
+        * empty command in `` should still set $? to 0.
+        * But we can't just set $? to 0 at the start,
+        * this breaks "false; echo `echo $?`" case.
+        */
+       bool empty = 1;
        while (1) {
                struct pipe *pipe_list;
 
                pipe_list = parse_stream(NULL, inp, end_trigger);
-               if (!pipe_list) /* EOF */
+               if (!pipe_list) { /* EOF */
+                       if (empty)
+                               G.last_exitcode = 0;
                        break;
+               }
                debug_print_tree(pipe_list, 0);
                debug_printf_exec("parse_and_run_stream: run_and_free_list\n");
                run_and_free_list(pipe_list);
+               empty = 0;
        }
 }
 
index 1f60ecfdad464de29061286a04eb4103f37b34ab..2500caba544132e7b1de3cdbeb346275c1f6843a 100644 (file)
@@ -1,17 +1,17 @@
 0
 0
 hush: can't execute '': No such file or directory
-0
+127
 hush: can't execute '': No such file or directory
-0
+127
 0
 0
 0
 0
 hush: can't execute '': No such file or directory
-0
+127
 hush: can't execute '': No such file or directory
-0
+127
 0
 0
 hush: can't execute '': No such file or directory
index a269f025aa0bf36f4a73ff9edad56b05084578e7..eaffafb22c700dd32a71fae6e5de6f3c99ae5942 100755 (executable)
@@ -1,17 +1,13 @@
 true;  ``; echo $?
 false; ``; echo $?
-# UNFIXED BUG. bash sets $? to 127:
 true;  `""`; echo $?
-# bash sets $? to 127:
 false; `""`; echo $?
 true;  `     `; echo $?
 false; `     `; echo $?
 
 true;  $(); echo $?
 false; $(); echo $?
-# bash sets $? to 127:
 true;  $(""); echo $?
-# bash sets $? to 127:
 false; $(""); echo $?
 true;  $(     ); echo $?
 false; $(     ); echo $?