2016-04-29 03:10:27 +00: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 15:11:39 +00:00
|
|
|
#ifndef FISH_EXPAND_H
|
|
|
|
#define FISH_EXPAND_H
|
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "config.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
#include <stddef.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
#include "common.h"
|
2014-03-22 00:13:33 +00:00
|
|
|
#include "parse_constants.h"
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
enum {
|
|
|
|
/// Flag specifying that cmdsubst expansion should be skipped.
|
2012-02-24 20:13:35 +00:00
|
|
|
EXPAND_SKIP_CMDSUBST = 1 << 0,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Flag specifying that variable expansion should be skipped.
|
2012-02-24 20:13:35 +00:00
|
|
|
EXPAND_SKIP_VARIABLES = 1 << 1,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Flag specifying that wildcard expansion should be skipped.
|
2012-02-24 20:13:35 +00:00
|
|
|
EXPAND_SKIP_WILDCARDS = 1 << 2,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// The expansion is being done for tab or auto completions. Returned completions may have the
|
|
|
|
/// wildcard as a prefix instead of a match.
|
2015-08-03 23:36:10 +00:00
|
|
|
EXPAND_FOR_COMPLETIONS = 1 << 3,
|
2017-04-09 04:05:18 +00:00
|
|
|
/// Only match files that are executable by the current user.
|
2012-02-24 20:13:35 +00:00
|
|
|
EXECUTABLES_ONLY = 1 << 4,
|
2017-04-09 04:05:18 +00:00
|
|
|
/// Only match directories.
|
2012-02-24 20:13:35 +00:00
|
|
|
DIRECTORIES_ONLY = 1 << 5,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Don't generate descriptions.
|
2012-06-29 23:40:54 +00:00
|
|
|
EXPAND_NO_DESCRIPTIONS = 1 << 6,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Don't expand jobs (but you can still expand processes). This is because
|
2016-06-06 01:46:04 +00:00
|
|
|
/// job expansion is not thread safe.
|
2014-10-15 19:42:55 +00:00
|
|
|
EXPAND_SKIP_JOBS = 1 << 7,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Don't expand home directories.
|
2014-10-15 19:42:55 +00:00
|
|
|
EXPAND_SKIP_HOME_DIRECTORIES = 1 << 8,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Allow fuzzy matching.
|
2015-10-03 22:44:57 +00:00
|
|
|
EXPAND_FUZZY_MATCH = 1 << 9,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Disallow directory abbreviations like /u/l/b for /usr/local/bin. Only applicable if
|
|
|
|
/// EXPAND_FUZZY_MATCH is set.
|
2016-02-06 22:39:47 +00:00
|
|
|
EXPAND_NO_FUZZY_DIRECTORIES = 1 << 10,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Do expansions specifically to support cd. This means using CDPATH as a list of potential
|
|
|
|
/// working directories.
|
2016-04-07 22:24:52 +00:00
|
|
|
EXPAND_SPECIAL_FOR_CD = 1 << 11,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Do expansions specifically to support external command completions. This means using PATH as
|
2016-06-06 01:46:04 +00:00
|
|
|
/// a list of potential working directories.
|
2016-04-07 22:24:52 +00:00
|
|
|
EXPAND_SPECIAL_FOR_COMMAND = 1 << 12
|
2012-02-24 20:13:35 +00:00
|
|
|
};
|
|
|
|
typedef int expand_flags_t;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-02-02 00:27:14 +00:00
|
|
|
class completion_t;
|
2012-01-16 16:56:47 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
enum {
|
|
|
|
/// Character representing a home directory.
|
2016-01-22 03:56:39 +00:00
|
|
|
HOME_DIRECTORY = EXPAND_RESERVED_BASE,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character representing process expansion.
|
2012-11-19 00:30:30 +00:00
|
|
|
PROCESS_EXPAND,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character representing variable expansion.
|
2012-11-19 00:30:30 +00:00
|
|
|
VARIABLE_EXPAND,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character representing variable expansion into a single element.
|
2012-11-19 00:30:30 +00:00
|
|
|
VARIABLE_EXPAND_SINGLE,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character representing the start of a bracket expansion.
|
2012-11-19 00:30:30 +00:00
|
|
|
BRACKET_BEGIN,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character representing the end of a bracket expansion.
|
2012-11-19 00:30:30 +00:00
|
|
|
BRACKET_END,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character representing separation between two bracket elements.
|
2012-11-19 00:30:30 +00:00
|
|
|
BRACKET_SEP,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Separate subtokens in a token with this character.
|
2012-11-19 00:30:30 +00:00
|
|
|
INTERNAL_SEPARATOR,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character representing an empty variable expansion. Only used transitively while expanding
|
|
|
|
/// variables.
|
2014-08-21 04:19:08 +00:00
|
|
|
VARIABLE_EXPAND_EMPTY,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// This is a special psuedo-char that is not used other than to mark the end of the the special
|
|
|
|
/// characters so we can sanity check the enum range.
|
2016-01-22 03:56:39 +00:00
|
|
|
EXPAND_SENTINAL
|
|
|
|
};
|
2005-12-03 16:43:56 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// These are the possible return values for expand_string. Note how zero value is the only error.
|
|
|
|
enum expand_error_t {
|
|
|
|
/// Error
|
2012-11-19 00:30:30 +00:00
|
|
|
EXPAND_ERROR,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Ok
|
2012-11-19 00:30:30 +00:00
|
|
|
EXPAND_OK,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Ok, a wildcard in the string matched no files.
|
2012-11-19 00:30:30 +00:00
|
|
|
EXPAND_WILDCARD_NO_MATCH,
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Ok, a wildcard in the string matched a file.
|
2012-11-19 00:30:30 +00:00
|
|
|
EXPAND_WILDCARD_MATCH
|
2012-05-09 09:33:42 +00:00
|
|
|
};
|
2005-12-03 16:43:56 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Character for separating two array elements. We use 30, i.e. the ascii record separator since
|
|
|
|
/// that seems logical.
|
2012-12-19 21:31:06 +00:00
|
|
|
#define ARRAY_SEP ((wchar_t)(0x1e))
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// String containing the character for separating two array elements.
|
2005-09-20 13:26:39 +00:00
|
|
|
#define ARRAY_SEP_STR L"\x1e"
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Error issued on array out of bounds.
|
2006-06-04 20:14:51 +00:00
|
|
|
#define ARRAY_BOUNDS_ERR _(L"Array index out of bounds")
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00: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 01:46:04 +00:00
|
|
|
/// \param flags Specifies if any expansion pass should be skipped. Legal values are any combination
|
2016-04-29 03:10:27 +00:00
|
|
|
/// of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
|
|
|
|
/// \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
|
|
|
|
/// strings containing wildcards to tell if the wildcard produced any matches.
|
|
|
|
__warn_unused expand_error_t expand_string(const wcstring &input, std::vector<completion_t> *output,
|
|
|
|
expand_flags_t flags, 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.
|
|
|
|
///
|
|
|
|
/// \param inout_str The parameter to expand in-place
|
2016-06-06 01:46:04 +00:00
|
|
|
/// \param flags Specifies if any expansion pass should be skipped. Legal values are any combination
|
2016-04-29 03:10:27 +00:00
|
|
|
/// of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
|
|
|
|
/// \param errors Resulting errors, or NULL to ignore
|
|
|
|
///
|
|
|
|
/// \return Whether expansion succeded
|
2014-03-22 00:13:33 +00:00
|
|
|
bool expand_one(wcstring &inout_str, expand_flags_t flags, parse_error_list_t *errors = NULL);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Convert the variable value to a human readable form, i.e. escape things, handle arrays, etc.
|
|
|
|
/// Suitable for pretty-printing. The result must be free'd!
|
|
|
|
///
|
|
|
|
/// \param in the value to escape
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring expand_escape_variable(const wcstring &in);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Perform tilde expansion and nothing else on the specified string, which is modified in place.
|
|
|
|
///
|
|
|
|
/// \param input the string to tilde expand
|
2011-12-27 03:18:46 +00:00
|
|
|
void expand_tilde(wcstring &input);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Perform the opposite of tilde expansion on the string, which is modified in place.
|
2013-12-16 23:33:20 +00:00
|
|
|
wcstring replace_home_directory_with_tilde(const wcstring &str);
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
/// Abbreviation support. Expand src as an abbreviation, returning true if one was found, false if
|
|
|
|
/// not. If result is not-null, returns the abbreviation by reference.
|
2013-07-17 07:38:04 +00:00
|
|
|
#define USER_ABBREVIATIONS_VARIABLE_NAME L"fish_user_abbreviations"
|
|
|
|
bool expand_abbreviation(const wcstring &src, wcstring *output);
|
|
|
|
|
2016-04-29 03:10:27 +00:00
|
|
|
// Terrible hacks
|
|
|
|
bool fish_xdm_login_hack_hack_hack_hack(std::vector<std::string> *cmds, int argc,
|
|
|
|
const char *const *argv);
|
2013-01-04 21:09:01 +00:00
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#endif
|