replaced the functions '--rename' option with '--copy'.

Copying the function implementation was the main point. Actually removing the original isn't necessary, as that
functionality already exists (functions -e).
This commit is contained in:
Christopher Nilsson 2010-09-09 23:48:18 +10:00
parent 208be0f4d4
commit 7914c92824
3 changed files with 17 additions and 45 deletions

View file

@ -1252,7 +1252,7 @@ static int builtin_functions( wchar_t **argv )
int show_hidden=0; int show_hidden=0;
int res = STATUS_BUILTIN_OK; int res = STATUS_BUILTIN_OK;
int query = 0; int query = 0;
int rename = 0; int copy = 0;
woptind=0; woptind=0;
@ -1284,7 +1284,7 @@ static int builtin_functions( wchar_t **argv )
} }
, ,
{ {
L"rename", no_argument, 0, 'r' L"copy", no_argument, 0, 'c'
} }
, ,
{ {
@ -1299,7 +1299,7 @@ static int builtin_functions( wchar_t **argv )
int opt = wgetopt_long( argc, int opt = wgetopt_long( argc,
argv, argv,
L"ed:nahqr", L"ed:nahqc",
long_options, long_options,
&opt_index ); &opt_index );
if( opt == -1 ) if( opt == -1 )
@ -1343,8 +1343,8 @@ static int builtin_functions( wchar_t **argv )
query = 1; query = 1;
break; break;
case 'r': case 'c':
rename = 1; copy = 1;
break; break;
case '?': case '?':
@ -1356,9 +1356,9 @@ static int builtin_functions( wchar_t **argv )
} }
/* /*
Erase, desc, query, rename and list are mutually exclusive Erase, desc, query, copy and list are mutually exclusive
*/ */
if( (erase + (!!desc) + list + query + rename) > 1 ) if( (erase + (!!desc) + list + query + copy) > 1 )
{ {
sb_printf( sb_err, sb_printf( sb_err,
_( L"%ls: Invalid combination of options\n" ), _( L"%ls: Invalid combination of options\n" ),
@ -1443,7 +1443,7 @@ static int builtin_functions( wchar_t **argv )
al_destroy( &names ); al_destroy( &names );
return STATUS_BUILTIN_OK; return STATUS_BUILTIN_OK;
} }
else if( rename ) else if( copy )
{ {
wchar_t *current_func; wchar_t *current_func;
wchar_t *new_func; wchar_t *new_func;
@ -1471,11 +1471,11 @@ static int builtin_functions( wchar_t **argv )
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
// keep things simple: don't allow existing names to be rename targets. // keep things simple: don't allow existing names to be copy targets.
if( function_exists( new_func ) ) if( function_exists( new_func ) )
{ {
sb_printf( sb_err, sb_printf( sb_err,
_( L"%ls: Function '%ls' already exists. Cannot rename '%ls'\n" ), _( L"%ls: Function '%ls' already exists. Cannot create copy '%ls'\n" ),
argv[0], argv[0],
new_func, new_func,
current_func ); current_func );
@ -1484,8 +1484,9 @@ static int builtin_functions( wchar_t **argv )
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
function_rename( current_func, new_func ); if( function_copy( current_func, new_func ) )
return STATUS_BUILTIN_OK; return STATUS_BUILTIN_OK;
return STATUS_BUILTIN_ERROR;
} }
for( i=woptind; i<argc; i++ ) for( i=woptind; i<argc; i++ )

View file

@ -222,8 +222,6 @@ int function_copy( const wchar_t *name, const wchar_t *new_name )
{ {
int i; int i;
function_internal_data_t *d, *orig_d; function_internal_data_t *d, *orig_d;
event_t ev, *orig_ev;
array_list_t *fn_events;
CHECK( name, 0 ); CHECK( name, 0 );
CHECK( new_name, 0 ); CHECK( new_name, 0 );
@ -245,41 +243,19 @@ int function_copy( const wchar_t *name, const wchar_t *new_name )
al_push( d->named_arguments, halloc_wcsdup( d, (wchar_t *)al_get( orig_d->named_arguments, i ) ) ); al_push( d->named_arguments, halloc_wcsdup( d, (wchar_t *)al_get( orig_d->named_arguments, i ) ) );
} }
d->description = orig_d->description?halloc_wcsdup(d, orig_d->description):0; d->description = orig_d->description?halloc_wcsdup(d, orig_d->description):0;
d->shadows = orig_d->shadows;
// This new instance of the function shouldn't be tied to the def
// file of the original.
d->definition_file = 0; d->definition_file = 0;
d->is_autoload = 0; d->is_autoload = 0;
d->shadows = orig_d->shadows;
} }
hash_put( &function, intern(new_name), d ); hash_put( &function, intern(new_name), d );
// wire up the same events... if any.
ev.type = EVENT_ANY;
ev.function_name = name;
event_get(&ev, fn_events);
if( fn_events )
{
ev.function_name = new_name;
for( i=0; i<al_get_count( fn_events ); i++ )
{
orig_ev = (event_t *)al_get( fn_events, i );
// event_add_handler will deep-copy ev, so we can reuse.
ev.type = orig_ev->type;
ev.param1 = orig_ev->param1;
event_add_handler( &ev );
}
}
return 1; return 1;
} }
void function_rename( const wchar_t *name, const wchar_t *new_name )
{
if( function_copy( name, new_name ) )
function_remove( name );
}
int function_exists( const wchar_t *cmd ) int function_exists( const wchar_t *cmd )
{ {

View file

@ -137,9 +137,4 @@ int function_get_shadows( const wchar_t *name );
*/ */
int function_copy( const wchar_t *name, const wchar_t *new_name ); int function_copy( const wchar_t *name, const wchar_t *new_name );
/**
Renames the specified function.
*/
void function_rename( const wchar_t *name, const wchar_t *new_name );
#endif #endif