2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
33 #include <ipxe/menu.h>
34 #include <ipxe/command.h>
35 #include <ipxe/parseopt.h>
36 #include <ipxe/settings.h>
37 #include <ipxe/features.h>
39 FEATURE ( FEATURE_MISC, "Menu", DHCP_EB_FEATURE_MENU, 1 );
49 /** "menu" option list */
50 static struct option_descriptor menu_opts[] = {
51 OPTION_DESC ( "name", 'n', required_argument,
52 struct menu_options, name, parse_string ),
53 OPTION_DESC ( "delete", 'd', no_argument,
54 struct menu_options, delete, parse_flag ),
57 /** "menu" command descriptor */
58 static struct command_descriptor menu_cmd =
59 COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
65 * @v argc Argument count
66 * @v argv Argument list
67 * @ret rc Return status code
69 static int menu_exec ( int argc, char **argv ) {
70 struct menu_options opts;
76 if ( ( rc = parse_options ( argc, argv, &menu_cmd, &opts ) ) != 0 )
77 goto err_parse_options;
80 title = concat_args ( &argv[optind] );
87 menu = create_menu ( opts.name, title );
93 /* Destroy menu, if applicable */
95 destroy_menu ( menu );
107 /** "item" options */
108 struct item_options {
113 /** Use as default */
115 /** Use as a separator */
119 /** "item" option list */
120 static struct option_descriptor item_opts[] = {
121 OPTION_DESC ( "menu", 'm', required_argument,
122 struct item_options, menu, parse_string ),
123 OPTION_DESC ( "key", 'k', required_argument,
124 struct item_options, key, parse_key ),
125 OPTION_DESC ( "default", 'd', no_argument,
126 struct item_options, is_default, parse_flag ),
127 OPTION_DESC ( "gap", 'g', no_argument,
128 struct item_options, is_gap, parse_flag ),
131 /** "item" command descriptor */
132 static struct command_descriptor item_cmd =
133 COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
134 "[<label> [<text>]]" );
139 * @v argc Argument count
140 * @v argv Argument list
141 * @ret rc Return status code
143 static int item_exec ( int argc, char **argv ) {
144 struct item_options opts;
146 struct menu_item *item;
152 if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
153 goto err_parse_options;
155 /* Parse label, if present */
157 label = argv[optind++]; /* May be NULL */
159 /* Parse text, if present */
160 if ( optind < argc ) {
161 text = concat_args ( &argv[optind] );
169 if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
173 item = add_menu_item ( menu, label, ( text ? text : "" ),
174 opts.key, opts.is_default );
177 goto err_add_menu_item;
191 /** "choose" options */
192 struct choose_options {
196 unsigned long timeout;
197 /** Default selection */
203 /** "choose" option list */
204 static struct option_descriptor choose_opts[] = {
205 OPTION_DESC ( "menu", 'm', required_argument,
206 struct choose_options, menu, parse_string ),
207 OPTION_DESC ( "default", 'd', required_argument,
208 struct choose_options, select, parse_string ),
209 OPTION_DESC ( "timeout", 't', required_argument,
210 struct choose_options, timeout, parse_timeout ),
211 OPTION_DESC ( "keep", 'k', no_argument,
212 struct choose_options, keep, parse_flag ),
215 /** "choose" command descriptor */
216 static struct command_descriptor choose_cmd =
217 COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
220 * The "choose" command
222 * @v argc Argument count
223 * @v argv Argument list
224 * @ret rc Return status code
226 static int choose_exec ( int argc, char **argv ) {
227 struct choose_options opts;
228 struct named_setting setting;
230 struct menu_item *item;
234 if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
235 goto err_parse_options;
237 /* Parse setting name */
238 if ( ( rc = parse_autovivified_setting ( argv[optind],
240 goto err_parse_setting;
243 if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
247 if ( ( rc = show_menu ( menu, opts.timeout, opts.select, &item ) ) != 0)
250 /* Apply default type if necessary */
251 if ( ! setting.setting.type )
252 setting.setting.type = &setting_type_string;
255 if ( ( rc = storef_setting ( setting.settings, &setting.setting,
256 item->label ) ) != 0 ) {
257 printf ( "Could not store \"%s\": %s\n",
258 setting.setting.name, strerror ( rc ) );
267 /* Destroy menu, if applicable */
269 destroy_menu ( menu );
277 struct command menu_commands[] __command = {