Skip to content

Commit

Permalink
Switch from getopt_long() to optget_long()
Browse files Browse the repository at this point in the history
Related #507
  • Loading branch information
krader1961 committed Oct 24, 2019
1 parent ddfe6b3 commit 7faa294
Show file tree
Hide file tree
Showing 34 changed files with 450 additions and 450 deletions.
22 changes: 11 additions & 11 deletions src/cmd/ksh93/bltins/alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
***********************************************************************/
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -29,12 +28,13 @@
#include "defs.h"
#include "error.h"
#include "name.h"
#include "optget_long.h"
#include "shcmd.h"
#include "variables.h"

static const char *short_options = "+:ptx";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins support --help
static const char *short_options = "ptx";
static const struct optget_option long_options[] = {
{"help", optget_no_arg, NULL, 1}, // all builtins support --help
{NULL, 0, NULL, 0}};

//
Expand All @@ -58,8 +58,8 @@ int b_alias(int argc, char *argv[], Shbltin_t *context) {
tdata.argnum = 0;
tdata.aflag = *argv[1];

optind = opterr = 0;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
optget_ind = 0;
while ((opt = optget_long(argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
Expand All @@ -78,24 +78,24 @@ int b_alias(int argc, char *argv[], Shbltin_t *context) {
break;
}
case ':': {
builtin_missing_argument(shp, cmd, argv[optind - 1]);
builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
return 2;
}
case '?': {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}

// This would normally be `argv += optind`. However, the setall() function treats argv[0]
// This would normally be `argv += optget_ind`. However, the setall() function treats argv[0]
// specially due to the behavior of the `typeset` command which also calls setall(). Here we are
// passing it a nonsense value that should have argv[0][0] be anything other than a `+` char.
//
// TODO: Convert this to the standard `argv += optind` when `setall()` is modified to have a
// TODO: Convert this to the standard `argv += optget_ind` when `setall()` is modified to have a
// separate flag for the value of the magic `*argv[0]` passed by `typeset` to that function.
argv += (optind - 1);
argv += (optget_ind - 1);
if (!nv_isflag(nvflags, NV_TAGGED)) return setall(argv, nvflags, troot, &tdata);

// Hacks to handle hash -r | --.
Expand Down
20 changes: 10 additions & 10 deletions src/cmd/ksh93/bltins/bg.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
***********************************************************************/
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <stdlib.h>

#include "builtins.h"
#include "defs.h"
#include "error.h"
#include "jobs.h"
#include "optget_long.h"
#include "sfio.h"
#include "shcmd.h"

#ifdef JOBS

static const char *short_options = "+:";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins supports --help
static const char *short_options = "";
static const struct optget_option long_options[] = {
{"help", optget_no_arg, NULL, 1}, // all builtins supports --help
{NULL, 0, NULL, 0}};

//
Expand All @@ -44,27 +44,27 @@ int b_bg(int argc, char *argv[], Shbltin_t *context) {
Shell_t *shp = context->shp;
char *cmd = argv[0];

// We use `getopt_long_only()` rather than `getopt_long()` to facilitate handling negative
// We use `getopt_long_only()` rather than `optget_long()` to facilitate handling negative
// integers that might otherwise look like a flag.
optind = opterr = 0;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
optget_ind = 0;
while ((opt = optget_long(argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
return 0;
}
case ':': {
builtin_missing_argument(shp, cmd, argv[optind - 1]);
builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
return 2;
}
case '?': {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}
argv += optind;
argv += optget_ind;

if (!sh_isoption(shp, SH_MONITOR) || !job.jobcontrol) {
if (sh_isstate(shp, SH_INTERACTIVE)) {
Expand Down
18 changes: 9 additions & 9 deletions src/cmd/ksh93/bltins/break.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
***********************************************************************/
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <stdlib.h>

#include "builtins.h"
#include "defs.h"
#include "error.h"
#include "optget_long.h"
#include "shcmd.h"

static const char *short_options = "+:";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins supports --help
static const char *short_options = "";
static const struct optget_option long_options[] = {
{"help", optget_no_arg, NULL, 1}, // all builtins supports --help
{NULL, 0, NULL, 0}};

//
Expand All @@ -41,26 +41,26 @@ int b_break(int argc, char *argv[], Shbltin_t *context) {
Shell_t *shp = context->shp;
char *cmd = argv[0];

optind = opterr = 0;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
optget_ind = 0;
while ((opt = optget_long(argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
return 0;
}
case ':': {
builtin_missing_argument(shp, cmd, argv[optind - 1]);
builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
return 2;
}
case '?': {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}

argv += optind;
argv += optget_ind;

long n = 1;
char *arg = *argv;
Expand Down
22 changes: 11 additions & 11 deletions src/cmd/ksh93/bltins/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "config_ast.h" // IWYU pragma: keep

#include <dlfcn.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
Expand All @@ -32,6 +31,7 @@
#include "dlldefs.h"
#include "error.h"
#include "name.h"
#include "optget_long.h"
#include "path.h"
#include "sfio.h"
#include "shcmd.h"
Expand All @@ -53,9 +53,9 @@ typedef void (*Libinit_f)(int, void *);
#define GROWLIB 4

static int maxlib;
static const char *short_options = "+:df:lnps";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins supports --help
static const char *short_options = "df:lnps";
static const struct optget_option long_options[] = {
{"help", optget_no_arg, NULL, 1}, // all builtins supports --help
{NULL, 0, NULL, 0}};

//
Expand Down Expand Up @@ -138,8 +138,8 @@ int b_builtin(int argc, char *argv[], Shbltin_t *context) {
stkp = tdata.sh->stk;
if (!tdata.sh->pathlist) path_absolute(tdata.sh, argv[0], NULL);

optind = opterr = 0;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
optget_ind = 0;
while ((opt = optget_long(argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
Expand All @@ -159,7 +159,7 @@ int b_builtin(int argc, char *argv[], Shbltin_t *context) {
break;
}
case 'f': {
arg = optarg;
arg = optget_arg;
break;
}
case 'l': {
Expand All @@ -171,21 +171,21 @@ int b_builtin(int argc, char *argv[], Shbltin_t *context) {
break;
}
case ':': {
builtin_missing_argument(shp, cmd, argv[optind - 1]);
builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
return 2;
}
case '?': {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}

argv += optind;
argv += optget_ind;
if (arg || *argv) {
if (sh_isoption(tdata.sh, SH_RESTRICTED)) {
errormsg(SH_DICT, ERROR_exit(1), e_restricted, argv[-optind]);
errormsg(SH_DICT, ERROR_exit(1), e_restricted, argv[-optget_ind]);
__builtin_unreachable();
}
if (tdata.sh->subshell && !tdata.sh->subshare) sh_subfork();
Expand Down
24 changes: 12 additions & 12 deletions src/cmd/ksh93/bltins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <pwd.h>
#include <stdbool.h>
Expand All @@ -36,15 +35,16 @@
#include "defs.h"
#include "error.h"
#include "name.h"
#include "optget_long.h"
#include "path.h"
#include "sfio.h"
#include "shcmd.h"
#include "stk.h"
#include "variables.h"

static const char *short_options = "+:f:LP@";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins support --help
static const char *short_options = "f:LP@";
static const struct optget_option long_options[] = {
{"help", optget_no_arg, NULL, 1}, // all builtins support --help
{NULL, 0, NULL, 0}};

//
Expand Down Expand Up @@ -81,8 +81,8 @@ int b_cd(int argc, char *argv[], Shbltin_t *context) {
__builtin_unreachable();
}

optind = opterr = 0;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
optget_ind = 0;
while ((opt = optget_long(argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
Expand All @@ -91,9 +91,9 @@ int b_cd(int argc, char *argv[], Shbltin_t *context) {
case 'f': {
char *cp;
fflag = true;
int64_t n = strton64(optarg, &cp, NULL, 0);
int64_t n = strton64(optget_arg, &cp, NULL, 0);
if (*cp || n < 0 || n > INT_MAX) {
errormsg(SH_DICT, ERROR_exit(0), "Invalid dirfd value: %s", optarg);
errormsg(SH_DICT, ERROR_exit(0), "Invalid dirfd value: %s", optget_arg);
return 2;
}
dirfd = n;
Expand All @@ -108,19 +108,19 @@ int b_cd(int argc, char *argv[], Shbltin_t *context) {
break;
}
case ':': {
builtin_missing_argument(shp, cmd, argv[optind - 1]);
builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
return 2;
}
case '?': {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}

argv += optind;
argc -= optind;
argv += optget_ind;
argc -= optget_ind;
dir = argv[0];
if (argc > 2) {
builtin_usage_error(shp, cmd, "Too many arguments (expected at most two args)");
Expand Down
22 changes: 11 additions & 11 deletions src/cmd/ksh93/bltins/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
//
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <string.h>
#include "stdlib.h"

#include "builtins.h"
#include "defs.h"
#include "error.h"
#include "optget_long.h"
#include "shcmd.h"

static const char *short_options = "+:pvxV";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins support --help
static const char *short_options = "pvxV";
static const struct optget_option long_options[] = {
{"help", optget_no_arg, NULL, 1}, // all builtins support --help
{NULL, 0, NULL, 0}};

//
Expand All @@ -48,14 +48,14 @@ int b_command(int argc, char *argv[], Shbltin_t *context) {
char *cmd = argv[0];

// We need to calculate argc because we might have been invoked with it set to zero. And that
// doesn't confuse the AST optget() function but does break getopt_long().
// doesn't confuse the AST optget() function but does break optget_long().
int true_argc = argc;
if (true_argc == 0) {
for (char **cp = argv; *cp; cp++) true_argc++;
}

optind = opterr = 0;
while ((opt = getopt_long(true_argc, argv, short_options, long_options, NULL)) != -1) {
optget_ind = 0;
while ((opt = optget_long(true_argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case 1: {
if (argc != 0) builtin_print_help(shp, cmd);
Expand Down Expand Up @@ -83,19 +83,19 @@ int b_command(int argc, char *argv[], Shbltin_t *context) {
}
case ':': {
if (argc == 0) return 0;
builtin_missing_argument(shp, cmd, argv[optind - 1]);
builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
return 2;
}
case '?': {
if (argc == 0) return 0;
builtin_unknown_option(shp, cmd, argv[optind - 1]);
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}
if (argc == 0) return flags ? 0 : optind;
argv += optind;
if (argc == 0) return flags ? 0 : optget_ind;
argv += optget_ind;
if (!*argv) {
builtin_usage_error(shp, cmd, "missing command argument");
return 2;
Expand Down
Loading

0 comments on commit 7faa294

Please sign in to comment.