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  /** Initial timeout */
211  unsigned long timeout;
212  /** Post-activity timeout */
213  unsigned long retimeout;
214  /** Default selection */
215  char *select;
216  /** Keep dynamic user interface */
217  int keep;
218 };
219 
220 /** "choose" option list */
221 static struct option_descriptor choose_opts[] = {
222  OPTION_DESC ( "menu", 'm', required_argument,
223  struct choose_options, dynui, parse_string ),
224  OPTION_DESC ( "default", 'd', required_argument,
226  OPTION_DESC ( "timeout", 't', required_argument,
228  OPTION_DESC ( "retimeout", 'r', required_argument,
229  struct choose_options, retimeout, parse_timeout ),
230  OPTION_DESC ( "keep", 'k', no_argument,
231  struct choose_options, keep, parse_flag ),
232 };
233 
234 /** "choose" command descriptor */
236  COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
237 
238 /**
239  * The "choose" command
240  *
241  * @v argc Argument count
242  * @v argv Argument list
243  * @ret rc Return status code
244  */
245 static int choose_exec ( int argc, char **argv ) {
246  struct choose_options opts;
247  struct named_setting setting;
248  struct dynamic_ui *dynui;
249  struct dynamic_item *item;
250  int rc;
251 
252  /* Parse options */
253  if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
254  goto err_parse_options;
255 
256  /* Parse setting name */
257  if ( ( rc = parse_autovivified_setting ( argv[optind],
258  &setting ) ) != 0 )
259  goto err_parse_setting;
260 
261  /* Identify dynamic user interface */
262  if ( ( rc = parse_dynui ( opts.dynui, &dynui ) ) != 0 )
263  goto err_parse_dynui;
264 
265  /* Show as menu */
266  if ( ( rc = show_menu ( dynui, opts.timeout, opts.retimeout,
267  opts.select, &item ) ) != 0 )
268  goto err_show_menu;
269 
270  /* Apply default type if necessary */
271  if ( ! setting.setting.type )
272  setting.setting.type = &setting_type_string;
273 
274  /* Store setting */
275  if ( ( rc = storef_setting ( setting.settings, &setting.setting,
276  item->name ) ) != 0 ) {
277  printf ( "Could not store \"%s\": %s\n",
278  setting.setting.name, strerror ( rc ) );
279  goto err_store;
280  }
281 
282  /* Success */
283  rc = 0;
284 
285  err_store:
286  err_show_menu:
287  /* Destroy dynamic user interface, if applicable */
288  if ( ! opts.keep )
289  destroy_dynui ( dynui );
290  err_parse_dynui:
291  err_parse_setting:
292  err_parse_options:
293  return rc;
294 }
295 
296 /** "present" options */
298  /** Dynamic user interface name */
299  char *dynui;
300  /** Keep dynamic user interface */
301  int keep;
302 };
303 
304 /** "present" option list */
305 static struct option_descriptor present_opts[] = {
306  OPTION_DESC ( "form", 'f', required_argument,
307  struct present_options, dynui, parse_string ),
308  OPTION_DESC ( "keep", 'k', no_argument,
309  struct present_options, keep, parse_flag ),
310 };
311 
312 /** "present" command descriptor */
314  COMMAND_DESC ( struct present_options, present_opts, 0, 0, NULL );
315 
316 /**
317  * The "present" command
318  *
319  * @v argc Argument count
320  * @v argv Argument list
321  * @ret rc Return status code
322  */
323 static int present_exec ( int argc, char **argv ) {
324  struct present_options opts;
325  struct dynamic_ui *dynui;
326  int rc;
327 
328  /* Parse options */
329  if ( ( rc = parse_options ( argc, argv, &present_cmd, &opts ) ) != 0 )
330  goto err_parse_options;
331 
332  /* Identify dynamic user interface */
333  if ( ( rc = parse_dynui ( opts.dynui, &dynui ) ) != 0 )
334  goto err_parse_dynui;
335 
336  /* Show as form */
337  if ( ( rc = show_form ( dynui ) ) != 0 )
338  goto err_show_form;
339 
340  /* Success */
341  rc = 0;
342 
343  err_show_form:
344  /* Destroy dynamic user interface, if applicable */
345  if ( ! opts.keep )
346  destroy_dynui ( dynui );
347  err_parse_dynui:
348  err_parse_options:
349  return rc;
350 }
351 
352 /** Dynamic user interface commands */
353 struct command dynui_commands[] __command = {
354  {
355  .name = "menu",
356  .exec = dynui_exec,
357  },
358  {
359  .name = "form",
360  .exec = dynui_exec,
361  },
362  {
363  .name = "item",
364  .exec = item_exec,
365  },
366  {
367  .name = "choose",
368  .exec = choose_exec,
369  },
370  {
371  .name = "present",
372  .exec = present_exec,
373  },
374 };
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:323
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:301
struct command dynui_commands [] __command
Dynamic user interface commands.
Definition: dynui_cmd.c:353
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
unsigned long retimeout
Post-activity timeout.
Definition: dynui_cmd.c:213
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:305
static struct command_descriptor present_cmd
"present" command descriptor
Definition: dynui_cmd.c:313
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:215
"present" options
Definition: dynui_cmd.c:297
int keep
Keep dynamic user interface.
Definition: dynui_cmd.c:217
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:245
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:221
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:235
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:299
Dynamic user interfaces.
unsigned long timeout
Initial 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