aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-08-11 00:59:36 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-08-11 00:59:36 +0200
commit4628945cd8d4679912f126d5f18f954210abb7d0 (patch)
tree84e44413a6c1b3035effa8279d372974d3911275
parent11f2e99c13b42675bb65cf2cfd3e3a98f95f2cee (diff)
downloadbusybox-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.c4
-rw-r--r--shell/ash_test/ash-getopts/getopt_simple.right34
-rwxr-xr-xshell/ash_test/ash-getopts/getopt_simple.tests75
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;
2099static void FAST_FUNC 2099static void FAST_FUNC
2100getoptsreset(const char *value) 2100getoptsreset(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
2var:'a' OPTIND:2
3var:'b' OPTIND:3
4exited: rc:0 var:'?' OPTIND:3
5*** OPTIND=1, optstring:'ab' args:-a -b c
6var:'a' OPTIND:2
7var:'b' OPTIND:3
8exited: rc:0 var:'?' OPTIND:3
9*** OPTIND=0, optstring:'ab' args:-a -b c
10var:'a' OPTIND:2
11var:'b' OPTIND:3
12exited: rc:0 var:'?' OPTIND:3
13*** unset OPTIND, optstring:'ab' args:-a -b c
14var:'a' OPTIND:2
15var:'b' OPTIND:3
16exited: rc:0 var:'?' OPTIND:3
17*** optstring:'ab' args:-a -b c
181 rc:0 var:'a' OPTIND:2
192 rc:0 var:'b' OPTIND:3
203 rc:1 var:'?' OPTIND:3
21*** unset OPTIND, optstring:'ab' args:-a c -c -b d
22var:'a' OPTIND:2
23exited: rc:0 var:'?' OPTIND:2
24*** unset OPTIND, optstring:'ab' args:-a -c -b d
25var:'a' OPTIND:2
26Illegal option -c
27var:'?' OPTIND:3
28var:'b' OPTIND:4
29exited: rc:0 var:'?' OPTIND:4
30*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d
31var:'a' OPTIND:2
32var:'?' OPTIND:3
33var:'b' OPTIND:4
34exited: 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
13echo "*** no OPTIND, optstring:'ab' args:-a -b c"
14var=QWERTY
15while getopts "ab" var -a -b c; do
16 echo "var:'$var' OPTIND:$OPTIND"
17done
18# unfortunately, "rc:0" is shown since while's overall exitcode is "success"
19echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
20
21# Resetting behavior =1
22echo "*** OPTIND=1, optstring:'ab' args:-a -b c"
23OPTIND=1
24while getopts "ab" var -a -b c; do
25 echo "var:'$var' OPTIND:$OPTIND"
26done
27echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
28
29# Resetting behavior =0
30echo "*** OPTIND=0, optstring:'ab' args:-a -b c"
31OPTIND=0
32while getopts "ab" var -a -b c; do
33 echo "var:'$var' OPTIND:$OPTIND"
34done
35echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
36
37# Resetting behavior "unset"
38echo "*** unset OPTIND, optstring:'ab' args:-a -b c"
39unset OPTIND
40while getopts "ab" var -a -b c; do
41 echo "var:'$var' OPTIND:$OPTIND"
42done
43echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
44
45# What is the final exitcode?
46echo "*** optstring:'ab' args:-a -b c"
47unset OPTIND
48getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND"
49getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND"
50getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND"
51
52# Where would it stop? c or -c?
53echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d"
54unset OPTIND
55while getopts "ab" var -a c -c -b d; do
56 echo "var:'$var' OPTIND:$OPTIND"
57done
58echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
59
60# What happens on unknown option?
61echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d"
62unset OPTIND
63while getopts "ab" var -a -c -b d; do
64 echo "var:'$var' OPTIND:$OPTIND"
65done
66echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
67
68# ORTERR=0 suppresses error message?
69echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d"
70unset OPTIND
71OPTERR=0
72while getopts "ab" var -a -c -b d; do
73 echo "var:'$var' OPTIND:$OPTIND"
74done
75echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"