From 551bd39889bad257fa946eace5819e7b27ad5e03 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Tue, 13 Jun 2017 21:50:55 -0700 Subject: [PATCH] split builtin command into its own module --- Makefile.in | 1 + src/builtin.cpp | 61 +-------------------------- src/builtin_command.cpp | 93 +++++++++++++++++++++++++++++++++++++++++ src/builtin_command.h | 9 ++++ 4 files changed, 104 insertions(+), 60 deletions(-) create mode 100644 src/builtin_command.cpp create mode 100644 src/builtin_command.h diff --git a/Makefile.in b/Makefile.in index 401dd9150..27fd02737 100644 --- a/Makefile.in +++ b/Makefile.in @@ -103,6 +103,7 @@ FISH_OBJS := obj/autoload.o obj/builtin.o obj/builtin_bind.o obj/builtin_block.o obj/builtin_history.o obj/builtin_status.o obj/builtin_read.o \ obj/builtin_source.o obj/builtin_random.o obj/builtin_echo.o \ obj/builtin_cd.o obj/builtin_disown.o obj/builtin_function.o \ + obj/builtin_command.o \ obj/builtin_complete.o obj/builtin_jobs.o obj/builtin_printf.o \ obj/builtin_set.o obj/builtin_set_color.o obj/builtin_string.o \ obj/builtin_test.o obj/builtin_ulimit.o obj/color.o obj/common.o \ diff --git a/src/builtin.cpp b/src/builtin.cpp index ed64961f6..884257423 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -32,6 +32,7 @@ #include "builtin_bind.h" #include "builtin_block.h" #include "builtin_cd.h" +#include "builtin_command.h" #include "builtin_commandline.h" #include "builtin_complete.h" #include "builtin_disown.h" @@ -60,7 +61,6 @@ #include "parse_constants.h" #include "parse_util.h" #include "parser.h" -#include "path.h" #include "proc.h" #include "reader.h" #include "tokenizer.h" @@ -286,65 +286,6 @@ static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **ar return STATUS_CMD_OK; } -/// Implementation of the builtin 'command'. Actual command running is handled by the parser, this -/// just processes the flags. -static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - int argc = builtin_count_args(argv); - bool find_path = false; - bool quiet = false; - - static const wchar_t *short_options = L"hqsv"; - static const struct woption long_options[] = {{L"quiet", no_argument, NULL, 'q'}, - {L"search", no_argument, NULL, 's'}, - {L"help", no_argument, NULL, 'h'}, - {NULL, 0, NULL, 0}}; - - int opt; - wgetopter_t w; - while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) { - switch (opt) { - case 'h': { - builtin_print_help(parser, streams, argv[0], streams.out); - return STATUS_CMD_OK; - } - case 's': - case 'v': { - find_path = true; - break; - } - case 'q': { - quiet = true; - break; - } - case '?': { - builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); - return STATUS_INVALID_ARGS; - } - default: { - DIE("unexpected retval from wgetopt_long"); - break; - } - } - } - - if (!find_path) { - builtin_print_help(parser, streams, argv[0], streams.out); - return STATUS_INVALID_ARGS; - } - - int found = 0; - - for (int idx = w.woptind; argv[idx]; ++idx) { - const wchar_t *command_name = argv[idx]; - wcstring path; - if (path_get_path(command_name, &path)) { - if (!quiet) streams.out.append_format(L"%ls\n", path.c_str()); - ++found; - } - } - return found ? STATUS_CMD_OK : STATUS_CMD_ERROR; -} - /// A generic bultin that only supports showing a help message. This is only a placeholder that /// prints the help message. Useful for commands that live in the parser. static int builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **argv) { diff --git a/src/builtin_command.cpp b/src/builtin_command.cpp new file mode 100644 index 000000000..d6a97d55b --- /dev/null +++ b/src/builtin_command.cpp @@ -0,0 +1,93 @@ +// Implementation of the command builtin. +#include "config.h" // IWYU pragma: keep + +#include + +#include "builtin.h" +#include "builtin_command.h" +#include "common.h" +#include "fallback.h" // IWYU pragma: keep +#include "io.h" +#include "path.h" +#include "wgetopt.h" +#include "wutil.h" // IWYU pragma: keep + +struct cmd_opts { + bool print_help = false; + bool find_path = false; + bool quiet = false; +}; +static const wchar_t *short_options = L"hqsv"; +static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'}, + {L"quiet", no_argument, NULL, 'q'}, + {L"search", no_argument, NULL, 's'}, + {NULL, 0, NULL, 0}}; + +static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t **argv, + 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': { + opts->print_help = true; + return STATUS_CMD_OK; + } + case 'q': { + opts->quiet = true; + break; + } + case 's': // -s and -v are aliases + case 'v': { + opts->find_path = true; + break; + } + 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 'command'. Actual command running is handled by the parser, this +/// just processes the flags. +int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) { + const wchar_t *cmd = argv[0]; + int argc = builtin_count_args(argv); + struct cmd_opts opts; + + int optind; + int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams); + if (retval != STATUS_CMD_OK) return retval; + + if (opts.print_help) { + builtin_print_help(parser, streams, cmd, streams.out); + return STATUS_CMD_OK; + } + + if (!opts.find_path) { + builtin_print_help(parser, streams, cmd, streams.out); + return STATUS_INVALID_ARGS; + } + + int found = 0; + for (int idx = optind; argv[idx]; ++idx) { + const wchar_t *command_name = argv[idx]; + wcstring path; + if (path_get_path(command_name, &path)) { + if (!opts.quiet) streams.out.append_format(L"%ls\n", path.c_str()); + ++found; + } + } + + return found ? STATUS_CMD_OK : STATUS_CMD_ERROR; +} diff --git a/src/builtin_command.h b/src/builtin_command.h new file mode 100644 index 000000000..75f5fc1e6 --- /dev/null +++ b/src/builtin_command.h @@ -0,0 +1,9 @@ +// Prototypes for executing builtin_command function. +#ifndef FISH_BUILTIN_COMMAND_H +#define FISH_BUILTIN_COMMAND_H + +class parser_t; +struct io_streams_t; + +int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv); +#endif