Clarify the function name deferral in functions_def

Prohibit making a function with an empty name
This commit is contained in:
ridiculousfish 2012-07-01 15:17:46 -07:00
parent fe26284376
commit 7bbc7a61ce

View File

@ -1085,6 +1085,8 @@ static int builtin_generic( parser_t &parser, wchar_t **argv )
*/ */
static void functions_def( const wcstring &name, wcstring &out ) static void functions_def( const wcstring &name, wcstring &out )
{ {
CHECK( ! name.empty(), );
wcstring desc, def; wcstring desc, def;
function_get_desc(name, &desc); function_get_desc(name, &desc);
function_get_definition(name, &def); function_get_definition(name, &def);
@ -1097,7 +1099,11 @@ static void functions_def( const wcstring &name, wcstring &out )
event_get( &search, &ev ); event_get( &search, &ev );
out.append(L"function "); out.append(L"function ");
if ( name[0]!=L'-' ){
/* Typically we prefer to specify the function name first, e.g. "function foo --description bar"
But If the function name starts with a -, we'll need to output it after all the options. */
bool defer_function_name = (name.at(0) == L'-');
if ( ! defer_function_name ){
out.append(name); out.append(name);
} }
@ -1168,7 +1174,8 @@ static void functions_def( const wcstring &name, wcstring &out )
} }
} }
if ( name[0]==L'-' ){ /* Output the function name if we deferred it */
if ( defer_function_name ){
out.append(L" -- "); out.append(L" -- ");
out.append(name); out.append(name);
} }
@ -3317,18 +3324,29 @@ static int builtin_end( parser_t &parser, wchar_t **argv )
if( d ) if( d )
{ {
/** if (d->name.empty())
Copy the text from the beginning of the function {
until the end command and use as the new definition /* Disallow empty function names */
for the specified function append_format(stderr_buffer, _( L"%ls: No function name given\n" ), argv[0] );
*/
wchar_t *def = wcsndup( parser.get_buffer()+parser.current_block->tok_pos, /* Return an error via a crummy way. Don't just return here, since we need to pop the block. */
parser.get_job_pos()-parser.current_block->tok_pos ); proc_set_last_status(STATUS_BUILTIN_ERROR);
d->definition = def; }
else
{
/**
Copy the text from the beginning of the function
until the end command and use as the new definition
for the specified function
*/
function_add( *d, parser ); wchar_t *def = wcsndup( parser.get_buffer()+parser.current_block->tok_pos,
free( def ); parser.get_job_pos()-parser.current_block->tok_pos );
d->definition = def;
function_add( *d, parser );
free( def );
}
} }
else else
{ {