hush: teach getopts to set/unset OPTARG
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 11 Aug 2017 00:05:21 +0000 (02:05 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 11 Aug 2017 00:05:21 +0000 (02:05 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash_test/ash-getopts/getopt_optarg.right [new file with mode: 0644]
shell/ash_test/ash-getopts/getopt_optarg.tests [new file with mode: 0755]
shell/hush.c
shell/hush_test/hush-getopts/getopt_optarg.right [new file with mode: 0644]
shell/hush_test/hush-getopts/getopt_optarg.tests [new file with mode: 0755]
shell/hush_test/hush-getopts/getopt_positional.tests

diff --git a/shell/ash_test/ash-getopts/getopt_optarg.right b/shell/ash_test/ash-getopts/getopt_optarg.right
new file mode 100644 (file)
index 0000000..dff28de
--- /dev/null
@@ -0,0 +1,18 @@
+*** no OPTIND, optstring:'w:et' args:-q -w e -r -t -y
+Illegal option -q
+var:'?' OPTIND:2 OPTARG:''
+var:'w' OPTIND:4 OPTARG:'e'
+Illegal option -r
+var:'?' OPTIND:5 OPTARG:''
+var:'t' OPTIND:6 OPTARG:''
+Illegal option -y
+var:'?' OPTIND:7 OPTARG:''
+exited: var:'?' OPTIND:7 OPTARG:''
+*** OPTIND=0, optstring:'w:et' args:-w 1 -w2 -w -e -e -t -t
+var:'w' OPTIND:3 OPTARG:'1'
+var:'w' OPTIND:4 OPTARG:'2'
+var:'w' OPTIND:6 OPTARG:'-e'
+var:'e' OPTIND:7 OPTARG:''
+var:'t' OPTIND:8 OPTARG:''
+var:'t' OPTIND:9 OPTARG:''
+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 (executable)
index 0000000..b346284
--- /dev/null
@@ -0,0 +1,16 @@
+set -- -q -w e -r -t -y
+echo "*** no OPTIND, optstring:'w:et' args:$*"
+var=QWERTY
+OPTARG=ASDFGH
+while getopts "w:et" var; do
+       echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+done
+echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+
+set -- -w 1 -w2 -w -e -e -t -t
+echo "*** OPTIND=0, optstring:'w:et' args:$*"
+OPTIND=0
+while getopts "w:et" var; do
+       echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+done
+echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
index dba12c12e548683ebfd34613e2f3b0d613fc358b..f9a8de423ba8daa6b23837ed0e6ee8432e5d880c 100644 (file)
@@ -9874,11 +9874,6 @@ static int FAST_FUNC builtin_getopts(char **argv)
 {
 /*
 TODO:
-if a character is followed by a colon, the option is expected to have
-an argument, which should be separated from it by white space.
-When an option requires an argument, getopts places that argument into
-the variable OPTARG.
-
 If an invalid option is seen, getopts places ? into VAR and, if
 not silent, prints an error message and unsets OPTARG. If
 getopts is silent, the option character found is placed in
@@ -9906,6 +9901,7 @@ Test that VAR is a valid variable name?
        opterr = cp ? atoi(cp) : 1;
        cp = get_local_var_value("OPTIND");
        optind = cp ? atoi(cp) : 0;
+       optarg = NULL;
 
        /* getopts stops on first non-option. Add "+" to force that */
        /*if (optstring[0] != '+')*/ {
@@ -9924,6 +9920,10 @@ Test that VAR is a valid variable name?
                exitcode = EXIT_FAILURE;
                c = '?';
        }
+       if (optarg)
+               set_local_var_from_halves("OPTARG", optarg);
+       else
+               unset_local_var("OPTARG");
        cbuf[0] = c;
        cbuf[1] = '\0';
        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 (file)
index 0000000..9dbd846
--- /dev/null
@@ -0,0 +1,18 @@
+*** no OPTIND, optstring:'w:et' args:-q -w e -r -t -y
+./getopt_optarg.tests: invalid option -- q
+var:'?' OPTIND:2 OPTARG:''
+var:'w' OPTIND:4 OPTARG:'e'
+./getopt_optarg.tests: invalid option -- r
+var:'?' OPTIND:5 OPTARG:''
+var:'t' OPTIND:6 OPTARG:''
+./getopt_optarg.tests: invalid option -- y
+var:'?' OPTIND:7 OPTARG:''
+exited: var:'?' OPTIND:7 OPTARG:''
+*** OPTIND=0, optstring:'w:et' args:-w 1 -w2 -w -e -e -t -t
+var:'w' OPTIND:3 OPTARG:'1'
+var:'w' OPTIND:4 OPTARG:'2'
+var:'w' OPTIND:6 OPTARG:'-e'
+var:'e' OPTIND:7 OPTARG:''
+var:'t' OPTIND:8 OPTARG:''
+var:'t' OPTIND:9 OPTARG:''
+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 (executable)
index 0000000..b346284
--- /dev/null
@@ -0,0 +1,16 @@
+set -- -q -w e -r -t -y
+echo "*** no OPTIND, optstring:'w:et' args:$*"
+var=QWERTY
+OPTARG=ASDFGH
+while getopts "w:et" var; do
+       echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+done
+echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+
+set -- -w 1 -w2 -w -e -e -t -t
+echo "*** OPTIND=0, optstring:'w:et' args:$*"
+OPTIND=0
+while getopts "w:et" var; do
+       echo "var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+done
+echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
index a5404a2a016103ba719cce336b0c34aa874d14e8..ddf063363dfdc1d80eb02e2d4036be550a40bf56 100755 (executable)
@@ -4,5 +4,4 @@ var=QWERTY
 while getopts "we" var; do
        echo "var:'$var' OPTIND:$OPTIND"
 done
-# unfortunately, "rc:0" is shown since while's overall exitcode is "success"
 echo "exited: var:'$var' OPTIND:$OPTIND"