aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2015-11-06 16:38:39 +0000
committerRon Yorston <rmy@pobox.com>2015-11-06 16:38:39 +0000
commitd6c9296168ad0a770967714515ffa1fca5e60145 (patch)
treed72556385814a18a0ac039ee0b43ef6f561c317f /shell
parentb60a9b30ba420808c4c9f540dcf92b11ee87a87b (diff)
parent196e400441652946b9c7ad7bc2d78c73885f2359 (diff)
downloadbusybox-w32-d6c9296168ad0a770967714515ffa1fca5e60145.tar.gz
busybox-w32-d6c9296168ad0a770967714515ffa1fca5e60145.tar.bz2
busybox-w32-d6c9296168ad0a770967714515ffa1fca5e60145.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'shell')
-rw-r--r--shell/ash.c101
-rw-r--r--shell/ash_test/ash-misc/func1.right6
-rwxr-xr-xshell/ash_test/ash-misc/func1.tests16
-rw-r--r--shell/ash_test/ash-misc/func2.right5
-rwxr-xr-xshell/ash_test/ash-misc/func2.tests9
-rw-r--r--shell/ash_test/ash-misc/func3.right4
-rwxr-xr-xshell/ash_test/ash-misc/func3.tests8
-rw-r--r--shell/ash_test/ash-misc/func4.right2
-rwxr-xr-xshell/ash_test/ash-misc/func4.tests7
-rw-r--r--shell/ash_test/ash-misc/func5.right6
-rwxr-xr-xshell/ash_test/ash-misc/func5.tests13
-rw-r--r--shell/ash_test/ash-misc/func_args1.right5
-rwxr-xr-xshell/ash_test/ash-misc/func_args1.tests8
-rw-r--r--shell/ash_test/ash-misc/func_bash1.right12
-rwxr-xr-xshell/ash_test/ash-misc/func_bash1.tests28
-rw-r--r--shell/ash_test/ash-misc/func_local1.right3
-rwxr-xr-xshell/ash_test/ash-misc/func_local1.tests5
-rw-r--r--shell/ash_test/ash-misc/func_local2.right14
-rwxr-xr-xshell/ash_test/ash-misc/func_local2.tests7
-rwxr-xr-xshell/hush_test/hush-misc/func_args1.tests2
20 files changed, 228 insertions, 33 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 08b6aa430..de5c44ead 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -8073,36 +8073,40 @@ changepath(const char *new)
8073 clearcmdentry(firstchange); 8073 clearcmdentry(firstchange);
8074 builtinloc = idx_bltin; 8074 builtinloc = idx_bltin;
8075} 8075}
8076 8076enum {
8077#define TEOF 0 8077 TEOF,
8078#define TNL 1 8078 TNL,
8079#define TREDIR 2 8079 TREDIR,
8080#define TWORD 3 8080 TWORD,
8081#define TSEMI 4 8081 TSEMI,
8082#define TBACKGND 5 8082 TBACKGND,
8083#define TAND 6 8083 TAND,
8084#define TOR 7 8084 TOR,
8085#define TPIPE 8 8085 TPIPE,
8086#define TLP 9 8086 TLP,
8087#define TRP 10 8087 TRP,
8088#define TENDCASE 11 8088 TENDCASE,
8089#define TENDBQUOTE 12 8089 TENDBQUOTE,
8090#define TNOT 13 8090 TNOT,
8091#define TCASE 14 8091 TCASE,
8092#define TDO 15 8092 TDO,
8093#define TDONE 16 8093 TDONE,
8094#define TELIF 17 8094 TELIF,
8095#define TELSE 18 8095 TELSE,
8096#define TESAC 19 8096 TESAC,
8097#define TFI 20 8097 TFI,
8098#define TFOR 21 8098 TFOR,
8099#define TIF 22 8099#if ENABLE_ASH_BASH_COMPAT
8100#define TIN 23 8100 TFUNCTION,
8101#define TTHEN 24 8101#endif
8102#define TUNTIL 25 8102 TIF,
8103#define TWHILE 26 8103 TIN,
8104#define TBEGIN 27 8104 TTHEN,
8105#define TEND 28 8105 TUNTIL,
8106 TWHILE,
8107 TBEGIN,
8108 TEND
8109};
8106typedef smallint token_id_t; 8110typedef smallint token_id_t;
8107 8111
8108/* first char is indicating which tokens mark the end of a list */ 8112/* first char is indicating which tokens mark the end of a list */
@@ -8131,6 +8135,9 @@ static const char *const tokname_array[] = {
8131 "\1esac", 8135 "\1esac",
8132 "\1fi", 8136 "\1fi",
8133 "\0for", 8137 "\0for",
8138#if ENABLE_ASH_BASH_COMPAT
8139 "\0function",
8140#endif
8134 "\0if", 8141 "\0if",
8135 "\0in", 8142 "\0in",
8136 "\1then", 8143 "\1then",
@@ -11205,6 +11212,7 @@ simplecmd(void)
11205 int savecheckkwd; 11212 int savecheckkwd;
11206#if ENABLE_ASH_BASH_COMPAT 11213#if ENABLE_ASH_BASH_COMPAT
11207 smallint double_brackets_flag = 0; 11214 smallint double_brackets_flag = 0;
11215 smallint function_flag = 0;
11208#endif 11216#endif
11209 11217
11210 args = NULL; 11218 args = NULL;
@@ -11221,6 +11229,11 @@ simplecmd(void)
11221 t = readtoken(); 11229 t = readtoken();
11222 switch (t) { 11230 switch (t) {
11223#if ENABLE_ASH_BASH_COMPAT 11231#if ENABLE_ASH_BASH_COMPAT
11232 case TFUNCTION:
11233 if (peektoken() != TWORD)
11234 raise_error_unexpected_syntax(TWORD);
11235 function_flag = 1;
11236 break;
11224 case TAND: /* "&&" */ 11237 case TAND: /* "&&" */
11225 case TOR: /* "||" */ 11238 case TOR: /* "||" */
11226 if (!double_brackets_flag) { 11239 if (!double_brackets_flag) {
@@ -11249,6 +11262,29 @@ simplecmd(void)
11249 app = &n->narg.next; 11262 app = &n->narg.next;
11250 savecheckkwd = 0; 11263 savecheckkwd = 0;
11251 } 11264 }
11265#if ENABLE_ASH_BASH_COMPAT
11266 if (function_flag) {
11267 checkkwd = CHKNL | CHKKWD;
11268 switch (peektoken()) {
11269 case TBEGIN:
11270 case TIF:
11271 case TCASE:
11272 case TUNTIL:
11273 case TWHILE:
11274 case TFOR:
11275 goto do_func;
11276 case TLP:
11277 function_flag = 0;
11278 break;
11279 case TWORD:
11280 if (strcmp("[[", wordtext) == 0)
11281 goto do_func;
11282 /* fall through */
11283 default:
11284 raise_error_unexpected_syntax(-1);
11285 }
11286 }
11287#endif
11252 break; 11288 break;
11253 case TREDIR: 11289 case TREDIR:
11254 *rpp = n = redirnode; 11290 *rpp = n = redirnode;
@@ -11256,6 +11292,7 @@ simplecmd(void)
11256 parsefname(); /* read name of redirection file */ 11292 parsefname(); /* read name of redirection file */
11257 break; 11293 break;
11258 case TLP: 11294 case TLP:
11295 IF_ASH_BASH_COMPAT(do_func:)
11259 if (args && app == &args->narg.next 11296 if (args && app == &args->narg.next
11260 && !vars && !redir 11297 && !vars && !redir
11261 ) { 11298 ) {
@@ -11263,7 +11300,7 @@ simplecmd(void)
11263 const char *name; 11300 const char *name;
11264 11301
11265 /* We have a function */ 11302 /* We have a function */
11266 if (readtoken() != TRP) 11303 if (IF_ASH_BASH_COMPAT(!function_flag &&) readtoken() != TRP)
11267 raise_error_unexpected_syntax(TRP); 11304 raise_error_unexpected_syntax(TRP);
11268 name = n->narg.text; 11305 name = n->narg.text;
11269 if (!goodname(name) 11306 if (!goodname(name)
@@ -11276,6 +11313,7 @@ simplecmd(void)
11276 n->narg.next = parse_command(); 11313 n->narg.next = parse_command();
11277 return n; 11314 return n;
11278 } 11315 }
11316 IF_ASH_BASH_COMPAT(function_flag = 0;)
11279 /* fall through */ 11317 /* fall through */
11280 default: 11318 default:
11281 tokpushback = 1; 11319 tokpushback = 1;
@@ -11456,6 +11494,7 @@ parse_command(void)
11456 n1 = list(0); 11494 n1 = list(0);
11457 t = TEND; 11495 t = TEND;
11458 break; 11496 break;
11497 IF_ASH_BASH_COMPAT(case TFUNCTION:)
11459 case TWORD: 11498 case TWORD:
11460 case TREDIR: 11499 case TREDIR:
11461 tokpushback = 1; 11500 tokpushback = 1;
diff --git a/shell/ash_test/ash-misc/func1.right b/shell/ash_test/ash-misc/func1.right
new file mode 100644
index 000000000..e21665aaf
--- /dev/null
+++ b/shell/ash_test/ash-misc/func1.right
@@ -0,0 +1,6 @@
1Hello
2Zero: 0
3One: 1 Param1: World
4Zero: 0 Param1: Restored
5Multi line function
6One: 1
diff --git a/shell/ash_test/ash-misc/func1.tests b/shell/ash_test/ash-misc/func1.tests
new file mode 100755
index 000000000..ffb269fad
--- /dev/null
+++ b/shell/ash_test/ash-misc/func1.tests
@@ -0,0 +1,16 @@
1f() { echo Hello; }
2g () { echo One: $# Param1: $1; }
3h ( )
4{
5 echo -n 'Multi ' && echo -n 'line '
6 echo function
7 false
8}
9
10f
11echo Zero: $?
12set -- Restored
13{ g World; }
14echo Zero: $? Param1: $1
15( h )
16echo One: $?
diff --git a/shell/ash_test/ash-misc/func2.right b/shell/ash_test/ash-misc/func2.right
new file mode 100644
index 000000000..f2a041da7
--- /dev/null
+++ b/shell/ash_test/ash-misc/func2.right
@@ -0,0 +1,5 @@
1First 0
2Second 0
3First 1
4Second 1
5Done
diff --git a/shell/ash_test/ash-misc/func2.tests b/shell/ash_test/ash-misc/func2.tests
new file mode 100755
index 000000000..763203f15
--- /dev/null
+++ b/shell/ash_test/ash-misc/func2.tests
@@ -0,0 +1,9 @@
1i=0
2while test $i != 2; do
3 f() { echo First $i; }
4 f
5 f() { echo Second $i; }
6 f
7 : $((i++))
8done
9echo Done
diff --git a/shell/ash_test/ash-misc/func3.right b/shell/ash_test/ash-misc/func3.right
new file mode 100644
index 000000000..b6d73459a
--- /dev/null
+++ b/shell/ash_test/ash-misc/func3.right
@@ -0,0 +1,4 @@
1One:1
2Zero:0
3One:1
4Five:5
diff --git a/shell/ash_test/ash-misc/func3.tests b/shell/ash_test/ash-misc/func3.tests
new file mode 100755
index 000000000..fa6f26a23
--- /dev/null
+++ b/shell/ash_test/ash-misc/func3.tests
@@ -0,0 +1,8 @@
1f() { false; return; echo BAD; };
2{ f; echo One:$?; }; echo Zero:$?
3
4f() { false; return; };
5f; echo One:$?
6
7f() { return 5; };
8f; echo Five:$?
diff --git a/shell/ash_test/ash-misc/func4.right b/shell/ash_test/ash-misc/func4.right
new file mode 100644
index 000000000..0c87e316a
--- /dev/null
+++ b/shell/ash_test/ash-misc/func4.right
@@ -0,0 +1,2 @@
124
2Done
diff --git a/shell/ash_test/ash-misc/func4.tests b/shell/ash_test/ash-misc/func4.tests
new file mode 100755
index 000000000..74c1b9a46
--- /dev/null
+++ b/shell/ash_test/ash-misc/func4.tests
@@ -0,0 +1,7 @@
1func() {
2 eval "echo \"\${val_${1}}\""
3}
4
5val_x=24
6(func x)
7echo Done
diff --git a/shell/ash_test/ash-misc/func5.right b/shell/ash_test/ash-misc/func5.right
new file mode 100644
index 000000000..2c9d316b3
--- /dev/null
+++ b/shell/ash_test/ash-misc/func5.right
@@ -0,0 +1,6 @@
11
22
33
41
52
63
diff --git a/shell/ash_test/ash-misc/func5.tests b/shell/ash_test/ash-misc/func5.tests
new file mode 100755
index 000000000..e967208cc
--- /dev/null
+++ b/shell/ash_test/ash-misc/func5.tests
@@ -0,0 +1,13 @@
1f() { echo $1; }
2f 1
3
4f() ( echo $1; )
5f 2
6
7f() ( echo $1 )
8f 3
9
10f() for i in 1 2 3; do
11 echo $i
12done
13f
diff --git a/shell/ash_test/ash-misc/func_args1.right b/shell/ash_test/ash-misc/func_args1.right
new file mode 100644
index 000000000..2dfb9629b
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_args1.right
@@ -0,0 +1,5 @@
1params: a b c
2'f 1 2 3' called
3params: a b c
4'f 1 2 3' called
5params: a b c
diff --git a/shell/ash_test/ash-misc/func_args1.tests b/shell/ash_test/ash-misc/func_args1.tests
new file mode 100755
index 000000000..d394c637f
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_args1.tests
@@ -0,0 +1,8 @@
1f() { echo "'f $1 $2 $3' called"; }
2
3set -- a b c
4echo "params: $1 $2 $3"
5f 1 2 3
6echo "params: $1 $2 $3"
7true | f 1 2 3
8echo "params: $1 $2 $3"
diff --git a/shell/ash_test/ash-misc/func_bash1.right b/shell/ash_test/ash-misc/func_bash1.right
new file mode 100644
index 000000000..41bf8828c
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_bash1.right
@@ -0,0 +1,12 @@
11
22
33
41
52
63
71
82
93
101
112
123
diff --git a/shell/ash_test/ash-misc/func_bash1.tests b/shell/ash_test/ash-misc/func_bash1.tests
new file mode 100755
index 000000000..2cc0970e8
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_bash1.tests
@@ -0,0 +1,28 @@
1function f() { echo $1; }
2f 1
3
4function f() ( echo $1; )
5f 2
6
7function f() ( echo $1 )
8f 3
9
10function f() for i in 1 2 3; do
11 echo $i
12done
13f
14
15function f { echo $1; }
16f 1
17
18# the next two don't work
19#function f ( echo $1; )
20f 2
21
22#function f ( echo $1 )
23f 3
24
25function f for i in 1 2 3; do
26 echo $i
27done
28f
diff --git a/shell/ash_test/ash-misc/func_local1.right b/shell/ash_test/ash-misc/func_local1.right
new file mode 100644
index 000000000..312178366
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_local1.right
@@ -0,0 +1,3 @@
1z=a
2z=z
3Done
diff --git a/shell/ash_test/ash-misc/func_local1.tests b/shell/ash_test/ash-misc/func_local1.tests
new file mode 100755
index 000000000..1d594e20c
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_local1.tests
@@ -0,0 +1,5 @@
1export z=z
2f() { local z=a; env | grep ^z; }
3f
4env | grep ^z
5echo Done
diff --git a/shell/ash_test/ash-misc/func_local2.right b/shell/ash_test/ash-misc/func_local2.right
new file mode 100644
index 000000000..fe9343ac8
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_local2.right
@@ -0,0 +1,14 @@
11
22
31
42
51
61
72
82
93
102
112
123
131
14Done
diff --git a/shell/ash_test/ash-misc/func_local2.tests b/shell/ash_test/ash-misc/func_local2.tests
new file mode 100755
index 000000000..1a9ae559d
--- /dev/null
+++ b/shell/ash_test/ash-misc/func_local2.tests
@@ -0,0 +1,7 @@
1x=1
2f() { echo $x; local x=$((x+1)); echo $x; }
3g() { f; echo $x; f; local x=$((x+1)); f; echo $x; f; }
4f
5g
6echo $x
7echo Done
diff --git a/shell/hush_test/hush-misc/func_args1.tests b/shell/hush_test/hush-misc/func_args1.tests
index 157921fb1..d394c637f 100755
--- a/shell/hush_test/hush-misc/func_args1.tests
+++ b/shell/hush_test/hush-misc/func_args1.tests
@@ -1,5 +1,3 @@
1# UNFIXED BUG
2
3f() { echo "'f $1 $2 $3' called"; } 1f() { echo "'f $1 $2 $3' called"; }
4 2
5set -- a b c 3set -- a b c