Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Aug 1, 2013
1 parent c70d4cb commit 2b86978
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/hci/commands/nvo_cmd.c
Expand Up @@ -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>
Expand Down Expand Up @@ -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 = {
{
Expand All @@ -273,4 +341,8 @@ struct command nvo_commands[] __command = {
.name = "read",
.exec = read_exec,
},
{
.name = "inc",
.exec = inc_exec,
},
};

0 comments on commit 2b86978

Please sign in to comment.