Bladeren bron

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

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 jaren geleden
bovenliggende
commit
ac12324f52
1 gewijzigde bestanden met toevoegingen van 30 en 3 verwijderingen
  1. 30
    3
      src/core/exec.c

+ 30
- 3
src/core/exec.c Bestand weergeven

303
 	return string;
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
  * "echo" command
324
  * "echo" command
308
  *
325
  *
310
  * @v argv		Argument list
327
  * @v argv		Argument list
311
  * @ret rc		Return status code
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
 	char *text;
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
 	if ( ! text )
341
 	if ( ! text )
318
 		return -ENOMEM;
342
 		return -ENOMEM;
319
-	printf ( "%s\n", text );
343
+
344
+	/* Print text */
345
+	printf ( "%s%s", text, ( opts.no_newline ? "" : "\n" ) );
346
+
320
 	free ( text );
347
 	free ( text );
321
 	return 0;
348
 	return 0;
322
 }
349
 }

Laden…
Annuleren
Opslaan