Skip to content

Commit

Permalink
Switch b_whence() from optget() to getopt_long()
Browse files Browse the repository at this point in the history
  • Loading branch information
krader1961 committed Oct 3, 2019
1 parent 44d6381 commit c68faf0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 48 deletions.
47 changes: 29 additions & 18 deletions src/cmd/ksh93/bltins/whence.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
* David Korn <dgkorn@gmail.com> *
* *
***********************************************************************/
//
// whence [-afvp] name...
//
#include "config_ast.h" // IWYU pragma: keep

#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -31,32 +29,43 @@
#include "defs.h"
#include "error.h"
#include "name.h"
#include "option.h"
#include "path.h"
#include "sfio.h"
#include "shcmd.h"
#include "shtable.h"
#include "stk.h"

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

//
// The `whence` command.
// The `type` and `whence` commands.
//
int b_whence(int argc, char *argv[], Shbltin_t *context) {
int flags = 0, n;
int flags = 0, opt;
Shell_t *shp = context->shp;
UNUSED(argc);
char *cmd = argv[0];

if (*argv[0] == 't') flags = WHENCE_V_FLAG;
while ((n = optget(argv, sh_optwhence))) {
switch (n) { //!OCLINT(MissingDefaultStatement)

optind = opterr = 0;
while ((opt = getopt_long_only(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
return 0;
}
case 't': {
flags |= WHENCE_T_FLAG;
break;
}
case 'a': {
flags |= WHENCE_A_FLAG;
flags |= WHENCE_V_FLAG;
break;
}
// FALLTHRU
case 'v': {
flags |= WHENCE_V_FLAG;
break;
Expand All @@ -76,19 +85,21 @@ int b_whence(int argc, char *argv[], Shbltin_t *context) {
break;
}
case ':': {
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
builtin_missing_argument(shp, cmd, argv[optind - 1]);
return 2;
}
case '?': {
errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
__builtin_unreachable();
builtin_unknown_option(shp, cmd, argv[optind - 1]);
return 2;
}
default: { abort(); }
}
}
argv += opt_info.index;
if (error_info.errors || !*argv) {
errormsg(SH_DICT, ERROR_usage(2), optusage(NULL));
__builtin_unreachable();
argv += optind;

if (!*argv) {
builtin_usage_error(shp, cmd, "expected at least one arg");
return 2;
}
if (flags & WHENCE_T_FLAG) flags &= ~WHENCE_V_FLAG;
return whence(shp, argv, flags);
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
'David J. Korn, et. al.', '1'),
('wait', 'wait', 'wait for process or job completion',
'David J. Korn, et. al.', '1'),
('whence', 'whence', 'locate a command and describe its type',
'David J. Korn, et. al.', '1'),

# External commands available as builtins.
('basename', 'basename', 'strip directory and suffix from filenames',
Expand Down
1 change: 1 addition & 0 deletions src/cmd/ksh93/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Welcome to the Korn Shell
unalias
unset
wait
whence

.. toctree::
:maxdepth: 1
Expand Down
30 changes: 0 additions & 30 deletions src/cmd/ksh93/docs/whence.1

This file was deleted.

55 changes: 55 additions & 0 deletions src/cmd/ksh93/docs/whence.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.. default-role:: code

:index:`whence` -- locate a command and describe its type
=========================================================

Synopsis
--------
| whence [flags] name...
Description
-----------
Without `-v`, `whence` writes on standard output an absolute pathname,
if any, corresponding to *name* based on the complete search order that
the shell uses. If *name* is not found, then no output is produced.

If `-v` is specified, the output will also contain information that
indicates how the given *name* would be interpreted by the shell in
the current execution environment.

Flags
-----
:-p, -P: Do not check to see if *name* is a reserved word, a built-in,
an alias, or a function. This turns off the `-v` option.

:-a: Displays all uses for each *name* rather than the first.

:-f: Do not check for functions.

:-t: Output only the type for each command.

:-q: Quiet mode. Returns 0 if all arguments are built-ins, functions,
or are programs found on the path.

:-v: For each name you specify, the shell displays a line that indicates
if that name is one of the following:

* Reserved word
* Alias
* Built-in
* Undefined function
* Function
* Tracked alias
* Program

Exit Status
-----------
:0: Each *name* was found by the shell.

:1: One or more *name*\s were not found by the shell.

:>1: An error occurred.

See Also
--------
`command`\(1)

0 comments on commit c68faf0

Please sign in to comment.