iPXE
dynui_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  * Dynamic user interface 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/dynui.h>
38 #include <ipxe/command.h>
39 #include <ipxe/parseopt.h>
40 #include <ipxe/settings.h>
41 #include <ipxe/features.h>
42 
44 
45 /** "dynui" options */
46 struct dynui_options {
47  /** Name */
48  char *name;
49  /** Delete */
50  int delete;
51 };
52 
53 /** "dynui" option list */
54 static struct option_descriptor dynui_opts[] = {
55  OPTION_DESC ( "name", 'n', required_argument,
56  struct dynui_options, name, parse_string ),
57  OPTION_DESC ( "delete", 'd', no_argument,
58  struct dynui_options, delete, parse_flag ),
59 };
60 
61 /** "dynui" command descriptor */
64  "[<title>]" );
65 
66 /**
67  * The "dynui" command
68  *
69  * @v argc Argument count
70  * @v argv Argument list
71  * @ret rc Return status code
72  */
73 static int dynui_exec ( int argc, char **argv ) {
74  struct dynui_options opts;
75  struct dynamic_ui *dynui;
76  char *title;
77  int rc;
78 
79  /* Parse options */
80  if ( ( rc = parse_options ( argc, argv, &dynui_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 dynamic user interface */
91  dynui = create_dynui ( opts.name, title );
92  if ( ! dynui ) {
93  rc = -ENOMEM;
94  goto err_create_dynui;
95  }
96 
97  /* Destroy dynamic user interface, if applicable */
98  if ( opts.delete )
99  destroy_dynui ( dynui );
100 
101  /* Success */
102  rc = 0;
103 
104  err_create_dynui:
105  free ( title );
106  err_parse_title:
107  err_parse_options:
108  return rc;
109 }
110 
111 /** "item" options */
112 struct item_options {
113  /** Dynamic user interface name */
114  char *dynui;
115  /** Shortcut key */
116  unsigned int key;
117  /** Use as default */
119  /** Value is a secret */
121  /** Use as a separator */
122  int is_gap;
123 };
124 
125 /** "item" option list */
126 static struct option_descriptor item_opts[] = {
127  OPTION_DESC ( "menu", 'm', required_argument,
128  struct item_options, dynui, parse_string ),
129  OPTION_DESC ( "form", 'f', required_argument,
130  struct item_options, dynui, parse_string ),
131  OPTION_DESC ( "key", 'k', required_argument,
132  struct item_options, key, parse_key ),
133  OPTION_DESC ( "default", 'd', no_argument,
134  struct item_options, is_default, parse_flag ),
135  OPTION_DESC ( "secret", 's', no_argument,
136  struct item_options, is_secret, parse_flag ),
137  OPTION_DESC ( "gap", 'g', no_argument,
138  struct item_options, is_gap, parse_flag ),
139 };
140 
141 /** "item" command descriptor */
144  "[<name> [<text>]]" );
145 
146 /**
147  * The "item" command
148  *
149  * @v argc Argument count
150  * @v argv Argument list
151  * @ret rc Return status code
152  */
153 static int item_exec ( int argc, char **argv ) {
154  struct item_options opts;
155  struct dynamic_ui *dynui;
156  struct dynamic_item *item;
157  unsigned int flags = 0;
158  char *name = NULL;
159  char *text = NULL;
160  int rc;
161 
162  /* Parse options */
163  if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
164  goto err_parse_options;
165 
166  /* Parse name, if present */
167  if ( ! opts.is_gap )
168  name = argv[optind++]; /* May be NULL */
169 
170  /* Parse text, if present */
171  if ( optind < argc ) {
172  text = concat_args ( &argv[optind] );
173  if ( ! text ) {
174  rc = -ENOMEM;
175  goto err_parse_text;
176  }
177  }
178 
179  /* Identify dynamic user interface */
180  if ( ( rc = parse_dynui ( opts.dynui, &dynui ) ) != 0 )
181  goto err_parse_dynui;
182 
183  /* Add dynamic user interface item */
184  if ( opts.is_default )
185  flags |= DYNUI_DEFAULT;
186  if ( opts.is_secret )
187  flags |= DYNUI_SECRET;
188  item = add_dynui_item ( dynui, name, ( text ? text : "" ), flags,
189  opts.key );
190  if ( ! item ) {
191  rc = -ENOMEM;
192  goto err_add_dynui_item;
193  }
194 
195  /* Success */
196  rc = 0;
197 
198  err_add_dynui_item:
199  err_parse_dynui:
200  free ( text );
201  err_parse_text:
202  err_parse_options:
203  return rc;
204 }
205 
206 /** "choose" options */
208  /** Dynamic user interface name */
209  char *dynui;
210  /** Timeout */
211  unsigned long timeout;
212  /** Default selection */
213  char *select;
214  /** Keep dynamic user interface */
215  int keep;
216 };
217 
218 /** "choose" option list */
219 static struct option_descriptor choose_opts[] = {
220  OPTION_DESC ( "menu", 'm', required_argument,
221  struct choose_options, dynui, parse_string ),
222  OPTION_DESC ( "default", 'd', required_argument,
224  OPTION_DESC ( "timeout", 't', required_argument,
226  OPTION_DESC ( "keep", 'k', no_argument,
227  struct choose_options, keep, parse_flag ),
228 };
229 
230 /** "choose" command descriptor */
232  COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
233 
234 /**
235  * The "choose" command
236  *
237  * @v argc Argument count
238  * @v argv Argument list
239  * @ret rc Return status code
240  */
241 static int choose_exec ( int argc, char **argv ) {
242  struct choose_options opts;
243  struct named_setting setting;
244  struct dynamic_ui *dynui;
245  struct dynamic_item *item;
246  int rc;
247 
248  /* Parse options */
249  if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
250  goto err_parse_options;
251 
252  /* Parse setting name */
253  if ( ( rc = parse_autovivified_setting ( argv[optind],
254  &setting ) ) != 0 )
255  goto err_parse_setting;
256 
257  /* Identify dynamic user interface */
258  if ( ( rc = parse_dynui ( opts.dynui, &dynui ) ) != 0 )
259  goto err_parse_dynui;
260 
261  /* Show as menu */
262  if ( ( rc = show_menu ( dynui, opts.timeout, opts.select,
263  &item ) ) != 0 )
264  goto err_show_menu;
265 
266  /* Apply default type if necessary */
267  if ( ! setting.setting.type )
268  setting.setting.type = &setting_type_string;
269 
270  /* Store setting */
271  if ( ( rc = storef_setting ( setting.settings, &setting.setting,
272  item->name ) ) != 0 ) {
273  printf ( "Could not store \"%s\": %s\n",
274  setting.setting.name, strerror ( rc ) );
275  goto err_store;
276  }
277 
278  /* Success */
279  rc = 0;
280 
281  err_store:
282  err_show_menu:
283  /* Destroy dynamic user interface, if applicable */
284  if ( ! opts.keep )
285  destroy_dynui ( dynui );
286  err_parse_dynui:
287  err_parse_setting:
288  err_parse_options:
289  return rc;
290 }
291 
292 /** "present" options */
294  /** Dynamic user interface name */
295  char *dynui;
296  /** Keep dynamic user interface */
297  int keep;
298 };
299 
300 /** "present" option list */
301 static struct option_descriptor present_opts[] = {
302  OPTION_DESC ( "form", 'f', required_argument,
303  struct present_options, dynui, parse_string ),
304  OPTION_DESC ( "keep", 'k', no_argument,
305  struct present_options, keep, parse_flag ),
306 };
307 
308 /** "present" command descriptor */
310  COMMAND_DESC ( struct present_options, present_opts, 0, 0, NULL );
311 
312 /**
313  * The "present" command
314  *
315  * @v argc Argument count
316  * @v argv Argument list
317  * @ret rc Return status code
318  */
319 static int present_exec ( int argc, char **argv ) {
320  struct present_options opts;
321  struct dynamic_ui *dynui;
322  int rc;
323 
324  /* Parse options */
325  if ( ( rc = parse_options ( argc, argv, &present_cmd, &opts ) ) != 0 )
326  goto err_parse_options;
327 
328  /* Identify dynamic user interface */
329  if ( ( rc = parse_dynui ( opts.dynui, &dynui ) ) != 0 )
330  goto err_parse_dynui;
331 
332  /* Show as form */
333  if ( ( rc = show_form ( dynui ) ) != 0 )
334  goto err_show_form;
335 
336  /* Success */
337  rc = 0;
338 
339  err_show_form:
340  /* Destroy dynamic user interface, if applicable */
341  if ( ! opts.keep )
342  destroy_dynui ( dynui );
343  err_parse_dynui:
344  err_parse_options:
345  return rc;
346 }
347 
348 /** Dynamic user interface commands */
349 struct command dynui_commands[] __command = {
350  {
351  .name = "menu",
352  .exec = dynui_exec,
353  },
354  {
355  .name = "form",
356  .exec = dynui_exec,
357  },
358  {
359  .name = "item",
360  .exec = item_exec,
361  },
362  {
363  .name = "choose",
364  .exec = choose_exec,
365  },
366  {
367  .name = "present",
368  .exec = present_exec,
369  },
370 };
char * dynui
Dynamic user interface name.
Definition: dynui_cmd.c:209
const char * title
Title.
Definition: dynui.h:21
static struct command_descriptor dynui_cmd
"dynui" command descriptor
Definition: dynui_cmd.c:62
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
A dynamic user interface item.
Definition: dynui.h:29
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
static int item_exec(int argc, char **argv)
The "item" command.
Definition: dynui_cmd.c:153
int parse_dynui(char *text, struct dynamic_ui **dynui)
Parse dynamic user interface name.
Definition: parseopt.c:203
static int present_exec(int argc, char **argv)
The "present" command.
Definition: dynui_cmd.c:319
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 keep
Keep dynamic user interface.
Definition: dynui_cmd.c:297
struct command dynui_commands [] __command
Dynamic user interface commands.
Definition: dynui_cmd.c:349
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
FEATURE(FEATURE_MISC, "Menu", DHCP_EB_FEATURE_MENU, 1)
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
"item" options
Definition: dynui_cmd.c:112
const char * name
Name.
Definition: settings.h:28
"dynui" options
Definition: dynui_cmd.c:46
static struct option_descriptor present_opts[]
"present" option list
Definition: dynui_cmd.c:301
static struct command_descriptor present_cmd
"present" command descriptor
Definition: dynui_cmd.c:309
static struct command_descriptor item_cmd
"item" command descriptor
Definition: dynui_cmd.c:142
#define ENOMEM
Not enough space.
Definition: errno.h:534
char * select
Default selection.
Definition: dynui_cmd.c:213
"present" options
Definition: dynui_cmd.c:293
int keep
Keep dynamic user interface.
Definition: dynui_cmd.c:215
Parse command-line options.
static int dynui_exec(int argc, char **argv)
The "dynui" command.
Definition: dynui_cmd.c:73
const char * name
Name.
Definition: dynui.h:33
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
char * name
Name.
Definition: dynui_cmd.c:48
#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: dynui_cmd.c:207
int is_default
Use as default.
Definition: dynui_cmd.c:118
static int choose_exec(int argc, char **argv)
The "choose" command.
Definition: dynui_cmd.c:241
static struct option_descriptor item_opts[]
"item" option list
Definition: dynui_cmd.c:126
struct dynamic_ui * create_dynui(const char *name, const char *title)
Create dynamic user interface.
Definition: dynui.c:48
static struct option_descriptor choose_opts[]
"choose" option list
Definition: dynui_cmd.c:219
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
struct dynamic_item * add_dynui_item(struct dynamic_ui *dynui, const char *name, const char *text, unsigned int flags, int shortcut)
Add dynamic user interface item.
Definition: dynui.c:103
uint8_t flags
Flags.
Definition: ena.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static struct command_descriptor choose_cmd
"choose" command descriptor
Definition: dynui_cmd.c:231
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
A setting.
Definition: settings.h:23
int show_form(struct dynamic_ui *dynui)
Show form.
Definition: form_ui.c:507
const char * name
Name of the command.
Definition: command.h:11
A dynamic user interface.
Definition: dynui.h:15
char * dynui
Dynamic user interface name.
Definition: dynui_cmd.c:114
int is_secret
Value is a secret.
Definition: dynui_cmd.c:120
int is_gap
Use as a separator.
Definition: dynui_cmd.c:122
static struct option_descriptor dynui_opts[]
"dynui" option list
Definition: dynui_cmd.c:54
const char * text
Text.
Definition: dynui.h:35
#define DYNUI_DEFAULT
Dynamic user interface item is default selection.
Definition: dynui.h:45
#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
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
void timeout(int)
char * dynui
Dynamic user interface name.
Definition: dynui_cmd.c:295
Dynamic user interfaces.
unsigned long timeout
Timeout.
Definition: dynui_cmd.c:211
static union @438 opts
"cert<xxx>" option list
void destroy_dynui(struct dynamic_ui *dynui)
Destroy dynamic user interface.
Definition: dynui.c:149
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define DYNUI_SECRET
Dynamic user interface item represents a secret.
Definition: dynui.h:48
String functions.
unsigned int key
Shortcut key.
Definition: dynui_cmd.c:116
union @383 key
Sense key.
Definition: scsi.h:18