2 * Copyright 2010-2011 Calxeda, Inc.
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)
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
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/>.
21 #include <linux/list.h>
26 * Internally, each item in a menu is represented by a struct menu_item.
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.
35 struct list_head list;
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.
45 struct menu_item *default_item;
48 void (*item_data_print)(void *);
49 struct list_head items;
53 * An iterator function for menu items. callback will be called for each item
54 * in m, with m, a pointer to the item, and extra being passed to callback. If
55 * callback returns a value other than NULL, iteration stops and the value
56 * return by callback is returned from menu_items_iter. This allows it to be
57 * used for search type operations. It is also safe for callback to remove the
58 * item from the list of items.
60 static inline void *menu_items_iter(struct menu *m,
61 void *(*callback)(struct menu *, struct menu_item *, void *),
64 struct list_head *pos, *n;
65 struct menu_item *item;
68 list_for_each_safe(pos, n, &m->items) {
69 item = list_entry(pos, struct menu_item, list);
71 ret = callback(m, item, extra);
81 * Print a menu_item. If the consumer provided an item_data_print function
82 * when creating the menu, call it with a pointer to the item's private data.
83 * Otherwise, print the key of the item.
85 static inline void *menu_item_print(struct menu *m,
86 struct menu_item *item,
89 if (!m->item_data_print)
90 printf("%s\n", item->key);
92 m->item_data_print(item->data);
98 * Free the memory used by a menu item. This includes the memory used by its
101 static inline void *menu_item_destroy(struct menu *m,
102 struct menu_item *item,
114 * Display a menu so the user can make a choice of an item. First display its
115 * title, if any, and then each item in the menu.
117 static inline void menu_display(struct menu *m)
120 printf("%s:\n", m->title);
122 menu_items_iter(m, menu_item_print, NULL);
126 * Check if an item's key matches a provided string, pointed to by extra. If
127 * extra is NULL, an item with a NULL key will match. Otherwise, the item's
128 * key has to match according to strcmp.
130 * This is called via menu_items_iter, so it returns a pointer to the item if
131 * the key matches, and returns NULL otherwise.
133 static inline void *menu_item_key_match(struct menu *m,
134 struct menu_item *item, void *extra)
136 char *item_key = extra;
138 if (!item_key || !item->key) {
139 if (item_key == item->key)
145 if (strcmp(item->key, item_key) == 0)
152 * Find the first item with a key matching item_key, if any exists.
154 static inline struct menu_item *menu_item_by_key(struct menu *m,
157 return menu_items_iter(m, menu_item_key_match, item_key);
161 * Checks whether or not the default menu item should be used without
162 * prompting for a user choice. If the menu is set to always prompt, return
163 * 0. Otherwise, return 1 to indicate we should use the default menu item.
165 static inline int menu_use_default(struct menu *m)
171 * Set *choice to point to the default item's data, if any default item was
172 * set, and returns 1. If no default item was set, returns -ENOENT.
174 static inline int menu_default_choice(struct menu *m, void **choice)
176 if (m->default_item) {
177 *choice = m->default_item->data;
185 * Displays the menu and asks the user to choose an item. *choice will point
186 * to the private data of the item the user chooses. The user makes a choice
187 * by inputting a string matching the key of an item. Invalid choices will
188 * cause the user to be prompted again, repeatedly, until the user makes a
189 * valid choice. The user can exit the menu without making a choice via ^c.
191 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
193 static inline int menu_interactive_choice(struct menu *m, void **choice)
195 char cbuf[CONFIG_SYS_CBSIZE];
196 struct menu_item *choice_item = NULL;
199 while (!choice_item) {
204 readret = readline_into_buffer("Enter choice: ", cbuf);
207 choice_item = menu_item_by_key(m, cbuf);
210 printf("%s not found\n", cbuf);
217 *choice = choice_item->data;
223 * menu_default_set() - Sets the default choice for the menu. This is safe to
224 * call more than once on a menu.
226 * m - Points to a menu created by menu_create().
228 * item_key - Points to a string that, when compared using strcmp, matches the
229 * key for an existing item in the menu.
231 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
232 * key matching item_key is found.
234 int menu_default_set(struct menu *m, char *item_key)
236 struct menu_item *item;
241 item = menu_item_by_key(m, item_key);
246 m->default_item = item;
252 * menu_get_choice() - Returns the user's selected menu entry, or the default
253 * if the menu is set to not prompt. This is safe to call more than once.
255 * m - Points to a menu created by menu_create().
257 * choice - Points to a location that will store a pointer to the selected
258 * menu item. If no item is selected or there is an error, no value will be
259 * written at the location it points to.
261 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
262 * default has been set and the menu is set to not prompt, or -EINTR if the
263 * user exits the menu via ^c.
265 int menu_get_choice(struct menu *m, void **choice)
270 if (menu_use_default(m))
271 return menu_default_choice(m, choice);
273 return menu_interactive_choice(m, choice);
277 * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
278 * data of an item if it already exists, but doesn't change the order of the
281 * m - Points to a menu created by menu_create().
283 * item_key - Points to a string that will uniquely identify the item. The
284 * string will be copied to internal storage, and is safe to discard after
285 * passing to menu_item_add.
287 * item_data - An opaque pointer associated with an item. It is never
288 * dereferenced internally, but will be passed to the item_data_print, and
289 * will be returned from menu_get_choice if the menu item is selected.
291 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
292 * insufficient memory to add the menu item.
294 int menu_item_add(struct menu *m, char *item_key, void *item_data)
296 struct menu_item *item;
301 item = menu_item_by_key(m, item_key);
304 item->data = item_data;
308 item = malloc(sizeof *item);
312 item->key = strdup(item_key);
319 item->data = item_data;
321 list_add_tail(&item->list, &m->items);
327 * menu_create() - Creates a menu handle with default settings
329 * title - If not NULL, points to a string that will be displayed before the
330 * list of menu items. It will be copied to internal storage, and is safe to
331 * discard after passing to menu_create().
333 * prompt - If 0, don't ask for user input.
335 * item_data_print - If not NULL, will be called for each item when the menu
336 * is displayed, with the pointer to the item's data passed as the argument.
337 * If NULL, each item's key will be printed instead. Since an item's key is
338 * what must be entered to select an item, the item_data_print function should
339 * make it obvious what the key for each entry is.
341 * Returns a pointer to the menu if successful, or NULL if there is
342 * insufficient memory available to create the menu.
344 struct menu *menu_create(char *title, int prompt,
345 void (*item_data_print)(void *))
349 m = malloc(sizeof *m);
354 m->default_item = NULL;
356 m->item_data_print = item_data_print;
359 m->title = strdup(title);
368 INIT_LIST_HEAD(&m->items);
374 * menu_destroy() - frees the memory used by a menu and its items.
376 * m - Points to a menu created by menu_create().
378 * Returns 1 if successful, or -EINVAL if m is NULL.
380 int menu_destroy(struct menu *m)
385 menu_items_iter(m, menu_item_destroy, NULL);