Skip to content

Commit

Permalink
Use halloc in a few more places, including the highlight code
Browse files Browse the repository at this point in the history
darcs-hash:20060612141233-ac50b-1c44411dd31cdc31d6ccb226f567c308c4fc0f55.gz
  • Loading branch information
liljencrantz committed Jun 12, 2006
1 parent 038dcca commit d32751d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 72 deletions.
19 changes: 8 additions & 11 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,8 @@ static int builtin_cd( wchar_t **argv )
wchar_t *dir_in;
wchar_t *dir;
int res=0;

void *context = halloc( 0, 0 );

if( argv[1] == 0 )
{
dir_in = env_get( L"HOME" );
Expand All @@ -1979,7 +1980,7 @@ static int builtin_cd( wchar_t **argv )
else
dir_in = argv[1];

dir = parser_cdpath_get( dir_in );
dir = parser_cdpath_get( context, dir_in );

if( !dir )
{
Expand All @@ -1994,10 +1995,9 @@ static int builtin_cd( wchar_t **argv )
(void *)0 );
}

return 1;
res = 1;
}

if( wchdir( dir ) != 0 )
else if( wchdir( dir ) != 0 )
{
sb_printf( sb_err,
_( L"%ls: '%ls' is not a directory\n" ),
Expand All @@ -2010,18 +2010,15 @@ static int builtin_cd( wchar_t **argv )
(void *)0 );
}

free( dir );

return 1;
res = 1;
}

if( !set_pwd(L"PWD") )
else if( !set_pwd(L"PWD") )
{
res=1;
sb_printf( sb_err, _( L"%ls: Could not set PWD variable\n" ), argv[0] );
}

free( dir );
halloc_free( context );

return res;
}
Expand Down
47 changes: 28 additions & 19 deletions complete.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "intern.h"
#include "translate.h"
#include "parse_util.h"
#include "halloc.h"
#include "halloc_util.h"
#include "wutil.h"

Expand Down Expand Up @@ -557,18 +558,19 @@ void complete_remove( const wchar_t *cmd,
}

/**
Find the full path and commandname from a command string. the
result of \c pathp must be freed by the caller, the result of \c
cmdp must not be freed by the caller.
Find the full path and commandname from a command string. Both
pointers are allocated using halloc and will be free'd when\c
context is halloc_free'd.
*/
static void parse_cmd_string( const wchar_t *str,
static void parse_cmd_string( void *context,
const wchar_t *str,
wchar_t **pathp,
wchar_t **cmdp )
{
wchar_t *cmd, *path;

/* Get the path of the command */
path = get_filename( str );
path = parser_get_filename( context, str );
if( path == 0 )
{
/**
Expand Down Expand Up @@ -611,6 +613,8 @@ int complete_is_valid_option( const wchar_t *str,
int gnu_opt_len=0;
char *short_validated;

void *context;

if( !str || !opt )
{
debug( 0, L"%s called with null input", __func__ );
Expand Down Expand Up @@ -652,11 +656,15 @@ int complete_is_valid_option( const wchar_t *str,
return 0;
}

if( !(short_validated = malloc( wcslen( opt ) )))
context = halloc( 0, 0 );

if( !(short_validated = halloc( context, wcslen( opt ) )))
{
die_mem();
}



memset( short_validated, 0, wcslen( opt ) );

hash_init( &gnu_match_hash,
Expand All @@ -677,7 +685,7 @@ int complete_is_valid_option( const wchar_t *str,
}
}

parse_cmd_string( str, &path, &cmd );
parse_cmd_string( context, str, &path, &cmd );

/*
Make sure completions are loaded for the specified command
Expand Down Expand Up @@ -776,7 +784,6 @@ int complete_is_valid_option( const wchar_t *str,
}
}
}
free( path );

if( authorative )
{
Expand Down Expand Up @@ -830,8 +837,9 @@ int complete_is_valid_option( const wchar_t *str,
}

hash_destroy( &gnu_match_hash );
free( short_validated );

halloc_free( context );

return (authorative && found_match)?opt_found:1;
}

Expand Down Expand Up @@ -1567,13 +1575,14 @@ static int complete_param( wchar_t *cmd_orig,
wchar_t *cmd, *path;
int use_common=1, use_files=1;

parse_cmd_string( cmd_orig, &path, &cmd );
void *context = halloc( 0, 0 );

parse_cmd_string( context, cmd_orig, &path, &cmd );

complete_load( cmd, 1 );

al_init( &matches );


for( i=first_entry; i; i=i->next )
{
wchar_t *match = i->cmd_type?path:cmd;
Expand Down Expand Up @@ -1696,11 +1705,10 @@ static int complete_param( wchar_t *cmd_orig,
*/
if( o->long_opt[0] != L'\0' )
{
string_buffer_t whole_opt;
sb_init( &whole_opt );
sb_append2( &whole_opt, o->old_mode?L"-":L"--", o->long_opt, (void *)0 );
string_buffer_t *whole_opt = sb_halloc( context );
sb_append2( whole_opt, o->old_mode?L"-":L"--", o->long_opt, (void *)0 );

if( wcsncmp( str, (wchar_t *)whole_opt.buff, wcslen(str) )==0)
if( wcsncmp( str, (wchar_t *)whole_opt->buff, wcslen(str) )==0)
{
/*
If the option requires arguments, add
Expand All @@ -1713,7 +1721,7 @@ static int complete_param( wchar_t *cmd_orig,
if( o->old_mode || !(o->result_mode & NO_COMMON ) )
{
al_push( comp_out,
wcsdupcat2( &((wchar_t *)whole_opt.buff)[wcslen(str)],
wcsdupcat2( &((wchar_t *)whole_opt->buff)[wcslen(str)],
COMPLETE_SEP_STR,
C_(o->desc),
(void *)0) );
Expand All @@ -1722,20 +1730,21 @@ static int complete_param( wchar_t *cmd_orig,
if( !o->old_mode && ( wcslen(o->comp) || (o->result_mode & NO_COMMON ) ) )
{
al_push( comp_out,
wcsdupcat2( &((wchar_t *)whole_opt.buff)[wcslen(str)],
wcsdupcat2( &((wchar_t *)whole_opt->buff)[wcslen(str)],
L"=",
COMPLETE_SEP_STR,
C_(o->desc),
(void *)0) );
}
}
sb_destroy( &whole_opt );
}
}
}
}
}
free( path );

halloc_free( context );

return use_files;
}

Expand Down
2 changes: 1 addition & 1 deletion halloc_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void *halloc_register( void *context, void *data )
return data;
}

wchar_t *halloc_wcsdup( void *context, wchar_t *in )
wchar_t *halloc_wcsdup( void *context, const wchar_t *in )
{
size_t len=wcslen(in);
wchar_t *out = halloc( context, sizeof( wchar_t)*(len+1));
Expand Down
2 changes: 1 addition & 1 deletion halloc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void *halloc_register( void *context, void *data );
Make a copy of the specified string using memory allocated using
halloc and the specified context
*/
wchar_t *halloc_wcsdup( void *context, wchar_t *str );
wchar_t *halloc_wcsdup( void *context, const wchar_t *str );

/**
Make a copy of the specified substring using memory allocated using
Expand Down
49 changes: 28 additions & 21 deletions highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "common.h"
#include "complete.h"
#include "output.h"
#include "halloc.h"
#include "halloc_util.h"

static void highlight_universal_internal( wchar_t * buff,
int *color,
Expand Down Expand Up @@ -381,11 +383,25 @@ void highlight_shell( wchar_t * buff,
int i;
int last_val;
wchar_t *last_cmd=0;
int len = wcslen(buff);
int len;

void *context;

if( !buff || !color )
{
debug( 0, L"%s called with null input", __func__ );
return;
}



len = wcslen(buff);

if( !len )
return;

context = halloc( 0, 0 );

for( i=0; buff[i] != 0; i++ )
color[i] = -1;

Expand Down Expand Up @@ -430,7 +446,7 @@ void highlight_shell( wchar_t * buff,
/*
Command. First check that the command actually exists.
*/
wchar_t *cmd = expand_one( 0,
wchar_t *cmd = expand_one( context,
wcsdup(tok_last( &tok )),
EXPAND_SKIP_SUBSHELL | EXPAND_SKIP_VARIABLES);

Expand Down Expand Up @@ -491,15 +507,13 @@ void highlight_shell( wchar_t * buff,
/*
Check if this is a regular command
*/
is_cmd |= !!(tmp=get_filename( cmd ));
free(tmp);
is_cmd |= !!(tmp=parser_get_filename( context, cmd ));

/*
Could not find the command. Maybe it is
a path for a implicit cd command.
*/
is_cmd |= !!(tmp=parser_cdpath_get( cmd ));
free( tmp );
is_cmd |= !!(tmp=parser_cdpath_get( context, cmd ));

if( is_cmd )
{
Expand All @@ -513,13 +527,10 @@ void highlight_shell( wchar_t * buff,
}
had_cmd = 1;
}
free(cmd);

if( had_cmd )
{
if( last_cmd )
free( last_cmd );
last_cmd = wcsdup( tok_last( &tok ) );
last_cmd = halloc_wcsdup( context, tok_last( &tok ) );
}
}

Expand Down Expand Up @@ -553,7 +564,7 @@ void highlight_shell( wchar_t * buff,
{
case TOK_STRING:
{
target = expand_one( 0, wcsdup( tok_last( &tok ) ), EXPAND_SKIP_SUBSHELL);
target = expand_one( context, wcsdup( tok_last( &tok ) ), EXPAND_SKIP_SUBSHELL);
/*
Redirect filename may contain a subshell.
If so, it will be ignored/not flagged.
Expand All @@ -571,7 +582,7 @@ void highlight_shell( wchar_t * buff,

if( target != 0 )
{
wchar_t *dir = wcsdup( target );
wchar_t *dir = halloc_wcsdup( context, target );
wchar_t *dir_end = wcsrchr( dir, L'/' );
struct stat buff;
/*
Expand All @@ -587,9 +598,8 @@ void highlight_shell( wchar_t * buff,
if( error )
al_push( error, wcsdupcat2( L"Directory \'", dir, L"\' does not exist", 0 ) );

}
}
}
free( dir );

/*
If the file is read from or appended to, check
Expand All @@ -605,7 +615,6 @@ void highlight_shell( wchar_t * buff,
al_push( error, wcsdupcat2( L"File \'", target, L"\' does not exist", 0 ) );
}
}
free( target );
}
break;
}
Expand Down Expand Up @@ -655,16 +664,13 @@ void highlight_shell( wchar_t * buff,
}
}

if( last_cmd )
free( last_cmd );

tok_destroy( &tok );

/*
Locate and syntax highlight subshells recursively
*/

wchar_t *buffcpy = wcsdup( buff );
wchar_t *buffcpy = halloc_wcsdup( context, buff );
wchar_t *subpos=buffcpy;
int done=0;

Expand Down Expand Up @@ -694,8 +700,6 @@ void highlight_shell( wchar_t * buff,
subpos = end+1;

}
free( buffcpy);


last_val=0;
for( i=0; buff[i] != 0; i++ )
Expand All @@ -719,6 +723,9 @@ void highlight_shell( wchar_t * buff,
color[i]=0;
}
}

halloc_free( context );

}

/**
Expand Down
Loading

0 comments on commit d32751d

Please sign in to comment.