iPXE
menu_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Menu commands
00029  *
00030  */
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <errno.h>
00036 #include <getopt.h>
00037 #include <ipxe/menu.h>
00038 #include <ipxe/command.h>
00039 #include <ipxe/parseopt.h>
00040 #include <ipxe/settings.h>
00041 #include <ipxe/features.h>
00042 
00043 FEATURE ( FEATURE_MISC, "Menu", DHCP_EB_FEATURE_MENU, 1 );
00044 
00045 /** "menu" options */
00046 struct menu_options {
00047         /** Name */
00048         char *name;
00049         /** Delete */
00050         int delete;
00051 };
00052 
00053 /** "menu" option list */
00054 static struct option_descriptor menu_opts[] = {
00055         OPTION_DESC ( "name", 'n', required_argument,
00056                       struct menu_options, name, parse_string ),
00057         OPTION_DESC ( "delete", 'd', no_argument,
00058                       struct menu_options, delete, parse_flag ),
00059 };
00060 
00061 /** "menu" command descriptor */
00062 static struct command_descriptor menu_cmd =
00063         COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
00064                        "[<title>]" );
00065 
00066 /**
00067  * The "menu" command
00068  *
00069  * @v argc              Argument count
00070  * @v argv              Argument list
00071  * @ret rc              Return status code
00072  */
00073 static int menu_exec ( int argc, char **argv ) {
00074         struct menu_options opts;
00075         struct menu *menu;
00076         char *title;
00077         int rc;
00078 
00079         /* Parse options */
00080         if ( ( rc = parse_options ( argc, argv, &menu_cmd, &opts ) ) != 0 )
00081                 goto err_parse_options;
00082 
00083         /* Parse title */
00084         title = concat_args ( &argv[optind] );
00085         if ( ! title ) {
00086                 rc = -ENOMEM;
00087                 goto err_parse_title;
00088         }
00089 
00090         /* Create menu */
00091         menu = create_menu ( opts.name, title );
00092         if ( ! menu ) {
00093                 rc = -ENOMEM;
00094                 goto err_create_menu;
00095         }
00096 
00097         /* Destroy menu, if applicable */
00098         if ( opts.delete )
00099                 destroy_menu ( menu );
00100 
00101         /* Success */
00102         rc = 0;
00103 
00104  err_create_menu:
00105         free ( title );
00106  err_parse_title:
00107  err_parse_options:
00108         return rc;
00109 }
00110 
00111 /** "item" options */
00112 struct item_options {
00113         /** Menu name */
00114         char *menu;
00115         /** Shortcut key */
00116         unsigned int key;
00117         /** Use as default */
00118         int is_default;
00119         /** Use as a separator */
00120         int is_gap;
00121 };
00122 
00123 /** "item" option list */
00124 static struct option_descriptor item_opts[] = {
00125         OPTION_DESC ( "menu", 'm', required_argument,
00126                       struct item_options, menu, parse_string ),
00127         OPTION_DESC ( "key", 'k', required_argument,
00128                       struct item_options, key, parse_key ),
00129         OPTION_DESC ( "default", 'd', no_argument,
00130                       struct item_options, is_default, parse_flag ),
00131         OPTION_DESC ( "gap", 'g', no_argument,
00132                       struct item_options, is_gap, parse_flag ),
00133 };
00134 
00135 /** "item" command descriptor */
00136 static struct command_descriptor item_cmd =
00137         COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
00138                        "[<label> [<text>]]" );
00139 
00140 /**
00141  * The "item" command
00142  *
00143  * @v argc              Argument count
00144  * @v argv              Argument list
00145  * @ret rc              Return status code
00146  */
00147 static int item_exec ( int argc, char **argv ) {
00148         struct item_options opts;
00149         struct menu *menu;
00150         struct menu_item *item;
00151         char *label = NULL;
00152         char *text = NULL;
00153         int rc;
00154 
00155         /* Parse options */
00156         if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
00157                 goto err_parse_options;
00158 
00159         /* Parse label, if present */
00160         if ( ! opts.is_gap )
00161                 label = argv[optind++]; /* May be NULL */
00162 
00163         /* Parse text, if present */
00164         if ( optind < argc ) {
00165                 text = concat_args ( &argv[optind] );
00166                 if ( ! text ) {
00167                         rc = -ENOMEM;
00168                         goto err_parse_text;
00169                 }
00170         }
00171 
00172         /* Identify menu */
00173         if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
00174                 goto err_parse_menu;
00175 
00176         /* Add menu item */
00177         item = add_menu_item ( menu, label, ( text ? text : "" ),
00178                                opts.key, opts.is_default );
00179         if ( ! item ) {
00180                 rc = -ENOMEM;
00181                 goto err_add_menu_item;
00182         }
00183 
00184         /* Success */
00185         rc = 0;
00186 
00187  err_add_menu_item:
00188  err_parse_menu:
00189         free ( text );
00190  err_parse_text:
00191  err_parse_options:
00192         return rc;
00193 }
00194 
00195 /** "choose" options */
00196 struct choose_options {
00197         /** Menu name */
00198         char *menu;
00199         /** Timeout */
00200         unsigned long timeout;
00201         /** Default selection */
00202         char *select;
00203         /** Keep menu */
00204         int keep;
00205 };
00206 
00207 /** "choose" option list */
00208 static struct option_descriptor choose_opts[] = {
00209         OPTION_DESC ( "menu", 'm', required_argument,
00210                       struct choose_options, menu, parse_string ),
00211         OPTION_DESC ( "default", 'd', required_argument,
00212                       struct choose_options, select, parse_string ),
00213         OPTION_DESC ( "timeout", 't', required_argument,
00214                       struct choose_options, timeout, parse_timeout ),
00215         OPTION_DESC ( "keep", 'k', no_argument,
00216                       struct choose_options, keep, parse_flag ),
00217 };
00218 
00219 /** "choose" command descriptor */
00220 static struct command_descriptor choose_cmd =
00221         COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
00222 
00223 /**
00224  * The "choose" command
00225  *
00226  * @v argc              Argument count
00227  * @v argv              Argument list
00228  * @ret rc              Return status code
00229  */
00230 static int choose_exec ( int argc, char **argv ) {
00231         struct choose_options opts;
00232         struct named_setting setting;
00233         struct menu *menu;
00234         struct menu_item *item;
00235         int rc;
00236 
00237         /* Parse options */
00238         if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
00239                 goto err_parse_options;
00240 
00241         /* Parse setting name */
00242         if ( ( rc = parse_autovivified_setting ( argv[optind],
00243                                                  &setting ) ) != 0 )
00244                 goto err_parse_setting;
00245 
00246         /* Identify menu */
00247         if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
00248                 goto err_parse_menu;
00249 
00250         /* Show menu */
00251         if ( ( rc = show_menu ( menu, opts.timeout, opts.select, &item ) ) != 0)
00252                 goto err_show_menu;
00253 
00254         /* Apply default type if necessary */
00255         if ( ! setting.setting.type )
00256                 setting.setting.type = &setting_type_string;
00257 
00258         /* Store setting */
00259         if ( ( rc = storef_setting ( setting.settings, &setting.setting,
00260                                      item->label ) ) != 0 ) {
00261                 printf ( "Could not store \"%s\": %s\n",
00262                          setting.setting.name, strerror ( rc ) );
00263                 goto err_store;
00264         }
00265 
00266         /* Success */
00267         rc = 0;
00268 
00269  err_store:
00270  err_show_menu:
00271         /* Destroy menu, if applicable */
00272         if ( ! opts.keep )
00273                 destroy_menu ( menu );
00274  err_parse_menu:
00275  err_parse_setting:
00276  err_parse_options:
00277         return rc;
00278 }
00279 
00280 /** Menu commands */
00281 struct command menu_commands[] __command = {
00282         {
00283                 .name = "menu",
00284                 .exec = menu_exec,
00285         },
00286         {
00287                 .name = "item",
00288                 .exec = item_exec,
00289         },
00290         {
00291                 .name = "choose",
00292                 .exec = choose_exec,
00293         },
00294 };