diff --git a/src/builtin.cpp b/src/builtin.cpp index 686dfbace..c660defe2 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -119,8 +119,8 @@ static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h {nullptr, 0, nullptr, 0}}; int parse_help_only_cmd_opts(struct help_only_cmd_opts_t &opts, int *optind, int argc, - wchar_t **argv, parser_t &parser, io_streams_t &streams) { - wchar_t *cmd = argv[0]; + const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -204,7 +204,7 @@ void builtin_print_error_trailer(parser_t &parser, output_stream_t &b, const wch /// 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 maybe_t builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +static maybe_t builtin_generic(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; @@ -231,7 +231,7 @@ static maybe_t builtin_generic(parser_t &parser, io_streams_t &streams, wch // Since this is just for counting, it can be massive. #define COUNT_CHUNK_SIZE (512 * 256) /// Implementation of the builtin count command, used to count the number of arguments sent to it. -static maybe_t builtin_count(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +static maybe_t builtin_count(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { UNUSED(parser); int argc = 0; @@ -266,7 +266,7 @@ static maybe_t builtin_count(parser_t &parser, io_streams_t &streams, wchar /// This function handles both the 'continue' and the 'break' builtins that are used for loop /// control. static maybe_t builtin_break_continue(parser_t &parser, io_streams_t &streams, - wchar_t **argv) { + const wchar_t **argv) { int is_break = (std::wcscmp(argv[0], L"break") == 0); int argc = builtin_count_args(argv); @@ -297,8 +297,9 @@ static maybe_t builtin_break_continue(parser_t &parser, io_streams_t &strea } /// Implementation of the builtin breakpoint command, used to launch the interactive debugger. -static maybe_t builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +static maybe_t builtin_breakpoint(parser_t &parser, io_streams_t &streams, + const wchar_t **argv) { + const wchar_t *cmd = argv[0]; if (argv[1] != nullptr) { streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, cmd, 0, builtin_count_args(argv) - 1); return STATUS_INVALID_ARGS; @@ -323,21 +324,21 @@ static maybe_t builtin_breakpoint(parser_t &parser, io_streams_t &streams, return parser.get_last_status(); } -maybe_t builtin_true(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_true(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { UNUSED(parser); UNUSED(streams); UNUSED(argv); return STATUS_CMD_OK; } -maybe_t builtin_false(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_false(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { UNUSED(parser); UNUSED(streams); UNUSED(argv); return STATUS_CMD_ERROR; } -maybe_t builtin_gettext(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_gettext(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { UNUSED(parser); UNUSED(streams); for (int i = 1; i < builtin_count_args(argv); i++) { @@ -455,7 +456,7 @@ static const wchar_t *const help_builtins[] = {L"for", L"while", L"function", L static bool cmd_needs_help(const wchar_t *cmd) { return contains(help_builtins, cmd); } /// Execute a builtin command -proc_status_t builtin_run(parser_t &parser, wchar_t **argv, io_streams_t &streams) { +proc_status_t builtin_run(parser_t &parser, const wchar_t **argv, io_streams_t &streams) { UNUSED(parser); UNUSED(streams); if (argv == nullptr || argv[0] == nullptr) diff --git a/src/builtin.h b/src/builtin.h index 00b9ec6c6..fece694aa 100644 --- a/src/builtin.h +++ b/src/builtin.h @@ -20,7 +20,7 @@ struct builtin_data_t { // Name of the builtin. const wchar_t *name; // Function pointer to the builtin implementation. - maybe_t (*func)(parser_t &parser, io_streams_t &streams, wchar_t **argv); + maybe_t (*func)(parser_t &parser, io_streams_t &streams, const wchar_t **argv); // Description of what the builtin does. const wchar_t *desc; @@ -85,7 +85,7 @@ enum { COMMAND_NOT_BUILTIN, BUILTIN_REGULAR, BUILTIN_FUNCTION }; void builtin_init(); bool builtin_exists(const wcstring &cmd); -proc_status_t builtin_run(parser_t &parser, wchar_t **argv, io_streams_t &streams); +proc_status_t builtin_run(parser_t &parser, const wchar_t **argv, io_streams_t &streams); wcstring_list_t builtin_get_names(); void builtin_get_names(completion_list_t *list); @@ -110,6 +110,6 @@ void builtin_wperror(const wchar_t *s, io_streams_t &streams); struct help_only_cmd_opts_t { bool print_help = false; }; -int parse_help_only_cmd_opts(help_only_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv, - parser_t &parser, io_streams_t &streams); +int parse_help_only_cmd_opts(help_only_cmd_opts_t &opts, int *optind, int argc, + const wchar_t **argv, parser_t &parser, io_streams_t &streams); #endif diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index 289dbec75..b4facf9a4 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -321,9 +321,9 @@ static bool parse_option_spec(argparse_cmd_opts_t &opts, //!OCLINT(high npath c return true; } -static int collect_option_specs(argparse_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv, - io_streams_t &streams) { - wchar_t *cmd = argv[0]; +static int collect_option_specs(argparse_cmd_opts_t &opts, int *optind, int argc, + const wchar_t **argv, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; // A counter to give short chars to long-only options because getopt needs that. // Luckily we have wgetopt so we can use wchars - this is one of the private use areas so we @@ -360,8 +360,8 @@ static int collect_option_specs(argparse_cmd_opts_t &opts, int *optind, int argc } static int parse_cmd_opts(argparse_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -564,7 +564,7 @@ static int handle_flag(parser_t &parser, const argparse_cmd_opts_t &opts, option static int argparse_parse_flags(parser_t &parser, argparse_cmd_opts_t &opts, const wchar_t *short_options, const woption *long_options, - const wchar_t *cmd, int argc, wchar_t **argv, int *optind, + const wchar_t *cmd, int argc, const wchar_t **argv, int *optind, io_streams_t &streams) { int opt; int long_idx = -1; @@ -704,7 +704,7 @@ static void set_argparse_result_vars(env_stack_t &vars, const argparse_cmd_opts_ /// an external command also means its output has to be in a form that can be eval'd. Because our /// version is a builtin it can directly set variables local to the current scope (e.g., a /// function). It doesn't need to write anything to stdout that then needs to be eval'd. -maybe_t builtin_argparse(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_argparse(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); argparse_cmd_opts_t opts; diff --git a/src/builtin_argparse.h b/src/builtin_argparse.h index 24e4b6079..8d0422adc 100644 --- a/src/builtin_argparse.h +++ b/src/builtin_argparse.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_argparse(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_argparse(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_bg.cpp b/src/builtin_bg.cpp index 798d72511..d3ea75d0c 100644 --- a/src/builtin_bg.cpp +++ b/src/builtin_bg.cpp @@ -37,7 +37,7 @@ static int send_to_bg(parser_t &parser, io_streams_t &streams, job_t *j) { } /// Builtin for putting a job in the background. -maybe_t builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_bg(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; diff --git a/src/builtin_bg.h b/src/builtin_bg.h index bb628a433..133c2cb57 100644 --- a/src/builtin_bg.h +++ b/src/builtin_bg.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_bg(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_bind.cpp b/src/builtin_bind.cpp index 632097761..502464037 100644 --- a/src/builtin_bind.cpp +++ b/src/builtin_bind.cpp @@ -188,8 +188,8 @@ bool builtin_bind_t::add(const wcstring &seq, const wchar_t *const *cmds, size_t /// @param use_terminfo /// Whether to look use terminfo -k name /// -bool builtin_bind_t::erase(wchar_t **seq, bool all, const wchar_t *mode, bool use_terminfo, - bool user, io_streams_t &streams) { +bool builtin_bind_t::erase(const wchar_t *const *seq, bool all, const wchar_t *mode, + bool use_terminfo, bool user, io_streams_t &streams) { if (all) { input_mappings_->clear(mode, user); return false; @@ -214,8 +214,8 @@ bool builtin_bind_t::erase(wchar_t **seq, bool all, const wchar_t *mode, bool us return res; } -bool builtin_bind_t::insert(int optind, int argc, wchar_t **argv, io_streams_t &streams) { - wchar_t *cmd = argv[0]; +bool builtin_bind_t::insert(int optind, int argc, const wchar_t **argv, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int arg_count = argc - optind; if (arg_count < 2) { @@ -300,8 +300,8 @@ void builtin_bind_t::list_modes(io_streams_t &streams) { } static int parse_cmd_opts(bind_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; static const wchar_t *const short_options = L":aehkKfM:Lm:s"; static const struct woption long_options[] = {{L"all", no_argument, nullptr, 'a'}, {L"erase", no_argument, nullptr, 'e'}, @@ -399,8 +399,9 @@ static int parse_cmd_opts(bind_cmd_opts_t &opts, int *optind, //!OCLINT(high nc } /// The bind builtin, used for setting character sequences. -maybe_t builtin_bind_t::builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_bind_t::builtin_bind(parser_t &parser, io_streams_t &streams, + const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); bind_cmd_opts_t opts; this->opts = &opts; diff --git a/src/builtin_bind.h b/src/builtin_bind.h index a6050f93e..26951564d 100644 --- a/src/builtin_bind.h +++ b/src/builtin_bind.h @@ -11,7 +11,7 @@ struct bind_cmd_opts_t; class builtin_bind_t { public: - maybe_t builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv); + maybe_t builtin_bind(parser_t &parser, io_streams_t &streams, const wchar_t **argv); builtin_bind_t() : input_mappings_(input_mappings()) {} @@ -28,17 +28,17 @@ class builtin_bind_t { void function_names(io_streams_t &streams); bool add(const wcstring &seq, const wchar_t *const *cmds, size_t cmds_len, const wchar_t *mode, const wchar_t *sets_mode, bool terminfo, bool user, io_streams_t &streams); - bool erase(wchar_t **seq, bool all, const wchar_t *mode, bool use_terminfo, bool user, - io_streams_t &streams); + bool erase(const wchar_t *const *seq, bool all, const wchar_t *mode, bool use_terminfo, + bool user, io_streams_t &streams); bool get_terminfo_sequence(const wcstring &seq, wcstring *out_seq, io_streams_t &streams) const; - bool insert(int optind, int argc, wchar_t **argv, io_streams_t &streams); + bool insert(int optind, int argc, const wchar_t **argv, io_streams_t &streams); void list_modes(io_streams_t &streams); bool list_one(const wcstring &seq, const wcstring &bind_mode, bool user, io_streams_t &streams); bool list_one(const wcstring &seq, const wcstring &bind_mode, bool user, bool preset, io_streams_t &streams); }; -inline maybe_t builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +inline maybe_t builtin_bind(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { builtin_bind_t bind; return bind.builtin_bind(parser, streams, argv); } diff --git a/src/builtin_block.cpp b/src/builtin_block.cpp index e2e9b78d8..6b9491a5c 100644 --- a/src/builtin_block.cpp +++ b/src/builtin_block.cpp @@ -22,8 +22,8 @@ struct block_cmd_opts_t { }; static int parse_cmd_opts(block_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; static const wchar_t *const short_options = L":eghl"; static const struct woption long_options[] = {{L"erase", no_argument, nullptr, 'e'}, {L"local", no_argument, nullptr, 'l'}, @@ -70,7 +70,7 @@ static int parse_cmd_opts(block_cmd_opts_t &opts, int *optind, //!OCLINT(high n } /// The block builtin, used for temporarily blocking events. -maybe_t builtin_block(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_block(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); block_cmd_opts_t opts; diff --git a/src/builtin_block.h b/src/builtin_block.h index 96cf844cf..5b7b4234b 100644 --- a/src/builtin_block.h +++ b/src/builtin_block.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_block(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_block(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_builtin.cpp b/src/builtin_builtin.cpp index 30d291274..7096a6cdd 100644 --- a/src/builtin_builtin.cpp +++ b/src/builtin_builtin.cpp @@ -25,9 +25,9 @@ static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h {L"query", no_argument, nullptr, 'q'}, {nullptr, 0, nullptr, 0}}; -static int parse_cmd_opts(builtin_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv, +static int parse_cmd_opts(builtin_cmd_opts_t &opts, int *optind, int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -65,7 +65,7 @@ static int parse_cmd_opts(builtin_cmd_opts_t &opts, int *optind, int argc, wchar /// 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. -maybe_t builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_builtin(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); builtin_cmd_opts_t opts; diff --git a/src/builtin_builtin.h b/src/builtin_builtin.h index 75e5ccc4c..ec247fcac 100644 --- a/src/builtin_builtin.h +++ b/src/builtin_builtin.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_builtin(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_cd.cpp b/src/builtin_cd.cpp index ccdd01958..880a5e522 100644 --- a/src/builtin_cd.cpp +++ b/src/builtin_cd.cpp @@ -20,7 +20,7 @@ /// 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. -maybe_t builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_cd(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; diff --git a/src/builtin_cd.h b/src/builtin_cd.h index a28a85f4c..26070e6c7 100644 --- a/src/builtin_cd.h +++ b/src/builtin_cd.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_cd(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_command.cpp b/src/builtin_command.cpp index e42093edf..53d826a3c 100644 --- a/src/builtin_command.cpp +++ b/src/builtin_command.cpp @@ -28,9 +28,9 @@ static const struct woption long_options[] = { {L"quiet", no_argument, nullptr, 'q'}, {L"query", no_argument, nullptr, 'q'}, {L"search", no_argument, nullptr, 's'}, {nullptr, 0, nullptr, 0}}; -static int parse_cmd_opts(command_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv, +static int parse_cmd_opts(command_cmd_opts_t &opts, int *optind, int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -72,7 +72,7 @@ static int parse_cmd_opts(command_cmd_opts_t &opts, int *optind, int argc, wchar /// Implementation of the builtin 'command'. Actual command running is handled by the parser, this /// just processes the flags. -maybe_t builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_command(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); command_cmd_opts_t opts; diff --git a/src/builtin_command.h b/src/builtin_command.h index 8557bc13d..84c7a7672 100644 --- a/src/builtin_command.h +++ b/src/builtin_command.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_command(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_commandline.cpp b/src/builtin_commandline.cpp index da4c4afdc..888026bef 100644 --- a/src/builtin_commandline.cpp +++ b/src/builtin_commandline.cpp @@ -124,7 +124,7 @@ static void write_part(const wchar_t *begin, const wchar_t *end, int cut_at_curs } /// The commandline builtin. It is used for specifying a new value for the commandline. -maybe_t builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_commandline(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { // Pointer to what the commandline builtin considers to be the current contents of the command // line buffer. const wchar_t *current_buffer = nullptr; @@ -132,7 +132,7 @@ maybe_t builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_ // What the commandline builtin considers to be the current cursor position. auto current_cursor_pos = static_cast(-1); - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int buffer_part = 0; bool cut_at_cursor = false; diff --git a/src/builtin_commandline.h b/src/builtin_commandline.h index 277056ae5..8b4d3d31d 100644 --- a/src/builtin_commandline.h +++ b/src/builtin_commandline.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_commandline(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_complete.cpp b/src/builtin_complete.cpp index 72b1571ea..a063775d1 100644 --- a/src/builtin_complete.cpp +++ b/src/builtin_complete.cpp @@ -123,10 +123,10 @@ static void builtin_complete_print(const wcstring &cmd, io_streams_t &streams, p /// The complete builtin. Used for specifying programmable tab-completions. Calls the functions in // complete.cpp for any heavy lifting. -maybe_t builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_complete(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { ASSERT_IS_MAIN_THREAD(); - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); completion_mode_t result_mode{}; int remove = 0; diff --git a/src/builtin_complete.h b/src/builtin_complete.h index c2f3ab31b..bb7097f49 100644 --- a/src/builtin_complete.h +++ b/src/builtin_complete.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_complete(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_contains.cpp b/src/builtin_contains.cpp index 389ad0916..ad91667f7 100644 --- a/src/builtin_contains.cpp +++ b/src/builtin_contains.cpp @@ -23,9 +23,9 @@ static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h {L"index", no_argument, nullptr, 'i'}, {nullptr, 0, nullptr, 0}}; -static int parse_cmd_opts(contains_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv, +static int parse_cmd_opts(contains_cmd_opts_t &opts, int *optind, int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -58,7 +58,7 @@ static int parse_cmd_opts(contains_cmd_opts_t &opts, int *optind, int argc, wcha /// Implementation of the builtin contains command, used to check if a specified string is part of /// a list. -maybe_t builtin_contains(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_contains(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); contains_cmd_opts_t opts; @@ -72,7 +72,7 @@ maybe_t builtin_contains(parser_t &parser, io_streams_t &streams, wchar_t * return STATUS_CMD_OK; } - wchar_t *needle = argv[optind]; + const wchar_t *needle = argv[optind]; if (!needle) { streams.err.append_format(_(L"%ls: Key not specified\n"), cmd); } else { diff --git a/src/builtin_contains.h b/src/builtin_contains.h index fca65875c..96f00ded1 100644 --- a/src/builtin_contains.h +++ b/src/builtin_contains.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_contains(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_contains(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_disown.cpp b/src/builtin_disown.cpp index cfccefa7a..b6ff9c9cb 100644 --- a/src/builtin_disown.cpp +++ b/src/builtin_disown.cpp @@ -42,7 +42,7 @@ static int disown_job(const wchar_t *cmd, parser_t &parser, io_streams_t &stream } /// Builtin for removing jobs from the job list. -maybe_t builtin_disown(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_disown(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; diff --git a/src/builtin_disown.h b/src/builtin_disown.h index d6c836928..d0eb2debe 100644 --- a/src/builtin_disown.h +++ b/src/builtin_disown.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_disown(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_disown(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_echo.cpp b/src/builtin_echo.cpp index e380f63ed..12ed26c19 100644 --- a/src/builtin_echo.cpp +++ b/src/builtin_echo.cpp @@ -21,11 +21,11 @@ struct echo_cmd_opts_t { static const wchar_t *const short_options = L"+:Eens"; static const struct woption *const long_options = nullptr; -static int parse_cmd_opts(echo_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv, +static int parse_cmd_opts(echo_cmd_opts_t &opts, int *optind, int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { UNUSED(parser); UNUSED(streams); - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; echo_cmd_opts_t oldopts = opts; @@ -194,8 +194,8 @@ static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *cons /// /// Bash only respects -n if it's the first argument. We'll do the same. We also support a new, /// fish specific, option -s to mean "no spaces". -maybe_t builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_echo(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; UNUSED(cmd); int argc = builtin_count_args(argv); echo_cmd_opts_t opts; diff --git a/src/builtin_echo.h b/src/builtin_echo.h index 612e869a7..7b9d0ba1a 100644 --- a/src/builtin_echo.h +++ b/src/builtin_echo.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_echo(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_emit.cpp b/src/builtin_emit.cpp index 161d652a0..97ad03767 100644 --- a/src/builtin_emit.cpp +++ b/src/builtin_emit.cpp @@ -11,7 +11,7 @@ #include "wutil.h" // IWYU pragma: keep /// Implementation of the builtin emit command, used to create events. -maybe_t builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_emit(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; diff --git a/src/builtin_emit.h b/src/builtin_emit.h index 6ca671d88..fc1b06e8c 100644 --- a/src/builtin_emit.h +++ b/src/builtin_emit.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_emit(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_eval.cpp b/src/builtin_eval.cpp index 19a09d2b3..bd95e1390 100644 --- a/src/builtin_eval.cpp +++ b/src/builtin_eval.cpp @@ -15,7 +15,7 @@ #include "wutil.h" // IWYU pragma: keep /// Implementation of eval builtin. -maybe_t builtin_eval(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_eval(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { int argc = builtin_count_args(argv); if (argc <= 1) { return STATUS_CMD_OK; diff --git a/src/builtin_eval.h b/src/builtin_eval.h index ae67c7da9..128671b4d 100644 --- a/src/builtin_eval.h +++ b/src/builtin_eval.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_eval(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_eval(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_exit.cpp b/src/builtin_exit.cpp index 4a28265d5..c1a400fb7 100644 --- a/src/builtin_exit.cpp +++ b/src/builtin_exit.cpp @@ -24,10 +24,10 @@ static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h {nullptr, 0, nullptr, 0}}; static int parse_cmd_opts(exit_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method) - int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) { + int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { UNUSED(parser); UNUSED(streams); - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -58,7 +58,7 @@ static int parse_cmd_opts(exit_cmd_opts_t &opts, int *optind, //!OCLINT(high nc } /// The exit builtin. Calls reader_exit to exit and returns the value specified. -maybe_t builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_exit(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); exit_cmd_opts_t opts; diff --git a/src/builtin_exit.h b/src/builtin_exit.h index f446daddf..91822b9a5 100644 --- a/src/builtin_exit.h +++ b/src/builtin_exit.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_exit(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_fg.cpp b/src/builtin_fg.cpp index 1211c27c1..19f155f54 100644 --- a/src/builtin_fg.cpp +++ b/src/builtin_fg.cpp @@ -21,7 +21,7 @@ #include "wutil.h" // IWYU pragma: keep /// Builtin for putting a job in the foreground. -maybe_t builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_fg(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; diff --git a/src/builtin_fg.h b/src/builtin_fg.h index 8503f7286..713c431b1 100644 --- a/src/builtin_fg.h +++ b/src/builtin_fg.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_fg(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_function.cpp b/src/builtin_function.cpp index 5d9590c67..9e09be99d 100644 --- a/src/builtin_function.cpp +++ b/src/builtin_function.cpp @@ -54,7 +54,7 @@ static const struct woption long_options[] = { {nullptr, 0, nullptr, 0}}; static int parse_cmd_opts(function_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method) - int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) { + int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { const wchar_t *cmd = L"function"; int opt; wgetopter_t w; @@ -210,8 +210,8 @@ maybe_t builtin_function(parser_t &parser, io_streams_t &streams, args.insert(args.end(), c_args.begin(), c_args.end()); null_terminated_array_t argv_array(args); - wchar_t **argv = argv_array.get(); - wchar_t *cmd = argv[0]; + const wchar_t **argv = argv_array.get(); + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); // A valid function name has to be the first argument. diff --git a/src/builtin_functions.cpp b/src/builtin_functions.cpp index e65d4ab43..5b8bf8985 100644 --- a/src/builtin_functions.cpp +++ b/src/builtin_functions.cpp @@ -42,8 +42,8 @@ struct functions_cmd_opts_t { bool report_metadata = false; bool verbose = false; bool handlers = false; - wchar_t *handlers_type = nullptr; - wchar_t *description = nullptr; + const wchar_t *handlers_type = nullptr; + const wchar_t *description = nullptr; }; static const wchar_t *const short_options = L":Ht:Dacd:ehnqv"; static const struct woption long_options[] = {{L"erase", no_argument, nullptr, 'e'}, @@ -60,8 +60,8 @@ static const struct woption long_options[] = {{L"erase", no_argument, nullptr, ' {nullptr, 0, nullptr, 0}}; static int parse_cmd_opts(functions_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -184,7 +184,7 @@ static int report_function_metadata(const wchar_t *funcname, bool verbose, io_st } /// The functions builtin, used for listing and erasing functions. -maybe_t builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_functions(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); functions_cmd_opts_t opts; @@ -212,15 +212,13 @@ maybe_t builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t } if (opts.description) { - wchar_t *func; - if (argc - optind != 1) { streams.err.append_format(_(L"%ls: Expected exactly one function name\n"), cmd); builtin_print_error_trailer(parser, streams.err, cmd); return STATUS_INVALID_ARGS; } - func = argv[optind]; + const wchar_t *func = argv[optind]; if (!function_exists(func, parser)) { streams.err.append_format(_(L"%ls: Function '%ls' does not exist\n"), cmd, func); builtin_print_error_trailer(parser, streams.err, cmd); diff --git a/src/builtin_functions.h b/src/builtin_functions.h index 1746eaac3..3378d50ed 100644 --- a/src/builtin_functions.h +++ b/src/builtin_functions.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_functions(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_history.cpp b/src/builtin_history.cpp index 5dffd9e98..2f8cde9de 100644 --- a/src/builtin_history.cpp +++ b/src/builtin_history.cpp @@ -61,7 +61,7 @@ static const struct woption long_options[] = {{L"prefix", no_argument, nullptr, {nullptr, 0, nullptr, 0}}; /// Remember the history subcommand and disallow selecting more than one history subcommand. -static bool set_hist_cmd(wchar_t *const cmd, hist_cmd_t *hist_cmd, hist_cmd_t sub_cmd, +static bool set_hist_cmd(const wchar_t *cmd, hist_cmd_t *hist_cmd, hist_cmd_t sub_cmd, io_streams_t &streams) { if (*hist_cmd != HIST_UNDEF) { wchar_t err_text[1024]; @@ -95,8 +95,8 @@ static bool check_for_unexpected_hist_args(const history_cmd_opts_t &opts, const } static int parse_cmd_opts(history_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -200,8 +200,8 @@ static int parse_cmd_opts(history_cmd_opts_t &opts, int *optind, //!OCLINT(high } /// Manipulate history of interactive commands executed by the user. -maybe_t builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_history(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); history_cmd_opts_t opts; diff --git a/src/builtin_history.h b/src/builtin_history.h index 3aef6aacd..7ab35a04f 100644 --- a/src/builtin_history.h +++ b/src/builtin_history.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_history(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_jobs.cpp b/src/builtin_jobs.cpp index e10b12d6d..6f9f86d56 100644 --- a/src/builtin_jobs.cpp +++ b/src/builtin_jobs.cpp @@ -121,8 +121,8 @@ static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_ } /// The jobs builtin. Used for printing running jobs. Defined in builtin_jobs.c. -maybe_t builtin_jobs(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_jobs(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); bool found = false; int mode = JOBS_DEFAULT; diff --git a/src/builtin_jobs.h b/src/builtin_jobs.h index a92375573..5d4f1e0a8 100644 --- a/src/builtin_jobs.h +++ b/src/builtin_jobs.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_jobs(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_jobs(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index 310d2e554..9c43bc6a7 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -43,7 +43,7 @@ static const struct woption long_options[] = {{L"scale", required_argument, null {nullptr, 0, nullptr, 0}}; static int parse_cmd_opts(math_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method) - int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) { + int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { const wchar_t *cmd = L"math"; int opt; wgetopter_t w; @@ -140,13 +140,13 @@ static const wchar_t *math_get_arg_stdin(wcstring *storage, const io_streams_t & } /// Return the next argument from argv. -static const wchar_t *math_get_arg_argv(int *argidx, wchar_t **argv) { +static const wchar_t *math_get_arg_argv(int *argidx, const wchar_t **argv) { return argv && argv[*argidx] ? argv[(*argidx)++] : nullptr; } /// Get the arguments from argv or stdin based on the execution context. This mimics how builtin /// `string` does it. -static const wchar_t *math_get_arg(int *argidx, wchar_t **argv, wcstring *storage, +static const wchar_t *math_get_arg(int *argidx, const wchar_t **argv, wcstring *storage, const io_streams_t &streams) { if (math_args_from_stdin(streams)) { assert(streams.stdin_fd >= 0 && @@ -272,8 +272,8 @@ static int evaluate_expression(const wchar_t *cmd, const parser_t &parser, io_st } /// The math builtin evaluates math expressions. -maybe_t builtin_math(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_math(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); math_cmd_opts_t opts; int optind; diff --git a/src/builtin_math.h b/src/builtin_math.h index 80ef56702..7b2566d6a 100644 --- a/src/builtin_math.h +++ b/src/builtin_math.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_math(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_math(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_printf.cpp b/src/builtin_printf.cpp index 61cef9fcd..90fa3a116 100644 --- a/src/builtin_printf.cpp +++ b/src/builtin_printf.cpp @@ -90,7 +90,7 @@ struct builtin_printf_state_t { void print_direc(const wchar_t *start, size_t length, wchar_t conversion, bool have_field_width, int field_width, bool have_precision, int precision, wchar_t const *argument); - int print_formatted(const wchar_t *format, int argc, wchar_t **argv); + int print_formatted(const wchar_t *format, int argc, const wchar_t **argv); void nonfatal_error(const wchar_t *fmt, ...); void fatal_error(const wchar_t *fmt, ...); @@ -585,7 +585,7 @@ static inline void modify_allowed_format_specifiers(bool ok[UCHAR_MAX + 1], cons /// Print the text in FORMAT, using ARGV (with ARGC elements) for arguments to any `%' directives. /// Return the number of elements of ARGV used. -int builtin_printf_state_t::print_formatted(const wchar_t *format, int argc, wchar_t **argv) { +int builtin_printf_state_t::print_formatted(const wchar_t *format, int argc, const wchar_t **argv) { int save_argc = argc; /* Preserve original value. */ const wchar_t *f; /* Pointer into `format'. */ const wchar_t *direc_start; /* Start of % directive. */ @@ -737,7 +737,7 @@ int builtin_printf_state_t::print_formatted(const wchar_t *format, int argc, wch } /// The printf builtin. -maybe_t builtin_printf(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_printf(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; @@ -759,7 +759,7 @@ maybe_t builtin_printf(parser_t &parser, io_streams_t &streams, wchar_t **a builtin_printf_state_t state(streams); int args_used; - wchar_t *format = argv[0]; + const wchar_t *format = argv[0]; argc--; argv++; diff --git a/src/builtin_printf.h b/src/builtin_printf.h index 08be3e076..c724fde3d 100644 --- a/src/builtin_printf.h +++ b/src/builtin_printf.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_printf(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_printf(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_pwd.cpp b/src/builtin_pwd.cpp index 907a04fdf..80c8e579e 100644 --- a/src/builtin_pwd.cpp +++ b/src/builtin_pwd.cpp @@ -19,7 +19,7 @@ static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h {L"logical", no_argument, nullptr, 'L'}, {L"physical", no_argument, nullptr, 'P'}, {nullptr, 0, nullptr, 0}}; -maybe_t builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_pwd(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { UNUSED(parser); const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); diff --git a/src/builtin_pwd.h b/src/builtin_pwd.h index 5e7b392fa..7b90b4156 100644 --- a/src/builtin_pwd.h +++ b/src/builtin_pwd.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_pwd(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_random.cpp b/src/builtin_random.cpp index 88e4c431a..8b38694ec 100644 --- a/src/builtin_random.cpp +++ b/src/builtin_random.cpp @@ -27,8 +27,8 @@ static std::minstd_rand get_seeded_engine() { } /// The random builtin generates random numbers. -maybe_t builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_random(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); help_only_cmd_opts_t opts; diff --git a/src/builtin_random.h b/src/builtin_random.h index 80fd5b3f7..bf852824f 100644 --- a/src/builtin_random.h +++ b/src/builtin_random.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_random(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_read.cpp b/src/builtin_read.cpp index 6b9707375..f76c2ad60 100644 --- a/src/builtin_read.cpp +++ b/src/builtin_read.cpp @@ -80,8 +80,8 @@ static const struct woption long_options[] = {{L"array", no_argument, nullptr, ' {nullptr, 0, nullptr, 0}}; static int parse_cmd_opts(read_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -434,8 +434,8 @@ static int validate_read_args(const wchar_t *cmd, read_cmd_opts_t &opts, int arg } /// The read builtin. Reads from stdin and stores the values in environment variables. -maybe_t builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_read(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); wcstring buff; int exit_res = STATUS_CMD_OK; @@ -475,7 +475,7 @@ maybe_t builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **arg opts.shell = false; } - wchar_t *const *var_ptr = argv; + const wchar_t *const *var_ptr = argv; auto vars_left = [&]() { return argv + argc - var_ptr; }; auto clear_remaining_vars = [&]() { while (vars_left()) { diff --git a/src/builtin_read.h b/src/builtin_read.h index f1fdccdee..3093a12af 100644 --- a/src/builtin_read.h +++ b/src/builtin_read.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_read(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_realpath.cpp b/src/builtin_realpath.cpp index 1268e4e73..d91d9fff4 100644 --- a/src/builtin_realpath.cpp +++ b/src/builtin_realpath.cpp @@ -27,7 +27,7 @@ static const struct woption long_options[] = {{L"no-symlinks", no_argument, null {nullptr, 0, nullptr, 0}}; static int parse_cmd_opts(realpath_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method) - int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) { + int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; @@ -61,7 +61,7 @@ static int parse_cmd_opts(realpath_cmd_opts_t &opts, int *optind, //!OCLINT(hig /// An implementation of the external realpath command. Doesn't support any options. /// In general scripts shouldn't invoke this directly. They should just use `realpath` which /// will fallback to this builtin if an external command cannot be found. -maybe_t builtin_realpath(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_realpath(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; realpath_cmd_opts_t opts; int argc = builtin_count_args(argv); diff --git a/src/builtin_realpath.h b/src/builtin_realpath.h index 2022e9d29..70d6088db 100644 --- a/src/builtin_realpath.h +++ b/src/builtin_realpath.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_realpath(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_realpath(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_return.cpp b/src/builtin_return.cpp index 5f521960d..181711eb9 100644 --- a/src/builtin_return.cpp +++ b/src/builtin_return.cpp @@ -23,10 +23,10 @@ static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h {nullptr, 0, nullptr, 0}}; static int parse_cmd_opts(return_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method) - int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) { + int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { UNUSED(parser); UNUSED(streams); - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -57,7 +57,7 @@ static int parse_cmd_opts(return_cmd_opts_t &opts, int *optind, //!OCLINT(high } /// Function for handling the return builtin. -maybe_t builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_return(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); return_cmd_opts_t opts; diff --git a/src/builtin_return.h b/src/builtin_return.h index 401ed6ec0..f43c33f71 100644 --- a/src/builtin_return.h +++ b/src/builtin_return.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_return(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_set.cpp b/src/builtin_set.cpp index d1c3737e5..32c59d03a 100644 --- a/src/builtin_set.cpp +++ b/src/builtin_set.cpp @@ -81,8 +81,8 @@ static const struct woption long_options[] = { static int is_path_variable(const wcstring &env) { return env == L"PATH" || env == L"CDPATH"; } static int parse_cmd_opts(set_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; @@ -470,8 +470,8 @@ static env_mode_flags_t compute_scope(const set_cmd_opts_t &opts) { /// Print the names of all environment variables in the scope. It will include the values unless the /// `set --names` flag was used. -static int builtin_set_list(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv, - parser_t &parser, io_streams_t &streams) { +static int builtin_set_list(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, + const wchar_t **argv, parser_t &parser, io_streams_t &streams) { UNUSED(cmd); UNUSED(argc); UNUSED(argv); @@ -520,8 +520,8 @@ static int builtin_set_list(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, } // Query mode. Return the number of variables that do NOT exist out of the specified variables. -static int builtin_set_query(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv, - parser_t &parser, io_streams_t &streams) { +static int builtin_set_query(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, + const wchar_t **argv, parser_t &parser, io_streams_t &streams) { int retval = 0; env_mode_flags_t scope = compute_scope(opts); @@ -596,7 +596,7 @@ static void show_scope(const wchar_t *var_name, int scope, io_streams_t &streams /// Show mode. Show information about the named variable(s). static int builtin_set_show(const wchar_t *cmd, const set_cmd_opts_t &opts, int argc, - wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t **argv, parser_t &parser, io_streams_t &streams) { UNUSED(opts); const auto &vars = parser.vars(); if (argc == 0) { // show all vars @@ -610,7 +610,7 @@ static int builtin_set_show(const wchar_t *cmd, const set_cmd_opts_t &opts, int } } else { for (int i = 0; i < argc; i++) { - wchar_t *arg = argv[i]; + const wchar_t *arg = argv[i]; if (!valid_var_name(arg)) { streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, arg); @@ -635,8 +635,8 @@ static int builtin_set_show(const wchar_t *cmd, const set_cmd_opts_t &opts, int } /// Erase a variable. -static int builtin_set_erase(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv, - parser_t &parser, io_streams_t &streams) { +static int builtin_set_erase(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, + const wchar_t **argv, parser_t &parser, io_streams_t &streams) { int ret = STATUS_CMD_OK; env_mode_flags_t scope = compute_scope(opts); for (int i = 0; i < argc; i++) { @@ -745,7 +745,7 @@ static wcstring_list_t new_var_values_by_index(const split_var_t &split, int arg } /// Set a variable. -static int builtin_set_set(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wchar_t **argv, +static int builtin_set_set(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { if (argc == 0) { streams.err.append_format(BUILTIN_ERR_MIN_ARG_COUNT1, cmd, 1); @@ -819,8 +819,8 @@ static int builtin_set_set(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, w } /// The set builtin creates, updates, and erases (removes, deletes) variables. -maybe_t builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_set(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); set_cmd_opts_t opts; diff --git a/src/builtin_set.h b/src/builtin_set.h index 04c4877cd..e7ea6ddd8 100644 --- a/src/builtin_set.h +++ b/src/builtin_set.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_set(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_set_color.cpp b/src/builtin_set_color.cpp index f8f54f4de..073929ceb 100644 --- a/src/builtin_set_color.cpp +++ b/src/builtin_set_color.cpp @@ -105,7 +105,7 @@ static char dim_esc[] = "\x1B[2m"; #endif /// set_color builtin. -maybe_t builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_set_color(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { // By the time this is called we should have initialized the curses subsystem. assert(curses_initialized); diff --git a/src/builtin_set_color.h b/src/builtin_set_color.h index 06be7db91..1a8dd0cce 100644 --- a/src/builtin_set_color.h +++ b/src/builtin_set_color.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_set_color(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_source.cpp b/src/builtin_source.cpp index 0fc73326a..2c2aa3c91 100644 --- a/src/builtin_source.cpp +++ b/src/builtin_source.cpp @@ -22,7 +22,7 @@ /// The source builtin, sometimes called `.`. Evaluates the contents of a file in the current /// context. -maybe_t builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_source(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { ASSERT_IS_MAIN_THREAD(); const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); diff --git a/src/builtin_source.h b/src/builtin_source.h index e292727da..df6c6566d 100644 --- a/src/builtin_source.h +++ b/src/builtin_source.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_source(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_status.cpp b/src/builtin_status.cpp index ccb8200a5..8235527e1 100644 --- a/src/builtin_status.cpp +++ b/src/builtin_status.cpp @@ -82,7 +82,7 @@ const enum_map status_enum_map[] = { /// Values that may be returned from the test-feature option to status. enum { TEST_FEATURE_ON, TEST_FEATURE_OFF, TEST_FEATURE_NOT_RECOGNIZED }; -static maybe_t job_control_str_to_mode(const wchar_t *mode, wchar_t *cmd, +static maybe_t job_control_str_to_mode(const wchar_t *mode, const wchar_t *cmd, io_streams_t &streams) { if (std::wcscmp(mode, L"full") == 0) { return job_control_t::all; @@ -129,7 +129,7 @@ static const struct woption long_options[] = { {nullptr, 0, nullptr, 0}}; /// Remember the status subcommand and disallow selecting more than one status subcommand. -static bool set_status_cmd(wchar_t *const cmd, status_cmd_opts_t &opts, status_cmd_t sub_cmd, +static bool set_status_cmd(const wchar_t *cmd, status_cmd_opts_t &opts, status_cmd_t sub_cmd, io_streams_t &streams) { if (opts.status_cmd != STATUS_UNDEF) { wchar_t err_text[1024]; @@ -156,8 +156,8 @@ static void print_features(io_streams_t &streams) { } static int parse_cmd_opts(status_cmd_opts_t &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 argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -274,8 +274,8 @@ static int parse_cmd_opts(status_cmd_opts_t &opts, int *optind, //!OCLINT(high } /// The status builtin. Gives various status information on fish. -maybe_t builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_status(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); status_cmd_opts_t opts; diff --git a/src/builtin_status.h b/src/builtin_status.h index 01855e120..72e4a4b1b 100644 --- a/src/builtin_status.h +++ b/src/builtin_status.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_status(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index c0062f37a..e296bc6fc 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -197,7 +197,7 @@ struct options_t { //!OCLINT(too many fields) }; /// This handles the `--style=xxx` flag. -static int handle_flag_1(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_1(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { const wchar_t *cmd = argv[0]; @@ -221,7 +221,7 @@ static int handle_flag_1(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_N(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_N(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->no_newline_valid) { opts->no_newline = true; @@ -234,7 +234,7 @@ static int handle_flag_N(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_a(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_a(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->all_valid) { opts->all = true; @@ -247,7 +247,7 @@ static int handle_flag_a(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_c(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_c(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->chars_to_trim_valid) { opts->chars_to_trim = w.woptarg; @@ -265,7 +265,7 @@ static int handle_flag_c(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_e(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_e(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->end_valid) { opts->end = fish_wcstol(w.woptarg); @@ -285,7 +285,7 @@ static int handle_flag_e(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_f(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_f(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->filter_valid) { opts->filter = true; @@ -340,7 +340,7 @@ static int handle_flag_f(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_i(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_i(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->ignore_case_valid) { opts->ignore_case = true; @@ -353,7 +353,7 @@ static int handle_flag_i(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_l(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_l(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->length_valid) { opts->length = fish_wcstol(w.woptarg); @@ -373,7 +373,7 @@ static int handle_flag_l(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_m(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_m(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->max_valid) { opts->max = fish_wcstol(w.woptarg); @@ -390,7 +390,7 @@ static int handle_flag_m(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_n(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_n(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->count_valid) { opts->count = fish_wcstol(w.woptarg); @@ -416,7 +416,7 @@ static int handle_flag_n(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_q(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_q(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->quiet_valid) { opts->quiet = true; @@ -426,7 +426,7 @@ static int handle_flag_q(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_r(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_r(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->regex_valid) { opts->regex = true; @@ -439,7 +439,7 @@ static int handle_flag_r(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_s(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_s(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->start_valid) { opts->start = fish_wcstol(w.woptarg); @@ -456,7 +456,7 @@ static int handle_flag_s(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_v(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_v(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { if (opts->invert_valid) { opts->invert_match = true; @@ -466,7 +466,7 @@ static int handle_flag_v(wchar_t **argv, parser_t &parser, io_streams_t &streams return STATUS_INVALID_ARGS; } -static int handle_flag_w(wchar_t **argv, parser_t &parser, io_streams_t &streams, +static int handle_flag_w(const wchar_t **argv, parser_t &parser, io_streams_t &streams, const wgetopter_t &w, options_t *opts) { long width = 0; if (opts->width_valid) { @@ -553,7 +553,7 @@ static const std::unordered_map flag_to_function {'v', handle_flag_v}, {'w', handle_flag_w}, {1, handle_flag_1}}; /// Parse the arguments for flags recognized by a specific string subcommand. -static int parse_opts(options_t *opts, int *optind, int n_req_args, int argc, wchar_t **argv, +static int parse_opts(options_t *opts, int *optind, int n_req_args, int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { const wchar_t *cmd = argv[0]; wcstring short_opts = construct_short_opts(opts); @@ -606,7 +606,7 @@ static int parse_opts(options_t *opts, int *optind, int n_req_args, int argc, wc return STATUS_CMD_OK; } -static int string_escape(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_escape(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { options_t opts; opts.no_quoted_valid = true; opts.style_valid = true; @@ -634,7 +634,8 @@ static int string_escape(parser_t &parser, io_streams_t &streams, int argc, wcha DIE("should never reach this statement"); } -static int string_unescape(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_unescape(parser_t &parser, io_streams_t &streams, int argc, + const wchar_t **argv) { options_t opts; opts.no_quoted_valid = true; opts.style_valid = true; @@ -659,8 +660,8 @@ static int string_unescape(parser_t &parser, io_streams_t &streams, int argc, wc DIE("should never reach this statement"); } -static int string_join_maybe0(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv, - bool is_join0) { +static int string_join_maybe0(parser_t &parser, io_streams_t &streams, int argc, + const wchar_t **argv, bool is_join0) { options_t opts; opts.quiet_valid = true; int optind; @@ -688,15 +689,15 @@ static int string_join_maybe0(parser_t &parser, io_streams_t &streams, int argc, return nargs > 1 ? STATUS_CMD_OK : STATUS_CMD_ERROR; } -static int string_join(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_join(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { return string_join_maybe0(parser, streams, argc, argv, false /* is_join0 */); } -static int string_join0(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_join0(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { return string_join_maybe0(parser, streams, argc, argv, true /* is_join0 */); } -static int string_length(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_length(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { options_t opts; opts.quiet_valid = true; int optind; @@ -1120,8 +1121,8 @@ class pcre2_matcher_t : public string_matcher_t { } }; -static int string_match(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { - wchar_t *cmd = argv[0]; +static int string_match(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; options_t opts; opts.all_valid = true; @@ -1160,7 +1161,7 @@ static int string_match(parser_t &parser, io_streams_t &streams, int argc, wchar return matcher->match_count() > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR; } -static int string_pad(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_pad(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { options_t opts; opts.char_to_pad_valid = true; opts.right_valid = true; @@ -1384,7 +1385,7 @@ bool regex_replacer_t::replace_matches(const wcstring &arg) { return rc; } -static int string_replace(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_replace(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { options_t opts; opts.all_valid = true; opts.filter_valid = true; @@ -1414,9 +1415,9 @@ static int string_replace(parser_t &parser, io_streams_t &streams, int argc, wch return replacer->replace_count() > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR; } -static int string_split_maybe0(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv, - bool is_split0) { - wchar_t *cmd = argv[0]; +static int string_split_maybe0(parser_t &parser, io_streams_t &streams, int argc, + const wchar_t **argv, bool is_split0) { + const wchar_t *cmd = argv[0]; options_t opts; opts.quiet_valid = true; opts.right_valid = true; @@ -1502,15 +1503,15 @@ static int string_split_maybe0(parser_t &parser, io_streams_t &streams, int argc return split_count > arg_count ? STATUS_CMD_OK : STATUS_CMD_ERROR; } -static int string_split(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_split(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { return string_split_maybe0(parser, streams, argc, argv, false /* is_split0 */); } -static int string_split0(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_split0(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { return string_split_maybe0(parser, streams, argc, argv, true /* is_split0 */); } -static int string_collect(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_collect(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { options_t opts; opts.no_trim_newlines_valid = true; int optind; @@ -1557,7 +1558,7 @@ static wcstring wcsrepeat_until(const wcstring &to_repeat, size_t max) { return wcsrepeat(to_repeat, count) + to_repeat.substr(0, mod); } -static int string_repeat(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_repeat(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { options_t opts; opts.count_valid = true; opts.max_valid = true; @@ -1603,8 +1604,8 @@ static int string_repeat(parser_t &parser, io_streams_t &streams, int argc, wcha return all_empty ? STATUS_CMD_ERROR : STATUS_CMD_OK; } -static int string_sub(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { - wchar_t *cmd = argv[0]; +static int string_sub(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; options_t opts; opts.length_valid = true; @@ -1667,7 +1668,7 @@ static int string_sub(parser_t &parser, io_streams_t &streams, int argc, wchar_t return nsub > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR; } -static int string_trim(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_trim(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { options_t opts; opts.chars_to_trim_valid = true; opts.left_valid = true; @@ -1711,7 +1712,7 @@ static int string_trim(parser_t &parser, io_streams_t &streams, int argc, wchar_ } // A helper function for lower and upper. -static int string_transform(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv, +static int string_transform(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv, std::wint_t (*func)(std::wint_t)) { options_t opts; opts.quiet_valid = true; @@ -1737,12 +1738,12 @@ static int string_transform(parser_t &parser, io_streams_t &streams, int argc, w } /// Implementation of `string lower`. -static int string_lower(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_lower(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { return string_transform(parser, streams, argc, argv, std::towlower); } /// Implementation of `string upper`. -static int string_upper(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { +static int string_upper(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { return string_transform(parser, streams, argc, argv, std::towupper); } @@ -1750,7 +1751,7 @@ static int string_upper(parser_t &parser, io_streams_t &streams, int argc, wchar static constexpr const struct string_subcommand { const wchar_t *name; int (*handler)(parser_t &, io_streams_t &, int argc, //!OCLINT(unused param) - wchar_t **argv); //!OCLINT(unused param) + const wchar_t **argv); //!OCLINT(unused param) } string_subcommands[] = { {L"collect", &string_collect}, {L"escape", &string_escape}, {L"join", &string_join}, {L"join0", &string_join0}, {L"length", &string_length}, {L"lower", &string_lower}, @@ -1762,8 +1763,8 @@ static constexpr const struct string_subcommand { ASSERT_SORT_ORDER(string_subcommands, .name); /// The string builtin, for manipulating strings. -maybe_t builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_string(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); if (argc <= 1) { streams.err.append_format(BUILTIN_ERR_MISSING_SUBCMD, cmd); diff --git a/src/builtin_string.h b/src/builtin_string.h index 47c5e1227..224873405 100644 --- a/src/builtin_string.h +++ b/src/builtin_string.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_string(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_test.cpp b/src/builtin_test.cpp index 38125ed89..4c8c416f2 100644 --- a/src/builtin_test.cpp +++ b/src/builtin_test.cpp @@ -209,7 +209,7 @@ class test_parser { unique_ptr parse_just_a_string(unsigned int start, unsigned int end); static unique_ptr parse_args(const wcstring_list_t &args, wcstring &err, - wchar_t *program_name); + const wchar_t *program_name); }; struct range_t { @@ -549,7 +549,7 @@ unique_ptr test_parser::parse_expression(unsigned int start, unsigne } unique_ptr test_parser::parse_args(const wcstring_list_t &args, wcstring &err, - wchar_t *program_name) { + const wchar_t *program_name) { // Empty list and one-arg list should be handled by caller. assert(args.size() > 1); @@ -850,7 +850,7 @@ static bool unary_primary_evaluate(test_expressions::token_t token, const wcstri /// supports a more limited range of functionality. /// /// Return status is the final shell status, i.e. 0 for true, 1 for false and 2 for error. -maybe_t builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_test(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { UNUSED(parser); using namespace test_expressions; @@ -858,7 +858,7 @@ maybe_t builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **arg if (!argv[0]) return STATUS_INVALID_ARGS; // Whether we are invoked with bracket '[' or not. - wchar_t *program_name = argv[0]; + const wchar_t *program_name = argv[0]; const bool is_bracket = !std::wcscmp(program_name, L"["); size_t argc = 0; diff --git a/src/builtin_test.h b/src/builtin_test.h index 8b8935e86..9fadeadbf 100644 --- a/src/builtin_test.h +++ b/src/builtin_test.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_test(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_type.cpp b/src/builtin_type.cpp index a5e03ab6c..18994b884 100644 --- a/src/builtin_type.cpp +++ b/src/builtin_type.cpp @@ -43,10 +43,10 @@ static const struct woption long_options[] = {{L"help", no_argument, nullptr, 'h {L"quiet", no_argument, nullptr, 'q'}, {nullptr, 0, nullptr, 0}}; -static int parse_cmd_opts(type_cmd_opts_t &opts, int *optind, int argc, wchar_t **argv, +static int parse_cmd_opts(type_cmd_opts_t &opts, int *optind, int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) { UNUSED(parser); - wchar_t *cmd = argv[0]; + const wchar_t *cmd = argv[0]; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) { @@ -102,7 +102,7 @@ static int parse_cmd_opts(type_cmd_opts_t &opts, int *optind, int argc, wchar_t } /// Implementation of the builtin 'type'. -maybe_t builtin_type(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_type(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { UNUSED(parser); const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); diff --git a/src/builtin_type.h b/src/builtin_type.h index 99a32ba0a..93e98dc75 100644 --- a/src/builtin_type.h +++ b/src/builtin_type.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_type(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_type(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_ulimit.cpp b/src/builtin_ulimit.cpp index 72b114cfe..236c1bd5a 100644 --- a/src/builtin_ulimit.cpp +++ b/src/builtin_ulimit.cpp @@ -149,8 +149,8 @@ static int set_limit(int resource, int hard, int soft, rlim_t value, io_streams_ } /// The ulimit builtin, used for setting resource limits. -maybe_t builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { - wchar_t *cmd = argv[0]; +maybe_t builtin_ulimit(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { + const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); bool report_all = false; bool hard = false; diff --git a/src/builtin_ulimit.h b/src/builtin_ulimit.h index 1d64c39e4..a9d449d50 100644 --- a/src/builtin_ulimit.h +++ b/src/builtin_ulimit.h @@ -7,5 +7,5 @@ class parser_t; -maybe_t builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_ulimit(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/builtin_wait.cpp b/src/builtin_wait.cpp index e450ffafc..e8a8a68fe 100644 --- a/src/builtin_wait.cpp +++ b/src/builtin_wait.cpp @@ -170,7 +170,7 @@ static bool find_job_by_name(const wchar_t *proc, std::vector &ids, return found; } -maybe_t builtin_wait(parser_t &parser, io_streams_t &streams, wchar_t **argv) { +maybe_t builtin_wait(parser_t &parser, io_streams_t &streams, const wchar_t **argv) { int retval = STATUS_CMD_OK; const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); diff --git a/src/builtin_wait.h b/src/builtin_wait.h index a9945325b..260977679 100644 --- a/src/builtin_wait.h +++ b/src/builtin_wait.h @@ -7,5 +7,5 @@ class parser_t; struct io_streams_t; -maybe_t builtin_wait(parser_t &parser, io_streams_t &streams, wchar_t **argv); +maybe_t builtin_wait(parser_t &parser, io_streams_t &streams, const wchar_t **argv); #endif diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 52aa2e6c4..2f560f985 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -2768,31 +2768,24 @@ static void test_is_potential_path() { } /// Test the 'test' builtin. -maybe_t builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv); -static bool run_one_test_test(int expected, wcstring_list_t &lst, bool bracket) { +maybe_t builtin_test(parser_t &parser, io_streams_t &streams, const wchar_t **argv); +static bool run_one_test_test(int expected, const wcstring_list_t &lst, bool bracket) { parser_t &parser = parser_t::principal_parser(); - size_t i, count = lst.size(); - wchar_t **argv = new wchar_t *[count + 3]; - argv[0] = (wchar_t *)(bracket ? L"[" : L"test"); - for (i = 0; i < count; i++) { - argv[i + 1] = (wchar_t *)lst.at(i).c_str(); - } - if (bracket) { - argv[i + 1] = (wchar_t *)L"]"; - i++; - } - argv[i + 1] = NULL; + wcstring_list_t argv; + argv.push_back(bracket ? L"[" : L"test"); + argv.insert(argv.end(), lst.begin(), lst.end()); + if (bracket) argv.push_back(L"]"); + + null_terminated_array_t cargv(argv); + null_output_stream_t null{}; io_streams_t streams(null, null); - maybe_t result = builtin_test(parser, streams, argv); + maybe_t result = builtin_test(parser, streams, cargv.get()); if (result != expected) { std::wstring got = result ? std::to_wstring(result.value()) : L"nothing"; err(L"expected builtin_test() to return %d, got %s", expected, got.c_str()); } - - delete[] argv; - return result == expected; } @@ -5551,20 +5544,26 @@ static void test_pcre2_escape() { } } -maybe_t builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv); -static void run_one_string_test(const wchar_t *const *argv, int expected_rc, +maybe_t builtin_string(parser_t &parser, io_streams_t &streams, const wchar_t **argv); +static void run_one_string_test(const wchar_t *const *argv_raw, int expected_rc, const wchar_t *expected_out) { + // Copy to a null terminated array, as builtin_string may wish to rearrange our pointers. + wcstring_list_t argv_list(argv_raw, argv_raw + null_terminated_array_length(argv_raw)); + null_terminated_array_t argv(argv_list); + parser_t &parser = parser_t::principal_parser(); string_output_stream_t outs{}; null_output_stream_t errs{}; io_streams_t streams(outs, errs); streams.stdin_is_directly_redirected = false; // read from argv instead of stdin - maybe_t rc = builtin_string(parser, streams, const_cast(argv)); + maybe_t rc = builtin_string(parser, streams, argv.get()); + wcstring args; - for (int i = 0; argv[i] != NULL; i++) { - args += escape_string(argv[i], ESCAPE_ALL) + L' '; + for (const wcstring &arg : argv_list) { + args += escape_string(arg, ESCAPE_ALL) + L' '; } args.resize(args.size() - 1); + if (rc != expected_rc) { std::wstring got = rc ? std::to_wstring(rc.value()) : L"nothing"; err(L"Test failed on line %lu: [%ls]: expected return code %d but got %s", __LINE__, diff --git a/src/null_terminated_array.cpp b/src/null_terminated_array.cpp index e374668f2..2e10a9e36 100644 --- a/src/null_terminated_array.cpp +++ b/src/null_terminated_array.cpp @@ -1,7 +1,7 @@ #include "null_terminated_array.h" template -static CharT **make_null_terminated_array_helper( +static const CharT **make_null_terminated_array_helper( const std::vector > &argv) { size_t count = argv.size(); @@ -48,14 +48,14 @@ static CharT **make_null_terminated_array_helper( assert(reinterpret_cast(strings) - base == static_cast(pointers_allocation_len + strings_allocation_len)); - return reinterpret_cast(base); + return reinterpret_cast(base); } -wchar_t **make_null_terminated_array(const wcstring_list_t &lst) { +const wchar_t **make_null_terminated_array(const wcstring_list_t &lst) { return make_null_terminated_array_helper(lst); } -char **make_null_terminated_array(const std::vector &lst) { +const char **make_null_terminated_array(const std::vector &lst) { return make_null_terminated_array_helper(lst); } diff --git a/src/null_terminated_array.h b/src/null_terminated_array.h index b01ee5edf..b2690ee25 100644 --- a/src/null_terminated_array.h +++ b/src/null_terminated_array.h @@ -9,29 +9,32 @@ #include "common.h" -wchar_t **make_null_terminated_array(const wcstring_list_t &lst); -char **make_null_terminated_array(const std::vector &lst); +const wchar_t **make_null_terminated_array(const wcstring_list_t &lst); +const char **make_null_terminated_array(const std::vector &lst); + +/// \return the length of a null terminated array. +template +inline size_t null_terminated_array_length(const CharT *const *arr) { + if (!arr) return 0; + size_t len = 0; + while (arr[len] != nullptr) { + len++; + } + return len; +} // Helper class for managing a null-terminated array of null-terminated strings (of some char type). template class null_terminated_array_t { using string_list_t = std::vector>; - CharT **array{nullptr}; + const CharT **array{nullptr}; // No assignment or copying. void operator=(null_terminated_array_t rhs) = delete; null_terminated_array_t(const null_terminated_array_t &) = delete; - size_t size() const { - size_t len = 0; - if (array != nullptr) { - while (array[len] != nullptr) { - len++; - } - } - return len; - } + size_t size() const { return nullterm_array_length(array); } void free(void) { ::free(reinterpret_cast(array)); @@ -74,7 +77,7 @@ class null_terminated_array_t { string_list_t to_list() const { return to_list(array); } const CharT *const *get() const { return array; } - CharT **get() { return array; } + const CharT **get() { return array; } void clear() { this->free(); } }; diff --git a/src/proc.h b/src/proc.h index beb072997..e316c360b 100644 --- a/src/proc.h +++ b/src/proc.h @@ -233,7 +233,7 @@ class process_t { void set_argv(const wcstring_list_t &argv) { argv_array.set(argv); } /// Returns argv. - wchar_t **get_argv() { return argv_array.get(); } + const wchar_t **get_argv() { return argv_array.get(); } const wchar_t *const *get_argv() const { return argv_array.get(); } const null_terminated_array_t &get_argv_array() const { return argv_array; } diff --git a/src/wgetopt.cpp b/src/wgetopt.cpp index a73b85a08..2f052b0ff 100644 --- a/src/wgetopt.cpp +++ b/src/wgetopt.cpp @@ -65,11 +65,11 @@ // // `first_nonopt' and `last_nonopt' are relocated so that they describe the new indices of the // non-options in ARGV after they are moved. -void wgetopter_t::exchange(wchar_t **argv) { +void wgetopter_t::exchange(string_array_t argv) { int bottom = first_nonopt; int middle = last_nonopt; int top = woptind; - wchar_t *tem; + const wchar_t *tem{}; // Exchange the shorter segment with the far end of the longer segment. That puts the shorter // segment into the right place. It leaves the longer segment in the right place overall, but it @@ -139,7 +139,7 @@ void wgetopter_t::_wgetopt_initialize(const wchar_t *optstring) { // Advance to the next ARGV-element. int wgetopter_t::_advance_to_next_argv( //!OCLINT(high cyclomatic complexity) - int argc, wchar_t **argv, const struct woption *longopts) { + int argc, string_array_t argv, const struct woption *longopts) { if (ordering == PERMUTE) { // If we have just processed some options following some non-options, exchange them so // that the options come first. @@ -196,7 +196,7 @@ int wgetopter_t::_advance_to_next_argv( //!OCLINT(high cyclomatic complexity) } // Check for a matching short opt. -int wgetopter_t::_handle_short_opt(int argc, wchar_t **argv) { +int wgetopter_t::_handle_short_opt(int argc, string_array_t argv) { // Look at and handle the next short option-character. wchar_t c = *nextchar++; const wchar_t *temp = std::wcschr(shortopts, c); @@ -254,8 +254,9 @@ int wgetopter_t::_handle_short_opt(int argc, wchar_t **argv) { return c; } -void wgetopter_t::_update_long_opt(int argc, wchar_t **argv, const struct woption *pfound, - wchar_t *nameend, int *longind, int option_index, int *retval) { +void wgetopter_t::_update_long_opt(int argc, string_array_t argv, const struct woption *pfound, + const wchar_t *nameend, int *longind, int option_index, + int *retval) { woptind++; if (*nameend) { // Don't test has_arg with >, because some C compilers don't allow it to be used on @@ -301,8 +302,8 @@ void wgetopter_t::_update_long_opt(int argc, wchar_t **argv, const struct woptio // Find a matching long opt. const struct woption *wgetopter_t::_find_matching_long_opt(const struct woption *longopts, - wchar_t *nameend, int *exact, int *ambig, - int *indfound) const { + const wchar_t *nameend, int *exact, + int *ambig, int *indfound) const { const struct woption *pfound = nullptr; int option_index = 0; @@ -330,13 +331,13 @@ const struct woption *wgetopter_t::_find_matching_long_opt(const struct woption } // Check for a matching long opt. -bool wgetopter_t::_handle_long_opt(int argc, wchar_t **argv, const struct woption *longopts, +bool wgetopter_t::_handle_long_opt(int argc, string_array_t argv, const struct woption *longopts, int *longind, int long_only, int *retval) { int exact = 0; int ambig = 0; int indfound = 0; - wchar_t *nameend; + const wchar_t *nameend; for (nameend = nextchar; *nameend && *nameend != '='; nameend++) ; //!OCLINT(empty body) @@ -419,7 +420,7 @@ bool wgetopter_t::_handle_long_opt(int argc, wchar_t **argv, const struct woptio // long-named option has been found by the most recent call. // // If LONG_ONLY is nonzero, '-' as well as '--' can introduce long-named options. -int wgetopter_t::_wgetopt_internal(int argc, wchar_t **argv, const wchar_t *optstring, +int wgetopter_t::_wgetopt_internal(int argc, string_array_t argv, const wchar_t *optstring, const struct woption *longopts, int *longind, int long_only) { if (!initialized) _wgetopt_initialize(optstring); woptarg = nullptr; @@ -452,7 +453,7 @@ int wgetopter_t::_wgetopt_internal(int argc, wchar_t **argv, const wchar_t *opts return _handle_short_opt(argc, argv); } -int wgetopter_t::wgetopt_long(int argc, wchar_t **argv, const wchar_t *options, +int wgetopter_t::wgetopt_long(int argc, string_array_t argv, const wchar_t *options, const struct woption *long_options, int *opt_index) { return _wgetopt_internal(argc, argv, options, long_options, opt_index, 0); } diff --git a/src/wgetopt.h b/src/wgetopt.h index 496c27285..2ee594644 100644 --- a/src/wgetopt.h +++ b/src/wgetopt.h @@ -43,28 +43,17 @@ Cambridge, MA 02139, USA. */ #include +/// Instanced getopt() wrapper. class wgetopter_t { - private: - void exchange(wchar_t **argv); - void _wgetopt_initialize(const wchar_t *optstring); - int _wgetopt_internal(int argc, wchar_t **argv, const wchar_t *optstring, - const struct woption *longopts, int *longind, int long_only); - int _advance_to_next_argv(int argc, wchar_t **argv, const struct woption *longopts); - int _handle_short_opt(int argc, wchar_t **argv); - bool _handle_long_opt(int argc, wchar_t **argv, const struct woption *longopts, int *longind, - int long_only, int *retval); - const struct woption *_find_matching_long_opt(const struct woption *longopts, wchar_t *nameend, - int *exact, int *ambig, int *indfound) const; - void _update_long_opt(int argc, wchar_t **argv, const struct woption *pfound, wchar_t *nameend, - int *longind, int option_index, int *retval); - bool initialized = false; - bool missing_arg_return_colon = false; - public: + /// Note wgetopter expects an mutable array of const strings. It modifies the order of the + /// strings, but not their contents. + using string_array_t = const wchar_t **; + // For communication from `getopt' to the caller. When `getopt' finds an option that takes an // argument, the argument value is returned here. Also, when `ordering' is RETURN_IN_ORDER, each // non-option ARGV-element is returned here. - wchar_t *woptarg = nullptr; + const wchar_t *woptarg = nullptr; const wchar_t *shortopts = nullptr; @@ -73,7 +62,7 @@ class wgetopter_t { // // If this is zero, or a null string, it means resume the scan by advancing to the next // ARGV-element. - wchar_t *nextchar = nullptr; + const wchar_t *nextchar = nullptr; // Index in ARGV of the next element to be scanned. This is used for communication to and from // the caller and for communication between successive calls to `getopt'. @@ -128,8 +117,25 @@ class wgetopter_t { wgetopter_t() {} - int wgetopt_long(int argc, wchar_t **argv, const wchar_t *options, + int wgetopt_long(int argc, string_array_t argv, const wchar_t *options, const struct woption *long_options, int *opt_index); + + private: + void exchange(string_array_t argv); + void _wgetopt_initialize(const wchar_t *optstring); + int _wgetopt_internal(int argc, string_array_t argv, const wchar_t *optstring, + const struct woption *longopts, int *longind, int long_only); + int _advance_to_next_argv(int argc, string_array_t argv, const struct woption *longopts); + int _handle_short_opt(int argc, string_array_t argv); + bool _handle_long_opt(int argc, string_array_t argv, const struct woption *longopts, + int *longind, int long_only, int *retval); + const struct woption *_find_matching_long_opt(const struct woption *longopts, + const wchar_t *nameend, int *exact, int *ambig, + int *indfound) const; + void _update_long_opt(int argc, string_array_t argv, const struct woption *pfound, + const wchar_t *nameend, int *longind, int option_index, int *retval); + bool initialized = false; + bool missing_arg_return_colon = false; }; /// Describe the long-named options requested by the application. The LONG_OPTIONS argument to