From c3a2b1463e149304cfcc3912c30218df0c4e748a Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Sat, 14 Sep 2019 20:36:16 -0700 Subject: [PATCH] Switch b_enum() from optget() to getopt_long() --- .gitignore | 5 ++--- src/cmd/ksh93/bltins/enum.c | 39 ++++++++++++++++++++++---------- src/cmd/ksh93/docs/conf.py | 2 ++ src/cmd/ksh93/docs/enum.1 | 19 ---------------- src/cmd/ksh93/docs/enum.rst | 42 +++++++++++++++++++++++++++++++++++ src/cmd/ksh93/docs/index.rst | 1 + src/cmd/ksh93/tests/b_enum.sh | 5 ++++- 7 files changed, 78 insertions(+), 35 deletions(-) delete mode 100644 src/cmd/ksh93/docs/enum.1 create mode 100644 src/cmd/ksh93/docs/enum.rst diff --git a/.gitignore b/.gitignore index 0f2093eb8066..912297d3d44b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # There are some patterns below we include reluctantly. They should be in an # individual's ~/.config/git/ignore file. For example, ".DS_Store" for people -# working on MacOS. We include them to help ensure they don't added to the +# working on MacOS. We include them to help ensure they arent't added to the # project. # Files and file extensions that should never be checked in regardless of @@ -34,6 +34,5 @@ ._* Desktop.ini -# Artifacts of the build and test process that should not be committed. +# Artifacts of the build, install, and test process that should not be committed. build/ -src/cmd/ksh93/docs/_build/ diff --git a/src/cmd/ksh93/bltins/enum.c b/src/cmd/ksh93/bltins/enum.c index cfa34f2c1df4..53aaf09d26a2 100644 --- a/src/cmd/ksh93/bltins/enum.c +++ b/src/cmd/ksh93/bltins/enum.c @@ -19,6 +19,7 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep +#include #include #include #include @@ -30,11 +31,16 @@ #include "defs.h" #include "error.h" #include "name.h" -#include "option.h" #include "sfio.h" #include "shcmd.h" #include "stk.h" +static const char *short_options = "+:ip"; +static const struct option long_options[] = { + {"help", no_argument, NULL, 1}, // all builtins support --help + {"ignorecase", no_argument, NULL, 'i'}, + {NULL, 0, NULL, 0}}; + struct Enum { Namfun_t namfun; char node[NV_MINSZ + sizeof(char *)]; @@ -210,7 +216,7 @@ static_fn int sh_outenum(Shell_t *shp, Sfio_t *iop, Namval_t *tp) { int b_enum(int argc, char **argv, Shbltin_t *context) { bool pflag = false, iflag = false; - int i, n; + int i, n, opt; ssize_t sz = -1; Namval_t *np, *tp, *mp; Namarr_t *ap; @@ -218,14 +224,22 @@ int b_enum(int argc, char **argv, Shbltin_t *context) { const char *sp; struct Enum *ep; Shell_t *shp = context->shp; + char *cmd = argv[0]; + struct { Optdisc_t opt; Namval_t *np; } optdisc; if (cmdinit(argc, argv, context, ERROR_NOTIFY)) return -1; - while ((n = optget(argv, sh_optenum))) { - switch (n) { //!OCLINT(MissingDefaultStatement) + + optind = 0; + while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { + switch (opt) { + case 1: { + builtin_print_help(shp, cmd); + return 0; + } case 'p': { pflag = true; break; @@ -235,20 +249,21 @@ int b_enum(int argc, char **argv, Shbltin_t *context) { break; } case ':': { - errormsg(SH_DICT, 2, "%s", opt_info.arg); - break; + builtin_missing_argument(shp, cmd, argv[opterr]); + return 2; } case '?': { - errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg); - __builtin_unreachable(); + builtin_unknown_option(shp, cmd, argv[opterr]); + return 2; } + default: { abort(); } } } - argv += opt_info.index; - argc -= opt_info.index; - if (error_info.errors || argc != 1) { - error(ERROR_USAGE | 2, "%s", optusage(NULL)); + argv += optind; + argc -= optind; + if (argc != 1) { + builtin_usage_error(shp, cmd, "expected one argument, got %d", argc); return 1; } diff --git a/src/cmd/ksh93/docs/conf.py b/src/cmd/ksh93/docs/conf.py index f9e5b3bab062..48c4d2c47e7d 100644 --- a/src/cmd/ksh93/docs/conf.py +++ b/src/cmd/ksh93/docs/conf.py @@ -76,6 +76,8 @@ 'David J. Korn, et. al.', '1'), ('echo', 'echo', 'output a line of text', 'David J. Korn, et. al.', '1'), + ('enum', 'enum', 'create an enumeration type', + 'David J. Korn, et. al.', '1'), ('fg', 'fg', 'move jobs to the foreground', 'David J. Korn, et. al.', '1'), ('source', 'source', 'execute commands in the current environment', diff --git a/src/cmd/ksh93/docs/enum.1 b/src/cmd/ksh93/docs/enum.1 deleted file mode 100644 index 2a126d0e7133..000000000000 --- a/src/cmd/ksh93/docs/enum.1 +++ /dev/null @@ -1,19 +0,0 @@ -[-? -@(#)$Id: enum (AT&T Research) 2013-04-29 $ -] -[+NAME?enum - create an enumeration type] -[+DESCRIPTION?\benum\b is a declaration command that creates an enumeration type \atypename\a that can only store any one of the values in the indexed array variable \atypename\a.] -[+?If the list of \avalue\as is omitted, then \atypename\a must name an indexed array variable with at least two elements.] -[+?When an enumeration variable is used in arithmetic expression, its value is the index into the array that defined it starting from index 0. Enumeration strings can be used in an arithmetic expression when comparing against an enumeration variable.] -[+?The enum \b_Bool\b exists by default with values \btrue\b and \bfalse\b. The predefined alias \bbool\b is defined as \b_Bool\b.] -[i:ignorecase?The values are case insensitive.] -[p?Writes the enums to standard output. If \atypename\a is omitted then all \benum\bs are written.] - -\atypename\a[\b=(\b \avalue\a ... \b)\b] - -[+EXIT STATUS] -{ -[+0?Successful completion.] -[+>0?An error occurred.] -} -[+SEE ALSO?\bksh\b(1), \btypeset\b(1).] diff --git a/src/cmd/ksh93/docs/enum.rst b/src/cmd/ksh93/docs/enum.rst new file mode 100644 index 000000000000..8f53948e05c6 --- /dev/null +++ b/src/cmd/ksh93/docs/enum.rst @@ -0,0 +1,42 @@ +.. default-role:: code + +:index:`enum` -- create an enumeration type +=========================================== + +Synopsis +-------- +| enum [flags] *typename*[ `=(` *value* ... `)` ] + +Description +----------- +`enum` is a declaration command that creates an enumeration type *typename* +that can only store any one of the values in the indexed array variable +*typename*. + +If the list of *value*s is omitted, then *typename* must name an indexed +array variable with at least two elements. + +When an enumeration variable is used in an arithmetic expression, its +value is the index into the array that defined it starting from index +0. Enumeration strings can be used in an arithmetic expression when +comparing against an enumeration variable. + +The enum `_Bool` exists by default with values `true` and `false`. The +predefined alias `bool` is defined as `_Bool`. + +Flags +----- +:-i, --ignorecase: The values are case insensitive. + +:-p: Writes the enums to standard output. If *typename* is omitted + then all `enum`s are written. + +Exit Status +----------- +0 Successful completion. + +>0 An error occurred. + +See Also +-------- +`typeset`\(1). diff --git a/src/cmd/ksh93/docs/index.rst b/src/cmd/ksh93/docs/index.rst index 56623e0c8511..10e569c872da 100644 --- a/src/cmd/ksh93/docs/index.rst +++ b/src/cmd/ksh93/docs/index.rst @@ -20,6 +20,7 @@ Welcome to the Korn Shell continue disown echo + enum fg source diff --git a/src/cmd/ksh93/tests/b_enum.sh b/src/cmd/ksh93/tests/b_enum.sh index c5c810a8f186..19f950f63a7e 100644 --- a/src/cmd/ksh93/tests/b_enum.sh +++ b/src/cmd/ksh93/tests/b_enum.sh @@ -19,7 +19,10 @@ ######################################################################## # ======= -enum 2>&1 | grep -q Usage || log_error "Running enum without any arguments should show usage info" +# Test what happens when enum is invoked with no arguments. +expect="enum: expected one argument, got 0" +actual=$(enum 2>&1) +[[ $actual =~ "$expect" ]] || log_error "enum with no arguments" "$expect" "$actual" # TODO: Moving this line after the `for` loop that below changes output. That's a bug. Fix it. # =======