From 7faa2941b32fd8ec45adb657233d767ec03d484f Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Fri, 18 Oct 2019 19:18:02 -0700 Subject: [PATCH] Switch from getopt_long() to optget_long() Related #507 --- src/cmd/ksh93/bltins/alias.c | 22 +++++------ src/cmd/ksh93/bltins/bg.c | 20 +++++----- src/cmd/ksh93/bltins/break.c | 18 ++++----- src/cmd/ksh93/bltins/builtin.c | 22 +++++------ src/cmd/ksh93/bltins/cd.c | 24 +++++------ src/cmd/ksh93/bltins/command.c | 22 +++++------ src/cmd/ksh93/bltins/continue.c | 18 ++++----- src/cmd/ksh93/bltins/disown.c | 18 ++++----- src/cmd/ksh93/bltins/enum.c | 22 +++++------ src/cmd/ksh93/bltins/eval.c | 18 ++++----- src/cmd/ksh93/bltins/exec.c | 20 +++++----- src/cmd/ksh93/bltins/export.c | 18 ++++----- src/cmd/ksh93/bltins/fg.c | 18 ++++----- src/cmd/ksh93/bltins/print.c | 30 +++++++------- src/cmd/ksh93/bltins/printf.c | 22 +++++------ src/cmd/ksh93/bltins/pwd.c | 20 +++++----- src/cmd/ksh93/bltins/read.c | 42 ++++++++++---------- src/cmd/ksh93/bltins/readonly.c | 18 ++++----- src/cmd/ksh93/bltins/shift.c | 18 ++++----- src/cmd/ksh93/bltins/sleep.c | 18 ++++----- src/cmd/ksh93/bltins/source.c | 18 ++++----- src/cmd/ksh93/bltins/trap.c | 18 ++++----- src/cmd/ksh93/bltins/ulimit.c | 20 +++++----- src/cmd/ksh93/cmds/basename.c | 26 ++++++------ src/cmd/ksh93/cmds/cat.c | 42 ++++++++++---------- src/cmd/ksh93/cmds/chmod.c | 50 +++++++++++------------ src/cmd/ksh93/cmds/cmp.c | 50 +++++++++++------------ src/cmd/ksh93/cmds/cut.c | 70 ++++++++++++++++----------------- src/cmd/ksh93/cmds/dirname.c | 26 ++++++------ src/cmd/ksh93/cmds/logname.c | 18 ++++----- src/cmd/ksh93/cmds/mkdir.c | 26 ++++++------ src/cmd/ksh93/cmds/sync.c | 36 ++++++++--------- src/cmd/ksh93/edit/pcomplete.c | 46 +++++++++++----------- src/cmd/ksh93/sh/shcomp.c | 26 ++++++------ 34 files changed, 450 insertions(+), 450 deletions(-) diff --git a/src/cmd/ksh93/bltins/alias.c b/src/cmd/ksh93/bltins/alias.c index 78238f83fca8..8da23f6d1743 100644 --- a/src/cmd/ksh93/bltins/alias.c +++ b/src/cmd/ksh93/bltins/alias.c @@ -19,7 +19,6 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include @@ -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}}; // @@ -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); @@ -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 | --. diff --git a/src/cmd/ksh93/bltins/bg.c b/src/cmd/ksh93/bltins/bg.c index adc644a459e5..f19766bc77e0 100644 --- a/src/cmd/ksh93/bltins/bg.c +++ b/src/cmd/ksh93/bltins/bg.c @@ -19,21 +19,21 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #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}}; // @@ -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)) { diff --git a/src/cmd/ksh93/bltins/break.c b/src/cmd/ksh93/bltins/break.c index f8050f1963d9..43531990a7f5 100644 --- a/src/cmd/ksh93/bltins/break.c +++ b/src/cmd/ksh93/bltins/break.c @@ -19,17 +19,17 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #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}}; // @@ -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; diff --git a/src/cmd/ksh93/bltins/builtin.c b/src/cmd/ksh93/bltins/builtin.c index 140f11c08288..5225879127c9 100644 --- a/src/cmd/ksh93/bltins/builtin.c +++ b/src/cmd/ksh93/bltins/builtin.c @@ -20,7 +20,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -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" @@ -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}}; // @@ -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); @@ -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': { @@ -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(); diff --git a/src/cmd/ksh93/bltins/cd.c b/src/cmd/ksh93/bltins/cd.c index 0642a2d7bb6e..cab1ebde04b3 100644 --- a/src/cmd/ksh93/bltins/cd.c +++ b/src/cmd/ksh93/bltins/cd.c @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -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}}; // @@ -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); @@ -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; @@ -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)"); diff --git a/src/cmd/ksh93/bltins/command.c b/src/cmd/ksh93/bltins/command.c index 999d8857072c..c880bf45b36d 100644 --- a/src/cmd/ksh93/bltins/command.c +++ b/src/cmd/ksh93/bltins/command.c @@ -22,18 +22,18 @@ // #include "config_ast.h" // IWYU pragma: keep -#include #include #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}}; // @@ -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); @@ -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; diff --git a/src/cmd/ksh93/bltins/continue.c b/src/cmd/ksh93/bltins/continue.c index 2b1122dc9676..4ff8813e2a68 100644 --- a/src/cmd/ksh93/bltins/continue.c +++ b/src/cmd/ksh93/bltins/continue.c @@ -19,17 +19,17 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #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 support --help +static const char *short_options = ""; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -42,26 +42,26 @@ int b_continue(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; n = 1; arg = *argv; diff --git a/src/cmd/ksh93/bltins/disown.c b/src/cmd/ksh93/bltins/disown.c index 2f8b7e8f3c75..1741a71577d2 100644 --- a/src/cmd/ksh93/bltins/disown.c +++ b/src/cmd/ksh93/bltins/disown.c @@ -19,20 +19,20 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #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 support --help +static const char *short_options = ""; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -43,26 +43,26 @@ int b_disown(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; if (!sh_isoption(shp, SH_MONITOR) || !job.jobcontrol) { if (sh_isstate(shp, SH_INTERACTIVE)) { errormsg(SH_DICT, ERROR_exit(1), e_no_jctl); diff --git a/src/cmd/ksh93/bltins/enum.c b/src/cmd/ksh93/bltins/enum.c index 2fad1f5ee58a..7fd8a2229bbb 100644 --- a/src/cmd/ksh93/bltins/enum.c +++ b/src/cmd/ksh93/bltins/enum.c @@ -19,7 +19,6 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include @@ -31,15 +30,16 @@ #include "defs.h" #include "error.h" #include "name.h" +#include "optget_long.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'}, +static const char *short_options = "ip"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"ignorecase", optget_no_arg, NULL, 'i'}, {NULL, 0, NULL, 0}}; struct Enum { @@ -234,8 +234,8 @@ int b_enum(int argc, char **argv, Shbltin_t *context) { if (cmdinit(argc, argv, context, ERROR_NOTIFY)) return -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); @@ -250,19 +250,19 @@ int b_enum(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; if (argc != 1) { builtin_usage_error(shp, cmd, "expected one argument, got %d", argc); return 1; diff --git a/src/cmd/ksh93/bltins/eval.c b/src/cmd/ksh93/bltins/eval.c index 1429b85e546d..a5a13963a73a 100644 --- a/src/cmd/ksh93/bltins/eval.c +++ b/src/cmd/ksh93/bltins/eval.c @@ -19,16 +19,16 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include "builtins.h" #include "defs.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 support --help +static const char *short_options = ""; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -40,26 +40,26 @@ int b_eval(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; if (*argv && **argv) { sh_offstate(shp, SH_MONITOR); sh_eval(shp, sh_sfeval((const char **)argv), 0); diff --git a/src/cmd/ksh93/bltins/exec.c b/src/cmd/ksh93/bltins/exec.c index 1978d640473d..5f5e3d7bee10 100644 --- a/src/cmd/ksh93/bltins/exec.c +++ b/src/cmd/ksh93/bltins/exec.c @@ -19,7 +19,6 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include @@ -31,6 +30,7 @@ #include "fault.h" #include "jobs.h" #include "name.h" +#include "optget_long.h" #include "path.h" #include "shcmd.h" #include "shnodes.h" @@ -87,9 +87,9 @@ static int exec_args(char *argv[], struct login *logp) { abort(); } -static const char *short_options = "+:a:c"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "a:c"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -105,15 +105,15 @@ int b_exec(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 'a': { - logdata.arg0 = optarg; + logdata.arg0 = optget_arg; break; } case 'c': { @@ -121,18 +121,18 @@ int b_exec(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 (*argv) return exec_args(argv, &logdata); return 0; } diff --git a/src/cmd/ksh93/bltins/export.c b/src/cmd/ksh93/bltins/export.c index 86094067e079..be88f7635805 100644 --- a/src/cmd/ksh93/bltins/export.c +++ b/src/cmd/ksh93/bltins/export.c @@ -19,18 +19,18 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include "builtins.h" #include "defs.h" #include "name.h" +#include "optget_long.h" #include "shcmd.h" -static const char *short_options = "+:np"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "np"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -47,8 +47,8 @@ int b_export(int argc, char *argv[], Shbltin_t *context) { tdata.aflag = '-'; tdata.argnum = -1; // do not change size - 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); @@ -63,18 +63,18 @@ int b_export(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 - 1); + argv += (optget_ind - 1); if (!tdata.sh->prefix) tdata.sh->prefix = ""; nvflag_t nvflags = (NV_ASSIGN | NV_EXPORT | NV_IDENT); return setall(argv, nvflags, tdata.sh->var_tree, &tdata); diff --git a/src/cmd/ksh93/bltins/fg.c b/src/cmd/ksh93/bltins/fg.c index 1a3ec76d4254..08c29410f299 100644 --- a/src/cmd/ksh93/bltins/fg.c +++ b/src/cmd/ksh93/bltins/fg.c @@ -19,21 +19,21 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #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}}; // @@ -45,25 +45,25 @@ int b_fg(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; if (!sh_isoption(shp, SH_MONITOR) || !job.jobcontrol) { if (sh_isstate(shp, SH_INTERACTIVE)) { diff --git a/src/cmd/ksh93/bltins/print.c b/src/cmd/ksh93/bltins/print.c index d844faa6e0e7..0a2e90c01022 100644 --- a/src/cmd/ksh93/bltins/print.c +++ b/src/cmd/ksh93/bltins/print.c @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -37,6 +36,7 @@ #include "history.h" #include "io.h" #include "name.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" #include "stk.h" @@ -91,9 +91,9 @@ static_fn ssize_t fmtbase64(Shell_t *, Sfio_t *, char *, const char *, int); static char *nullarg[] = {0, 0}; // The short options deliberately does not include 'R'. Even though it is a valid flag. -static const char *short_options = "+:enf:prsu:vCR"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "enf:prsu:vCR"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -110,8 +110,8 @@ int b_print(int argc, char *argv[], Shbltin_t *context) { prdata.fd = 1; bool done = false; - optind = opterr = 0; - while (!done && (opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { + optget_ind = 0; + while (!done && (opt = optget_long(argc, argv, short_options, long_options)) != -1) { switch (opt) { case 1: { builtin_print_help(shp, cmd); @@ -127,7 +127,7 @@ int b_print(int argc, char *argv[], Shbltin_t *context) { break; } case 'f': { - prdata.format = optarg; + prdata.format = optget_arg; break; } case 's': { @@ -151,13 +151,13 @@ int b_print(int argc, char *argv[], Shbltin_t *context) { break; } case 'u': { - if (optarg[0] == 'p' && optarg[1] == 0) { + if (optget_arg[0] == 'p' && optget_arg[1] == 0) { prdata.fd = shp->coutpipe; prdata.msg = e_query; break; } - prdata.fd = (int)strtol(optarg, &optarg, 10); - if (*optarg) { + prdata.fd = (int)strtol(optget_arg, &optget_arg, 10); + if (*optget_arg) { prdata.fd = -1; } else if (!sh_iovalidfd(shp, prdata.fd)) { prdata.fd = -1; @@ -193,7 +193,7 @@ int b_print(int argc, char *argv[], Shbltin_t *context) { // regardless of whether `-R` or `-Rn` was seen. But any subsequent args are treated // as if `--` was seen. Meaning a second `-n` is treated as a literal `-n` to be // printed. - argv += optind; + argv += optget_ind; int n = strlen(argv[-1]); if (!(argv[-1][0] == '-' && argv[-1][n - 1] == 'R')) { // The current flag must be of the form -*R?. @@ -209,22 +209,22 @@ int b_print(int argc, char *argv[], Shbltin_t *context) { } R_flag = true; prdata.raw = true; - optind = 0; + optget_ind = 0; done = true; 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 (argc < 0 && !(prdata.format = *argv++)) { builtin_usage_error(shp, cmd, "too few arguments"); diff --git a/src/cmd/ksh93/bltins/printf.c b/src/cmd/ksh93/bltins/printf.c index ab7bffa22c03..8f4cfdc67128 100644 --- a/src/cmd/ksh93/bltins/printf.c +++ b/src/cmd/ksh93/bltins/printf.c @@ -19,7 +19,6 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include @@ -27,11 +26,12 @@ #include "defs.h" #include "error.h" #include "name.h" +#include "optget_long.h" #include "shcmd.h" -static const char *short_options = "+:v:"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "v:"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -46,34 +46,34 @@ int b_printf(int argc, char *argv[], Shbltin_t *context) { memset(&prdata, 0, sizeof(prdata)); prdata.fd = 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); return 0; } case 'v': { - prdata.var_name = nv_open(optarg, shp->var_tree, NV_VARNAME | NV_NOARRAY); + prdata.var_name = nv_open(optget_arg, shp->var_tree, NV_VARNAME | NV_NOARRAY); if (!prdata.var_name) { - errormsg(SH_DICT, 2, "Cannot create variable %s", optarg); + errormsg(SH_DICT, 2, "Cannot create variable %s", optget_arg); return 2; } 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 (!*argv) { builtin_usage_error(shp, cmd, "at least one argument (the format) is required"); return 2; diff --git a/src/cmd/ksh93/bltins/pwd.c b/src/cmd/ksh93/bltins/pwd.c index ef023bd9188d..ece9bf8b14a1 100644 --- a/src/cmd/ksh93/bltins/pwd.c +++ b/src/cmd/ksh93/bltins/pwd.c @@ -19,7 +19,6 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include @@ -30,14 +29,15 @@ #include "builtins.h" #include "defs.h" #include "error.h" +#include "optget_long.h" #include "path.h" #include "sfio.h" #include "shcmd.h" #include "stk.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}}; // @@ -50,8 +50,8 @@ int b_pwd(int argc, char *argv[], Shbltin_t *context) { bool pflag = false; int opt, fd = -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); @@ -59,9 +59,9 @@ int b_pwd(int argc, char *argv[], Shbltin_t *context) { } case 'f': { char *cp; - int64_t n = strton64(optarg, &cp, NULL, 0); + int64_t n = strton64(optget_arg, &cp, NULL, 0); if (*cp || n < 0 || n > INT_MAX) { - builtin_usage_error(shp, cmd, "%s: invalid -f value", optarg); + builtin_usage_error(shp, cmd, "%s: invalid -f value", optget_arg); return 2; } fd = n; @@ -76,11 +76,11 @@ int b_pwd(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(); } diff --git a/src/cmd/ksh93/bltins/read.c b/src/cmd/ksh93/bltins/read.c index 8065ad59a912..d78e9ef1ccff 100644 --- a/src/cmd/ksh93/bltins/read.c +++ b/src/cmd/ksh93/bltins/read.c @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -42,6 +41,7 @@ #include "io.h" #include "lexstates.h" #include "name.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" #include "stk.h" @@ -203,9 +203,9 @@ static struct Method methods[] = { {"ksh", NULL}, {NULL, NULL}}; -static const char *short_options = "+:ad:n:p:rsu:t:vACN:S"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "ad:n:p:rsu:t:vACN:S"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -242,8 +242,8 @@ int b_read(int argc, char *argv[], Shbltin_t *context) { goto bypass; } - 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); @@ -264,19 +264,19 @@ int b_read(int argc, char *argv[], Shbltin_t *context) { break; } case 't': { - sec = sh_strnum(shp, optarg, NULL, 1); + sec = sh_strnum(shp, optget_arg, NULL, 1); timeout = sec ? 1000 * sec : 1; break; } case 'd': { flags &= ((1 << (D_FLAG + 1)) - 1); - flags |= (mb1char(&optarg) << (D_FLAG + 1)) | (1 << D_FLAG); + flags |= (mb1char(&optget_arg) << (D_FLAG + 1)) | (1 << D_FLAG); break; } case 'p': { - if (shp->cpipe[0] <= 0 || - (*optarg != '-' && (!strmatch(optarg, "+(\\w)") || isdigit(*optarg)))) { - name = optarg; + if (shp->cpipe[0] <= 0 || (*optget_arg != '-' && (!strmatch(optget_arg, "+(\\w)") || + isdigit(*optget_arg)))) { + name = optget_arg; } else { // For backward compatibility. fd = shp->cpipe[0]; @@ -287,7 +287,7 @@ int b_read(int argc, char *argv[], Shbltin_t *context) { } #if SUPPORT_JSON case 'm': { - method = optarg; + method = optget_arg; flags |= C_FLAG; break; } @@ -295,9 +295,9 @@ int b_read(int argc, char *argv[], Shbltin_t *context) { case 'n': case 'N': { char *cp; - int64_t n = strton64(optarg, &cp, NULL, 0); + int64_t n = strton64(optget_arg, &cp, NULL, 0); if (*cp || n < 0 || n > INT_MAX) { - builtin_usage_error(shp, cmd, "%s: invalid -%c value", optarg, optopt); + builtin_usage_error(shp, cmd, "%s: invalid -%c value", optget_arg, optget_opt); return 2; } len = n; @@ -319,7 +319,7 @@ int b_read(int argc, char *argv[], Shbltin_t *context) { break; } case 'u': { - if (optarg[0] == 'p' && optarg[1] == 0) { + if (optget_arg[0] == 'p' && optget_arg[1] == 0) { fd = shp->cpipe[0]; if (fd <= 0) { errormsg(SH_DICT, ERROR_exit(1), e_query); @@ -329,9 +329,9 @@ int b_read(int argc, char *argv[], Shbltin_t *context) { } char *cp; - int64_t n = strton64(optarg, &cp, NULL, 0); + int64_t n = strton64(optget_arg, &cp, NULL, 0); if (*cp || n < 0 || n > INT_MAX || !sh_iovalidfd(shp, n)) { - builtin_usage_error(shp, cmd, "%s: invalid -u value", optarg); + builtin_usage_error(shp, cmd, "%s: invalid -u value", optget_arg); return 2; } fd = n; @@ -342,21 +342,21 @@ int b_read(int argc, char *argv[], Shbltin_t *context) { break; } case ':': { - if (optopt == 'p' && shp->cpipe[0] > 0) { + if (optget_opt == 'p' && shp->cpipe[0] > 0) { fd = shp->cpipe[0]; break; } - 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 (method) { for (mindex = 0; methods[mindex].name; mindex++) { diff --git a/src/cmd/ksh93/bltins/readonly.c b/src/cmd/ksh93/bltins/readonly.c index f5bf16a1e721..05e35c39b465 100644 --- a/src/cmd/ksh93/bltins/readonly.c +++ b/src/cmd/ksh93/bltins/readonly.c @@ -19,18 +19,18 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include "builtins.h" #include "defs.h" #include "name.h" +#include "optget_long.h" #include "shcmd.h" -static const char *short_options = "+:p"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "p"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -47,8 +47,8 @@ int b_readonly(int argc, char *argv[], Shbltin_t *context) { tdata.aflag = '-'; tdata.argnum = -1; // do not change size - 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); @@ -59,11 +59,11 @@ int b_readonly(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(); } @@ -71,7 +71,7 @@ int b_readonly(int argc, char *argv[], Shbltin_t *context) { } // The unusual RHS is due to how setall() is shared by several closely related commands. - argv += (optind - 1); + argv += (optget_ind - 1); nvflag_t nvflags = (NV_ASSIGN | NV_RDONLY | NV_VARNAME); return setall(argv, nvflags, tdata.sh->var_tree, &tdata); } diff --git a/src/cmd/ksh93/bltins/shift.c b/src/cmd/ksh93/bltins/shift.c index 9519a6781655..bf2bf7f3ebca 100644 --- a/src/cmd/ksh93/bltins/shift.c +++ b/src/cmd/ksh93/bltins/shift.c @@ -19,17 +19,17 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #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 support --help +static const char *short_options = ""; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -40,25 +40,25 @@ int b_shift(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; char *arg = *argv; int n = arg ? (int)sh_arith(shp, arg) : 1; diff --git a/src/cmd/ksh93/bltins/sleep.c b/src/cmd/ksh93/bltins/sleep.c index fbb2d0490231..d9c554ec422a 100644 --- a/src/cmd/ksh93/bltins/sleep.c +++ b/src/cmd/ksh93/bltins/sleep.c @@ -20,7 +20,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -33,14 +32,15 @@ #include "defs.h" #include "error.h" #include "fault.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" #include "tmx.h" #include "tv.h" -static const char *short_options = "+:s"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "s"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -58,8 +58,8 @@ int b_sleep(int argc, char *argv[], Shbltin_t *context) { if (shp->subshell) sh_subfork(); if (!(shp->sigflag[SIGALRM] & (SH_SIGFAULT | SH_SIGOFF))) sh_sigtrap(shp, SIGALRM); - 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); @@ -70,17 +70,17 @@ int b_sleep(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; char *cp = *argv; if (cp) { diff --git a/src/cmd/ksh93/bltins/source.c b/src/cmd/ksh93/bltins/source.c index 9b2ecb65395d..c239f52c1f69 100644 --- a/src/cmd/ksh93/bltins/source.c +++ b/src/cmd/ksh93/bltins/source.c @@ -19,7 +19,6 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include @@ -30,6 +29,7 @@ #include "fault.h" #include "io.h" #include "name.h" +#include "optget_long.h" #include "path.h" #include "sfio.h" #include "shcmd.h" @@ -39,9 +39,9 @@ #define DOTMAX MAXDEPTH // maximum level of . nesting -- same as path recursion max -static const char *short_options = "+:"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = ""; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -64,26 +64,26 @@ int b_source(int argc, char *argv[], Shbltin_t *context) { Sfio_t *iop = NULL; short level; - 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; script = *argv; if (!script) { builtin_usage_error(shp, cmd, "you must provide the name of the script to be sourced"); diff --git a/src/cmd/ksh93/bltins/trap.c b/src/cmd/ksh93/bltins/trap.c index 4e79d1ee050f..e1c72560654d 100644 --- a/src/cmd/ksh93/bltins/trap.c +++ b/src/cmd/ksh93/bltins/trap.c @@ -20,7 +20,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -30,13 +29,14 @@ #include "defs.h" #include "error.h" #include "fault.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" #include "stk.h" -static const char *short_options = "+:alp"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "alp"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -49,8 +49,8 @@ int b_trap(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); @@ -69,17 +69,17 @@ int b_trap(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 (pflag && aflag) { errormsg(SH_DICT, ERROR_usage(2), "-a and -p are mutually exclusive"); diff --git a/src/cmd/ksh93/bltins/ulimit.c b/src/cmd/ksh93/bltins/ulimit.c index 7d43205cc436..1d8762ca2827 100644 --- a/src/cmd/ksh93/bltins/ulimit.c +++ b/src/cmd/ksh93/bltins/ulimit.c @@ -19,7 +19,6 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include @@ -31,15 +30,16 @@ #include "defs.h" #include "error.h" #include "name.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" #define HARD 2 #define SOFT 4 -static const char *short_options = "+:abcdefilmnpqrstuvwxHMST"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "abcdefilmnpqrstuvwxHMST"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {"as", 0, NULL, 'M'}, {"core", 0, NULL, 'c'}, {"cpu", 0, NULL, 't'}, @@ -74,8 +74,8 @@ int b_ulimit(int argc, char *argv[], Shbltin_t *context) { int label, unit, nosupport; rlim_t i; - 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); @@ -94,11 +94,11 @@ int b_ulimit(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: { @@ -116,7 +116,7 @@ int b_ulimit(int argc, char *argv[], Shbltin_t *context) { } } - char *limit = argv[optind]; + char *limit = argv[optget_ind]; // Default to -f. if (hit == 0) { for (int n = 0; shtab_limits[n].option; n++) { @@ -127,7 +127,7 @@ int b_ulimit(int argc, char *argv[], Shbltin_t *context) { } } // Only one option at a time for setting. - if (argc > optind + 1) { + if (argc > optget_ind + 1) { builtin_usage_error(shp, cmd, "too many arguments"); return 2; } diff --git a/src/cmd/ksh93/cmds/basename.c b/src/cmd/ksh93/cmds/basename.c index c305fa26f37e..81a3ff6f0705 100644 --- a/src/cmd/ksh93/cmds/basename.c +++ b/src/cmd/ksh93/cmds/basename.c @@ -28,20 +28,20 @@ */ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include #include "builtins.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" -static const char *short_options = "+:as:"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins supports --help - {"all", no_argument, NULL, 'a'}, - {"suffix", no_argument, NULL, 's'}, +static const char *short_options = "as:"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins supports --help + {"all", optget_no_arg, NULL, 'a'}, + {"suffix", optget_no_arg, NULL, 's'}, {NULL, 0, NULL, 0}}; static void namebase(Sfio_t *outfile, char *pathname, char *suffix) { @@ -84,8 +84,8 @@ int b_basename(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); @@ -97,22 +97,22 @@ int b_basename(int argc, char **argv, Shbltin_t *context) { } case 's': { all = true; - suffix = optarg; + suffix = optget_arg; 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; if (argc < 1 || (!all && argc > 2)) { builtin_print_help(shp, cmd); return 2; diff --git a/src/cmd/ksh93/cmds/cat.c b/src/cmd/ksh93/cmds/cat.c index 0bf60c4e3737..aa170799e4c1 100644 --- a/src/cmd/ksh93/cmds/cat.c +++ b/src/cmd/ksh93/cmds/cat.c @@ -28,7 +28,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -36,6 +35,7 @@ #include "ast.h" #include "builtins.h" #include "error.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" @@ -302,21 +302,21 @@ static int vcat(char *states, Sfio_t *ip, Sfio_t *op, int flags) { } } -static const char *short_options = "+:bdenstuvABDEST"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"number-nonblank", no_argument, NULL, 'b'}, - {"dos-input", no_argument, NULL, 'd'}, - {"number", no_argument, NULL, 'n'}, - {"unbuffer", no_argument, NULL, 'u'}, - {"show-nonprinting", no_argument, NULL, 'v'}, - {"print-chars", no_argument, NULL, 'v'}, - {"show-all", no_argument, NULL, 'A'}, - {"squeeze-blank", no_argument, NULL, 'B'}, - {"dos-output", no_argument, NULL, 'D'}, - {"show-ends", no_argument, NULL, 'E'}, - {"silent", no_argument, NULL, 'S'}, - {"show-blank", no_argument, NULL, 'T'}, +static const char *short_options = "bdenstuvABDEST"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"number-nonblank", optget_no_arg, NULL, 'b'}, + {"dos-input", optget_no_arg, NULL, 'd'}, + {"number", optget_no_arg, NULL, 'n'}, + {"unbuffer", optget_no_arg, NULL, 'u'}, + {"show-nonprinting", optget_no_arg, NULL, 'v'}, + {"print-chars", optget_no_arg, NULL, 'v'}, + {"show-all", optget_no_arg, NULL, 'A'}, + {"squeeze-blank", optget_no_arg, NULL, 'B'}, + {"dos-output", optget_no_arg, NULL, 'D'}, + {"show-ends", optget_no_arg, NULL, 'E'}, + {"silent", optget_no_arg, NULL, 'S'}, + {"show-blank", optget_no_arg, NULL, 'T'}, {NULL, 0, NULL, 0}}; // @@ -337,8 +337,8 @@ int b_cat(int argc, char **argv, Shbltin_t *context) { if (cmdinit(argc, argv, context, 0)) return -1; att = !path_is_bsd_universe(); mode = "r"; - 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); @@ -401,17 +401,17 @@ int b_cat(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; memset(states, 0, sizeof(states)); if (flags & V_FLAG) { diff --git a/src/cmd/ksh93/cmds/chmod.c b/src/cmd/ksh93/cmds/chmod.c index d357538995d8..bb68429a8122 100644 --- a/src/cmd/ksh93/cmds/chmod.c +++ b/src/cmd/ksh93/cmds/chmod.c @@ -28,7 +28,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -41,6 +40,7 @@ #include "builtins.h" #include "defs.h" #include "error.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" @@ -56,22 +56,22 @@ #define ENOSYS EINVAL #endif -static const char *short_options = "+:cfhilnr::w::x::vF:HLPR"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"metaphysical", no_argument, NULL, 'H'}, // don't let clang-format rewrap this list - {"logical", no_argument, NULL, 'L'}, - {"follow", no_argument, NULL, 'L'}, - {"physical", no_argument, NULL, 'P'}, - {"nofollow", no_argument, NULL, 'P'}, - {"recursive", no_argument, NULL, 'R'}, - {"changes", no_argument, NULL, 'c'}, - {"quiet", no_argument, NULL, 'f'}, - {"silent", no_argument, NULL, 'f'}, - {"ignore-umask", no_argument, NULL, 'i'}, - {"symlink", no_argument, NULL, 'h'}, - {"show", no_argument, NULL, 'n'}, - {"reference", required_argument, NULL, 'F'}, +static const char *short_options = "cfhilnr::w::x::vF:HLPR"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"metaphysical", optget_no_arg, NULL, 'H'}, // don't let clang-format rewrap this list + {"logical", optget_no_arg, NULL, 'L'}, + {"follow", optget_no_arg, NULL, 'L'}, + {"physical", optget_no_arg, NULL, 'P'}, + {"nofollow", optget_no_arg, NULL, 'P'}, + {"recursive", optget_no_arg, NULL, 'R'}, + {"changes", optget_no_arg, NULL, 'c'}, + {"quiet", optget_no_arg, NULL, 'f'}, + {"silent", optget_no_arg, NULL, 'f'}, + {"ignore-umask", optget_no_arg, NULL, 'i'}, + {"symlink", optget_no_arg, NULL, 'h'}, + {"show", optget_no_arg, NULL, 'n'}, + {"reference", optget_required_arg, NULL, 'F'}, {NULL, 0, NULL, 0}}; // @@ -104,8 +104,8 @@ int b_chmod(int argc, char **argv, Shbltin_t *context) { // unrecognized short flags. // bool done = false; - optind = opterr = 0; - while (!done && (opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { + optget_ind = 0; + while (!done && (opt = optget_long(argc, argv, short_options, long_options)) != -1) { switch (opt) { case 1: { builtin_print_help(shp, cmd); @@ -137,8 +137,8 @@ int b_chmod(int argc, char **argv, Shbltin_t *context) { break; } case 'F': { - if (stat(optarg, &st)) { - errormsg(SH_DICT, ERROR_exit(0), "%s: cannot stat", optarg); + if (stat(optget_arg, &st)) { + errormsg(SH_DICT, ERROR_exit(0), "%s: cannot stat", optget_arg); return 2; } mode = st.st_mode; @@ -165,22 +165,22 @@ int b_chmod(int argc, char **argv, Shbltin_t *context) { case 'r': case 'w': case 'x': { - optind--; + optget_ind--; done = true; 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 (!*argv || (!amode && !*(argv + 1))) { builtin_usage_error(shp, cmd, "Missing mode and/or file names"); diff --git a/src/cmd/ksh93/cmds/cmp.c b/src/cmd/ksh93/cmds/cmp.c index 84535695ddcd..867e1aba812d 100644 --- a/src/cmd/ksh93/cmds/cmp.c +++ b/src/cmd/ksh93/cmds/cmp.c @@ -32,7 +32,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -42,6 +41,7 @@ #include "builtins.h" #include "defs.h" #include "error.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" @@ -50,19 +50,19 @@ #define CMP_CHARS 0x04 #define CMP_BYTES 0x08 -static const char *short_options = "+:bcd:i:ln:s"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"print-bytes", no_argument, NULL, 'b'}, - {"print-chars", no_argument, NULL, 'c'}, - {"differences", required_argument, NULL, 'd'}, - {"ignore-initial", required_argument, NULL, 'i'}, - {"skip", required_argument, NULL, 'i'}, - {"verbose", no_argument, NULL, 'l'}, - {"bytes", required_argument, NULL, 'n'}, - {"count", required_argument, NULL, 'n'}, - {"quiet", no_argument, NULL, 's'}, - {"silent", no_argument, NULL, 's'}, +static const char *short_options = "bcd:i:ln:s"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"print-bytes", optget_no_arg, NULL, 'b'}, + {"print-chars", optget_no_arg, NULL, 'c'}, + {"differences", optget_required_arg, NULL, 'd'}, + {"ignore-initial", optget_required_arg, NULL, 'i'}, + {"skip", optget_required_arg, NULL, 'i'}, + {"verbose", optget_no_arg, NULL, 'l'}, + {"bytes", optget_required_arg, NULL, 'n'}, + {"count", optget_required_arg, NULL, 'n'}, + {"quiet", optget_no_arg, NULL, 's'}, + {"silent", optget_no_arg, NULL, 's'}, {NULL, 0, NULL, 0}}; static void pretty(Sfio_t *out, int o, int delim, int flags) { @@ -213,8 +213,8 @@ int b_cmp(int argc, char **argv, Shbltin_t *context) { int flags = 0; if (cmdinit(argc, argv, context, 0)) return -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); @@ -231,22 +231,22 @@ int b_cmp(int argc, char **argv, Shbltin_t *context) { case 'd': { flags |= CMP_VERBOSE; char *cp; - differences = strton64(optarg, &cp, NULL, 0); + differences = strton64(optget_arg, &cp, NULL, 0); if (*cp || differences < 0) { - errormsg(SH_DICT, ERROR_exit(0), "%s: invalid -d value", optarg); + errormsg(SH_DICT, ERROR_exit(0), "%s: invalid -d value", optget_arg); return 2; } break; } case 'i': { - o1 = strtoll(optarg, &e, 0); + o1 = strtoll(optget_arg, &e, 0); if (*e == ':') { o2 = strtoll(e + 1, &e, 0); } else { o2 = o1; } if (*e) { - builtin_usage_error(shp, cmd, "%s: skip1:skip2 expected", optarg); + builtin_usage_error(shp, cmd, "%s: skip1:skip2 expected", optget_arg); return 2; } break; @@ -257,9 +257,9 @@ int b_cmp(int argc, char **argv, Shbltin_t *context) { } case 'n': { char *cp; - count = strton64(optarg, &cp, NULL, 0); + count = strton64(optget_arg, &cp, NULL, 0); if (*cp || count < 0) { - errormsg(SH_DICT, ERROR_exit(0), "%s: invalid -n value", optarg); + errormsg(SH_DICT, ERROR_exit(0), "%s: invalid -n value", optget_arg); return 2; } break; @@ -269,17 +269,17 @@ int b_cmp(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 (!(file1 = *argv++) || !(file2 = *argv++)) { builtin_usage_error(shp, cmd, "expected two file name arguments"); return 2; diff --git a/src/cmd/ksh93/cmds/cut.c b/src/cmd/ksh93/cmds/cut.c index 6308a29bd930..e02046943968 100644 --- a/src/cmd/ksh93/cmds/cut.c +++ b/src/cmd/ksh93/cmds/cut.c @@ -27,7 +27,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -38,6 +37,7 @@ #include "builtins.h" #include "defs.h" #include "error.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" #include "stk.h" @@ -75,23 +75,23 @@ typedef struct Cut_s { #define SP_WORD 2 #define SP_WIDE 3 -static const char *short_options = "+:b:c:d:f:nr:sD:NR:"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"newline", no_argument, NULL, 2}, - {"split", no_argument, NULL, 3}, - {"nonewline", no_argument, NULL, 'N'}, - {"nosplit", no_argument, NULL, 'n'}, - {"bytes", required_argument, NULL, 'b'}, - {"characters", required_argument, NULL, 'c'}, - {"delimeter", required_argument, NULL, 'd'}, - {"fields", required_argument, NULL, 'f'}, - {"reclen", required_argument, NULL, 'r'}, - {"only-delimeted", no_argument, NULL, 's'}, - {"suppress", no_argument, NULL, 's'}, - {"output-delimiter", required_argument, NULL, 'D'}, - {"line-delimiter", required_argument, NULL, 'D'}, - {"line-delimeter", required_argument, NULL, 'D'}, +static const char *short_options = "b:c:d:f:nr:sD:NR:"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"newline", optget_no_arg, NULL, 2}, + {"split", optget_no_arg, NULL, 3}, + {"nonewline", optget_no_arg, NULL, 'N'}, + {"nosplit", optget_no_arg, NULL, 'n'}, + {"bytes", optget_required_arg, NULL, 'b'}, + {"characters", optget_required_arg, NULL, 'c'}, + {"delimeter", optget_required_arg, NULL, 'd'}, + {"fields", optget_required_arg, NULL, 'f'}, + {"reclen", optget_required_arg, NULL, 'r'}, + {"only-delimeted", optget_no_arg, NULL, 's'}, + {"suppress", optget_no_arg, NULL, 's'}, + {"output-delimiter", optget_required_arg, NULL, 'D'}, + {"line-delimiter", optget_required_arg, NULL, 'D'}, + {"line-delimeter", optget_required_arg, NULL, 'D'}, {NULL, 0, NULL, 0}}; /* @@ -511,8 +511,8 @@ int b_cut(int argc, char **argv, Shbltin_t *context) { ldelim.chr = '\n'; wdelim.len = ldelim.len = 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); @@ -532,36 +532,36 @@ int b_cut(int argc, char **argv, Shbltin_t *context) { error(2, "f option already specified"); break; } - cp = optarg; + cp = optget_arg; mode |= opt == 'b' ? C_BYTES : C_CHARS; break; } case 'D': { - ldelim.str = optarg; + ldelim.str = optget_arg; if (mbwide()) { - s = optarg; + s = optget_arg; ldelim.chr = mb1char(&s); - if ((n = s - optarg) > 1) { + if ((n = s - optget_arg) > 1) { ldelim.len = n; break; } } - ldelim.chr = *(unsigned char *)optarg; + ldelim.chr = *(unsigned char *)optget_arg; ldelim.len = 1; break; } case 'd': { - wdelim.str = optarg; + wdelim.str = optget_arg; if (mbwide()) { - s = optarg; + s = optget_arg; wdelim.chr = mb1char(&s); - n = s - optarg; + n = s - optget_arg; if (n > 1) { wdelim.len = n; break; } } - wdelim.chr = *(unsigned char *)optarg; + wdelim.chr = *(unsigned char *)optget_arg; wdelim.len = 1; break; } @@ -570,7 +570,7 @@ int b_cut(int argc, char **argv, Shbltin_t *context) { error(2, "c option already specified"); break; } - cp = optarg; + cp = optget_arg; mode |= C_FIELDS; break; } @@ -585,9 +585,9 @@ int b_cut(int argc, char **argv, Shbltin_t *context) { case 'R': case 'r': { char *cp; - reclen = strton64(optarg, &cp, NULL, 0); + reclen = strton64(optget_arg, &cp, NULL, 0); if (*cp || reclen <= 0) { - errormsg(SH_DICT, ERROR_exit(0), "%s: invalid -r value", optarg); + errormsg(SH_DICT, ERROR_exit(0), "%s: invalid -r value", optget_arg); return 2; } break; @@ -597,17 +597,17 @@ int b_cut(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 (!cp) { builtin_usage_error(shp, cmd, "b, c or f option must be specified"); diff --git a/src/cmd/ksh93/cmds/dirname.c b/src/cmd/ksh93/cmds/dirname.c index b6da801078b5..69ebc3515e13 100644 --- a/src/cmd/ksh93/cmds/dirname.c +++ b/src/cmd/ksh93/cmds/dirname.c @@ -28,22 +28,22 @@ */ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include "ast.h" #include "builtins.h" #include "error.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" -static const char *short_options = "+:frx"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"file", no_argument, NULL, 'f'}, - {"relative", no_argument, NULL, 'r'}, - {"executable", no_argument, NULL, 'x'}, +static const char *short_options = "frx"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"file", optget_no_arg, NULL, 'f'}, + {"relative", optget_no_arg, NULL, 'r'}, + {"executable", optget_no_arg, NULL, 'x'}, {NULL, 0, NULL, 0}}; static void l_dirname(Sfio_t *outfile, const char *pathname) { @@ -88,8 +88,8 @@ int b_dirname(int argc, char **argv, Shbltin_t *context) { if (cmdinit(argc, argv, context, 0)) return -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); @@ -109,18 +109,18 @@ int b_dirname(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; if (argc != 1) { builtin_usage_error(shp, cmd, "expected exactly one argument, got %d", argc); return 2; diff --git a/src/cmd/ksh93/cmds/logname.c b/src/cmd/ksh93/cmds/logname.c index 63f6b6c1c58e..511891bbcc70 100644 --- a/src/cmd/ksh93/cmds/logname.c +++ b/src/cmd/ksh93/cmds/logname.c @@ -20,18 +20,18 @@ ***********************************************************************/ #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include #include "builtins.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" -static const char *short_options = "+:"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = ""; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; int b_logname(int argc, char **argv, Shbltin_t *context) { @@ -42,25 +42,25 @@ int b_logname(int argc, char **argv, Shbltin_t *context) { char *cmd = argv[0]; if (cmdinit(argc, argv, context, 0)) return -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); 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 (*argv) { builtin_usage_error(shp, cmd, "this command doesn't take any arguments"); diff --git a/src/cmd/ksh93/cmds/mkdir.c b/src/cmd/ksh93/cmds/mkdir.c index a63264d956e8..20b007cd5848 100644 --- a/src/cmd/ksh93/cmds/mkdir.c +++ b/src/cmd/ksh93/cmds/mkdir.c @@ -21,7 +21,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -30,15 +29,16 @@ #include "ast.h" #include "builtins.h" #include "error.h" +#include "optget_long.h" #include "shcmd.h" #define DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO) -static const char *short_options = "+:m:pv"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"mode", required_argument, NULL, 'm'}, - {"verbose", no_argument, NULL, 'v'}, +static const char *short_options = "m:pv"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"mode", optget_required_arg, NULL, 'm'}, + {"verbose", optget_no_arg, NULL, 'v'}, {NULL, 0, NULL, 0}}; int b_mkdir(int argc, char **argv, Shbltin_t *context) { @@ -57,8 +57,8 @@ int b_mkdir(int argc, char **argv, Shbltin_t *context) { char *cmd = argv[0]; if (cmdinit(argc, argv, context, 0)) return -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); @@ -66,8 +66,8 @@ int b_mkdir(int argc, char **argv, Shbltin_t *context) { } case 'm': mflag = 1; - mode = strperm(optarg, &part, mode); - if (*part) error(ERROR_exit(0), "%s: invalid mode", optarg); + mode = strperm(optget_arg, &part, mode); + if (*part) error(ERROR_exit(0), "%s: invalid mode", optget_arg); break; case 'p': pflag = 1; @@ -76,17 +76,17 @@ int b_mkdir(int argc, char **argv, Shbltin_t *context) { vflag = 1; 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 (!*argv) { builtin_usage_error(shp, cmd, "you must provide at least one directory"); diff --git a/src/cmd/ksh93/cmds/sync.c b/src/cmd/ksh93/cmds/sync.c index c5b337e967a5..0d63c4ce5d8d 100644 --- a/src/cmd/ksh93/cmds/sync.c +++ b/src/cmd/ksh93/cmds/sync.c @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -32,17 +31,18 @@ #include "ast.h" #include "builtins.h" #include "error.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" -static const char *short_options = "+:fs:S:X"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"sfsync", no_argument, NULL, 'f'}, - {"fsync", required_argument, NULL, 's'}, - {"syncfs", required_argument, NULL, 'S'}, - {"sync", no_argument, NULL, 'X'}, - {"all", no_argument, NULL, 'X'}, +static const char *short_options = "fs:S:X"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"sfsync", optget_no_arg, NULL, 'f'}, + {"fsync", optget_required_arg, NULL, 's'}, + {"syncfs", optget_required_arg, NULL, 'S'}, + {"sync", optget_no_arg, NULL, 'X'}, + {"all", optget_no_arg, NULL, 'X'}, {NULL, 0, NULL, 0}}; // keep clang-format from compacting this table #if !_lib_syncfs @@ -63,8 +63,8 @@ int b_sync(int argc, char **argv, Shbltin_t *context) { if (cmdinit(argc, argv, context, 0)) return -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); @@ -76,9 +76,9 @@ int b_sync(int argc, char **argv, Shbltin_t *context) { } case 's': { char *cp; - int64_t n = strton64(optarg, &cp, NULL, 0); + int64_t n = strton64(optget_arg, &cp, NULL, 0); if (*cp || n < 0 || n > INT_MAX) { - builtin_usage_error(shp, cmd, "%s: invalid -N value", optarg); + builtin_usage_error(shp, cmd, "%s: invalid -N value", optget_arg); return 2; } fsync_fd = n; @@ -86,9 +86,9 @@ int b_sync(int argc, char **argv, Shbltin_t *context) { } case 'S': { char *cp; - int64_t n = strton64(optarg, &cp, NULL, 0); + int64_t n = strton64(optget_arg, &cp, NULL, 0); if (*cp || n < 0 || n > INT_MAX) { - builtin_usage_error(shp, cmd, "%s: invalid -N value", optarg); + builtin_usage_error(shp, cmd, "%s: invalid -N value", optget_arg); return 2; } syncfs_fd = n; @@ -100,17 +100,17 @@ int b_sync(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 (*argv) { builtin_usage_error(shp, cmd, "unexpected arguments"); diff --git a/src/cmd/ksh93/edit/pcomplete.c b/src/cmd/ksh93/edit/pcomplete.c index aa63e457a8b5..71b24043713e 100644 --- a/src/cmd/ksh93/edit/pcomplete.c +++ b/src/cmd/ksh93/edit/pcomplete.c @@ -20,7 +20,6 @@ #include "config_ast.h" // IWYU pragma: keep #include -#include #include #include #include @@ -33,6 +32,7 @@ #include "edit.h" #include "error.h" #include "name.h" +#include "optget_long.h" #include "sfio.h" #include "shcmd.h" #include "shtable.h" @@ -452,9 +452,9 @@ static void print_out(struct Complete *cp, Sfio_t *out) { } } -static const char *short_options = "+:abcdefgjkprsuvo:A:C:DEF:G:P:S:W:X:"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help +static const char *short_options = "abcdefgjkprsuvo:A:C:DEF:G:P:S:W:X:"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help {NULL, 0, NULL, 0}}; // @@ -475,16 +475,16 @@ int b_complete(int argc, char *argv[], Shbltin_t *context) { memset(&comp, 0, sizeof(comp)); comp.sh = shp; - 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 'A': { - if ((n = action(Action_names, optarg)) == 0) { - errormsg(SH_DICT, ERROR_exit(1), "invalid -%c option name %s", 'A', optarg); + if ((n = action(Action_names, optget_arg)) == 0) { + errormsg(SH_DICT, ERROR_exit(1), "invalid -%c option name %s", 'A', optget_arg); __builtin_unreachable(); } } @@ -517,8 +517,8 @@ int b_complete(int argc, char *argv[], Shbltin_t *context) { break; } case 'o': { - if ((n = action(Option_names, optarg)) == 0) { - errormsg(SH_DICT, ERROR_exit(1), "invalid -%c option name %s", 'o', optarg); + if ((n = action(Option_names, optget_arg)) == 0) { + errormsg(SH_DICT, ERROR_exit(1), "invalid -%c option name %s", 'o', optget_arg); __builtin_unreachable(); } n = (strchr(Options, opt) - Options); @@ -526,37 +526,37 @@ int b_complete(int argc, char *argv[], Shbltin_t *context) { break; } case 'G': { - comp.globpat = optarg; + comp.globpat = optget_arg; break; } case 'W': { - comp.wordlist = optarg; + comp.wordlist = optget_arg; break; } case 'C': { - comp.command = optarg; + comp.command = optget_arg; break; } case 'F': { - comp.fname = optarg; + comp.fname = optget_arg; break; } case 'S': { - comp.suffix = optarg; + comp.suffix = optget_arg; break; } case 'P': { - comp.prefix = optarg; + comp.prefix = optget_arg; break; } case 'X': { - comp.filter = optarg; + comp.filter = optget_arg; if (strchr(comp.filter, '&')) comp.options |= FILTER_AMP; break; } case 'r': { if (!complete) { // compgen doesn' support this option - builtin_unknown_option(shp, cmd, argv[optind - 1]); + builtin_unknown_option(shp, cmd, argv[optget_ind - 1]); return 2; } delete = true; @@ -564,7 +564,7 @@ int b_complete(int argc, char *argv[], Shbltin_t *context) { } case 'p': { if (!complete) { // compgen doesn' support this option - builtin_unknown_option(shp, cmd, argv[optind - 1]); + builtin_unknown_option(shp, cmd, argv[optget_ind - 1]); return 2; } print = true; @@ -578,18 +578,18 @@ int b_complete(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; if (complete) { char *name; diff --git a/src/cmd/ksh93/sh/shcomp.c b/src/cmd/ksh93/sh/shcomp.c index c1ac94650b61..581c8223cf67 100644 --- a/src/cmd/ksh93/sh/shcomp.c +++ b/src/cmd/ksh93/sh/shcomp.c @@ -25,7 +25,6 @@ // #include "config_ast.h" // IWYU pragma: keep -#include #include #include #include @@ -36,6 +35,7 @@ #include "defs.h" #include "error.h" #include "name.h" +#include "optget_long.h" #include "sfio.h" #include "shnodes.h" #include "stk.h" @@ -44,12 +44,12 @@ #define VERSION 3 static const char header[6] = {CNTL('k'), CNTL('s'), CNTL('h'), 0, VERSION, 0}; -static const char *short_options = "+:nvD"; -static const struct option long_options[] = { - {"help", no_argument, NULL, 1}, // all builtins support --help - {"dictionary", no_argument, NULL, 'D'}, - {"noexec", no_argument, NULL, 'n'}, - {"verbose", no_argument, NULL, 'v'}, +static const char *short_options = "nvD"; +static const struct optget_option long_options[] = { + {"help", optget_no_arg, NULL, 1}, // all builtins support --help + {"dictionary", optget_no_arg, NULL, 'D'}, + {"noexec", optget_no_arg, NULL, 'n'}, + {"verbose", optget_no_arg, NULL, 'v'}, {NULL, 0, NULL, 0}}; int main(int argc, char *argv[]) { @@ -65,8 +65,8 @@ int main(int argc, char *argv[]) { error_info.id = argv[0]; shp = sh_init(argc, argv, 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); @@ -85,18 +85,18 @@ int main(int argc, char *argv[]) { 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; shp->shcomp = 1; if (argc > 2) {