More cleanup of static string buffers in favor of wcstring

This commit is contained in:
ridiculousfish 2012-01-31 20:22:25 -08:00
parent 562ba1291e
commit eef1e3e77e
4 changed files with 36 additions and 76 deletions

View file

@ -100,13 +100,13 @@ struct builtin_data_t
*/
const wchar_t *desc;
bool operator<(const wchar_t *) const;
bool operator<(const wcstring &) const;
bool operator<(const builtin_data_t *) const;
};
bool builtin_data_t::operator<(const wchar_t *other) const
bool builtin_data_t::operator<(const wcstring &other) const
{
return wcscmp(this->name, other) < 0;
return wcscmp(this->name, other.c_str()) < 0;
}
bool builtin_data_t::operator<(const builtin_data_t *other) const
@ -187,49 +187,21 @@ static int count_char( const wchar_t *str, wchar_t c )
return res;
}
wchar_t *builtin_help_get( parser_t &parser, const wchar_t *name )
wcstring builtin_help_get( parser_t &parser, const wchar_t *name )
{
wcstring_list_t lst;
string_buffer_t cmd;
wchar_t *name_esc;
/*
Because the contents of this buffer is returned by this
function, it must not be free'd on exit, so we allocate it
using halloc.
*/
static string_buffer_t *out = 0;
size_t i;
sb_init( &cmd );
if( !out )
wcstring out;
const wcstring name_esc = escape_string(name, 1);
const wcstring cmd = format_string(L"__fish_print_help %ls", name_esc.c_str());
if( exec_subshell2( cmd, lst ) >= 0 )
{
out = sb_halloc( global_context );
}
else
{
sb_clear( out );
}
name_esc = escape( name, 1 );
sb_printf( &cmd, L"__fish_print_help %ls", name_esc );
if( exec_subshell2( (wchar_t *)cmd.buff, lst ) >= 0 )
{
for( i=0; i<lst.size(); i++ )
for( size_t i=0; i<lst.size(); i++ )
{
sb_append( out, lst.at(i).c_str() );
sb_append( out, L"\n" );
out.append(lst.at(i));
out.push_back(L'\n');
}
}
sb_destroy( &cmd );
free( name_esc );
return (wchar_t *)out->buff;
return out;
}
/**
@ -246,7 +218,6 @@ wchar_t *builtin_help_get( parser_t &parser, const wchar_t *name )
static void builtin_print_help( parser_t &parser, const wchar_t *cmd, string_buffer_t *b )
{
const wchar_t *h;
int is_short = 0;
if( b == sb_err )
@ -255,12 +226,12 @@ static void builtin_print_help( parser_t &parser, const wchar_t *cmd, string_buf
parser.current_line() );
}
h = builtin_help_get( parser, cmd );
const wcstring h = builtin_help_get( parser, cmd );
if( !h )
if( !h.size())
return;
wchar_t *str = wcsdup( h );
wchar_t *str = wcsdup( h.c_str() );
if( str )
{
@ -3654,10 +3625,10 @@ static const builtin_data_t builtin_datas[]=
#define BUILTIN_COUNT (sizeof builtin_datas / sizeof *builtin_datas)
static const builtin_data_t *builtin_lookup(const wchar_t *name) {
static const builtin_data_t *builtin_lookup(const wcstring &name) {
const builtin_data_t *array_end = builtin_datas + BUILTIN_COUNT;
const builtin_data_t *found = std::lower_bound(builtin_datas, array_end, name);
if (found != array_end && ! wcscmp(found->name, name)) {
if (found != array_end && name == found->name) {
return found;
} else {
return NULL;
@ -3681,10 +3652,8 @@ void builtin_destroy()
al_destroy( &io_stack );
}
int builtin_exists( const wchar_t *cmd )
int builtin_exists( const wcstring &cmd )
{
CHECK( cmd, 0 );
return !!builtin_lookup(cmd);
}

View file

@ -123,7 +123,7 @@ void builtin_destroy();
/**
Is there a builtin command with the given name?
*/
int builtin_exists( const wchar_t *cmd );
int builtin_exists( const wcstring &cmd );
/**
Execute a builtin command
@ -142,9 +142,7 @@ 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 list.
*/
/** Insert all builtin names into list. */
void builtin_get_names(std::vector<completion_t> &list);
/**
@ -174,10 +172,9 @@ const wchar_t *builtin_complete_get_temporary_buffer();
/**
Run the __fish_print_help function to obtain the help information
for the specified command. The resulting string will be valid until
the next time this function is called, and must never be free'd manually.
for the specified command.
*/
wchar_t *builtin_help_get( parser_t &parser, const wchar_t *cmd );
wcstring builtin_help_get( parser_t &parser, const wchar_t *cmd );
#endif

View file

@ -707,7 +707,7 @@ void tokenize( const wchar_t * const buff, int * const color, const int pos, arr
any files for that
*/
if( use_builtin )
is_cmd |= builtin_exists( cmd.c_str() );
is_cmd |= builtin_exists( cmd );
if( use_function )
is_cmd |= function_exists_no_autoload( cmd.c_str(), vars );

View file

@ -1883,7 +1883,7 @@ int parser_t::parse_job( process_t *p,
if( !p->type )
{
if( use_builtin &&
builtin_exists(args.at(0).completion.c_str()))
builtin_exists(args.at(0).completion))
{
p->type = INTERNAL_BUILTIN;
is_new_block |= parser_keywords_is_block( args.at( 0 ).completion );
@ -2504,7 +2504,6 @@ int parser_t::eval( const wcstring &cmdStr, io_data_t *io, enum block_type_t blo
if( (!error_code) && (!exit_status()) && (!proc_get_last_status()) )
{
wchar_t *h;
//debug( 2, L"Status %d\n", proc_get_last_status() );
@ -2514,9 +2513,9 @@ int parser_t::eval( const wcstring &cmdStr, io_data_t *io, enum block_type_t blo
BLOCK_END_ERR_MSG );
fwprintf( stderr, L"%ls", parser_t::current_line() );
h = builtin_help_get( *this, L"end" );
if( h )
fwprintf( stderr, L"%ls", h );
const wcstring h = builtin_help_get( *this, L"end" );
if( h.size() )
fwprintf( stderr, L"%ls", h.c_str() );
break;
}
@ -3026,16 +3025,14 @@ int parser_t::test( const wchar_t * buff,
if( out )
{
wchar_t *h;
error( SYNTAX_ERROR,
tok_get_pos( &tok ),
INVALID_CASE_ERR_MSG );
print_errors( out, prefix);
h = builtin_help_get( *this, L"case" );
if( h )
sb_printf( out, L"%ls", h );
const wcstring h = builtin_help_get( *this, L"case" );
if( h.size() )
sb_printf( out, L"%ls", h.c_str() );
}
}
}
@ -3189,15 +3186,13 @@ int parser_t::test( const wchar_t * buff,
err = 1;
if( out )
{
wchar_t *h;
error( SYNTAX_ERROR,
tok_get_pos( &tok ),
INVALID_END_ERR_MSG );
print_errors( out, prefix );
h = builtin_help_get( *this, L"end" );
if( h )
sb_printf( out, L"%ls", h );
const wcstring h = builtin_help_get( *this, L"end" );
if( h.size() )
sb_printf( out, L"%ls", h.c_str() );
}
}
@ -3469,7 +3464,6 @@ int parser_t::test( const wchar_t * buff,
if( out && count>0 )
{
const wchar_t *h;
const wchar_t *cmd;
error( SYNTAX_ERROR,
@ -3481,10 +3475,10 @@ int parser_t::test( const wchar_t * buff,
cmd = parser_get_block_command( block_type[count -1] );
if( cmd )
{
h = builtin_help_get( *this, cmd );
if( cmd )
const wcstring h = builtin_help_get( *this, cmd );
if( h.size() )
{
sb_printf( out, L"%ls", h );
sb_printf( out, L"%ls", h.c_str() );
}
}