From 2b869786c57623715618a07c2bcaba83eb76d8c4 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 1 Aug 2013 14:42:28 +0100 Subject: [PATCH] [cmdline] Add "inc" command The "inc" command allows the numeric value of a setting to be incremented, allowing for the construction of simple loops within an iPXE script. Signed-off-by: Michael Brown --- src/hci/commands/nvo_cmd.c | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/hci/commands/nvo_cmd.c b/src/hci/commands/nvo_cmd.c index dd54a84e..3fd684d0 100644 --- a/src/hci/commands/nvo_cmd.c +++ b/src/hci/commands/nvo_cmd.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -255,6 +256,73 @@ static int read_exec ( int argc, char **argv ) { return set_core_exec ( argc, argv, &clear_read_cmd, read_value ); } +/** "inc" options */ +struct inc_options {}; + +/** "inc" option list */ +static struct option_descriptor inc_opts[] = {}; + +/** "inc" command descriptor */ +static struct command_descriptor inc_cmd = + COMMAND_DESC ( struct inc_options, inc_opts, 1, 2, + " []" ); + +/** + * "inc" command + * + * @v argc Argument count + * @v argv Argument list + * @ret rc Return status code + */ +static int inc_exec ( int argc, char **argv ) { + struct inc_options opts; + struct named_setting setting; + unsigned int increment = 1; + unsigned long value; + int rc; + + /* Parse options */ + if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 ) + goto err_parse_options; + + /* Parse setting name */ + if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 ) + goto err_parse_setting; + + /* Parse increment (if present) */ + if ( ( ( optind + 1 ) < argc ) && + ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0)) + goto err_parse_increment; + + /* Fetch existing setting value, if any, allowing for the fact + * that numeric settings are big-endian and variable-length. + */ + if ( ( rc = fetchn_setting ( setting.settings, &setting.setting, + &value ) ) != 0 ) { + /* Treat as a non-existent :int32 setting with a zero value */ + value = 0; + if ( ! setting.setting.type ) + setting.setting.type = &setting_type_int32; + } + + /* Increment value */ + value += increment; + + /* Store updated setting value */ + if ( ( rc = storen_setting ( setting.settings, &setting.setting, + value ) ) != 0 ) { + printf ( "Could not store \"%s\": %s\n", + setting.setting.name, strerror ( rc ) ); + goto err_store; + } + + err_store: + err_parse_increment: + err_parse_setting: + err_parse_options: + return rc; +} + /** Non-volatile option commands */ struct command nvo_commands[] __command = { { @@ -273,4 +341,8 @@ struct command nvo_commands[] __command = { .name = "read", .exec = read_exec, }, + { + .name = "inc", + .exec = inc_exec, + }, };