iPXE
menu_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * Menu commands
29  *
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <getopt.h>
37 #include <ipxe/menu.h>
38 #include <ipxe/command.h>
39 #include <ipxe/parseopt.h>
40 #include <ipxe/settings.h>
41 #include <ipxe/features.h>
42 
44 
45 /** "menu" options */
46 struct menu_options {
47  /** Name */
48  char *name;
49  /** Delete */
50  int delete;
51 };
52 
53 /** "menu" option list */
54 static struct option_descriptor menu_opts[] = {
55  OPTION_DESC ( "name", 'n', required_argument,
56  struct menu_options, name, parse_string ),
57  OPTION_DESC ( "delete", 'd', no_argument,
58  struct menu_options, delete, parse_flag ),
59 };
60 
61 /** "menu" command descriptor */
64  "[<title>]" );
65 
66 /**
67  * The "menu" command
68  *
69  * @v argc Argument count
70  * @v argv Argument list
71  * @ret rc Return status code
72  */
73 static int menu_exec ( int argc, char **argv ) {
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 }
110 
111 /** "item" options */
112 struct item_options {
113  /** Menu name */
114  char *menu;
115  /** Shortcut key */
116  unsigned int key;
117  /** Use as default */
119  /** Use as a separator */
120  int is_gap;
121 };
122 
123 /** "item" option list */
124 static struct option_descriptor item_opts[] = {
125  OPTION_DESC ( "menu", 'm', required_argument,
126  struct item_options, menu, parse_string ),
127  OPTION_DESC ( "key", 'k', required_argument,
128  struct item_options, key, parse_key ),
129  OPTION_DESC ( "default", 'd', no_argument,
130  struct item_options, is_default, parse_flag ),
131  OPTION_DESC ( "gap", 'g', no_argument,
132  struct item_options, is_gap, parse_flag ),
133 };
134 
135 /** "item" command descriptor */
138  "[<label> [<text>]]" );
139 
140 /**
141  * The "item" command
142  *
143  * @v argc Argument count
144  * @v argv Argument list
145  * @ret rc Return status code
146  */
147 static int item_exec ( int argc, char **argv ) {
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 }
194 
195 /** "choose" options */
197  /** Menu name */
198  char *menu;
199  /** Timeout */
200  unsigned long timeout;
201  /** Default selection */
202  char *select;
203  /** Keep menu */
204  int keep;
205 };
206 
207 /** "choose" option list */
208 static struct option_descriptor choose_opts[] = {
209  OPTION_DESC ( "menu", 'm', required_argument,
210  struct choose_options, menu, parse_string ),
211  OPTION_DESC ( "default", 'd', required_argument,
213  OPTION_DESC ( "timeout", 't', required_argument,
215  OPTION_DESC ( "keep", 'k', no_argument,
216  struct choose_options, keep, parse_flag ),
217 };
218 
219 /** "choose" command descriptor */
221  COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
222 
223 /**
224  * The "choose" command
225  *
226  * @v argc Argument count
227  * @v argv Argument list
228  * @ret rc Return status code
229  */
230 static int choose_exec ( int argc, char **argv ) {
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 }
279 
280 /** Menu commands */
281 struct command menu_commands[] __command = {
282  {
283  .name = "menu",
284  .exec = menu_exec,
285  },
286  {
287  .name = "item",
288  .exec = item_exec,
289  },
290  {
291  .name = "choose",
292  .exec = choose_exec,
293  },
294 };
A menu item.
Definition: menu.h:27
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
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 parse_key(char *text, unsigned int *key)
Parse key.
Definition: parseopt.c:241
int optind
Current option index.
Definition: getopt.c:51
const char * label
Label.
Definition: menu.h:31
Error codes.
A command-line command.
Definition: command.h:9
#define DHCP_EB_FEATURE_MENU
Menu support.
Definition: features.h:55
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
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
A command descriptor.
Definition: parseopt.h:77
"item" options
Definition: menu_cmd.c:112
const char * name
Name.
Definition: settings.h:28
#define ENOMEM
Not enough space.
Definition: errno.h:534
char * select
Default selection.
Definition: menu_cmd.c:202
int keep
Keep menu.
Definition: menu_cmd.c:204
Parse command-line options.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
Feature list.
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
const struct setting_type * type
Setting type.
Definition: settings.h:36
"choose" options
Definition: menu_cmd.c:196
int is_default
Use as default.
Definition: menu_cmd.c:118
char * name
Name.
Definition: menu_cmd.c:48
Configuration settings.
#define FEATURE_MISC
Miscellaneous.
Definition: features.h:23
int storef_setting(struct settings *settings, const struct setting *setting, const char *value)
Store formatted value of setting.
Definition: settings.c:1319
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
char * menu
Menu name.
Definition: menu_cmd.c:114
const char * title
Title.
Definition: menu.h:21
Command line option parsing.
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
const char * text
Text.
Definition: menu.h:33
static union @437 opts
"cert<xxx>" option list
A setting.
Definition: settings.h:23
"menu" options
Definition: menu_cmd.c:46
const char * name
Name of the command.
Definition: command.h:11
int is_gap
Use as a separator.
Definition: menu_cmd.c:120
char * menu
Menu name.
Definition: menu_cmd.c:198
#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
A command-line option descriptor.
Definition: parseopt.h:23
int parse_menu(char *text, struct menu **menu)
Parse menu name.
Definition: parseopt.c:203
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
void timeout(int)
unsigned long timeout
Timeout.
Definition: menu_cmd.c:200
A menu.
Definition: menu.h:15
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
unsigned int key
Shortcut key.
Definition: menu_cmd.c:116
union @382 key
Sense key.
Definition: crypto.h:284