Skip to content

Commit

Permalink
Convert b_basename() to getopt_long()
Browse files Browse the repository at this point in the history
  • Loading branch information
krader1961 committed Sep 10, 2019
1 parent a08307c commit d0f92b4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
10 changes: 8 additions & 2 deletions src/cmd/ksh93/bltins/alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ int b_alias(int argc, char *argv[], Shbltin_t *context) {
tdata.argnum = 0;
tdata.aflag = *argv[1];

optind = 0;
optind = 1;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
return 0;
}
case 'p': {
tdata.prefix = argv[0];
tdata.prefix = cmd;
break;
}
case 't': {
Expand All @@ -85,6 +85,12 @@ int b_alias(int argc, char *argv[], Shbltin_t *context) {
}
}

// This would normally be `argv += optind`. 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
// separate flag for the value of the magic `*argv[0]` passed by `typeset` to that function.
argv += (optind - 1);
if (!nv_isflag(nvflags, NV_TAGGED)) return setall(argv, nvflags, troot, &tdata);

Expand Down
66 changes: 42 additions & 24 deletions src/cmd/ksh93/cmds/basename.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@
*/
#include "config_ast.h" // IWYU pragma: keep

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

#include "builtins.h"
#include "error.h"
#include "option.h"
#include "sfio.h"
#include "shcmd.h"

static const char *short_options = ":as:";
static const struct option long_options[] = {{"help", 0, NULL, 1}, // all builtins supports --help
{"all", 0, NULL, 'a'},
{"suffix", 0, NULL, 's'},
{NULL, 0, NULL, 0}};

static void namebase(Sfio_t *outfile, char *pathname, char *suffix) {
char *first, *last;
int n = 0;
for (first = last = pathname; *last; last++) {
;
}
/* back over trailing '/' */
if (last > first) {
if (last > first) { // back over trailing '/'
while (*--last == '/' && last > first) {
;
}
Expand All @@ -70,35 +76,47 @@ static void namebase(Sfio_t *outfile, char *pathname, char *suffix) {
}

int b_basename(int argc, char **argv, Shbltin_t *context) {
int opt;
char *string;
char *suffix = NULL;
int all = 0;
int n;
bool all = false;
Shell_t *shp = context->shp;
char *cmd = argv[0];

if (cmdinit(argc, argv, context, 0)) return -1;
while ((n = optget(argv, sh_optbasename))) {
switch (n) { //!OCLINT(MissingDefaultStatement)
case 'a':
all = 1;
break;
case 's':
all = 1;
suffix = opt_info.arg;
optind = 1;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
return 0;
}
case 'a': {
all = true;
break;
case ':':
error(2, "%s", opt_info.arg);
}
case 's': {
all = true;
suffix = optarg;
break;
case '?':
error(ERROR_usage(2), "%s", opt_info.arg);
__builtin_unreachable();
}
case ':': {
builtin_missing_argument(shp, cmd, argv[optind - 1]);
return 2;
}
case '?': {
builtin_unknown_option(shp, cmd, argv[optind - 1]);
return 2;
}
default: { abort(); }
}
}
argv += opt_info.index;
argc -= opt_info.index;
if (error_info.errors || argc < 1 || (!all && argc > 2)) {
error(ERROR_usage(2), "%s", optusage(NULL));
__builtin_unreachable();
argv += optind;
argc -= optind;
if (argc < 1 || (!all && argc > 2)) {
builtin_print_help(shp, cmd);
return 2;
}

if (!all) {
namebase(sfstdout, argv[0], argv[1]);
} else {
Expand Down
18 changes: 0 additions & 18 deletions src/cmd/ksh93/docs/basename.1

This file was deleted.

0 comments on commit d0f92b4

Please sign in to comment.