diff --git a/common.cpp b/common.cpp index 17e7348b4..424e97dd5 100644 --- a/common.cpp +++ b/common.cpp @@ -607,16 +607,16 @@ const wchar_t *wsetlocale(int category, const wchar_t *locale) return (wchar_t *)setlocale_buff->buff; } -int contains_internal( const wchar_t *a, ... ) +bool contains_internal( const wchar_t *a, ... ) { - wchar_t *arg; + const wchar_t *arg; va_list va; int res = 0; CHECK( a, 0 ); va_start( va, a ); - while( (arg=va_arg(va, wchar_t *) )!= 0 ) + while( (arg=va_arg(va, const wchar_t *) )!= 0 ) { if( wcscmp( a,arg) == 0 ) { @@ -626,7 +626,28 @@ int contains_internal( const wchar_t *a, ... ) } va_end( va ); - return res; + return res; +} + +/* wcstring variant of contains_internal. The first parameter is a wcstring, the rest are const wchar_t* */ +__sentinel bool contains_internal( const wcstring &needle, ... ) +{ + const wchar_t *arg; + va_list va; + int res = 0; + + va_start( va, needle ); + while( (arg=va_arg(va, const wchar_t *) )!= 0 ) + { + if( needle == arg) + { + res=1; + break; + } + + } + va_end( va ); + return res; } int read_blocked(int fd, void *buf, size_t count) diff --git a/common.h b/common.h index 88fc4ca76..23eee113d 100644 --- a/common.h +++ b/common.h @@ -440,7 +440,8 @@ 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_internal( const wchar_t *needle, ... ); +__sentinel bool contains_internal( const wchar_t *needle, ... ); +__sentinel bool contains_internal( const wcstring &needle, ... ); /** Call read while blocking the SIGCHLD signal. Should only be called diff --git a/highlight.cpp b/highlight.cpp index de20fde33..c1d2f3839 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -646,7 +646,7 @@ void tokenize( const wchar_t * const buff, int * const color, const int pos, arr int mark = tok_get_pos( &tok ); color[ tok_get_pos( &tok ) ] = HIGHLIGHT_COMMAND; - if( parser_keywords_is_subcommand( cmd.c_str() ) ) + if( parser_keywords_is_subcommand( cmd ) ) { int sw; @@ -668,7 +668,7 @@ void tokenize( const wchar_t * const buff, int * const color, const int pos, arr sw = parser_keywords_is_switch( tok_last( &tok ) ); - if( !parser_keywords_is_block( cmd.c_str() ) && + if( !parser_keywords_is_block( cmd ) && sw == ARG_SWITCH ) { /* diff --git a/parser.cpp b/parser.cpp index f2a52e3a4..b526251e6 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1887,7 +1887,7 @@ int parser_t::parse_job( process_t *p, builtin_exists(args->at(0).completion.c_str())) { p->type = INTERNAL_BUILTIN; - is_new_block |= parser_keywords_is_block( args->at( 0 ).completion.c_str() ); + is_new_block |= parser_keywords_is_block( args->at( 0 ).completion ); } } diff --git a/parser_keywords.cpp b/parser_keywords.cpp index 6b9ce0c73..2e3487024 100644 --- a/parser_keywords.cpp +++ b/parser_keywords.cpp @@ -14,15 +14,15 @@ Functions having to do with parser keywords, like testing if a function is a blo #include "parser_keywords.h" -int parser_keywords_is_switch( const wchar_t *cmd ) +bool parser_keywords_is_switch( const wcstring &cmd ) { - if( wcscmp( cmd, L"--" ) == 0 ) + if( cmd == L"--" ) return ARG_SKIP; else - return cmd[0] == L'-'; + return ! cmd.empty() && cmd.at(0) == L'-'; } -int parser_keywords_skip_arguments( const wchar_t *cmd ) +bool parser_keywords_skip_arguments( const wcstring &cmd ) { return contains( cmd, L"else", @@ -30,7 +30,7 @@ int parser_keywords_skip_arguments( const wchar_t *cmd ) } -int parser_keywords_is_subcommand( const wchar_t *cmd ) +bool parser_keywords_is_subcommand( const wcstring &cmd ) { return parser_keywords_skip_arguments( cmd ) || @@ -46,7 +46,7 @@ int parser_keywords_is_subcommand( const wchar_t *cmd ) } -int parser_keywords_is_block( const wchar_t *word) +bool parser_keywords_is_block( const wcstring &word) { return contains( word, L"for", @@ -57,7 +57,7 @@ int parser_keywords_is_block( const wchar_t *word) L"begin" ); } -int parser_keywords_is_reserved( const wchar_t *word) +bool parser_keywords_is_reserved( const wcstring &word) { return parser_keywords_is_block(word) || parser_keywords_is_subcommand( word ) || diff --git a/parser_keywords.h b/parser_keywords.h index e8212334c..c2cc15d97 100644 --- a/parser_keywords.h +++ b/parser_keywords.h @@ -23,7 +23,7 @@ enum Check if the specified argument is a switch. Return ARG_SWITCH if yes, ARG_NON_SWITCH if no and ARG_SKIP if the argument is '--' */ -int parser_keywords_is_switch( const wchar_t *cmd ); +bool parser_keywords_is_switch( const wcstring &cmd ); /** @@ -33,7 +33,7 @@ int parser_keywords_is_switch( const wchar_t *cmd ); \return 1 of the command parameter is a command, 0 otherwise */ -int parser_keywords_is_subcommand( const wchar_t *cmd ); +bool parser_keywords_is_subcommand( const wcstring &cmd ); /** Tests if the specified command is a reserved word, i.e. if it is @@ -44,20 +44,20 @@ int parser_keywords_is_subcommand( const wchar_t *cmd ); \param word The command name to test \return 1 of the command parameter is a command, 0 otherwise */ -int parser_keywords_is_reserved( const wchar_t *word ); +bool parser_keywords_is_reserved( const wcstring &word ); /** Test if the specified string is command that opens a new block */ -int parser_keywords_is_block( const wchar_t *word); +bool parser_keywords_is_block( const wcstring &word); /** Check if the specified command is one of the builtins that cannot have arguments, any followin argument is interpreted as a new command */ -int parser_keywords_skip_arguments( const wchar_t *cmd ); +bool parser_keywords_skip_arguments( const wcstring &cmd ); #endif