Browse Source

[cmdline] Allow "echo -n" to inhibit trailing newline

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 years ago
parent
commit
ac12324f52
1 changed files with 30 additions and 3 deletions
  1. 30
    3
      src/core/exec.c

+ 30
- 3
src/core/exec.c View File

@@ -303,6 +303,23 @@ char * concat_args ( char **args ) {
303 303
 	return string;
304 304
 }
305 305
 
306
+/** "echo" options */
307
+struct echo_options {
308
+	/** Do not print trailing newline */
309
+	int no_newline;
310
+};
311
+
312
+/** "echo" option list */
313
+static struct option_descriptor echo_opts[] = {
314
+	OPTION_DESC ( "n", 'n', no_argument,
315
+		      struct echo_options, no_newline, parse_flag ),
316
+};
317
+
318
+/** "echo" command descriptor */
319
+static struct command_descriptor echo_cmd =
320
+	COMMAND_DESC ( struct echo_options, echo_opts, 0, MAX_ARGUMENTS,
321
+		       "[-n] [...]" );
322
+
306 323
 /**
307 324
  * "echo" command
308 325
  *
@@ -310,13 +327,23 @@ char * concat_args ( char **args ) {
310 327
  * @v argv		Argument list
311 328
  * @ret rc		Return status code
312 329
  */
313
-static int echo_exec ( int argc __unused, char **argv ) {
330
+static int echo_exec ( int argc, char **argv ) {
331
+	struct echo_options opts;
314 332
 	char *text;
333
+	int rc;
315 334
 
316
-	text = concat_args ( &argv[1] );
335
+	/* Parse options */
336
+	if ( ( rc = parse_options ( argc, argv, &echo_cmd, &opts ) ) != 0 )
337
+		return rc;
338
+
339
+	/* Parse text */
340
+	text = concat_args ( &argv[optind] );
317 341
 	if ( ! text )
318 342
 		return -ENOMEM;
319
-	printf ( "%s\n", text );
343
+
344
+	/* Print text */
345
+	printf ( "%s%s", text, ( opts.no_newline ? "" : "\n" ) );
346
+
320 347
 	free ( text );
321 348
 	return 0;
322 349
 }

Loading…
Cancel
Save