]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[cmdline] Add "inc" command
authorMichael Brown <mcb30@ipxe.org>
Thu, 1 Aug 2013 13:42:28 +0000 (14:42 +0100)
committerMichael Brown <mcb30@ipxe.org>
Thu, 1 Aug 2013 13:42:28 +0000 (14:42 +0100)
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 <mcb30@ipxe.org>
src/hci/commands/nvo_cmd.c

index dd54a84e111138615296352f82c9fcf637588442..3fd684d04016ad8eb6b9e98c4ed712f9f3ff251d 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <errno.h>
 #include <getopt.h>
+#include <byteswap.h>
 #include <ipxe/settings.h>
 #include <ipxe/command.h>
 #include <ipxe/parseopt.h>
@@ -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,
+                      "<setting> [<increment>]" );
+
+/**
+ * "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,
+       },
 };