2005-09-20 21:26:39 +08:00
|
|
|
/** \file highlight.c
|
|
|
|
Functions for syntax highlighting
|
|
|
|
*/
|
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-06-12 22:12:33 +08:00
|
|
|
#include "halloc.h"
|
|
|
|
#include "halloc_util.h"
|
2006-07-28 21:52:03 +08:00
|
|
|
#include "wildcard.h"
|
2006-10-19 19:50:23 +08:00
|
|
|
#include "path.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 *) )
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
static void highlight_universal_internal( const wchar_t * buff,
|
|
|
|
int *color,
|
|
|
|
int pos );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The environment variables used to specify the color of different tokens.
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
static const wchar_t *highlight_var[] =
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
L"fish_color_normal",
|
2006-06-14 21:22:40 +08:00
|
|
|
L"fish_color_error",
|
2005-09-20 21:26:39 +08:00
|
|
|
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",
|
2006-05-27 00:46:38 +08:00
|
|
|
L"fish_color_operator",
|
2006-05-27 20:35:16 +08:00
|
|
|
L"fish_color_escape",
|
2006-06-14 21:22:40 +08:00
|
|
|
L"fish_color_quote",
|
|
|
|
L"fish_color_redirection",
|
|
|
|
L"fish_color_valid_path"
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2006-06-20 08:50:10 +08:00
|
|
|
/**
|
2011-12-27 11:18:46 +08:00
|
|
|
Tests if the specified string is the prefix of any valid path in the system.
|
2006-06-20 08:50:10 +08:00
|
|
|
|
|
|
|
\return zero it this is not a valid prefix, non-zero otherwise
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
// PCA DOES_IO
|
2012-01-03 05:40:03 +08:00
|
|
|
static bool is_potential_path( const wcstring &cpath )
|
2006-06-14 21:22:40 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
ASSERT_IS_BACKGROUND_THREAD();
|
|
|
|
|
|
|
|
const wchar_t *unescaped, *in;
|
|
|
|
wcstring cleaned_path;
|
2006-06-14 21:22:40 +08:00
|
|
|
int has_magic = 0;
|
2012-01-03 05:40:03 +08:00
|
|
|
bool res = false;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
wcstring path(cpath);
|
|
|
|
expand_tilde(path);
|
2012-01-03 05:40:03 +08:00
|
|
|
if (! unescape_string(path, 1))
|
|
|
|
return false;
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
unescaped = path.c_str();
|
|
|
|
|
|
|
|
// debug( 1, L"%ls -> %ls ->%ls", path, tilde, unescaped );
|
|
|
|
|
|
|
|
for( in = unescaped; *in; in++ )
|
|
|
|
{
|
|
|
|
switch( *in )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
case INTERNAL_SEPARATOR:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
cleaned_path += *in;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !has_magic && cleaned_path.length() )
|
|
|
|
{
|
|
|
|
int must_be_dir = 0;
|
|
|
|
DIR *dir;
|
|
|
|
must_be_dir = cleaned_path[cleaned_path.length()-1] == L'/';
|
|
|
|
if( must_be_dir )
|
|
|
|
{
|
|
|
|
dir = wopendir( cleaned_path.c_str() );
|
|
|
|
res = !!dir;
|
|
|
|
if( dir )
|
|
|
|
{
|
|
|
|
closedir( dir );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wcstring dir_name = wdirname(cleaned_path);
|
|
|
|
wcstring base_name = wbasename(cleaned_path);
|
|
|
|
|
|
|
|
if( dir_name == L"/" && base_name == L"/" )
|
|
|
|
{
|
2012-01-03 05:40:03 +08:00
|
|
|
res = true;
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
|
|
|
else if( (dir = wopendir( dir_name.c_str() )) )
|
|
|
|
{
|
|
|
|
wcstring ent;
|
|
|
|
while (wreaddir(dir, ent))
|
|
|
|
{
|
|
|
|
if( wcsncmp( base_name.c_str(), base_name.c_str(), base_name.length() ) == 0 )
|
|
|
|
{
|
2012-01-03 05:40:03 +08:00
|
|
|
res = true;
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir( dir );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
return res;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
int highlight_get_color( int highlight )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
size_t i;
|
2006-06-14 21:22:40 +08:00
|
|
|
int idx=0;
|
|
|
|
int result = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( highlight < 0 )
|
|
|
|
return FISH_COLOR_NORMAL;
|
2006-06-14 21:22:40 +08:00
|
|
|
if( highlight >= (1<<VAR_COUNT) )
|
2005-09-20 21:26:39 +08:00
|
|
|
return FISH_COLOR_NORMAL;
|
2006-06-14 21:22:40 +08:00
|
|
|
|
|
|
|
for( i=0; i<(VAR_COUNT-1); i++ )
|
|
|
|
{
|
|
|
|
if( highlight & (1<<i ))
|
|
|
|
{
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-14 18:42:17 +08:00
|
|
|
env_var_t val_wstr = env_get_string( highlight_var[idx]);
|
2006-06-14 21:22:40 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
// debug( 1, L"%d -> %d -> %ls", highlight, idx, val );
|
|
|
|
|
2012-01-14 18:42:17 +08:00
|
|
|
if (val_wstr.missing())
|
2012-01-13 01:49:05 +08:00
|
|
|
val_wstr = env_get_string( highlight_var[0]);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-14 18:42:17 +08:00
|
|
|
if( ! val_wstr.missing() )
|
|
|
|
result = output_color_code( val_wstr.c_str() );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
if( highlight & HIGHLIGHT_VALID_PATH )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-01-14 18:42:17 +08:00
|
|
|
env_var_t val2_wstr = env_get_string( L"fish_color_valid_path" );
|
|
|
|
const wchar_t *val2 = val2_wstr.missing() ? NULL : val2_wstr.c_str();
|
2012-01-13 01:49:05 +08:00
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
int result2 = output_color_code( val2 );
|
|
|
|
if( result == FISH_COLOR_NORMAL )
|
|
|
|
result = result2;
|
2011-12-27 11:18:46 +08:00
|
|
|
else
|
2006-06-14 21:22:40 +08:00
|
|
|
{
|
|
|
|
if( result2 & FISH_COLOR_BOLD )
|
|
|
|
result |= FISH_COLOR_BOLD;
|
|
|
|
if( result2 & FISH_COLOR_UNDERLINE )
|
|
|
|
result |= FISH_COLOR_UNDERLINE;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-06-14 21:22:40 +08:00
|
|
|
return result;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2006-05-27 08:56:18 +08:00
|
|
|
/**
|
|
|
|
Highligt operators (such as $, ~, %, as well as escaped characters.
|
|
|
|
*/
|
2006-05-27 00:46:38 +08:00
|
|
|
static void highlight_param( const wchar_t * buff,
|
|
|
|
int *color,
|
|
|
|
int pos,
|
|
|
|
array_list_t *error )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
int mode = 0;
|
2006-05-27 00:46:38 +08:00
|
|
|
int in_pos, len = wcslen( buff );
|
|
|
|
int bracket_count=0;
|
|
|
|
wchar_t c;
|
2006-06-17 18:41:28 +08:00
|
|
|
int normal_status = *color;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
for( in_pos=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
in_pos<len;
|
2006-05-27 00:46:38 +08:00
|
|
|
in_pos++ )
|
|
|
|
{
|
|
|
|
c = buff[in_pos];
|
|
|
|
switch( mode )
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mode 0 means unquoted string
|
|
|
|
*/
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
if( c == L'\\' )
|
|
|
|
{
|
|
|
|
int start_pos = in_pos;
|
2006-05-30 08:35:35 +08:00
|
|
|
in_pos++;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
if( wcschr( L"~%", buff[in_pos] ) )
|
2006-05-27 00:46:38 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
if( in_pos == 1 )
|
2006-05-27 21:40:26 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
color[start_pos] = HIGHLIGHT_ESCAPE;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 21:40:26 +08:00
|
|
|
}
|
2006-05-30 08:35:35 +08:00
|
|
|
}
|
|
|
|
else if( buff[in_pos]==L',' )
|
|
|
|
{
|
|
|
|
if( bracket_count )
|
2006-05-27 21:40:26 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
color[start_pos] = HIGHLIGHT_ESCAPE;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 21:40:26 +08:00
|
|
|
}
|
2006-05-30 08:35:35 +08:00
|
|
|
}
|
2007-09-25 19:55:14 +08:00
|
|
|
else if( wcschr( L"abefnrtv*?$(){}[]'\"<>^ \\#;|&", buff[in_pos] ) )
|
2006-05-30 08:35:35 +08:00
|
|
|
{
|
|
|
|
color[start_pos]=HIGHLIGHT_ESCAPE;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1]=normal_status;
|
2006-05-30 08:35:35 +08:00
|
|
|
}
|
2007-09-25 19:55:14 +08:00
|
|
|
else if( wcschr( L"c", buff[in_pos] ) )
|
|
|
|
{
|
|
|
|
color[start_pos]=HIGHLIGHT_ESCAPE;
|
|
|
|
color[in_pos+2]=normal_status;
|
|
|
|
}
|
2006-05-30 08:35:35 +08:00
|
|
|
else if( wcschr( L"uUxX01234567", buff[in_pos] ) )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
long long res=0;
|
|
|
|
int chars=2;
|
|
|
|
int base=16;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
int byte = 0;
|
|
|
|
wchar_t max_val = ASCII_MAX;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
switch( buff[in_pos] )
|
2006-05-27 00:46:38 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
case L'u':
|
|
|
|
{
|
|
|
|
chars=4;
|
|
|
|
max_val = UCS2_MAX;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
case L'U':
|
2006-05-27 00:46:38 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
chars=8;
|
|
|
|
max_val = WCHAR_MAX;
|
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
case L'x':
|
2006-05-27 00:46:38 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
case L'X':
|
2006-05-27 00:46:38 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
byte=1;
|
|
|
|
max_val = BYTE_MAX;
|
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
base=8;
|
|
|
|
chars=3;
|
|
|
|
in_pos--;
|
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2006-05-30 08:35:35 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
for( i=0; i<chars; i++ )
|
|
|
|
{
|
|
|
|
int d = convert_digit( buff[++in_pos],base);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
if( d < 0 )
|
|
|
|
{
|
|
|
|
in_pos--;
|
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-30 08:35:35 +08:00
|
|
|
res=(res*base)|d;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (res <= max_val) )
|
|
|
|
{
|
|
|
|
color[start_pos] = HIGHLIGHT_ESCAPE;
|
2011-12-27 11:18:46 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-30 08:35:35 +08:00
|
|
|
}
|
|
|
|
else
|
2011-12-27 11:18:46 +08:00
|
|
|
{
|
2006-05-30 08:35:35 +08:00
|
|
|
color[start_pos] = HIGHLIGHT_ERROR;
|
2011-12-27 11:18:46 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
|
|
|
}
|
2006-05-30 08:35:35 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
else
|
2006-05-27 00:46:38 +08:00
|
|
|
{
|
|
|
|
switch( buff[in_pos]){
|
|
|
|
case L'~':
|
|
|
|
case L'%':
|
|
|
|
{
|
|
|
|
if( in_pos == 0 )
|
|
|
|
{
|
|
|
|
color[in_pos] = HIGHLIGHT_OPERATOR;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-06-02 07:04:38 +08:00
|
|
|
case L'$':
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
wchar_t n = buff[in_pos+1];
|
2006-06-02 07:04:38 +08:00
|
|
|
color[in_pos] = (n==L'$'||wcsvarchr(n))? HIGHLIGHT_OPERATOR:HIGHLIGHT_ERROR;
|
2011-12-27 11:18:46 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-06-02 07:04:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case L'*':
|
|
|
|
case L'?':
|
2006-05-27 08:56:18 +08:00
|
|
|
case L'(':
|
|
|
|
case L')':
|
2006-05-27 00:46:38 +08:00
|
|
|
{
|
|
|
|
color[in_pos] = HIGHLIGHT_OPERATOR;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case L'{':
|
|
|
|
{
|
2006-05-27 08:56:18 +08:00
|
|
|
color[in_pos] = HIGHLIGHT_OPERATOR;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
bracket_count++;
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case L'}':
|
|
|
|
{
|
2006-05-27 08:56:18 +08:00
|
|
|
color[in_pos] = HIGHLIGHT_OPERATOR;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
bracket_count--;
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case L',':
|
|
|
|
{
|
|
|
|
if( bracket_count )
|
|
|
|
{
|
|
|
|
color[in_pos] = HIGHLIGHT_OPERATOR;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case L'\'':
|
|
|
|
{
|
2006-05-27 20:35:16 +08:00
|
|
|
color[in_pos] = HIGHLIGHT_QUOTE;
|
2006-05-27 00:46:38 +08:00
|
|
|
mode = 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case L'\"':
|
|
|
|
{
|
2006-05-27 20:35:16 +08:00
|
|
|
color[in_pos] = HIGHLIGHT_QUOTE;
|
2006-05-27 00:46:38 +08:00
|
|
|
mode = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mode 1 means single quoted string, i.e 'foo'
|
|
|
|
*/
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
if( c == L'\\' )
|
|
|
|
{
|
|
|
|
int start_pos = in_pos;
|
|
|
|
switch( buff[++in_pos] )
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
case L'\'':
|
|
|
|
{
|
|
|
|
color[start_pos] = HIGHLIGHT_ESCAPE;
|
2006-05-27 20:35:16 +08:00
|
|
|
color[in_pos+1] = HIGHLIGHT_QUOTE;
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
|
|
|
if( c == L'\'' )
|
|
|
|
{
|
|
|
|
mode = 0;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mode 2 means double quoted string, i.e. "foo"
|
|
|
|
*/
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
switch( c )
|
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
{
|
|
|
|
mode = 0;
|
2006-06-17 18:41:28 +08:00
|
|
|
color[in_pos+1] = normal_status;
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case '\\':
|
|
|
|
{
|
|
|
|
int start_pos = in_pos;
|
|
|
|
switch( buff[++in_pos] )
|
|
|
|
{
|
|
|
|
case L'\0':
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case '\\':
|
|
|
|
case L'$':
|
|
|
|
case '"':
|
|
|
|
{
|
|
|
|
color[start_pos] = HIGHLIGHT_ESCAPE;
|
2006-05-27 20:35:16 +08:00
|
|
|
color[in_pos+1] = HIGHLIGHT_QUOTE;
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-27 00:46:38 +08:00
|
|
|
case '$':
|
|
|
|
{
|
2006-06-02 07:04:38 +08:00
|
|
|
wchar_t n = buff[in_pos+1];
|
|
|
|
color[in_pos] = (n==L'$'||wcsvarchr(n))? HIGHLIGHT_OPERATOR:HIGHLIGHT_ERROR;
|
2011-12-27 11:18:46 +08:00
|
|
|
color[in_pos+1] = HIGHLIGHT_QUOTE;
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
}
|
2006-05-27 00:46:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 18:45:55 +08:00
|
|
|
static int has_expand_reserved( const wchar_t *str )
|
2008-02-05 07:09:05 +08:00
|
|
|
{
|
|
|
|
while( *str )
|
|
|
|
{
|
|
|
|
if( *str >= EXPAND_RESERVED &&
|
|
|
|
*str <= EXPAND_RESERVED_END )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
// PCA DOES_IO
|
|
|
|
void tokenize( const wchar_t * const buff, int * const color, const int pos, array_list_t *error, void *context, const env_vars &vars) {
|
|
|
|
ASSERT_IS_BACKGROUND_THREAD();
|
2012-01-30 18:45:55 +08:00
|
|
|
|
|
|
|
wcstring cmd;
|
2005-09-20 21:26:39 +08:00
|
|
|
int had_cmd=0;
|
|
|
|
int i;
|
2012-01-30 18:45:55 +08:00
|
|
|
wcstring last_cmd;
|
2006-06-12 22:12:33 +08:00
|
|
|
int len;
|
|
|
|
|
2010-09-18 09:51:16 +08:00
|
|
|
int accept_switches = 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 09:35:37 +08:00
|
|
|
int use_function = 1;
|
|
|
|
int use_command = 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
int use_builtin = 1;
|
|
|
|
|
2006-06-21 08:48:36 +08:00
|
|
|
CHECK( buff, );
|
|
|
|
CHECK( color, );
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2006-06-12 22:12:33 +08:00
|
|
|
len = wcslen(buff);
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !len )
|
|
|
|
return;
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
for( i=0; buff[i] != 0; i++ )
|
|
|
|
color[i] = -1;
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
tokenizer tok;
|
2005-09-20 21:26:39 +08:00
|
|
|
for( tok_init( &tok, buff, TOK_SHOW_COMMENTS );
|
2011-12-27 11:18:46 +08:00
|
|
|
tok_has_next( &tok );
|
|
|
|
tok_next( &tok ) )
|
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
int last_type = tok_last_type( &tok );
|
|
|
|
int prev_argc=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
switch( last_type )
|
|
|
|
{
|
|
|
|
case TOK_STRING:
|
|
|
|
{
|
|
|
|
if( had_cmd )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*Parameter */
|
|
|
|
wchar_t *param = tok_last( &tok );
|
|
|
|
if( param[0] == L'-' )
|
|
|
|
{
|
2006-12-04 20:07:07 +08:00
|
|
|
if (wcscmp( param, L"--" ) == 0 )
|
|
|
|
{
|
|
|
|
accept_switches = 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_PARAM;
|
2006-12-04 20:07:07 +08:00
|
|
|
}
|
|
|
|
else if( accept_switches )
|
|
|
|
{
|
2012-01-30 18:45:55 +08:00
|
|
|
if( complete_is_valid_option( last_cmd.c_str(), param, error, false /* no autoload */ ) )
|
2006-12-04 20:07:07 +08:00
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_PARAM;
|
|
|
|
else
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
else
|
2006-12-04 20:07:07 +08:00
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_PARAM;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_PARAM;
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
|
|
|
|
2012-01-30 18:45:55 +08:00
|
|
|
if( cmd == L"cd" )
|
2006-06-15 09:00:38 +08:00
|
|
|
{
|
2012-01-30 18:23:58 +08:00
|
|
|
wcstring dir_str = tok_last( &tok );
|
|
|
|
if (expand_one(dir_str, EXPAND_SKIP_CMDSUBST))
|
2006-06-15 09:00:38 +08:00
|
|
|
{
|
2012-01-30 18:23:58 +08:00
|
|
|
const wchar_t *dir = dir_str.c_str();
|
2008-01-09 11:06:47 +08:00
|
|
|
int is_long_help = wcsncmp(dir,L"--help", wcslen(dir) );
|
|
|
|
int is_short_help = wcsncmp(dir,L"-h", wcslen(dir) );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2008-01-09 11:06:47 +08:00
|
|
|
if( !is_long_help && !is_short_help && !path_get_cdpath( context, dir ) )
|
2006-06-17 18:41:28 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
2006-06-17 18:41:28 +08:00
|
|
|
}
|
2006-06-15 09:00:38 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-17 18:41:28 +08:00
|
|
|
highlight_param( param,
|
2011-12-27 11:18:46 +08:00
|
|
|
&color[tok_get_pos( &tok )],
|
|
|
|
pos-tok_get_pos( &tok ),
|
|
|
|
error );
|
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
2011-12-27 11:18:46 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
prev_argc=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
Command. First check that the command actually exists.
|
|
|
|
*/
|
2012-01-30 18:45:55 +08:00
|
|
|
cmd = tok_last( &tok );
|
|
|
|
bool expanded = expand_one(cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES);
|
|
|
|
if (! expanded || has_expand_reserved(cmd.c_str()))
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int is_cmd = 0;
|
|
|
|
int is_subcommand = 0;
|
|
|
|
int mark = tok_get_pos( &tok );
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_COMMAND;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-31 01:46:33 +08:00
|
|
|
if( parser_keywords_is_subcommand( cmd ) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 21:40:25 +08:00
|
|
|
int sw;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-30 18:45:55 +08:00
|
|
|
if( cmd == L"builtin")
|
2006-12-14 09:35:37 +08:00
|
|
|
{
|
|
|
|
use_function = 0;
|
|
|
|
use_command = 0;
|
|
|
|
use_builtin = 1;
|
|
|
|
}
|
2012-01-30 18:45:55 +08:00
|
|
|
else if( cmd == L"command")
|
2006-12-14 09:35:37 +08:00
|
|
|
{
|
|
|
|
use_command = 1;
|
|
|
|
use_function = 0;
|
|
|
|
use_builtin = 0;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
tok_next( &tok );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 17:50:26 +08:00
|
|
|
sw = parser_keywords_is_switch( tok_last( &tok ) );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-31 01:46:33 +08:00
|
|
|
if( !parser_keywords_is_block( cmd ) &&
|
2011-12-27 11:18:46 +08:00
|
|
|
sw == ARG_SWITCH )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
/*
|
|
|
|
The 'builtin' and 'command' builtins
|
|
|
|
are normally followed by another
|
|
|
|
command, but if they are invoked
|
|
|
|
with a switch, they aren't.
|
|
|
|
|
|
|
|
*/
|
2006-12-14 21:40:25 +08:00
|
|
|
use_command = 1;
|
|
|
|
use_function = 1;
|
|
|
|
use_builtin = 2;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-14 21:40:25 +08:00
|
|
|
if( sw == ARG_SKIP )
|
2006-12-14 09:35:37 +08:00
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_PARAM;
|
|
|
|
mark = tok_get_pos( &tok );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
is_subcommand = 1;
|
|
|
|
}
|
|
|
|
tok_set_pos( &tok, mark );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !is_subcommand )
|
|
|
|
{
|
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
OK, this is a command, it has been
|
|
|
|
successfully expanded and everything
|
|
|
|
looks ok. Lets check if the command
|
|
|
|
exists.
|
|
|
|
*/
|
|
|
|
|
2006-06-02 06:42:17 +08:00
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
First check if it is a builtin or
|
|
|
|
function, since we don't have to stat
|
|
|
|
any files for that
|
|
|
|
*/
|
2006-12-14 09:35:37 +08:00
|
|
|
if( use_builtin )
|
2012-01-30 18:45:55 +08:00
|
|
|
is_cmd |= builtin_exists( cmd.c_str() );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 09:35:37 +08:00
|
|
|
if( use_function )
|
2012-01-30 18:45:55 +08:00
|
|
|
is_cmd |= function_exists_no_autoload( cmd.c_str(), vars );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-02 06:42:17 +08:00
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
Moving on to expensive tests
|
|
|
|
*/
|
|
|
|
|
2006-06-02 06:42:17 +08:00
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
Check if this is a regular command
|
|
|
|
*/
|
2006-12-14 09:35:37 +08:00
|
|
|
if( use_command )
|
2011-12-27 11:18:46 +08:00
|
|
|
{
|
|
|
|
wcstring tmp;
|
|
|
|
is_cmd |= !!path_get_path_string( cmd, tmp, vars );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Could not find the command. Maybe it is
|
|
|
|
a path for a implicit cd command.
|
|
|
|
*/
|
2006-12-14 09:35:37 +08:00
|
|
|
if( use_builtin || (use_function && function_exists( L"cd") ) )
|
2011-12-27 11:18:46 +08:00
|
|
|
{
|
|
|
|
wcstring tmp;
|
|
|
|
is_cmd |= !!path_get_cdpath_string( cmd, tmp, vars );
|
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( is_cmd )
|
2011-12-27 11:18:46 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_COMMAND;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( error )
|
2012-01-30 18:45:55 +08:00
|
|
|
al_push( error, wcsdupcat ( L"Unknown command \'", cmd.c_str(), L"\'" ));
|
2005-09-20 21:26:39 +08:00
|
|
|
color[ tok_get_pos( &tok ) ] = (HIGHLIGHT_ERROR);
|
|
|
|
}
|
|
|
|
had_cmd = 1;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( had_cmd )
|
|
|
|
{
|
2012-01-30 18:45:55 +08:00
|
|
|
last_cmd = tok_last( &tok );
|
2006-06-02 06:42:17 +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
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-10-27 02:42:32 +08:00
|
|
|
case TOK_REDIRECT_NOCLOB:
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_REDIRECT_OUT:
|
|
|
|
case TOK_REDIRECT_IN:
|
|
|
|
case TOK_REDIRECT_APPEND:
|
|
|
|
case TOK_REDIRECT_FD:
|
|
|
|
{
|
|
|
|
if( !had_cmd )
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
|
|
|
if( error )
|
|
|
|
al_push( error, wcsdup ( L"Redirection without a command" ) );
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
wchar_t *target=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_REDIRECTION;
|
|
|
|
tok_next( &tok );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
Check that we are redirecting into a file
|
|
|
|
*/
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
switch( tok_last_type( &tok ) )
|
|
|
|
{
|
|
|
|
case TOK_STRING:
|
|
|
|
{
|
2006-08-22 22:38:31 +08:00
|
|
|
target = expand_one( context, wcsdup( tok_last( &tok ) ), EXPAND_SKIP_CMDSUBST);
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
Redirect filename may contain a cmdsubst.
|
|
|
|
If so, it will be ignored/not flagged.
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2005-09-20 21:26:39 +08:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
|
|
|
if( error )
|
|
|
|
al_push( error, wcsdup ( L"Invalid redirection" ) );
|
|
|
|
}
|
2011-12-27 11:18:46 +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
|
|
|
if( target != 0 )
|
|
|
|
{
|
2006-06-12 22:12:33 +08:00
|
|
|
wchar_t *dir = halloc_wcsdup( context, target );
|
2005-09-20 21:26:39 +08:00
|
|
|
wchar_t *dir_end = wcsrchr( dir, L'/' );
|
|
|
|
struct stat buff;
|
2011-12-27 11:18:46 +08:00
|
|
|
/*
|
|
|
|
If file is in directory other than '.', check
|
|
|
|
that the directory exists.
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
if( dir_end != 0 )
|
|
|
|
{
|
|
|
|
*dir_end = 0;
|
|
|
|
if( wstat( dir, &buff ) == -1 )
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
|
|
|
if( error )
|
2007-09-29 05:32:27 +08:00
|
|
|
al_push( error, wcsdupcat( L"Directory \'", dir, L"\' does not exist" ) );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-12 22:12:33 +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
|
|
|
/*
|
2011-12-27 11:18:46 +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 )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
if( wstat( target, &buff ) == -1 )
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
|
|
|
if( error )
|
2007-09-29 05:32:27 +08:00
|
|
|
al_push( error, wcsdupcat( L"File \'", target, L"\' does not exist" ) );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
2007-10-27 02:42:32 +08:00
|
|
|
if( last_type == TOK_REDIRECT_NOCLOB )
|
|
|
|
{
|
|
|
|
if( wstat( target, &buff ) != -1 )
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
|
|
|
if( error )
|
|
|
|
al_push( error, wcsdupcat( L"File \'", target, L"\' exists" ) );
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_PIPE:
|
|
|
|
case TOK_BACKGROUND:
|
|
|
|
{
|
|
|
|
if( had_cmd )
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_END;
|
|
|
|
had_cmd = 0;
|
2006-12-14 09:35:37 +08:00
|
|
|
use_command = 1;
|
|
|
|
use_function = 1;
|
|
|
|
use_builtin = 1;
|
2006-12-04 20:07:07 +08:00
|
|
|
accept_switches = 1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
2005-09-20 21:26:39 +08:00
|
|
|
if( error )
|
|
|
|
al_push( error, wcsdup ( L"No job to put in background" ) );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_END:
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_END;
|
|
|
|
had_cmd = 0;
|
2006-12-14 09:35:37 +08:00
|
|
|
use_command = 1;
|
|
|
|
use_function = 1;
|
|
|
|
use_builtin = 1;
|
2006-12-04 20:07:07 +08:00
|
|
|
accept_switches = 1;
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_COMMENT:
|
|
|
|
{
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_COMMENT;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_ERROR:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
If the tokenizer reports an error, highlight it as such.
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
if( error )
|
|
|
|
al_push( error, wcsdup ( tok_last( &tok) ) );
|
|
|
|
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR;
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
tok_destroy( &tok );
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
// PCA DOES_IO (calls is_potential_path, path_get_path, maybe others)
|
|
|
|
void highlight_shell( const wchar_t *buff, int *color, int pos, array_list_t *error, const env_vars &vars )
|
|
|
|
{
|
|
|
|
ASSERT_IS_BACKGROUND_THREAD();
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
int last_val;
|
|
|
|
|
|
|
|
void *context;
|
|
|
|
|
|
|
|
CHECK( buff, );
|
|
|
|
CHECK( color, );
|
|
|
|
|
|
|
|
len = wcslen(buff);
|
|
|
|
|
|
|
|
if( !len )
|
|
|
|
return;
|
|
|
|
|
|
|
|
context = halloc( 0, 0 );
|
|
|
|
|
|
|
|
for( i=0; buff[i] != 0; i++ )
|
|
|
|
color[i] = -1;
|
|
|
|
|
|
|
|
/* Tokenize the string */
|
|
|
|
tokenize(buff, color, pos, error, context, vars);
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
2006-08-22 22:38:31 +08:00
|
|
|
Locate and syntax highlight cmdsubsts recursively
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2005-10-25 19:03:52 +08:00
|
|
|
|
2006-06-12 22:12:33 +08:00
|
|
|
wchar_t *buffcpy = halloc_wcsdup( context, buff );
|
2005-09-20 21:26:39 +08:00
|
|
|
wchar_t *subpos=buffcpy;
|
|
|
|
int done=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
wchar_t *begin, *end;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
if( parse_util_locate_cmdsubst( subpos,
|
|
|
|
&begin,
|
2006-06-14 21:22:40 +08:00
|
|
|
&end,
|
2006-01-31 00:51:50 +08:00
|
|
|
1) <= 0)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !*end )
|
|
|
|
done=1;
|
|
|
|
else
|
|
|
|
*end=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
highlight_shell( begin+1, color +(begin-buffcpy)+1, -1, error, vars );
|
2006-05-27 08:56:18 +08:00
|
|
|
color[end-buffcpy]=HIGHLIGHT_OPERATOR;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( done )
|
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2010-09-18 09:51:16 +08:00
|
|
|
subpos = end+1;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2006-06-17 18:41:28 +08:00
|
|
|
/*
|
|
|
|
The highlighting code only changes the first element when the
|
|
|
|
color changes. This fills in the rest.
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
last_val=0;
|
|
|
|
for( i=0; buff[i] != 0; i++ )
|
|
|
|
{
|
|
|
|
if( color[i] >= 0 )
|
|
|
|
last_val = color[i];
|
|
|
|
else
|
|
|
|
color[i] = last_val;
|
|
|
|
}
|
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
/*
|
|
|
|
Color potentially valid paths in a special path color if they
|
|
|
|
are the current token.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( pos >= 0 && pos <= len )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
wchar_t *tok_begin, *tok_end;
|
|
|
|
wchar_t *token;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
parse_util_token_extent( buff, pos, &tok_begin, &tok_end, 0, 0 );
|
2006-07-03 18:46:47 +08:00
|
|
|
if( tok_begin && tok_end )
|
2006-06-14 21:22:40 +08:00
|
|
|
{
|
|
|
|
token = halloc_wcsndup( context, tok_begin, tok_end-tok_begin );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-14 21:22:40 +08:00
|
|
|
if( is_potential_path( token ) )
|
|
|
|
{
|
|
|
|
for( i=tok_begin-buff; i < (tok_end-buff); i++ )
|
|
|
|
{
|
|
|
|
color[i] |= HIGHLIGHT_VALID_PATH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
highlight_universal_internal( buff, color, pos );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
Spaces should not be highlighted at all, since it makes cursor look funky in some terminals
|
|
|
|
*/
|
|
|
|
for( i=0; buff[i]; i++ )
|
|
|
|
{
|
|
|
|
if( iswspace(buff[i]) )
|
|
|
|
{
|
|
|
|
color[i]=0;
|
|
|
|
}
|
|
|
|
}
|
2006-06-12 22:12:33 +08:00
|
|
|
|
|
|
|
halloc_free( context );
|
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.
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
static void highlight_universal_internal( const wchar_t * buff,
|
|
|
|
int *color,
|
|
|
|
int pos )
|
|
|
|
{
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2012-01-15 14:32:45 +08:00
|
|
|
if( (pos >= 0) && ((size_t)pos < wcslen(buff)) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
|
|
|
Highlight matching quotes
|
|
|
|
*/
|
|
|
|
if( (buff[pos] == L'\'') || (buff[pos] == L'\"') )
|
|
|
|
{
|
|
|
|
|
|
|
|
array_list_t l;
|
|
|
|
al_init( &l );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
int level=0;
|
|
|
|
wchar_t prev_q=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
const wchar_t *str=buff;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
int match_found=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
switch( *str )
|
|
|
|
{
|
|
|
|
case L'\\':
|
|
|
|
str++;
|
|
|
|
break;
|
|
|
|
case L'\"':
|
|
|
|
case L'\'':
|
|
|
|
if( level == 0 )
|
|
|
|
{
|
|
|
|
level++;
|
2006-08-01 00:55:11 +08:00
|
|
|
al_push_long( &l, (long)(str-buff) );
|
2005-09-20 21:26:39 +08:00
|
|
|
prev_q = *str;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( prev_q == *str )
|
|
|
|
{
|
2006-06-02 03:42:31 +08:00
|
|
|
long pos1, pos2;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
level--;
|
2006-08-01 00:55:11 +08:00
|
|
|
pos1 = al_pop_long( &l );
|
2005-09-20 21:26:39 +08:00
|
|
|
pos2 = str-buff;
|
|
|
|
if( pos1==pos || pos2==pos )
|
|
|
|
{
|
2006-06-14 21:22:40 +08:00
|
|
|
color[pos1]|=HIGHLIGHT_MATCH<<16;
|
|
|
|
color[pos2]|=HIGHLIGHT_MATCH<<16;
|
2005-09-20 21:26:39 +08:00
|
|
|
match_found = 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
prev_q = *str==L'\"'?L'\'':L'\"';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level++;
|
2006-08-01 00:55:11 +08:00
|
|
|
al_push_long( &l, (long)(str-buff) );
|
2005-09-20 21:26:39 +08:00
|
|
|
prev_q = *str;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( (*str == L'\0'))
|
|
|
|
break;
|
|
|
|
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
al_destroy( &l );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !match_found )
|
2006-06-14 21:22:40 +08:00
|
|
|
color[pos] = HIGHLIGHT_ERROR<<16;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Highlight matching parenthesis
|
|
|
|
*/
|
|
|
|
if( wcschr( L"()[]{}", buff[pos] ) )
|
|
|
|
{
|
|
|
|
int step = wcschr(L"({[", buff[pos])?1:-1;
|
|
|
|
wchar_t dec_char = *(wcschr( L"()[]{}", buff[pos] ) + step);
|
|
|
|
wchar_t inc_char = buff[pos];
|
|
|
|
int level = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
const wchar_t *str = &buff[pos];
|
|
|
|
int match_found=0;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
while( (str >= buff) && *str)
|
|
|
|
{
|
|
|
|
if( *str == inc_char )
|
|
|
|
level++;
|
|
|
|
if( *str == dec_char )
|
|
|
|
level--;
|
|
|
|
if( level == 0 )
|
|
|
|
{
|
|
|
|
int pos2 = str-buff;
|
2006-06-14 21:22:40 +08:00
|
|
|
color[pos]|=HIGHLIGHT_MATCH<<16;
|
|
|
|
color[pos2]|=HIGHLIGHT_MATCH<<16;
|
2005-09-20 21:26:39 +08:00
|
|
|
match_found=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
str+= step;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !match_found )
|
2006-06-14 21:22:40 +08:00
|
|
|
color[pos] = HIGHLIGHT_ERROR<<16;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
void highlight_universal( const wchar_t *buff, int *color, int pos, array_list_t *error, const env_vars &vars )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
int i;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-04 06:35:33 +08:00
|
|
|
for( i=0; buff[i]; i++ )
|
2005-09-20 21:26:39 +08:00
|
|
|
color[i] = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
highlight_universal_internal( buff, color, pos );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|