From a281c4080bf268fa08d0b6001ab081c53a225035 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 7 Mar 2011 18:31:38 +0000 Subject: [PATCH] [cmdline] Add generic concat_args() function Signed-off-by: Michael Brown --- src/core/exec.c | 49 +++++++++++++++++++++++++++++++----- src/hci/commands/image_cmd.c | 45 +++++++++++---------------------- src/include/ipxe/command.h | 2 ++ 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/core/exec.c b/src/core/exec.c index e6f204879..df722304b 100644 --- a/src/core/exec.c +++ b/src/core/exec.c @@ -267,6 +267,42 @@ int system ( const char *command ) { return rc; } +/** + * Concatenate arguments + * + * @v args Argument list (NULL-terminated) + * @ret string Concatenated arguments + * + * The returned string is allocated with malloc(). The caller is + * responsible for eventually free()ing this string. + */ +char * concat_args ( char **args ) { + char **arg; + size_t len; + char *string; + char *ptr; + + /* Calculate total string length */ + len = 1 /* NUL */; + for ( arg = args ; *arg ; arg++ ) + len += ( 1 /* possible space */ + strlen ( *arg ) ); + + /* Allocate string */ + string = zalloc ( len ); + if ( ! string ) + return NULL; + + /* Populate string */ + ptr = string; + for ( arg = args ; *arg ; arg++ ) { + ptr += sprintf ( ptr, "%s%s", + ( ( ptr == string ) ? "" : " " ), *arg ); + } + assert ( ptr < ( string + len ) ); + + return string; +} + /** * "echo" command * @@ -274,13 +310,14 @@ int system ( const char *command ) { * @v argv Argument list * @ret rc Return status code */ -static int echo_exec ( int argc, char **argv ) { - int i; +static int echo_exec ( int argc __unused, char **argv ) { + char *text; - for ( i = 1 ; i < argc ; i++ ) { - printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] ); - } - printf ( "\n" ); + text = concat_args ( &argv[1] ); + if ( ! text ) + return -ENOMEM; + printf ( "%s\n", text ); + free ( text ); return 0; } diff --git a/src/hci/commands/image_cmd.c b/src/hci/commands/image_cmd.c index 7001087b6..2a90b7cbe 100644 --- a/src/hci/commands/image_cmd.c +++ b/src/hci/commands/image_cmd.c @@ -39,37 +39,24 @@ FILE_LICENCE ( GPL2_OR_LATER ); * Fill in image command line * * @v image Image - * @v nargs Argument count * @v args Argument list * @ret rc Return status code */ -static int imgfill_cmdline ( struct image *image, unsigned int nargs, - char **args ) { - size_t len = 0; - unsigned int i; +static int imgfill_cmdline ( struct image *image, char **args ) { + char *cmdline = NULL; + int rc; - /* Clear command line if no arguments given */ - if ( ! nargs ) - return image_set_cmdline ( image, NULL ); - - /* Determine total length of command line */ - for ( i = 0 ; i < nargs ; i++ ) - len += ( strlen ( args[i] ) + 1 /* space or NUL */ ); - - { - char buf[len]; - char *ptr = buf; - - /* Assemble command line */ - buf[0] = '\0'; - for ( i = 0 ; i < nargs ; i++ ) { - ptr += sprintf ( ptr, "%s%s", ( i ? " " : "" ), - args[i] ); - } - assert ( ptr < ( buf + len ) ); - - return image_set_cmdline ( image, buf ); + /* Construct command line (if arguments are present) */ + if ( *args ) { + cmdline = concat_args ( args ); + if ( ! cmdline ) + return -ENOMEM; } + + /* Apply command line */ + rc = image_set_cmdline ( image, cmdline ); + free ( cmdline ); + return rc; } /** "imgfetch" options */ @@ -127,8 +114,7 @@ static int imgfetch_core_exec ( int argc, char **argv, return rc; /* Fill in command line */ - if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ), - &argv[ optind + 1 ] ) ) != 0 ) + if ( ( rc = imgfill_cmdline ( image, &argv[ optind + 1 ] ) ) != 0 ) return rc; /* Fetch the image */ @@ -255,8 +241,7 @@ static int imgargs_exec ( int argc, char **argv ) { return rc; /* Fill in command line */ - if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ), - &argv[ optind + 1 ] ) ) != 0 ) + if ( ( rc = imgfill_cmdline ( image, &argv[ optind + 1 ] ) ) != 0 ) return rc; return 0; diff --git a/src/include/ipxe/command.h b/src/include/ipxe/command.h index a7d0ae462..432da1abb 100644 --- a/src/include/ipxe/command.h +++ b/src/include/ipxe/command.h @@ -23,4 +23,6 @@ struct command { #define __command __table_entry ( COMMANDS, 01 ) +extern char * concat_args ( char **args ); + #endif /* _IPXE_COMMAND_H */