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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
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>
43 FEATURE ( FEATURE_MISC
, "Menu", DHCP_EB_FEATURE_MENU
, 1 );
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
),
61 /** "menu" command descriptor */
62 static struct command_descriptor menu_cmd
=
63 COMMAND_DESC ( struct menu_options
, menu_opts
, 0, MAX_ARGUMENTS
,
69 * @v argc Argument count
70 * @v argv Argument list
71 * @ret rc Return status code
73 static int menu_exec ( int argc
, char **argv
) {
74 struct menu_options opts
;
80 if ( ( rc
= parse_options ( argc
, argv
, &menu_cmd
, &opts
) ) != 0 )
81 goto err_parse_options
;
84 title
= concat_args ( &argv
[optind
] );
91 menu
= create_menu ( opts
.name
, title
);
97 /* Destroy menu, if applicable */
99 destroy_menu ( menu
);
111 /** "item" options */
112 struct item_options
{
117 /** Use as default */
119 /** Use as a separator */
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
),
135 /** "item" command descriptor */
136 static struct command_descriptor item_cmd
=
137 COMMAND_DESC ( struct item_options
, item_opts
, 0, MAX_ARGUMENTS
,
138 "[<label> [<text>]]" );
143 * @v argc Argument count
144 * @v argv Argument list
145 * @ret rc Return status code
147 static int item_exec ( int argc
, char **argv
) {
148 struct item_options opts
;
150 struct menu_item
*item
;
156 if ( ( rc
= parse_options ( argc
, argv
, &item_cmd
, &opts
) ) != 0 )
157 goto err_parse_options
;
159 /* Parse label, if present */
161 label
= argv
[optind
++]; /* May be NULL */
163 /* Parse text, if present */
164 if ( optind
< argc
) {
165 text
= concat_args ( &argv
[optind
] );
173 if ( ( rc
= parse_menu ( opts
.menu
, &menu
) ) != 0 )
177 item
= add_menu_item ( menu
, label
, ( text ? text
: "" ),
178 opts
.key
, opts
.is_default
);
181 goto err_add_menu_item
;
195 /** "choose" options */
196 struct choose_options
{
200 unsigned long timeout
;
201 /** Default selection */
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
,
212 struct choose_options
, select
, parse_string
),
213 OPTION_DESC ( "timeout", 't', required_argument
,
214 struct choose_options
, timeout
, parse_timeout
),
215 OPTION_DESC ( "keep", 'k', no_argument
,
216 struct choose_options
, keep
, parse_flag
),
219 /** "choose" command descriptor */
220 static struct command_descriptor choose_cmd
=
221 COMMAND_DESC ( struct choose_options
, choose_opts
, 1, 1, "<setting>" );
224 * The "choose" command
226 * @v argc Argument count
227 * @v argv Argument list
228 * @ret rc Return status code
230 static int choose_exec ( int argc
, char **argv
) {
231 struct choose_options opts
;
232 struct named_setting setting
;
234 struct menu_item
*item
;
238 if ( ( rc
= parse_options ( argc
, argv
, &choose_cmd
, &opts
) ) != 0 )
239 goto err_parse_options
;
241 /* Parse setting name */
242 if ( ( rc
= parse_autovivified_setting ( argv
[optind
],
244 goto err_parse_setting
;
247 if ( ( rc
= parse_menu ( opts
.menu
, &menu
) ) != 0 )
251 if ( ( rc
= show_menu ( menu
, opts
.timeout
, opts
.select
, &item
) ) != 0)
254 /* Apply default type if necessary */
255 if ( ! setting
.setting
.type
)
256 setting
.setting
.type
= &setting_type_string
;
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
) );
271 /* Destroy menu, if applicable */
273 destroy_menu ( menu
);
281 struct command menu_commands
[] __command
= {