iPXE
Data Structures | Functions | Variables
nvo_cmd.c File Reference

Non-volatile option commands. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <byteswap.h>
#include <ipxe/settings.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <readline/readline.h>

Go to the source code of this file.

Data Structures

struct  show_options
 "show" options More...
struct  set_core_options
 "set", "clear", and "read" options More...
struct  inc_options
 "inc" options More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int show_exec (int argc, char **argv)
 "show" command
static int set_core_exec (int argc, char **argv, struct command_descriptor *cmd, int(*get_value)(struct named_setting *setting, char **args, char **value))
 "set", "clear", and "read" command
static int set_value (struct named_setting *setting __unused, char **args, char **value)
 Get setting value for "set" command.
static int set_exec (int argc, char **argv)
 "set" command
static int clear_value (struct named_setting *setting __unused, char **args __unused, char **value)
 Get setting value for "clear" command.
static int clear_exec (int argc, char **argv)
 "clear" command
static int read_value (struct named_setting *setting, char **args __unused, char **value)
 Get setting value for "read" command.
static int read_exec (int argc, char **argv)
 "read" command
static int inc_exec (int argc, char **argv)
 "inc" command

Variables

static struct option_descriptor show_opts [] = {}
 "show" option list
static struct command_descriptor show_cmd
 "show" command descriptor
static struct option_descriptor set_core_opts [] = {}
 "set", "clear", and "read" option list
static struct command_descriptor set_cmd
 "set" command descriptor
static struct command_descriptor clear_read_cmd
 "clear" and "read" command descriptor
static struct option_descriptor inc_opts [] = {}
 "inc" option list
static struct command_descriptor inc_cmd
 "inc" command descriptor
struct command nvo_commands[] __command
 Non-volatile option commands.

Detailed Description

Non-volatile option commands.

Definition in file nvo_cmd.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int show_exec ( int  argc,
char **  argv 
) [static]

"show" command

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 61 of file nvo_cmd.c.

References fetchf_setting_copy(), free, len, setting::name, optind, origin, parse_existing_setting(), parse_options(), printf(), rc, named_setting::setting, setting_name(), named_setting::settings, strerror(), and value.

                                               {
        struct show_options opts;
        struct named_setting setting;
        struct settings *origin;
        struct setting fetched;
        char name_buf[32];
        char *value;
        int len;
        int rc;

        /* Parse options */
        if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
                goto err_parse_options;

        /* Parse setting name */
        if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
                goto err_parse_setting;

        /* Fetch formatted setting value */
        if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
                                           &origin, &fetched, &value ) ) < 0 ) {
                rc = len;
                printf ( "Could not find \"%s\": %s\n",
                         setting.setting.name, strerror ( rc ) );
                goto err_fetchf;
        }

        /* Print setting value */
        setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
        printf ( "%s = %s\n", name_buf, value );

        /* Success */
        rc = 0;

        free ( value );
 err_fetchf:
 err_parse_setting:
 err_parse_options:
        return rc;
}
static int set_core_exec ( int  argc,
char **  argv,
struct command_descriptor cmd,
int(*)(struct named_setting *setting, char **args, char **value get_value 
) [static]

"set", "clear", and "read" command

Parameters:
argcArgument count
argvArgument list
cmdCommand descriptor
get_valueMethod to obtain setting value
Return values:
rcReturn status code

Definition at line 127 of file nvo_cmd.c.

References free, setting::name, optind, parse_autovivified_setting(), parse_options(), printf(), rc, named_setting::setting, named_setting::settings, storef_setting(), strerror(), setting::type, and value.

Referenced by clear_exec(), read_exec(), and set_exec().

                                                                               {
        struct set_core_options opts;
        struct named_setting setting;
        char *value;
        int rc;

        /* Parse options */
        if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
                goto err_parse_options;

        /* Parse setting name */
        if ( ( rc = parse_autovivified_setting ( argv[optind],
                                                 &setting ) ) != 0 )
                goto err_parse_setting;

        /* Parse setting value */
        if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
                goto err_get_value;

        /* Apply default type if necessary */
        if ( ! setting.setting.type )
                setting.setting.type = &setting_type_string;

        /* Store setting */
        if ( ( rc = storef_setting ( setting.settings, &setting.setting,
                                     value ) ) != 0 ) {
                printf ( "Could not store \"%s\": %s\n",
                         setting.setting.name, strerror ( rc ) );
                goto err_store;
        }

 err_store:
        free ( value );
 err_get_value:
 err_parse_setting:
 err_parse_options:
        return rc;
}
static int set_value ( struct named_setting *setting  __unused,
char **  args,
char **  value 
) [static]

Get setting value for "set" command.

Parameters:
settingNamed setting
argsRemaining arguments
Return values:
valueSetting value
rcReturn status code

Definition at line 177 of file nvo_cmd.c.

References concat_args(), and ENOMEM.

Referenced by set_exec().

                                                   {

        *value = concat_args ( args );
        if ( ! *value )
                return -ENOMEM;

        return 0;
}
static int set_exec ( int  argc,
char **  argv 
) [static]

"set" command

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 194 of file nvo_cmd.c.

References set_core_exec(), and set_value().

                                              {
        return set_core_exec ( argc, argv, &set_cmd, set_value );
}
static int clear_value ( struct named_setting *setting  __unused,
char **args  __unused,
char **  value 
) [static]

Get setting value for "clear" command.

Parameters:
settingNamed setting
argsRemaining arguments
Return values:
valueSetting value
rcReturn status code

Definition at line 206 of file nvo_cmd.c.

References NULL.

Referenced by clear_exec().

                                                              {

        *value = NULL;
        return 0;
}
static int clear_exec ( int  argc,
char **  argv 
) [static]

"clear" command

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 220 of file nvo_cmd.c.

References clear_value(), and set_core_exec().

                                                {
        return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
}
static int read_value ( struct named_setting setting,
char **args  __unused,
char **  value 
) [static]

Get setting value for "read" command.

Parameters:
settingNamed setting
argsRemaining arguments
Return values:
valueSetting value
rcReturn status code

Definition at line 232 of file nvo_cmd.c.

References fetchf_setting_copy(), free, NULL, rc, named_setting::setting, and named_setting::settings.

Referenced by read_exec().

                                       {
        char *existing;
        int rc;

        /* Read existing value, treating errors as equivalent to an
         * empty initial setting.
         */
        fetchf_setting_copy ( setting->settings, &setting->setting,
                              NULL, &setting->setting, &existing );

        /* Read new value */
        if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
                goto err_readline;

 err_readline:
        free ( existing );
        return rc;
}
static int read_exec ( int  argc,
char **  argv 
) [static]

"read" command

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 259 of file nvo_cmd.c.

References read_value(), and set_core_exec().

                                               {
        return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
}
static int inc_exec ( int  argc,
char **  argv 
) [static]

"inc" command

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 281 of file nvo_cmd.c.

References fetchn_setting(), setting::name, NULL, optind, parse_existing_setting(), parse_integer(), parse_options(), printf(), rc, named_setting::setting, named_setting::settings, storen_setting(), strerror(), setting::type, and value.

                                              {
        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;

        /* Read existing value, treating errors as equivalent to a
         * zero-valued :int32 initial setting.
         */
        if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
                                     NULL, &setting.setting, &value ) ) != 0 ) {
                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;
}

Variable Documentation

struct option_descriptor show_opts[] = {} [static]

"show" option list

Definition at line 48 of file nvo_cmd.c.

struct command_descriptor show_cmd [static]
Initial value:
        COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" )

"show" command descriptor

Definition at line 51 of file nvo_cmd.c.

struct option_descriptor set_core_opts[] = {} [static]

"set", "clear", and "read" option list

Definition at line 106 of file nvo_cmd.c.

struct command_descriptor set_cmd [static]
Initial value:
        COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
                       "<setting> <value>" )

"set" command descriptor

Definition at line 109 of file nvo_cmd.c.

Initial value:
        COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
                       "<setting>" )

"clear" and "read" command descriptor

Definition at line 114 of file nvo_cmd.c.

struct option_descriptor inc_opts[] = {} [static]

"inc" option list

Definition at line 267 of file nvo_cmd.c.

struct command_descriptor inc_cmd [static]
Initial value:
        COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
                       "<setting> [<increment>]" )

"inc" command descriptor

Definition at line 270 of file nvo_cmd.c.

struct command nvo_commands [] __command
Initial value:
 {
        {
                .name = "show",
                .exec = show_exec,
        },
        {
                .name = "set",
                .exec = set_exec,
        },      
        {
                .name = "clear",
                .exec = clear_exec,
        },
        {
                .name = "read",
                .exec = read_exec,
        },
        {
                .name = "inc",
                .exec = inc_exec,
        },
}

Non-volatile option commands.

Definition at line 330 of file nvo_cmd.c.