diff options
| author | Michael Brown | 2011-03-07 19:31:38 +0100 |
|---|---|---|
| committer | Michael Brown | 2011-03-07 20:21:43 +0100 |
| commit | a281c4080bf268fa08d0b6001ab081c53a225035 (patch) | |
| tree | abc49ad178090fd248a27d79ceba40139d032b78 /src | |
| parent | [script] Add an iPXE error URI to the "not in a script" message (diff) | |
| download | ipxe-a281c4080bf268fa08d0b6001ab081c53a225035.tar.gz ipxe-a281c4080bf268fa08d0b6001ab081c53a225035.tar.xz ipxe-a281c4080bf268fa08d0b6001ab081c53a225035.zip | |
[cmdline] Add generic concat_args() function
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/exec.c | 49 | ||||
| -rw-r--r-- | src/hci/commands/image_cmd.c | 45 | ||||
| -rw-r--r-- | 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 @@ -268,19 +268,56 @@ int system ( const char *command ) { } /** + * 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 * * @v argc Argument count * @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; - - /* 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 ) ); +static int imgfill_cmdline ( struct image *image, char **args ) { + char *cmdline = NULL; + int rc; - 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 */ |
