From 1cc991e132bcd98f8e93dcc138e5a74d5a0402df Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 27 Mar 2011 13:23:18 +0100 Subject: [settings] Add "read" command The "read" command allows a script to prompt a user to enter a setting. For example: echo -n Static IP address: read net0/ip Total cost: 17 bytes. Signed-off-by: Michael Brown --- src/hci/commands/nvo_cmd.c | 127 +++++++++++++++++++++++++++++++-------------- 1 file changed, 88 insertions(+), 39 deletions(-) diff --git a/src/hci/commands/nvo_cmd.c b/src/hci/commands/nvo_cmd.c index 88d41270..a7e0f4a0 100644 --- a/src/hci/commands/nvo_cmd.c +++ b/src/hci/commands/nvo_cmd.c @@ -25,6 +25,7 @@ #include #include #include +#include FILE_LICENCE ( GPL2_OR_LATER ); @@ -80,48 +81,54 @@ static int show_exec ( int argc, char **argv ) { return 0; } -/** "set" options */ -struct set_options {}; +/** "set", "clear", and "read" options */ +struct set_core_options {}; -/** "set" option list */ -static struct option_descriptor set_opts[] = {}; +/** "set", "clear", and "read" option list */ +static struct option_descriptor set_core_opts[] = {}; /** "set" command descriptor */ static struct command_descriptor set_cmd = - COMMAND_DESC ( struct set_options, set_opts, 1, MAX_ARGUMENTS, + COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS, " " ); +/** "clear" and "read" command descriptor */ +static struct command_descriptor clear_read_cmd = + COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1, + "" ); + /** - * "set" command + * "set", "clear", and "read" command * * @v argc Argument count * @v argv Argument list + * @v cmd Command descriptor + * @v get_value Method to obtain setting value * @ret rc Return status code */ -static int set_exec ( int argc, char **argv ) { - struct set_options opts; +static int set_core_exec ( int argc, char **argv, + struct command_descriptor *cmd, + int ( * get_value ) ( char **args, char **value ) ) { + struct set_core_options opts; const char *name; char *value; int rc; /* Parse options */ - if ( ( rc = parse_options ( argc, argv, &set_cmd, &opts ) ) != 0 ) + if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 ) goto err_parse_options; /* Parse setting name */ name = argv[optind]; /* Parse setting value */ - value = concat_args ( &argv[ optind + 1 ] ); - if ( ! value ) { - rc = -ENOMEM; - goto err_concat_args; - } + if ( ( rc = get_value ( &argv[ optind + 1 ], &value ) ) != 0 ) + goto err_get_value; /* Determine total length of command line */ if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) { - printf ( "Could not set \"%s\"=\"%s\": %s\n", - name, value, strerror ( rc ) ); + printf ( "Could not %s \"%s\": %s\n", + argv[0], name, strerror ( rc ) ); goto err_store; } @@ -130,20 +137,50 @@ static int set_exec ( int argc, char **argv ) { err_store: free ( value ); - err_concat_args: + err_get_value: err_parse_options: return rc; } -/** "clear" options */ -struct clear_options {}; +/** + * Get setting value for "set" command + * + * @v args Remaining arguments + * @ret value Setting value + * @ret rc Return status code + */ +static int set_value ( char **args, char **value ) { + + *value = concat_args ( args ); + if ( ! *value ) + return -ENOMEM; + + return 0; +} + +/** + * "set" command + * + * @v argc Argument count + * @v argv Argument list + * @ret rc Return status code + */ +static int set_exec ( int argc, char **argv ) { + return set_core_exec ( argc, argv, &set_cmd, set_value ); +} -/** "clear" option list */ -static struct option_descriptor clear_opts[] = {}; +/** + * Get setting value for "clear" command + * + * @v args Remaining arguments + * @ret value Setting value + * @ret rc Return status code + */ +static int clear_value ( char **args __unused, char **value ) { -/** "clear" command descriptor */ -static struct command_descriptor clear_cmd = - COMMAND_DESC ( struct clear_options, clear_opts, 1, 1, "" ); + *value = NULL; + return 0; +} /** * "clear" command @@ -153,27 +190,35 @@ static struct command_descriptor clear_cmd = * @ret rc Return status code */ static int clear_exec ( int argc, char **argv ) { - struct clear_options opts; - const char *name; - int rc; + return set_core_exec ( argc, argv, &clear_read_cmd, clear_value ); +} - /* Parse options */ - if ( ( rc = parse_options ( argc, argv, &clear_cmd, &opts ) ) != 0 ) - return rc; +/** + * Get setting value for "read" command + * + * @ret value Setting value + * @ret rc Return status code + */ +static int read_value ( char **args __unused, char **value ) { - /* Parse setting name */ - name = argv[optind]; + *value = readline ( NULL ); + if ( ! *value ) + return -ENOMEM; - /* Clear setting */ - if ( ( rc = delete_named_setting ( name ) ) != 0 ) { - printf ( "Could not clear \"%s\": %s\n", - name, strerror ( rc ) ); - return rc; - } - return 0; } +/** + * "read" command + * + * @v argc Argument count + * @v argv Argument list + * @ret rc Return status code + */ +static int read_exec ( int argc, char **argv ) { + return set_core_exec ( argc, argv, &clear_read_cmd, read_value ); +} + /** Non-volatile option commands */ struct command nvo_commands[] __command = { { @@ -188,4 +233,8 @@ struct command nvo_commands[] __command = { .name = "clear", .exec = clear_exec, }, + { + .name = "read", + .exec = read_exec, + }, }; -- cgit v1.2.3-55-g7522