Skip to content

Commit

Permalink
Convert remaining getopt_long_only()
Browse files Browse the repository at this point in the history
Related #507
  • Loading branch information
krader1961 committed Oct 24, 2019
1 parent a9b86b6 commit 84648ca
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 174 deletions.
53 changes: 21 additions & 32 deletions src/cmd/ksh93/bltins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,61 @@
***********************************************************************/
#include "config_ast.h" // IWYU pragma: keep

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

#include "ast.h"
#include "builtins.h"
#include "defs.h"
#include "fault.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}};

//
// Builtin `exit` command. See also the return.c module which is similar to this module.
//
int b_exit(int argc, char *argv[], Shbltin_t *context) {
int n, opt;
char *arg;
int opt;
Shell_t *shp = context->shp;
char *cmd = argv[0];
checkpt_t *pp = shp->jmplist;

// We use `getopt_long_only()` rather than `getopt_long()` to facilitate handling negative
// integers that might otherwise look like a flag.
optind = opterr = 0;
bool done = false;
while (!done && (opt = getopt_long_only(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 -2: {
done = true;
optget_ind--;
break;
}
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 '?': {
if (!strmatch(argv[optind - 1], "[+-]+([0-9])")) {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
return 2;
}
optind--;
done = true;
break;
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}
argv += optget_ind;

pp->mode = SH_JMPEXIT;
argv += optind;
arg = *argv;
n = arg ? (int)strtol(arg, NULL, 10) : shp->oldexit;
if (n < 0 || n == 256 || n > SH_EXITMASK + shp->gd->sigmax) {
n &= ((unsigned int)n) & SH_EXITMASK;
}

// Return outside of function, dotscript and profile is exit.
if (shp->fn_depth == 0 && shp->dot_depth == 0 && !sh_isstate(shp, SH_PROFILE)) {
pp->mode = SH_JMPEXIT;
int exit_val = *argv ? strtol(*argv, NULL, 10) : shp->oldexit;
if (exit_val < 0 || exit_val == 256 || exit_val > SH_EXITMASK + shp->gd->sigmax) {
exit_val &= ((unsigned int)exit_val) & SH_EXITMASK;
}

shp->savexit = n;
shp->jmplist->mode = SH_JMPEXIT;
shp->savexit = exit_val;
sh_exit(shp, shp->savexit);
__builtin_unreachable();
}
65 changes: 28 additions & 37 deletions src/cmd/ksh93/bltins/hist.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
Expand All @@ -39,6 +38,7 @@
#include "history.h"
#include "io.h"
#include "name.h"
#include "optget_long.h"
#include "sfio.h"
#include "shcmd.h"
#include "stk.h"
Expand All @@ -48,9 +48,9 @@

static_fn void hist_subst(Shell_t *shp, const char *, int fd, const char *);

static const char *short_options = "+:e:lnprsN:";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins support --help
static const char *short_options = "#e:lnprsN:";
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 @@ -80,18 +80,29 @@ int b_hist(int argc, char *argv[], Shbltin_t *context) {
}
hp = shp->gd->hist_ptr;

// We use `getopt_long_only()` rather than `getopt_long()` to facilitate handling negative
// integers that might otherwise look like a flag.
optind = opterr = 0;
optget_ind = 0;
bool done = false;
while (!done && (opt = getopt_long_only(argc, argv, short_options, long_options, NULL)) != -1) {
while (!done && (opt = optget_long(argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case -2: {
if (indx == -1) {
if (optget_num > INT_MAX) {
builtin_usage_error(shp, cmd, "%s: invalid -N value", argv[optget_ind - 1]);
return 2;
}
flag = hist_max(hp) - optget_num - 1;
if (flag < 0) flag = 1;
range[++indx] = flag;
}
done = true;
break;
}
case 1: {
builtin_print_help(shp, cmd);
return 0;
}
case 'e': {
edit = optarg;
edit = optget_arg;
break;
}
case 'n': {
Expand All @@ -115,53 +126,33 @@ int b_hist(int argc, char *argv[], Shbltin_t *context) {
break;
}
case 'N': {
if (indx <= 0) {
if (indx == -1) {
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;
}

flag = hist_max(hp) - n - 1;
if (flag < 0) flag = 1;
range[++indx] = flag;
}
break;
}
case ':': {
builtin_missing_argument(shp, cmd, argv[optind - 1]);
builtin_missing_argument(shp, cmd, argv[optget_ind - 1]);
return 2;
}
case '?': {
char *cp;
int64_t n = strton64(argv[optind - 1] + 1, &cp, NULL, 0);
if (*cp) {
// It's not an integer so it's an invalid flag.
builtin_unknown_option(shp, cmd, argv[optind - 1]);
return 2;
}
// Looks like a negative integer which means it's equivalent to -N followed by its
// absolute value.
if (indx <= 0) {
if (*cp || n < 0 || n > INT_MAX) {
builtin_usage_error(shp, cmd, "%s: invalid -N value", optarg);
return 2;
}

flag = hist_max(hp) - n - 1;
if (flag < 0) flag = 1;
range[++indx] = flag;
}
done = true;
break;
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}
argv += optind;
argv += optget_ind;

// TODO: What is the usefulness of this flag ? Shall this be removed in future ?
// TODO: What is the usefulness of this flag? Shall this be removed in future?
if (pflag) {
hist_cancel(hp);
pflag = 0;
Expand Down
28 changes: 10 additions & 18 deletions src/cmd/ksh93/bltins/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@
***********************************************************************/
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>

#include "ast.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 = "+:lnp";
static const struct option long_options[] = {
{"help", no_argument, NULL, 1}, // all builtins support --help
static const char *short_options = "lnp";
static const struct optget_option long_options[] = {
{"help", optget_no_arg, NULL, 1}, // all builtins support --help
{NULL, 0, NULL, 0}};

//
Expand All @@ -48,11 +47,9 @@ int b_jobs(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
// integers that might otherwise look like a flag.
optind = opterr = 0;
optget_ind = 0;
bool done = false;
while (!done && (opt = getopt_long_only(argc, argv, short_options, long_options, NULL)) != -1) {
while (!done && (opt = optget_long(argc, argv, short_options, long_options)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
Expand All @@ -71,23 +68,18 @@ int b_jobs(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 '?': {
if (!strmatch(argv[optind - 1], "[+-]+([0-9])")) {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
return 2;
}
optind--;
done = true;
break;
builtin_unknown_option(shp, cmd, argv[optget_ind - 1]);
return 2;
}
default: { abort(); }
}
}

argv += optind;
argv += optget_ind;
if (!*argv) argv = NULL;
if (job_walk(shp, sfstdout, job_list, flag, argv)) {
errormsg(SH_DICT, ERROR_exit(1), e_no_job);
Expand Down
Loading

0 comments on commit 84648ca

Please sign in to comment.