瀏覽代碼

[cmdline] Add generic concat_args() function

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 年之前
父節點
當前提交
a281c4080b
共有 3 個文件被更改,包括 60 次插入36 次删除
  1. 43
    6
      src/core/exec.c
  2. 15
    30
      src/hci/commands/image_cmd.c
  3. 2
    0
      src/include/ipxe/command.h

+ 43
- 6
src/core/exec.c 查看文件

@@ -267,6 +267,42 @@ int system ( const char *command ) {
267 267
 	return rc;
268 268
 }
269 269
 
270
+/**
271
+ * Concatenate arguments
272
+ *
273
+ * @v args		Argument list (NULL-terminated)
274
+ * @ret string		Concatenated arguments
275
+ *
276
+ * The returned string is allocated with malloc().  The caller is
277
+ * responsible for eventually free()ing this string.
278
+ */
279
+char * concat_args ( char **args ) {
280
+	char **arg;
281
+	size_t len;
282
+	char *string;
283
+	char *ptr;
284
+
285
+	/* Calculate total string length */
286
+	len = 1 /* NUL */;
287
+	for ( arg = args ; *arg ; arg++ )
288
+		len += ( 1 /* possible space */ + strlen ( *arg ) );
289
+
290
+	/* Allocate string */
291
+	string = zalloc ( len );
292
+	if ( ! string )
293
+		return NULL;
294
+
295
+	/* Populate string */
296
+	ptr = string;
297
+	for ( arg = args ; *arg ; arg++ ) {
298
+		ptr += sprintf ( ptr, "%s%s",
299
+				 ( ( ptr == string ) ? "" : " " ), *arg );
300
+	}
301
+	assert ( ptr < ( string + len ) );
302
+
303
+	return string;
304
+}
305
+
270 306
 /**
271 307
  * "echo" command
272 308
  *
@@ -274,13 +310,14 @@ int system ( const char *command ) {
274 310
  * @v argv		Argument list
275 311
  * @ret rc		Return status code
276 312
  */
277
-static int echo_exec ( int argc, char **argv ) {
278
-	int i;
313
+static int echo_exec ( int argc __unused, char **argv ) {
314
+	char *text;
279 315
 
280
-	for ( i = 1 ; i < argc ; i++ ) {
281
-		printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] );
282
-	}
283
-	printf ( "\n" );
316
+	text = concat_args ( &argv[1] );
317
+	if ( ! text )
318
+		return -ENOMEM;
319
+	printf ( "%s\n", text );
320
+	free ( text );
284 321
 	return 0;
285 322
 }
286 323
 

+ 15
- 30
src/hci/commands/image_cmd.c 查看文件

@@ -39,37 +39,24 @@ FILE_LICENCE ( GPL2_OR_LATER );
39 39
  * Fill in image command line
40 40
  *
41 41
  * @v image		Image
42
- * @v nargs		Argument count
43 42
  * @v args		Argument list
44 43
  * @ret rc		Return status code
45 44
  */
46
-static int imgfill_cmdline ( struct image *image, unsigned int nargs, 
47
-			     char **args ) {
48
-	size_t len = 0;
49
-	unsigned int i;
50
-
51
-	/* Clear command line if no arguments given */
52
-	if ( ! nargs )
53
-		return image_set_cmdline ( image, NULL );
54
-
55
-	/* Determine total length of command line */
56
-	for ( i = 0 ; i < nargs ; i++ )
57
-		len += ( strlen ( args[i] ) + 1 /* space or NUL */ );
58
-
59
-	{
60
-		char buf[len];
61
-		char *ptr = buf;
62
-
63
-		/* Assemble command line */
64
-		buf[0] = '\0';
65
-		for ( i = 0 ; i < nargs ; i++ ) {
66
-			ptr += sprintf ( ptr, "%s%s", ( i ? " " : "" ),
67
-					 args[i] );
68
-		}
69
-		assert ( ptr < ( buf + len ) );
45
+static int imgfill_cmdline ( struct image *image, char **args ) {
46
+	char *cmdline = NULL;
47
+	int rc;
70 48
 
71
-		return image_set_cmdline ( image, buf );
49
+	/* Construct command line (if arguments are present) */
50
+	if ( *args ) {
51
+		cmdline = concat_args ( args );
52
+		if ( ! cmdline )
53
+			return -ENOMEM;
72 54
 	}
55
+
56
+	/* Apply command line */
57
+	rc = image_set_cmdline ( image, cmdline );
58
+	free ( cmdline );
59
+	return rc;
73 60
 }
74 61
 
75 62
 /** "imgfetch" options */
@@ -127,8 +114,7 @@ static int imgfetch_core_exec ( int argc, char **argv,
127 114
 		return rc;
128 115
 
129 116
 	/* Fill in command line */
130
-	if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ),
131
-				      &argv[ optind + 1 ] ) ) != 0 )
117
+	if ( ( rc = imgfill_cmdline ( image, &argv[ optind + 1 ] ) ) != 0 )
132 118
 		return rc;
133 119
 
134 120
 	/* Fetch the image */
@@ -255,8 +241,7 @@ static int imgargs_exec ( int argc, char **argv ) {
255 241
 		return rc;
256 242
 
257 243
 	/* Fill in command line */
258
-	if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ),
259
-				      &argv[ optind + 1 ] ) ) != 0 )
244
+	if ( ( rc = imgfill_cmdline ( image, &argv[ optind + 1 ] ) ) != 0 )
260 245
 		return rc;
261 246
 
262 247
 	return 0;

+ 2
- 0
src/include/ipxe/command.h 查看文件

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

Loading…
取消
儲存