2005-09-20 21:26:39 +08:00
/**\file expand.h
2005-12-07 23:57:17 +08:00
Prototypes for string expansion functions . These functions perform
several kinds of parameter expansion . There are a lot of issues
2005-09-20 21:26:39 +08: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 .
2012-11-18 18:23:22 +08:00
2005-09-20 21:26:39 +08:00
*/
2005-10-04 23:11:39 +08:00
# ifndef FISH_EXPAND_H
2005-10-24 23:26:25 +08:00
/**
Header guard
*/
2005-10-04 23:11:39 +08:00
# define FISH_EXPAND_H
2015-07-25 23:14:25 +08:00
# include "config.h" // for __warn_unused
2005-10-04 23:11:39 +08:00
# include <wchar.h>
2015-07-25 23:14:25 +08:00
# include <string> // for string
# include <vector> // for vector
2005-10-04 23:11:39 +08:00
2011-12-27 11:18:46 +08:00
# include "common.h"
2014-03-22 08:13:33 +08:00
# include "parse_constants.h"
2005-10-04 23:11:39 +08:00
2012-11-19 08:30:30 +08:00
enum
{
2012-02-25 04:13:35 +08:00
/** Flag specifying that cmdsubst expansion should be skipped */
EXPAND_SKIP_CMDSUBST = 1 < < 0 ,
2012-11-18 18:23:22 +08:00
2012-02-25 04:13:35 +08:00
/** Flag specifying that variable expansion should be skipped */
EXPAND_SKIP_VARIABLES = 1 < < 1 ,
2012-11-18 18:23:22 +08:00
2012-02-25 04:13:35 +08:00
/** Flag specifying that wildcard expansion should be skipped */
EXPAND_SKIP_WILDCARDS = 1 < < 2 ,
/**
2015-08-04 07:36:10 +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 .
2012-11-18 18:23:22 +08:00
*/
2015-08-04 07:36:10 +08:00
EXPAND_FOR_COMPLETIONS = 1 < < 3 ,
2012-02-25 04:13:35 +08:00
/** Only match files that are executable by the current user. Only applicable together with ACCEPT_INCOMPLETE. */
EXECUTABLES_ONLY = 1 < < 4 ,
2012-11-18 18:23:22 +08:00
2012-02-25 04:13:35 +08:00
/** Only match directories. Only applicable together with ACCEPT_INCOMPLETE. */
DIRECTORIES_ONLY = 1 < < 5 ,
2012-11-18 18:23:22 +08:00
2012-02-25 04:13:35 +08:00
/** Don't generate descriptions */
2012-06-30 07:40:54 +08:00
EXPAND_NO_DESCRIPTIONS = 1 < < 6 ,
2012-11-18 18:23:22 +08:00
2012-08-07 15:01:48 +08:00
/** Don't expand jobs (but you can still expand processes). This is because job expansion is not thread safe. */
2014-10-16 03:42:55 +08:00
EXPAND_SKIP_JOBS = 1 < < 7 ,
2013-01-13 04:55:23 +08:00
2013-01-13 04:53:40 +08:00
/** Don't expand home directories */
2014-10-16 03:42:55 +08:00
EXPAND_SKIP_HOME_DIRECTORIES = 1 < < 8 ,
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
/** Allow fuzzy matching */
2015-10-04 06:44:57 +08:00
EXPAND_FUZZY_MATCH = 1 < < 9 ,
/** Disallow directory abbreviations like /u/l/b for /usr/local/bin. Only applicable if EXPAND_FUZZY_MATCH is set. */
2016-02-07 06:39:47 +08:00
EXPAND_NO_FUZZY_DIRECTORIES = 1 < < 10 ,
/** Do expansions specifically to support cd (CDPATH, etc) */
EXPAND_SPECIAL_CD = 1 < < 11
2012-02-25 04:13:35 +08:00
} ;
typedef int expand_flags_t ;
2005-09-20 21:26:39 +08:00
2005-10-24 23:26:25 +08:00
/**
2005-10-20 19:27:54 +08:00
Use unencoded private - use keycodes for internal characters
*/
# define EXPAND_RESERVED 0xf000
2008-02-05 07:09:05 +08:00
/**
End of range reserved for expand
*/
# define EXPAND_RESERVED_END 0xf000f
2005-09-20 21:26:39 +08:00
2012-02-02 08:27:14 +08:00
class completion_t ;
2012-01-17 00:56:47 +08:00
2005-10-20 19:27:54 +08:00
enum
{
2012-11-19 08:30:30 +08:00
/** Character represeting a home directory */
HOME_DIRECTORY = EXPAND_RESERVED ,
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
/** Character represeting process expansion */
PROCESS_EXPAND ,
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/** Character representing variable expansion */
VARIABLE_EXPAND ,
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/** Character rpresenting variable expansion into a single element*/
VARIABLE_EXPAND_SINGLE ,
2005-11-03 00:49:13 +08:00
2012-11-19 08:30:30 +08:00
/** Character representing the start of a bracket expansion */
BRACKET_BEGIN ,
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/** Character representing the end of a bracket expansion */
BRACKET_END ,
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/** Character representing separation between two bracket elements */
BRACKET_SEP ,
/**
Separate subtokens in a token with this character .
*/
INTERNAL_SEPARATOR ,
2005-12-07 22:43:07 +08:00
2014-08-21 12:19:08 +08:00
/**
Character representing an empty variable expansion .
Only used transitively while expanding variables .
*/
VARIABLE_EXPAND_EMPTY ,
2005-10-20 19:27:54 +08:00
}
2012-11-19 08:30:30 +08:00
;
2005-09-20 21:26:39 +08:00
2005-12-04 00:43:56 +08:00
2016-02-05 16:00:38 +08:00
/** These are the possible return values for expand_string. Note how zero value is the only error. */
enum expand_error_t
2005-12-04 00:43:56 +08:00
{
2012-11-19 08:30:30 +08:00
/** 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
2012-05-09 17:33:42 +08:00
} ;
2005-12-04 00:43:56 +08:00
2005-09-20 21:26:39 +08:00
/** Character for separating two array elements. We use 30, i.e. the ascii record separator since that seems logical. */
2012-12-20 05:31:06 +08:00
# define ARRAY_SEP ((wchar_t)(0x1e))
2005-09-20 21:26:39 +08:00
/** String containing the character for separating two array elements */
# define ARRAY_SEP_STR L"\x1e"
2006-06-20 08:50:10 +08:00
/**
Error issued on array out of bounds
*/
2006-06-05 04:14:51 +08:00
# define ARRAY_BOUNDS_ERR _(L"Array index out of bounds")
2005-09-20 21:26:39 +08:00
/**
Perform various forms of expansion on in , such as tilde expansion
2006-06-15 18:37:06 +08:00
( \ ~ USER becomes the users home directory ) , variable expansion
( \ $ VAR_NAME becomes the value of the environment variable VAR_NAME ) ,
2006-08-22 22:38:31 +08:00
cmdsubst expansion and wildcard expansion . The results are inserted
2005-09-20 21:26:39 +08:00
into the list out .
2012-11-18 18:23:22 +08:00
2005-09-20 21:26:39 +08:00
If the parameter does not need expansion , it is copied into the list
2012-05-09 17:33:42 +08:00
out .
2006-06-15 18:37:06 +08:00
2012-02-06 08:42:24 +08:00
\ param input The parameter to expand
\ param output The list to which the result will be appended .
2006-08-22 22:38:31 +08: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
2014-03-22 08:13:33 +08:00
\ param errors Resulting errors , or NULL to ignore
2006-06-15 18:37:06 +08: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 21:26:39 +08:00
*/
2016-02-05 16:00:38 +08:00
__warn_unused expand_error_t expand_string ( const wcstring & input , std : : vector < completion_t > * output , expand_flags_t flags , parse_error_list_t * errors ) ;
2005-09-20 21:26:39 +08:00
2011-12-27 14:51:34 +08:00
2005-09-20 21:26:39 +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 .
2012-11-18 18:23:22 +08:00
2012-02-01 13:30:09 +08:00
\ param inout_str The parameter to expand in - place
2006-08-22 22:38:31 +08: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
2014-03-22 08:13:33 +08:00
\ param errors Resulting errors , or NULL to ignore
2012-02-01 13:30:09 +08:00
\ return Whether expansion succeded
2005-09-20 21:26:39 +08:00
*/
2014-03-22 08:13:33 +08:00
bool expand_one ( wcstring & inout_str , expand_flags_t flags , parse_error_list_t * errors = NULL ) ;
2005-09-20 21:26:39 +08:00
/**
2012-01-14 18:42:17 +08: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 18:37:06 +08:00
\ param in the value to escape
2005-09-20 21:26:39 +08:00
*/
2012-11-19 08:30:30 +08:00
wcstring expand_escape_variable ( const wcstring & in ) ;
2005-09-20 21:26:39 +08:00
/**
2011-12-27 11:18:46 +08:00
Perform tilde expansion and nothing else on the specified string , which is modified in place .
2006-06-15 18:37:06 +08:00
2011-12-27 11:18:46 +08:00
\ param input the string to tilde expand
2005-09-20 21:26:39 +08:00
*/
2011-12-27 11:18:46 +08:00
void expand_tilde ( wcstring & input ) ;
2005-09-20 21:26:39 +08:00
2013-12-17 07:33:20 +08:00
/** Perform the opposite of tilde expansion on the string, which is modified in place */
wcstring replace_home_directory_with_tilde ( const wcstring & str ) ;
2012-07-17 03:05:36 +08:00
/**
Testing function for getting all process names .
*/
std : : vector < wcstring > expand_get_all_process_names ( void ) ;
2006-07-20 21:02:46 +08:00
2013-07-17 15:38:04 +08: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. */
# define USER_ABBREVIATIONS_VARIABLE_NAME L"fish_user_abbreviations"
bool expand_abbreviation ( const wcstring & src , wcstring * output ) ;
2013-01-05 05:09:01 +08:00
/* Terrible hacks */
bool fish_xdm_login_hack_hack_hack_hack ( std : : vector < std : : string > * cmds , int argc , const char * const * argv ) ;
bool fish_openSUSE_dbus_hack_hack_hack_hack ( std : : vector < completion_t > * args ) ;
2005-10-04 23:11:39 +08:00
# endif
2013-01-05 05:09:01 +08:00