blob: aa16c9a199d772f7bd9971e401efdb883bf06895 [file] [log] [blame]
Jason Hobbsb69bf522011-08-23 11:06:49 +00001/*
2 * Copyright 2010-2011 Calxeda, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <common.h>
19#include <malloc.h>
20#include <errno.h>
21#include <linux/list.h>
22
23#include "menu.h"
24
25/*
26 * Internally, each item in a menu is represented by a struct menu_item.
27 *
28 * These items will be alloc'd and initialized by menu_item_add and destroyed
29 * by menu_item_destroy, and the consumer of the interface never sees that
30 * this struct is used at all.
31 */
32struct menu_item {
33 char *key;
34 void *data;
35 struct list_head list;
36};
37
38/*
39 * The menu is composed of a list of items along with settings and callbacks
40 * provided by the user. An incomplete definition of this struct is available
41 * in menu.h, but the full definition is here to prevent consumers from
42 * relying on its contents.
43 */
44struct menu {
45 struct menu_item *default_item;
Jason Hobbsb41bc5a2011-08-23 11:06:50 +000046 int timeout;
Jason Hobbsb69bf522011-08-23 11:06:49 +000047 char *title;
48 int prompt;
49 void (*item_data_print)(void *);
50 struct list_head items;
51};
52
53/*
54 * An iterator function for menu items. callback will be called for each item
55 * in m, with m, a pointer to the item, and extra being passed to callback. If
56 * callback returns a value other than NULL, iteration stops and the value
57 * return by callback is returned from menu_items_iter. This allows it to be
58 * used for search type operations. It is also safe for callback to remove the
59 * item from the list of items.
60 */
61static inline void *menu_items_iter(struct menu *m,
62 void *(*callback)(struct menu *, struct menu_item *, void *),
63 void *extra)
64{
65 struct list_head *pos, *n;
66 struct menu_item *item;
67 void *ret;
68
69 list_for_each_safe(pos, n, &m->items) {
70 item = list_entry(pos, struct menu_item, list);
71
72 ret = callback(m, item, extra);
73
74 if (ret)
75 return ret;
76 }
77
78 return NULL;
79}
80
81/*
82 * Print a menu_item. If the consumer provided an item_data_print function
83 * when creating the menu, call it with a pointer to the item's private data.
84 * Otherwise, print the key of the item.
85 */
86static inline void *menu_item_print(struct menu *m,
87 struct menu_item *item,
88 void *extra)
89{
Wolfgang Denkd887ad52011-11-28 20:19:41 +010090 if (!m->item_data_print) {
Anatolij Gustschin21574972011-12-03 06:46:07 +000091 puts(item->key);
Wolfgang Denkd887ad52011-11-28 20:19:41 +010092 putc('\n');
93 } else {
Jason Hobbsb69bf522011-08-23 11:06:49 +000094 m->item_data_print(item->data);
Wolfgang Denkd887ad52011-11-28 20:19:41 +010095 }
Jason Hobbsb69bf522011-08-23 11:06:49 +000096
97 return NULL;
98}
99
100/*
101 * Free the memory used by a menu item. This includes the memory used by its
102 * key.
103 */
104static inline void *menu_item_destroy(struct menu *m,
105 struct menu_item *item,
106 void *extra)
107{
108 if (item->key)
109 free(item->key);
110
111 free(item);
112
113 return NULL;
114}
115
Heiko Schochere0611dd2012-01-16 21:13:20 +0000116void __menu_display_statusline(struct menu *m)
117{
118 return;
119}
120void menu_display_statusline(struct menu *m)
121 __attribute__ ((weak, alias("__menu_display_statusline")));
122
Jason Hobbsb69bf522011-08-23 11:06:49 +0000123/*
124 * Display a menu so the user can make a choice of an item. First display its
125 * title, if any, and then each item in the menu.
126 */
127static inline void menu_display(struct menu *m)
128{
Wolfgang Denkd887ad52011-11-28 20:19:41 +0100129 if (m->title) {
130 puts(m->title);
131 putc('\n');
132 }
Heiko Schochere0611dd2012-01-16 21:13:20 +0000133 menu_display_statusline(m);
Jason Hobbsb69bf522011-08-23 11:06:49 +0000134
135 menu_items_iter(m, menu_item_print, NULL);
136}
137
138/*
139 * Check if an item's key matches a provided string, pointed to by extra. If
140 * extra is NULL, an item with a NULL key will match. Otherwise, the item's
141 * key has to match according to strcmp.
142 *
143 * This is called via menu_items_iter, so it returns a pointer to the item if
144 * the key matches, and returns NULL otherwise.
145 */
146static inline void *menu_item_key_match(struct menu *m,
147 struct menu_item *item, void *extra)
148{
149 char *item_key = extra;
150
151 if (!item_key || !item->key) {
152 if (item_key == item->key)
153 return item;
154
155 return NULL;
156 }
157
158 if (strcmp(item->key, item_key) == 0)
159 return item;
160
161 return NULL;
162}
163
164/*
165 * Find the first item with a key matching item_key, if any exists.
166 */
167static inline struct menu_item *menu_item_by_key(struct menu *m,
168 char *item_key)
169{
170 return menu_items_iter(m, menu_item_key_match, item_key);
171}
172
173/*
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000174 * Wait for the user to hit a key according to the timeout set for the menu.
175 * Returns 1 if the user hit a key, or 0 if the timeout expired.
176 */
177static inline int menu_interrupted(struct menu *m)
178{
179 if (!m->timeout)
180 return 0;
181
182 if (abortboot(m->timeout/10))
183 return 1;
184
185 return 0;
186}
187
188/*
Jason Hobbsb69bf522011-08-23 11:06:49 +0000189 * Checks whether or not the default menu item should be used without
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000190 * prompting for a user choice. If the menu is set to always prompt, or the
191 * user hits a key during the timeout period, return 0. Otherwise, return 1 to
192 * indicate we should use the default menu item.
Jason Hobbsb69bf522011-08-23 11:06:49 +0000193 */
194static inline int menu_use_default(struct menu *m)
195{
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000196 return !m->prompt && !menu_interrupted(m);
Jason Hobbsb69bf522011-08-23 11:06:49 +0000197}
198
199/*
200 * Set *choice to point to the default item's data, if any default item was
201 * set, and returns 1. If no default item was set, returns -ENOENT.
202 */
203static inline int menu_default_choice(struct menu *m, void **choice)
204{
205 if (m->default_item) {
206 *choice = m->default_item->data;
207 return 1;
208 }
209
210 return -ENOENT;
211}
212
213/*
214 * Displays the menu and asks the user to choose an item. *choice will point
215 * to the private data of the item the user chooses. The user makes a choice
216 * by inputting a string matching the key of an item. Invalid choices will
217 * cause the user to be prompted again, repeatedly, until the user makes a
218 * valid choice. The user can exit the menu without making a choice via ^c.
219 *
220 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
221 */
222static inline int menu_interactive_choice(struct menu *m, void **choice)
223{
224 char cbuf[CONFIG_SYS_CBSIZE];
225 struct menu_item *choice_item = NULL;
226 int readret;
227
228 while (!choice_item) {
229 cbuf[0] = '\0';
230
231 menu_display(m);
232
Heiko Schocher9c348312012-01-16 21:13:05 +0000233 readret = readline_into_buffer("Enter choice: ", cbuf,
234 m->timeout);
Jason Hobbsb69bf522011-08-23 11:06:49 +0000235
236 if (readret >= 0) {
237 choice_item = menu_item_by_key(m, cbuf);
238
Heiko Schocherfc4fa6a2012-01-16 22:24:29 +0000239 if (!choice_item) {
Jason Hobbsb69bf522011-08-23 11:06:49 +0000240 printf("%s not found\n", cbuf);
Heiko Schocherfc4fa6a2012-01-16 22:24:29 +0000241 m->timeout = 0;
242 }
Jason Hobbsb69bf522011-08-23 11:06:49 +0000243 } else {
Wolfgang Denkd887ad52011-11-28 20:19:41 +0100244 puts("^C\n");
Jason Hobbsb69bf522011-08-23 11:06:49 +0000245 return -EINTR;
246 }
247 }
248
249 *choice = choice_item->data;
250
251 return 1;
252}
253
254/*
255 * menu_default_set() - Sets the default choice for the menu. This is safe to
256 * call more than once on a menu.
257 *
258 * m - Points to a menu created by menu_create().
259 *
260 * item_key - Points to a string that, when compared using strcmp, matches the
261 * key for an existing item in the menu.
262 *
263 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
264 * key matching item_key is found.
265 */
266int menu_default_set(struct menu *m, char *item_key)
267{
268 struct menu_item *item;
269
270 if (!m)
271 return -EINVAL;
272
273 item = menu_item_by_key(m, item_key);
274
275 if (!item)
276 return -ENOENT;
277
278 m->default_item = item;
279
280 return 1;
281}
282
283/*
284 * menu_get_choice() - Returns the user's selected menu entry, or the default
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000285 * if the menu is set to not prompt or the timeout expires. This is safe to
286 * call more than once.
Jason Hobbsb69bf522011-08-23 11:06:49 +0000287 *
288 * m - Points to a menu created by menu_create().
289 *
290 * choice - Points to a location that will store a pointer to the selected
291 * menu item. If no item is selected or there is an error, no value will be
292 * written at the location it points to.
293 *
294 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000295 * default has been set and the menu is set to not prompt or the timeout
296 * expires, or -EINTR if the user exits the menu via ^c.
Jason Hobbsb69bf522011-08-23 11:06:49 +0000297 */
298int menu_get_choice(struct menu *m, void **choice)
299{
300 if (!m || !choice)
301 return -EINVAL;
302
303 if (menu_use_default(m))
304 return menu_default_choice(m, choice);
305
306 return menu_interactive_choice(m, choice);
307}
308
309/*
310 * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
311 * data of an item if it already exists, but doesn't change the order of the
312 * item.
313 *
314 * m - Points to a menu created by menu_create().
315 *
316 * item_key - Points to a string that will uniquely identify the item. The
317 * string will be copied to internal storage, and is safe to discard after
318 * passing to menu_item_add.
319 *
320 * item_data - An opaque pointer associated with an item. It is never
321 * dereferenced internally, but will be passed to the item_data_print, and
322 * will be returned from menu_get_choice if the menu item is selected.
323 *
324 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
325 * insufficient memory to add the menu item.
326 */
327int menu_item_add(struct menu *m, char *item_key, void *item_data)
328{
329 struct menu_item *item;
330
331 if (!m)
332 return -EINVAL;
333
334 item = menu_item_by_key(m, item_key);
335
336 if (item) {
337 item->data = item_data;
338 return 1;
339 }
340
341 item = malloc(sizeof *item);
342 if (!item)
343 return -ENOMEM;
344
345 item->key = strdup(item_key);
346
347 if (!item->key) {
348 free(item);
349 return -ENOMEM;
350 }
351
352 item->data = item_data;
353
354 list_add_tail(&item->list, &m->items);
355
356 return 1;
357}
358
359/*
360 * menu_create() - Creates a menu handle with default settings
361 *
362 * title - If not NULL, points to a string that will be displayed before the
363 * list of menu items. It will be copied to internal storage, and is safe to
364 * discard after passing to menu_create().
365 *
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000366 * timeout - A delay in seconds to wait for user input. If 0, timeout is
367 * disabled, and the default choice will be returned unless prompt is 1.
368 *
369 * prompt - If 0, don't ask for user input unless there is an interrupted
370 * timeout. If 1, the user will be prompted for input regardless of the value
371 * of timeout.
Jason Hobbsb69bf522011-08-23 11:06:49 +0000372 *
373 * item_data_print - If not NULL, will be called for each item when the menu
374 * is displayed, with the pointer to the item's data passed as the argument.
375 * If NULL, each item's key will be printed instead. Since an item's key is
376 * what must be entered to select an item, the item_data_print function should
377 * make it obvious what the key for each entry is.
378 *
379 * Returns a pointer to the menu if successful, or NULL if there is
380 * insufficient memory available to create the menu.
381 */
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000382struct menu *menu_create(char *title, int timeout, int prompt,
Jason Hobbsb69bf522011-08-23 11:06:49 +0000383 void (*item_data_print)(void *))
384{
385 struct menu *m;
386
387 m = malloc(sizeof *m);
388
389 if (!m)
390 return NULL;
391
392 m->default_item = NULL;
393 m->prompt = prompt;
Jason Hobbsb41bc5a2011-08-23 11:06:50 +0000394 m->timeout = timeout;
Jason Hobbsb69bf522011-08-23 11:06:49 +0000395 m->item_data_print = item_data_print;
396
397 if (title) {
398 m->title = strdup(title);
399 if (!m->title) {
400 free(m);
401 return NULL;
402 }
403 } else
404 m->title = NULL;
405
406
407 INIT_LIST_HEAD(&m->items);
408
409 return m;
410}
411
412/*
413 * menu_destroy() - frees the memory used by a menu and its items.
414 *
415 * m - Points to a menu created by menu_create().
416 *
417 * Returns 1 if successful, or -EINVAL if m is NULL.
418 */
419int menu_destroy(struct menu *m)
420{
421 if (!m)
422 return -EINVAL;
423
424 menu_items_iter(m, menu_item_destroy, NULL);
425
426 if (m->title)
427 free(m->title);
428
429 free(m);
430
431 return 1;
432}