From ffdabace5e8b08a1b47f67b31dada3c0b3355850 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Wed, 14 Jun 2017 22:12:29 -0700 Subject: [PATCH] more builtin style cleanup --- Makefile.in | 111 ++++++++++++++++++++++---------------- src/builtin_bind.cpp | 2 +- src/builtin_block.cpp | 2 +- src/builtin_builtin.cpp | 2 +- src/builtin_cd.cpp | 39 +------------- src/builtin_command.cpp | 2 +- src/builtin_contains.cpp | 2 +- src/builtin_disown.cpp | 40 +------------- src/builtin_emit.cpp | 40 +------------- src/builtin_exit.cpp | 2 +- src/builtin_function.cpp | 2 +- src/builtin_functions.cpp | 17 +++--- src/builtin_history.cpp | 2 +- src/builtin_pwd.cpp | 42 +-------------- src/builtin_random.cpp | 40 +------------- src/builtin_read.cpp | 2 +- src/builtin_return.cpp | 2 +- src/builtin_source.cpp | 38 +------------ src/builtin_status.cpp | 2 +- 19 files changed, 97 insertions(+), 292 deletions(-) diff --git a/Makefile.in b/Makefile.in index 63c94993e..7166c1887 100644 --- a/Makefile.in +++ b/Makefile.in @@ -98,33 +98,27 @@ HAVE_DOXYGEN=@HAVE_DOXYGEN@ # # All objects that the system needs to build fish, except fish.o # -FISH_OBJS := obj/autoload.o obj/builtin.o obj/builtin_bind.o obj/builtin_block.o \ - obj/builtin_commandline.o obj/builtin_emit.o obj/builtin_functions.o \ - obj/builtin_bg.o obj/builtin_fg.o obj/builtin_return.o obj/builtin_exit.o \ - obj/builtin_realpath.o \ - obj/builtin_history.o obj/builtin_status.o obj/builtin_read.o obj/builtin_pwd.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_builtin.o obj/builtin_command.o obj/builtin_contains.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 \ - obj/complete.o obj/env.o obj/env_universal_common.o obj/event.o \ - obj/exec.o obj/expand.o obj/fallback.o obj/fish_version.o \ - obj/function.o obj/highlight.o obj/history.o obj/input.o \ - obj/input_common.o obj/intern.o obj/io.o obj/iothread.o obj/kill.o \ - obj/output.o obj/pager.o obj/parse_execution.o \ - obj/parse_productions.o obj/parse_tree.o obj/parse_util.o \ - obj/parser.o obj/parser_keywords.o obj/path.o obj/postfork.o \ - obj/proc.o obj/reader.o obj/sanity.o obj/screen.o obj/signal.o \ - obj/tokenizer.o obj/utf8.o obj/util.o obj/wcstringutil.o \ - obj/wgetopt.o obj/wildcard.o obj/wutil.o +FISH_OBJS := obj/autoload.o obj/builtin.o obj/builtin_bg.o obj/builtin_bind.o obj/builtin_block.o \ + obj/builtin_builtin.o obj/builtin_cd.o obj/builtin_command.o obj/builtin_commandline.o \ + obj/builtin_complete.o obj/builtin_contains.o obj/builtin_disown.o obj/builtin_echo.o \ + obj/builtin_emit.o obj/builtin_exit.o obj/builtin_fg.o obj/builtin_function.o \ + obj/builtin_functions.o obj/builtin_history.o obj/builtin_jobs.o obj/builtin_printf.o \ + obj/builtin_pwd.o obj/builtin_random.o obj/builtin_read.o obj/builtin_realpath.o \ + obj/builtin_return.o obj/builtin_set.o obj/builtin_set_color.o obj/builtin_source.o \ + obj/builtin_status.o obj/builtin_string.o obj/builtin_test.o obj/builtin_ulimit.o \ + obj/color.o obj/common.o obj/complete.o obj/env.o obj/env_universal_common.o obj/event.o \ + obj/exec.o obj/expand.o obj/fallback.o obj/fish_version.o obj/function.o obj/highlight.o \ + obj/history.o obj/input.o obj/input_common.o obj/intern.o obj/io.o obj/iothread.o \ + obj/kill.o obj/output.o obj/pager.o obj/parse_execution.o obj/parse_productions.o \ + obj/parse_tree.o obj/parse_util.o obj/parser.o obj/parser_keywords.o obj/path.o \ + obj/postfork.o obj/proc.o obj/reader.o obj/sanity.o obj/screen.o obj/signal.o \ + obj/tokenizer.o obj/utf8.o obj/util.o obj/wcstringutil.o obj/wgetopt.o obj/wildcard.o \ + obj/wutil.o FISH_INDENT_OBJS := obj/fish_indent.o obj/print_help.o $(FISH_OBJS) FISH_KEYREAD_OBJS := obj/fish_key_reader.o obj/print_help.o $(FISH_OBJS) - # # All objects that the system needs to build fish_tests # @@ -455,7 +449,7 @@ lexicon.txt: doc_src/commands.hdr $(FUNCTIONS_DIR_FILES) $(COMPLETIONS_DIR_FILES -e "s|'\(.*\)'|func \1|p"; $v $(SED) >lexicon.tmp -n \ -e '/set_default/s/.*\(fish_[a-z][a-z_]*\).*$$/clrv \1/p'; \ - $(SED) >lexicon.tmp -n \ + $(SED) >lexicon.tmp -n \ -e '/^#.!#/s/^#.!# \(.... [a-z][a-z_]*\)/\1/p'; $v mv lexicon.tmp lexicon.txt; rm -f lexicon_catalog.tmp lexicon_catalog.txt; @@ -896,7 +890,7 @@ style-all: # distclean: clean -$v test ! -f $(PCRE2_DIR)/Makefile || \ - $(MAKE) V=$(V) -C $(PCRE2_DIR) distclean + $(MAKE) V=$(V) -C $(PCRE2_DIR) distclean $v rm -rf config.status config.log config.h Makefile autom4te.cache aclocal.m4 .PHONY: distclean @@ -914,11 +908,11 @@ clean: # send all output to /dev/null - unless there's an error, in which case run it again not silenced. ifeq ($(V), 0 ) -@test ! -f $(PCRE2_DIR)/Makefile || \ - $(MAKE) -C $(PCRE2_DIR) clean + $(MAKE) -C $(PCRE2_DIR) clean else -@test ! -f $(PCRE2_DIR)/Makefile || \ - $(MAKE) -s -C $(PCRE2_DIR) clean > /dev/null || \ - $(MAKE) -s -C $(PCRE2_DIR) clean + $(MAKE) -s -C $(PCRE2_DIR) clean > /dev/null || \ + $(MAKE) -s -C $(PCRE2_DIR) clean endif $v rm -f obj/*.o *.o doc.h doc.tmp $v rm -f doc_src/*.doxygen doc_src/*.cpp doc_src/*.o doc_src/commands.hdr @@ -951,31 +945,37 @@ v = $(V$(V)) obj/autoload.o: config.h src/autoload.h src/common.h src/fallback.h obj/autoload.o: src/signal.h src/lru.h src/env.h src/exec.h src/wutil.h obj/builtin.o: config.h src/builtin.h src/common.h src/fallback.h -obj/builtin.o: src/signal.h src/builtin_bind.h src/builtin_block.h -obj/builtin.o: src/builtin_builtin.h src/builtin_cd.h src/builtin_command.h -obj/builtin.o: src/builtin_commandline.h src/builtin_complete.h -obj/builtin.o: src/builtin_contains.h src/builtin_disown.h src/builtin_echo.h -obj/builtin.o: src/builtin_emit.h src/builtin_functions.h +obj/builtin.o: src/signal.h src/builtin_bg.h src/builtin_bind.h +obj/builtin.o: src/builtin_block.h src/builtin_builtin.h src/builtin_cd.h +obj/builtin.o: src/builtin_command.h src/builtin_commandline.h +obj/builtin.o: src/builtin_complete.h src/builtin_contains.h +obj/builtin.o: src/builtin_disown.h src/builtin_echo.h src/builtin_emit.h +obj/builtin.o: src/builtin_exit.h src/builtin_fg.h src/builtin_functions.h obj/builtin.o: src/builtin_history.h src/builtin_jobs.h src/builtin_printf.h -obj/builtin.o: src/builtin_random.h src/builtin_read.h src/builtin_set.h +obj/builtin.o: src/builtin_pwd.h src/builtin_random.h src/builtin_read.h +obj/builtin.o: src/builtin_realpath.h src/builtin_return.h src/builtin_set.h obj/builtin.o: src/builtin_set_color.h src/builtin_source.h obj/builtin.o: src/builtin_status.h src/builtin_string.h src/builtin_test.h -obj/builtin.o: src/builtin_ulimit.h src/complete.h src/env.h src/exec.h -obj/builtin.o: src/intern.h src/io.h src/parse_constants.h src/parse_util.h +obj/builtin.o: src/builtin_ulimit.h src/complete.h src/exec.h src/intern.h +obj/builtin.o: src/io.h src/parse_constants.h src/parse_util.h obj/builtin.o: src/tokenizer.h src/parser.h src/event.h src/expand.h obj/builtin.o: src/parse_tree.h src/proc.h src/reader.h src/highlight.h -obj/builtin.o: src/color.h src/wgetopt.h src/wutil.h +obj/builtin.o: src/color.h src/env.h src/wgetopt.h src/wutil.h +obj/builtin_bg.o: config.h src/builtin.h src/common.h src/fallback.h +obj/builtin_bg.o: src/signal.h src/builtin_bg.h src/io.h src/proc.h +obj/builtin_bg.o: src/parse_tree.h src/parse_constants.h src/tokenizer.h +obj/builtin_bg.o: src/wutil.h obj/builtin_bind.o: config.h src/builtin.h src/common.h src/fallback.h -obj/builtin_bind.o: src/signal.h src/builtin_bind.h src/input.h src/env.h -obj/builtin_bind.o: src/io.h src/wgetopt.h src/wutil.h +obj/builtin_bind.o: src/signal.h src/builtin_bind.h src/input.h src/io.h +obj/builtin_bind.o: src/wgetopt.h src/wutil.h obj/builtin_block.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_block.o: src/signal.h src/builtin_block.h src/event.h src/io.h obj/builtin_block.o: src/parser.h src/expand.h src/parse_constants.h obj/builtin_block.o: src/parse_tree.h src/tokenizer.h src/proc.h obj/builtin_block.o: src/wgetopt.h src/wutil.h obj/builtin_builtin.o: config.h src/builtin.h src/common.h src/fallback.h -obj/builtin_builtin.o: src/signal.h src/builtin_builtin.h src/io.h src/path.h -obj/builtin_builtin.o: src/env.h src/wgetopt.h src/wutil.h +obj/builtin_builtin.o: src/signal.h src/builtin_builtin.h src/io.h +obj/builtin_builtin.o: src/wgetopt.h src/wutil.h obj/builtin_cd.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_cd.o: src/signal.h src/builtin_cd.h src/env.h src/io.h obj/builtin_cd.o: src/parser.h src/event.h src/expand.h src/parse_constants.h @@ -986,11 +986,11 @@ obj/builtin_command.o: src/signal.h src/builtin_command.h src/io.h src/path.h obj/builtin_command.o: src/env.h src/wgetopt.h src/wutil.h obj/builtin_commandline.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_commandline.o: src/signal.h src/input.h src/builtin_bind.h -obj/builtin_commandline.o: src/env.h src/io.h src/parse_util.h -obj/builtin_commandline.o: src/parse_constants.h src/tokenizer.h src/proc.h -obj/builtin_commandline.o: src/parse_tree.h src/reader.h src/complete.h -obj/builtin_commandline.o: src/highlight.h src/color.h src/util.h -obj/builtin_commandline.o: src/wgetopt.h src/wutil.h +obj/builtin_commandline.o: src/io.h src/parse_util.h src/parse_constants.h +obj/builtin_commandline.o: src/tokenizer.h src/proc.h src/parse_tree.h +obj/builtin_commandline.o: src/reader.h src/complete.h src/highlight.h +obj/builtin_commandline.o: src/color.h src/env.h src/util.h src/wgetopt.h +obj/builtin_commandline.o: src/wutil.h obj/builtin_complete.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_complete.o: src/signal.h src/complete.h src/env.h src/io.h obj/builtin_complete.o: src/parse_constants.h src/parse_util.h @@ -1011,6 +1011,16 @@ obj/builtin_echo.o: src/wutil.h obj/builtin_emit.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_emit.o: src/signal.h src/builtin_emit.h src/event.h src/io.h obj/builtin_emit.o: src/wgetopt.h src/wutil.h +obj/builtin_exit.o: config.h src/builtin.h src/common.h src/fallback.h +obj/builtin_exit.o: src/signal.h src/builtin_exit.h src/io.h src/proc.h +obj/builtin_exit.o: src/parse_tree.h src/parse_constants.h src/tokenizer.h +obj/builtin_exit.o: src/reader.h src/complete.h src/highlight.h src/color.h +obj/builtin_exit.o: src/env.h src/wgetopt.h src/wutil.h +obj/builtin_fg.o: config.h src/builtin.h src/common.h src/fallback.h +obj/builtin_fg.o: src/signal.h src/builtin_fg.h src/env.h src/io.h src/proc.h +obj/builtin_fg.o: src/parse_tree.h src/parse_constants.h src/tokenizer.h +obj/builtin_fg.o: src/reader.h src/complete.h src/highlight.h src/color.h +obj/builtin_fg.o: src/wutil.h obj/builtin_function.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_function.o: src/signal.h src/builtin_function.h src/complete.h obj/builtin_function.o: src/event.h src/function.h src/env.h src/io.h @@ -1034,6 +1044,9 @@ obj/builtin_jobs.o: src/parse_constants.h src/tokenizer.h src/wgetopt.h obj/builtin_jobs.o: src/wutil.h obj/builtin_printf.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_printf.o: src/signal.h src/io.h src/wutil.h +obj/builtin_pwd.o: config.h src/builtin.h src/common.h src/fallback.h +obj/builtin_pwd.o: src/signal.h src/builtin_pwd.h src/event.h src/io.h +obj/builtin_pwd.o: src/wgetopt.h src/wutil.h obj/builtin_random.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_random.o: src/signal.h src/builtin_random.h src/io.h obj/builtin_random.o: src/wgetopt.h src/wutil.h @@ -1043,6 +1056,14 @@ obj/builtin_read.o: src/event.h src/expand.h src/parse_constants.h obj/builtin_read.o: src/highlight.h src/color.h src/io.h src/proc.h obj/builtin_read.o: src/parse_tree.h src/tokenizer.h src/reader.h obj/builtin_read.o: src/wcstringutil.h src/wgetopt.h src/wutil.h +obj/builtin_realpath.o: config.h src/builtin.h src/common.h src/fallback.h +obj/builtin_realpath.o: src/signal.h src/builtin_realpath.h src/io.h +obj/builtin_realpath.o: src/wutil.h +obj/builtin_return.o: config.h src/builtin.h src/common.h src/fallback.h +obj/builtin_return.o: src/signal.h src/builtin_return.h src/io.h src/parser.h +obj/builtin_return.o: src/event.h src/expand.h src/parse_constants.h +obj/builtin_return.o: src/parse_tree.h src/tokenizer.h src/proc.h +obj/builtin_return.o: src/wgetopt.h src/wutil.h obj/builtin_set.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_set.o: src/signal.h src/env.h src/expand.h src/parse_constants.h obj/builtin_set.o: src/io.h src/proc.h src/parse_tree.h src/tokenizer.h diff --git a/src/builtin_bind.cpp b/src/builtin_bind.cpp index 9bbbf0d5d..0a0c50c29 100644 --- a/src/builtin_bind.cpp +++ b/src/builtin_bind.cpp @@ -296,7 +296,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc } case L'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case L'k': { opts->use_terminfo = true; diff --git a/src/builtin_block.cpp b/src/builtin_block.cpp index 9abbe3445..9d929d7d9 100644 --- a/src/builtin_block.cpp +++ b/src/builtin_block.cpp @@ -36,7 +36,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc switch (opt) { case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case 'g': { opts->scope = GLOBAL; diff --git a/src/builtin_builtin.cpp b/src/builtin_builtin.cpp index f4b0aec73..a42a323f3 100644 --- a/src/builtin_builtin.cpp +++ b/src/builtin_builtin.cpp @@ -31,7 +31,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t switch (opt) { case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case 'n': { opts->list_names = true; diff --git a/src/builtin_cd.cpp b/src/builtin_cd.cpp index 854c04e6b..927b7a7d3 100644 --- a/src/builtin_cd.cpp +++ b/src/builtin_cd.cpp @@ -3,7 +3,6 @@ #include #include -#include #include "builtin.h" #include "builtin_cd.h" @@ -14,42 +13,8 @@ #include "parser.h" #include "path.h" #include "proc.h" -#include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep -struct cmd_opts { - bool print_help = false; -}; -static const wchar_t *short_options = L"h"; -static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'}, - {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) { //!OCLINT(too few branches) - case 'h': { - opts->print_help = true; - return STATUS_CMD_OK; - } - 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 cd builtin. Changes the current directory to the one specified or to $HOME if none is /// specified. The directory can be relative to any directory in the CDPATH variable. /// The cd builtin. Changes the current directory to the one specified or to $HOME if none is @@ -57,9 +22,9 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t int builtin_cd(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; + struct cmd_opts_help_only opts; int optind; - int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams); + int retval = parse_cmd_opts_help_only(&opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { diff --git a/src/builtin_command.cpp b/src/builtin_command.cpp index d6a97d55b..9c2426928 100644 --- a/src/builtin_command.cpp +++ b/src/builtin_command.cpp @@ -32,7 +32,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t switch (opt) { case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case 'q': { opts->quiet = true; diff --git a/src/builtin_contains.cpp b/src/builtin_contains.cpp index 0766e18a1..100e00822 100644 --- a/src/builtin_contains.cpp +++ b/src/builtin_contains.cpp @@ -29,7 +29,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, int argc, wchar_t switch (opt) { case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case 'i': { opts->print_index = true; diff --git a/src/builtin_disown.cpp b/src/builtin_disown.cpp index 79cf8c53b..1a0c608d7 100644 --- a/src/builtin_disown.cpp +++ b/src/builtin_disown.cpp @@ -3,7 +3,6 @@ #include #include -#include #include @@ -14,43 +13,8 @@ #include "io.h" #include "parser.h" #include "proc.h" -#include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep -struct cmd_opts { - bool print_help = false; -}; - -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]; - static const wchar_t *short_options = L"h"; - static const struct woption long_options[] = {{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) { //!OCLINT(too few branches) - case 'h': { - opts->print_help = true; - return STATUS_CMD_OK; - } - 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; -} - /// Helper for builtin_disown. static int disown_job(const wchar_t *cmd, parser_t &parser, io_streams_t &streams, job_t *j) { if (j == 0) { @@ -75,10 +39,10 @@ static int disown_job(const wchar_t *cmd, parser_t &parser, io_streams_t &stream int builtin_disown(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; + struct cmd_opts_help_only opts; int optind; - int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams); + int retval = parse_cmd_opts_help_only(&opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { diff --git a/src/builtin_emit.cpp b/src/builtin_emit.cpp index f0c9da005..1c861123e 100644 --- a/src/builtin_emit.cpp +++ b/src/builtin_emit.cpp @@ -1,58 +1,22 @@ // Implementation of the emit builtin. #include "config.h" // IWYU pragma: keep -#include - #include "builtin.h" #include "builtin_emit.h" #include "common.h" #include "event.h" #include "fallback.h" // IWYU pragma: keep #include "io.h" -#include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep -struct cmd_opts { - bool print_help = false; -}; -static const wchar_t *short_options = L"h"; -static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'}, - {NULL, 0, NULL, 0}}; - -static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high ncss method) - 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) { //!OCLINT(too few branches) - case 'h': { - opts->print_help = true; - return STATUS_CMD_OK; - } - 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 emit command, used to create events. int builtin_emit(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; + struct cmd_opts_help_only opts; int optind; - int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams); + int retval = parse_cmd_opts_help_only(&opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { diff --git a/src/builtin_exit.cpp b/src/builtin_exit.cpp index b3c22a944..cfdae708b 100644 --- a/src/builtin_exit.cpp +++ b/src/builtin_exit.cpp @@ -32,7 +32,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc switch (opt) { //!OCLINT(too few branches) case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case '?': { // We would normally invoke builtin_unknown_option() and return an error. diff --git a/src/builtin_function.cpp b/src/builtin_function.cpp index a1438417e..e48cfee00 100644 --- a/src/builtin_function.cpp +++ b/src/builtin_function.cpp @@ -152,7 +152,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc } case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case ':': { streams.err.append_format(BUILTIN_ERR_MISSING, cmd, argv[w.woptind - 1]); diff --git a/src/builtin_functions.cpp b/src/builtin_functions.cpp index bde019d6f..888b2213d 100644 --- a/src/builtin_functions.cpp +++ b/src/builtin_functions.cpp @@ -37,19 +37,18 @@ struct cmd_opts { bool verbose = false; wchar_t *description = NULL; }; +static const wchar_t *short_options = L"Dacehnqv"; +static const struct woption long_options[] = { + {L"erase", no_argument, NULL, 'e'}, {L"description", required_argument, NULL, 'd'}, + {L"names", no_argument, NULL, 'n'}, {L"all", no_argument, NULL, 'a'}, + {L"help", no_argument, NULL, 'h'}, {L"query", no_argument, NULL, 'q'}, + {L"copy", no_argument, NULL, 'c'}, {L"details", no_argument, NULL, 'D'}, + {L"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0}}; static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high ncss method) int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) { wchar_t *cmd = argv[0]; - static const wchar_t *short_options = L"Dacehnqv"; - static const struct woption long_options[] = { - {L"erase", no_argument, NULL, 'e'}, {L"description", required_argument, NULL, 'd'}, - {L"names", no_argument, NULL, 'n'}, {L"all", no_argument, NULL, 'a'}, - {L"help", no_argument, NULL, 'h'}, {L"query", no_argument, NULL, 'q'}, - {L"copy", no_argument, NULL, 'c'}, {L"details", no_argument, NULL, 'D'}, - {L"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0}}; - int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) { @@ -80,7 +79,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, } case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case 'q': { opts->query = true; diff --git a/src/builtin_history.cpp b/src/builtin_history.cpp index 7a28409a7..31c4795d2 100644 --- a/src/builtin_history.cpp +++ b/src/builtin_history.cpp @@ -167,7 +167,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc } case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case ':': { streams.err.append_format(BUILTIN_ERR_MISSING, cmd, argv[w.woptind - 1]); diff --git a/src/builtin_pwd.cpp b/src/builtin_pwd.cpp index a0ba64d40..c700ab6a0 100644 --- a/src/builtin_pwd.cpp +++ b/src/builtin_pwd.cpp @@ -1,60 +1,22 @@ // Implementation of the pwd builtin. #include "config.h" // IWYU pragma: keep -#include - #include "builtin.h" #include "builtin_pwd.h" #include "common.h" -#include "event.h" #include "fallback.h" // IWYU pragma: keep #include "io.h" -#include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep -struct cmd_opts { - bool print_help = false; -}; -static const wchar_t *short_options = L"h"; -static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'}, - {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) { //!OCLINT(too few branches) - case 'h': { - opts->print_help = true; - return STATUS_CMD_OK; - } - 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 pwd builtin. We don't respect -P to resolve symbolic links because we /// try to always resolve them. int builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv) { UNUSED(parser); const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); - struct cmd_opts opts; - + struct cmd_opts_help_only opts; int optind; - int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams); + int retval = parse_cmd_opts_help_only(&opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { diff --git a/src/builtin_random.cpp b/src/builtin_random.cpp index 6ada8bf9c..3270ab38b 100644 --- a/src/builtin_random.cpp +++ b/src/builtin_random.cpp @@ -2,7 +2,6 @@ #include "config.h" // IWYU pragma: keep #include -#include #include #include @@ -14,51 +13,16 @@ #include "common.h" #include "fallback.h" // IWYU pragma: keep #include "io.h" -#include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep -struct cmd_opts { - bool print_help = false; -}; - -static const wchar_t *short_options = L"h"; -static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'}, - {NULL, 0, NULL, 0}}; - -static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high ncss method) - 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) { //!OCLINT(too few branches) - case 'h': { - opts->print_help = true; - return STATUS_CMD_OK; - } - 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 random builtin generates random numbers. int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **argv) { wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); - struct cmd_opts opts; + struct cmd_opts_help_only opts; int optind; - int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams); + int retval = parse_cmd_opts_help_only(&opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { diff --git a/src/builtin_read.cpp b/src/builtin_read.cpp index 660a7d351..1dac3abd7 100644 --- a/src/builtin_read.cpp +++ b/src/builtin_read.cpp @@ -146,7 +146,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc } case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case ':': { streams.err.append_format(BUILTIN_ERR_MISSING, cmd, argv[w.woptind - 1]); diff --git a/src/builtin_return.cpp b/src/builtin_return.cpp index 5e36d0ae3..bb5e385dd 100644 --- a/src/builtin_return.cpp +++ b/src/builtin_return.cpp @@ -32,7 +32,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc switch (opt) { //!OCLINT(too few branches) case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case '?': { // We would normally invoke builtin_unknown_option() and return an error. diff --git a/src/builtin_source.cpp b/src/builtin_source.cpp index 412f09a44..ce2e51e7f 100644 --- a/src/builtin_source.cpp +++ b/src/builtin_source.cpp @@ -16,51 +16,17 @@ #include "parser.h" #include "proc.h" #include "reader.h" -#include "wgetopt.h" #include "wutil.h" // IWYU pragma: keep -struct cmd_opts { - bool print_help = false; -}; -static const wchar_t *short_options = L"h"; -static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'}, - {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) { //!OCLINT(too few branches) - case 'h': { - opts->print_help = true; - return STATUS_CMD_OK; - } - case '?': { - builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); - return STATUS_INVALID_ARGS; - } - default: { - DIE("unexpected retval from wgetopt_long"); - break; - } - } - } - - *optind = w.woptind; - return STATUS_CMD_OK; -} - /// The source builtin, sometimes called `.`. Evaluates the contents of a file in the current /// context. int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) { ASSERT_IS_MAIN_THREAD(); const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); - struct cmd_opts opts; + struct cmd_opts_help_only opts; int optind; - int retval = parse_cmd_opts(&opts, &optind, argc, argv, parser, streams); + int retval = parse_cmd_opts_help_only(&opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { diff --git a/src/builtin_status.cpp b/src/builtin_status.cpp index e39cecc7b..a59f8dcfc 100644 --- a/src/builtin_status.cpp +++ b/src/builtin_status.cpp @@ -193,7 +193,7 @@ static int parse_cmd_opts(struct cmd_opts *opts, int *optind, //!OCLINT(high nc } case 'h': { opts->print_help = true; - return STATUS_CMD_OK; + break; } case ':': { builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);