]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[cmdline] Add generic concat_args() function
authorMichael Brown <mcb30@ipxe.org>
Mon, 7 Mar 2011 18:31:38 +0000 (18:31 +0000)
committerMichael Brown <mcb30@ipxe.org>
Mon, 7 Mar 2011 19:21:43 +0000 (19:21 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/exec.c
src/hci/commands/image_cmd.c
src/include/ipxe/command.h

index e6f204879b5c9aa42850b8780514bd9eb03043d1..df722304b3aaaa33bcdeedcb7592bd6972f94c83 100644 (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;
 }
 
index 7001087b6ec65f618f117b4588d831e3199c1947..2a90b7cbe73b40dc2dac17e8b85648cd5d9722eb 100644 (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;
-
-       /* 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;
index a7d0ae4623af8f3bf77d7daba6e5040982776cfe..432da1abb6ad66cd8b4683cd4e5896798cde8c94 100644 (file)
@@ -23,4 +23,6 @@ struct command {
 
 #define __command __table_entry ( COMMANDS, 01 )
 
+extern char * concat_args ( char **args );
+
 #endif /* _IPXE_COMMAND_H */