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. More...
 
static int item_exec (int argc, char **argv)
 The "item" command. More...
 
static int choose_exec (int argc, char **argv)
 The "choose" command. More...
 

Variables

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

Detailed Description

Menu commands.

Definition in file menu_cmd.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FEATURE()

FEATURE ( FEATURE_MISC  ,
"Menu"  ,
DHCP_EB_FEATURE_MENU  ,
 
)

◆ menu_exec()

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.

73  {
74  struct menu_options opts;
75  struct menu *menu;
76  char *title;
77  int rc;
78 
79  /* Parse options */
80  if ( ( rc = parse_options ( argc, argv, &menu_cmd, &opts ) ) != 0 )
81  goto err_parse_options;
82 
83  /* Parse title */
84  title = concat_args ( &argv[optind] );
85  if ( ! title ) {
86  rc = -ENOMEM;
87  goto err_parse_title;
88  }
89 
90  /* Create menu */
91  menu = create_menu ( opts.name, title );
92  if ( ! menu ) {
93  rc = -ENOMEM;
94  goto err_create_menu;
95  }
96 
97  /* Destroy menu, if applicable */
98  if ( opts.delete )
99  destroy_menu ( menu );
100 
101  /* Success */
102  rc = 0;
103 
104  err_create_menu:
105  free ( title );
106  err_parse_title:
107  err_parse_options:
108  return rc;
109 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int optind
Current option index.
Definition: getopt.c:51
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
#define ENOMEM
Not enough space.
Definition: errno.h:534
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
const char * title
Title.
Definition: menu.h:21
static union @437 opts
"cert<xxx>" option list
"menu" options
Definition: menu_cmd.c:46
A menu.
Definition: menu.h:15

References concat_args(), create_menu(), destroy_menu(), ENOMEM, free, menu_cmd, optind, opts, parse_options(), rc, and menu::title.

◆ item_exec()

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.

147  {
148  struct item_options opts;
149  struct menu *menu;
150  struct menu_item *item;
151  char *label = NULL;
152  char *text = NULL;
153  int rc;
154 
155  /* Parse options */
156  if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
157  goto err_parse_options;
158 
159  /* Parse label, if present */
160  if ( ! opts.is_gap )
161  label = argv[optind++]; /* May be NULL */
162 
163  /* Parse text, if present */
164  if ( optind < argc ) {
165  text = concat_args ( &argv[optind] );
166  if ( ! text ) {
167  rc = -ENOMEM;
168  goto err_parse_text;
169  }
170  }
171 
172  /* Identify menu */
173  if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
174  goto err_parse_menu;
175 
176  /* Add menu item */
177  item = add_menu_item ( menu, label, ( text ? text : "" ),
178  opts.key, opts.is_default );
179  if ( ! item ) {
180  rc = -ENOMEM;
181  goto err_add_menu_item;
182  }
183 
184  /* Success */
185  rc = 0;
186 
187  err_add_menu_item:
188  err_parse_menu:
189  free ( text );
190  err_parse_text:
191  err_parse_options:
192  return rc;
193 }
A menu item.
Definition: menu.h:27
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int optind
Current option index.
Definition: getopt.c:51
const char * label
Label.
Definition: menu.h:31
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
"item" options
Definition: menu_cmd.c:112
#define ENOMEM
Not enough space.
Definition: errno.h:534
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
const char * text
Text.
Definition: menu.h:33
static union @437 opts
"cert<xxx>" option list
int parse_menu(char *text, struct menu **menu)
Parse menu name.
Definition: parseopt.c:203
A menu.
Definition: menu.h:15
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References add_menu_item(), concat_args(), ENOMEM, free, item_cmd, menu_item::label, NULL, optind, opts, parse_menu(), parse_options(), rc, and menu_item::text.

◆ choose_exec()

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.

230  {
231  struct choose_options opts;
232  struct named_setting setting;
233  struct menu *menu;
234  struct menu_item *item;
235  int rc;
236 
237  /* Parse options */
238  if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
239  goto err_parse_options;
240 
241  /* Parse setting name */
242  if ( ( rc = parse_autovivified_setting ( argv[optind],
243  &setting ) ) != 0 )
244  goto err_parse_setting;
245 
246  /* Identify menu */
247  if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
248  goto err_parse_menu;
249 
250  /* Show menu */
251  if ( ( rc = show_menu ( menu, opts.timeout, opts.select, &item ) ) != 0)
252  goto err_show_menu;
253 
254  /* Apply default type if necessary */
255  if ( ! setting.setting.type )
256  setting.setting.type = &setting_type_string;
257 
258  /* Store setting */
259  if ( ( rc = storef_setting ( setting.settings, &setting.setting,
260  item->label ) ) != 0 ) {
261  printf ( "Could not store \"%s\": %s\n",
262  setting.setting.name, strerror ( rc ) );
263  goto err_store;
264  }
265 
266  /* Success */
267  rc = 0;
268 
269  err_store:
270  err_show_menu:
271  /* Destroy menu, if applicable */
272  if ( ! opts.keep )
273  destroy_menu ( menu );
274  err_parse_menu:
275  err_parse_setting:
276  err_parse_options:
277  return rc;
278 }
A menu item.
Definition: menu.h:27
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
A parsed named setting.
Definition: parseopt.h:122
int optind
Current option index.
Definition: getopt.c:51
const char * label
Label.
Definition: menu.h:31
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition: parseopt.c:336
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
const char * name
Name.
Definition: settings.h:28
const struct setting_type * type
Setting type.
Definition: settings.h:36
"choose" options
Definition: menu_cmd.c:196
int storef_setting(struct settings *settings, const struct setting *setting, const char *value)
Store formatted value of setting.
Definition: settings.c:1319
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static union @437 opts
"cert<xxx>" option list
A setting.
Definition: settings.h:23
int parse_menu(char *text, struct menu **menu)
Parse menu name.
Definition: parseopt.c:203
A menu.
Definition: menu.h:15

References choose_cmd, destroy_menu(), menu_item::label, setting::name, optind, opts, parse_autovivified_setting(), parse_menu(), parse_options(), printf(), rc, show_menu(), storef_setting(), strerror(), and setting::type.

Variable Documentation

◆ menu_opts

struct option_descriptor menu_opts[]
static
Initial value:
= {
OPTION_DESC ( "delete", 'd', no_argument,
struct menu_options, delete, parse_flag ),
}
const char * name
Definition: ath9k_hw.c:1984
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
Option does not take an argument.
Definition: getopt.h:16
"menu" options
Definition: menu_cmd.c:46
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18

"menu" option list

Definition at line 54 of file menu_cmd.c.

◆ menu_cmd

struct command_descriptor menu_cmd
static
Initial value:
=
"[<title>]" )
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
"menu" options
Definition: menu_cmd.c:46
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108

"menu" command descriptor

Definition at line 62 of file menu_cmd.c.

Referenced by menu_exec().

◆ item_opts

struct option_descriptor item_opts[]
static
Initial value:
= {
OPTION_DESC ( "default", 'd', no_argument,
OPTION_DESC ( "gap", 'g', no_argument,
struct item_options, is_gap, parse_flag ),
}
int parse_key(char *text, unsigned int *key)
Parse key.
Definition: parseopt.c:241
"item" options
Definition: menu_cmd.c:112
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
Option does not take an argument.
Definition: getopt.h:16
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
int is_default
Is default item.
Definition: menu.h:37
A menu.
Definition: menu.h:15
union @382 key
Sense key.
Definition: crypto.h:284

"item" option list

Definition at line 124 of file menu_cmd.c.

◆ item_cmd

struct command_descriptor item_cmd
static
Initial value:
=
"[<label> [<text>]]" )
"item" options
Definition: menu_cmd.c:112
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108

"item" command descriptor

Definition at line 136 of file menu_cmd.c.

Referenced by item_exec().

◆ choose_opts

struct option_descriptor choose_opts[]
static
Initial value:
= {
OPTION_DESC ( "default", 'd', required_argument,
OPTION_DESC ( "timeout", 't', required_argument,
OPTION_DESC ( "keep", 'k', no_argument,
struct choose_options, keep, parse_flag ),
}
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
"choose" options
Definition: menu_cmd.c:196
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
int select(fd_set *readfds, int wait)
Check file descriptors for readiness.
Definition: posix_io.c:229
Option does not take an argument.
Definition: getopt.h:16
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
void timeout(int)
A menu.
Definition: menu.h:15

"choose" option list

Definition at line 208 of file menu_cmd.c.

◆ choose_cmd

struct command_descriptor choose_cmd
static
Initial value:
=
COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" )
"choose" options
Definition: menu_cmd.c:196
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108

"choose" command descriptor

Definition at line 220 of file menu_cmd.c.

Referenced by choose_exec().

◆ __command

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.