summaryrefslogtreecommitdiffstats
path: root/src/core/exec.c
diff options
context:
space:
mode:
authorMichael Brown2011-03-27 13:49:18 +0200
committerMichael Brown2011-03-27 13:49:18 +0200
commitac12324f526af1290abc43ced5ca468a2e18a42d (patch)
tree0ca49b5b86345bfd8eb2026296273f9c5113a994 /src/core/exec.c
parent[parseopt] Add support for boolean options (diff)
downloadipxe-ac12324f526af1290abc43ced5ca468a2e18a42d.tar.gz
ipxe-ac12324f526af1290abc43ced5ca468a2e18a42d.tar.xz
ipxe-ac12324f526af1290abc43ced5ca468a2e18a42d.zip
[cmdline] Allow "echo -n" to inhibit trailing newline
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/exec.c')
-rw-r--r--src/core/exec.c33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/core/exec.c b/src/core/exec.c
index df722304..7ce38443 100644
--- a/src/core/exec.c
+++ b/src/core/exec.c
@@ -303,6 +303,23 @@ char * concat_args ( char **args ) {
return string;
}
+/** "echo" options */
+struct echo_options {
+ /** Do not print trailing newline */
+ int no_newline;
+};
+
+/** "echo" option list */
+static struct option_descriptor echo_opts[] = {
+ OPTION_DESC ( "n", 'n', no_argument,
+ struct echo_options, no_newline, parse_flag ),
+};
+
+/** "echo" command descriptor */
+static struct command_descriptor echo_cmd =
+ COMMAND_DESC ( struct echo_options, echo_opts, 0, MAX_ARGUMENTS,
+ "[-n] [...]" );
+
/**
* "echo" command
*
@@ -310,13 +327,23 @@ char * concat_args ( char **args ) {
* @v argv Argument list
* @ret rc Return status code
*/
-static int echo_exec ( int argc __unused, char **argv ) {
+static int echo_exec ( int argc, char **argv ) {
+ struct echo_options opts;
char *text;
+ int rc;
- text = concat_args ( &argv[1] );
+ /* Parse options */
+ if ( ( rc = parse_options ( argc, argv, &echo_cmd, &opts ) ) != 0 )
+ return rc;
+
+ /* Parse text */
+ text = concat_args ( &argv[optind] );
if ( ! text )
return -ENOMEM;
- printf ( "%s\n", text );
+
+ /* Print text */
+ printf ( "%s%s", text, ( opts.no_newline ? "" : "\n" ) );
+
free ( text );
return 0;
}