2005-09-20 17:26:39 +04:00
/**\file expand.h
2005-12-07 18:57:17 +03:00
Prototypes for string expansion functions . These functions perform
several kinds of parameter expansion . There are a lot of issues
2005-09-20 17:26:39 +04: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 .
2005-12-03 19:43:56 +03:00
2005-09-20 17:26:39 +04:00
*/
2005-10-04 19:11:39 +04:00
# ifndef FISH_EXPAND_H
2005-10-24 19:26:25 +04:00
/**
Header guard
*/
2005-10-04 19:11:39 +04:00
# define FISH_EXPAND_H
# include <wchar.h>
# include "util.h"
2005-09-20 17:26:39 +04:00
/**
2005-12-07 18:57:17 +03:00
Flag specifying that subshell expansion should be skipped
2005-09-20 17:26:39 +04:00
*/
# define EXPAND_SKIP_SUBSHELL 1
/**
2005-12-07 18:57:17 +03:00
Flag specifying that variable expansion should be skipped
2005-09-20 17:26:39 +04:00
*/
# define EXPAND_SKIP_VARIABLES 2
/**
2005-12-07 18:57:17 +03:00
Flag specifying that wildcard expansion should be skipped
2005-09-20 17:26:39 +04: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 19:26:25 +04:00
/**
2005-10-20 15:27:54 +04:00
Use unencoded private - use keycodes for internal characters
*/
# define EXPAND_RESERVED 0xf000
2005-09-20 17:26:39 +04:00
2005-10-20 15:27:54 +04:00
enum
{
/** Character represeting a home directory */
HOME_DIRECTORY = EXPAND_RESERVED ,
2005-09-20 17:26:39 +04:00
2005-12-07 18:57:17 +03:00
/** Character represeting process expansion */
2005-10-20 15:27:54 +04:00
PROCESS_EXPAND ,
2005-12-07 18:57:17 +03:00
/** Character representing variable expansion */
2005-10-20 15:27:54 +04:00
VARIABLE_EXPAND ,
2005-09-20 17:26:39 +04:00
2005-12-07 18:57:17 +03:00
/** Character rpresenting variable expansion into a single element*/
2005-11-02 19:49:13 +03:00
VARIABLE_EXPAND_SINGLE ,
2005-12-07 18:57:17 +03:00
/** Character representing the start of a bracket expansion */
2005-10-20 15:27:54 +04:00
BRACKET_BEGIN ,
2005-09-20 17:26:39 +04:00
2005-12-07 18:57:17 +03:00
/** Character representing the end of a bracket expansion */
2005-10-20 15:27:54 +04:00
BRACKET_END ,
2005-09-20 17:26:39 +04:00
2005-10-20 15:27:54 +04:00
/** Character representing separation between two bracket elements */
BRACKET_SEP ,
2005-12-07 17:43:07 +03:00
/**
Separate subtokens in a token with this character .
*/
INTERNAL_SEPARATOR ,
2005-10-20 15:27:54 +04:00
}
;
2005-09-20 17:26:39 +04:00
2005-12-03 19:43:56 +03: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 17:26:39 +04: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"
/**
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 ) ,
2005-12-07 18:57:17 +03:00
subshell expansion and wildcard expansion . The results are inserted
2005-09-20 17:26:39 +04:00
into the list out .
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 .
\ param in The parameter to expand
2005-12-07 18:57:17 +03:00
\ param flag Specifies if any expansion pass should be skipped . Legal values are any combination of EXPAND_SKIP_SUBSHELL EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
2005-09-20 17:26:39 +04:00
\ param out The list to which the result will be appended .
2005-12-03 19:43:56 +03:00
\ return One of EXPAND_OK , EXPAND_ERROR , EXPAND_WILDCARD_MATCH and EXPAND_WILDCARD_NO_MATCH
2005-09-20 17:26:39 +04:00
*/
int expand_string ( wchar_t * in , array_list_t * out , int flag ) ;
/**
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 in The parameter to expand
2005-12-07 18:57:17 +03:00
\ param flag Specifies if any expansion pass should be skipped . Legal values are any combination of EXPAND_SKIP_SUBSHELL EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
2005-09-20 17:26:39 +04:00
\ return The expanded parameter , or 0 on failiure
*/
wchar_t * expand_one ( wchar_t * in , int flag ) ;
2005-10-07 14:36:51 +04:00
/**
Expand backslashed escapes and substitute them with their unescaped
counterparts . Also optionally change the wildcards , the tilde
2005-10-24 19:26:25 +04:00
character and a few more into constants which are defined in a
private use area of Unicode . This assumes wchar_t is a unicode
character . character set .
2005-10-07 14:36:51 +04:00
The result must be free ( ) d . The original string is not modified . If
an invalid sequence is specified , 0 is returned .
*/
wchar_t * expand_unescape ( const wchar_t * in , int escape_special ) ;
2005-09-20 17:26:39 +04:00
/**
Replace special characters with escape sequences . Newline is
replaced with \ n , etc .
2005-10-07 14:36:51 +04:00
The result must be free ( ) d . The original string is not modified .
2005-09-20 17:26:39 +04:00
\ param in The string to be escaped
\ param escape_all Whether all characters wich hold special meaning in fish ( Pipe , semicolon , etc , ) should be escaped , or only unprintable characters
2005-10-07 14:36:51 +04:00
\ return The escaped string
2005-09-20 17:26:39 +04:00
*/
2005-10-07 14:36:51 +04:00
wchar_t * expand_escape ( const wchar_t * in , int escape_all ) ;
2005-09-20 17:26:39 +04:00
/**
Convert the variable value to a human readable form , i . e . escape things , handle arrays , etc . Suitable for pretty - printing .
*/
wchar_t * expand_escape_variable ( const wchar_t * in ) ;
/**
2005-12-07 18:57:17 +03:00
Perform tilde expansion and nothing else on the specified string .
2005-09-20 17:26:39 +04:00
2005-12-07 18:57:17 +03:00
If tilde expansion is needed , the original string is freed and a
2005-09-20 17:26:39 +04:00
new string , allocated using malloc , is returned .
*/
wchar_t * expand_tilde ( wchar_t * in ) ;
/**
2005-10-25 15:03:52 +04:00
Locate the first subshell in the specified string .
2005-09-20 17:26:39 +04:00
\ param in the string to search for subshells
\ param begin the starting paranthesis of the subshell
\ param end the ending paranthesis of the subshell
\ param flags set this variable to ACCEPT_INCOMPLETE if in tab_completion mode
\ return - 1 on syntax error , 0 if no subshells exist and 1 on sucess
*/
int expand_locate_subshell ( wchar_t * in ,
wchar_t * * begin ,
wchar_t * * end ,
int flags ) ;
/**
Tokenize the specified string into the specified array_list_t .
Each new element is allocated using malloc and must be freed by the
caller .
\ param val the input string . The contents of this string is not changed .
\ param out the list in which to place the elements .
*/
void expand_variable_array ( const wchar_t * val , array_list_t * out ) ;
2005-10-04 19:11:39 +04:00
# endif