Skip to content

Commit

Permalink
Switch b_enum() from optget() to getopt_long()
Browse files Browse the repository at this point in the history
  • Loading branch information
krader1961 committed Sep 15, 2019
1 parent bc155a2 commit c3a2b14
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 35 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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/
39 changes: 27 additions & 12 deletions src/cmd/ksh93/bltins/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
***********************************************************************/
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -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 *)];
Expand Down Expand Up @@ -210,22 +216,30 @@ 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;
char *cp;
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;
Expand All @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
19 changes: 0 additions & 19 deletions src/cmd/ksh93/docs/enum.1

This file was deleted.

42 changes: 42 additions & 0 deletions src/cmd/ksh93/docs/enum.rst
Original file line number Diff line number Diff line change
@@ -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).
1 change: 1 addition & 0 deletions src/cmd/ksh93/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Welcome to the Korn Shell
continue
disown
echo
enum
fg
source

Expand Down
5 changes: 4 additions & 1 deletion src/cmd/ksh93/tests/b_enum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
# =======
Expand Down

0 comments on commit c3a2b14

Please sign in to comment.