From 0e716763d80f56f6cb80ad9b673af8fff9296a95 Mon Sep 17 00:00:00 2001 From: liljencrantz Date: Sat, 29 Sep 2007 07:32:27 +1000 Subject: [PATCH] Replace variadic functions like sb_append and contains_str with variadic macros without a sentinel. darcs-hash:20070928213227-75c98-2e7b06242acfd5fd0bf02ce77c41d52374f2363a.gz --- builtin.c | 22 +++++++++++----------- builtin_commandline.c | 6 +++--- builtin_jobs.c | 2 +- builtin_set.c | 7 +++---- builtin_ulimit.c | 4 ++-- common.c | 9 ++------- common.h | 13 ++++--------- complete.c | 18 ++++++++---------- env.c | 4 ++-- expand.c | 4 ++-- highlight.c | 6 +++--- history.c | 2 +- input.c | 1 + kill.c | 4 ++-- parse_util.c | 2 +- parser.c | 12 ++++++------ parser_keywords.c | 8 ++++---- path.c | 6 +++--- reader.c | 10 +++++----- util.c | 15 +++------------ util.h | 8 ++------ wildcard.c | 2 +- 22 files changed, 70 insertions(+), 95 deletions(-) diff --git a/builtin.c b/builtin.c index 6b0b99794..402ec4f94 100644 --- a/builtin.c +++ b/builtin.c @@ -162,13 +162,13 @@ static void builtin_wperror( const wchar_t *s) { if( s != 0 ) { - sb_append2( sb_err, s, L": ", (void *)0 ); + sb_append( sb_err, s, L": ", (void *)0 ); } char *err = strerror( errno ); wchar_t *werr = str2wcs( err ); if( werr ) { - sb_append2( sb_err, werr, L"\n", (void *)0 ); + sb_append( sb_err, werr, L"\n", (void *)0 ); free( werr ); } } @@ -944,7 +944,7 @@ static int builtin_builtin( wchar_t **argv ) { wchar_t *el = (wchar_t *)al_get( &names, i ); - sb_append2( sb_out, + sb_append( sb_out, el, L"\n", (void *)0 ); @@ -1103,7 +1103,7 @@ static void functions_def( wchar_t *name, string_buffer_t *out ) al_init( &ev ); event_get( &search, &ev ); - sb_append2( out, + sb_append( out, L"function ", name, (void *)0); @@ -1112,13 +1112,13 @@ static void functions_def( wchar_t *name, string_buffer_t *out ) { wchar_t *esc_desc = escape( desc, 1 ); - sb_append2( out, L" --description ", esc_desc, (void *)0 ); + sb_append( out, L" --description ", esc_desc, (void *)0 ); free( esc_desc ); } if( !function_get_shadows( name ) ) { - sb_append2( out, L" --no-scope-shadowing", (void *)0 ); + sb_append( out, L" --no-scope-shadowing", (void *)0 ); } for( i=0; i 1) ) { - sb_append2( sb_err, + sb_append( sb_err, argv[0], L": Too many arguments\n", (void *)0 ); diff --git a/builtin_jobs.c b/builtin_jobs.c index d53d43e99..ec6ba21b7 100644 --- a/builtin_jobs.c +++ b/builtin_jobs.c @@ -96,7 +96,7 @@ static void builtin_jobs_print( job_t *j, int mode, int header ) #ifdef HAVE__PROC_SELF_STAT sb_printf( sb_out, L"%d%%\t", cpu_use(j) ); #endif - sb_append2( sb_out, + sb_append( sb_out, job_is_stopped(j)?_(L"stopped"):_(L"running"), L"\t", j->command, diff --git a/builtin_set.c b/builtin_set.c index 0477757f4..3eaca346e 100644 --- a/builtin_set.c +++ b/builtin_set.c @@ -46,10 +46,9 @@ Functions used for implementing the set builtin. */ static int is_path_variable( const wchar_t *env ) { - return contains_str( env, + return contains( env, L"PATH", - L"CDPATH", - (void *)0 ); + L"CDPATH" ); } /** @@ -388,7 +387,7 @@ static void print_variables(int include_values, int esc, int scope) e_value = esc ? expand_escape_variable(value) : wcsdup(value); - sb_append2(sb_out, L" ", e_value, (void *)0); + sb_append(sb_out, L" ", e_value, (void *)0); free(e_value); if( shorten ) diff --git a/builtin_ulimit.c b/builtin_ulimit.c index 335484514..6215fd402 100644 --- a/builtin_ulimit.c +++ b/builtin_ulimit.c @@ -432,7 +432,7 @@ static int builtin_ulimit( wchar_t ** argv ) } else { - sb_append2( sb_err, + sb_append( sb_err, argv[0], L": Too many arguments\n", (void *)0 ); @@ -503,7 +503,7 @@ static int builtin_ulimit( wchar_t ** argv ) default: { - sb_append2( sb_err, + sb_append( sb_err, argv[0], L": Too many arguments\n", (void *)0 ); diff --git a/common.c b/common.c index 7a0504751..545d36b09 100644 --- a/common.c +++ b/common.c @@ -385,12 +385,7 @@ char **wcsv2strv( const wchar_t **in ) } -wchar_t *wcsdupcat( const wchar_t *a, const wchar_t *b ) -{ - return wcsdupcat2( a, b, (void *)0 ); -} - -wchar_t *wcsdupcat2( const wchar_t *a, ... ) +wchar_t *wcsdupcat_internal( const wchar_t *a, ... ) { int len=wcslen(a); int pos; @@ -548,7 +543,7 @@ const wchar_t *wsetlocale(int category, const wchar_t *locale) return (wchar_t *)setlocale_buff->buff; } -int contains_str( const wchar_t *a, ... ) +int contains_internal( const wchar_t *a, ... ) { wchar_t *arg; va_list va; diff --git a/common.h b/common.h index 2c88791a3..f322badaa 100644 --- a/common.h +++ b/common.h @@ -140,7 +140,8 @@ extern wchar_t *program_name; */ #define N_(wstr) wstr -#define CONTAINS( str,... ) contains_str( str, __VA_ARGS__, (void *)0 ) +#define contains( str,... ) contains_internal( str, __VA_ARGS__, (void *)0 ) +#define wcsdupcat( str,... ) wcsdupcat_internal( str, __VA_ARGS__, (void *)0 ) /* Print a stack trace to stderr @@ -222,17 +223,12 @@ char **wcsv2strv( const wchar_t **in ); */ wchar_t **strv2wcsv( const char **in ); -/** - Returns a newly allocated concatenation of the specified wide - character strings -*/ -wchar_t *wcsdupcat( const wchar_t *a, const wchar_t *b ); /** Returns a newly allocated concatenation of the specified wide character strings. The last argument must be a null pointer. */ -__sentinel wchar_t *wcsdupcat2( const wchar_t *a, ... ); +__sentinel wchar_t *wcsdupcat_internal( const wchar_t *a, ... ); /** Test if the given string is a valid variable name @@ -304,7 +300,7 @@ const wchar_t *wsetlocale( int category, const wchar_t *locale ); \return zero if needle is not found, of if needle is null, non-zero otherwise */ -__sentinel int contains_str( const wchar_t *needle, ... ); +__sentinel int contains_internal( const wchar_t *needle, ... ); /** Call read while blocking the SIGCHLD signal. Should only be called @@ -410,7 +406,6 @@ void write_screen( const wchar_t *msg, string_buffer_t *buff ); */ void tokenize_variable_array( const wchar_t *val, array_list_t *out ); - /** Make sure the specified direcotry exists. If needed, try to create it and any currently not existing parent directories.. diff --git a/complete.c b/complete.c index fd07ea192..d0d216b71 100644 --- a/complete.c +++ b/complete.c @@ -805,7 +805,7 @@ int complete_is_valid_option( const wchar_t *str, str[0] = opt[j]; str[1]=0; al_push( errors, - wcsdupcat2(_( L"Unknown option: " ), L"'", str, L"'", (void *)0) ); + wcsdupcat(_( L"Unknown option: " ), L"'", str, L"'" ) ); } opt_found = 0; @@ -823,12 +823,12 @@ int complete_is_valid_option( const wchar_t *str, if( hash_get_count( &gnu_match_hash )==0) { al_push( errors, - wcsdupcat2( _(L"Unknown option: "), L"'", opt, L"\'", (void *)0) ); + wcsdupcat( _(L"Unknown option: "), L"'", opt, L"\'" ) ); } else { al_push( errors, - wcsdupcat2( _(L"Multiple matches for option: "), L"'", opt, L"\'", (void *)0) ); + wcsdupcat( _(L"Multiple matches for option: "), L"'", opt, L"\'" ) ); } } } @@ -1112,10 +1112,9 @@ static void complete_cmd( const wchar_t *cmd, nxt_path != 0; nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) ) { - nxt_completion = wcsdupcat2( nxt_path, + nxt_completion = wcsdupcat( nxt_path, (nxt_path[wcslen(nxt_path)-1]==L'/'?L"":L"/"), - cmd, - (void *)0 ); + cmd ); if( ! nxt_completion ) continue; @@ -1168,10 +1167,9 @@ static void complete_cmd( const wchar_t *cmd, nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) ) { wchar_t *nxt_completion= - wcsdupcat2( nxt_path, + wcsdupcat( nxt_path, (nxt_path[wcslen(nxt_path)-1]==L'/'?L"":L"/"), - cmd, - (void *)0 ); + cmd ); if( ! nxt_completion ) { continue; @@ -1487,7 +1485,7 @@ static int complete_param( const wchar_t *cmd_orig, int match=0, match_no_case=0; string_buffer_t *whole_opt = sb_halloc( context ); - sb_append2( whole_opt, o->old_mode?L"-":L"--", o->long_opt, (void *)0 ); + sb_append( whole_opt, o->old_mode?L"-":L"--", o->long_opt, (void *)0 ); match = wcsncmp( str, (wchar_t *)whole_opt->buff, wcslen(str) )==0; diff --git a/env.c b/env.c index 008521ad5..55915e3be 100644 --- a/env.c +++ b/env.c @@ -466,7 +466,7 @@ static void setup_path() sb_append( &b, path ); } - sb_append2( &b, + sb_append( &b, ARRAY_SEP_STR, path_el[j], (void *)0 ); @@ -713,7 +713,7 @@ int env_set( const wchar_t *key, CHECK( key, ENV_INVALID ); - if( val && CONTAINS( key, L"PWD", L"HOME" ) ) + if( val && contains( key, L"PWD", L"HOME" ) ) { void *context = halloc( 0, 0 ); const wchar_t *val_canonical = path_make_canonical( context, val ); diff --git a/expand.c b/expand.c index 4a1c00ec9..4ec14af3a 100644 --- a/expand.c +++ b/expand.c @@ -202,7 +202,7 @@ wchar_t *expand_escape_variable( const wchar_t *in ) if( wcschr( el, L' ' ) && is_quotable( el ) ) { - sb_append2( &buff, + sb_append( &buff, L"'", el, L"'", @@ -228,7 +228,7 @@ wchar_t *expand_escape_variable( const wchar_t *in ) if( is_quotable( el ) ) { - sb_append2( &buff, + sb_append( &buff, L"'", el, L"'", diff --git a/highlight.c b/highlight.c index 4832df2ce..603d75b90 100644 --- a/highlight.c +++ b/highlight.c @@ -741,7 +741,7 @@ void highlight_shell( wchar_t * buff, else { if( error ) - al_push( error, wcsdupcat2 ( L"Unknown command \'", cmd, L"\'", (void *)0 )); + al_push( error, wcsdupcat ( L"Unknown command \'", cmd, L"\'" )); color[ tok_get_pos( &tok ) ] = (HIGHLIGHT_ERROR); } had_cmd = 1; @@ -815,7 +815,7 @@ void highlight_shell( wchar_t * buff, { color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR; if( error ) - al_push( error, wcsdupcat2( L"Directory \'", dir, L"\' does not exist", (void *)0 ) ); + al_push( error, wcsdupcat( L"Directory \'", dir, L"\' does not exist" ) ); } } @@ -831,7 +831,7 @@ void highlight_shell( wchar_t * buff, { color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR; if( error ) - al_push( error, wcsdupcat2( L"File \'", target, L"\' does not exist", (void *)0 ) ); + al_push( error, wcsdupcat( L"File \'", target, L"\' does not exist" ) ); } } } diff --git a/history.c b/history.c index c607092c1..df24ddb2d 100644 --- a/history.c +++ b/history.c @@ -468,7 +468,7 @@ static wchar_t *history_filename( void *context, const wchar_t *name, const wcha if( !path ) return 0; - res = wcsdupcat2( path, L"/", name, L"_history", suffix?suffix:(void *)0, (void *)0 ); + res = wcsdupcat( path, L"/", name, L"_history", suffix?suffix:(void *)0); halloc_register_function( context, &free, res ); return res; } diff --git a/input.c b/input.c index d0b898408..c7ef37694 100644 --- a/input.c +++ b/input.c @@ -618,6 +618,7 @@ const wchar_t *input_mapping_get( const wchar_t *sequence ) } return 0; } + /** Add all terminfo mappings */ diff --git a/kill.c b/kill.c index 813381012..002da998d 100644 --- a/kill.c +++ b/kill.c @@ -96,7 +96,7 @@ void kill_add( wchar_t *str ) if( (disp = env_get( L"DISPLAY" )) ) { wchar_t *escaped_str = escape( str, 1 ); - wchar_t *cmd = wcsdupcat2(L"echo ", escaped_str, L"|xsel -b",(void *)0); + wchar_t *cmd = wcsdupcat(L"echo ", escaped_str, L"|xsel -b" ); if( exec_subshell( cmd, 0 ) == -1 ) { /* @@ -211,7 +211,7 @@ static void kill_check_x_buffer() else { wchar_t *old = new_cut_buffer; - new_cut_buffer= wcsdupcat2( new_cut_buffer, L"\\n", next_line, (void *)0 ); + new_cut_buffer= wcsdupcat( new_cut_buffer, L"\\n", next_line ); free( old ); free( next_line ); } diff --git a/parse_util.c b/parse_util.c index 78c49711b..4011d52c7 100644 --- a/parse_util.c +++ b/parse_util.c @@ -1014,7 +1014,7 @@ static int parse_util_load_internal( const wchar_t *cmd, struct stat buf; wchar_t *next = (wchar_t *)al_get( path_list, i ); sb_clear( path ); - sb_append2( path, next, L"/", cmd, L".fish", (void *)0 ); + sb_append( path, next, L"/", cmd, L".fish", (void *)0 ); if( (wstat( (wchar_t *)path->buff, &buf )== 0) && (waccess( (wchar_t *)path->buff, R_OK ) == 0) ) diff --git a/parser.c b/parser.c index 0832939ac..a699219e3 100644 --- a/parser.c +++ b/parser.c @@ -527,7 +527,7 @@ const wchar_t *parser_get_block_desc( int block ) */ static int parser_is_pipe_forbidden( wchar_t *word ) { - return CONTAINS( word, + return contains( word, L"exec", L"case", L"break", @@ -1008,7 +1008,7 @@ void parser_stack_trace( block_t *b, string_buffer_t *buff) for( i=1; b->param2.function_call_process->argv[i]; i++ ) { - sb_append2( &tmp, i>1?L" ":L"", b->param2.function_call_process->argv[i], (void *)0 ); + sb_append( &tmp, i>1?L" ":L"", b->param2.function_call_process->argv[i], (void *)0 ); } sb_printf( buff, _(L"\twith parameter list '%ls'\n"), (wchar_t *)tmp.buff ); @@ -1763,7 +1763,7 @@ static int parse_job( process_t *p, mark = tok_get_pos( tok ); - if( CONTAINS( nxt, + if( contains( nxt, L"command", L"builtin", L"not", @@ -2944,7 +2944,7 @@ int parser_test( const wchar_t * buff, command is needed, such as after 'and' or 'while' */ - if( CONTAINS( cmd, + if( contains( cmd, L"end" ) ) { err=1; @@ -3018,7 +3018,7 @@ int parser_test( const wchar_t * buff, had_cmd = 0; } - if( CONTAINS( cmd, + if( contains( cmd, L"or", L"and" ) ) { @@ -3152,7 +3152,7 @@ int parser_test( const wchar_t * buff, /* Test that break and continue are only used within loop blocks */ - if( CONTAINS( cmd, L"break", L"continue" ) ) + if( contains( cmd, L"break", L"continue" ) ) { int found_loop=0; int i; diff --git a/parser_keywords.c b/parser_keywords.c index b9df0edfc..6b9ce0c73 100644 --- a/parser_keywords.c +++ b/parser_keywords.c @@ -24,7 +24,7 @@ int parser_keywords_is_switch( const wchar_t *cmd ) int parser_keywords_skip_arguments( const wchar_t *cmd ) { - return CONTAINS( cmd, + return contains( cmd, L"else", L"begin" ); } @@ -34,7 +34,7 @@ int parser_keywords_is_subcommand( const wchar_t *cmd ) { return parser_keywords_skip_arguments( cmd ) || - CONTAINS( cmd, + contains( cmd, L"command", L"builtin", L"while", @@ -48,7 +48,7 @@ int parser_keywords_is_subcommand( const wchar_t *cmd ) int parser_keywords_is_block( const wchar_t *word) { - return CONTAINS( word, + return contains( word, L"for", L"while", L"if", @@ -61,7 +61,7 @@ int parser_keywords_is_reserved( const wchar_t *word) { return parser_keywords_is_block(word) || parser_keywords_is_subcommand( word ) || - CONTAINS( word, + contains( word, L"end", L"case", L"else", diff --git a/path.c b/path.c index 51e675e0c..bfd054d41 100644 --- a/path.c +++ b/path.c @@ -52,7 +52,7 @@ wchar_t *path_get_path( void *context, const wchar_t *cmd ) path = env_get(L"PATH"); if( path == 0 ) { - if( CONTAINS( PREFIX L"/bin", L"/bin", L"/usr/bin" ) ) + if( contains( PREFIX L"/bin", L"/bin", L"/usr/bin" ) ) { path = L"/bin" ARRAY_SEP_STR L"/usr/bin"; } @@ -197,9 +197,9 @@ wchar_t *path_get_cdpath( void *context, wchar_t *dir ) } whole_path = - wcsdupcat2( expanded_path, + wcsdupcat( expanded_path, ( expanded_path[path_len-1] != L'/' )?L"/":L"", - dir, (void *)0 ); + dir ); free(expanded_path ); diff --git a/reader.c b/reader.c index c52685da1..7fdc4227a 100644 --- a/reader.c +++ b/reader.c @@ -559,11 +559,11 @@ void reader_write_title() don't. Since we can't see the underlying terminal below screen there is no way to fix this. */ - if( !term || !CONTAINS( term, L"xterm", L"screen", L"nxterm", L"rxvt" ) ) + if( !term || !contains( term, L"xterm", L"screen", L"nxterm", L"rxvt" ) ) { char *n = ttyname( STDIN_FILENO ); - if( CONTAINS( term, L"linux" ) ) + if( contains( term, L"linux" ) ) { return; } @@ -1569,8 +1569,8 @@ static void handle_history( const wchar_t *new_str ) Check if the specified string is contained in the list, using wcscmp as a comparison function */ -static int contains( const wchar_t *needle, - array_list_t *haystack ) +static int contains_al( const wchar_t *needle, + array_list_t *haystack ) { int i; for( i=0; isearch_prev ) ) + if( !contains_al( tok_last( &tok ), &data->search_prev ) ) { free(str); data->token_history_pos = tok_get_pos( &tok ); diff --git a/util.c b/util.c index 2761a704b..c2e435d3a 100644 --- a/util.c +++ b/util.c @@ -1175,16 +1175,6 @@ string_buffer_t *sb_new() return res; } - -void sb_append( string_buffer_t *b, const wchar_t * s) -{ - CHECK( b, ); - CHECK( s, ); - - b_append( b, s, sizeof(wchar_t)*(wcslen(s)+1) ); - b->used -= sizeof(wchar_t); -} - void sb_append_substring( string_buffer_t *b, const wchar_t *s, size_t l ) { wchar_t tmp=0; @@ -1209,7 +1199,7 @@ void sb_append_char( string_buffer_t *b, wchar_t c ) b->used -= sizeof(wchar_t); } -void sb_append2( string_buffer_t *b, ... ) +void sb_append_internal( string_buffer_t *b, ... ) { va_list va; wchar_t *arg; @@ -1219,7 +1209,8 @@ void sb_append2( string_buffer_t *b, ... ) va_start( va, b ); while( (arg=va_arg(va, wchar_t *) )!= 0 ) { - sb_append( b, arg ); + b_append( b, arg, sizeof(wchar_t)*(wcslen(arg)+1) ); + b->used -= sizeof(wchar_t); } va_end( va ); } diff --git a/util.h b/util.h index 26d85748b..7d83d0011 100644 --- a/util.h +++ b/util.h @@ -598,11 +598,6 @@ void sb_init( string_buffer_t * ); */ string_buffer_t *sb_new(); -/** - Append a string to the buffer -*/ -void sb_append( string_buffer_t *, const wchar_t * ); - /** Append a part of a string to the buffer */ @@ -613,6 +608,7 @@ void sb_append_substring( string_buffer_t *, const wchar_t *, size_t ); */ void sb_append_char( string_buffer_t *, wchar_t ); +#define sb_append( sb,... ) sb_append_internal( sb, __VA_ARGS__, (void *)0 ) /** Append a null terminated list of strings to the buffer. @@ -622,7 +618,7 @@ void sb_append_char( string_buffer_t *, wchar_t ); Do not forget to cast the last 0 to (void *), or you might encounter errors on 64-bit platforms! */ -__sentinel void sb_append2( string_buffer_t *, ... ); +__sentinel void sb_append_internal( string_buffer_t *, ... ); /** Append formated string data to the buffer. This function internally diff --git a/wildcard.c b/wildcard.c index c955d5f4e..60d6dd6b8 100644 --- a/wildcard.c +++ b/wildcard.c @@ -727,7 +727,7 @@ static void wildcard_completion_allocate( array_list_t *list, } else { - sb_append2( sb, desc, L", ", (void *)0 ); + sb_append( sb, desc, L", ", (void *)0 ); if( sz < 0 ) { sb_append( sb, L"unknown" );