blob: 7b66d199a9b13054706f5886e19829a85c322d12 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0685d822011-08-23 11:06:49 +00002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Leon Yu9686a932019-06-21 12:12:39 +08004 * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs0685d822011-08-23 11:06:49 +00005 */
6
7#include <common.h>
Simon Glassdec3c012014-04-10 20:01:25 -06008#include <cli.h>
Jason Hobbs0685d822011-08-23 11:06:49 +00009#include <malloc.h>
10#include <errno.h>
11#include <linux/list.h>
12
13#include "menu.h"
14
15/*
16 * Internally, each item in a menu is represented by a struct menu_item.
17 *
18 * These items will be alloc'd and initialized by menu_item_add and destroyed
19 * by menu_item_destroy, and the consumer of the interface never sees that
20 * this struct is used at all.
21 */
22struct menu_item {
23 char *key;
24 void *data;
25 struct list_head list;
26};
27
28/*
29 * The menu is composed of a list of items along with settings and callbacks
30 * provided by the user. An incomplete definition of this struct is available
31 * in menu.h, but the full definition is here to prevent consumers from
32 * relying on its contents.
33 */
34struct menu {
35 struct menu_item *default_item;
Jason Hobbs65febe62011-08-23 11:06:50 +000036 int timeout;
Jason Hobbs0685d822011-08-23 11:06:49 +000037 char *title;
38 int prompt;
39 void (*item_data_print)(void *);
Pali Rohárd0d8d3b2013-03-23 14:50:40 +000040 char *(*item_choice)(void *);
41 void *item_choice_data;
Jason Hobbs0685d822011-08-23 11:06:49 +000042 struct list_head items;
Leon Yu9686a932019-06-21 12:12:39 +080043 int item_cnt;
Jason Hobbs0685d822011-08-23 11:06:49 +000044};
45
46/*
47 * An iterator function for menu items. callback will be called for each item
48 * in m, with m, a pointer to the item, and extra being passed to callback. If
49 * callback returns a value other than NULL, iteration stops and the value
50 * return by callback is returned from menu_items_iter. This allows it to be
51 * used for search type operations. It is also safe for callback to remove the
52 * item from the list of items.
53 */
54static inline void *menu_items_iter(struct menu *m,
55 void *(*callback)(struct menu *, struct menu_item *, void *),
56 void *extra)
57{
58 struct list_head *pos, *n;
59 struct menu_item *item;
60 void *ret;
61
62 list_for_each_safe(pos, n, &m->items) {
63 item = list_entry(pos, struct menu_item, list);
64
65 ret = callback(m, item, extra);
66
67 if (ret)
68 return ret;
69 }
70
71 return NULL;
72}
73
74/*
75 * Print a menu_item. If the consumer provided an item_data_print function
76 * when creating the menu, call it with a pointer to the item's private data.
77 * Otherwise, print the key of the item.
78 */
79static inline void *menu_item_print(struct menu *m,
80 struct menu_item *item,
81 void *extra)
82{
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +010083 if (!m->item_data_print) {
Anatolij Gustschinb50e1e02011-12-03 06:46:07 +000084 puts(item->key);
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +010085 putc('\n');
86 } else {
Jason Hobbs0685d822011-08-23 11:06:49 +000087 m->item_data_print(item->data);
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +010088 }
Jason Hobbs0685d822011-08-23 11:06:49 +000089
90 return NULL;
91}
92
93/*
94 * Free the memory used by a menu item. This includes the memory used by its
95 * key.
96 */
97static inline void *menu_item_destroy(struct menu *m,
98 struct menu_item *item,
99 void *extra)
100{
101 if (item->key)
102 free(item->key);
103
104 free(item);
105
106 return NULL;
107}
108
Jeroen Hofstee4e5f9e62014-10-08 22:57:25 +0200109__weak void menu_display_statusline(struct menu *m)
Heiko Schochereb2296f2012-01-16 21:13:20 +0000110{
Heiko Schochereb2296f2012-01-16 21:13:20 +0000111}
Heiko Schochereb2296f2012-01-16 21:13:20 +0000112
Jason Hobbs0685d822011-08-23 11:06:49 +0000113/*
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.
116 */
117static inline void menu_display(struct menu *m)
118{
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +0100119 if (m->title) {
120 puts(m->title);
121 putc('\n');
122 }
Heiko Schochereb2296f2012-01-16 21:13:20 +0000123 menu_display_statusline(m);
Jason Hobbs0685d822011-08-23 11:06:49 +0000124
125 menu_items_iter(m, menu_item_print, NULL);
126}
127
128/*
129 * Check if an item's key matches a provided string, pointed to by extra. If
130 * extra is NULL, an item with a NULL key will match. Otherwise, the item's
131 * key has to match according to strcmp.
132 *
133 * This is called via menu_items_iter, so it returns a pointer to the item if
134 * the key matches, and returns NULL otherwise.
135 */
136static inline void *menu_item_key_match(struct menu *m,
137 struct menu_item *item, void *extra)
138{
139 char *item_key = extra;
140
141 if (!item_key || !item->key) {
142 if (item_key == item->key)
143 return item;
144
145 return NULL;
146 }
147
148 if (strcmp(item->key, item_key) == 0)
149 return item;
150
151 return NULL;
152}
153
154/*
155 * Find the first item with a key matching item_key, if any exists.
156 */
157static inline struct menu_item *menu_item_by_key(struct menu *m,
158 char *item_key)
159{
160 return menu_items_iter(m, menu_item_key_match, item_key);
161}
162
163/*
Jason Hobbs0685d822011-08-23 11:06:49 +0000164 * Set *choice to point to the default item's data, if any default item was
165 * set, and returns 1. If no default item was set, returns -ENOENT.
166 */
Anatolij Gustschinfb6068a2013-03-23 14:52:04 +0000167int menu_default_choice(struct menu *m, void **choice)
Jason Hobbs0685d822011-08-23 11:06:49 +0000168{
169 if (m->default_item) {
170 *choice = m->default_item->data;
171 return 1;
172 }
173
174 return -ENOENT;
175}
176
177/*
178 * Displays the menu and asks the user to choose an item. *choice will point
179 * to the private data of the item the user chooses. The user makes a choice
180 * by inputting a string matching the key of an item. Invalid choices will
181 * cause the user to be prompted again, repeatedly, until the user makes a
182 * valid choice. The user can exit the menu without making a choice via ^c.
183 *
184 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
185 */
186static inline int menu_interactive_choice(struct menu *m, void **choice)
187{
188 char cbuf[CONFIG_SYS_CBSIZE];
189 struct menu_item *choice_item = NULL;
190 int readret;
191
192 while (!choice_item) {
193 cbuf[0] = '\0';
194
195 menu_display(m);
196
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000197 if (!m->item_choice) {
Simon Glassbe6aafc2014-04-10 20:01:27 -0600198 readret = cli_readline_into_buffer("Enter choice: ",
Masahiro Yamadac46d0fe2018-05-24 17:04:57 +0900199 cbuf, m->timeout);
Jason Hobbs0685d822011-08-23 11:06:49 +0000200
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000201 if (readret >= 0) {
202 choice_item = menu_item_by_key(m, cbuf);
203 if (!choice_item)
204 printf("%s not found\n", cbuf);
Tuomas Tynkkynen60c47af2015-05-07 21:29:19 +0300205 } else if (readret == -1) {
206 printf("<INTERRUPT>\n");
207 return -EINTR;
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000208 } else {
209 return menu_default_choice(m, choice);
Heiko Schochere795b2e2012-01-16 22:24:29 +0000210 }
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000211 } else {
212 char *key = m->item_choice(m->item_choice_data);
213
214 if (key)
215 choice_item = menu_item_by_key(m, key);
216 }
217
218 if (!choice_item)
219 m->timeout = 0;
Jason Hobbs0685d822011-08-23 11:06:49 +0000220 }
221
222 *choice = choice_item->data;
223
224 return 1;
225}
226
227/*
228 * menu_default_set() - Sets the default choice for the menu. This is safe to
229 * call more than once on a menu.
230 *
231 * m - Points to a menu created by menu_create().
232 *
233 * item_key - Points to a string that, when compared using strcmp, matches the
234 * key for an existing item in the menu.
235 *
236 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
237 * key matching item_key is found.
238 */
239int menu_default_set(struct menu *m, char *item_key)
240{
241 struct menu_item *item;
242
243 if (!m)
244 return -EINVAL;
245
246 item = menu_item_by_key(m, item_key);
247
248 if (!item)
249 return -ENOENT;
250
251 m->default_item = item;
252
253 return 1;
254}
255
256/*
257 * menu_get_choice() - Returns the user's selected menu entry, or the default
Jason Hobbs65febe62011-08-23 11:06:50 +0000258 * if the menu is set to not prompt or the timeout expires. This is safe to
259 * call more than once.
Jason Hobbs0685d822011-08-23 11:06:49 +0000260 *
261 * m - Points to a menu created by menu_create().
262 *
263 * choice - Points to a location that will store a pointer to the selected
264 * menu item. If no item is selected or there is an error, no value will be
265 * written at the location it points to.
266 *
267 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
Jason Hobbs65febe62011-08-23 11:06:50 +0000268 * default has been set and the menu is set to not prompt or the timeout
269 * expires, or -EINTR if the user exits the menu via ^c.
Jason Hobbs0685d822011-08-23 11:06:49 +0000270 */
271int menu_get_choice(struct menu *m, void **choice)
272{
273 if (!m || !choice)
274 return -EINVAL;
275
Leon Yu9686a932019-06-21 12:12:39 +0800276 if (!m->prompt || m->item_cnt == 1)
Jason Hobbs0685d822011-08-23 11:06:49 +0000277 return menu_default_choice(m, choice);
278
279 return menu_interactive_choice(m, choice);
280}
281
282/*
283 * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
284 * data of an item if it already exists, but doesn't change the order of the
285 * item.
286 *
287 * m - Points to a menu created by menu_create().
288 *
289 * item_key - Points to a string that will uniquely identify the item. The
290 * string will be copied to internal storage, and is safe to discard after
291 * passing to menu_item_add.
292 *
293 * item_data - An opaque pointer associated with an item. It is never
294 * dereferenced internally, but will be passed to the item_data_print, and
295 * will be returned from menu_get_choice if the menu item is selected.
296 *
297 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
298 * insufficient memory to add the menu item.
299 */
300int menu_item_add(struct menu *m, char *item_key, void *item_data)
301{
302 struct menu_item *item;
303
304 if (!m)
305 return -EINVAL;
306
307 item = menu_item_by_key(m, item_key);
308
309 if (item) {
310 item->data = item_data;
311 return 1;
312 }
313
314 item = malloc(sizeof *item);
315 if (!item)
316 return -ENOMEM;
317
318 item->key = strdup(item_key);
319
320 if (!item->key) {
321 free(item);
322 return -ENOMEM;
323 }
324
325 item->data = item_data;
326
327 list_add_tail(&item->list, &m->items);
Leon Yu9686a932019-06-21 12:12:39 +0800328 m->item_cnt++;
Jason Hobbs0685d822011-08-23 11:06:49 +0000329
330 return 1;
331}
332
333/*
334 * menu_create() - Creates a menu handle with default settings
335 *
336 * title - If not NULL, points to a string that will be displayed before the
337 * list of menu items. It will be copied to internal storage, and is safe to
338 * discard after passing to menu_create().
339 *
Jason Hobbs65febe62011-08-23 11:06:50 +0000340 * timeout - A delay in seconds to wait for user input. If 0, timeout is
341 * disabled, and the default choice will be returned unless prompt is 1.
342 *
343 * prompt - If 0, don't ask for user input unless there is an interrupted
344 * timeout. If 1, the user will be prompted for input regardless of the value
345 * of timeout.
Jason Hobbs0685d822011-08-23 11:06:49 +0000346 *
347 * item_data_print - If not NULL, will be called for each item when the menu
348 * is displayed, with the pointer to the item's data passed as the argument.
349 * If NULL, each item's key will be printed instead. Since an item's key is
350 * what must be entered to select an item, the item_data_print function should
351 * make it obvious what the key for each entry is.
352 *
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000353 * item_choice - If not NULL, will be called when asking the user to choose an
Alexander Merkle0137e602016-03-17 15:44:47 +0100354 * item. Returns a key string corresponding to the chosen item or NULL if
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000355 * no item has been selected.
356 *
357 * item_choice_data - Will be passed as the argument to the item_choice function
358 *
Jason Hobbs0685d822011-08-23 11:06:49 +0000359 * Returns a pointer to the menu if successful, or NULL if there is
360 * insufficient memory available to create the menu.
361 */
Jason Hobbs65febe62011-08-23 11:06:50 +0000362struct menu *menu_create(char *title, int timeout, int prompt,
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000363 void (*item_data_print)(void *),
364 char *(*item_choice)(void *),
365 void *item_choice_data)
Jason Hobbs0685d822011-08-23 11:06:49 +0000366{
367 struct menu *m;
368
369 m = malloc(sizeof *m);
370
371 if (!m)
372 return NULL;
373
374 m->default_item = NULL;
375 m->prompt = prompt;
Jason Hobbs65febe62011-08-23 11:06:50 +0000376 m->timeout = timeout;
Jason Hobbs0685d822011-08-23 11:06:49 +0000377 m->item_data_print = item_data_print;
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000378 m->item_choice = item_choice;
379 m->item_choice_data = item_choice_data;
Leon Yu9686a932019-06-21 12:12:39 +0800380 m->item_cnt = 0;
Jason Hobbs0685d822011-08-23 11:06:49 +0000381
382 if (title) {
383 m->title = strdup(title);
384 if (!m->title) {
385 free(m);
386 return NULL;
387 }
388 } else
389 m->title = NULL;
390
391
392 INIT_LIST_HEAD(&m->items);
393
394 return m;
395}
396
397/*
398 * menu_destroy() - frees the memory used by a menu and its items.
399 *
400 * m - Points to a menu created by menu_create().
401 *
402 * Returns 1 if successful, or -EINVAL if m is NULL.
403 */
404int menu_destroy(struct menu *m)
405{
406 if (!m)
407 return -EINVAL;
408
409 menu_items_iter(m, menu_item_destroy, NULL);
410
411 if (m->title)
412 free(m->title);
413
414 free(m);
415
416 return 1;
417}