|
@@ -0,0 +1,280 @@
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+/** @file
|
|
22
|
+ *
|
|
23
|
+ * Menu commands
|
|
24
|
+ *
|
|
25
|
+ */
|
|
26
|
+
|
|
27
|
+#include <stdio.h>
|
|
28
|
+#include <stdlib.h>
|
|
29
|
+#include <string.h>
|
|
30
|
+#include <errno.h>
|
|
31
|
+#include <getopt.h>
|
|
32
|
+#include <ipxe/menu.h>
|
|
33
|
+#include <ipxe/command.h>
|
|
34
|
+#include <ipxe/parseopt.h>
|
|
35
|
+#include <ipxe/settings.h>
|
|
36
|
+#include <ipxe/features.h>
|
|
37
|
+
|
|
38
|
+FEATURE ( FEATURE_MISC, "Menu", DHCP_EB_FEATURE_MENU, 1 );
|
|
39
|
+
|
|
40
|
+/** "menu" options */
|
|
41
|
+struct menu_options {
|
|
42
|
+ /** Name */
|
|
43
|
+ const char *name;
|
|
44
|
+ /** Delete */
|
|
45
|
+ int delete;
|
|
46
|
+};
|
|
47
|
+
|
|
48
|
+/** "menu" option list */
|
|
49
|
+static struct option_descriptor menu_opts[] = {
|
|
50
|
+ OPTION_DESC ( "name", 'n', required_argument,
|
|
51
|
+ struct menu_options, name, parse_string ),
|
|
52
|
+ OPTION_DESC ( "delete", 'd', no_argument,
|
|
53
|
+ struct menu_options, delete, parse_flag ),
|
|
54
|
+};
|
|
55
|
+
|
|
56
|
+/** "menu" command descriptor */
|
|
57
|
+static struct command_descriptor menu_cmd =
|
|
58
|
+ COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
|
|
59
|
+ "[--name <name>] [--delete] [<title>]" );
|
|
60
|
+
|
|
61
|
+/**
|
|
62
|
+ * The "menu" command
|
|
63
|
+ *
|
|
64
|
+ * @v argc Argument count
|
|
65
|
+ * @v argv Argument list
|
|
66
|
+ * @ret rc Return status code
|
|
67
|
+ */
|
|
68
|
+static int menu_exec ( int argc, char **argv ) {
|
|
69
|
+ struct menu_options opts;
|
|
70
|
+ struct menu *menu;
|
|
71
|
+ char *title;
|
|
72
|
+ int rc;
|
|
73
|
+
|
|
74
|
+ /* Parse options */
|
|
75
|
+ if ( ( rc = parse_options ( argc, argv, &menu_cmd, &opts ) ) != 0 )
|
|
76
|
+ goto err_parse_options;
|
|
77
|
+
|
|
78
|
+ /* Parse title */
|
|
79
|
+ title = concat_args ( &argv[optind] );
|
|
80
|
+ if ( ! title ) {
|
|
81
|
+ rc = -ENOMEM;
|
|
82
|
+ goto err_parse_title;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ /* Create menu */
|
|
86
|
+ menu = create_menu ( opts.name, title );
|
|
87
|
+ if ( ! menu ) {
|
|
88
|
+ rc = -ENOMEM;
|
|
89
|
+ goto err_create_menu;
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ /* Destroy menu, if applicable */
|
|
93
|
+ if ( opts.delete )
|
|
94
|
+ destroy_menu ( menu );
|
|
95
|
+
|
|
96
|
+ /* Success */
|
|
97
|
+ rc = 0;
|
|
98
|
+
|
|
99
|
+ err_create_menu:
|
|
100
|
+ free ( title );
|
|
101
|
+ err_parse_title:
|
|
102
|
+ err_parse_options:
|
|
103
|
+ return rc;
|
|
104
|
+}
|
|
105
|
+
|
|
106
|
+/** "item" options */
|
|
107
|
+struct item_options {
|
|
108
|
+ /** Menu name */
|
|
109
|
+ const char *menu;
|
|
110
|
+ /** Shortcut key */
|
|
111
|
+ unsigned int key;
|
|
112
|
+ /** Use as default */
|
|
113
|
+ int is_default;
|
|
114
|
+ /** Use as a separator */
|
|
115
|
+ int is_gap;
|
|
116
|
+};
|
|
117
|
+
|
|
118
|
+/** "item" option list */
|
|
119
|
+static struct option_descriptor item_opts[] = {
|
|
120
|
+ OPTION_DESC ( "menu", 'm', required_argument,
|
|
121
|
+ struct item_options, menu, parse_string ),
|
|
122
|
+ OPTION_DESC ( "key", 'k', required_argument,
|
|
123
|
+ struct item_options, key, parse_key ),
|
|
124
|
+ OPTION_DESC ( "default", 'd', no_argument,
|
|
125
|
+ struct item_options, is_default, parse_flag ),
|
|
126
|
+ OPTION_DESC ( "gap", 'g', no_argument,
|
|
127
|
+ struct item_options, is_gap, parse_flag ),
|
|
128
|
+};
|
|
129
|
+
|
|
130
|
+/** "item" command descriptor */
|
|
131
|
+static struct command_descriptor item_cmd =
|
|
132
|
+ COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
|
|
133
|
+ "[--menu <menu>] [--key <key>] [--default] "
|
|
134
|
+ "[<label>|--gap [<text>]]" );
|
|
135
|
+
|
|
136
|
+/**
|
|
137
|
+ * The "item" command
|
|
138
|
+ *
|
|
139
|
+ * @v argc Argument count
|
|
140
|
+ * @v argv Argument list
|
|
141
|
+ * @ret rc Return status code
|
|
142
|
+ */
|
|
143
|
+static int item_exec ( int argc, char **argv ) {
|
|
144
|
+ struct item_options opts;
|
|
145
|
+ struct menu *menu;
|
|
146
|
+ struct menu_item *item;
|
|
147
|
+ char *label = NULL;
|
|
148
|
+ char *text = NULL;
|
|
149
|
+ int rc;
|
|
150
|
+
|
|
151
|
+ /* Parse options */
|
|
152
|
+ if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
|
|
153
|
+ goto err_parse_options;
|
|
154
|
+
|
|
155
|
+ /* Parse label, if present */
|
|
156
|
+ if ( ! opts.is_gap )
|
|
157
|
+ label = argv[optind++]; /* May be NULL */
|
|
158
|
+
|
|
159
|
+ /* Parse text, if present */
|
|
160
|
+ if ( optind < argc ) {
|
|
161
|
+ text = concat_args ( &argv[optind] );
|
|
162
|
+ if ( ! text ) {
|
|
163
|
+ rc = -ENOMEM;
|
|
164
|
+ goto err_parse_text;
|
|
165
|
+ }
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ /* Identify menu */
|
|
169
|
+ if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
|
|
170
|
+ goto err_parse_menu;
|
|
171
|
+
|
|
172
|
+ /* Add menu item */
|
|
173
|
+ item = add_menu_item ( menu, label, ( text ? text : "" ),
|
|
174
|
+ opts.key, opts.is_default );
|
|
175
|
+ if ( ! item ) {
|
|
176
|
+ rc = -ENOMEM;
|
|
177
|
+ goto err_add_menu_item;
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ /* Success */
|
|
181
|
+ rc = 0;
|
|
182
|
+
|
|
183
|
+ err_add_menu_item:
|
|
184
|
+ err_parse_menu:
|
|
185
|
+ free ( text );
|
|
186
|
+ err_parse_text:
|
|
187
|
+ err_parse_options:
|
|
188
|
+ return rc;
|
|
189
|
+}
|
|
190
|
+
|
|
191
|
+/** "choose" options */
|
|
192
|
+struct choose_options {
|
|
193
|
+ /** Menu name */
|
|
194
|
+ const char *menu;
|
|
195
|
+ /** Timeout */
|
|
196
|
+ unsigned int timeout;
|
|
197
|
+ /** Keep menu */
|
|
198
|
+ int keep;
|
|
199
|
+};
|
|
200
|
+
|
|
201
|
+/** "choose" option list */
|
|
202
|
+static struct option_descriptor choose_opts[] = {
|
|
203
|
+ OPTION_DESC ( "menu", 'm', required_argument,
|
|
204
|
+ struct choose_options, menu, parse_string ),
|
|
205
|
+ OPTION_DESC ( "timeout", 't', required_argument,
|
|
206
|
+ struct choose_options, timeout, parse_integer ),
|
|
207
|
+ OPTION_DESC ( "keep", 'k', no_argument,
|
|
208
|
+ struct choose_options, keep, parse_flag ),
|
|
209
|
+};
|
|
210
|
+
|
|
211
|
+/** "choose" command descriptor */
|
|
212
|
+static struct command_descriptor choose_cmd =
|
|
213
|
+ COMMAND_DESC ( struct choose_options, choose_opts, 1, 1,
|
|
214
|
+ "[--menu <menu>] [--timeout <timeout>] [--keep] "
|
|
215
|
+ "<setting>" );
|
|
216
|
+
|
|
217
|
+/**
|
|
218
|
+ * The "choose" command
|
|
219
|
+ *
|
|
220
|
+ * @v argc Argument count
|
|
221
|
+ * @v argv Argument list
|
|
222
|
+ * @ret rc Return status code
|
|
223
|
+ */
|
|
224
|
+static int choose_exec ( int argc, char **argv ) {
|
|
225
|
+ struct choose_options opts;
|
|
226
|
+ struct menu *menu;
|
|
227
|
+ struct menu_item *item;
|
|
228
|
+ const char *setting;
|
|
229
|
+ int rc;
|
|
230
|
+
|
|
231
|
+ /* Parse options */
|
|
232
|
+ if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
|
|
233
|
+ goto err_parse_options;
|
|
234
|
+
|
|
235
|
+ /* Parse setting name */
|
|
236
|
+ setting = argv[optind];
|
|
237
|
+
|
|
238
|
+ /* Identify menu */
|
|
239
|
+ if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
|
|
240
|
+ goto err_parse_menu;
|
|
241
|
+
|
|
242
|
+ /* Show menu */
|
|
243
|
+ if ( ( rc = show_menu ( menu, opts.timeout, &item ) ) != 0 )
|
|
244
|
+ goto err_show_menu;
|
|
245
|
+
|
|
246
|
+ /* Store setting */
|
|
247
|
+ if ( ( rc = storef_named_setting ( setting, item->label ) ) != 0 ) {
|
|
248
|
+ printf ( "Could not store \"%s\": %s\n",
|
|
249
|
+ setting, strerror ( rc ) );
|
|
250
|
+ goto err_store;
|
|
251
|
+ }
|
|
252
|
+
|
|
253
|
+ /* Success */
|
|
254
|
+ rc = 0;
|
|
255
|
+
|
|
256
|
+ err_store:
|
|
257
|
+ err_show_menu:
|
|
258
|
+ /* Destroy menu, if applicable */
|
|
259
|
+ if ( ! opts.keep )
|
|
260
|
+ destroy_menu ( menu );
|
|
261
|
+ err_parse_menu:
|
|
262
|
+ err_parse_options:
|
|
263
|
+ return rc;
|
|
264
|
+}
|
|
265
|
+
|
|
266
|
+/** Menu commands */
|
|
267
|
+struct command menu_commands[] __command = {
|
|
268
|
+ {
|
|
269
|
+ .name = "menu",
|
|
270
|
+ .exec = menu_exec,
|
|
271
|
+ },
|
|
272
|
+ {
|
|
273
|
+ .name = "item",
|
|
274
|
+ .exec = item_exec,
|
|
275
|
+ },
|
|
276
|
+ {
|
|
277
|
+ .name = "choose",
|
|
278
|
+ .exec = choose_exec,
|
|
279
|
+ },
|
|
280
|
+};
|