diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 01:32:46 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-11 01:32:46 +0200 |
commit | 74d40589288890fffea437ed2f38ad1f2dc740e5 (patch) | |
tree | fafc1724b3b2c11280c067eaa4eaabb57e5f34cd | |
parent | 4628945cd8d4679912f126d5f18f954210abb7d0 (diff) | |
download | busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.tar.gz busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.tar.bz2 busybox-w32-74d40589288890fffea437ed2f38ad1f2dc740e5.zip |
hush: getopts builtin
function old new delta
builtin_getopts - 271 +271
bltins1 372 384 +12
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 283/0) Total: 283 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/ash_test/ash-getopts/getopt_positional.right | 6 | ||||
-rwxr-xr-x | shell/ash_test/ash-getopts/getopt_positional.tests | 8 | ||||
-rw-r--r-- | shell/hush.c | 77 | ||||
-rw-r--r-- | shell/hush_test/hush-getopts/getopt_positional.right | 6 | ||||
-rwxr-xr-x | shell/hush_test/hush-getopts/getopt_positional.tests | 8 | ||||
-rw-r--r-- | shell/hush_test/hush-getopts/getopt_simple.right | 34 | ||||
-rwxr-xr-x | shell/hush_test/hush-getopts/getopt_simple.tests | 75 |
7 files changed, 212 insertions, 2 deletions
diff --git a/shell/ash_test/ash-getopts/getopt_positional.right b/shell/ash_test/ash-getopts/getopt_positional.right new file mode 100644 index 000000000..37d0ec845 --- /dev/null +++ b/shell/ash_test/ash-getopts/getopt_positional.right | |||
@@ -0,0 +1,6 @@ | |||
1 | *** no OPTIND, optstring:'we' args:-q -w -e r -t -y | ||
2 | Illegal option -q | ||
3 | var:'?' OPTIND:2 | ||
4 | var:'w' OPTIND:3 | ||
5 | var:'e' OPTIND:4 | ||
6 | exited: var:'?' OPTIND:4 | ||
diff --git a/shell/ash_test/ash-getopts/getopt_positional.tests b/shell/ash_test/ash-getopts/getopt_positional.tests new file mode 100755 index 000000000..a5404a2a0 --- /dev/null +++ b/shell/ash_test/ash-getopts/getopt_positional.tests | |||
@@ -0,0 +1,8 @@ | |||
1 | set -- -q -w -e r -t -y | ||
2 | echo "*** no OPTIND, optstring:'we' args:$*" | ||
3 | var=QWERTY | ||
4 | while getopts "we" var; do | ||
5 | echo "var:'$var' OPTIND:$OPTIND" | ||
6 | done | ||
7 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" | ||
8 | echo "exited: var:'$var' OPTIND:$OPTIND" | ||
diff --git a/shell/hush.c b/shell/hush.c index b53d1dcfb..dba12c12e 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -48,7 +48,7 @@ | |||
48 | * tilde expansion | 48 | * tilde expansion |
49 | * aliases | 49 | * aliases |
50 | * builtins mandated by standards we don't support: | 50 | * builtins mandated by standards we don't support: |
51 | * [un]alias, command, fc, getopts: | 51 | * [un]alias, command, fc: |
52 | * command -v CMD: print "/path/to/CMD" | 52 | * command -v CMD: print "/path/to/CMD" |
53 | * prints "CMD" for builtins | 53 | * prints "CMD" for builtins |
54 | * prints "alias ALIAS='EXPANSION'" for aliases | 54 | * prints "alias ALIAS='EXPANSION'" for aliases |
@@ -58,7 +58,6 @@ | |||
58 | * (can use this to override standalone shell as well) | 58 | * (can use this to override standalone shell as well) |
59 | * -p: use default $PATH | 59 | * -p: use default $PATH |
60 | * command BLTIN: disables special-ness (e.g. errors do not abort) | 60 | * command BLTIN: disables special-ness (e.g. errors do not abort) |
61 | * getopts: getopt() for shells | ||
62 | * fc -l[nr] [BEG] [END]: list range of commands in history | 61 | * fc -l[nr] [BEG] [END]: list range of commands in history |
63 | * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands | 62 | * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands |
64 | * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP | 63 | * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP |
@@ -294,6 +293,11 @@ | |||
294 | //config: default y | 293 | //config: default y |
295 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH | 294 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
296 | //config: | 295 | //config: |
296 | //config:config HUSH_GETOPTS | ||
297 | //config: bool "getopts builtin" | ||
298 | //config: default y | ||
299 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH | ||
300 | //config: | ||
297 | //config:config HUSH_MEMLEAK | 301 | //config:config HUSH_MEMLEAK |
298 | //config: bool "memleak builtin (debugging)" | 302 | //config: bool "memleak builtin (debugging)" |
299 | //config: default n | 303 | //config: default n |
@@ -983,6 +987,9 @@ static int builtin_readonly(char **argv) FAST_FUNC; | |||
983 | static int builtin_fg_bg(char **argv) FAST_FUNC; | 987 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
984 | static int builtin_jobs(char **argv) FAST_FUNC; | 988 | static int builtin_jobs(char **argv) FAST_FUNC; |
985 | #endif | 989 | #endif |
990 | #if ENABLE_HUSH_GETOPTS | ||
991 | static int builtin_getopts(char **argv) FAST_FUNC; | ||
992 | #endif | ||
986 | #if ENABLE_HUSH_HELP | 993 | #if ENABLE_HUSH_HELP |
987 | static int builtin_help(char **argv) FAST_FUNC; | 994 | static int builtin_help(char **argv) FAST_FUNC; |
988 | #endif | 995 | #endif |
@@ -1079,6 +1086,9 @@ static const struct built_in_command bltins1[] = { | |||
1079 | #if ENABLE_HUSH_JOB | 1086 | #if ENABLE_HUSH_JOB |
1080 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), | 1087 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
1081 | #endif | 1088 | #endif |
1089 | #if ENABLE_HUSH_GETOPTS | ||
1090 | BLTIN("getopts" , builtin_getopts , NULL), | ||
1091 | #endif | ||
1082 | #if ENABLE_HUSH_HELP | 1092 | #if ENABLE_HUSH_HELP |
1083 | BLTIN("help" , builtin_help , NULL), | 1093 | BLTIN("help" , builtin_help , NULL), |
1084 | #endif | 1094 | #endif |
@@ -9859,6 +9869,69 @@ static int FAST_FUNC builtin_shift(char **argv) | |||
9859 | return EXIT_FAILURE; | 9869 | return EXIT_FAILURE; |
9860 | } | 9870 | } |
9861 | 9871 | ||
9872 | #if ENABLE_HUSH_GETOPTS | ||
9873 | static int FAST_FUNC builtin_getopts(char **argv) | ||
9874 | { | ||
9875 | /* | ||
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 | ||
9883 | not silent, prints an error message and unsets OPTARG. If | ||
9884 | getopts is silent, the option character found is placed in | ||
9885 | OPTARG and no diagnostic message is printed. | ||
9886 | |||
9887 | If a required argument is not found, and getopts is not silent, | ||
9888 | a question mark (?) is placed in VAR, OPTARG is unset, and a | ||
9889 | diagnostic message is printed. If getopts is silent, then a | ||
9890 | colon (:) is placed in VAR and OPTARG is set to the option | ||
9891 | character found. | ||
9892 | |||
9893 | Test that VAR is a valid variable name? | ||
9894 | */ | ||
9895 | char cbuf[2]; | ||
9896 | const char *cp, *optstring, *var; | ||
9897 | int c, exitcode; | ||
9898 | |||
9899 | optstring = *++argv; | ||
9900 | if (!optstring || !(var = *++argv)) { | ||
9901 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); | ||
9902 | return EXIT_FAILURE; | ||
9903 | } | ||
9904 | |||
9905 | cp = get_local_var_value("OPTERR"); | ||
9906 | opterr = cp ? atoi(cp) : 1; | ||
9907 | cp = get_local_var_value("OPTIND"); | ||
9908 | optind = cp ? atoi(cp) : 0; | ||
9909 | |||
9910 | /* getopts stops on first non-option. Add "+" to force that */ | ||
9911 | /*if (optstring[0] != '+')*/ { | ||
9912 | char *s = alloca(strlen(optstring) + 2); | ||
9913 | sprintf(s, "+%s", optstring); | ||
9914 | optstring = s; | ||
9915 | } | ||
9916 | |||
9917 | if (argv[1]) | ||
9918 | argv[0] = G.global_argv[0]; /* for error messages */ | ||
9919 | else | ||
9920 | argv = G.global_argv; | ||
9921 | c = getopt(string_array_len(argv), argv, optstring); | ||
9922 | exitcode = EXIT_SUCCESS; | ||
9923 | if (c < 0) { /* -1: end of options */ | ||
9924 | exitcode = EXIT_FAILURE; | ||
9925 | c = '?'; | ||
9926 | } | ||
9927 | cbuf[0] = c; | ||
9928 | cbuf[1] = '\0'; | ||
9929 | set_local_var_from_halves(var, cbuf); | ||
9930 | set_local_var_from_halves("OPTIND", utoa(optind)); | ||
9931 | return exitcode; | ||
9932 | } | ||
9933 | #endif | ||
9934 | |||
9862 | static int FAST_FUNC builtin_source(char **argv) | 9935 | static int FAST_FUNC builtin_source(char **argv) |
9863 | { | 9936 | { |
9864 | char *arg_path, *filename; | 9937 | char *arg_path, *filename; |
diff --git a/shell/hush_test/hush-getopts/getopt_positional.right b/shell/hush_test/hush-getopts/getopt_positional.right new file mode 100644 index 000000000..f1c942476 --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_positional.right | |||
@@ -0,0 +1,6 @@ | |||
1 | *** no OPTIND, optstring:'we' args:-q -w -e r -t -y | ||
2 | ./getopt_positional.tests: invalid option -- q | ||
3 | var:'?' OPTIND:2 | ||
4 | var:'w' OPTIND:3 | ||
5 | var:'e' OPTIND:4 | ||
6 | exited: var:'?' OPTIND:4 | ||
diff --git a/shell/hush_test/hush-getopts/getopt_positional.tests b/shell/hush_test/hush-getopts/getopt_positional.tests new file mode 100755 index 000000000..a5404a2a0 --- /dev/null +++ b/shell/hush_test/hush-getopts/getopt_positional.tests | |||
@@ -0,0 +1,8 @@ | |||
1 | set -- -q -w -e r -t -y | ||
2 | echo "*** no OPTIND, optstring:'we' args:$*" | ||
3 | var=QWERTY | ||
4 | while getopts "we" var; do | ||
5 | echo "var:'$var' OPTIND:$OPTIND" | ||
6 | done | ||
7 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" | ||
8 | echo "exited: var:'$var' OPTIND:$OPTIND" | ||
diff --git a/shell/hush_test/hush-getopts/getopt_simple.right b/shell/hush_test/hush-getopts/getopt_simple.right new file mode 100644 index 000000000..b4855fa1a --- /dev/null +++ b/shell/hush_test/hush-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 | ./getopt_simple.tests: invalid 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/hush_test/hush-getopts/getopt_simple.tests b/shell/hush_test/hush-getopts/getopt_simple.tests new file mode 100755 index 000000000..8615ae366 --- /dev/null +++ b/shell/hush_test/hush-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" | ||