/** \file function.c Prototypes for functions for storing and retrieving function information. These functions also take care of autoloading functions in the $fish_function_path. Actual function evaluation is taken care of by the parser and to some degree the builtin handling library. */ #include "config.h" #include #include #include #include #include #include #include "wutil.h" #include "fallback.h" #include "util.h" #include "function.h" #include "proc.h" #include "parser.h" #include "common.h" #include "intern.h" #include "event.h" #include "reader.h" #include "parse_util.h" #include "env.h" #include "expand.h" #include "halloc.h" #include "halloc_util.h" /** Struct describing a function */ typedef struct { /** Function definition */ wchar_t *cmd; /** Function description */ wchar_t *desc; /** File where this function was defined */ const wchar_t *definition_file; /** Line where definition started */ int definition_offset; array_list_t *named_arguments; /** Flag for specifying that this function was automatically loaded */ int is_autoload; } function_data_t; /** Table containing all functions */ static hash_table_t function; /** Kludgy flag set by the load function in order to tell function_add that the function being defined is autoloaded. There should be a better way to do this... */ static int is_autoload = 0; /** Make sure that if the specified function is a dynamically loaded function, it has been fully loaded. */ static int load( const wchar_t *name ) { int was_autoload = is_autoload; int res; function_data_t *data; data = (function_data_t *)hash_get( &function, name ); if( data && !data->is_autoload ) return 0; is_autoload = 1; res = parse_util_load( name, L"fish_function_path", &function_remove, 1 ); is_autoload = was_autoload; return res; } /** Insert a list of all dynamically loaded functions into the specified list. */ static void autoload_names( array_list_t *out, int get_hidden ) { int i; array_list_t path_list; const wchar_t *path_var = env_get( L"fish_function_path" ); if( ! path_var ) return; al_init( &path_list ); tokenize_variable_array( path_var, &path_list ); for( i=0; id_name; wchar_t *suffix; if( !get_hidden && fn[0] == L'_' ) continue; suffix = wcsrchr( fn, L'.' ); if( suffix && (wcscmp( suffix, L".fish" ) == 0 ) ) { const wchar_t *dup; *suffix = 0; dup = intern( fn ); if( !dup ) DIE_MEM(); al_push( out, dup ); } } closedir(dir); } al_foreach( &path_list, &free ); al_destroy( &path_list ); } void function_init() { hash_init( &function, &hash_wcs_func, &hash_wcs_cmp ); } static void clear_entry( void *key, void *value ) { halloc_free( value ); } void function_destroy() { hash_foreach( &function, &clear_entry ); hash_destroy( &function ); } void function_add( const wchar_t *name, const wchar_t *val, const wchar_t *desc, array_list_t *events, array_list_t *named_arguments ) { int i; wchar_t *cmd_end; function_data_t *d; CHECK( name, ); CHECK( val, ); function_remove( name ); d = halloc( 0, sizeof( function_data_t ) ); d->definition_offset = parse_util_lineno( parser_get_buffer(), current_block->tok_pos )-1; d->cmd = halloc_wcsdup( d, val ); if( named_arguments ) { d->named_arguments = al_halloc( d ); for( i=0; inamed_arguments, halloc_wcsdup( d, (wchar_t *)al_get( named_arguments, i ) ) ); } } cmd_end = d->cmd + wcslen(d->cmd)-1; d->desc = desc?halloc_wcsdup( d, desc ):0; d->definition_file = intern(reader_current_filename()); d->is_autoload = is_autoload; hash_put( &function, intern(name), d ); for( i=0; icmd; } array_list_t *function_get_named_arguments( const wchar_t *name ) { function_data_t *data; CHECK( name, 0 ); load( name ); data = (function_data_t *)hash_get( &function, name ); if( data == 0 ) return 0; return data->named_arguments; } const wchar_t *function_get_desc( const wchar_t *name ) { function_data_t *data; CHECK( name, 0 ); load( name ); data = (function_data_t *)hash_get( &function, name ); if( data == 0 ) return 0; return _(data->desc); } void function_set_desc( const wchar_t *name, const wchar_t *desc ) { function_data_t *data; CHECK( name, ); CHECK( desc, ); load( name ); data = (function_data_t *)hash_get( &function, name ); if( data == 0 ) return; data->desc = halloc_wcsdup( data, desc ); } /** Search arraylist of strings for specified string */ static int al_contains_str( array_list_t *list, const wchar_t * str ) { int i; for( i=0; i