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-4628945cd8d4679912f126d5f18f954210abb7d0.tar.gz |
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; static void FAST_FUNC getoptsreset(const char *value) { - shellparam.optind = number(value) ?: 1; + shellparam.optind = 1; + if (is_number(value)) + shellparam.optind = number(value) ?: 1; shellparam.optoff = -1; } #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 @@ +*** no OPTIND, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** OPTIND=1, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** OPTIND=0, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** unset OPTIND, optstring:'ab' args:-a -b c +var:'a' OPTIND:2 +var:'b' OPTIND:3 +exited: rc:0 var:'?' OPTIND:3 +*** optstring:'ab' args:-a -b c +1 rc:0 var:'a' OPTIND:2 +2 rc:0 var:'b' OPTIND:3 +3 rc:1 var:'?' OPTIND:3 +*** unset OPTIND, optstring:'ab' args:-a c -c -b d +var:'a' OPTIND:2 +exited: rc:0 var:'?' OPTIND:2 +*** unset OPTIND, optstring:'ab' args:-a -c -b d +var:'a' OPTIND:2 +Illegal option -c +var:'?' OPTIND:3 +var:'b' OPTIND:4 +exited: rc:0 var:'?' OPTIND:4 +*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d +var:'a' OPTIND:2 +var:'?' OPTIND:3 +var:'b' OPTIND:4 +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 @@ +# Simple usage cases for getopts. +# +# OPTIND is either not touched at all (first loop with getopts, +# relying on shell startup init), or getopts state is reset +# before new loop with "unset OPTIND", "OPTIND=1" or "OPTIND=0". +# +# Each option is a separate argument (no "-abc"). This conceptually +# needs only $OPTIND to hold getopts state. +# +# We check that loop does not stop on unknown option (sets "?"), +# stops on _first_ non-option argument. + +echo "*** no OPTIND, optstring:'ab' args:-a -b c" +var=QWERTY +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +# unfortunately, "rc:0" is shown since while's overall exitcode is "success" +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# Resetting behavior =1 +echo "*** OPTIND=1, optstring:'ab' args:-a -b c" +OPTIND=1 +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# Resetting behavior =0 +echo "*** OPTIND=0, optstring:'ab' args:-a -b c" +OPTIND=0 +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# Resetting behavior "unset" +echo "*** unset OPTIND, optstring:'ab' args:-a -b c" +unset OPTIND +while getopts "ab" var -a -b c; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# What is the final exitcode? +echo "*** optstring:'ab' args:-a -b c" +unset OPTIND +getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND" +getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND" +getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND" + +# Where would it stop? c or -c? +echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d" +unset OPTIND +while getopts "ab" var -a c -c -b d; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# What happens on unknown option? +echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d" +unset OPTIND +while getopts "ab" var -a -c -b d; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" + +# ORTERR=0 suppresses error message? +echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d" +unset OPTIND +OPTERR=0 +while getopts "ab" var -a -c -b d; do + echo "var:'$var' OPTIND:$OPTIND" +done +echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |