2 * Copyright 2010-2011 Calxeda, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/list.h>
15 * Internally, each item in a menu is represented by a struct menu_item.
17 * These items will be alloc'd and initialized by menu_item_add and destroyed
18 * by menu_item_destroy, and the consumer of the interface never sees that
19 * this struct is used at all.
24 struct list_head list;
28 * The menu is composed of a list of items along with settings and callbacks
29 * provided by the user. An incomplete definition of this struct is available
30 * in menu.h, but the full definition is here to prevent consumers from
31 * relying on its contents.
34 struct menu_item *default_item;
38 void (*item_data_print)(void *);
39 char *(*item_choice)(void *);
40 void *item_choice_data;
41 struct list_head items;
45 * An iterator function for menu items. callback will be called for each item
46 * in m, with m, a pointer to the item, and extra being passed to callback. If
47 * callback returns a value other than NULL, iteration stops and the value
48 * return by callback is returned from menu_items_iter. This allows it to be
49 * used for search type operations. It is also safe for callback to remove the
50 * item from the list of items.
52 static inline void *menu_items_iter(struct menu *m,
53 void *(*callback)(struct menu *, struct menu_item *, void *),
56 struct list_head *pos, *n;
57 struct menu_item *item;
60 list_for_each_safe(pos, n, &m->items) {
61 item = list_entry(pos, struct menu_item, list);
63 ret = callback(m, item, extra);
73 * Print a menu_item. If the consumer provided an item_data_print function
74 * when creating the menu, call it with a pointer to the item's private data.
75 * Otherwise, print the key of the item.
77 static inline void *menu_item_print(struct menu *m,
78 struct menu_item *item,
81 if (!m->item_data_print) {
85 m->item_data_print(item->data);
92 * Free the memory used by a menu item. This includes the memory used by its
95 static inline void *menu_item_destroy(struct menu *m,
96 struct menu_item *item,
107 void __menu_display_statusline(struct menu *m)
111 void menu_display_statusline(struct menu *m)
112 __attribute__ ((weak, alias("__menu_display_statusline")));
115 * Display a menu so the user can make a choice of an item. First display its
116 * title, if any, and then each item in the menu.
118 static inline void menu_display(struct menu *m)
124 menu_display_statusline(m);
126 menu_items_iter(m, menu_item_print, NULL);
130 * Check if an item's key matches a provided string, pointed to by extra. If
131 * extra is NULL, an item with a NULL key will match. Otherwise, the item's
132 * key has to match according to strcmp.
134 * This is called via menu_items_iter, so it returns a pointer to the item if
135 * the key matches, and returns NULL otherwise.
137 static inline void *menu_item_key_match(struct menu *m,
138 struct menu_item *item, void *extra)
140 char *item_key = extra;
142 if (!item_key || !item->key) {
143 if (item_key == item->key)
149 if (strcmp(item->key, item_key) == 0)
156 * Find the first item with a key matching item_key, if any exists.
158 static inline struct menu_item *menu_item_by_key(struct menu *m,
161 return menu_items_iter(m, menu_item_key_match, item_key);
165 * Set *choice to point to the default item's data, if any default item was
166 * set, and returns 1. If no default item was set, returns -ENOENT.
168 int menu_default_choice(struct menu *m, void **choice)
170 if (m->default_item) {
171 *choice = m->default_item->data;
179 * Displays the menu and asks the user to choose an item. *choice will point
180 * to the private data of the item the user chooses. The user makes a choice
181 * by inputting a string matching the key of an item. Invalid choices will
182 * cause the user to be prompted again, repeatedly, until the user makes a
183 * valid choice. The user can exit the menu without making a choice via ^c.
185 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
187 static inline int menu_interactive_choice(struct menu *m, void **choice)
189 char cbuf[CONFIG_SYS_CBSIZE];
190 struct menu_item *choice_item = NULL;
193 while (!choice_item) {
198 if (!m->item_choice) {
199 readret = readline_into_buffer("Enter choice: ", cbuf,
203 choice_item = menu_item_by_key(m, cbuf);
205 printf("%s not found\n", cbuf);
207 return menu_default_choice(m, choice);
210 char *key = m->item_choice(m->item_choice_data);
213 choice_item = menu_item_by_key(m, key);
220 *choice = choice_item->data;
226 * menu_default_set() - Sets the default choice for the menu. This is safe to
227 * call more than once on a menu.
229 * m - Points to a menu created by menu_create().
231 * item_key - Points to a string that, when compared using strcmp, matches the
232 * key for an existing item in the menu.
234 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
235 * key matching item_key is found.
237 int menu_default_set(struct menu *m, char *item_key)
239 struct menu_item *item;
244 item = menu_item_by_key(m, item_key);
249 m->default_item = item;
255 * menu_get_choice() - Returns the user's selected menu entry, or the default
256 * if the menu is set to not prompt or the timeout expires. This is safe to
257 * call more than once.
259 * m - Points to a menu created by menu_create().
261 * choice - Points to a location that will store a pointer to the selected
262 * menu item. If no item is selected or there is an error, no value will be
263 * written at the location it points to.
265 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
266 * default has been set and the menu is set to not prompt or the timeout
267 * expires, or -EINTR if the user exits the menu via ^c.
269 int menu_get_choice(struct menu *m, void **choice)
275 return menu_default_choice(m, choice);
277 return menu_interactive_choice(m, choice);
281 * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
282 * data of an item if it already exists, but doesn't change the order of the
285 * m - Points to a menu created by menu_create().
287 * item_key - Points to a string that will uniquely identify the item. The
288 * string will be copied to internal storage, and is safe to discard after
289 * passing to menu_item_add.
291 * item_data - An opaque pointer associated with an item. It is never
292 * dereferenced internally, but will be passed to the item_data_print, and
293 * will be returned from menu_get_choice if the menu item is selected.
295 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
296 * insufficient memory to add the menu item.
298 int menu_item_add(struct menu *m, char *item_key, void *item_data)
300 struct menu_item *item;
305 item = menu_item_by_key(m, item_key);
308 item->data = item_data;
312 item = malloc(sizeof *item);
316 item->key = strdup(item_key);
323 item->data = item_data;
325 list_add_tail(&item->list, &m->items);
331 * menu_create() - Creates a menu handle with default settings
333 * title - If not NULL, points to a string that will be displayed before the
334 * list of menu items. It will be copied to internal storage, and is safe to
335 * discard after passing to menu_create().
337 * timeout - A delay in seconds to wait for user input. If 0, timeout is
338 * disabled, and the default choice will be returned unless prompt is 1.
340 * prompt - If 0, don't ask for user input unless there is an interrupted
341 * timeout. If 1, the user will be prompted for input regardless of the value
344 * item_data_print - If not NULL, will be called for each item when the menu
345 * is displayed, with the pointer to the item's data passed as the argument.
346 * If NULL, each item's key will be printed instead. Since an item's key is
347 * what must be entered to select an item, the item_data_print function should
348 * make it obvious what the key for each entry is.
350 * item_choice - If not NULL, will be called when asking the user to choose an
351 * item. Returns a key string corresponding to the choosen item or NULL if
352 * no item has been selected.
354 * item_choice_data - Will be passed as the argument to the item_choice function
356 * Returns a pointer to the menu if successful, or NULL if there is
357 * insufficient memory available to create the menu.
359 struct menu *menu_create(char *title, int timeout, int prompt,
360 void (*item_data_print)(void *),
361 char *(*item_choice)(void *),
362 void *item_choice_data)
366 m = malloc(sizeof *m);
371 m->default_item = NULL;
373 m->timeout = timeout;
374 m->item_data_print = item_data_print;
375 m->item_choice = item_choice;
376 m->item_choice_data = item_choice_data;
379 m->title = strdup(title);
388 INIT_LIST_HEAD(&m->items);
394 * menu_destroy() - frees the memory used by a menu and its items.
396 * m - Points to a menu created by menu_create().
398 * Returns 1 if successful, or -EINVAL if m is NULL.
400 int menu_destroy(struct menu *m)
405 menu_items_iter(m, menu_item_destroy, NULL);