Minor edits - add a few input checks, remove a few commented pieces of debug code, add a few brackets, etc.

darcs-hash:20070115175144-ac50b-2045f2132156645222e6dde57487aa299a5316e2.gz
This commit is contained in:
axel 2007-01-16 03:51:44 +10:00
parent c6ebb23f38
commit cb7caf2afc
5 changed files with 160 additions and 115 deletions

View file

@ -153,6 +153,16 @@ void set_color( int c, int c2 )
int is_bold = 0;
int is_underline = 0;
/*
Test if we have at least basic support for setting fonts, colors
and related bits - otherwise just give up...
*/
if( !exit_attribute_mode )
{
return;
}
is_bold |= (c&FISH_COLOR_BOLD)!=0;
is_bold |= (c2&FISH_COLOR_BOLD)!=0;

View file

@ -126,5 +126,4 @@ void output_set_writer( int (*writer)(char) );
int (*output_get_writer())(char) ;
#endif

View file

@ -97,7 +97,6 @@ int parse_util_lineno( const wchar_t *str, int len )
static int res2 = 1;
CHECK( str, 0 );
if( str != prev_str || i>len )
{
@ -129,7 +128,9 @@ int parse_util_lineno( const wchar_t *str, int len )
for( ; str[i] && i<len; i++ )
{
if( str[i] == L'\n' )
{
res++;
}
}
return res;
}
@ -169,13 +170,17 @@ int parse_util_locate_cmdsubst( const wchar_t *in,
if( *pos == '(' )
{
if(( paran_count == 0)&&(paran_begin==0))
{
paran_begin = pos;
}
paran_count++;
}
else if( *pos == ')' )
{
paran_count--;
if( (paran_count == 0) && (paran_end == 0) )
{
paran_end = pos;
@ -208,9 +213,14 @@ int parse_util_locate_cmdsubst( const wchar_t *in,
}
if( begin )
{
*begin = paran_begin;
}
if( end )
{
*end = paran_count?(wchar_t *)in+wcslen(in):paran_end;
}
return 1;
}
@ -228,9 +238,14 @@ void parse_util_cmdsubst_extent( const wchar_t *buff,
CHECK( buff, );
if( a )
{
*a = (wchar_t *)buff;
}
if( b )
{
*b = (wchar_t *)buff+wcslen(buff);
}
pos = (wchar_t *)buff;
@ -255,10 +270,17 @@ void parse_util_cmdsubst_extent( const wchar_t *buff,
if(( begin < cursor ) && (end >= cursor) )
{
begin++;
if( a )
{
*a = begin;
}
if( b )
{
*b = end;
}
break;
}
@ -291,14 +313,21 @@ static void job_or_process_extent( const wchar_t *buff,
CHECK( buff, );
if( a )
{
*a=0;
}
if( b )
{
*b = 0;
}
parse_util_cmdsubst_extent( buff, cursor_pos, &begin, &end );
if( !end || !begin )
{
return;
}
pos = cursor_pos - (begin - buff);
if( a )
@ -327,9 +356,13 @@ static void job_or_process_extent( const wchar_t *buff,
switch( tok_last_type( &tok ) )
{
case TOK_PIPE:
{
if( !process )
{
break;
}
}
case TOK_END:
case TOK_BACKGROUND:
{
@ -338,15 +371,19 @@ static void job_or_process_extent( const wchar_t *buff,
{
finished=1;
if( b )
{
*b = (wchar_t *)buff + tok_begin;
}
}
else
{
if( a )
{
*a = (wchar_t *)buff + tok_begin+1;
}
}
break;
break;
}
}
}
@ -398,7 +435,9 @@ void parse_util_token_extent( const wchar_t *buff,
parse_util_cmdsubst_extent( buff, cursor_pos, &begin, &end );
if( !end || !begin )
{
return;
}
pos = cursor_pos - (begin - buff);
@ -430,7 +469,9 @@ void parse_util_token_extent( const wchar_t *buff,
Calculate end of token
*/
if( tok_last_type( &tok ) == TOK_STRING )
{
tok_end +=wcslen(tok_last(&tok));
}
/*
Cursor was before beginning of this token, means that the
@ -472,14 +513,25 @@ void parse_util_token_extent( const wchar_t *buff,
tok_destroy( &tok );
if( tok_begin )
{
*tok_begin = a;
if( tok_end )
*tok_end = b;
if( prev_begin )
*prev_begin = pa;
if( prev_end )
*prev_end = pb;
}
if( tok_end )
{
*tok_end = b;
}
if( prev_begin )
{
*prev_begin = pa;
}
if( prev_end )
{
*prev_end = pb;
}
assert( pa >= buff );
assert( pa <= (buff+wcslen(buff) ) );
assert( pb >= pa );
@ -560,7 +612,9 @@ void parse_util_load_reset( const wchar_t *path_var_name,
void *key, *data;
hash_remove( all_loaded, path_var_name, &key, &data );
if( key )
{
clear_loaded_entry( key, data, (void *)on_load );
}
}
}
@ -600,6 +654,16 @@ int parse_util_unload( const wchar_t *cmd,
return !!val;
}
/**
Unload all autoloaded items that have expired, that where loaded in
the specified path.
\param path_var_name The variable containing the path to autoload in
\param skip unloading the the specified file
\param on_load the callback function to call for every unloaded file
*/
static void parse_util_autounload( const wchar_t *path_var_name,
const wchar_t *skip,
void (*on_load)(const wchar_t *cmd) )
@ -637,8 +701,10 @@ static void parse_util_autounload( const wchar_t *path_var_name,
}
if( !tm[0] )
{
continue;
}
if( hash_get( &loaded->is_loading, item ) )
{
continue;
@ -690,7 +756,6 @@ int parse_util_load( const wchar_t *cmd,
// debug( 0, L"Autoload %ls in %ls", cmd, path_var_name );
parse_util_autounload( path_var_name, cmd, on_load );
path_var = env_get( path_var_name );
/*
@ -698,7 +763,6 @@ int parse_util_load( const wchar_t *cmd,
*/
if( !path_var )
{
// debug( 0, L"Path null" );
return 0;
}
@ -846,9 +910,13 @@ static int parse_util_load_internal( const wchar_t *cmd,
}
if( !path )
{
path = sb_halloc( global_context );
}
else
{
sb_clear( path );
}
/*
Iterate over path searching for suitable completion files
@ -873,7 +941,9 @@ static int parse_util_load_internal( const wchar_t *cmd,
{
tm = malloc(sizeof(time_t)*2);
if( !tm )
{
DIE_MEM();
}
}
tm[0] = buf.st_mtime;
@ -883,8 +953,10 @@ static int parse_util_load_internal( const wchar_t *cmd,
tm );
if( on_load )
{
on_load(cmd );
}
/*
Source the completion file for the specified completion
*/
@ -920,7 +992,9 @@ static int parse_util_load_internal( const wchar_t *cmd,
{
tm = malloc(sizeof(time_t)*2);
if( !tm )
{
DIE_MEM();
}
tm[0] = 0;
tm[1] = time(0);
@ -941,7 +1015,9 @@ void parse_util_set_argv( wchar_t **argv )
for( arg=argv; *arg; arg++ )
{
if( arg != argv )
{
sb_append( &sb, ARRAY_SEP_STR );
}
sb_append( &sb, *arg );
}
@ -964,13 +1040,16 @@ wchar_t *parse_util_unescape_wildcards( const wchar_t *str )
unescaped = wcsdup(str);
if( !unescaped )
{
DIE_MEM();
}
for( in=out=unescaped; *in; in++ )
{
switch( *in )
{
case L'\\':
{
if( *(in+1) )
{
in++;
@ -978,20 +1057,26 @@ wchar_t *parse_util_unescape_wildcards( const wchar_t *str )
}
*(out++)=*in;
break;
}
case L'*':
{
*(out++)=ANY_STRING;
break;
}
case L'?':
{
*(out++)=ANY_CHAR;
break;
}
default:
{
*(out++)=*in;
break;
}
}
}
}
return unescaped;
}

View file

@ -115,6 +115,8 @@ commence.
*/
#define DEFAULT_PROMPT L"whoami; echo @; hostname|cut -d . -f 1; echo \" \"; pwd; printf '> ';"
#define PROMPT_FUNCTION_NAME L"fish_prompt"
/**
The default title for the reader. This is used by reader_readline.
*/
@ -1910,8 +1912,8 @@ static int read_i()
{
wchar_t *tmp;
if( function_exists( L"fish_prompt" ) )
reader_set_prompt( L"fish_prompt" );
if( function_exists( PROMPT_FUNCTION_NAME ) )
reader_set_prompt( PROMPT_FUNCTION_NAME );
else
reader_set_prompt( DEFAULT_PROMPT );

133
util.c
View file

@ -115,25 +115,6 @@ void q_destroy( dyn_queue_t *q )
free( q->start );
}
/*
static q_print( dyn_queue_t *q )
{
int i;
int size = (q->stop-q->start);
printf( "Storlek: %d\n", size );
for( i=0; i< size; i++ )
{
printf( " %c%c %d: %d\n",
&q->start[i]==q->get_pos?'g':' ',
&q->start[i]==q->put_pos?'p':' ',
i,
q->start[i] );
}
}
*/
/**
Reallocate the queue_t
*/
@ -577,8 +558,11 @@ int hash_wcs_func( void *data )
*/
for( i=0; i<WORD_COUNT; i++ )
{
if( *in==0)
if( !*in)
{
/*
We have reached EOF, fill in the rest with zeroes
*/
for( ;i<WORD_COUNT; i++ )
w[i]=0;
}
@ -671,32 +655,6 @@ void pq_init( priority_queue_t *q,
q->compare = compare;
}
/**
Check that the priority queue is in a valid state
*/
/*
static void pq_check( priority_queue_t *q, int i )
{
int l,r;
if( q->count <= i )
return;
l=i*2+1;
r=i*2+2;
if( (q->count > l) && (q->compare(q->arr[i], q->arr[l]) < 0) )
{
printf( "ERROR: Place %d less than %d\n", i, l );
}
if( (q->count > r) && (q->compare(q->arr[i], q->arr[r]) < 0) )
{
printf( "ERROR: Place %d less than %d\n", i, r );
}
pq_check( q, l );
pq_check( q, r );
}
*/
int pq_put( priority_queue_t *q,
void *e )
@ -934,9 +892,6 @@ static int al_set_generic( array_list_t *l, int pos, anything_t v )
l->pos = pos;
if( al_push_generic( l, v ) )
{
/* fwprintf( stderr, L"Clearing from index %d to index %d\n",
old_pos, pos );
*/
memset( &l->arr[old_pos],
0,
sizeof(anything_t) * (pos - old_pos) );
@ -996,6 +951,7 @@ func_ptr_t al_get_func( array_list_t *l, int pos )
void al_truncate( array_list_t *l, int new_sz )
{
CHECK( l, );
l->pos = new_sz;
}
@ -1072,18 +1028,24 @@ func_ptr_t al_peek_func( array_list_t *l )
int al_empty( array_list_t *l )
{
CHECK( l, 1 );
return l->pos == 0;
}
int al_get_count( array_list_t *l )
{
CHECK( l, 0 );
return l->pos;
}
void al_foreach( array_list_t *l, void (*func)( void * ))
{
int i;
CHECK( l, );
CHECK( func, );
for( i=0; i<l->pos; i++ )
func( l->arr[i].ptr_val );
}
@ -1091,12 +1053,19 @@ void al_foreach( array_list_t *l, void (*func)( void * ))
void al_foreach2( array_list_t *l, void (*func)( void *, void *), void *aux)
{
int i;
CHECK( l, );
CHECK( func, );
for( i=0; i<l->pos; i++ )
func( l->arr[i].ptr_val, aux );
}
int wcsfilecmp( const wchar_t *a, const wchar_t *b )
{
CHECK( a, 0 );
CHECK( b, 0 );
if( *a==0 )
{
if( *b==0)
@ -1168,6 +1137,8 @@ void sb_init( string_buffer_t * b)
{
wchar_t c=0;
CHECK( b, );
if( !b )
{
return;
@ -1195,17 +1166,8 @@ string_buffer_t *sb_new()
void sb_append( string_buffer_t *b, const wchar_t * s)
{
// fwprintf( stderr, L"Append string \'%ls\'\n", s );
if( !s )
{
return;
}
if( !b )
{
return;
}
CHECK( b, );
CHECK( s, );
b_append( b, s, sizeof(wchar_t)*(wcslen(s)+1) );
b->used -= sizeof(wchar_t);
@ -1215,15 +1177,8 @@ void sb_append_substring( string_buffer_t *b, const wchar_t *s, size_t l )
{
wchar_t tmp=0;
if( !s )
{
return;
}
if( !b )
{
return;
}
CHECK( b, );
CHECK( s, );
b_append( b, s, sizeof(wchar_t)*l );
b_append( b, &tmp, sizeof(wchar_t) );
@ -1235,10 +1190,7 @@ void sb_append_char( string_buffer_t *b, wchar_t c )
{
wchar_t tmp=0;
if( !b )
{
return;
}
CHECK( b, );
b_append( b, &c, sizeof(wchar_t) );
b_append( b, &tmp, sizeof(wchar_t) );
@ -1250,11 +1202,8 @@ void sb_append2( string_buffer_t *b, ... )
va_list va;
wchar_t *arg;
if( !b )
{
return;
}
CHECK( b, );
va_start( va, b );
while( (arg=va_arg(va, wchar_t *) )!= 0 )
{
@ -1268,11 +1217,9 @@ int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... )
va_list va;
int res;
if( !buffer )
{
return -1;
}
CHECK( buffer, -1 );
CHECK( format, -1 );
va_start( va, format );
res = sb_vprintf( buffer, format, va );
va_end( va );
@ -1284,10 +1231,8 @@ int sb_vprintf( string_buffer_t *buffer, const wchar_t *format, va_list va_orig
{
int res;
if( !buffer )
{
return -1;
}
CHECK( buffer, -1 );
CHECK( format, -1 );
if( !buffer->length )
{
@ -1352,17 +1297,17 @@ int sb_vprintf( string_buffer_t *buffer, const wchar_t *format, va_list va_orig
void sb_destroy( string_buffer_t * b )
{
if( !b )
{
return;
}
CHECK( b, );
free( b->buff );
}
void sb_clear( string_buffer_t * b )
{
wchar_t c=0;
CHECK( b, );
b->used=0;
b_append( b, &c, sizeof( wchar_t));
b->used -= sizeof(wchar_t);
@ -1371,6 +1316,7 @@ void sb_clear( string_buffer_t * b )
void b_init( buffer_t *b)
{
CHECK( b, );
memset( b,0,sizeof(buffer_t));
}
@ -1378,6 +1324,7 @@ void b_init( buffer_t *b)
void b_destroy( buffer_t *b )
{
CHECK( b, );
free( b->buff );
}
@ -1387,6 +1334,8 @@ int b_append( buffer_t *b, const void *d, ssize_t len )
if( len<=0 )
return 0;
CHECK( b, -1 );
if( !b )
{
return 0;