aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-06-16 13:02:47 +0100
committerRon Yorston <rmy@pobox.com>2023-06-16 13:02:47 +0100
commit4b3d7e62cff6015e1c4b856165efd90cf9182a5c (patch)
tree0a59c6a94a47c3e8f1a47b0a3218f9591b842385 /shell
parent0627e352656effac8d8e617378e7a68edfce41df (diff)
parent2ca39ffd447ca874fcea933194829717d5573247 (diff)
downloadbusybox-w32-4b3d7e62cff6015e1c4b856165efd90cf9182a5c.tar.gz
busybox-w32-4b3d7e62cff6015e1c4b856165efd90cf9182a5c.tar.bz2
busybox-w32-4b3d7e62cff6015e1c4b856165efd90cf9182a5c.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'shell')
-rw-r--r--shell/ash_test/ash-misc/elif1.right4
-rwxr-xr-xshell/ash_test/ash-misc/elif1.tests6
-rw-r--r--shell/ash_test/ash-misc/elif2.right2
-rwxr-xr-xshell/ash_test/ash-misc/elif2.tests8
-rw-r--r--shell/hush.c31
-rw-r--r--shell/hush_test/hush-misc/elif1.right4
-rwxr-xr-xshell/hush_test/hush-misc/elif1.tests6
-rw-r--r--shell/hush_test/hush-misc/elif2.right2
-rwxr-xr-xshell/hush_test/hush-misc/elif2.tests8
9 files changed, 63 insertions, 8 deletions
diff --git a/shell/ash_test/ash-misc/elif1.right b/shell/ash_test/ash-misc/elif1.right
new file mode 100644
index 000000000..36dc59fed
--- /dev/null
+++ b/shell/ash_test/ash-misc/elif1.right
@@ -0,0 +1,4 @@
1ELIF1
2ELIF2
3ELIF THEN
4Ok:0
diff --git a/shell/ash_test/ash-misc/elif1.tests b/shell/ash_test/ash-misc/elif1.tests
new file mode 100755
index 000000000..77b8a25ea
--- /dev/null
+++ b/shell/ash_test/ash-misc/elif1.tests
@@ -0,0 +1,6 @@
1if false; then
2 :
3elif echo 'ELIF1'; echo 'ELIF2'; then
4 echo "ELIF THEN"
5fi
6echo "Ok:$?"
diff --git a/shell/ash_test/ash-misc/elif2.right b/shell/ash_test/ash-misc/elif2.right
new file mode 100644
index 000000000..8f2851f91
--- /dev/null
+++ b/shell/ash_test/ash-misc/elif2.right
@@ -0,0 +1,2 @@
1THEN
2Ok:0
diff --git a/shell/ash_test/ash-misc/elif2.tests b/shell/ash_test/ash-misc/elif2.tests
new file mode 100755
index 000000000..3e5876f05
--- /dev/null
+++ b/shell/ash_test/ash-misc/elif2.tests
@@ -0,0 +1,8 @@
1if true; then
2 echo "THEN"
3elif echo "ELIF false"; false; then
4 echo "ELIF THEN"
5else
6 echo "ELSE"
7fi
8echo "Ok:$?"
diff --git a/shell/hush.c b/shell/hush.c
index 810dafd35..cdaa67a3b 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -1061,6 +1061,7 @@ static int builtin_export(char **argv) FAST_FUNC;
1061#if ENABLE_HUSH_READONLY 1061#if ENABLE_HUSH_READONLY
1062static int builtin_readonly(char **argv) FAST_FUNC; 1062static int builtin_readonly(char **argv) FAST_FUNC;
1063#endif 1063#endif
1064static int builtin_false(char **argv) FAST_FUNC;
1064#if ENABLE_HUSH_JOB 1065#if ENABLE_HUSH_JOB
1065static int builtin_fg_bg(char **argv) FAST_FUNC; 1066static int builtin_fg_bg(char **argv) FAST_FUNC;
1066static int builtin_jobs(char **argv) FAST_FUNC; 1067static int builtin_jobs(char **argv) FAST_FUNC;
@@ -1161,6 +1162,7 @@ static const struct built_in_command bltins1[] ALIGN_PTR = {
1161#if ENABLE_HUSH_EXPORT 1162#if ENABLE_HUSH_EXPORT
1162 BLTIN("export" , builtin_export , "Set environment variables"), 1163 BLTIN("export" , builtin_export , "Set environment variables"),
1163#endif 1164#endif
1165 BLTIN("false" , builtin_false , NULL),
1164#if ENABLE_HUSH_JOB 1166#if ENABLE_HUSH_JOB
1165 BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), 1167 BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"),
1166#endif 1168#endif
@@ -9758,7 +9760,7 @@ static int run_list(struct pipe *pi)
9758 smallint last_rword; /* ditto */ 9760 smallint last_rword; /* ditto */
9759#endif 9761#endif
9760 9762
9761 debug_printf_exec("run_list start lvl %d\n", G.run_list_level); 9763 debug_printf_exec("run_list lvl %d start\n", G.run_list_level);
9762 debug_enter(); 9764 debug_enter();
9763 9765
9764#if ENABLE_HUSH_LOOPS 9766#if ENABLE_HUSH_LOOPS
@@ -9817,7 +9819,7 @@ static int run_list(struct pipe *pi)
9817 break; 9819 break;
9818 9820
9819 IF_HAS_KEYWORDS(rword = pi->res_word;) 9821 IF_HAS_KEYWORDS(rword = pi->res_word;)
9820 debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", 9822 debug_printf_exec(": rword:%d cond_code:%d last_rword:%d\n",
9821 rword, cond_code, last_rword); 9823 rword, cond_code, last_rword);
9822 9824
9823 sv_errexit_depth = G.errexit_depth; 9825 sv_errexit_depth = G.errexit_depth;
@@ -9851,23 +9853,29 @@ static int run_list(struct pipe *pi)
9851 } 9853 }
9852 } 9854 }
9853 last_followup = pi->followup; 9855 last_followup = pi->followup;
9854 IF_HAS_KEYWORDS(last_rword = rword;)
9855#if ENABLE_HUSH_IF 9856#if ENABLE_HUSH_IF
9856 if (cond_code) { 9857 if (cond_code != 0) {
9857 if (rword == RES_THEN) { 9858 if (rword == RES_THEN) {
9858 /* if false; then ... fi has exitcode 0! */ 9859 /* if false; then ... fi has exitcode 0! */
9859 G.last_exitcode = rcode = EXIT_SUCCESS; 9860 G.last_exitcode = rcode = EXIT_SUCCESS;
9860 /* "if <false> THEN cmd": skip cmd */ 9861 /* "if <false> THEN cmd": skip cmd */
9862 debug_printf_exec("skipped THEN cmd because IF condition was false\n");
9863 last_rword = rword;
9861 continue; 9864 continue;
9862 } 9865 }
9863 } else { 9866 } else {
9864 if (rword == RES_ELSE || rword == RES_ELIF) { 9867 if (rword == RES_ELSE
9868 || (rword == RES_ELIF && last_rword != RES_ELIF)
9869 ) {
9865 /* "if <true> then ... ELSE/ELIF cmd": 9870 /* "if <true> then ... ELSE/ELIF cmd":
9866 * skip cmd and all following ones */ 9871 * skip cmd and all following ones */
9872 debug_printf_exec("skipped ELSE/ELIF branch because IF condition was true\n");
9867 break; 9873 break;
9868 } 9874 }
9875 //if (rword == RES_THEN): "if <true> THEN cmd", run cmd (fall through)
9869 } 9876 }
9870#endif 9877#endif
9878 IF_HAS_KEYWORDS(last_rword = rword;)
9871#if ENABLE_HUSH_LOOPS 9879#if ENABLE_HUSH_LOOPS
9872 if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ 9880 if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */
9873 if (!for_lcur) { 9881 if (!for_lcur) {
@@ -9943,7 +9951,7 @@ static int run_list(struct pipe *pi)
9943 ); 9951 );
9944 /* TODO: which FNM_xxx flags to use? */ 9952 /* TODO: which FNM_xxx flags to use? */
9945 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); 9953 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
9946 debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", 9954 debug_printf_exec("cond_code=fnmatch(pattern:'%s',str:'%s'):%d\n",
9947 pattern, case_word, cond_code); 9955 pattern, case_word, cond_code);
9948 free(pattern); 9956 free(pattern);
9949 if (cond_code == 0) { 9957 if (cond_code == 0) {
@@ -10069,8 +10077,10 @@ static int run_list(struct pipe *pi)
10069 10077
10070 /* Analyze how result affects subsequent commands */ 10078 /* Analyze how result affects subsequent commands */
10071#if ENABLE_HUSH_IF 10079#if ENABLE_HUSH_IF
10072 if (rword == RES_IF || rword == RES_ELIF) 10080 if (rword == RES_IF || rword == RES_ELIF) {
10081 debug_printf_exec("cond_code=rcode:%d\n", rcode);
10073 cond_code = rcode; 10082 cond_code = rcode;
10083 }
10074#endif 10084#endif
10075 check_jobs_and_continue: 10085 check_jobs_and_continue:
10076 checkjobs(NULL, 0 /*(no pid to wait for)*/); 10086 checkjobs(NULL, 0 /*(no pid to wait for)*/);
@@ -10111,7 +10121,7 @@ static int run_list(struct pipe *pi)
10111 free(case_word); 10121 free(case_word);
10112#endif 10122#endif
10113 debug_leave(); 10123 debug_leave();
10114 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); 10124 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level, rcode);
10115 return rcode; 10125 return rcode;
10116} 10126}
10117 10127
@@ -10823,6 +10833,11 @@ static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM)
10823 return 0; 10833 return 0;
10824} 10834}
10825 10835
10836static int FAST_FUNC builtin_false(char **argv UNUSED_PARAM)
10837{
10838 return 1;
10839}
10840
10826#if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL 10841#if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL
10827static NOINLINE int run_applet_main(char **argv, int (*applet_main_func)(int argc, char **argv)) 10842static NOINLINE int run_applet_main(char **argv, int (*applet_main_func)(int argc, char **argv))
10828{ 10843{
diff --git a/shell/hush_test/hush-misc/elif1.right b/shell/hush_test/hush-misc/elif1.right
new file mode 100644
index 000000000..36dc59fed
--- /dev/null
+++ b/shell/hush_test/hush-misc/elif1.right
@@ -0,0 +1,4 @@
1ELIF1
2ELIF2
3ELIF THEN
4Ok:0
diff --git a/shell/hush_test/hush-misc/elif1.tests b/shell/hush_test/hush-misc/elif1.tests
new file mode 100755
index 000000000..77b8a25ea
--- /dev/null
+++ b/shell/hush_test/hush-misc/elif1.tests
@@ -0,0 +1,6 @@
1if false; then
2 :
3elif echo 'ELIF1'; echo 'ELIF2'; then
4 echo "ELIF THEN"
5fi
6echo "Ok:$?"
diff --git a/shell/hush_test/hush-misc/elif2.right b/shell/hush_test/hush-misc/elif2.right
new file mode 100644
index 000000000..8f2851f91
--- /dev/null
+++ b/shell/hush_test/hush-misc/elif2.right
@@ -0,0 +1,2 @@
1THEN
2Ok:0
diff --git a/shell/hush_test/hush-misc/elif2.tests b/shell/hush_test/hush-misc/elif2.tests
new file mode 100755
index 000000000..3e5876f05
--- /dev/null
+++ b/shell/hush_test/hush-misc/elif2.tests
@@ -0,0 +1,8 @@
1if true; then
2 echo "THEN"
3elif echo "ELIF false"; false; then
4 echo "ELIF THEN"
5else
6 echo "ELSE"
7fi
8echo "Ok:$?"