david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[cmdline] Add generic concat_args() function

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
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;
}
/**
* 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;
}

View File

@ -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;

View File

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