diff --git a/src/complete.cpp b/src/complete.cpp index 37929e372..cd2652cc6 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -665,11 +665,11 @@ void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool if (use_command) { // Append all possible executables - expand_error_t result = expand_string(str_cmd, &this->completions, - EXPAND_SPECIAL_FOR_COMMAND | EXPAND_FOR_COMPLETIONS | - EXECUTABLES_ONLY | this->expand_flags(), - vars, NULL); - if (result != EXPAND_ERROR && this->wants_descriptions()) { + expand_result_t result = expand_string(str_cmd, &this->completions, + EXPAND_SPECIAL_FOR_COMMAND | EXPAND_FOR_COMPLETIONS | + EXECUTABLES_ONLY | this->expand_flags(), + vars, NULL); + if (result != expand_result_t::error && this->wants_descriptions()) { this->complete_cmd_desc(str_cmd); } } @@ -677,7 +677,7 @@ void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool if (use_implicit_cd) { // We don't really care if this succeeds or fails. If it succeeds this->completions will be // updated with choices for the user. - expand_error_t ignore = + expand_result_t ignore = // Append all matching directories expand_string(str_cmd, &this->completions, EXPAND_FOR_COMPLETIONS | DIRECTORIES_ONLY | this->expand_flags(), vars, @@ -1092,7 +1092,8 @@ void completer_t::complete_param_expand(const wcstring &str, bool do_file, // See #4954. const wcstring sep_string = wcstring(str, sep_index + 1); std::vector local_completions; - if (expand_string(sep_string, &local_completions, flags, vars, NULL) == EXPAND_ERROR) { + if (expand_string(sep_string, &local_completions, flags, vars, NULL) == + expand_result_t::error) { debug(3, L"Error while expanding string '%ls'", sep_string.c_str()); } @@ -1111,7 +1112,7 @@ void completer_t::complete_param_expand(const wcstring &str, bool do_file, // consider relaxing this if there was a preceding double-dash argument. if (string_prefixes_string(L"-", str)) flags &= ~EXPAND_FUZZY_MATCH; - if (expand_string(str, &this->completions, flags, vars, NULL) == EXPAND_ERROR) { + if (expand_string(str, &this->completions, flags, vars, NULL) == expand_result_t::error) { debug(3, L"Error while expanding string '%ls'", str.c_str()); } } diff --git a/src/expand.cpp b/src/expand.cpp index 800cafc6a..9e9f5c8cd 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -470,8 +470,8 @@ static bool expand_variables(wcstring instr, std::vector *out, siz } /// Perform brace expansion. -static expand_error_t expand_braces(const wcstring &instr, expand_flags_t flags, - std::vector *out, parse_error_list_t *errors) { +static expand_result_t expand_braces(const wcstring &instr, expand_flags_t flags, + std::vector *out, parse_error_list_t *errors) { bool syntax_error = false; int brace_count = 0; @@ -542,12 +542,12 @@ static expand_error_t expand_braces(const wcstring &instr, expand_flags_t flags, if (syntax_error) { append_syntax_error(errors, SOURCE_LOCATION_UNKNOWN, _(L"Mismatched braces")); - return EXPAND_ERROR; + return expand_result_t::error; } if (brace_begin == NULL) { append_completion(out, instr); - return EXPAND_OK; + return expand_result_t::ok; } length_preceding_braces = (brace_begin - in); @@ -585,7 +585,7 @@ static expand_error_t expand_braces(const wcstring &instr, expand_flags_t flags, brace_count--; } } - return EXPAND_OK; + return expand_result_t::ok; } /// Perform cmdsubst expansion. @@ -880,24 +880,24 @@ class expander_t { /// An expansion stage is a member function pointer. /// It accepts the input string (transferring ownership) and returns the list of output /// completions by reference. It may return an error, which halts expansion. - using stage_t = expand_error_t (expander_t::*)(wcstring, std::vector *); + using stage_t = expand_result_t (expander_t::*)(wcstring, std::vector *); - expand_error_t stage_cmdsubst(wcstring input, std::vector *out); - expand_error_t stage_variables(wcstring input, std::vector *out); - expand_error_t stage_braces(wcstring input, std::vector *out); - expand_error_t stage_home_and_self(wcstring input, std::vector *out); - expand_error_t stage_wildcards(wcstring path_to_expand, std::vector *out); + expand_result_t stage_cmdsubst(wcstring input, std::vector *out); + expand_result_t stage_variables(wcstring input, std::vector *out); + expand_result_t stage_braces(wcstring input, std::vector *out); + expand_result_t stage_home_and_self(wcstring input, std::vector *out); + expand_result_t stage_wildcards(wcstring path_to_expand, std::vector *out); expander_t(const environment_t &vars, expand_flags_t flags, parse_error_list_t *errors) : vars(vars), flags(flags), errors(errors) {} public: - static expand_error_t expand_string(wcstring input, std::vector *out_completions, - expand_flags_t flags, const environment_t &vars, - parse_error_list_t *errors); + static expand_result_t expand_string(wcstring input, std::vector *out_completions, + expand_flags_t flags, const environment_t &vars, + parse_error_list_t *errors); }; -expand_error_t expander_t::stage_cmdsubst(wcstring input, std::vector *out) { +expand_result_t expander_t::stage_cmdsubst(wcstring input, std::vector *out) { if (EXPAND_SKIP_CMDSUBST & flags) { size_t cur = 0, start = 0, end; switch (parse_util_locate_cmdsubst_range(input, &cur, nullptr, &start, &end, true)) { @@ -908,17 +908,17 @@ expand_error_t expander_t::stage_cmdsubst(wcstring input, std::vector *out) { +expand_result_t expander_t::stage_variables(wcstring input, std::vector *out) { // We accept incomplete strings here, since complete uses expand_string to expand incomplete // strings from the commandline. wcstring next; @@ -934,29 +934,29 @@ expand_error_t expander_t::stage_variables(wcstring input, std::vector *out) { +expand_result_t expander_t::stage_braces(wcstring input, std::vector *out) { UNUSED(vars); return expand_braces(input, flags, out, errors); } -expand_error_t expander_t::stage_home_and_self(wcstring input, std::vector *out) { +expand_result_t expander_t::stage_home_and_self(wcstring input, std::vector *out) { if (!(EXPAND_SKIP_HOME_DIRECTORIES & flags)) { expand_home_directory(input, vars); } expand_percent_self(input); append_completion(out, std::move(input)); - return EXPAND_OK; + return expand_result_t::ok; } -expand_error_t expander_t::stage_wildcards(wcstring path_to_expand, - std::vector *out) { - expand_error_t result = EXPAND_OK; +expand_result_t expander_t::stage_wildcards(wcstring path_to_expand, + std::vector *out) { + expand_result_t result = expand_result_t::ok; remove_internal_separator(&path_to_expand, flags & EXPAND_SKIP_WILDCARDS); const bool has_wildcard = wildcard_has(path_to_expand, true /* internal, i.e. ANY_STRING */); @@ -1017,17 +1017,17 @@ expand_error_t expander_t::stage_wildcards(wcstring path_to_expand, } } - result = EXPAND_WILDCARD_NO_MATCH; + result = expand_result_t::wildcard_no_match; std::vector expanded; for (size_t wd_idx = 0; wd_idx < effective_working_dirs.size(); wd_idx++) { int local_wc_res = wildcard_expand_string( path_to_expand, effective_working_dirs.at(wd_idx), flags, &expanded); if (local_wc_res > 0) { // Something matched,so overall we matched. - result = EXPAND_WILDCARD_MATCH; + result = expand_result_t::wildcard_match; } else if (local_wc_res < 0) { // Cancellation - result = EXPAND_ERROR; + result = expand_result_t::error; break; } } @@ -1045,13 +1045,14 @@ expand_error_t expander_t::stage_wildcards(wcstring path_to_expand, return result; } -expand_error_t expander_t::expand_string(wcstring input, std::vector *out_completions, - expand_flags_t flags, const environment_t &vars, - parse_error_list_t *errors) { +expand_result_t expander_t::expand_string(wcstring input, + std::vector *out_completions, + expand_flags_t flags, const environment_t &vars, + parse_error_list_t *errors) { // Early out. If we're not completing, and there's no magic in the input, we're done. if (!(flags & EXPAND_FOR_COMPLETIONS) && expand_is_clean(input)) { append_completion(out_completions, std::move(input)); - return EXPAND_OK; + return expand_result_t::ok; } expander_t expand(vars, flags, errors); @@ -1065,18 +1066,18 @@ expand_error_t expander_t::expand_string(wcstring input, std::vector completions, output_storage; append_completion(&completions, input); - expand_error_t total_result = EXPAND_OK; + expand_result_t total_result = expand_result_t::ok; for (stage_t stage : stages) { for (completion_t &comp : completions) { - expand_error_t this_result = + expand_result_t this_result = (expand.*stage)(std::move(comp.completion), &output_storage); // If this_result was no match, but total_result is that we have a match, then don't // change it. - if (!(this_result == EXPAND_WILDCARD_NO_MATCH && - total_result == EXPAND_WILDCARD_MATCH)) { + if (!(this_result == expand_result_t::wildcard_no_match && + total_result == expand_result_t::wildcard_match)) { total_result = this_result; } - if (total_result == EXPAND_ERROR) { + if (total_result == expand_result_t::error) { break; } } @@ -1084,12 +1085,12 @@ expand_error_t expander_t::expand_string(wcstring input, std::vector *out_completions, - expand_flags_t flags, const environment_t &vars, - parse_error_list_t *errors) { +expand_result_t expand_string(wcstring input, std::vector *out_completions, + expand_flags_t flags, const environment_t &vars, + parse_error_list_t *errors) { return expander_t::expand_string(std::move(input), out_completions, flags, vars, errors); } @@ -1116,7 +1117,8 @@ bool expand_one(wcstring &string, expand_flags_t flags, const environment_t &var return true; } - if (expand_string(string, &completions, flags | EXPAND_NO_DESCRIPTIONS, vars, errors) && + if (expand_string(string, &completions, flags | EXPAND_NO_DESCRIPTIONS, vars, errors) != + expand_result_t::error && completions.size() == 1) { string = std::move(completions.at(0).completion); return true; @@ -1124,21 +1126,21 @@ bool expand_one(wcstring &string, expand_flags_t flags, const environment_t &var return false; } -expand_error_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars, - wcstring *out_cmd, wcstring_list_t *out_args, - parse_error_list_t *errors) { +expand_result_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars, + wcstring *out_cmd, wcstring_list_t *out_args, + parse_error_list_t *errors) { // Fast path. if (expand_is_clean(instr)) { *out_cmd = instr; - return EXPAND_OK; + return expand_result_t::ok; } std::vector completions; - expand_error_t expand_err = expand_string( + expand_result_t expand_err = expand_string( instr, &completions, EXPAND_SKIP_CMDSUBST | EXPAND_NO_DESCRIPTIONS | EXPAND_SKIP_JOBS, vars, errors); - if (expand_err == EXPAND_OK || expand_err == EXPAND_WILDCARD_MATCH) { + if (expand_err == expand_result_t::ok || expand_err == expand_result_t::wildcard_match) { // The first completion is the command, any remaning are arguments. bool first = true; for (auto &comp : completions) { diff --git a/src/expand.h b/src/expand.h index 3970ae0b2..46ac206fe 100644 --- a/src/expand.h +++ b/src/expand.h @@ -89,15 +89,15 @@ enum { }; /// These are the possible return values for expand_string. Note how zero value is the only error. -enum expand_error_t { +enum class expand_result_t { /// Error - EXPAND_ERROR, + error, /// Ok - EXPAND_OK, + ok, /// Ok, a wildcard in the string matched no files. - EXPAND_WILDCARD_NO_MATCH, + wildcard_no_match, /// Ok, a wildcard in the string matched a file. - EXPAND_WILDCARD_MATCH + wildcard_match, }; /// The string represented by PROCESS_EXPAND_SELF @@ -118,12 +118,12 @@ enum expand_error_t { /// \param vars variables used during expansion. /// \param errors Resulting errors, or NULL to ignore /// -/// \return One of EXPAND_OK, EXPAND_ERROR, EXPAND_WILDCARD_MATCH and EXPAND_WILDCARD_NO_MATCH. -/// EXPAND_WILDCARD_NO_MATCH and EXPAND_WILDCARD_MATCH are normal exit conditions used only on +/// \return An expand_result_t. +/// wildcard_no_match and wildcard_match are normal exit conditions used only on /// strings containing wildcards to tell if the wildcard produced any matches. -__warn_unused expand_error_t expand_string(wcstring input, std::vector *output, - expand_flags_t flags, const environment_t &vars, - parse_error_list_t *errors); +__warn_unused expand_result_t expand_string(wcstring input, std::vector *output, + expand_flags_t flags, const environment_t &vars, + parse_error_list_t *errors); /// expand_one is identical to expand_string, except it will fail if in expands to more than one /// string. This is used for expanding command names. @@ -143,9 +143,9 @@ bool expand_one(wcstring &inout_str, expand_flags_t flags, const environment_t & /// that API does not distinguish between expansion resulting in an empty command (''), and /// expansion resulting in no command (e.g. unset variable). // \return an expand error. -expand_error_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars, - wcstring *out_cmd, wcstring_list_t *out_args, - parse_error_list_t *errors = NULL); +expand_result_t expand_to_command_and_args(const wcstring &instr, const environment_t &vars, + wcstring *out_cmd, wcstring_list_t *out_args, + parse_error_list_t *errors = NULL); /// Convert the variable value to a human readable form, i.e. escape things, handle arrays, etc. /// Suitable for pretty-printing. diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 5741b20b6..f0f69647c 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -1592,7 +1592,7 @@ static bool expand_test(const wchar_t *in, expand_flags_t flags, ...) { wchar_t *arg; parse_error_list_t errors; - if (expand_string(in, &output, flags, pwd_environment_t{}, &errors) == EXPAND_ERROR) { + if (expand_string(in, &output, flags, pwd_environment_t{}, &errors) == expand_result_t::error) { if (errors.empty()) { err(L"Bug: Parse error reported but no error text found."); } else { diff --git a/src/highlight.cpp b/src/highlight.cpp index f9e9ea158..fe993b121 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -344,8 +344,8 @@ static bool plain_statement_get_expanded_command(const wcstring &src, // Get the command. Try expanding it. If we cannot, it's an error. maybe_t cmd = command_for_plain_statement(stmt, src); if (!cmd) return false; - expand_error_t err = expand_to_command_and_args(*cmd, vars, out_cmd, nullptr); - return err == EXPAND_OK || err == EXPAND_WILDCARD_MATCH; + expand_result_t err = expand_to_command_and_args(*cmd, vars, out_cmd, nullptr); + return err == expand_result_t::ok || err == expand_result_t::wildcard_match; } rgb_color_t highlight_get_color(const highlight_spec_t &highlight, bool is_background) { diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index 2afbc2155..018a35481 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -449,21 +449,21 @@ parse_execution_result_t parse_execution_context_t::run_switch_statement( // Expand it. We need to offset any errors by the position of the string. std::vector switch_values_expanded; parse_error_list_t errors; - int expand_ret = expand_string(switch_value, &switch_values_expanded, EXPAND_NO_DESCRIPTIONS, - parser->vars(), &errors); + auto expand_ret = expand_string(switch_value, &switch_values_expanded, EXPAND_NO_DESCRIPTIONS, + parser->vars(), &errors); parse_error_offset_source_start(&errors, switch_value_n.source_range()->start); switch (expand_ret) { - case EXPAND_ERROR: { + case expand_result_t::error: { result = report_errors(errors); break; } - case EXPAND_WILDCARD_NO_MATCH: { + case expand_result_t::wildcard_no_match: { result = report_unmatched_wildcard_error(switch_value_n); break; } - case EXPAND_WILDCARD_MATCH: - case EXPAND_OK: { + case expand_result_t::wildcard_match: + case expand_result_t::ok: { break; } default: { @@ -746,15 +746,15 @@ parse_execution_result_t parse_execution_context_t::expand_command( wcstring unexp_cmd = *command_for_plain_statement(statement, pstree->src); // Expand the string to produce completions, and report errors. - expand_error_t expand_err = + expand_result_t expand_err = expand_to_command_and_args(unexp_cmd, parser->vars(), out_cmd, out_args, &errors); - if (expand_err == EXPAND_ERROR) { + if (expand_err == expand_result_t::error) { proc_set_last_statuses(statuses_t::just(STATUS_ILLEGAL_CMD)); return report_errors(errors); - } else if (expand_err == EXPAND_WILDCARD_NO_MATCH) { + } else if (expand_err == expand_result_t::wildcard_no_match) { return report_unmatched_wildcard_error(statement); } - assert(expand_err == EXPAND_OK || expand_err == EXPAND_WILDCARD_MATCH); + assert(expand_err == expand_result_t::ok || expand_err == expand_result_t::wildcard_match); // Complain if the resulting expansion was empty, or expanded to an empty string. if (out_cmd->empty()) { @@ -906,15 +906,15 @@ parse_execution_result_t parse_execution_context_t::expand_arguments_from_nodes( // Expand this string. parse_error_list_t errors; arg_expanded.clear(); - int expand_ret = + auto expand_ret = expand_string(arg_str, &arg_expanded, EXPAND_NO_DESCRIPTIONS, parser->vars(), &errors); parse_error_offset_source_start(&errors, arg_node.source_range()->start); switch (expand_ret) { - case EXPAND_ERROR: { + case expand_result_t::error: { this->report_errors(errors); return parse_execution_errored; } - case EXPAND_WILDCARD_NO_MATCH: { + case expand_result_t::wildcard_no_match: { if (glob_behavior == failglob) { // Report the unmatched wildcard error and stop processing. report_unmatched_wildcard_error(arg_node); @@ -922,8 +922,8 @@ parse_execution_result_t parse_execution_context_t::expand_arguments_from_nodes( } break; } - case EXPAND_WILDCARD_MATCH: - case EXPAND_OK: { + case expand_result_t::wildcard_match: + case expand_result_t::ok: { break; } default: { diff --git a/src/parse_util.cpp b/src/parse_util.cpp index 2a0054513..a575cebd8 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -1134,7 +1134,7 @@ static bool detect_errors_in_plain_statement(const wcstring &buff_src, wcstring command; // Check that we can expand the command. if (expand_to_command_and_args(*unexp_command, null_environment_t{}, &command, nullptr, - parse_errors) == EXPAND_ERROR) { + parse_errors) == expand_result_t::error) { errored = true; parse_error_offset_source_start(parse_errors, source_start); } diff --git a/src/parser.cpp b/src/parser.cpp index 1a7e9ef3e..364791bb1 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -332,7 +332,7 @@ void parser_t::expand_argument_list(const wcstring &arg_list_src, expand_flags_t while (auto arg = arg_list.next_in_list()) { const wcstring arg_src = arg.get_source(arg_list_src); if (expand_string(arg_src, output_arg_list, eflags, vars, NULL /* errors */) == - EXPAND_ERROR) { + expand_result_t::error) { break; // failed to expand a string } }