From 1b9824ae4647f07559f5bc800870309db3aac6fb Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Mon, 12 Jun 2017 18:40:58 -0700 Subject: [PATCH] split builtin emit into its own module --- Makefile.in | 30 ++++++++++++------ src/builtin.cpp | 38 +---------------------- src/builtin_emit.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++ src/builtin_emit.h | 9 ++++++ 4 files changed, 103 insertions(+), 47 deletions(-) create mode 100644 src/builtin_emit.cpp create mode 100644 src/builtin_emit.h diff --git a/Makefile.in b/Makefile.in index 2c565d28c..3ddd9865e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -99,7 +99,7 @@ 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_commandline.o obj/builtin_emit.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 \ @@ -945,20 +945,25 @@ 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/signal.h src/builtin.h src/common.h -obj/builtin.o: src/fallback.h src/builtin_bind.h src/builtin_commandline.h -obj/builtin.o: src/builtin_complete.h src/builtin_jobs.h src/builtin_printf.h +obj/builtin.o: src/fallback.h src/builtin_bind.h src/builtin_block.h +obj/builtin.o: src/builtin_commandline.h src/builtin_complete.h +obj/builtin.o: src/builtin_emit.h src/builtin_jobs.h src/builtin_printf.h obj/builtin.o: src/builtin_set.h src/builtin_set_color.h src/builtin_string.h obj/builtin.o: src/builtin_test.h src/builtin_ulimit.h src/complete.h obj/builtin.o: src/env.h src/event.h src/exec.h src/expand.h obj/builtin.o: src/parse_constants.h src/function.h src/highlight.h -obj/builtin.o: src/color.h src/history.h src/wutil.h src/input.h src/intern.h -obj/builtin.o: src/io.h src/parse_util.h src/tokenizer.h src/parser.h -obj/builtin.o: src/parse_tree.h src/proc.h src/parser_keywords.h src/path.h -obj/builtin.o: src/reader.h src/wcstringutil.h src/wgetopt.h +obj/builtin.o: src/color.h src/history.h src/wutil.h src/intern.h src/io.h +obj/builtin.o: src/parse_util.h src/tokenizer.h src/parser.h src/parse_tree.h +obj/builtin.o: src/proc.h src/parser_keywords.h src/path.h src/reader.h +obj/builtin.o: src/wcstringutil.h src/wgetopt.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/env.h src/input.h -obj/builtin_bind.o: src/io.h src/tokenizer.h src/wcstringutil.h src/wgetopt.h -obj/builtin_bind.o: src/wutil.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_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_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 @@ -972,6 +977,11 @@ obj/builtin_complete.o: src/parse_constants.h src/parse_util.h obj/builtin_complete.o: src/tokenizer.h src/parser.h src/event.h src/expand.h obj/builtin_complete.o: src/parse_tree.h src/proc.h src/reader.h obj/builtin_complete.o: src/highlight.h src/color.h src/wgetopt.h 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/parser.h src/expand.h src/parse_constants.h +obj/builtin_emit.o: src/parse_tree.h src/tokenizer.h src/proc.h +obj/builtin_emit.o: src/wgetopt.h src/wutil.h obj/builtin_jobs.o: config.h src/builtin.h src/common.h src/fallback.h obj/builtin_jobs.o: src/signal.h src/io.h src/proc.h src/parse_tree.h obj/builtin_jobs.o: src/parse_constants.h src/tokenizer.h src/wgetopt.h diff --git a/src/builtin.cpp b/src/builtin.cpp index aaaf02538..bec832fea 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -42,6 +42,7 @@ #include "builtin_block.h" #include "builtin_commandline.h" #include "builtin_complete.h" +#include "builtin_emit.h" #include "builtin_jobs.h" #include "builtin_printf.h" #include "builtin_set.h" @@ -293,43 +294,6 @@ static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **ar return STATUS_CMD_OK; } -/// Implementation of the builtin emit command, used to create events. -static int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - int argc = builtin_count_args(argv); - - static const wchar_t *short_options = L"h"; - static const struct woption long_options[] = {{L"help", no_argument, 0, 'h'}, {0, 0, 0, 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': { - builtin_print_help(parser, streams, argv[0], streams.out); - 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; - } - } - } - - if (!argv[w.woptind]) { - streams.err.append_format(L"%ls: expected event name\n", argv[0]); - return STATUS_INVALID_ARGS; - } - const wchar_t *eventname = argv[w.woptind]; - wcstring_list_t args(argv + w.woptind + 1, argv + argc); - event_fire_generic(eventname, &args); - - 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) { diff --git a/src/builtin_emit.cpp b/src/builtin_emit.cpp new file mode 100644 index 000000000..a20cfb4d9 --- /dev/null +++ b/src/builtin_emit.cpp @@ -0,0 +1,73 @@ +// 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 emit_opts { + bool print_help = false; +}; + +static int parse_emit_opts(struct emit_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"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, 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; +} + +/// 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 emit_opts opts; + + int optind; + int retval = parse_emit_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_INVALID_ARGS; + } + + if (!argv[optind]) { + streams.err.append_format(L"%ls: expected event name\n", argv[0]); + return STATUS_INVALID_ARGS; + } + + const wchar_t *eventname = argv[optind]; + wcstring_list_t args(argv + optind + 1, argv + argc); + event_fire_generic(eventname, &args); + return STATUS_CMD_OK; +} diff --git a/src/builtin_emit.h b/src/builtin_emit.h new file mode 100644 index 000000000..0bf500002 --- /dev/null +++ b/src/builtin_emit.h @@ -0,0 +1,9 @@ +// Prototypes for executing builtin_emit function. +#ifndef FISH_BUILTIN_EMIT_H +#define FISH_BUILTIN_EMIT_H + +class parser_t; +struct io_streams_t; + +int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv); +#endif