From 45412f2b1f9b1b97fce85533a9d3fe0309e5c090 Mon Sep 17 00:00:00 2001 From: axel Date: Sun, 22 Apr 2007 19:50:26 +1000 Subject: [PATCH] Move keyword detection code to separate file darcs-hash:20070422095026-ac50b-77a840e2830370f46b7a48fd8863095d2cd7a5f0.gz --- Makefile.in | 3 +- builtin.c | 4 +-- complete.c | 4 +-- function.c | 3 +- highlight.c | 7 +++-- parse_util.h | 2 ++ parser.c | 76 +++++------------------------------------------ parser.h | 44 --------------------------- parser_keywords.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++ parser_keywords.h | 63 +++++++++++++++++++++++++++++++++++++++ path.c | 1 + wildcard.c | 1 + 12 files changed, 162 insertions(+), 121 deletions(-) create mode 100644 parser_keywords.c create mode 100644 parser_keywords.h diff --git a/Makefile.in b/Makefile.in index 3ac8669e7..d25f4ce3e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -92,7 +92,8 @@ FISH_OBJS := function.o builtin.o complete.o env.o exec.o expand.o \ highlight.o history.o kill.o parser.o proc.o reader.o sanity.o \ tokenizer.o wildcard.o wgetopt.o wutil.o input.o output.o intern.o \ env_universal.o env_universal_common.o input_common.o event.o \ - signal.o io.o parse_util.o common.o screen.o path.o + signal.o io.o parse_util.o common.o screen.o path.o \ + parser_keywords.o # diff --git a/builtin.c b/builtin.c index 0bdab295d..065fdb1ae 100644 --- a/builtin.c +++ b/builtin.c @@ -62,10 +62,10 @@ #include "signal.h" #include "exec.h" #include "highlight.h" - #include "halloc.h" #include "halloc_util.h" #include "parse_util.h" +#include "parser_keywords.h" #include "expand.h" #include "path.h" @@ -1335,7 +1335,7 @@ static int builtin_function( wchar_t **argv ) res=1; } - else if( parser_is_reserved(argv[woptind] ) ) + else if( parser_keywords_is_reserved(argv[woptind] ) ) { sb_printf( sb_err, diff --git a/complete.c b/complete.c index d6b4ff02b..7d9109009 100644 --- a/complete.c +++ b/complete.c @@ -39,8 +39,8 @@ These functions are used for storing and retrieving tab-completion data, as well #include "reader.h" #include "history.h" #include "intern.h" - #include "parse_util.h" +#include "parser_keywords.h" #include "halloc.h" #include "halloc_util.h" #include "wutil.h" @@ -1890,7 +1890,7 @@ void complete( const wchar_t *cmd, if( !had_cmd ) { - if( parser_is_subcommand( ncmd ) ) + if( parser_keywords_is_subcommand( ncmd ) ) { if( wcscmp( ncmd, L"builtin" )==0) { diff --git a/function.c b/function.c index 23a942cf2..361edd853 100644 --- a/function.c +++ b/function.c @@ -28,6 +28,7 @@ #include "event.h" #include "reader.h" #include "parse_util.h" +#include "parser_keywords.h" #include "env.h" #include "expand.h" #include "halloc.h" @@ -213,7 +214,7 @@ int function_exists( const wchar_t *cmd ) CHECK( cmd, 0 ); - if( parser_is_reserved(cmd) ) + if( parser_keywords_is_reserved(cmd) ) return 0; load( cmd ); diff --git a/highlight.c b/highlight.c index 47d1ad749..f09ff805f 100644 --- a/highlight.c +++ b/highlight.c @@ -22,6 +22,7 @@ #include "proc.h" #include "parser.h" #include "parse_util.h" +#include "parser_keywords.h" #include "builtin.h" #include "function.h" #include "env.h" @@ -642,7 +643,7 @@ void highlight_shell( wchar_t * buff, int mark = tok_get_pos( &tok ); color[ tok_get_pos( &tok ) ] = HIGHLIGHT_COMMAND; - if( parser_is_subcommand( cmd ) ) + if( parser_keywords_is_subcommand( cmd ) ) { int sw; @@ -662,9 +663,9 @@ void highlight_shell( wchar_t * buff, tok_next( &tok ); - sw = parser_is_switch( tok_last( &tok ) ); + sw = parser_keywords_is_switch( tok_last( &tok ) ); - if( !parser_is_block( cmd ) && + if( !parser_keywords_is_block( cmd ) && sw == ARG_SWITCH ) { /* diff --git a/parse_util.h b/parse_util.h index 796669240..e0f4cf261 100644 --- a/parse_util.h +++ b/parse_util.h @@ -146,4 +146,6 @@ void parse_util_set_argv( wchar_t **argv, array_list_t *named_arguments ); */ wchar_t *parse_util_unescape_wildcards( const wchar_t *in ); + + #endif diff --git a/parser.c b/parser.c index e00ca4c5c..947d9fa31 100644 --- a/parser.c +++ b/parser.c @@ -26,6 +26,7 @@ The fish parser. Contains functions for parsing and evaluating code. #include "wutil.h" #include "proc.h" #include "parser.h" +#include "parser_keywords.h" #include "tokenizer.h" #include "exec.h" #include "wildcard.h" @@ -512,67 +513,6 @@ const wchar_t *parser_get_block_desc( int block ) return _(UNKNOWN_BLOCK); } -/** - Check if the specified bcommand is one of the builtins that cannot - have arguments, any followin argument is interpreted as a new - command -*/ -static int parser_skip_arguments( const wchar_t *cmd ) -{ - return CONTAINS( cmd, - L"else", - L"begin" ); -} - -int parser_is_switch( const wchar_t *cmd ) -{ - if( wcscmp( cmd, L"--" ) == 0 ) - return ARG_SKIP; - else - return cmd[0] == L'-'; -} - - -int parser_is_subcommand( const wchar_t *cmd ) -{ - - return parser_skip_arguments( cmd ) || - CONTAINS( cmd, - L"command", - L"builtin", - L"while", - L"exec", - L"if", - L"and", - L"or", - L"not" ); - -} - -int parser_is_block( const wchar_t *word) -{ - return CONTAINS( word, - L"for", - L"while", - L"if", - L"function", - L"switch", - L"begin" ); -} - -int parser_is_reserved( const wchar_t *word) -{ - return parser_is_block(word) || - parser_is_subcommand( word ) || - CONTAINS( word, - L"end", - L"case", - L"else", - L"return", - L"continue", - L"break" ); -} - /** Returns 1 if the specified command is a builtin that may not be used in a pipeline */ @@ -614,7 +554,7 @@ static const wchar_t *parser_find_end( const wchar_t * buff ) { count--; } - else if( parser_is_block( tok_last(&tok) ) ) + else if( parser_keywords_is_block( tok_last(&tok) ) ) { count++; } @@ -1844,7 +1784,7 @@ static int parse_job( process_t *p, } tok_next( tok ); - sw = parser_is_switch( tok_last( tok ) ); + sw = parser_keywords_is_switch( tok_last( tok ) ); if( sw == ARG_SWITCH ) { @@ -2007,7 +1947,7 @@ static int parse_job( process_t *p, builtin_exists( (wchar_t *)al_get( args, 0 ) ) ) { p->type = INTERNAL_BUILTIN; - is_new_block |= parser_is_block( (wchar_t *)al_get( args, 0 ) ); + is_new_block |= parser_keywords_is_block( (wchar_t *)al_get( args, 0 ) ); } } @@ -2236,7 +2176,7 @@ static int parse_job( process_t *p, if( !error_code ) { - if( p->type == INTERNAL_BUILTIN && parser_skip_arguments( (wchar_t *)al_get(args, 0) ) ) + if( p->type == INTERNAL_BUILTIN && parser_keywords_skip_arguments( (wchar_t *)al_get(args, 0) ) ) { if( !p->argv ) halloc_register( j, p->argv = list_to_char_arr( args ) ); @@ -3045,7 +2985,7 @@ int parser_test( const wchar_t * buff, /* Handle block commands */ - if( parser_is_block( cmd ) ) + if( parser_keywords_is_block( cmd ) ) { if( count >= BLOCK_MAX_COUNT ) { @@ -3066,12 +3006,12 @@ int parser_test( const wchar_t * buff, } /* - If parser_is_subcommand is true, the command + If parser_keywords_is_subcommand is true, the command accepts a second command as it's first argument. If parser_skip_arguments is true, the second argument is optional. */ - if( parser_is_subcommand( cmd ) && !parser_skip_arguments(cmd ) ) + if( parser_keywords_is_subcommand( cmd ) && !parser_keywords_skip_arguments(cmd ) ) { needs_cmd = 1; had_cmd = 0; diff --git a/parser.h b/parser.h index b7c3ea684..1dc3a02c4 100644 --- a/parser.h +++ b/parser.h @@ -15,17 +15,6 @@ #define PARSER_TEST_ERROR 1 #define PARSER_TEST_INCOMPLETE 2 -/** - REturn valuse for parser_is_switch() -*/ -enum -{ - ARG_NON_SWITCH, - ARG_SWITCH, - ARG_SKIP -} - ; - /** event_block_t represents a block on events of the specified type */ @@ -232,39 +221,6 @@ int eval_args( const wchar_t *line, void error( int ec, int p, const wchar_t *str, ... ); -/** - 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_is_switch( const wchar_t *cmd ); - - -/** - Tests if the specified commands parameters should be interpreted as another command, which will be true if the command is either 'command', 'exec', 'if', 'while' or 'builtin'. - - \param cmd The command name to test - \return 1 of the command parameter is a command, 0 otherwise -*/ - -int parser_is_subcommand( const wchar_t *cmd ); - -/** - Tests if the specified command is a reserved word, i.e. if it is - the name of one of the builtin functions that change the block or - command scope, like 'for', 'end' or 'command' or 'exec'. These - functions may not be overloaded, so their names are reserved. - - \param word The command name to test - \return 1 of the command parameter is a command, 0 otherwise -*/ -int parser_is_reserved( const wchar_t *word ); - -/** - Test if the specified string is command that opens a new block -*/ - -int parser_is_block( const wchar_t *word); - /** Returns a string describing the current parser pisition in the format 'FILENAME (line LINE_NUMBER): LINE'. Example: diff --git a/parser_keywords.c b/parser_keywords.c new file mode 100644 index 000000000..37b3dcd85 --- /dev/null +++ b/parser_keywords.c @@ -0,0 +1,75 @@ +/** \file parser_keywords.c + + +*/ + +#include "config.h" + + +#include +#include + +#include "fallback.h" +//#include "util.h" + +//#include "wutil.h" +#include "common.h" +#include "parser_keywords.h" + + +int parser_keywords_is_switch( const wchar_t *cmd ) +{ + if( wcscmp( cmd, L"--" ) == 0 ) + return ARG_SKIP; + else + return cmd[0] == L'-'; +} + +int parser_keywords_skip_arguments( const wchar_t *cmd ) +{ + return CONTAINS( cmd, + L"else", + L"begin" ); +} + + +int parser_keywords_is_subcommand( const wchar_t *cmd ) +{ + + return parser_keywords_skip_arguments( cmd ) || + CONTAINS( cmd, + L"command", + L"builtin", + L"while", + L"exec", + L"if", + L"and", + L"or", + L"not" ); + +} + +int parser_keywords_is_block( const wchar_t *word) +{ + return CONTAINS( word, + L"for", + L"while", + L"if", + L"function", + L"switch", + L"begin" ); +} + +int parser_keywords_is_reserved( const wchar_t *word) +{ + return parser_keywords_is_block(word) || + parser_keywords_is_subcommand( word ) || + CONTAINS( word, + L"end", + L"case", + L"else", + L"return", + L"continue", + L"break" ); +} + diff --git a/parser_keywords.h b/parser_keywords.h new file mode 100644 index 000000000..e8212334c --- /dev/null +++ b/parser_keywords.h @@ -0,0 +1,63 @@ +/** \file parser_keywords.h + +Functions having to do with parser keywords, like testing if a function is a block command. +*/ + +#ifndef FISH_PARSER_KEYWORD_H +#define FISH_PARSER_KEYWORD_H + +/** + Return valuse for parser_keywords_is_switch() +*/ +enum +{ + ARG_NON_SWITCH, + ARG_SWITCH, + ARG_SKIP +} + ; + + + +/** + 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 ); + + +/** + Tests if the specified commands parameters should be interpreted as another command, which will be true if the command is either 'command', 'exec', 'if', 'while' or 'builtin'. + + \param cmd The command name to test + \return 1 of the command parameter is a command, 0 otherwise +*/ + +int parser_keywords_is_subcommand( const wchar_t *cmd ); + +/** + Tests if the specified command is a reserved word, i.e. if it is + the name of one of the builtin functions that change the block or + command scope, like 'for', 'end' or 'command' or 'exec'. These + functions may not be overloaded, so their names are reserved. + + \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 ); + +/** + Test if the specified string is command that opens a new block +*/ + +int parser_keywords_is_block( const wchar_t *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 ); + + +#endif diff --git a/path.c b/path.c index 2f66de064..469c702c0 100644 --- a/path.c +++ b/path.c @@ -66,6 +66,7 @@ wchar_t *path_get_path( void *context, const wchar_t *cmd ) Allocate string long enough to hold the whole command */ wchar_t *new_cmd = halloc( context, sizeof(wchar_t)*(wcslen(cmd)+wcslen(path)+2) ); + /* We tokenize a copy of the path, since strtok modifies its arguments diff --git a/wildcard.c b/wildcard.c index 73c715a67..c955d5f4e 100644 --- a/wildcard.c +++ b/wildcard.c @@ -1185,6 +1185,7 @@ int wildcard_expand( const wchar_t *wc, int c = al_get_count( out ); int res = wildcard_expand_internal( wc, base_dir, flags, out ); int i; + if( flags & ACCEPT_INCOMPLETE ) {