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

Menu commands. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/menu.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <ipxe/settings.h>
#include <ipxe/features.h>

Go to the source code of this file.

Data Structures

struct  menu_options
 "menu" options More...
struct  item_options
 "item" options More...
struct  choose_options
 "choose" options More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FEATURE (FEATURE_MISC,"Menu", DHCP_EB_FEATURE_MENU, 1)
static int menu_exec (int argc, char **argv)
 The "menu" command.
static int item_exec (int argc, char **argv)
 The "item" command.
static int choose_exec (int argc, char **argv)
 The "choose" command.

Variables

static struct option_descriptor menu_opts []
 "menu" option list
static struct command_descriptor menu_cmd
 "menu" command descriptor
static struct option_descriptor item_opts []
 "item" option list
static struct command_descriptor item_cmd
 "item" command descriptor
static struct option_descriptor choose_opts []
 "choose" option list
static struct command_descriptor choose_cmd
 "choose" command descriptor
struct command menu_commands[] __command
 Menu commands.

Detailed Description

Menu commands.

Definition in file menu_cmd.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
FEATURE ( FEATURE_MISC  ,
"Menu"  ,
DHCP_EB_FEATURE_MENU  ,
 
)
static int menu_exec ( int  argc,
char **  argv 
) [static]

The "menu" command.

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

Definition at line 73 of file menu_cmd.c.

References concat_args(), create_menu(), menu_options::delete, destroy_menu(), ENOMEM, free, menu_options::name, optind, parse_options(), rc, and menu::title.

                                               {
        struct menu_options opts;
        struct menu *menu;
        char *title;
        int rc;

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

        /* Parse title */
        title = concat_args ( &argv[optind] );
        if ( ! title ) {
                rc = -ENOMEM;
                goto err_parse_title;
        }

        /* Create menu */
        menu = create_menu ( opts.name, title );
        if ( ! menu ) {
                rc = -ENOMEM;
                goto err_create_menu;
        }

        /* Destroy menu, if applicable */
        if ( opts.delete )
                destroy_menu ( menu );

        /* Success */
        rc = 0;

 err_create_menu:
        free ( title );
 err_parse_title:
 err_parse_options:
        return rc;
}
static int item_exec ( int  argc,
char **  argv 
) [static]

The "item" command.

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

Definition at line 147 of file menu_cmd.c.

References add_menu_item(), concat_args(), ENOMEM, free, item_options::is_default, item_options::is_gap, item_options::key, menu_item::label, item_options::menu, NULL, optind, parse_menu(), parse_options(), rc, and menu_item::text.

                                               {
        struct item_options opts;
        struct menu *menu;
        struct menu_item *item;
        char *label = NULL;
        char *text = NULL;
        int rc;

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

        /* Parse label, if present */
        if ( ! opts.is_gap )
                label = argv[optind++]; /* May be NULL */

        /* Parse text, if present */
        if ( optind < argc ) {
                text = concat_args ( &argv[optind] );
                if ( ! text ) {
                        rc = -ENOMEM;
                        goto err_parse_text;
                }
        }

        /* Identify menu */
        if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
                goto err_parse_menu;

        /* Add menu item */
        item = add_menu_item ( menu, label, ( text ? text : "" ),
                               opts.key, opts.is_default );
        if ( ! item ) {
                rc = -ENOMEM;
                goto err_add_menu_item;
        }

        /* Success */
        rc = 0;

 err_add_menu_item:
 err_parse_menu:
        free ( text );
 err_parse_text:
 err_parse_options:
        return rc;
}
static int choose_exec ( int  argc,
char **  argv 
) [static]

The "choose" command.

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

Definition at line 230 of file menu_cmd.c.

References destroy_menu(), choose_options::keep, menu_item::label, choose_options::menu, setting::name, optind, parse_autovivified_setting(), parse_menu(), parse_options(), printf(), rc, choose_options::select, named_setting::setting, named_setting::settings, show_menu(), storef_setting(), strerror(), choose_options::timeout, and setting::type.

                                                 {
        struct choose_options opts;
        struct named_setting setting;
        struct menu *menu;
        struct menu_item *item;
        int rc;

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

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

        /* Identify menu */
        if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
                goto err_parse_menu;

        /* Show menu */
        if ( ( rc = show_menu ( menu, opts.timeout, opts.select, &item ) ) != 0)
                goto err_show_menu;

        /* 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,
                                     item->label ) ) != 0 ) {
                printf ( "Could not store \"%s\": %s\n",
                         setting.setting.name, strerror ( rc ) );
                goto err_store;
        }

        /* Success */
        rc = 0;

 err_store:
 err_show_menu:
        /* Destroy menu, if applicable */
        if ( ! opts.keep )
                destroy_menu ( menu );
 err_parse_menu:
 err_parse_setting:
 err_parse_options:
        return rc;
}

Variable Documentation

struct option_descriptor menu_opts[] [static]
Initial value:
 {
        OPTION_DESC ( "name", 'n', required_argument,
                      struct menu_options, name, parse_string ),
        OPTION_DESC ( "delete", 'd', no_argument,
                      struct menu_options, delete, parse_flag ),
}

"menu" option list

Definition at line 54 of file menu_cmd.c.

struct command_descriptor menu_cmd [static]
Initial value:
        COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
                       "[<title>]" )

"menu" command descriptor

Definition at line 62 of file menu_cmd.c.

struct option_descriptor item_opts[] [static]
Initial value:
 {
        OPTION_DESC ( "menu", 'm', required_argument,
                      struct item_options, menu, parse_string ),
        OPTION_DESC ( "key", 'k', required_argument,
                      struct item_options, key, parse_key ),
        OPTION_DESC ( "default", 'd', no_argument,
                      struct item_options, is_default, parse_flag ),
        OPTION_DESC ( "gap", 'g', no_argument,
                      struct item_options, is_gap, parse_flag ),
}

"item" option list

Definition at line 124 of file menu_cmd.c.

struct command_descriptor item_cmd [static]
Initial value:
        COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
                       "[<label> [<text>]]" )

"item" command descriptor

Definition at line 136 of file menu_cmd.c.

struct option_descriptor choose_opts[] [static]
Initial value:

"choose" option list

Definition at line 208 of file menu_cmd.c.

struct command_descriptor choose_cmd [static]
Initial value:
        COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" )

"choose" command descriptor

Definition at line 220 of file menu_cmd.c.

struct command menu_commands [] __command
Initial value:
 {
        {
                .name = "menu",
                .exec = menu_exec,
        },
        {
                .name = "item",
                .exec = item_exec,
        },
        {
                .name = "choose",
                .exec = choose_exec,
        },
}

Menu commands.

Definition at line 281 of file menu_cmd.c.