2017-06-14 13:18:56 +08:00
|
|
|
// Implementation of the contains builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2019-03-13 05:06:01 +08:00
|
|
|
#include <cwchar>
|
2017-06-14 13:18:56 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "builtin_contains.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 contains_cmd_opts_t {
|
2017-06-14 13:18:56 +08:00
|
|
|
bool print_help = false;
|
|
|
|
bool print_index = false;
|
|
|
|
};
|
2018-09-29 11:45:56 +08:00
|
|
|
static const wchar_t *const short_options = L"+:hi";
|
2017-06-14 13:18:56 +08:00
|
|
|
static const struct woption long_options[] = {
|
|
|
|
{L"help", no_argument, NULL, 'h'}, {L"index", no_argument, NULL, 'i'}, {NULL, 0, NULL, 0}};
|
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
static int parse_cmd_opts(contains_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv,
|
2017-06-14 13:18:56 +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:18:56 +08:00
|
|
|
}
|
|
|
|
case 'i': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.print_index = true;
|
2017-06-14 13:18:56 +08:00
|
|
|
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:18:56 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of the builtin contains command, used to check if a specified string is part of
|
|
|
|
/// a list.
|
|
|
|
int builtin_contains(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
|
|
|
contains_cmd_opts_t opts;
|
2017-06-14 13:18:56 +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:18:56 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
2017-06-15 03:26:05 +08:00
|
|
|
return STATUS_CMD_OK;
|
2017-06-14 13:18:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *needle = argv[optind];
|
|
|
|
if (!needle) {
|
|
|
|
streams.err.append_format(_(L"%ls: Key not specified\n"), cmd);
|
|
|
|
} else {
|
|
|
|
for (int i = optind + 1; i < argc; i++) {
|
2019-03-13 05:06:01 +08:00
|
|
|
if (!std::wcscmp(needle, argv[i])) {
|
2017-06-14 13:18:56 +08:00
|
|
|
if (opts.print_index) streams.out.append_format(L"%d\n", i - optind);
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|