diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 02:05:21 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 02:05:21 +0200 |
commit | 81f962f3df0d7194b7a52c6f83259727759094c4 (patch) | |
tree | 906d5d55118f0a7c96579c7d12c62b3a14311a7a /shell | |
parent | 74d40589288890fffea437ed2f38ad1f2dc740e5 (diff) | |
download | busybox-w32-81f962f3df0d7194b7a52c6f83259727759094c4.tar.gz busybox-w32-81f962f3df0d7194b7a52c6f83259727759094c4.tar.bz2 busybox-w32-81f962f3df0d7194b7a52c6f83259727759094c4.zip |
hush: teach getopts to set/unset OPTARG
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
-rw-r--r-- | shell/ash_test/ash-getopts/getopt_optarg.right | 18 | ||||
-rwxr-xr-x | shell/ash_test/ash-getopts/getopt_optarg.tests | 16 | ||||
-rw-r--r-- | shell/hush.c | 10 | ||||
-rw-r--r-- | shell/hush_test/hush-getopts/getopt_optarg.right | 18 | ||||
-rwxr-xr-x | shell/hush_test/hush-getopts/getopt_optarg.tests | 16 | ||||
-rwxr-xr-x | shell/hush_test/hush-getopts/getopt_positional.tests | 1 |
6 files changed, 73 insertions, 6 deletions
diff --git a/shell/ash_test/ash-getopts/getopt_optarg.right b/shell/ash_test/ash-getopts/getopt_optarg.right new file mode 100644 index 000000000..dff28de57 --- /dev/null +++ b/shell/ash_test/ash-getopts/getopt_optarg.right | |||
@@ -0,0 +1,18 @@ | |||
1 | *** no OPTIND, optstring:'w:et' args:-q -w e -r -t -y | ||
2 | Illegal option -q | ||
3 | var:'?' OPTIND:2 OPTARG:'' | ||
4 | var:'w' OPTIND:4 OPTARG:'e' | ||
5 | Illegal option -r | ||
6 | var:'?' OPTIND:5 OPTARG:'' | ||
7 | var:'t' OPTIND:6 OPTARG:'' | ||
8 | Illegal option -y | ||
9 | var:'?' OPTIND:7 OPTARG:'' | ||
10 | exited: var:'?' OPTIND:7 OPTARG:'' | ||
11 | *** OPTIND=0, optstring:'w:et' args:-w 1 -w2 -w -e -e -t -t | ||
12 | var:'w' OPTIND:3 OPTARG:'1' | ||
13 | var:'w' OPTIND:4 OPTARG:'2' | ||
14 | var:'w' OPTIND:6 OPTARG:'-e' | ||
15 | var:'e' OPTIND:7 OPTARG:'' | ||
16 | var:'t' OPTIND:8 OPTARG:'' | ||
17 | var:'t' OPTIND:9 OPTARG:'' | ||
18 | exited: var:'?' OPTIND:9 OPTARG:'' | ||
diff --git a/shell/ash_test/ash-getopts/getopt_optarg.tests b/shell/ash_test/ash-getopts/getopt_optarg.tests new file mode 100755 index 000000000..b346284f0 --- /dev/null +++ b/shell/ash_test/ash-getopts/getopt_optarg.tests | |||
@@ -0,0 +1,16 @@ | |||
1 | set -- -q -w e -r -t -y | ||
2 | echo "*** no OPTIND, optstring:'w:et' args:$*" | ||
3 | var=QWERTY | ||
4 | OPTARG=ASDFGH | ||
5 | while getopts "w:et" var; do | ||
6 | echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
7 | done | ||
8 | echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
9 | |||
10 | set -- -w 1 -w2 -w -e -e -t -t | ||
11 | echo "*** OPTIND=0, optstring:'w:et' args:$*" | ||
12 | OPTIND=0 | ||
13 | while getopts "w:et" var; do | ||
14 | echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
15 | done | ||
16 | echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
diff --git a/shell/hush.c b/shell/hush.c index dba12c12e..f9a8de423 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -9874,11 +9874,6 @@ static int FAST_FUNC builtin_getopts(char **argv) | |||
9874 | { | 9874 | { |
9875 | /* | 9875 | /* |
9876 | TODO: | 9876 | TODO: |
9877 | if a character is followed by a colon, the option is expected to have | ||
9878 | an argument, which should be separated from it by white space. | ||
9879 | When an option requires an argument, getopts places that argument into | ||
9880 | the variable OPTARG. | ||
9881 | |||
9882 | If an invalid option is seen, getopts places ? into VAR and, if | 9877 | If an invalid option is seen, getopts places ? into VAR and, if |
9883 | not silent, prints an error message and unsets OPTARG. If | 9878 | not silent, prints an error message and unsets OPTARG. If |
9884 | getopts is silent, the option character found is placed in | 9879 | getopts is silent, the option character found is placed in |
@@ -9906,6 +9901,7 @@ Test that VAR is a valid variable name? | |||
9906 | opterr = cp ? atoi(cp) : 1; | 9901 | opterr = cp ? atoi(cp) : 1; |
9907 | cp = get_local_var_value("OPTIND"); | 9902 | cp = get_local_var_value("OPTIND"); |
9908 | optind = cp ? atoi(cp) : 0; | 9903 | optind = cp ? atoi(cp) : 0; |
9904 | optarg = NULL; | ||
9909 | 9905 | ||
9910 | /* getopts stops on first non-option. Add "+" to force that */ | 9906 | /* getopts stops on first non-option. Add "+" to force that */ |
9911 | /*if (optstring[0] != '+')*/ { | 9907 | /*if (optstring[0] != '+')*/ { |
@@ -9924,6 +9920,10 @@ Test that VAR is a valid variable name? | |||
9924 | exitcode = EXIT_FAILURE; | 9920 | exitcode = EXIT_FAILURE; |
9925 | c = '?'; | 9921 | c = '?'; |
9926 | } | 9922 | } |
9923 | if (optarg) | ||
9924 | set_local_var_from_halves("OPTARG", optarg); | ||
9925 | else | ||
9926 | unset_local_var("OPTARG"); | ||
9927 | cbuf[0] = c; | 9927 | cbuf[0] = c; |
9928 | cbuf[1] = '\0'; | 9928 | cbuf[1] = '\0'; |
9929 | set_local_var_from_halves(var, cbuf); | 9929 | set_local_var_from_halves(var, cbuf); |
diff --git a/shell/hush_test/hush-getopts/getopt_optarg.right b/shell/hush_test/hush-getopts/getopt_optarg.right new file mode 100644 index 000000000..9dbd8460e --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_optarg.right | |||
@@ -0,0 +1,18 @@ | |||
1 | *** no OPTIND, optstring:'w:et' args:-q -w e -r -t -y | ||
2 | ./getopt_optarg.tests: invalid option -- q | ||
3 | var:'?' OPTIND:2 OPTARG:'' | ||
4 | var:'w' OPTIND:4 OPTARG:'e' | ||
5 | ./getopt_optarg.tests: invalid option -- r | ||
6 | var:'?' OPTIND:5 OPTARG:'' | ||
7 | var:'t' OPTIND:6 OPTARG:'' | ||
8 | ./getopt_optarg.tests: invalid option -- y | ||
9 | var:'?' OPTIND:7 OPTARG:'' | ||
10 | exited: var:'?' OPTIND:7 OPTARG:'' | ||
11 | *** OPTIND=0, optstring:'w:et' args:-w 1 -w2 -w -e -e -t -t | ||
12 | var:'w' OPTIND:3 OPTARG:'1' | ||
13 | var:'w' OPTIND:4 OPTARG:'2' | ||
14 | var:'w' OPTIND:6 OPTARG:'-e' | ||
15 | var:'e' OPTIND:7 OPTARG:'' | ||
16 | var:'t' OPTIND:8 OPTARG:'' | ||
17 | var:'t' OPTIND:9 OPTARG:'' | ||
18 | exited: var:'?' OPTIND:9 OPTARG:'' | ||
diff --git a/shell/hush_test/hush-getopts/getopt_optarg.tests b/shell/hush_test/hush-getopts/getopt_optarg.tests new file mode 100755 index 000000000..b346284f0 --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_optarg.tests | |||
@@ -0,0 +1,16 @@ | |||
1 | set -- -q -w e -r -t -y | ||
2 | echo "*** no OPTIND, optstring:'w:et' args:$*" | ||
3 | var=QWERTY | ||
4 | OPTARG=ASDFGH | ||
5 | while getopts "w:et" var; do | ||
6 | echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
7 | done | ||
8 | echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
9 | |||
10 | set -- -w 1 -w2 -w -e -e -t -t | ||
11 | echo "*** OPTIND=0, optstring:'w:et' args:$*" | ||
12 | OPTIND=0 | ||
13 | while getopts "w:et" var; do | ||
14 | echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
15 | done | ||
16 | echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" | ||
diff --git a/shell/hush_test/hush-getopts/getopt_positional.tests b/shell/hush_test/hush-getopts/getopt_positional.tests index a5404a2a0..ddf063363 100755 --- a/shell/hush_test/hush-getopts/getopt_positional.tests +++ b/shell/hush_test/hush-getopts/getopt_positional.tests | |||
@@ -4,5 +4,4 @@ var=QWERTY | |||
4 | while getopts "we" var; do | 4 | while getopts "we" var; do |
5 | echo "var:'$var' OPTIND:$OPTIND" | 5 | echo "var:'$var' OPTIND:$OPTIND" |
6 | done | 6 | done |
7 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" | ||
8 | echo "exited: var:'$var' OPTIND:$OPTIND" | 7 | echo "exited: var:'$var' OPTIND:$OPTIND" |