david/ipxe
Archived
1
0

[menu] Add "--default" option to "choose" command

Suggested-by: Robin Smidsrød <robin@smidsrod.no>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2012-04-28 23:18:55 +01:00
parent f8bb40b002
commit 838a76a042
3 changed files with 16 additions and 7 deletions

View File

@ -194,6 +194,8 @@ struct choose_options {
const char *menu; const char *menu;
/** Timeout */ /** Timeout */
unsigned int timeout; unsigned int timeout;
/** Default selection */
const char *select;
/** Keep menu */ /** Keep menu */
int keep; int keep;
}; };
@ -202,6 +204,8 @@ struct choose_options {
static struct option_descriptor choose_opts[] = { static struct option_descriptor choose_opts[] = {
OPTION_DESC ( "menu", 'm', required_argument, OPTION_DESC ( "menu", 'm', required_argument,
struct choose_options, menu, parse_string ), struct choose_options, menu, parse_string ),
OPTION_DESC ( "default", 'd', required_argument,
struct choose_options, select, parse_string ),
OPTION_DESC ( "timeout", 't', required_argument, OPTION_DESC ( "timeout", 't', required_argument,
struct choose_options, timeout, parse_integer ), struct choose_options, timeout, parse_integer ),
OPTION_DESC ( "keep", 'k', no_argument, OPTION_DESC ( "keep", 'k', no_argument,
@ -211,8 +215,8 @@ static struct option_descriptor choose_opts[] = {
/** "choose" command descriptor */ /** "choose" command descriptor */
static struct command_descriptor choose_cmd = static struct command_descriptor choose_cmd =
COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, COMMAND_DESC ( struct choose_options, choose_opts, 1, 1,
"[--menu <menu>] [--timeout <timeout>] [--keep] " "[--menu <menu>] [--default <label>] "
"<setting>" ); "[--timeout <timeout>] [--keep] <setting>" );
/** /**
* The "choose" command * The "choose" command
@ -240,7 +244,7 @@ static int choose_exec ( int argc, char **argv ) {
goto err_parse_menu; goto err_parse_menu;
/* Show menu */ /* Show menu */
if ( ( rc = show_menu ( menu, opts.timeout, &item ) ) != 0 ) if ( ( rc = show_menu ( menu, opts.timeout, opts.select, &item ) ) != 0)
goto err_show_menu; goto err_show_menu;
/* Store setting */ /* Store setting */

View File

@ -303,7 +303,7 @@ static int menu_loop ( struct menu_ui *ui, struct menu_item **selected ) {
* @ret rc Return status code * @ret rc Return status code
*/ */
int show_menu ( struct menu *menu, unsigned int timeout_ms, int show_menu ( struct menu *menu, unsigned int timeout_ms,
struct menu_item **selected ) { const char *select, struct menu_item **selected ) {
struct menu_item *item; struct menu_item *item;
struct menu_ui ui; struct menu_ui ui;
int labelled_count = 0; int labelled_count = 0;
@ -318,9 +318,14 @@ int show_menu ( struct menu *menu, unsigned int timeout_ms,
if ( ! labelled_count ) if ( ! labelled_count )
ui.selected = ui.count; ui.selected = ui.count;
labelled_count++; labelled_count++;
if ( select ) {
if ( strcmp ( select, item->label ) == 0 )
ui.selected = ui.count;
} else {
if ( item->is_default ) if ( item->is_default )
ui.selected = ui.count; ui.selected = ui.count;
} }
}
ui.count++; ui.count++;
} }
if ( ! labelled_count ) { if ( ! labelled_count ) {

View File

@ -44,6 +44,6 @@ extern struct menu_item * add_menu_item ( struct menu *menu, const char *label,
extern void destroy_menu ( struct menu *menu ); extern void destroy_menu ( struct menu *menu );
extern struct menu * find_menu ( const char *name ); extern struct menu * find_menu ( const char *name );
extern int show_menu ( struct menu *menu, unsigned int timeout_ms, extern int show_menu ( struct menu *menu, unsigned int timeout_ms,
struct menu_item **selected ); const char *select, struct menu_item **selected );
#endif /* _IPXE_MENU_H */ #endif /* _IPXE_MENU_H */