• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2010-2011 Calxeda, Inc.
4  * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
5  */
6 
7 #include <common.h>
8 #include <cli.h>
9 #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  */
22 struct 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  */
34 struct menu {
35 	struct menu_item *default_item;
36 	int timeout;
37 	char *title;
38 	int prompt;
39 	void (*item_data_print)(void *);
40 	char *(*item_choice)(void *);
41 	void *item_choice_data;
42 	struct list_head items;
43 	int item_cnt;
44 };
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  */
menu_items_iter(struct menu * m,void * (* callback)(struct menu *,struct menu_item *,void *),void * extra)54 static 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  */
menu_item_print(struct menu * m,struct menu_item * item,void * extra)79 static inline void *menu_item_print(struct menu *m,
80 				struct menu_item *item,
81 				void *extra)
82 {
83 	if (!m->item_data_print) {
84 		puts(item->key);
85 		putc('\n');
86 	} else {
87 		m->item_data_print(item->data);
88 	}
89 
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  */
menu_item_destroy(struct menu * m,struct menu_item * item,void * extra)97 static 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 
menu_display_statusline(struct menu * m)109 __weak void menu_display_statusline(struct menu *m)
110 {
111 }
112 
113 /*
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  */
menu_display(struct menu * m)117 static inline void menu_display(struct menu *m)
118 {
119 	if (m->title) {
120 		puts(m->title);
121 		putc('\n');
122 	}
123 	menu_display_statusline(m);
124 
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  */
menu_item_key_match(struct menu * m,struct menu_item * item,void * extra)136 static 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  */
menu_item_by_key(struct menu * m,char * item_key)157 static 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 /*
164  * 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  */
menu_default_choice(struct menu * m,void ** choice)167 int menu_default_choice(struct menu *m, void **choice)
168 {
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  */
menu_interactive_choice(struct menu * m,void ** choice)186 static 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 
197 		if (!m->item_choice) {
198 			readret = cli_readline_into_buffer("Enter choice: ",
199 							   cbuf, m->timeout);
200 
201 			if (readret >= 0) {
202 				choice_item = menu_item_by_key(m, cbuf);
203 				if (!choice_item)
204 					printf("%s not found\n", cbuf);
205 			} else if (readret == -1)  {
206 				printf("<INTERRUPT>\n");
207 				return -EINTR;
208 			} else {
209 				return menu_default_choice(m, choice);
210 			}
211 		} 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;
220 	}
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  */
menu_default_set(struct menu * m,char * item_key)239 int 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
258  * if the menu is set to not prompt or the timeout expires. This is safe to
259  * call more than once.
260  *
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
268  * 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.
270  */
menu_get_choice(struct menu * m,void ** choice)271 int menu_get_choice(struct menu *m, void **choice)
272 {
273 	if (!m || !choice)
274 		return -EINVAL;
275 
276 	if (!m->prompt || m->item_cnt == 1)
277 		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  */
menu_item_add(struct menu * m,char * item_key,void * item_data)300 int 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);
328 	m->item_cnt++;
329 
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  *
340  * 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.
346  *
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  *
353  * item_choice - If not NULL, will be called when asking the user to choose an
354  * item. Returns a key string corresponding to the chosen item or NULL if
355  * no item has been selected.
356  *
357  * item_choice_data - Will be passed as the argument to the item_choice function
358  *
359  * Returns a pointer to the menu if successful, or NULL if there is
360  * insufficient memory available to create the menu.
361  */
menu_create(char * title,int timeout,int prompt,void (* item_data_print)(void *),char * (* item_choice)(void *),void * item_choice_data)362 struct menu *menu_create(char *title, int timeout, int prompt,
363 				void (*item_data_print)(void *),
364 				char *(*item_choice)(void *),
365 				void *item_choice_data)
366 {
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;
376 	m->timeout = timeout;
377 	m->item_data_print = item_data_print;
378 	m->item_choice = item_choice;
379 	m->item_choice_data = item_choice_data;
380 	m->item_cnt = 0;
381 
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  */
menu_destroy(struct menu * m)404 int 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 }
418