[cmdline] Add generic concat_args() function

Signed-off-by: Michael Brown <mcb30@ipxe.org>
pull/1/head
Michael Brown 2011-03-07 18:31:38 +00:00
parent 9e162121b1
commit a281c4080b
3 changed files with 60 additions and 36 deletions

View File

@ -267,6 +267,42 @@ int system ( const char *command ) {
return rc; 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 * "echo" command
* *
@ -274,13 +310,14 @@ int system ( const char *command ) {
* @v argv Argument list * @v argv Argument list
* @ret rc Return status code * @ret rc Return status code
*/ */
static int echo_exec ( int argc, char **argv ) { static int echo_exec ( int argc __unused, char **argv ) {
int i; char *text;
for ( i = 1 ; i < argc ; i++ ) { text = concat_args ( &argv[1] );
printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] ); if ( ! text )
} return -ENOMEM;
printf ( "\n" ); printf ( "%s\n", text );
free ( text );
return 0; return 0;
} }

View File

@ -39,37 +39,24 @@ FILE_LICENCE ( GPL2_OR_LATER );
* Fill in image command line * Fill in image command line
* *
* @v image Image * @v image Image
* @v nargs Argument count
* @v args Argument list * @v args Argument list
* @ret rc Return status code * @ret rc Return status code
*/ */
static int imgfill_cmdline ( struct image *image, unsigned int nargs, static int imgfill_cmdline ( struct image *image, char **args ) {
char **args ) { char *cmdline = NULL;
size_t len = 0; int rc;
unsigned int i;
/* Clear command line if no arguments given */ /* Construct command line (if arguments are present) */
if ( ! nargs ) if ( *args ) {
return image_set_cmdline ( image, NULL ); cmdline = concat_args ( args );
if ( ! cmdline )
/* Determine total length of command line */ return -ENOMEM;
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 );
} }
/* Apply command line */
rc = image_set_cmdline ( image, cmdline );
free ( cmdline );
return rc;
} }
/** "imgfetch" options */ /** "imgfetch" options */
@ -127,8 +114,7 @@ static int imgfetch_core_exec ( int argc, char **argv,
return rc; return rc;
/* Fill in command line */ /* Fill in command line */
if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ), if ( ( rc = imgfill_cmdline ( image, &argv[ optind + 1 ] ) ) != 0 )
&argv[ optind + 1 ] ) ) != 0 )
return rc; return rc;
/* Fetch the image */ /* Fetch the image */
@ -255,8 +241,7 @@ static int imgargs_exec ( int argc, char **argv ) {
return rc; return rc;
/* Fill in command line */ /* Fill in command line */
if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ), if ( ( rc = imgfill_cmdline ( image, &argv[ optind + 1 ] ) ) != 0 )
&argv[ optind + 1 ] ) ) != 0 )
return rc; return rc;
return 0; return 0;

View File

@ -23,4 +23,6 @@ struct command {
#define __command __table_entry ( COMMANDS, 01 ) #define __command __table_entry ( COMMANDS, 01 )
extern char * concat_args ( char **args );
#endif /* _IPXE_COMMAND_H */ #endif /* _IPXE_COMMAND_H */