From 8ff36deeb49e517ee076bb1a8db3d98adb6d7012 Mon Sep 17 00:00:00 2001 From: axel Date: Fri, 7 Oct 2005 20:36:51 +1000 Subject: [PATCH] API cleanup darcs-hash:20051007103651-ac50b-3b518ce795adf2d39b8ebfcba6a2df7c36411b15.gz --- builtin.c | 6 +- builtin_set.c | 9 +- common.c | 200 ++++++++++++++++++++--------------------- common.h | 4 +- complete.c | 10 ++- env_universal_common.c | 7 +- event.c | 2 +- expand.c | 36 +++++--- expand.h | 37 ++++---- fish_pager.c | 2 +- kill.c | 4 +- output.c | 2 +- reader.c | 8 +- 13 files changed, 171 insertions(+), 156 deletions(-) diff --git a/builtin.c b/builtin.c index bb54cd216..e8b892e53 100644 --- a/builtin.c +++ b/builtin.c @@ -1744,12 +1744,12 @@ static int builtin_complete( wchar_t **argv ) case 'p': cmd_type = PATH; - cmd = expand_backslash( wcsdup(woptarg), 1); + cmd = expand_unescape( woptarg, 1); break; case 'c': cmd_type = COMMAND; - cmd = expand_backslash( wcsdup(woptarg), 1); + cmd = expand_unescape( woptarg, 1); break; case 'd': @@ -2744,7 +2744,7 @@ static int builtin_case( wchar_t **argv ) for( i=1; iswitch_value, unescaped ) ) { diff --git a/builtin_set.c b/builtin_set.c index cf22e8406..dff154229 100644 --- a/builtin_set.c +++ b/builtin_set.c @@ -221,7 +221,7 @@ static int fill_buffer_from_list(string_buffer_t *sb, array_list_t *list) Print the names of all environment variables in the scope, with or without values, with or without escaping */ -static void print_variables(int include_values, int escape, int scope) +static void print_variables(int include_values, int esc, int scope) { array_list_t names; int i; @@ -234,14 +234,15 @@ static void print_variables(int include_values, int escape, int scope) for( i = 0; i < al_get_count(&names); i++ ) { wchar_t *key = (wchar_t *)al_get( &names, i ); - /* Why does expand_escape free its argument ?! */ - wchar_t *e_key = escape ? expand_escape(wcsdup(key), 1) : wcsdup(key); + wchar_t *e_key = esc ? escape(key, 1) : wcsdup(key); + sb_append(sb_out, e_key); if( include_values ) { wchar_t *value = env_get(key); - wchar_t *e_value = escape ? expand_escape_variable(value) : wcsdup(value); + wchar_t *e_value; + e_value = esc ? expand_escape_variable(value) : wcsdup(value); sb_append2(sb_out, L" ", e_value, (void *)0); free(e_value); } diff --git a/common.c b/common.c index 1a13c1497..00545f90b 100644 --- a/common.c +++ b/common.c @@ -809,98 +809,98 @@ void debug( int level, wchar_t *msg, ... ) fwprintf( stderr, L"\n" ); } -wchar_t *escape( wchar_t *in, +wchar_t *escape( const wchar_t *in, int escape_all ) { - wchar_t *killme=in; wchar_t *out = malloc( sizeof(wchar_t)*(wcslen(in)*4 + 1)); - if( out != 0 ) + wchar_t *pos=out; + if( !out ) + die_mem(); + + while( *in != 0 ) { - wchar_t *pos=out; - while( *in != 0 ) + switch( *in ) { - switch( *in ) - { - case L'\t': - *(pos++) = L'\\'; - *(pos++) = L't'; - break; + case L'\t': + *(pos++) = L'\\'; + *(pos++) = L't'; + break; - case L'\n': - *(pos++) = L'\\'; - *(pos++) = L'n'; - break; + case L'\n': + *(pos++) = L'\\'; + *(pos++) = L'n'; + break; - case L'\b': - *(pos++) = L'\\'; - *(pos++) = L'b'; - break; + case L'\b': + *(pos++) = L'\\'; + *(pos++) = L'b'; + break; - case L'\r': - *(pos++) = L'\\'; - *(pos++) = L'r'; - break; + case L'\r': + *(pos++) = L'\\'; + *(pos++) = L'r'; + break; - case L'\e': - *(pos++) = L'\\'; - *(pos++) = L'e'; - break; + case L'\e': + *(pos++) = L'\\'; + *(pos++) = L'e'; + break; - case L'\\': - case L'&': - case L'$': - case L' ': - case L'#': - case L'^': - case L'<': - case L'>': - case L'@': - case L'(': - case L')': - case L'{': - case L'}': - case L'?': - case L'*': - case L'|': - case L';': - case L':': - case L'\'': - case L'\"': - if( escape_all ) - *pos++ = L'\\'; + case L'\\': + case L'&': + case L'$': + case L' ': + case L'#': + case L'^': + case L'<': + case L'>': + case L'@': + case L'(': + case L')': + case L'{': + case L'}': + case L'?': + case L'*': + case L'|': + case L';': + case L':': + case L'\'': + case L'\"': + if( escape_all ) + *pos++ = L'\\'; + *pos++ = *in; + break; + + default: + if( *in < 32 ) + { + int tmp = (*in)%16; + *pos++ = L'\\'; + *pos++ = L'x'; + *pos++ = ((*in>15)? L'1' : L'0'); + *pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp; + } + else *pos++ = *in; - break; - - default: - if( *in < 32 ) - { - int tmp = (*in)%16; - *pos++ = L'\\'; - *pos++ = L'x'; - *pos++ = ((*in>15)? L'1' : L'0'); - *pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp; - } - else - *pos++ = *in; - break; - } - in++; + break; } - *pos = 0; - free(killme); + in++; } - + *pos = 0; return out; } -wchar_t *unescape( wchar_t * in, int escape_special ) +wchar_t *unescape( const wchar_t * orig, int escape_special ) { - int in_pos, out_pos, len = wcslen( in ); + int in_pos, out_pos, len = wcslen( orig ); int c; int bracket_count=0; wchar_t prev=0; - + wchar_t *in = wcsdup(orig); + if( !in ) + die_mem(); + for( in_pos=0, out_pos=0; in_pos= timeout || elapsed < 0 ) { @@ -1314,14 +1312,14 @@ int acquire_lock_file( const char *lockfile, const int timeout, int force ) force was not specified */ debug( 1, L"acquire_lock_file: timed out " - L"trying to obtain lockfile %s using " - L"linkfile %s", lockfile, linkfile ); + L"trying to obtain lockfile %s using " + L"linkfile %s", lockfile, linkfile ); break; } } nanosleep( &pollint, NULL ); } while( gettimeofday( &end, NULL ) == 0 ); -done: + done: /* The linkfile is not needed once the lockfile has been created */ (void)unlink( linkfile ); free( linkfile ); diff --git a/common.h b/common.h index 08c38241e..d23d3a076 100644 --- a/common.h +++ b/common.h @@ -247,10 +247,10 @@ void debug( int level, wchar_t *msg, ... ); \return The escaped string, or 0 if there is not enough memory */ -wchar_t *escape( wchar_t *in, +wchar_t *escape( const wchar_t *in, int escape_all ); -wchar_t *unescape( wchar_t * in, int escape_special ); +wchar_t *unescape( const wchar_t * in, int escape_special ); void block(); void unblock(); diff --git a/complete.c b/complete.c index 3ec43de8e..09ab7d627 100644 --- a/complete.c +++ b/complete.c @@ -820,7 +820,9 @@ static const wchar_t *complete_get_desc_suffix( const wchar_t *suff_orig ) } } - suff = expand_escape( suff, 0 ); + wchar_t *tmp = expand_escape( suff, 0 ); + free(suff); + suff = tmp; wchar_t *desc = (wchar_t *)hash_get( suffix_hash, suff ); @@ -1043,7 +1045,7 @@ static void complete_cmd_desc( const wchar_t *cmd, array_list_t *comp ) return; } - esc = expand_escape( wcsdup(cmd_start), 1 ); + esc = expand_escape( cmd_start, 1 ); if( esc ) { @@ -1495,7 +1497,7 @@ void complete_load( wchar_t *cmd, { if( !tm || (*tm != buf.st_mtime ) ) { - wchar_t *esc = expand_escape( wcsdup((wchar_t *)path.buff), 1 ); + wchar_t *esc = expand_escape( (wchar_t *)path.buff, 1 ); wchar_t *src_cmd = wcsdupcat( L". ", esc ); /* if( tm ) @@ -2164,7 +2166,7 @@ static void append_switch( string_buffer_t *out, if( !argument || argument==L"" ) return; - esc = expand_escape( wcsdup(argument), 1 ); + esc = expand_escape( argument, 1 ); sb_printf( out, L" --%ls %ls", opt, esc ); free(esc); } diff --git a/env_universal_common.c b/env_universal_common.c index 175fb28e7..3a7416c77 100644 --- a/env_universal_common.c +++ b/env_universal_common.c @@ -219,13 +219,16 @@ static void parse_message( wchar_t *msg, tmp = wcschr( name, L':' ); if( tmp ) { + wchar_t *val_unescaped; + wchar_t *key =malloc( sizeof( wchar_t)*(tmp-name+1)); memcpy( key, name, sizeof( wchar_t)*(tmp-name)); key[tmp-name]=0; val = tmp+1; - val = unescape( wcsdup(val), 0 ); + + val = unescape( val, 0 ); var_entry_t *entry = malloc( sizeof(var_entry_t) + sizeof(wchar_t)*(wcslen(val)+1) ); @@ -386,7 +389,7 @@ message_t *create_message( int type, val_in=L""; } - wchar_t *esc = escape(wcsdup(val_in),1); + wchar_t *esc = escape(val_in,1); if( !esc ) break; diff --git a/event.c b/event.c index ce495069f..b9710e43f 100644 --- a/event.c +++ b/event.c @@ -324,7 +324,7 @@ static void event_fire_internal( event_t *event, array_list_t *arguments ) for( j=0; j