[git commit master] hush: fix var=`exit 2` not setting $? to 2

Denys Vlasenko vda.linux at googlemail.com
Thu May 20 12:27:09 UTC 2010


commit: http://git.busybox.net/busybox/commit/?id=cddbb610cb0ea8d74668653aeaded710d2d13768
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/hush.c                                |   15 ++++++++++-----
 shell/hush_test/hush-psubst/falsetick.right |   27 +++++++++++++++++++++++++++
 shell/hush_test/hush-psubst/falsetick.tests |   22 ++++++++++++++++++++++
 3 files changed, 59 insertions(+), 5 deletions(-)
 create mode 100644 shell/hush_test/hush-psubst/falsetick.right
 create mode 100755 shell/hush_test/hush-psubst/falsetick.tests

diff --git a/shell/hush.c b/shell/hush.c
index df81eaa..8da9439 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -2523,7 +2523,7 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char
 			 * and $IFS-splitted */
 			debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
 			G.last_exitcode = process_command_subs(&subst_result, arg);
-			debug_printf_subst("SUBST RES '%s'\n", subst_result.data);
+			debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data);
 			val = subst_result.data;
 			goto store_val;
 #endif
@@ -4117,9 +4117,9 @@ static NOINLINE int run_pipe(struct pipe *pi)
 
 		if (argv[command->assignment_cnt] == NULL) {
 			/* Assignments, but no command */
-			/* Ensure redirects take effect. Try "a=t >file" */
+			/* Ensure redirects take effect (that is, create files).
+			 * Try "a=t >file": */
 			rcode = setup_redirects(command, squirrel);
-//FIXME: "false; q=`false`; echo $?" should print 1
 			restore_redirects(squirrel);
 			/* Set shell variables */
 			while (*argv) {
@@ -4129,6 +4129,11 @@ static NOINLINE int run_pipe(struct pipe *pi)
 				set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0);
 				argv++;
 			}
+			/* Redirect error sets $? to 1. Othervise,
+			 * if evaluating assignment value set $?, retain it.
+			 * Try "false; q=`exit 2`; echo $?" - should print 2: */
+			if (rcode == 0)
+				rcode = G.last_exitcode;
 			/* Do we need to flag set_local_var() errors?
 			 * "assignment to readonly var" and "putenv error"
 			 */
@@ -4186,7 +4191,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
 				old_vars = set_vars_and_save_old(new_env);
 				if (!funcp) {
 					debug_printf_exec(": builtin '%s' '%s'...\n",
-						x->cmd, argv_expanded[1]);
+						x->b_cmd, argv_expanded[1]);
 					rcode = x->b_function(argv_expanded) & 0xff;
 					fflush_all();
 				}
@@ -6814,7 +6819,7 @@ int hush_main(int argc, char **argv)
 	struct variable *cur_var;
 
 	INIT_G();
-	if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */
+	if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */
 		G.last_exitcode = EXIT_SUCCESS;
 #if !BB_MMU
 	G.argv0_for_re_execing = argv[0];
diff --git a/shell/hush_test/hush-psubst/falsetick.right b/shell/hush_test/hush-psubst/falsetick.right
new file mode 100644
index 0000000..0b98fb7
--- /dev/null
+++ b/shell/hush_test/hush-psubst/falsetick.right
@@ -0,0 +1,27 @@
+0
+0
+0
+0
+2
+2
+2
+2
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+hush: can't open '/does/not/exist': No such file or directory
+1
+Done: a=b
diff --git a/shell/hush_test/hush-psubst/falsetick.tests b/shell/hush_test/hush-psubst/falsetick.tests
new file mode 100755
index 0000000..44d2eae
--- /dev/null
+++ b/shell/hush_test/hush-psubst/falsetick.tests
@@ -0,0 +1,22 @@
+# Exitcode 0 (`` has no exitcode, but assignment has):
+true;  a=``; echo $?
+false; a=``; echo $?
+true;  a=$(); echo $?
+false; a=$(); echo $?
+# Exitcode 2 (`cmd` expansion sets exitcode after assignment set it to 0):
+true;  a=`exit 2`; echo $?
+false; a=`exit 2`; echo $?
+true;  a=$(exit 2); echo $?
+false; a=$(exit 2); echo $?
+# Exitcode 1 (redirect sets exitcode to 1 on error after them):
+true;  a=`` >/does/not/exist; echo $?
+false; a=`` >/does/not/exist; echo $?
+true;  a=$() >/does/not/exist; echo $?
+false; a=$() >/does/not/exist; echo $?
+true;  a=`exit 2` >/does/not/exist; echo $?
+false; a=`exit 2` >/does/not/exist; echo $?
+true;  a=$(exit 2) >/does/not/exist; echo $?
+false; a=$(exit 2) >/does/not/exist; echo $?
+# ...and assignment still happens despite redirect error:
+true;  a=$(echo b) >/does/not/exist; echo $?
+echo "Done: a=$a"
-- 
1.6.3.3



More information about the busybox-cvs mailing list