]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/menu.c
common: add possibility for readline_into_buffer timeout
[karo-tx-uboot.git] / common / menu.c
1 /*
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  */
32 struct 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  */
44 struct menu {
45         struct menu_item *default_item;
46         int timeout;
47         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  */
61 static 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  */
86 static inline void *menu_item_print(struct menu *m,
87                                 struct menu_item *item,
88                                 void *extra)
89 {
90         if (!m->item_data_print) {
91                 puts(item->key);
92                 putc('\n');
93         } else {
94                 m->item_data_print(item->data);
95         }
96
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  */
104 static 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
116 /*
117  * Display a menu so the user can make a choice of an item. First display its
118  * title, if any, and then each item in the menu.
119  */
120 static inline void menu_display(struct menu *m)
121 {
122         if (m->title) {
123                 puts(m->title);
124                 putc('\n');
125         }
126
127         menu_items_iter(m, menu_item_print, NULL);
128 }
129
130 /*
131  * Check if an item's key matches a provided string, pointed to by extra. If
132  * extra is NULL, an item with a NULL key will match. Otherwise, the item's
133  * key has to match according to strcmp.
134  *
135  * This is called via menu_items_iter, so it returns a pointer to the item if
136  * the key matches, and returns NULL otherwise.
137  */
138 static inline void *menu_item_key_match(struct menu *m,
139                         struct menu_item *item, void *extra)
140 {
141         char *item_key = extra;
142
143         if (!item_key || !item->key) {
144                 if (item_key == item->key)
145                         return item;
146
147                 return NULL;
148         }
149
150         if (strcmp(item->key, item_key) == 0)
151                 return item;
152
153         return NULL;
154 }
155
156 /*
157  * Find the first item with a key matching item_key, if any exists.
158  */
159 static inline struct menu_item *menu_item_by_key(struct menu *m,
160                                                         char *item_key)
161 {
162         return menu_items_iter(m, menu_item_key_match, item_key);
163 }
164
165 /*
166  * Wait for the user to hit a key according to the timeout set for the menu.
167  * Returns 1 if the user hit a key, or 0 if the timeout expired.
168  */
169 static inline int menu_interrupted(struct menu *m)
170 {
171         if (!m->timeout)
172                 return 0;
173
174         if (abortboot(m->timeout/10))
175                 return 1;
176
177         return 0;
178 }
179
180 /*
181  * Checks whether or not the default menu item should be used without
182  * prompting for a user choice. If the menu is set to always prompt, or the
183  * user hits a key during the timeout period, return 0. Otherwise, return 1 to
184  * indicate we should use the default menu item.
185  */
186 static inline int menu_use_default(struct menu *m)
187 {
188         return !m->prompt && !menu_interrupted(m);
189 }
190
191 /*
192  * Set *choice to point to the default item's data, if any default item was
193  * set, and returns 1. If no default item was set, returns -ENOENT.
194  */
195 static inline int menu_default_choice(struct menu *m, void **choice)
196 {
197         if (m->default_item) {
198                 *choice = m->default_item->data;
199                 return 1;
200         }
201
202         return -ENOENT;
203 }
204
205 /*
206  * Displays the menu and asks the user to choose an item. *choice will point
207  * to the private data of the item the user chooses. The user makes a choice
208  * by inputting a string matching the key of an item. Invalid choices will
209  * cause the user to be prompted again, repeatedly, until the user makes a
210  * valid choice. The user can exit the menu without making a choice via ^c.
211  *
212  * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
213  */
214 static inline int menu_interactive_choice(struct menu *m, void **choice)
215 {
216         char cbuf[CONFIG_SYS_CBSIZE];
217         struct menu_item *choice_item = NULL;
218         int readret;
219
220         while (!choice_item) {
221                 cbuf[0] = '\0';
222
223                 menu_display(m);
224
225                 readret = readline_into_buffer("Enter choice: ", cbuf,
226                                 m->timeout);
227
228                 if (readret >= 0) {
229                         choice_item = menu_item_by_key(m, cbuf);
230
231                         if (!choice_item)
232                                 printf("%s not found\n", cbuf);
233                 } else {
234                         puts("^C\n");
235                         return -EINTR;
236                 }
237         }
238
239         *choice = choice_item->data;
240
241         return 1;
242 }
243
244 /*
245  * menu_default_set() - Sets the default choice for the menu. This is safe to
246  * call more than once on a menu.
247  *
248  * m - Points to a menu created by menu_create().
249  *
250  * item_key - Points to a string that, when compared using strcmp, matches the
251  * key for an existing item in the menu.
252  *
253  * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
254  * key matching item_key is found.
255  */
256 int menu_default_set(struct menu *m, char *item_key)
257 {
258         struct menu_item *item;
259
260         if (!m)
261                 return -EINVAL;
262
263         item = menu_item_by_key(m, item_key);
264
265         if (!item)
266                 return -ENOENT;
267
268         m->default_item = item;
269
270         return 1;
271 }
272
273 /*
274  * menu_get_choice() - Returns the user's selected menu entry, or the default
275  * if the menu is set to not prompt or the timeout expires. This is safe to
276  * call more than once.
277  *
278  * m - Points to a menu created by menu_create().
279  *
280  * choice - Points to a location that will store a pointer to the selected
281  * menu item. If no item is selected or there is an error, no value will be
282  * written at the location it points to.
283  *
284  * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
285  * default has been set and the menu is set to not prompt or the timeout
286  * expires, or -EINTR if the user exits the menu via ^c.
287  */
288 int menu_get_choice(struct menu *m, void **choice)
289 {
290         if (!m || !choice)
291                 return -EINVAL;
292
293         if (menu_use_default(m))
294                 return menu_default_choice(m, choice);
295
296         return menu_interactive_choice(m, choice);
297 }
298
299 /*
300  * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
301  * data of an item if it already exists, but doesn't change the order of the
302  * item.
303  *
304  * m - Points to a menu created by menu_create().
305  *
306  * item_key - Points to a string that will uniquely identify the item.  The
307  * string will be copied to internal storage, and is safe to discard after
308  * passing to menu_item_add.
309  *
310  * item_data - An opaque pointer associated with an item. It is never
311  * dereferenced internally, but will be passed to the item_data_print, and
312  * will be returned from menu_get_choice if the menu item is selected.
313  *
314  * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
315  * insufficient memory to add the menu item.
316  */
317 int menu_item_add(struct menu *m, char *item_key, void *item_data)
318 {
319         struct menu_item *item;
320
321         if (!m)
322                 return -EINVAL;
323
324         item = menu_item_by_key(m, item_key);
325
326         if (item) {
327                 item->data = item_data;
328                 return 1;
329         }
330
331         item = malloc(sizeof *item);
332         if (!item)
333                 return -ENOMEM;
334
335         item->key = strdup(item_key);
336
337         if (!item->key) {
338                 free(item);
339                 return -ENOMEM;
340         }
341
342         item->data = item_data;
343
344         list_add_tail(&item->list, &m->items);
345
346         return 1;
347 }
348
349 /*
350  * menu_create() - Creates a menu handle with default settings
351  *
352  * title - If not NULL, points to a string that will be displayed before the
353  * list of menu items. It will be copied to internal storage, and is safe to
354  * discard after passing to menu_create().
355  *
356  * timeout - A delay in seconds to wait for user input. If 0, timeout is
357  * disabled, and the default choice will be returned unless prompt is 1.
358  *
359  * prompt - If 0, don't ask for user input unless there is an interrupted
360  * timeout. If 1, the user will be prompted for input regardless of the value
361  * of timeout.
362  *
363  * item_data_print - If not NULL, will be called for each item when the menu
364  * is displayed, with the pointer to the item's data passed as the argument.
365  * If NULL, each item's key will be printed instead.  Since an item's key is
366  * what must be entered to select an item, the item_data_print function should
367  * make it obvious what the key for each entry is.
368  *
369  * Returns a pointer to the menu if successful, or NULL if there is
370  * insufficient memory available to create the menu.
371  */
372 struct menu *menu_create(char *title, int timeout, int prompt,
373                                 void (*item_data_print)(void *))
374 {
375         struct menu *m;
376
377         m = malloc(sizeof *m);
378
379         if (!m)
380                 return NULL;
381
382         m->default_item = NULL;
383         m->prompt = prompt;
384         m->timeout = timeout;
385         m->item_data_print = item_data_print;
386
387         if (title) {
388                 m->title = strdup(title);
389                 if (!m->title) {
390                         free(m);
391                         return NULL;
392                 }
393         } else
394                 m->title = NULL;
395
396
397         INIT_LIST_HEAD(&m->items);
398
399         return m;
400 }
401
402 /*
403  * menu_destroy() - frees the memory used by a menu and its items.
404  *
405  * m - Points to a menu created by menu_create().
406  *
407  * Returns 1 if successful, or -EINVAL if m is NULL.
408  */
409 int menu_destroy(struct menu *m)
410 {
411         if (!m)
412                 return -EINVAL;
413
414         menu_items_iter(m, menu_item_destroy, NULL);
415
416         if (m->title)
417                 free(m->title);
418
419         free(m);
420
421         return 1;
422 }