2005-09-20 13:26:39 +00:00
|
|
|
/**\file expand.h
|
|
|
|
|
2005-12-07 15:57:17 +00:00
|
|
|
Prototypes for string expansion functions. These functions perform
|
|
|
|
several kinds of parameter expansion. There are a lot of issues
|
2005-09-20 13:26:39 +00:00
|
|
|
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.
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_EXPAND_H
|
2005-10-24 15:26:25 +00:00
|
|
|
/**
|
|
|
|
Header guard
|
|
|
|
*/
|
2005-10-04 15:11:39 +00:00
|
|
|
#define FISH_EXPAND_H
|
|
|
|
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
2011-12-27 03:18:46 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include <list>
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2006-08-22 14:38:31 +00:00
|
|
|
Flag specifying that cmdsubst expansion should be skipped
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-08-22 14:38:31 +00:00
|
|
|
#define EXPAND_SKIP_CMDSUBST 1
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-07 15:57:17 +00:00
|
|
|
Flag specifying that variable expansion should be skipped
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
#define EXPAND_SKIP_VARIABLES 2
|
|
|
|
|
|
|
|
/**
|
2005-12-07 15:57:17 +00:00
|
|
|
Flag specifying that wildcard expansion should be skipped
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
#define EXPAND_SKIP_WILDCARDS 4
|
|
|
|
|
|
|
|
/**
|
|
|
|
Incomplete matches in the last segment are ok (for tab
|
|
|
|
completion). An incomplete match is a wildcard that matches a
|
|
|
|
prefix of the filename. If accept_incomplete is true, only the
|
|
|
|
remainder of the string is returned.
|
|
|
|
*/
|
|
|
|
#define ACCEPT_INCOMPLETE 8
|
|
|
|
|
|
|
|
/**
|
|
|
|
Only match files that are executable by the current user. Only applicable together with ACCEPT_INCOMPLETE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define EXECUTABLES_ONLY 16
|
|
|
|
|
|
|
|
/**
|
|
|
|
Only match directories. Only applicable together with ACCEPT_INCOMPLETE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DIRECTORIES_ONLY 32
|
|
|
|
|
2005-10-24 15:26:25 +00:00
|
|
|
/**
|
2005-10-20 11:27:54 +00:00
|
|
|
Use unencoded private-use keycodes for internal characters
|
|
|
|
*/
|
|
|
|
#define EXPAND_RESERVED 0xf000
|
2008-02-04 23:09:05 +00:00
|
|
|
/**
|
|
|
|
End of range reserved for expand
|
|
|
|
*/
|
|
|
|
#define EXPAND_RESERVED_END 0xf000f
|
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
|
|
|
|
2005-10-20 11:27:54 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
/** Character represeting a home directory */
|
|
|
|
HOME_DIRECTORY = EXPAND_RESERVED,
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-12-07 15:57:17 +00:00
|
|
|
/** Character represeting process expansion */
|
2005-10-20 11:27:54 +00:00
|
|
|
PROCESS_EXPAND,
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-12-07 15:57:17 +00:00
|
|
|
/** Character representing variable expansion */
|
2005-10-20 11:27:54 +00:00
|
|
|
VARIABLE_EXPAND,
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-12-07 15:57:17 +00:00
|
|
|
/** Character rpresenting variable expansion into a single element*/
|
2005-11-02 16:49:13 +00:00
|
|
|
VARIABLE_EXPAND_SINGLE,
|
|
|
|
|
2005-12-07 15:57:17 +00:00
|
|
|
/** Character representing the start of a bracket expansion */
|
2005-10-20 11:27:54 +00:00
|
|
|
BRACKET_BEGIN,
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-12-07 15:57:17 +00:00
|
|
|
/** Character representing the end of a bracket expansion */
|
2005-10-20 11:27:54 +00:00
|
|
|
BRACKET_END,
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-10-20 11:27:54 +00:00
|
|
|
/** Character representing separation between two bracket elements */
|
|
|
|
BRACKET_SEP,
|
2005-12-07 14:43:07 +00:00
|
|
|
/**
|
2011-12-27 03:18:46 +00:00
|
|
|
Separate subtokens in a token with this character.
|
2005-12-07 14:43:07 +00:00
|
|
|
*/
|
|
|
|
INTERNAL_SEPARATOR,
|
|
|
|
|
2005-10-20 11:27:54 +00:00
|
|
|
}
|
|
|
|
;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-12-03 16:43:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
These are the possible return values for expand_string
|
|
|
|
*/
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/** Error */
|
|
|
|
EXPAND_ERROR,
|
|
|
|
/** Ok */
|
|
|
|
EXPAND_OK,
|
|
|
|
/** Ok, a wildcard in the string matched no files */
|
|
|
|
EXPAND_WILDCARD_NO_MATCH,
|
|
|
|
/* Ok, a wildcard in the string matched a file */
|
|
|
|
EXPAND_WILDCARD_MATCH
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/** Character for separating two array elements. We use 30, i.e. the ascii record separator since that seems logical. */
|
|
|
|
#define ARRAY_SEP 0x1e
|
|
|
|
|
|
|
|
/** String containing the character for separating two array elements */
|
|
|
|
#define ARRAY_SEP_STR L"\x1e"
|
|
|
|
|
2006-06-20 00:50:10 +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
|
|
|
|
2012-01-23 04:47:13 +00:00
|
|
|
class parser_t;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Perform various forms of expansion on in, such as tilde expansion
|
2006-06-15 10:37:06 +00:00
|
|
|
(\~USER becomes the users home directory), variable expansion
|
|
|
|
(\$VAR_NAME becomes the value of the environment variable VAR_NAME),
|
2006-08-22 14:38:31 +00:00
|
|
|
cmdsubst expansion and wildcard expansion. The results are inserted
|
2005-09-20 13:26:39 +00:00
|
|
|
into the list out.
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
If the parameter does not need expansion, it is copied into the list
|
|
|
|
out. If expansion is performed, the original parameter is freed and
|
|
|
|
newly allocated strings are inserted into the list out.
|
2006-06-15 10:37:06 +00:00
|
|
|
|
|
|
|
If \c context is non-null, all the strings contained in the
|
|
|
|
array_list_t \c out will be registered to be free'd when context is
|
|
|
|
free'd.
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-06-15 10:37:06 +00:00
|
|
|
\param context the halloc context to use for automatic deallocation
|
2005-09-20 13:26:39 +00:00
|
|
|
\param in The parameter to expand
|
2006-08-22 14:38:31 +00:00
|
|
|
\param flag Specifies if any expansion pass should be skipped. Legal values are any combination of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
|
2005-09-20 13:26:39 +00:00
|
|
|
\param out The list to which the result will be appended.
|
2006-06-15 10:37:06 +00:00
|
|
|
\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.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-10-19 15:19:47 +00:00
|
|
|
__warn_unused int expand_string( void *context, wchar_t *in, array_list_t *out, int flag );
|
2012-01-16 16:56:47 +00:00
|
|
|
__warn_unused int expand_string2( const wcstring &input, std::vector<completion_t> &output, int flag );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2011-12-27 06:51:34 +00:00
|
|
|
|
2005-09-20 13:26:39 +00: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.
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-01 05:30:09 +00:00
|
|
|
\param inout_str The parameter to expand in-place
|
2006-08-22 14:38:31 +00:00
|
|
|
\param flag Specifies if any expansion pass should be skipped. Legal values are any combination of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
|
2012-02-01 05:30:09 +00:00
|
|
|
\return Whether expansion succeded
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-01-30 10:23:58 +00:00
|
|
|
bool expand_one( wcstring &inout_str, int flag );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-14 10:42:17 +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!
|
2006-06-15 10:37:06 +00:00
|
|
|
|
|
|
|
\param in the value to escape
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
wchar_t *expand_escape_variable( const wchar_t *in );
|
2012-01-14 10:42:17 +00:00
|
|
|
wcstring expand_escape_variable2( const wcstring &in );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2011-12-27 03:18:46 +00:00
|
|
|
Perform tilde expansion and nothing else on the specified string, which is modified in place.
|
2006-06-15 10:37:06 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
\param input the string to tilde expand
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
void expand_tilde(wcstring &input);
|
|
|
|
wchar_t * expand_tilde_compat( wchar_t *input );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
|
2006-05-21 22:16:04 +00:00
|
|
|
/**
|
|
|
|
Test if the specified argument is clean, i.e. it does not contain
|
|
|
|
any tokens which need to be expanded or otherwise altered. Clean
|
|
|
|
strings can be passed through expand_string and expand_one without
|
|
|
|
changing them. About two thirds of all strings are clean, so
|
|
|
|
skipping expansion on them actually does save a small amount of
|
|
|
|
time, since it avoids multiple memory allocations during the
|
|
|
|
expansion process.
|
2006-06-15 10:37:06 +00:00
|
|
|
|
|
|
|
\param in the string to test
|
2006-05-21 22:16:04 +00:00
|
|
|
*/
|
|
|
|
int expand_is_clean( const wchar_t *in );
|
|
|
|
|
2006-07-20 13:02:46 +00:00
|
|
|
/**
|
|
|
|
Perform error reporting for a syntax error related to the variable
|
|
|
|
expansion beginning at the specified character of the specified
|
|
|
|
token. This function will call the error function with an
|
|
|
|
explanatory string about what is wrong with the specified token.
|
|
|
|
|
|
|
|
\param token The token containing the error
|
|
|
|
\param token_pos The position where the expansion begins
|
|
|
|
\param error_pos The position on the line to report to the error function.
|
|
|
|
*/
|
2012-01-20 19:24:43 +00:00
|
|
|
void expand_variable_error( parser_t &parser, const wchar_t *token, int token_pos, int error_pos );
|
2006-07-20 13:02:46 +00:00
|
|
|
|
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#endif
|