diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 00:59:36 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 00:59:36 +0200 |
commit | 4628945cd8d4679912f126d5f18f954210abb7d0 (patch) | |
tree | 84e44413a6c1b3035effa8279d372974d3911275 | |
parent | 11f2e99c13b42675bb65cf2cfd3e3a98f95f2cee (diff) | |
download | busybox-w32-4628945cd8d4679912f126d5f18f954210abb7d0.tar.gz busybox-w32-4628945cd8d4679912f126d5f18f954210abb7d0.tar.bz2 busybox-w32-4628945cd8d4679912f126d5f18f954210abb7d0.zip |
ash: fix "unset OPTIND" throwing an error message
Added test was failing quite severely. Now only one subtest fails
(OPTERR=0 has no effect).
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/ash.c | 4 | ||||
-rw-r--r-- | shell/ash_test/ash-getopts/getopt_simple.right | 34 | ||||
-rwxr-xr-x | shell/ash_test/ash-getopts/getopt_simple.tests | 75 |
3 files changed, 112 insertions, 1 deletions
diff --git a/shell/ash.c b/shell/ash.c index 5c03f1fdc..15c7c325a 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -2099,7 +2099,9 @@ extern struct globals_var *const ash_ptr_to_globals_var; | |||
2099 | static void FAST_FUNC | 2099 | static void FAST_FUNC |
2100 | getoptsreset(const char *value) | 2100 | getoptsreset(const char *value) |
2101 | { | 2101 | { |
2102 | shellparam.optind = number(value) ?: 1; | 2102 | shellparam.optind = 1; |
2103 | if (is_number(value)) | ||
2104 | shellparam.optind = number(value) ?: 1; | ||
2103 | shellparam.optoff = -1; | 2105 | shellparam.optoff = -1; |
2104 | } | 2106 | } |
2105 | #endif | 2107 | #endif |
diff --git a/shell/ash_test/ash-getopts/getopt_simple.right b/shell/ash_test/ash-getopts/getopt_simple.right new file mode 100644 index 000000000..07e3c57f5 --- /dev/null +++ b/shell/ash_test/ash-getopts/getopt_simple.right | |||
@@ -0,0 +1,34 @@ | |||
1 | *** no OPTIND, optstring:'ab' args:-a -b c | ||
2 | var:'a' OPTIND:2 | ||
3 | var:'b' OPTIND:3 | ||
4 | exited: rc:0 var:'?' OPTIND:3 | ||
5 | *** OPTIND=1, optstring:'ab' args:-a -b c | ||
6 | var:'a' OPTIND:2 | ||
7 | var:'b' OPTIND:3 | ||
8 | exited: rc:0 var:'?' OPTIND:3 | ||
9 | *** OPTIND=0, optstring:'ab' args:-a -b c | ||
10 | var:'a' OPTIND:2 | ||
11 | var:'b' OPTIND:3 | ||
12 | exited: rc:0 var:'?' OPTIND:3 | ||
13 | *** unset OPTIND, optstring:'ab' args:-a -b c | ||
14 | var:'a' OPTIND:2 | ||
15 | var:'b' OPTIND:3 | ||
16 | exited: rc:0 var:'?' OPTIND:3 | ||
17 | *** optstring:'ab' args:-a -b c | ||
18 | 1 rc:0 var:'a' OPTIND:2 | ||
19 | 2 rc:0 var:'b' OPTIND:3 | ||
20 | 3 rc:1 var:'?' OPTIND:3 | ||
21 | *** unset OPTIND, optstring:'ab' args:-a c -c -b d | ||
22 | var:'a' OPTIND:2 | ||
23 | exited: rc:0 var:'?' OPTIND:2 | ||
24 | *** unset OPTIND, optstring:'ab' args:-a -c -b d | ||
25 | var:'a' OPTIND:2 | ||
26 | Illegal option -c | ||
27 | var:'?' OPTIND:3 | ||
28 | var:'b' OPTIND:4 | ||
29 | exited: rc:0 var:'?' OPTIND:4 | ||
30 | *** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d | ||
31 | var:'a' OPTIND:2 | ||
32 | var:'?' OPTIND:3 | ||
33 | var:'b' OPTIND:4 | ||
34 | exited: rc:0 var:'?' OPTIND:4 | ||
diff --git a/shell/ash_test/ash-getopts/getopt_simple.tests b/shell/ash_test/ash-getopts/getopt_simple.tests new file mode 100755 index 000000000..8615ae366 --- /dev/null +++ b/shell/ash_test/ash-getopts/getopt_simple.tests | |||
@@ -0,0 +1,75 @@ | |||
1 | # Simple usage cases for getopts. | ||
2 | # | ||
3 | # OPTIND is either not touched at all (first loop with getopts, | ||
4 | # relying on shell startup init), or getopts state is reset | ||
5 | # before new loop with "unset OPTIND", "OPTIND=1" or "OPTIND=0". | ||
6 | # | ||
7 | # Each option is a separate argument (no "-abc"). This conceptually | ||
8 | # needs only $OPTIND to hold getopts state. | ||
9 | # | ||
10 | # We check that loop does not stop on unknown option (sets "?"), | ||
11 | # stops on _first_ non-option argument. | ||
12 | |||
13 | echo "*** no OPTIND, optstring:'ab' args:-a -b c" | ||
14 | var=QWERTY | ||
15 | while getopts "ab" var -a -b c; do | ||
16 | echo "var:'$var' OPTIND:$OPTIND" | ||
17 | done | ||
18 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" | ||
19 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
20 | |||
21 | # Resetting behavior =1 | ||
22 | echo "*** OPTIND=1, optstring:'ab' args:-a -b c" | ||
23 | OPTIND=1 | ||
24 | while getopts "ab" var -a -b c; do | ||
25 | echo "var:'$var' OPTIND:$OPTIND" | ||
26 | done | ||
27 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
28 | |||
29 | # Resetting behavior =0 | ||
30 | echo "*** OPTIND=0, optstring:'ab' args:-a -b c" | ||
31 | OPTIND=0 | ||
32 | while getopts "ab" var -a -b c; do | ||
33 | echo "var:'$var' OPTIND:$OPTIND" | ||
34 | done | ||
35 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
36 | |||
37 | # Resetting behavior "unset" | ||
38 | echo "*** unset OPTIND, optstring:'ab' args:-a -b c" | ||
39 | unset OPTIND | ||
40 | while getopts "ab" var -a -b c; do | ||
41 | echo "var:'$var' OPTIND:$OPTIND" | ||
42 | done | ||
43 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
44 | |||
45 | # What is the final exitcode? | ||
46 | echo "*** optstring:'ab' args:-a -b c" | ||
47 | unset OPTIND | ||
48 | getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND" | ||
49 | getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND" | ||
50 | getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND" | ||
51 | |||
52 | # Where would it stop? c or -c? | ||
53 | echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d" | ||
54 | unset OPTIND | ||
55 | while getopts "ab" var -a c -c -b d; do | ||
56 | echo "var:'$var' OPTIND:$OPTIND" | ||
57 | done | ||
58 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
59 | |||
60 | # What happens on unknown option? | ||
61 | echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d" | ||
62 | unset OPTIND | ||
63 | while getopts "ab" var -a -c -b d; do | ||
64 | echo "var:'$var' OPTIND:$OPTIND" | ||
65 | done | ||
66 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||
67 | |||
68 | # ORTERR=0 suppresses error message? | ||
69 | echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d" | ||
70 | unset OPTIND | ||
71 | OPTERR=0 | ||
72 | while getopts "ab" var -a -c -b d; do | ||
73 | echo "var:'$var' OPTIND:$OPTIND" | ||
74 | done | ||
75 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" | ||