2017-06-14 13:03:27 +08:00
|
|
|
// Implementation of the builtin builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2017-06-15 03:26:05 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
2017-06-14 13:03:27 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "builtin_builtin.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "io.h"
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
struct builtin_cmd_opts_t {
|
2017-06-14 13:03:27 +08:00
|
|
|
bool print_help = false;
|
|
|
|
bool list_names = false;
|
2019-02-09 19:34:00 +08:00
|
|
|
bool query = false;
|
2017-06-14 13:03:27 +08:00
|
|
|
};
|
2019-02-09 19:34:00 +08:00
|
|
|
static const wchar_t *const short_options = L":hnq";
|
2017-06-14 13:03:27 +08:00
|
|
|
static const struct woption long_options[] = {
|
2019-02-09 19:34:00 +08:00
|
|
|
{L"help", no_argument, NULL, 'h'}, {L"names", no_argument, NULL, 'n'},
|
|
|
|
{L"query", no_argument, NULL, 'q'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2017-06-14 13:03:27 +08:00
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
static int parse_cmd_opts(builtin_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv,
|
2017-06-14 13:03:27 +08:00
|
|
|
parser_t &parser, io_streams_t &streams) {
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'h': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.print_help = true;
|
2017-06-15 13:12:29 +08:00
|
|
|
break;
|
2017-06-14 13:03:27 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.list_names = true;
|
2017-06-14 13:03:27 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-02-09 19:34:00 +08:00
|
|
|
case 'q': {
|
|
|
|
opts.query = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-30 12:49:57 +08:00
|
|
|
case ':': {
|
2017-07-02 05:03:47 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-30 12:49:57 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-14 13:03:27 +08:00
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The builtin builtin, used for giving builtins precedence over functions. Mostly handled by the
|
|
|
|
/// parser. All this code does is some additional operational modes, such as printing a list of all
|
|
|
|
/// builtins, printing help, etc.
|
|
|
|
int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
const wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2017-06-16 08:57:37 +08:00
|
|
|
builtin_cmd_opts_t opts;
|
2017-06-14 13:03:27 +08:00
|
|
|
|
|
|
|
int optind;
|
2017-06-16 08:57:37 +08:00
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-14 13:03:27 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:34:00 +08:00
|
|
|
if (opts.query && opts.list_names) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO2, cmd,
|
|
|
|
_(L"--query and --names are mutually exclusive"));
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.query) {
|
|
|
|
wcstring_list_t names = builtin_get_names();
|
|
|
|
retval = STATUS_CMD_ERROR;
|
|
|
|
for (int i = optind; i < argc; i++) {
|
|
|
|
if (contains(names, argv[i])) {
|
|
|
|
retval = STATUS_CMD_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:03:27 +08:00
|
|
|
if (opts.list_names) {
|
|
|
|
wcstring_list_t names = builtin_get_names();
|
2017-06-15 03:26:05 +08:00
|
|
|
std::sort(names.begin(), names.end());
|
2017-06-14 13:03:27 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
|
|
|
const wchar_t *el = names.at(i).c_str();
|
|
|
|
|
|
|
|
streams.out.append(el);
|
|
|
|
streams.out.append(L"\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|