2005-09-20 21:26:39 +08:00
/** \file highlight.c
2012-11-18 18:23:22 +08:00
Functions for syntax highlighting
2005-09-20 21:26:39 +08:00
*/
2006-08-11 09:18:35 +08:00
# include "config.h"
2005-09-20 21:26:39 +08:00
# include <stdlib.h>
# include <stdio.h>
# include <sys/stat.h>
# include <unistd.h>
# include <errno.h>
# include <wchar.h>
# include <wctype.h>
# include <termios.h>
# include <signal.h>
2006-02-28 21:17:16 +08:00
# include "fallback.h"
2005-09-20 21:26:39 +08:00
# include "util.h"
2006-02-28 21:17:16 +08:00
2005-09-20 21:26:39 +08:00
# include "wutil.h"
# include "highlight.h"
# include "tokenizer.h"
# include "proc.h"
# include "parser.h"
2006-01-31 00:51:50 +08:00
# include "parse_util.h"
2007-04-22 17:50:26 +08:00
# include "parser_keywords.h"
2005-09-20 21:26:39 +08:00
# include "builtin.h"
# include "function.h"
# include "env.h"
# include "expand.h"
# include "sanity.h"
# include "common.h"
# include "complete.h"
# include "output.h"
2006-07-28 21:52:03 +08:00
# include "wildcard.h"
2006-10-19 19:50:23 +08:00
# include "path.h"
2012-06-29 02:54:37 +08:00
# include "history.h"
2005-09-20 21:26:39 +08:00
2006-06-20 08:50:10 +08:00
/**
Number of elements in the highlight_var array
*/
2006-06-14 21:22:40 +08:00
# define VAR_COUNT ( sizeof(highlight_var) / sizeof(wchar_t *) )
2012-11-19 08:30:30 +08:00
static void highlight_universal_internal ( const wcstring & buff , std : : vector < int > & color , size_t pos ) ;
2005-09-20 21:26:39 +08:00
/**
The environment variables used to specify the color of different tokens .
*/
2012-11-18 18:23:22 +08:00
static const wchar_t * const highlight_var [ ] =
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
L " fish_color_normal " ,
L " fish_color_error " ,
L " fish_color_command " ,
L " fish_color_end " ,
L " fish_color_param " ,
L " fish_color_comment " ,
L " fish_color_match " ,
L " fish_color_search_match " ,
L " fish_color_operator " ,
L " fish_color_escape " ,
L " fish_color_quote " ,
L " fish_color_redirection " ,
L " fish_color_valid_path " ,
2012-02-07 12:14:19 +08:00
L " fish_color_autosuggestion "
} ;
2005-09-20 21:26:39 +08:00
2012-05-08 08:31:24 +08:00
/* If the given path looks like it's relative to the working directory, then prepend that working directory. */
2012-11-19 08:30:30 +08:00
static wcstring apply_working_directory ( const wcstring & path , const wcstring & working_directory )
{
2012-05-08 08:31:24 +08:00
if ( path . empty ( ) | | working_directory . empty ( ) )
return path ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* We're going to make sure that if we want to prepend the wd, that the string has no leading / */
bool prepend_wd ;
2012-11-19 08:30:30 +08:00
switch ( path . at ( 0 ) )
{
2012-11-19 16:31:03 +08:00
case L ' / ' :
case L ' ~ ' :
prepend_wd = false ;
break ;
default :
prepend_wd = true ;
break ;
2012-05-08 08:31:24 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( ! prepend_wd )
{
2012-05-08 08:31:24 +08:00
/* No need to prepend the wd, so just return the path we were given */
return path ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-05-08 08:31:24 +08:00
/* Remove up to one ./ */
wcstring path_component = path ;
2012-11-19 08:30:30 +08:00
if ( string_prefixes_string ( L " ./ " , path_component ) )
{
2012-05-08 08:31:24 +08:00
path_component . erase ( 0 , 2 ) ;
}
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* Removing leading /s */
2012-11-19 08:30:30 +08:00
while ( string_prefixes_string ( L " / " , path_component ) )
{
2012-05-08 08:31:24 +08:00
path_component . erase ( 0 , 1 ) ;
}
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* Construct and return a new path */
wcstring new_path = working_directory ;
append_path_component ( new_path , path_component ) ;
return new_path ;
}
}
2012-06-17 05:08:58 +08:00
/* Determine if the filesystem containing the given fd is case insensitive. */
typedef std : : map < wcstring , bool > case_sensitivity_cache_t ;
bool fs_is_case_insensitive ( const wcstring & path , int fd , case_sensitivity_cache_t & case_sensitivity_cache )
{
/* If _PC_CASE_SENSITIVE is not defined, assume case sensitive */
bool result = false ;
# ifdef _PC_CASE_SENSITIVE
/* Try the cache first */
case_sensitivity_cache_t : : iterator cache = case_sensitivity_cache . find ( path ) ;
2012-11-19 08:30:30 +08:00
if ( cache ! = case_sensitivity_cache . end ( ) )
{
2012-06-17 05:08:58 +08:00
/* Use the cached value */
result = cache - > second ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-06-17 05:08:58 +08:00
/* Ask the system. A -1 value means error (so assume case sensitive), a 1 value means case sensitive, and a 0 value means case insensitive */
long ret = fpathconf ( fd , _PC_CASE_SENSITIVE ) ;
result = ( ret = = 0 ) ;
case_sensitivity_cache [ path ] = result ;
}
# endif
return result ;
}
2012-05-14 11:19:02 +08:00
/* Tests whether the specified string cpath is the prefix of anything we could cd to. directories is a list of possible parent directories (typically either the working directory, or the cdpath). This does I/O!
2012-07-07 05:34:53 +08:00
We expect the path to already be unescaped .
2012-05-14 11:19:02 +08:00
*/
2012-07-07 05:34:53 +08:00
bool is_potential_path ( const wcstring & const_path , const wcstring_list_t & directories , path_flags_t flags , wcstring * out_path )
2006-06-14 21:22:40 +08:00
{
2011-12-27 11:18:46 +08:00
ASSERT_IS_BACKGROUND_THREAD ( ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
const bool require_dir = ! ! ( flags & PATH_REQUIRE_DIR ) ;
2012-05-08 08:31:24 +08:00
wcstring clean_path ;
2012-11-19 08:30:30 +08:00
int has_magic = 0 ;
bool result = false ;
2012-11-18 18:23:22 +08:00
2012-05-14 11:19:02 +08:00
wcstring path ( const_path ) ;
2012-07-07 05:34:53 +08:00
if ( flags & PATH_EXPAND_TILDE )
2012-08-24 02:21:35 +08:00
expand_tilde ( path ) ;
2012-11-18 18:23:22 +08:00
// debug( 1, L"%ls -> %ls ->%ls", path, tilde, unescaped );
2012-11-19 08:30:30 +08:00
for ( size_t i = 0 ; i < path . size ( ) ; i + + )
2011-12-27 11:18:46 +08:00
{
2012-07-07 05:34:53 +08:00
wchar_t c = path . at ( i ) ;
2012-11-19 08:30:30 +08:00
switch ( c )
2011-12-27 11:18:46 +08:00
{
2012-11-19 16:31:03 +08:00
case PROCESS_EXPAND :
case VARIABLE_EXPAND :
case VARIABLE_EXPAND_SINGLE :
case BRACKET_BEGIN :
case BRACKET_END :
case BRACKET_SEP :
case ANY_CHAR :
case ANY_STRING :
case ANY_STRING_RECURSIVE :
{
has_magic = 1 ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case INTERNAL_SEPARATOR :
{
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
default :
{
clean_path . push_back ( c ) ;
break ;
}
2012-11-18 18:23:22 +08:00
2011-12-27 11:18:46 +08:00
}
2012-11-18 18:23:22 +08:00
2011-12-27 11:18:46 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( ! has_magic & & ! clean_path . empty ( ) )
2011-12-27 11:18:46 +08:00
{
2012-05-08 08:31:24 +08:00
/* Don't test the same path multiple times, which can happen if the path is absolute and the CDPATH contains multiple entries */
std : : set < wcstring > checked_paths ;
2012-11-18 18:23:22 +08:00
2012-06-17 05:08:58 +08:00
/* Keep a cache of which paths / filesystems are case sensitive */
case_sensitivity_cache_t case_sensitivity_cache ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
for ( size_t wd_idx = 0 ; wd_idx < directories . size ( ) & & ! result ; wd_idx + + )
{
2012-05-08 08:31:24 +08:00
const wcstring & wd = directories . at ( wd_idx ) ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
const wcstring abs_path = apply_working_directory ( clean_path , wd ) ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* Skip this if it's empty or we've already checked it */
if ( abs_path . empty ( ) | | checked_paths . count ( abs_path ) )
continue ;
checked_paths . insert ( abs_path ) ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* If we end with a slash, then it must be a directory */
bool must_be_full_dir = abs_path . at ( abs_path . size ( ) - 1 ) = = L ' / ' ;
2012-11-18 18:23:22 +08:00
if ( must_be_full_dir )
2011-12-27 11:18:46 +08:00
{
2012-05-08 08:31:24 +08:00
struct stat buf ;
2012-11-19 08:30:30 +08:00
if ( 0 = = wstat ( abs_path , & buf ) & & S_ISDIR ( buf . st_mode ) )
{
2012-05-08 08:31:24 +08:00
result = true ;
/* Return the path suffix, not the whole absolute path */
if ( out_path )
* out_path = clean_path ;
}
2011-12-27 11:18:46 +08:00
}
2012-05-08 08:31:24 +08:00
else
2011-12-27 11:18:46 +08:00
{
2012-05-08 08:31:24 +08:00
DIR * dir = NULL ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* We do not end with a slash; it does not have to be a directory */
const wcstring dir_name = wdirname ( abs_path ) ;
const wcstring base_name = wbasename ( abs_path ) ;
if ( dir_name = = L " / " & & base_name = = L " / " )
2011-12-27 11:18:46 +08:00
{
2012-05-08 08:31:24 +08:00
result = true ;
if ( out_path )
* out_path = clean_path ;
}
2012-11-19 08:30:30 +08:00
else if ( ( dir = wopendir ( dir_name ) ) )
{
2012-05-14 11:19:02 +08:00
// We opened the dir_name; look for a string where the base name prefixes it
2012-05-08 08:31:24 +08:00
wcstring ent ;
2012-11-18 18:23:22 +08:00
2012-06-17 05:08:58 +08:00
// Check if we're case insensitive
bool case_insensitive = fs_is_case_insensitive ( dir_name , dirfd ( dir ) , case_sensitivity_cache ) ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
// Don't ask for the is_dir value unless we care, because it can cause extra filesystem acces */
bool is_dir = false ;
while ( wreaddir_resolving ( dir , dir_name , ent , require_dir ? & is_dir : NULL ) )
2012-11-18 18:23:22 +08:00
{
2012-06-17 05:08:58 +08:00
/* Determine which function to call to check for prefixes */
bool ( * prefix_func ) ( const wcstring & , const wcstring & ) ;
2012-11-19 08:30:30 +08:00
if ( case_insensitive )
{
2012-06-17 05:08:58 +08:00
prefix_func = string_prefixes_string_case_insensitive ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-06-17 05:08:58 +08:00
prefix_func = string_prefixes_string ;
}
if ( prefix_func ( base_name , ent ) & & ( ! require_dir | | is_dir ) )
2012-05-08 08:31:24 +08:00
{
result = true ;
2012-11-19 08:30:30 +08:00
if ( out_path )
{
2012-05-14 11:49:14 +08:00
/* We want to return the path in the same "form" as it was given. Take the given path, get its basename. Append that to the output if the basename actually prefixes the path (which it won't if the given path contains no slashes), and isn't a slash (so we don't duplicate slashes). Then append the directory entry. */
2012-11-18 18:23:22 +08:00
2012-05-14 11:19:02 +08:00
out_path - > clear ( ) ;
const wcstring path_base = wdirname ( const_path ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( prefix_func ( path_base , const_path ) )
{
2012-05-14 11:19:02 +08:00
out_path - > append ( path_base ) ;
2012-05-14 11:49:14 +08:00
if ( ! string_suffixes_string ( L " / " , * out_path ) )
out_path - > push_back ( L ' / ' ) ;
2012-05-14 11:19:02 +08:00
}
2012-05-08 08:31:24 +08:00
out_path - > append ( ent ) ;
/* We actually do want a trailing / for directories, since it makes autosuggestion a bit nicer */
if ( is_dir )
out_path - > push_back ( L ' / ' ) ;
}
break ;
2012-02-20 18:13:31 +08:00
}
2011-12-27 11:18:46 +08:00
}
2012-05-08 08:31:24 +08:00
closedir ( dir ) ;
2011-12-27 11:18:46 +08:00
}
}
}
}
2012-05-08 08:31:24 +08:00
return result ;
}
2012-07-07 05:34:53 +08:00
/* Given a string, return whether it prefixes a path that we could cd into. Return that path in out_path. Expects path to be unescaped. */
2012-08-24 02:21:35 +08:00
static bool is_potential_cd_path ( const wcstring & path , const wcstring & working_directory , path_flags_t flags , wcstring * out_path )
{
2012-05-08 08:31:24 +08:00
wcstring_list_t directories ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( string_prefixes_string ( L " ./ " , path ) )
{
2012-05-14 11:19:02 +08:00
/* Ignore the CDPATH in this case; just use the working directory */
directories . push_back ( working_directory ) ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-05-14 11:19:02 +08:00
/* Get the CDPATH */
env_var_t cdpath = env_get_string ( L " CDPATH " ) ;
if ( cdpath . missing_or_empty ( ) )
cdpath = L " . " ;
2012-11-18 18:23:22 +08:00
2012-05-14 11:19:02 +08:00
/* Tokenize it into directories */
wcstokenizer tokenizer ( cdpath , ARRAY_SEP_STR ) ;
wcstring next_path ;
while ( tokenizer . next ( next_path ) )
{
/* Ensure that we use the working directory for relative cdpaths like "." */
directories . push_back ( apply_working_directory ( next_path , working_directory ) ) ;
}
2012-05-08 08:31:24 +08:00
}
2012-11-18 18:23:22 +08:00
2012-05-14 11:19:02 +08:00
/* Call is_potential_path with all of these directories */
2012-07-07 05:34:53 +08:00
bool result = is_potential_path ( path , directories , flags | PATH_REQUIRE_DIR , out_path ) ;
2012-05-14 11:19:02 +08:00
#if 0
2012-11-19 08:30:30 +08:00
if ( out_path )
{
2012-05-14 11:19:02 +08:00
printf ( " %ls -> %ls \n " , path . c_str ( ) , out_path - > c_str ( ) ) ;
}
# endif
return result ;
2006-06-14 21:22:40 +08:00
}
2012-11-19 08:30:30 +08:00
rgb_color_t highlight_get_color ( int highlight , bool is_background )
2012-02-12 09:07:56 +08:00
{
2012-11-19 08:30:30 +08:00
size_t idx = 0 ;
rgb_color_t result ;
if ( highlight < 0 )
return rgb_color_t : : normal ( ) ;
if ( highlight > ( 1 < < VAR_COUNT ) )
return rgb_color_t : : normal ( ) ;
for ( size_t i = 0 ; i < VAR_COUNT ; i + + )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
if ( highlight & ( 1 < < i ) )
{
idx = i ;
break ;
}
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
env_var_t val_wstr = env_get_string ( highlight_var [ idx ] ) ;
2012-11-18 18:23:22 +08:00
// debug( 1, L"%d -> %d -> %ls", highlight, idx, val );
2012-11-19 08:30:30 +08:00
if ( val_wstr . missing ( ) )
val_wstr = env_get_string ( highlight_var [ 0 ] ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( ! val_wstr . missing ( ) )
result = parse_color ( val_wstr , is_background ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( highlight & HIGHLIGHT_VALID_PATH )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
env_var_t val2_wstr = env_get_string ( L " fish_color_valid_path " ) ;
const wcstring val2 = val2_wstr . missing ( ) ? L " " : val2_wstr . c_str ( ) ;
rgb_color_t result2 = parse_color ( val2 , is_background ) ;
if ( result . is_normal ( ) )
result = result2 ;
else
{
if ( result2 . is_bold ( ) )
result . set_bold ( true ) ;
if ( result2 . is_underline ( ) )
result . set_underline ( true ) ;
}
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
return result ;
2012-02-12 09:07:56 +08:00
}
2006-06-14 21:22:40 +08:00
2005-09-20 21:26:39 +08:00
2006-05-27 08:56:18 +08:00
/**
2012-05-08 03:55:13 +08:00
Highlight operators ( such as $ , ~ , % , as well as escaped characters .
2006-05-27 08:56:18 +08:00
*/
2012-11-19 08:30:30 +08:00
static void highlight_param ( const wcstring & buffstr , std : : vector < int > & colors , wcstring_list_t * error )
2012-05-08 03:55:13 +08:00
{
2012-03-01 08:14:03 +08:00
const wchar_t * const buff = buffstr . c_str ( ) ;
2012-11-19 08:30:30 +08:00
enum { e_unquoted , e_single_quoted , e_double_quoted } mode = e_unquoted ;
size_t in_pos , len = buffstr . size ( ) ;
int bracket_count = 0 ;
int normal_status = colors . at ( 0 ) ;
for ( in_pos = 0 ; in_pos < len ; in_pos + + )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
wchar_t c = buffstr . at ( in_pos ) ;
switch ( mode )
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
/*
Mode 0 means unquoted string
*/
case e_unquoted :
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
if ( c = = L ' \\ ' )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
size_t start_pos = in_pos ;
in_pos + + ;
if ( wcschr ( L " ~% " , buff [ in_pos ] ) )
2012-03-04 12:38:16 +08:00
{
2012-11-19 16:31:03 +08:00
if ( in_pos = = 1 )
{
colors . at ( start_pos ) = HIGHLIGHT_ESCAPE ;
colors . at ( in_pos + 1 ) = normal_status ;
}
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
else if ( buff [ in_pos ] = = L ' , ' )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( bracket_count )
{
colors . at ( start_pos ) = HIGHLIGHT_ESCAPE ;
colors . at ( in_pos + 1 ) = normal_status ;
}
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
else if ( wcschr ( L " abefnrtv*?$() { } [ ] ' \ " <>^ \\ #;|& " , buff [ in_pos ] ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
colors . at ( start_pos ) = HIGHLIGHT_ESCAPE ;
colors . at ( in_pos + 1 ) = normal_status ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
else if ( wcschr ( L " c " , buff [ in_pos ] ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
colors . at ( start_pos ) = HIGHLIGHT_ESCAPE ;
if ( in_pos + 2 < colors . size ( ) )
colors . at ( in_pos + 2 ) = normal_status ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
else if ( wcschr ( L " uUxX01234567 " , buff [ in_pos ] ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
int i ;
long long res = 0 ;
int chars = 2 ;
int base = 16 ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
wchar_t max_val = ASCII_MAX ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
switch ( buff [ in_pos ] )
{
case L ' u ' :
{
chars = 4 ;
max_val = UCS2_MAX ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' U ' :
{
chars = 8 ;
max_val = WCHAR_MAX ;
break ;
}
case L ' x ' :
{
break ;
}
case L ' X ' :
{
max_val = BYTE_MAX ;
break ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
default :
{
base = 8 ;
chars = 3 ;
in_pos - - ;
break ;
}
}
for ( i = 0 ; i < chars ; i + + )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
long d = convert_digit ( buff [ + + in_pos ] , base ) ;
if ( d < 0 )
{
in_pos - - ;
break ;
}
res = ( res * base ) | d ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
if ( ( res < = max_val ) )
{
colors . at ( start_pos ) = HIGHLIGHT_ESCAPE ;
colors . at ( in_pos + 1 ) = normal_status ;
}
else
{
colors . at ( start_pos ) = HIGHLIGHT_ERROR ;
colors . at ( in_pos + 1 ) = normal_status ;
}
2012-11-19 08:30:30 +08:00
}
}
2012-11-19 16:31:03 +08:00
else
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
switch ( buff [ in_pos ] )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
case L ' ~ ' :
case L ' % ' :
{
if ( in_pos = = 0 )
{
colors . at ( in_pos ) = HIGHLIGHT_OPERATOR ;
colors . at ( in_pos + 1 ) = normal_status ;
}
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' $ ' :
{
wchar_t n = buff [ in_pos + 1 ] ;
colors . at ( in_pos ) = ( n = = L ' $ ' | | wcsvarchr ( n ) ) ? HIGHLIGHT_OPERATOR : HIGHLIGHT_ERROR ;
colors . at ( in_pos + 1 ) = normal_status ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' * ' :
case L ' ? ' :
case L ' ( ' :
case L ' ) ' :
{
colors . at ( in_pos ) = HIGHLIGHT_OPERATOR ;
colors . at ( in_pos + 1 ) = normal_status ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' { ' :
{
colors . at ( in_pos ) = HIGHLIGHT_OPERATOR ;
colors . at ( in_pos + 1 ) = normal_status ;
bracket_count + + ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' } ' :
{
colors . at ( in_pos ) = HIGHLIGHT_OPERATOR ;
colors . at ( in_pos + 1 ) = normal_status ;
bracket_count - - ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' , ' :
{
if ( bracket_count )
{
colors . at ( in_pos ) = HIGHLIGHT_OPERATOR ;
colors . at ( in_pos + 1 ) = normal_status ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' \' ' :
{
colors . at ( in_pos ) = HIGHLIGHT_QUOTE ;
mode = e_single_quoted ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case L ' \" ' :
{
colors . at ( in_pos ) = HIGHLIGHT_QUOTE ;
mode = e_double_quoted ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
}
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
break ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 16:31:03 +08:00
/*
Mode 1 means single quoted string , i . e ' foo '
*/
case e_single_quoted :
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
if ( c = = L ' \\ ' )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
size_t start_pos = in_pos ;
switch ( buff [ + + in_pos ] )
{
case ' \\ ' :
case L ' \' ' :
{
colors . at ( start_pos ) = HIGHLIGHT_ESCAPE ;
colors . at ( in_pos + 1 ) = HIGHLIGHT_QUOTE ;
break ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case 0 :
{
return ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
}
if ( c = = L ' \' ' )
{
mode = e_unquoted ;
colors . at ( in_pos + 1 ) = normal_status ;
}
2012-11-19 08:30:30 +08:00
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
/*
Mode 2 means double quoted string , i . e . " foo "
*/
case e_double_quoted :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
switch ( c )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
case ' " ' :
{
mode = e_unquoted ;
colors . at ( in_pos + 1 ) = normal_status ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case ' \\ ' :
{
size_t start_pos = in_pos ;
switch ( buff [ + + in_pos ] )
{
case L ' \0 ' :
{
return ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case ' \\ ' :
case L ' $ ' :
case ' " ' :
{
colors . at ( start_pos ) = HIGHLIGHT_ESCAPE ;
colors . at ( in_pos + 1 ) = HIGHLIGHT_QUOTE ;
break ;
}
}
break ;
}
case ' $ ' :
{
wchar_t n = buff [ in_pos + 1 ] ;
colors . at ( in_pos ) = ( n = = L ' $ ' | | wcsvarchr ( n ) ) ? HIGHLIGHT_OPERATOR : HIGHLIGHT_ERROR ;
colors . at ( in_pos + 1 ) = HIGHLIGHT_QUOTE ;
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
}
break ;
2012-11-19 08:30:30 +08:00
}
}
2012-11-18 18:23:22 +08:00
}
2006-05-27 00:46:38 +08:00
}
2012-11-19 08:30:30 +08:00
static int has_expand_reserved ( const wchar_t * str )
2008-02-05 07:09:05 +08:00
{
2012-11-19 08:30:30 +08:00
while ( * str )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
if ( * str > = EXPAND_RESERVED & &
* str < = EXPAND_RESERVED_END )
{
return 1 ;
}
str + + ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
return 0 ;
2008-02-05 07:09:05 +08:00
}
2012-05-08 08:43:05 +08:00
/* Parse a command line. Return by reference the last command, its arguments, and the offset in the string of the beginning of the last argument. This is used by autosuggestions */
static bool autosuggest_parse_command ( const wcstring & str , wcstring * out_command , wcstring_list_t * out_arguments , int * out_last_arg_pos )
{
if ( str . empty ( ) )
return false ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:43:05 +08:00
wcstring cmd ;
wcstring_list_t args ;
int arg_pos = - 1 ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:43:05 +08:00
bool had_cmd = false ;
2012-11-22 09:48:35 +08:00
tokenizer_t tok ( str . c_str ( ) , TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS ) ;
for ( ; tok_has_next ( & tok ) ; tok_next ( & tok ) )
2012-05-08 08:43:05 +08:00
{
int last_type = tok_last_type ( & tok ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
switch ( last_type )
2012-05-08 03:55:13 +08:00
{
2012-11-19 16:31:03 +08:00
case TOK_STRING :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( had_cmd )
2012-05-08 03:55:13 +08:00
{
2012-11-19 16:31:03 +08:00
/* Parameter to the command. We store these escaped. */
args . push_back ( tok_last ( & tok ) ) ;
arg_pos = tok_get_pos ( & tok ) ;
2012-05-08 08:43:05 +08:00
}
else
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
/* Command. First check that the command actually exists. */
wcstring local_cmd = tok_last ( & tok ) ;
bool expanded = expand_one ( cmd , EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES ) ;
if ( ! expanded | | has_expand_reserved ( cmd . c_str ( ) ) )
{
/* We can't expand this cmd, ignore it */
}
else
2012-05-08 08:43:05 +08:00
{
2012-11-19 16:31:03 +08:00
bool is_subcommand = false ;
int mark = tok_get_pos ( & tok ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
if ( parser_keywords_is_subcommand ( cmd ) )
2012-05-08 03:55:13 +08:00
{
2012-11-19 16:31:03 +08:00
int sw ;
tok_next ( & tok ) ;
sw = parser_keywords_is_switch ( tok_last ( & tok ) ) ;
if ( ! parser_keywords_is_block ( cmd ) & &
sw = = ARG_SWITCH )
{
/* It's an argument to the subcommand itself */
}
else
{
if ( sw = = ARG_SKIP )
mark = tok_get_pos ( & tok ) ;
is_subcommand = true ;
}
tok_set_pos ( & tok , mark ) ;
2012-05-08 03:55:13 +08:00
}
2012-11-19 16:31:03 +08:00
if ( ! is_subcommand )
2012-05-08 08:43:05 +08:00
{
2012-11-19 16:31:03 +08:00
/* It's really a command */
had_cmd = true ;
cmd = local_cmd ;
2012-05-08 08:43:05 +08:00
}
2012-05-08 03:55:13 +08:00
}
2012-11-18 18:23:22 +08:00
2012-05-08 03:55:13 +08:00
}
2012-11-19 16:31:03 +08:00
break ;
}
case TOK_REDIRECT_NOCLOB :
case TOK_REDIRECT_OUT :
case TOK_REDIRECT_IN :
case TOK_REDIRECT_APPEND :
case TOK_REDIRECT_FD :
{
if ( ! had_cmd )
{
break ;
}
tok_next ( & tok ) ;
break ;
2012-05-08 03:55:13 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case TOK_PIPE :
case TOK_BACKGROUND :
case TOK_END :
2012-05-08 08:43:05 +08:00
{
2012-11-19 16:31:03 +08:00
had_cmd = false ;
2013-01-12 04:57:52 +08:00
cmd . clear ( ) ;
args . clear ( ) ;
2012-11-19 16:31:03 +08:00
arg_pos = - 1 ;
2012-05-08 08:43:05 +08:00
break ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case TOK_COMMENT :
case TOK_ERROR :
default :
{
break ;
}
2012-05-08 03:55:13 +08:00
}
2012-05-08 08:43:05 +08:00
}
2012-11-18 18:23:22 +08:00
2012-05-08 08:43:05 +08:00
/* Remember our command if we have one */
2012-11-19 08:30:30 +08:00
if ( had_cmd )
{
2012-05-08 08:43:05 +08:00
if ( out_command ) out_command - > swap ( cmd ) ;
if ( out_arguments ) out_arguments - > swap ( args ) ;
if ( out_last_arg_pos ) * out_last_arg_pos = arg_pos ;
}
return had_cmd ;
}
2012-05-08 03:55:13 +08:00
2012-07-07 05:34:53 +08:00
/* We have to return an escaped string here */
2012-11-19 08:30:30 +08:00
bool autosuggest_suggest_special ( const wcstring & str , const wcstring & working_directory , wcstring & outSuggestion )
{
2012-02-20 18:13:31 +08:00
if ( str . empty ( ) )
return false ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
ASSERT_IS_BACKGROUND_THREAD ( ) ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* Parse the string */
2012-05-08 08:43:05 +08:00
wcstring parsed_command ;
wcstring_list_t parsed_arguments ;
int parsed_last_arg_pos = - 1 ;
if ( ! autosuggest_parse_command ( str , & parsed_command , & parsed_arguments , & parsed_last_arg_pos ) )
2013-04-08 14:54:43 +08:00
{
2012-05-08 08:43:05 +08:00
return false ;
2013-04-08 14:54:43 +08:00
}
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
bool result = false ;
2012-11-19 08:30:30 +08:00
if ( parsed_command = = L " cd " & & ! parsed_arguments . empty ( ) )
{
2012-05-08 08:31:24 +08:00
/* We can possibly handle this specially */
2012-07-07 05:34:53 +08:00
const wcstring escaped_dir = parsed_arguments . back ( ) ;
2012-05-08 08:31:24 +08:00
wcstring suggested_path ;
2012-11-18 18:23:22 +08:00
2012-05-08 08:31:24 +08:00
/* We always return true because we recognized the command. This prevents us from falling back to dumber algorithms; for example we won't suggest a non-directory for the cd command. */
result = true ;
outSuggestion . clear ( ) ;
2012-11-18 18:23:22 +08:00
2012-07-07 05:34:53 +08:00
/* Unescape the parameter */
2013-11-25 14:57:49 +08:00
wcstring unescaped_dir ;
bool unescaped = unescape_string ( escaped_dir , & unescaped_dir , UNESCAPE_INCOMPLETE ) ;
2012-11-18 18:23:22 +08:00
2012-07-07 05:34:53 +08:00
/* Determine the quote type we got from the input directory. */
wchar_t quote = L ' \0 ' ;
parse_util_get_parameter_info ( escaped_dir , 0 , & quote , NULL , NULL ) ;
2012-11-18 18:23:22 +08:00
2012-07-07 05:34:53 +08:00
/* Big hack to avoid expanding a tilde inside quotes */
path_flags_t path_flags = ( quote = = L ' \0 ' ) ? PATH_EXPAND_TILDE : 0 ;
2012-08-24 02:21:35 +08:00
if ( unescaped & & is_potential_cd_path ( unescaped_dir , working_directory , path_flags , & suggested_path ) )
{
2012-11-18 18:23:22 +08:00
2012-07-07 05:34:53 +08:00
/* Note: this looks really wrong for strings that have an "unescapable" character in them, e.g. a \t, because parse_util_escape_string_with_quote will insert that character */
wcstring escaped_suggested_path = parse_util_escape_string_with_quote ( suggested_path , quote ) ;
2012-05-14 11:19:02 +08:00
2012-07-07 05:34:53 +08:00
/* Return it */
2012-05-08 08:31:24 +08:00
outSuggestion = str ;
2012-05-08 08:43:05 +08:00
outSuggestion . erase ( parsed_last_arg_pos ) ;
2012-07-07 05:34:53 +08:00
if ( quote ! = L ' \0 ' ) outSuggestion . push_back ( quote ) ;
outSuggestion . append ( escaped_suggested_path ) ;
if ( quote ! = L ' \0 ' ) outSuggestion . push_back ( quote ) ;
2012-05-08 08:31:24 +08:00
}
2012-11-19 08:30:30 +08:00
}
else
{
2012-05-08 08:31:24 +08:00
/* Either an error or some other command, so we don't handle it specially */
2012-03-31 02:16:24 +08:00
}
2012-05-08 08:31:24 +08:00
return result ;
2012-02-20 18:13:31 +08:00
}
2012-11-19 08:30:30 +08:00
bool autosuggest_validate_from_history ( const history_item_t & item , file_detection_context_t & detector , const wcstring & working_directory , const env_vars_snapshot_t & vars )
{
2012-02-19 10:54:36 +08:00
ASSERT_IS_BACKGROUND_THREAD ( ) ;
2012-06-29 02:54:37 +08:00
2012-05-08 03:55:13 +08:00
bool handled = false , suggestionOK = false ;
2012-05-08 08:43:05 +08:00
2012-11-18 18:23:22 +08:00
/* Parse the string */
2012-05-08 08:43:05 +08:00
wcstring parsed_command ;
wcstring_list_t parsed_arguments ;
int parsed_last_arg_pos = - 1 ;
2012-06-29 02:54:37 +08:00
if ( ! autosuggest_parse_command ( item . str ( ) , & parsed_command , & parsed_arguments , & parsed_last_arg_pos ) )
2012-05-08 08:43:05 +08:00
return false ;
2012-11-19 08:30:30 +08:00
if ( parsed_command = = L " cd " & & ! parsed_arguments . empty ( ) )
{
2012-05-08 03:55:13 +08:00
/* We can possibly handle this specially */
2012-05-08 08:43:05 +08:00
wcstring dir = parsed_arguments . back ( ) ;
2012-05-08 03:55:13 +08:00
if ( expand_one ( dir , EXPAND_SKIP_CMDSUBST ) )
{
handled = true ;
bool is_help = string_prefixes_string ( dir , L " --help " ) | | string_prefixes_string ( dir , L " -h " ) ;
2012-11-19 08:30:30 +08:00
if ( is_help )
{
2012-05-08 03:55:13 +08:00
suggestionOK = false ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-07-21 13:11:05 +08:00
wcstring path ;
bool can_cd = path_get_cdpath ( dir , & path , working_directory . c_str ( ) , vars ) ;
2012-11-19 08:30:30 +08:00
if ( ! can_cd )
{
2012-05-08 03:55:13 +08:00
suggestionOK = false ;
2012-11-19 08:30:30 +08:00
}
else if ( paths_are_same_file ( working_directory , path ) )
{
2012-05-08 03:55:13 +08:00
/* Don't suggest the working directory as the path! */
suggestionOK = false ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-05-08 03:55:13 +08:00
suggestionOK = true ;
}
}
2012-11-18 18:23:22 +08:00
}
}
2012-06-29 02:54:37 +08:00
/* If not handled specially, handle it here */
2012-11-19 08:30:30 +08:00
if ( ! handled )
{
2012-06-29 02:54:37 +08:00
bool cmd_ok = false ;
2012-07-21 11:39:31 +08:00
if ( path_get_path ( parsed_command , NULL ) )
{
2012-06-29 02:54:37 +08:00
cmd_ok = true ;
}
2012-07-21 11:39:31 +08:00
else if ( builtin_exists ( parsed_command ) | | function_exists_no_autoload ( parsed_command , vars ) )
{
2012-06-29 02:54:37 +08:00
cmd_ok = true ;
}
2012-11-19 08:30:30 +08:00
if ( cmd_ok )
{
2012-06-29 02:54:37 +08:00
const path_list_t & paths = item . get_required_paths ( ) ;
2012-11-19 08:30:30 +08:00
if ( paths . empty ( ) )
{
2012-06-29 02:54:37 +08:00
suggestionOK = true ;
}
2012-11-19 08:30:30 +08:00
else
{
2012-06-29 02:54:37 +08:00
detector . potential_paths = paths ;
suggestionOK = detector . paths_are_valid ( paths ) ;
}
}
2012-02-19 10:54:36 +08:00
}
2012-06-29 02:54:37 +08:00
return suggestionOK ;
2012-02-19 10:54:36 +08:00
}
2011-12-27 11:18:46 +08:00
2012-02-11 09:54:21 +08:00
// This function does I/O
2012-11-19 08:30:30 +08:00
static void tokenize ( const wchar_t * const buff , std : : vector < int > & color , const size_t pos , wcstring_list_t * error , const wcstring & working_directory , const env_vars_snapshot_t & vars )
{
2011-12-27 11:18:46 +08:00
ASSERT_IS_BACKGROUND_THREAD ( ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
wcstring cmd ;
int had_cmd = 0 ;
wcstring last_cmd ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
int accept_switches = 1 ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
int use_function = 1 ;
int use_command = 1 ;
int use_builtin = 1 ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
CHECK ( buff , ) ;
2010-09-18 09:51:16 +08:00
2012-08-05 04:02:44 +08:00
if ( buff [ 0 ] = = L ' \0 ' )
return ;
2010-09-18 09:51:16 +08:00
2012-11-18 18:23:22 +08:00
std : : fill ( color . begin ( ) , color . end ( ) , - 1 ) ;
2010-09-18 09:51:16 +08:00
2012-11-22 09:48:35 +08:00
tokenizer_t tok ( buff , TOK_SHOW_COMMENTS | TOK_SQUASH_ERRORS ) ;
for ( ; tok_has_next ( & tok ) ; tok_next ( & tok ) )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
int last_type = tok_last_type ( & tok ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
switch ( last_type )
{
2012-11-19 16:31:03 +08:00
case TOK_STRING :
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
if ( had_cmd )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
/*Parameter */
2012-11-22 14:09:35 +08:00
const wchar_t * param = tok_last ( & tok ) ;
2012-11-19 16:31:03 +08:00
if ( param [ 0 ] = = L ' - ' )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( wcscmp ( param , L " -- " ) = = 0 )
{
accept_switches = 0 ;
2012-11-19 08:30:30 +08:00
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_PARAM ;
2012-11-19 16:31:03 +08:00
}
else if ( accept_switches )
{
if ( complete_is_valid_option ( last_cmd , param , error , false /* no autoload */ ) )
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_PARAM ;
else
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
}
2012-11-19 08:30:30 +08:00
else
2012-11-19 16:31:03 +08:00
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_PARAM ;
}
2012-11-19 08:30:30 +08:00
}
else
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_PARAM ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
if ( cmd = = L " cd " )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
wcstring dir = tok_last ( & tok ) ;
if ( expand_one ( dir , EXPAND_SKIP_CMDSUBST ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
int is_help = string_prefixes_string ( dir , L " --help " ) | | string_prefixes_string ( dir , L " -h " ) ;
if ( ! is_help & & ! is_potential_cd_path ( dir , working_directory , PATH_EXPAND_TILDE , NULL ) )
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
}
2012-11-19 08:30:30 +08:00
}
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
/* Highlight the parameter. highlight_param wants to write one more color than we have characters (hysterical raisins) so allocate one more in the vector. But don't copy it back. */
const wcstring param_str = param ;
size_t tok_pos = tok_get_pos ( & tok ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
std : : vector < int > : : const_iterator where = color . begin ( ) + tok_pos ;
std : : vector < int > subcolors ( where , where + param_str . size ( ) ) ;
subcolors . push_back ( - 1 ) ;
highlight_param ( param_str , subcolors , error ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
/* Copy the subcolors back into our colors array */
std : : copy ( subcolors . begin ( ) , subcolors . begin ( ) + param_str . size ( ) , color . begin ( ) + tok_pos ) ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
else
{
2012-11-19 16:31:03 +08:00
/*
Command . First check that the command actually exists .
*/
cmd = tok_last ( & tok ) ;
bool expanded = expand_one ( cmd , EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES | EXPAND_SKIP_JOBS ) ;
if ( ! expanded | | has_expand_reserved ( cmd . c_str ( ) ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
}
else
{
bool is_cmd = false ;
int is_subcommand = 0 ;
int mark = tok_get_pos ( & tok ) ;
2013-07-27 18:45:48 +08:00
color . at ( tok_get_pos ( & tok ) ) = use_builtin ? HIGHLIGHT_COMMAND : HIGHLIGHT_ERROR ;
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
if ( parser_keywords_is_subcommand ( cmd ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
int sw ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( cmd = = L " builtin " )
{
use_function = 0 ;
use_command = 0 ;
use_builtin = 1 ;
}
2013-07-27 18:45:48 +08:00
else if ( cmd = = L " command " | | cmd = = L " exec " )
2012-11-19 16:31:03 +08:00
{
use_command = 1 ;
use_function = 0 ;
use_builtin = 0 ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
tok_next ( & tok ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
sw = parser_keywords_is_switch ( tok_last ( & tok ) ) ;
if ( ! parser_keywords_is_block ( cmd ) & &
sw = = ARG_SWITCH )
2011-12-27 11:18:46 +08:00
{
2012-11-19 16:31:03 +08:00
/*
The ' builtin ' and ' command ' builtins
are normally followed by another
command , but if they are invoked
with a switch , they aren ' t .
*/
use_command = 1 ;
use_function = 1 ;
use_builtin = 2 ;
2011-12-27 11:18:46 +08:00
}
2012-11-19 16:31:03 +08:00
else
{
if ( sw = = ARG_SKIP )
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_PARAM ;
mark = tok_get_pos ( & tok ) ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
is_subcommand = 1 ;
}
tok_set_pos ( & tok , mark ) ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
if ( ! is_subcommand )
{
/*
OK , this is a command , it has been
successfully expanded and everything
looks ok . Lets check if the command
exists .
*/
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/*
First check if it is a builtin or
function , since we don ' t have to stat
any files for that
*/
if ( ! is_cmd & & use_builtin )
is_cmd = builtin_exists ( cmd ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( ! is_cmd & & use_function )
is_cmd = function_exists_no_autoload ( cmd , vars ) ;
2012-11-19 08:30:30 +08:00
2013-07-17 17:55:15 +08:00
if ( ! is_cmd )
is_cmd = expand_abbreviation ( cmd , NULL ) ;
2012-11-19 16:31:03 +08:00
/*
Moving on to expensive tests
*/
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/*
Check if this is a regular command
*/
if ( ! is_cmd & & use_command )
{
is_cmd = path_get_path ( cmd , NULL , vars ) ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* Maybe it is a path for a implicit cd command. */
if ( ! is_cmd )
{
if ( use_builtin | | ( use_function & & function_exists_no_autoload ( L " cd " , vars ) ) )
is_cmd = path_can_be_implicit_cd ( cmd , NULL , working_directory . c_str ( ) , vars ) ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( is_cmd )
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_COMMAND ;
}
else
2012-06-03 05:04:25 +08:00
{
2012-11-19 16:31:03 +08:00
if ( error )
{
error - > push_back ( format_string ( L " Unknown command \' %ls \' " , cmd . c_str ( ) ) ) ;
}
color . at ( tok_get_pos ( & tok ) ) = ( HIGHLIGHT_ERROR ) ;
2012-06-03 05:04:25 +08:00
}
2012-11-19 16:31:03 +08:00
had_cmd = 1 ;
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
if ( had_cmd )
{
last_cmd = tok_last ( & tok ) ;
}
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
}
2012-11-19 08:30:30 +08:00
break ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
case TOK_REDIRECT_NOCLOB :
case TOK_REDIRECT_OUT :
case TOK_REDIRECT_IN :
case TOK_REDIRECT_APPEND :
case TOK_REDIRECT_FD :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( ! had_cmd )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
if ( error )
error - > push_back ( L " Redirection without a command " ) ;
break ;
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
wcstring target_str ;
const wchar_t * target = NULL ;
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_REDIRECTION ;
tok_next ( & tok ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
/*
2012-11-19 16:31:03 +08:00
Check that we are redirecting into a file
2012-11-19 08:30:30 +08:00
*/
2012-11-19 16:31:03 +08:00
switch ( tok_last_type ( & tok ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
case TOK_STRING :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
target_str = tok_last ( & tok ) ;
if ( expand_one ( target_str , EXPAND_SKIP_CMDSUBST ) )
{
target = target_str . c_str ( ) ;
}
/*
Redirect filename may contain a cmdsubst .
If so , it will be ignored / not flagged .
*/
}
break ;
default :
{
size_t pos = tok_get_pos ( & tok ) ;
if ( pos < color . size ( ) )
{
color . at ( pos ) = HIGHLIGHT_ERROR ;
}
2012-11-19 08:30:30 +08:00
if ( error )
2012-11-19 16:31:03 +08:00
error - > push_back ( L " Invalid redirection " ) ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 16:31:03 +08:00
if ( target ! = 0 )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
wcstring dir = target ;
size_t slash_idx = dir . find_last_of ( L ' / ' ) ;
struct stat buff ;
/*
If file is in directory other than ' . ' , check
that the directory exists .
*/
if ( slash_idx ! = wcstring : : npos )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
dir . resize ( slash_idx ) ;
if ( wstat ( dir , & buff ) = = - 1 )
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
if ( error )
error - > push_back ( format_string ( L " Directory \' %ls \' does not exist " , dir . c_str ( ) ) ) ;
}
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
/*
If the file is read from or appended to , check
if it exists .
*/
if ( last_type = = TOK_REDIRECT_IN | |
last_type = = TOK_REDIRECT_APPEND )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( wstat ( target , & buff ) = = - 1 )
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
if ( error )
error - > push_back ( format_string ( L " File \' %ls \' does not exist " , target ) ) ;
}
}
if ( last_type = = TOK_REDIRECT_NOCLOB )
{
if ( wstat ( target , & buff ) ! = - 1 )
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
if ( error )
error - > push_back ( format_string ( L " File \' %ls \' exists " , target ) ) ;
}
2012-11-19 08:30:30 +08:00
}
}
2012-11-19 16:31:03 +08:00
break ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case TOK_PIPE :
case TOK_BACKGROUND :
{
if ( had_cmd )
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_END ;
had_cmd = 0 ;
use_command = 1 ;
use_function = 1 ;
use_builtin = 1 ;
accept_switches = 1 ;
}
else
{
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
if ( error )
error - > push_back ( L " No job to put in background " ) ;
}
break ;
}
case TOK_END :
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_END ;
had_cmd = 0 ;
use_command = 1 ;
use_function = 1 ;
use_builtin = 1 ;
accept_switches = 1 ;
2012-11-19 16:31:03 +08:00
break ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 16:31:03 +08:00
case TOK_COMMENT :
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_COMMENT ;
break ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case TOK_ERROR :
default :
{
/*
If the tokenizer reports an error , highlight it as such .
*/
if ( error )
error - > push_back ( tok_last ( & tok ) ) ;
color . at ( tok_get_pos ( & tok ) ) = HIGHLIGHT_ERROR ;
break ;
}
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
}
2011-12-27 11:18:46 +08:00
}
2005-09-20 21:26:39 +08:00
2011-12-27 11:18:46 +08:00
2012-06-03 05:04:25 +08:00
// PCA This function does I/O, (calls is_potential_path, path_get_path, maybe others) and so ought to only run on a background thread
2012-11-19 08:30:30 +08:00
void highlight_shell ( const wcstring & buff , std : : vector < int > & color , size_t pos , wcstring_list_t * error , const env_vars_snapshot_t & vars )
2011-12-27 11:18:46 +08:00
{
ASSERT_IS_BACKGROUND_THREAD ( ) ;
2012-11-18 18:23:22 +08:00
2012-02-22 09:55:56 +08:00
const size_t length = buff . size ( ) ;
assert ( buff . size ( ) = = color . size ( ) ) ;
2012-07-18 03:47:01 +08:00
2012-11-19 08:30:30 +08:00
if ( length = = 0 )
return ;
2012-11-18 18:23:22 +08:00
2012-02-22 03:45:13 +08:00
std : : fill ( color . begin ( ) , color . end ( ) , - 1 ) ;
2011-12-27 11:18:46 +08:00
2013-07-17 16:35:30 +08:00
/* Do something sucky and get the current working directory on this background thread. This should really be passed in. */
2013-04-27 15:45:38 +08:00
const wcstring working_directory = env_get_pwd_slash ( ) ;
2012-05-08 08:52:02 +08:00
2011-12-27 11:18:46 +08:00
/* Tokenize the string */
2012-05-08 08:52:02 +08:00
tokenize ( buff . c_str ( ) , color , pos , error , working_directory , vars ) ;
2010-09-18 09:51:16 +08:00
2013-04-27 15:45:38 +08:00
/* Locate and syntax highlight cmdsubsts recursively */
2005-10-25 19:03:52 +08:00
2012-11-19 08:30:30 +08:00
wchar_t * const subbuff = wcsdup ( buff . c_str ( ) ) ;
2012-02-14 09:15:41 +08:00
wchar_t * subpos = subbuff ;
2013-04-20 01:21:36 +08:00
bool done = false ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
while ( 1 )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
wchar_t * begin , * end ;
2012-11-18 18:23:22 +08:00
2013-07-17 16:35:30 +08:00
if ( parse_util_locate_cmdsubst ( subpos , & begin , & end , true ) < = 0 )
2012-11-19 08:30:30 +08:00
{
break ;
}
2013-04-20 01:21:36 +08:00
/* Note: This *end = 0 writes into subbuff! */
2012-11-19 08:30:30 +08:00
if ( ! * end )
2013-04-20 01:21:36 +08:00
done = true ;
2012-11-19 08:30:30 +08:00
else
2013-04-20 01:21:36 +08:00
* end = 0 ;
2012-11-18 18:23:22 +08:00
2012-02-22 03:45:13 +08:00
//our subcolors start at color + (begin-subbuff)+1
size_t start = begin - subbuff + 1 , len = wcslen ( begin + 1 ) ;
2012-05-09 17:33:42 +08:00
std : : vector < int > subcolors ( len , - 1 ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
highlight_shell ( begin + 1 , subcolors , - 1 , error , vars ) ;
2012-11-18 18:23:22 +08:00
2012-02-22 03:45:13 +08:00
// insert subcolors
std : : copy ( subcolors . begin ( ) , subcolors . end ( ) , color . begin ( ) + start ) ;
2012-11-18 18:23:22 +08:00
2012-02-22 03:45:13 +08:00
// highlight the end of the subcommand
assert ( end > = subbuff ) ;
2012-11-19 08:30:30 +08:00
if ( ( size_t ) ( end - subbuff ) < length )
{
2012-02-22 03:45:13 +08:00
color . at ( end - subbuff ) = HIGHLIGHT_OPERATOR ;
}
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( done )
break ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
subpos = end + 1 ;
}
2012-02-14 09:15:41 +08:00
free ( subbuff ) ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/*
The highlighting code only changes the first element when the
color changes . This fills in the rest .
*/
int last_val = 0 ;
for ( size_t i = 0 ; i < buff . size ( ) ; i + + )
{
2013-04-20 01:21:36 +08:00
int & current_val = color . at ( i ) ;
2013-05-05 17:33:17 +08:00
if ( current_val > = 0 )
{
2013-04-20 01:21:36 +08:00
last_val = current_val ;
2013-05-05 17:33:17 +08:00
}
else
{
2013-04-20 01:21:36 +08:00
current_val = last_val ; //note - this writes into the vector
}
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
/*
Color potentially valid paths in a special path color if they
are the current token .
For reasons that I don ' t yet understand , it ' s required that pos be allowed to be length ( e . g . when backspacing ) .
*/
if ( pos < = length )
{
2012-11-18 18:23:22 +08:00
2012-02-22 09:55:56 +08:00
const wchar_t * cbuff = buff . c_str ( ) ;
2012-11-19 08:30:30 +08:00
const wchar_t * tok_begin , * tok_end ;
parse_util_token_extent ( cbuff , pos , & tok_begin , & tok_end , 0 , 0 ) ;
if ( tok_begin & & tok_end )
{
wcstring token ( tok_begin , tok_end - tok_begin ) ;
2013-11-25 14:57:49 +08:00
if ( unescape_string_in_place ( & token , UNESCAPE_SPECIAL ) )
2012-08-24 02:21:35 +08:00
{
/* Big hack: is_potential_path expects a tilde, but unescape_string gives us HOME_DIRECTORY. Put it back. */
if ( ! token . empty ( ) & & token . at ( 0 ) = = HOME_DIRECTORY )
token . at ( 0 ) = L ' ~ ' ;
2013-11-25 14:57:49 +08:00
const wcstring_list_t working_directory_list ( 1 , working_directory ) ;
2012-08-24 02:21:35 +08:00
if ( is_potential_path ( token , working_directory_list , PATH_EXPAND_TILDE ) )
{
2012-11-19 08:30:30 +08:00
for ( ptrdiff_t i = tok_begin - cbuff ; i < ( tok_end - cbuff ) ; i + + )
2012-08-24 02:21:35 +08:00
{
// Don't color HIGHLIGHT_ERROR because it looks dorky. For example, trying to cd into a non-directory would show an underline and also red.
2012-11-19 08:30:30 +08:00
if ( ! ( color . at ( i ) & HIGHLIGHT_ERROR ) )
{
2012-08-24 02:21:35 +08:00
color . at ( i ) | = HIGHLIGHT_VALID_PATH ;
}
2012-02-22 09:55:56 +08:00
}
2012-08-24 02:21:35 +08:00
}
}
2012-11-19 08:30:30 +08:00
}
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
highlight_universal_internal ( buff , color , pos ) ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
/*
Spaces should not be highlighted at all , since it makes cursor look funky in some terminals
*/
for ( size_t i = 0 ; i < buff . size ( ) ; i + + )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
if ( iswspace ( buff . at ( i ) ) )
{
color . at ( i ) = 0 ;
}
2012-11-18 18:23:22 +08:00
}
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
/**
Perform quote and parenthesis highlighting on the specified string .
*/
2012-11-19 08:30:30 +08:00
static void highlight_universal_internal ( const wcstring & buffstr , std : : vector < int > & color , size_t pos )
2012-11-18 18:23:22 +08:00
{
2012-02-22 09:55:56 +08:00
assert ( buffstr . size ( ) = = color . size ( ) ) ;
2012-11-19 08:30:30 +08:00
if ( pos < buffstr . size ( ) )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
/*
Highlight matching quotes
*/
if ( ( buffstr . at ( pos ) = = L ' \' ' ) | | ( buffstr . at ( pos ) = = L ' \" ' ) )
{
2012-12-21 09:37:09 +08:00
std : : vector < size_t > lst ;
2012-11-19 08:30:30 +08:00
int level = 0 ;
wchar_t prev_q = 0 ;
2012-11-18 18:23:22 +08:00
2012-02-22 09:55:56 +08:00
const wchar_t * const buff = buffstr . c_str ( ) ;
2012-11-19 08:30:30 +08:00
const wchar_t * str = buff ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
int match_found = 0 ;
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
while ( * str )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
switch ( * str )
2012-11-18 18:23:22 +08:00
{
2012-11-19 16:31:03 +08:00
case L ' \\ ' :
str + + ;
break ;
case L ' \" ' :
case L ' \' ' :
if ( level = = 0 )
2012-11-19 08:30:30 +08:00
{
level + + ;
lst . push_back ( str - buff ) ;
prev_q = * str ;
}
2012-11-19 16:31:03 +08:00
else
{
if ( prev_q = = * str )
{
2012-12-21 09:37:09 +08:00
size_t pos1 , pos2 ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
level - - ;
pos1 = lst . back ( ) ;
pos2 = str - buff ;
if ( pos1 = = pos | | pos2 = = pos )
{
color . at ( pos1 ) | = HIGHLIGHT_MATCH < < 16 ;
color . at ( pos2 ) | = HIGHLIGHT_MATCH < < 16 ;
match_found = 1 ;
}
prev_q = * str = = L ' \" ' ? L ' \' ' : L ' \" ' ;
}
else
{
level + + ;
lst . push_back ( str - buff ) ;
prev_q = * str ;
}
}
break ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
if ( ( * str = = L ' \0 ' ) )
break ;
str + + ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
if ( ! match_found )
color . at ( pos ) = HIGHLIGHT_ERROR < < 16 ;
2012-11-18 18:23:22 +08:00
}
2012-11-19 08:30:30 +08:00
/*
Highlight matching parenthesis
*/
2012-02-22 09:55:56 +08:00
const wchar_t c = buffstr . at ( pos ) ;
2012-11-19 08:30:30 +08:00
if ( wcschr ( L " ()[]{} " , c ) )
2012-11-18 18:23:22 +08:00
{
2012-11-19 08:30:30 +08:00
int step = wcschr ( L " ({[ " , c ) ? 1 : - 1 ;
wchar_t dec_char = * ( wcschr ( L " ()[]{} " , c ) + step ) ;
wchar_t inc_char = c ;
int level = 0 ;
int match_found = 0 ;
for ( long i = pos ; i > = 0 & & ( size_t ) i < buffstr . size ( ) ; i + = step )
{
const wchar_t test_char = buffstr . at ( i ) ;
if ( test_char = = inc_char )
level + + ;
if ( test_char = = dec_char )
level - - ;
if ( level = = 0 )
{
long pos2 = i ;
color . at ( pos ) | = HIGHLIGHT_MATCH < < 16 ;
color . at ( pos2 ) | = HIGHLIGHT_MATCH < < 16 ;
match_found = 1 ;
break ;
}
}
2012-11-18 18:23:22 +08:00
2012-11-19 08:30:30 +08:00
if ( ! match_found )
color [ pos ] = HIGHLIGHT_ERROR < < 16 ;
}
2012-11-18 18:23:22 +08:00
}
2005-09-20 21:26:39 +08:00
}
2012-11-19 08:30:30 +08:00
void highlight_universal ( const wcstring & buff , std : : vector < int > & color , size_t pos , wcstring_list_t * error , const env_vars_snapshot_t & vars )
2005-09-20 21:26:39 +08:00
{
2012-02-22 09:55:56 +08:00
assert ( buff . size ( ) = = color . size ( ) ) ;
2012-11-18 18:23:22 +08:00
std : : fill ( color . begin ( ) , color . end ( ) , 0 ) ;
2012-11-19 08:30:30 +08:00
highlight_universal_internal ( buff , color , pos ) ;
2005-09-20 21:26:39 +08:00
}