These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / menu_cmd.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * Menu commands
29  *
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <getopt.h>
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>
42
43 FEATURE ( FEATURE_MISC, "Menu", DHCP_EB_FEATURE_MENU, 1 );
44
45 /** "menu" options */
46 struct menu_options {
47         /** Name */
48         char *name;
49         /** Delete */
50         int delete;
51 };
52
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 ),
59 };
60
61 /** "menu" command descriptor */
62 static struct command_descriptor menu_cmd =
63         COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
64                        "[<title>]" );
65
66 /**
67  * The "menu" command
68  *
69  * @v argc              Argument count
70  * @v argv              Argument list
71  * @ret rc              Return status code
72  */
73 static int menu_exec ( int argc, char **argv ) {
74         struct menu_options opts;
75         struct menu *menu;
76         char *title;
77         int rc;
78
79         /* Parse options */
80         if ( ( rc = parse_options ( argc, argv, &menu_cmd, &opts ) ) != 0 )
81                 goto err_parse_options;
82
83         /* Parse title */
84         title = concat_args ( &argv[optind] );
85         if ( ! title ) {
86                 rc = -ENOMEM;
87                 goto err_parse_title;
88         }
89
90         /* Create menu */
91         menu = create_menu ( opts.name, title );
92         if ( ! menu ) {
93                 rc = -ENOMEM;
94                 goto err_create_menu;
95         }
96
97         /* Destroy menu, if applicable */
98         if ( opts.delete )
99                 destroy_menu ( menu );
100
101         /* Success */
102         rc = 0;
103
104  err_create_menu:
105         free ( title );
106  err_parse_title:
107  err_parse_options:
108         return rc;
109 }
110
111 /** "item" options */
112 struct item_options {
113         /** Menu name */
114         char *menu;
115         /** Shortcut key */
116         unsigned int key;
117         /** Use as default */
118         int is_default;
119         /** Use as a separator */
120         int is_gap;
121 };
122
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 ),
133 };
134
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>]]" );
139
140 /**
141  * The "item" command
142  *
143  * @v argc              Argument count
144  * @v argv              Argument list
145  * @ret rc              Return status code
146  */
147 static int item_exec ( int argc, char **argv ) {
148         struct item_options opts;
149         struct menu *menu;
150         struct menu_item *item;
151         char *label = NULL;
152         char *text = NULL;
153         int rc;
154
155         /* Parse options */
156         if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
157                 goto err_parse_options;
158
159         /* Parse label, if present */
160         if ( ! opts.is_gap )
161                 label = argv[optind++]; /* May be NULL */
162
163         /* Parse text, if present */
164         if ( optind < argc ) {
165                 text = concat_args ( &argv[optind] );
166                 if ( ! text ) {
167                         rc = -ENOMEM;
168                         goto err_parse_text;
169                 }
170         }
171
172         /* Identify menu */
173         if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
174                 goto err_parse_menu;
175
176         /* Add menu item */
177         item = add_menu_item ( menu, label, ( text ? text : "" ),
178                                opts.key, opts.is_default );
179         if ( ! item ) {
180                 rc = -ENOMEM;
181                 goto err_add_menu_item;
182         }
183
184         /* Success */
185         rc = 0;
186
187  err_add_menu_item:
188  err_parse_menu:
189         free ( text );
190  err_parse_text:
191  err_parse_options:
192         return rc;
193 }
194
195 /** "choose" options */
196 struct choose_options {
197         /** Menu name */
198         char *menu;
199         /** Timeout */
200         unsigned long timeout;
201         /** Default selection */
202         char *select;
203         /** Keep menu */
204         int keep;
205 };
206
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 ),
217 };
218
219 /** "choose" command descriptor */
220 static struct command_descriptor choose_cmd =
221         COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
222
223 /**
224  * The "choose" command
225  *
226  * @v argc              Argument count
227  * @v argv              Argument list
228  * @ret rc              Return status code
229  */
230 static int choose_exec ( int argc, char **argv ) {
231         struct choose_options opts;
232         struct named_setting setting;
233         struct menu *menu;
234         struct menu_item *item;
235         int rc;
236
237         /* Parse options */
238         if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
239                 goto err_parse_options;
240
241         /* Parse setting name */
242         if ( ( rc = parse_autovivified_setting ( argv[optind],
243                                                  &setting ) ) != 0 )
244                 goto err_parse_setting;
245
246         /* Identify menu */
247         if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
248                 goto err_parse_menu;
249
250         /* Show menu */
251         if ( ( rc = show_menu ( menu, opts.timeout, opts.select, &item ) ) != 0)
252                 goto err_show_menu;
253
254         /* Apply default type if necessary */
255         if ( ! setting.setting.type )
256                 setting.setting.type = &setting_type_string;
257
258         /* Store setting */
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 ) );
263                 goto err_store;
264         }
265
266         /* Success */
267         rc = 0;
268
269  err_store:
270  err_show_menu:
271         /* Destroy menu, if applicable */
272         if ( ! opts.keep )
273                 destroy_menu ( menu );
274  err_parse_menu:
275  err_parse_setting:
276  err_parse_options:
277         return rc;
278 }
279
280 /** Menu commands */
281 struct command menu_commands[] __command = {
282         {
283                 .name = "menu",
284                 .exec = menu_exec,
285         },
286         {
287                 .name = "item",
288                 .exec = item_exec,
289         },
290         {
291                 .name = "choose",
292                 .exec = choose_exec,
293         },
294 };