mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-25 12:23:09 +00:00
Excised some more halloc
This commit is contained in:
parent
006523ac59
commit
8a2737566c
6 changed files with 64 additions and 90 deletions
64
complete.cpp
64
complete.cpp
|
@ -299,16 +299,6 @@ static void complete_free_opt_recursive( complete_entry_opt_t *o )
|
||||||
halloc_free( o );
|
halloc_free( o );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
Free a complete_entry_t and its contents
|
|
||||||
*/
|
|
||||||
static void complete_free_entry( complete_entry_t *c )
|
|
||||||
{
|
|
||||||
// free( c->cmd );
|
|
||||||
free( c->short_opt_str );
|
|
||||||
complete_free_opt_recursive( c->first_option );
|
|
||||||
free( c );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Search for an exactly matching completion entry
|
Search for an exactly matching completion entry
|
||||||
|
@ -584,6 +574,24 @@ static void parse_cmd_string( void *context,
|
||||||
*cmdp=cmd;
|
*cmdp=cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Find the full path and commandname from a command string 'str'.
|
||||||
|
*/
|
||||||
|
static void parse_cmd_string(const wcstring &str, wcstring &path, wcstring &cmd) {
|
||||||
|
if (! path_get_path_string(str, path)) {
|
||||||
|
/** Use the empty string as the 'path' for commands that can not be found. */
|
||||||
|
path = L"";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Make sure the path is not included in the command */
|
||||||
|
size_t last_slash = str.find_last_of(L'/');
|
||||||
|
if (last_slash != wcstring::npos) {
|
||||||
|
cmd = str.substr(last_slash + 1);
|
||||||
|
} else {
|
||||||
|
cmd = str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int complete_is_valid_option( const wchar_t *str,
|
int complete_is_valid_option( const wchar_t *str,
|
||||||
const wchar_t *opt,
|
const wchar_t *opt,
|
||||||
array_list_t *errors,
|
array_list_t *errors,
|
||||||
|
@ -591,7 +599,7 @@ int complete_is_valid_option( const wchar_t *str,
|
||||||
{
|
{
|
||||||
complete_entry_t *i;
|
complete_entry_t *i;
|
||||||
complete_entry_opt_t *o;
|
complete_entry_opt_t *o;
|
||||||
wchar_t *cmd, *path;
|
wcstring cmd, path;
|
||||||
int found_match = 0;
|
int found_match = 0;
|
||||||
int authoritative = 1;
|
int authoritative = 1;
|
||||||
int opt_found=0;
|
int opt_found=0;
|
||||||
|
@ -601,9 +609,9 @@ int complete_is_valid_option( const wchar_t *str,
|
||||||
int is_short_opt=0;
|
int is_short_opt=0;
|
||||||
int is_gnu_exact=0;
|
int is_gnu_exact=0;
|
||||||
int gnu_opt_len=0;
|
int gnu_opt_len=0;
|
||||||
char *short_validated;
|
|
||||||
|
std::vector<char> short_validated;
|
||||||
|
|
||||||
void *context;
|
|
||||||
|
|
||||||
CHECK( str, 0 );
|
CHECK( str, 0 );
|
||||||
CHECK( opt, 0 );
|
CHECK( opt, 0 );
|
||||||
|
@ -639,17 +647,9 @@ int complete_is_valid_option( const wchar_t *str,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
context = halloc( 0, 0 );
|
|
||||||
|
|
||||||
if( !(short_validated = (char *)halloc( context, wcslen( opt ) )))
|
|
||||||
{
|
|
||||||
DIE_MEM();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
memset( short_validated, 0, wcslen( opt ) );
|
|
||||||
|
|
||||||
|
short_validated.resize(wcslen(opt), 0);
|
||||||
|
|
||||||
hash_init( &gnu_match_hash,
|
hash_init( &gnu_match_hash,
|
||||||
&hash_wcs_func,
|
&hash_wcs_func,
|
||||||
&hash_wcs_cmp );
|
&hash_wcs_cmp );
|
||||||
|
@ -668,16 +668,16 @@ int complete_is_valid_option( const wchar_t *str,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_cmd_string( context, str, &path, &cmd );
|
parse_cmd_string( str, path, cmd );
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Make sure completions are loaded for the specified command
|
Make sure completions are loaded for the specified command
|
||||||
*/
|
*/
|
||||||
if (allow_autoload) complete_load( cmd, 0 );
|
if (allow_autoload) complete_load( cmd, false );
|
||||||
|
|
||||||
for( i=first_entry; i; i=i->next )
|
for( i=first_entry; i; i=i->next )
|
||||||
{
|
{
|
||||||
wchar_t *match = i->cmd_type?path:cmd;
|
const wcstring &match = i->cmd_type?path:cmd;
|
||||||
const wchar_t *a;
|
const wchar_t *a;
|
||||||
|
|
||||||
if( !wildcard_match( match, i->cmd ) )
|
if( !wildcard_match( match, i->cmd ) )
|
||||||
|
@ -756,12 +756,12 @@ int complete_is_valid_option( const wchar_t *str,
|
||||||
nopt[1]=opt[1];
|
nopt[1]=opt[1];
|
||||||
nopt[2]=L'\0';
|
nopt[2]=L'\0';
|
||||||
|
|
||||||
short_validated[a-opt] =
|
short_validated.at(a-opt) =
|
||||||
complete_is_valid_argument( str, nopt, &opt[2]);
|
complete_is_valid_argument( str, nopt, &opt[2]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
short_validated[a-opt]=1;
|
short_validated.at(a-opt)=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -782,7 +782,7 @@ int complete_is_valid_option( const wchar_t *str,
|
||||||
opt_found=1;
|
opt_found=1;
|
||||||
for( j=1; j<wcslen(opt); j++)
|
for( j=1; j<wcslen(opt); j++)
|
||||||
{
|
{
|
||||||
if ( !short_validated[j])
|
if ( !short_validated.at(j))
|
||||||
{
|
{
|
||||||
if( errors )
|
if( errors )
|
||||||
{
|
{
|
||||||
|
@ -821,7 +821,6 @@ int complete_is_valid_option( const wchar_t *str,
|
||||||
|
|
||||||
hash_destroy( &gnu_match_hash );
|
hash_destroy( &gnu_match_hash );
|
||||||
|
|
||||||
halloc_free( context );
|
|
||||||
return (authoritative && found_match)?opt_found:1;
|
return (authoritative && found_match)?opt_found:1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1289,9 +1288,8 @@ static int short_ok( const wchar_t *arg,
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void complete_load( const wchar_t *name, int reload )
|
void complete_load( const wcstring &name, bool reload )
|
||||||
{
|
{
|
||||||
CHECK( name, );
|
|
||||||
completion_autoloader.load( name, reload );
|
completion_autoloader.load( name, reload );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1316,7 +1314,7 @@ static int complete_param( const wchar_t *cmd_orig,
|
||||||
|
|
||||||
parse_cmd_string( context, cmd_orig, &path, &cmd );
|
parse_cmd_string( context, cmd_orig, &path, &cmd );
|
||||||
|
|
||||||
complete_load( cmd, 1 );
|
complete_load( cmd, true );
|
||||||
|
|
||||||
for( i=first_entry; i; i=i->next )
|
for( i=first_entry; i; i=i->next )
|
||||||
{
|
{
|
||||||
|
|
|
@ -256,7 +256,7 @@ int complete_is_valid_argument( const wchar_t *str,
|
||||||
\param cmd the command for which to load command-specific completions
|
\param cmd the command for which to load command-specific completions
|
||||||
\param reload should the commands completions be reloaded, even if they where previously loaded. (This is set to true on actual completions, so that changed completion are updated in running shells)
|
\param reload should the commands completions be reloaded, even if they where previously loaded. (This is set to true on actual completions, so that changed completion are updated in running shells)
|
||||||
*/
|
*/
|
||||||
void complete_load( const wchar_t *cmd, int reload );
|
void complete_load( const wcstring &cmd, bool reload );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Create a new completion entry
|
Create a new completion entry
|
||||||
|
|
21
fish.cpp
21
fish.cpp
|
@ -73,10 +73,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
*/
|
*/
|
||||||
static int read_init()
|
static int read_init()
|
||||||
{
|
{
|
||||||
wchar_t *config_dir;
|
|
||||||
wchar_t *config_dir_escaped;
|
|
||||||
void *context;
|
|
||||||
string_buffer_t *eval_buff;
|
|
||||||
parser_t &parser = parser_t::principal_parser();
|
parser_t &parser = parser_t::principal_parser();
|
||||||
|
|
||||||
parser.eval( L"builtin . " DATADIR "/fish/config.fish 2>/dev/null", 0, TOP );
|
parser.eval( L"builtin . " DATADIR "/fish/config.fish 2>/dev/null", 0, TOP );
|
||||||
|
@ -85,24 +81,19 @@ static int read_init()
|
||||||
/*
|
/*
|
||||||
We need to get the configuration directory before we can source the user configuration file
|
We need to get the configuration directory before we can source the user configuration file
|
||||||
*/
|
*/
|
||||||
context = halloc( 0, 0 );
|
wcstring config_dir;
|
||||||
eval_buff = sb_halloc( context );
|
|
||||||
config_dir = path_get_config( context );
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
If config_dir is null then we have no configuration directory
|
If path_get_config returns false then we have no configuration directory
|
||||||
and no custom config to load.
|
and no custom config to load.
|
||||||
*/
|
*/
|
||||||
if( config_dir )
|
if (path_get_config(config_dir))
|
||||||
{
|
{
|
||||||
config_dir_escaped = escape( config_dir, 1 );
|
wcstring config_dir_escaped = escape_string( config_dir, 1 );
|
||||||
sb_printf( eval_buff, L"builtin . %ls/config.fish 2>/dev/null", config_dir_escaped );
|
wcstring eval_buff = format_string(L"builtin . %ls/config.fish 2>/dev/null", config_dir_escaped.c_str());
|
||||||
parser.eval( (wchar_t *)eval_buff->buff, 0, TOP );
|
parser.eval( eval_buff.c_str(), 0, TOP );
|
||||||
free( config_dir_escaped );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
halloc_free( context );
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
54
output.cpp
54
output.cpp
|
@ -102,18 +102,7 @@ static const int col_idx[]=
|
||||||
6,
|
6,
|
||||||
7,
|
7,
|
||||||
FISH_COLOR_NORMAL,
|
FISH_COLOR_NORMAL,
|
||||||
}
|
};
|
||||||
;
|
|
||||||
|
|
||||||
/**
|
|
||||||
Size of writestr_buff
|
|
||||||
*/
|
|
||||||
static size_t writestr_buff_sz=0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
Temp buffer used for converting from wide to narrow strings
|
|
||||||
*/
|
|
||||||
static char *writestr_buff = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
The function used for output
|
The function used for output
|
||||||
|
@ -127,14 +116,6 @@ static int (*out)(char c) = &writeb_internal;
|
||||||
static wchar_t *current_term = 0;
|
static wchar_t *current_term = 0;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
Cleanup function. Run automatically through halloc
|
|
||||||
*/
|
|
||||||
static void output_destroy()
|
|
||||||
{
|
|
||||||
free( writestr_buff );
|
|
||||||
}
|
|
||||||
|
|
||||||
void output_set_writer( int (*writer)(char) )
|
void output_set_writer( int (*writer)(char) )
|
||||||
{
|
{
|
||||||
CHECK( writer, );
|
CHECK( writer, );
|
||||||
|
@ -436,39 +417,30 @@ void writestr( const wchar_t *str )
|
||||||
}
|
}
|
||||||
|
|
||||||
len++;
|
len++;
|
||||||
|
|
||||||
/*
|
|
||||||
Reallocate if needed
|
|
||||||
*/
|
|
||||||
if( writestr_buff_sz < len )
|
|
||||||
{
|
|
||||||
if( !writestr_buff )
|
|
||||||
{
|
|
||||||
halloc_register_function_void( global_context, &output_destroy );
|
|
||||||
}
|
|
||||||
|
|
||||||
writestr_buff = (char *)realloc( writestr_buff, len );
|
|
||||||
if( !writestr_buff )
|
|
||||||
{
|
|
||||||
DIE_MEM();
|
|
||||||
}
|
|
||||||
writestr_buff_sz = len;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Convert
|
Convert
|
||||||
*/
|
*/
|
||||||
wcstombs( writestr_buff,
|
char *buffer, static_buffer[256];
|
||||||
|
if (len <= sizeof static_buffer)
|
||||||
|
buffer = static_buffer;
|
||||||
|
else
|
||||||
|
buffer = new char[len];
|
||||||
|
|
||||||
|
wcstombs( buffer,
|
||||||
str,
|
str,
|
||||||
writestr_buff_sz );
|
len );
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Write
|
Write
|
||||||
*/
|
*/
|
||||||
for( pos = writestr_buff; *pos; pos++ )
|
for( pos = buffer; *pos; pos++ )
|
||||||
{
|
{
|
||||||
out( *pos );
|
out( *pos );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (buffer != static_buffer)
|
||||||
|
delete[] buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
12
path.cpp
12
path.cpp
|
@ -252,6 +252,18 @@ wchar_t *path_get_path( const wchar_t *cmd )
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool path_get_path_string(const wcstring &cmd, wcstring &output)
|
||||||
|
{
|
||||||
|
bool success = false;
|
||||||
|
wchar_t *tmp = path_get_path(cmd.c_str());
|
||||||
|
if (tmp) {
|
||||||
|
output = tmp;
|
||||||
|
free(tmp);
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env_vars &vars)
|
bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env_vars &vars)
|
||||||
{
|
{
|
||||||
|
|
1
path.h
1
path.h
|
@ -34,6 +34,7 @@ bool path_get_config(wcstring &path);
|
||||||
wchar_t *path_get_path( const wchar_t *cmd );
|
wchar_t *path_get_path( const wchar_t *cmd );
|
||||||
|
|
||||||
class env_vars;
|
class env_vars;
|
||||||
|
bool path_get_path_string(const wcstring &cmd, wcstring &output);
|
||||||
bool path_get_path_string(const wcstring &cmd, wcstring &output, const env_vars &vars);
|
bool path_get_path_string(const wcstring &cmd, wcstring &output, const env_vars &vars);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue