summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shell/ash_test/ash-getopts/getopt_silent.right6
-rwxr-xr-xshell/ash_test/ash-getopts/getopt_silent.tests23
-rw-r--r--shell/hush.c48
-rw-r--r--shell/hush_test/hush-getopts/getopt_silent.right6
-rwxr-xr-xshell/hush_test/hush-getopts/getopt_silent.tests23
5 files changed, 89 insertions, 17 deletions
diff --git a/shell/ash_test/ash-getopts/getopt_silent.right b/shell/ash_test/ash-getopts/getopt_silent.right
new file mode 100644
index 0000000..03d4eb1
--- /dev/null
+++ b/shell/ash_test/ash-getopts/getopt_silent.right
@@ -0,0 +1,6 @@
+*** optstring:':ac' args:-a -b -c
+1 rc:0 var:'a' OPTIND:2 OPTARG:''
+2 rc:0 var:'?' OPTIND:3 OPTARG:'b'
+3 rc:0 var:'c' OPTIND:4 OPTARG:''
+4 rc:1 var:'?' OPTIND:4 OPTARG:''
+5 rc:1 var:'?' OPTIND:4 OPTARG:''
diff --git a/shell/ash_test/ash-getopts/getopt_silent.tests b/shell/ash_test/ash-getopts/getopt_silent.tests
new file mode 100755
index 0000000..097d7ba
--- /dev/null
+++ b/shell/ash_test/ash-getopts/getopt_silent.tests
@@ -0,0 +1,23 @@
+# Open Group Base Specifications Issue 7:
+# """
+# If an unknown option is met, VAR shall be set to "?". In this case,
+# if the first character in optstring is ":", OPTARG shall be set
+# to the option character found, but no output shall be written
+# to standard error; otherwise, the shell variable OPTARG shall be
+# unset and a diagnostic message shall be written to standard error."
+# ...
+# If an option-argument is missing:
+# If the first character of optstring is ":", VAR shall be set to ":"
+# and OPTARG shall be set to the option character found.
+# """
+
+echo "*** optstring:':ac' args:-a -b -c"
+getopts ":ac" var -a -b -c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+getopts ":ac" var -a -b -c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+getopts ":ac" var -a -b -c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+getopts ":ac" var -a -b -c; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+# Previous line should result in "rc:1", which is normally treated
+# in getopts loops as exit condition.
+# Nevertheless, let's verify that calling it yet another time doesn't do
+# anything weird:
+getopts ":ac" var -a -b -c; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
diff --git a/shell/hush.c b/shell/hush.c
index 1e58d71..517b8c1 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -9877,11 +9877,6 @@ static int FAST_FUNC builtin_getopts(char **argv)
/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html
TODO:
-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
-OPTARG and no diagnostic message is printed.
-
If a required argument is not found, and getopts is not silent,
a question mark (?) is placed in VAR, OPTARG is unset, and a
diagnostic message is printed. If getopts is silent, then a
@@ -9902,11 +9897,16 @@ Test that VAR is a valid variable name?
return EXIT_FAILURE;
}
- cp = get_local_var_value("OPTERR");
- opterr = cp ? atoi(cp) : 1;
+ if (optstring[0] == ':') {
+ opterr = 0;
+ } else {
+ cp = get_local_var_value("OPTERR");
+ opterr = cp ? atoi(cp) : 1;
+ }
cp = get_local_var_value("OPTIND");
optind = cp ? atoi(cp) : 0;
optarg = NULL;
+ cbuf[1] = '\0';
/* getopts stops on first non-option. Add "+" to force that */
/*if (optstring[0] != '+')*/ {
@@ -9920,25 +9920,39 @@ Test that VAR is a valid variable name?
else
argv = G.global_argv;
c = getopt(string_array_len(argv), argv, optstring);
+
+ /* Set OPTARG */
+ /* Always set or unset, never left as-is, even on exit/error:
+ * "If no option was found, or if the option that was found
+ * does not have an option-argument, OPTARG shall be unset."
+ */
+ cp = optarg;
+ if (c == '?') {
+ /* If ":optstring" and unknown option is seen,
+ * it is stored to OPTARG.
+ */
+ if (optstring[1] == ':') {
+ cbuf[0] = optopt;
+ cp = cbuf;
+ }
+ }
+ if (cp)
+ set_local_var_from_halves("OPTARG", cp);
+ else
+ unset_local_var("OPTARG");
+
+ /* Convert -1 to "?" */
exitcode = EXIT_SUCCESS;
if (c < 0) { /* -1: end of options */
exitcode = EXIT_FAILURE;
c = '?';
}
+
+ /* Set OPTIND */
cbuf[0] = c;
- cbuf[1] = '\0';
set_local_var_from_halves(var, cbuf);
set_local_var_from_halves("OPTIND", utoa(optind));
- /* Always set or unset, never left as-is, even on exit/error:
- * "If no option was found, or if the option that was found
- * does not have an option-argument, OPTARG shall be unset."
- */
- if (optarg)
- set_local_var_from_halves("OPTARG", optarg);
- else
- unset_local_var("OPTARG");
-
return exitcode;
}
#endif
diff --git a/shell/hush_test/hush-getopts/getopt_silent.right b/shell/hush_test/hush-getopts/getopt_silent.right
new file mode 100644
index 0000000..03d4eb1
--- /dev/null
+++ b/shell/hush_test/hush-getopts/getopt_silent.right
@@ -0,0 +1,6 @@
+*** optstring:':ac' args:-a -b -c
+1 rc:0 var:'a' OPTIND:2 OPTARG:''
+2 rc:0 var:'?' OPTIND:3 OPTARG:'b'
+3 rc:0 var:'c' OPTIND:4 OPTARG:''
+4 rc:1 var:'?' OPTIND:4 OPTARG:''
+5 rc:1 var:'?' OPTIND:4 OPTARG:''
diff --git a/shell/hush_test/hush-getopts/getopt_silent.tests b/shell/hush_test/hush-getopts/getopt_silent.tests
new file mode 100755
index 0000000..097d7ba
--- /dev/null
+++ b/shell/hush_test/hush-getopts/getopt_silent.tests
@@ -0,0 +1,23 @@
+# Open Group Base Specifications Issue 7:
+# """
+# If an unknown option is met, VAR shall be set to "?". In this case,
+# if the first character in optstring is ":", OPTARG shall be set
+# to the option character found, but no output shall be written
+# to standard error; otherwise, the shell variable OPTARG shall be
+# unset and a diagnostic message shall be written to standard error."
+# ...
+# If an option-argument is missing:
+# If the first character of optstring is ":", VAR shall be set to ":"
+# and OPTARG shall be set to the option character found.
+# """
+
+echo "*** optstring:':ac' args:-a -b -c"
+getopts ":ac" var -a -b -c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+getopts ":ac" var -a -b -c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+getopts ":ac" var -a -b -c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+getopts ":ac" var -a -b -c; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
+# Previous line should result in "rc:1", which is normally treated
+# in getopts loops as exit condition.
+# Nevertheless, let's verify that calling it yet another time doesn't do
+# anything weird:
+getopts ":ac" var -a -b -c; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"