From 3adf6d25f65ebc17be3f421be93c4c52e01dde81 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 31 Jan 2012 19:47:56 -0800 Subject: Cleanup of builtins. No more hashes. --- builtin.cpp | 336 +++++++++++++++++------------------------------------------ builtin.h | 9 +- complete.cpp | 2 +- 3 files changed, 103 insertions(+), 244 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index 84f563ac..6985c220 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -9,7 +9,7 @@ where NAME is the name of the builtin, and args is a zero-terminated list of arguments. - 2). Add a line like { L"NAME", &builtin_NAME, N_(L"Bla bla bla") }, to the builtin_data variable. The description is used by the completion system. + 2). Add a line like { L"NAME", &builtin_NAME, N_(L"Bla bla bla") }, to the builtin_data_t variable. The description is used by the completion system. Note that this array is sorted! 3). Create a file doc_src/NAME.txt, containing the manual for the builtin in Doxygen-format. Check the other builtin manuals for proper syntax. @@ -85,7 +85,7 @@ /** Datastructure to describe a builtin. */ -typedef struct builtin_data +struct builtin_data_t { /** Name of the builtin @@ -99,14 +99,20 @@ typedef struct builtin_data Description of what the builtin does */ const wchar_t *desc; -} - builtin_data_t; + + bool operator<(const wchar_t *) const; + bool operator<(const builtin_data_t *) const; +}; +bool builtin_data_t::operator<(const wchar_t *other) const +{ + return wcscmp(this->name, other) < 0; +} -/** - Table of all builtins -*/ -static hash_table_t builtin; +bool builtin_data_t::operator<(const builtin_data_t *other) const +{ + return wcscmp(this->name, other->name) < 0; +} int builtin_out_redirect; int builtin_err_redirect; @@ -127,11 +133,6 @@ static array_list_t io_stack; */ static int builtin_stdin; -/** - Table containing descriptions for all builtins -*/ -static hash_table_t *desc=0; - /** The underlying IO redirections behind the current builtin. This should normally not be used - sb_out and friends are already @@ -963,23 +964,18 @@ static int builtin_builtin( parser_t &parser, wchar_t **argv ) if( list ) { - array_list_t names; - int i; - - al_init( &names ); - builtin_get_names( &names ); - sort_list( &names ); + wcstring_list_t names = builtin_get_names(); + sort(names.begin(), names.end()); - for( i=0; iname, name)) { + return found; + } else { + return NULL; + } } - ; - void builtin_init() { - int i; - wopterr = 0; - al_init( &io_stack ); - hash_init( &builtin, &hash_wcs_func, &hash_wcs_cmp ); - for( i=0; builtin_data[i].name; i++ ) + for( size_t i=0; i < BUILTIN_COUNT; i++ ) { - hash_put( &builtin, builtin_data[i].name, (void *)builtin_data[i].func ); - intern_static( builtin_data[i].name ); + intern_static( builtin_datas[i].name ); } } void builtin_destroy() { - if( desc ) - { - hash_destroy( desc ); - free( desc ); - desc=0; - } - al_destroy( &io_stack ); - hash_destroy( &builtin ); } int builtin_exists( const wchar_t *cmd ) { CHECK( cmd, 0 ); - - return !!hash_get(&builtin, cmd); + + return !!builtin_lookup(cmd); } /** @@ -3835,8 +3707,9 @@ int builtin_run( parser_t &parser, const wchar_t * const *argv, io_data_t *io ) CHECK( argv, STATUS_BUILTIN_ERROR ); CHECK( argv[0], STATUS_BUILTIN_ERROR ); - - cmd = (int (*)(parser_t &parser, const wchar_t * const*))hash_get( &builtin, argv[0] ); + + const builtin_data_t *data = builtin_lookup(argv[0]); + cmd = (int (*)(parser_t &parser, const wchar_t * const*))(data ? data->func : NULL); if( argv[1] != 0 && !internal_help(argv[0]) ) { @@ -3847,7 +3720,7 @@ int builtin_run( parser_t &parser, const wchar_t * const *argv, io_data_t *io ) } } - if( cmd != 0 ) + if( data != NULL ) { int status; @@ -3863,44 +3736,29 @@ int builtin_run( parser_t &parser, const wchar_t * const *argv, io_data_t *io ) } -void builtin_get_names( array_list_t *list ) +wcstring_list_t builtin_get_names(void) { - CHECK( list, ); - hash_get_keys( &builtin, list ); + wcstring_list_t result; + result.reserve(BUILTIN_COUNT); + for (size_t i=0; i < BUILTIN_COUNT; i++) { + result.push_back(builtin_datas[i].name); + } + return result; } -void builtin_get_names2(std::vector &list) { - for (int i=0;i &list) { + for (size_t i=0; i < BUILTIN_COUNT; i++) { completion_t data_to_push; - - if (builtin.arr[i].key == 0) - continue; - - data_to_push.completion = (wchar_t*)builtin.arr[i].key; + data_to_push.completion = builtin_datas[i].name; list.push_back( data_to_push ); - } + } } -const wchar_t *builtin_get_desc( const wchar_t *b ) +const wchar_t *builtin_get_desc( const wchar_t *name ) { - CHECK( b, 0 ); - - if( !desc ) - { - int i; - desc = (hash_table_t *)malloc( sizeof( hash_table_t ) ); - if( !desc) - return 0; - - hash_init( desc, &hash_wcs_func, &hash_wcs_cmp ); - - for( i=0; builtin_data[i].name; i++ ) - { - hash_put( desc, builtin_data[i].name, builtin_data[i].desc ); - } - } - - return _( hash_get( desc, b )); + CHECK( name, 0 ); + const builtin_data_t *builtin = builtin_lookup(name); + return builtin ? _(builtin->desc) : NULL; } void builtin_push_io( parser_t &parser, int in ) diff --git a/builtin.h b/builtin.h index d35aecea..b73f3389 100644 --- a/builtin.h +++ b/builtin.h @@ -139,12 +139,13 @@ int builtin_exists( const wchar_t *cmd ); */ int builtin_run( parser_t &parser, const wchar_t * const *argv, io_data_t *io ); +/** Returns a list of all builtin names */ +wcstring_list_t builtin_get_names(void); + /** - Insert all builtin names into l. These are not copies of the strings and should not be freed after use. + Insert all builtin names into list. */ -void builtin_get_names( array_list_t *list ); - -void builtin_get_names2 (std::vector&); +void builtin_get_names(std::vector &list); /** Pushes a new set of input/output to the stack. The new stdin is supplied, a new set of output string_buffer_ts is created. diff --git a/complete.cpp b/complete.cpp index 1102f6ab..75771143 100644 --- a/complete.cpp +++ b/complete.cpp @@ -1201,7 +1201,7 @@ static void complete_cmd( const wchar_t *cmd, if( use_builtin ) { - builtin_get_names2( possible_comp ); + builtin_get_names( possible_comp ); complete_strings( comp, cmd, 0, &builtin_get_desc, possible_comp, 0 ); } // al_destroy( &possible_comp ); -- cgit v1.2.3