Lines Matching +full:- +full:m

1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
46 * in m, with m, a pointer to the item, and extra being passed to callback. If
52 static inline void *menu_items_iter(struct menu *m, in menu_items_iter() argument
60 list_for_each_safe(pos, n, &m->items) { in menu_items_iter()
63 ret = callback(m, item, extra); in menu_items_iter()
77 static inline void *menu_item_print(struct menu *m, in menu_item_print() argument
81 if (!m->item_data_print) { in menu_item_print()
82 puts(item->key); in menu_item_print()
85 m->item_data_print(item->data); in menu_item_print()
95 static inline void *menu_item_destroy(struct menu *m, in menu_item_destroy() argument
99 if (item->key) in menu_item_destroy()
100 free(item->key); in menu_item_destroy()
107 __weak void menu_display_statusline(struct menu *m) in menu_display_statusline() argument
115 static inline void menu_display(struct menu *m) in menu_display() argument
117 if (m->title) { in menu_display()
118 puts(m->title); in menu_display()
121 menu_display_statusline(m); in menu_display()
123 menu_items_iter(m, menu_item_print, NULL); in menu_display()
134 static inline void *menu_item_key_match(struct menu *m, in menu_item_key_match() argument
139 if (!item_key || !item->key) { in menu_item_key_match()
140 if (item_key == item->key) in menu_item_key_match()
146 if (strcmp(item->key, item_key) == 0) in menu_item_key_match()
155 static inline struct menu_item *menu_item_by_key(struct menu *m, in menu_item_by_key() argument
158 return menu_items_iter(m, menu_item_key_match, item_key); in menu_item_by_key()
163 * set, and returns 1. If no default item was set, returns -ENOENT.
165 int menu_default_choice(struct menu *m, void **choice) in menu_default_choice() argument
167 if (m->default_item) { in menu_default_choice()
168 *choice = m->default_item->data; in menu_default_choice()
172 return -ENOENT; in menu_default_choice()
182 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
184 static inline int menu_interactive_choice(struct menu *m, void **choice) in menu_interactive_choice() argument
193 menu_display(m); in menu_interactive_choice()
195 if (!m->item_choice) { in menu_interactive_choice()
197 cbuf, m->timeout); in menu_interactive_choice()
200 choice_item = menu_item_by_key(m, cbuf); in menu_interactive_choice()
203 } else if (readret == -1) { in menu_interactive_choice()
205 return -EINTR; in menu_interactive_choice()
207 return menu_default_choice(m, choice); in menu_interactive_choice()
210 char *key = m->item_choice(m->item_choice_data); in menu_interactive_choice()
213 choice_item = menu_item_by_key(m, key); in menu_interactive_choice()
217 m->timeout = 0; in menu_interactive_choice()
220 *choice = choice_item->data; in menu_interactive_choice()
226 * menu_default_set() - Sets the default choice for the menu. This is safe to
229 * m - Points to a menu created by menu_create().
231 * item_key - Points to a string that, when compared using strcmp, matches the
234 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
237 int menu_default_set(struct menu *m, char *item_key) in menu_default_set() argument
241 if (!m) in menu_default_set()
242 return -EINVAL; in menu_default_set()
244 item = menu_item_by_key(m, item_key); in menu_default_set()
247 return -ENOENT; in menu_default_set()
249 m->default_item = item; in menu_default_set()
255 * menu_get_choice() - Returns the user's selected menu entry, or the default
259 * m - Points to a menu created by menu_create().
261 * choice - Points to a location that will store a pointer to the selected
265 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
267 * expires, or -EINTR if the user exits the menu via ^c.
269 int menu_get_choice(struct menu *m, void **choice) in menu_get_choice() argument
271 if (!m || !choice) in menu_get_choice()
272 return -EINVAL; in menu_get_choice()
274 if (!m->prompt) in menu_get_choice()
275 return menu_default_choice(m, choice); in menu_get_choice()
277 return menu_interactive_choice(m, choice); in menu_get_choice()
281 * menu_item_add() - Adds or replaces a menu item. Note that this replaces 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
291 * item_data - An opaque pointer associated with an item. It is never
295 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
298 int menu_item_add(struct menu *m, char *item_key, void *item_data) in menu_item_add() argument
302 if (!m) in menu_item_add()
303 return -EINVAL; in menu_item_add()
305 item = menu_item_by_key(m, item_key); in menu_item_add()
308 item->data = item_data; in menu_item_add()
314 return -ENOMEM; in menu_item_add()
316 item->key = strdup(item_key); in menu_item_add()
318 if (!item->key) { in menu_item_add()
320 return -ENOMEM; in menu_item_add()
323 item->data = item_data; in menu_item_add()
325 list_add_tail(&item->list, &m->items); in menu_item_add()
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
337 * timeout - A delay in seconds to wait for user input. If 0, timeout is
340 * prompt - If 0, don't ask for user input unless there is an interrupted
344 * item_data_print - If not NULL, will be called for each item when the menu
350 * item_choice - If not NULL, will be called when asking the user to choose an
354 * item_choice_data - Will be passed as the argument to the item_choice function
364 struct menu *m; in menu_create() local
366 m = malloc(sizeof *m); in menu_create()
368 if (!m) in menu_create()
371 m->default_item = NULL; in menu_create()
372 m->prompt = prompt; in menu_create()
373 m->timeout = timeout; in menu_create()
374 m->item_data_print = item_data_print; in menu_create()
375 m->item_choice = item_choice; in menu_create()
376 m->item_choice_data = item_choice_data; in menu_create()
379 m->title = strdup(title); in menu_create()
380 if (!m->title) { in menu_create()
381 free(m); in menu_create()
385 m->title = NULL; in menu_create()
388 INIT_LIST_HEAD(&m->items); in menu_create()
390 return m; in menu_create()
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) in menu_destroy() argument
402 if (!m) in menu_destroy()
403 return -EINVAL; in menu_destroy()
405 menu_items_iter(m, menu_item_destroy, NULL); in menu_destroy()
407 if (m->title) in menu_destroy()
408 free(m->title); in menu_destroy()
410 free(m); in menu_destroy()