2016-04-29 11:10:27 +08:00
|
|
|
// Prototypes for string expansion functions. These functions perform several kinds of parameter
|
|
|
|
// expansion. There are a lot of issues with regards to memory allocation. Overall, these functions
|
|
|
|
// would benefit from using a more clever memory allocation scheme, perhaps an evil combination of
|
|
|
|
// talloc, string buffers and reference counting.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_EXPAND_H
|
|
|
|
#define FISH_EXPAND_H
|
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "config.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
|
2022-08-21 14:14:48 +08:00
|
|
|
#include <initializer_list>
|
2018-10-16 14:36:40 +08:00
|
|
|
#include <map>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
#include "common.h"
|
2019-04-26 02:22:17 +08:00
|
|
|
#include "enum_set.h"
|
2018-09-24 13:47:38 +08:00
|
|
|
#include "maybe.h"
|
2014-03-22 08:13:33 +08:00
|
|
|
#include "parse_constants.h"
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2017-08-06 06:08:39 +08:00
|
|
|
class env_var_t;
|
2018-09-11 13:29:52 +08:00
|
|
|
class environment_t;
|
2020-01-16 09:14:47 +08:00
|
|
|
class operation_context_t;
|
2017-08-06 06:08:39 +08:00
|
|
|
|
2019-04-26 02:34:49 +08:00
|
|
|
/// Set of flags controlling expansions.
|
2019-04-26 02:22:17 +08:00
|
|
|
enum class expand_flag {
|
2019-04-26 02:34:49 +08:00
|
|
|
/// Skip command substitutions.
|
|
|
|
skip_cmdsubst,
|
|
|
|
/// Skip variable expansion.
|
|
|
|
skip_variables,
|
|
|
|
/// Skip wildcard expansion.
|
|
|
|
skip_wildcards,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// The expansion is being done for tab or auto completions. Returned completions may have the
|
|
|
|
/// wildcard as a prefix instead of a match.
|
2019-04-26 02:34:49 +08:00
|
|
|
for_completions,
|
2017-04-09 12:05:18 +08:00
|
|
|
/// Only match files that are executable by the current user.
|
2019-04-26 02:34:49 +08:00
|
|
|
executables_only,
|
2017-04-09 12:05:18 +08:00
|
|
|
/// Only match directories.
|
2019-04-26 02:34:49 +08:00
|
|
|
directories_only,
|
2020-09-28 07:49:12 +08:00
|
|
|
/// Generate descriptions, stored in the description field of completions.
|
|
|
|
gen_descriptions,
|
2021-11-28 10:42:25 +08:00
|
|
|
/// Un-expand home directories to tildes after.
|
|
|
|
preserve_home_tildes,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Allow fuzzy matching.
|
2019-04-26 02:34:49 +08:00
|
|
|
fuzzy_match,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Disallow directory abbreviations like /u/l/b for /usr/local/bin. Only applicable if
|
2019-04-26 02:34:49 +08:00
|
|
|
/// fuzzy_match is set.
|
|
|
|
no_fuzzy_directories,
|
2022-09-13 06:33:29 +08:00
|
|
|
/// Allows matching a leading dot even if the wildcard does not contain one.
|
|
|
|
/// By default, wildcards only match a leading dot literally; this is why e.g. '*' does not
|
|
|
|
/// match hidden files.
|
|
|
|
allow_nonliteral_leading_dot,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Do expansions specifically to support cd. This means using CDPATH as a list of potential
|
2018-11-04 04:30:55 +08:00
|
|
|
/// working directories, and to use logical instead of physical paths.
|
2019-04-26 02:34:49 +08:00
|
|
|
special_for_cd,
|
2018-01-09 05:36:20 +08:00
|
|
|
/// Do expansions specifically for cd autosuggestion. This is to differentiate between cd
|
|
|
|
/// completions and cd autosuggestions.
|
2019-04-26 02:34:49 +08:00
|
|
|
special_for_cd_autosuggestion,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Do expansions specifically to support external command completions. This means using PATH as
|
2016-06-06 09:46:04 +08:00
|
|
|
/// a list of potential working directories.
|
2019-04-26 02:34:49 +08:00
|
|
|
special_for_command,
|
2019-04-26 02:22:17 +08:00
|
|
|
|
|
|
|
COUNT,
|
2012-02-25 04:13:35 +08:00
|
|
|
};
|
2019-04-26 02:22:17 +08:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct enum_info_t<expand_flag> {
|
|
|
|
static constexpr auto count = expand_flag::COUNT;
|
|
|
|
};
|
|
|
|
|
|
|
|
using expand_flags_t = enum_set_t<expand_flag>;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-02 08:27:14 +08:00
|
|
|
class completion_t;
|
2020-01-16 08:13:41 +08:00
|
|
|
using completion_list_t = std::vector<completion_t>;
|
2020-12-02 05:19:34 +08:00
|
|
|
class completion_receiver_t;
|
2012-01-17 00:56:47 +08:00
|
|
|
|
2019-09-15 04:17:22 +08:00
|
|
|
enum : wchar_t {
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Character representing a home directory.
|
2016-01-22 11:56:39 +08:00
|
|
|
HOME_DIRECTORY = EXPAND_RESERVED_BASE,
|
2018-10-11 05:26:29 +08:00
|
|
|
/// Character representing process expansion for %self.
|
|
|
|
PROCESS_EXPAND_SELF,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Character representing variable expansion.
|
2012-11-19 08:30:30 +08:00
|
|
|
VARIABLE_EXPAND,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Character representing variable expansion into a single element.
|
2012-11-19 08:30:30 +08:00
|
|
|
VARIABLE_EXPAND_SINGLE,
|
2019-09-23 06:33:08 +08:00
|
|
|
/// Character representing the start of a bracket expansion.
|
2018-03-11 03:16:07 +08:00
|
|
|
BRACE_BEGIN,
|
2019-09-23 06:33:08 +08:00
|
|
|
/// Character representing the end of a bracket expansion.
|
2018-03-11 03:16:07 +08:00
|
|
|
BRACE_END,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Character representing separation between two bracket elements.
|
2018-03-11 03:16:07 +08:00
|
|
|
BRACE_SEP,
|
2018-03-12 11:02:43 +08:00
|
|
|
/// Character that takes the place of any whitespace within non-quoted text in braces
|
|
|
|
BRACE_SPACE,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Separate subtokens in a token with this character.
|
2012-11-19 08:30:30 +08:00
|
|
|
INTERNAL_SEPARATOR,
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Character representing an empty variable expansion. Only used transitively while expanding
|
|
|
|
/// variables.
|
2014-08-21 12:19:08 +08:00
|
|
|
VARIABLE_EXPAND_EMPTY,
|
2019-11-25 19:03:25 +08:00
|
|
|
/// This is a special pseudo-char that is not used other than to mark the end of the the special
|
2016-04-29 11:10:27 +08:00
|
|
|
/// characters so we can sanity check the enum range.
|
2020-03-01 07:56:52 +08:00
|
|
|
EXPAND_SENTINEL
|
2016-01-22 11:56:39 +08:00
|
|
|
};
|
2005-12-04 00:43:56 +08:00
|
|
|
|
2020-01-23 03:51:40 +08:00
|
|
|
/// These are the possible return values for expand_string.
|
2020-01-24 09:34:46 +08:00
|
|
|
struct expand_result_t {
|
|
|
|
enum result_t {
|
|
|
|
/// There was an error, for example, unmatched braces.
|
|
|
|
error,
|
|
|
|
/// Expansion succeeded.
|
|
|
|
ok,
|
|
|
|
/// Expansion was cancelled (e.g. control-C).
|
|
|
|
cancel,
|
|
|
|
/// Expansion succeeded, but a wildcard in the string matched no files,
|
|
|
|
/// so the output is empty.
|
|
|
|
wildcard_no_match,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The result of expansion.
|
|
|
|
result_t result;
|
|
|
|
|
|
|
|
/// If expansion resulted in an error, this is an appropriate value with which to populate
|
|
|
|
/// $status.
|
|
|
|
int status{0};
|
|
|
|
|
|
|
|
/* implicit */ expand_result_t(result_t result) : result(result) {}
|
|
|
|
|
|
|
|
/// operator== allows for comparison against result_t values.
|
|
|
|
bool operator==(result_t rhs) const { return result == rhs; }
|
|
|
|
bool operator!=(result_t rhs) const { return !(*this == rhs); }
|
|
|
|
|
|
|
|
/// Make an error value with the given status.
|
|
|
|
static expand_result_t make_error(int status) {
|
|
|
|
assert(status != 0 && "status cannot be 0 for an error result");
|
|
|
|
expand_result_t result(error);
|
|
|
|
result.status = status;
|
|
|
|
return result;
|
|
|
|
}
|
2012-05-09 17:33:42 +08:00
|
|
|
};
|
2005-12-04 00:43:56 +08:00
|
|
|
|
2018-10-11 05:26:29 +08:00
|
|
|
/// The string represented by PROCESS_EXPAND_SELF
|
2022-09-21 02:58:37 +08:00
|
|
|
#define PROCESS_EXPAND_SELF_STR L"%self"
|
|
|
|
#define PROCESS_EXPAND_SELF_STR_LEN 5
|
2018-10-11 05:26:29 +08:00
|
|
|
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Perform various forms of expansion on in, such as tilde expansion (\~USER becomes the users home
|
|
|
|
/// directory), variable expansion (\$VAR_NAME becomes the value of the environment variable
|
|
|
|
/// VAR_NAME), cmdsubst expansion and wildcard expansion. The results are inserted into the list
|
|
|
|
/// out.
|
|
|
|
///
|
|
|
|
/// If the parameter does not need expansion, it is copied into the list out.
|
|
|
|
///
|
|
|
|
/// \param input The parameter to expand
|
|
|
|
/// \param output The list to which the result will be appended.
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param flags Specifies if any expansion pass should be skipped. Legal values are any combination
|
2019-04-26 02:34:49 +08:00
|
|
|
/// of skip_cmdsubst skip_variables and skip_wildcards
|
2020-01-16 09:14:47 +08:00
|
|
|
/// \param ctx The parser, variables, and cancellation checker for this operation. The parser may
|
|
|
|
/// be null. \param errors Resulting errors, or nullptr to ignore
|
2016-04-29 11:10:27 +08:00
|
|
|
///
|
2019-04-23 06:06:52 +08:00
|
|
|
/// \return An expand_result_t.
|
|
|
|
/// wildcard_no_match and wildcard_match are normal exit conditions used only on
|
2016-04-29 11:10:27 +08:00
|
|
|
/// strings containing wildcards to tell if the wildcard produced any matches.
|
2020-01-16 08:13:41 +08:00
|
|
|
__warn_unused expand_result_t expand_string(wcstring input, completion_list_t *output,
|
2020-01-16 09:14:47 +08:00
|
|
|
expand_flags_t flags, const operation_context_t &ctx,
|
|
|
|
parse_error_list_t *errors = nullptr);
|
2016-04-29 11:10:27 +08:00
|
|
|
|
2020-12-02 05:19:34 +08:00
|
|
|
/// Variant of string that inserts its results into a completion_receiver_t.
|
|
|
|
__warn_unused expand_result_t expand_string(wcstring input, completion_receiver_t *output,
|
|
|
|
expand_flags_t flags, const operation_context_t &ctx,
|
|
|
|
parse_error_list_t *errors = nullptr);
|
|
|
|
|
2016-04-29 11:10:27 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// \param inout_str The parameter to expand in-place
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param flags Specifies if any expansion pass should be skipped. Legal values are any combination
|
2019-04-26 02:34:49 +08:00
|
|
|
/// of skip_cmdsubst skip_variables and skip_wildcards
|
2020-01-16 09:14:47 +08:00
|
|
|
/// \param ctx The parser, variables, and cancellation checker for this operation. The parser may be
|
|
|
|
/// null. \param errors Resulting errors, or nullptr to ignore
|
2016-04-29 11:10:27 +08:00
|
|
|
///
|
2019-11-19 08:54:36 +08:00
|
|
|
/// \return Whether expansion succeeded.
|
2020-01-16 09:14:47 +08:00
|
|
|
bool expand_one(wcstring &string, expand_flags_t flags, const operation_context_t &ctx,
|
|
|
|
parse_error_list_t *errors = nullptr);
|
2018-08-26 16:41:45 +08:00
|
|
|
|
|
|
|
/// Expand a command string like $HOME/bin/cmd into a command and list of arguments.
|
|
|
|
/// Return the command and arguments by reference.
|
|
|
|
/// If the expansion resulted in no or an empty command, the command will be an empty string. Note
|
|
|
|
/// that API does not distinguish between expansion resulting in an empty command (''), and
|
|
|
|
/// expansion resulting in no command (e.g. unset variable).
|
2020-12-21 05:36:12 +08:00
|
|
|
/// If \p skip_wildcards is true, then do not do wildcard expansion
|
|
|
|
/// \return an expand error.
|
2020-01-16 09:14:47 +08:00
|
|
|
expand_result_t expand_to_command_and_args(const wcstring &instr, const operation_context_t &ctx,
|
2023-04-19 06:19:10 +08:00
|
|
|
wcstring *out_cmd, std::vector<wcstring> *out_args,
|
2020-12-21 05:36:12 +08:00
|
|
|
parse_error_list_t *errors = nullptr,
|
|
|
|
bool skip_wildcards = false);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Convert the variable value to a human readable form, i.e. escape things, handle arrays, etc.
|
2017-08-06 12:40:27 +08:00
|
|
|
/// Suitable for pretty-printing.
|
|
|
|
wcstring expand_escape_variable(const env_var_t &var);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-12-30 00:43:05 +08:00
|
|
|
/// Convert a string value to a human readable form, i.e. escape things, handle arrays, etc.
|
|
|
|
/// Suitable for pretty-printing.
|
2020-04-03 10:43:34 +08:00
|
|
|
wcstring expand_escape_string(const wcstring &el);
|
2019-12-30 00:43:05 +08:00
|
|
|
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Perform tilde expansion and nothing else on the specified string, which is modified in place.
|
|
|
|
///
|
|
|
|
/// \param input the string to tilde expand
|
2018-09-22 12:52:47 +08:00
|
|
|
void expand_tilde(wcstring &input, const environment_t &vars);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-29 11:10:27 +08:00
|
|
|
/// Perform the opposite of tilde expansion on the string, which is modified in place.
|
2018-09-22 12:52:47 +08:00
|
|
|
wcstring replace_home_directory_with_tilde(const wcstring &str, const environment_t &vars);
|
2013-12-17 07:33:20 +08:00
|
|
|
|
2016-04-29 11:10:27 +08:00
|
|
|
// Terrible hacks
|
|
|
|
bool fish_xdm_login_hack_hack_hack_hack(std::vector<std::string> *cmds, int argc,
|
|
|
|
const char *const *argv);
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|