xref: /openbmc/u-boot/common/menu.c (revision 8379c799)
1 /*
2  * Copyright 2010-2011 Calxeda, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
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 };
44 
45 /*
46  * An iterator function for menu items. callback will be called for each item
47  * in m, with m, a pointer to the item, and extra being passed to callback. If
48  * callback returns a value other than NULL, iteration stops and the value
49  * return by callback is returned from menu_items_iter.  This allows it to be
50  * used for search type operations. It is also safe for callback to remove the
51  * item from the list of items.
52  */
53 static inline void *menu_items_iter(struct menu *m,
54 		void *(*callback)(struct menu *, struct menu_item *, void *),
55 		void *extra)
56 {
57 	struct list_head *pos, *n;
58 	struct menu_item *item;
59 	void *ret;
60 
61 	list_for_each_safe(pos, n, &m->items) {
62 		item = list_entry(pos, struct menu_item, list);
63 
64 		ret = callback(m, item, extra);
65 
66 		if (ret)
67 			return ret;
68 	}
69 
70 	return NULL;
71 }
72 
73 /*
74  * Print a menu_item. If the consumer provided an item_data_print function
75  * when creating the menu, call it with a pointer to the item's private data.
76  * Otherwise, print the key of the item.
77  */
78 static inline void *menu_item_print(struct menu *m,
79 				struct menu_item *item,
80 				void *extra)
81 {
82 	if (!m->item_data_print) {
83 		puts(item->key);
84 		putc('\n');
85 	} else {
86 		m->item_data_print(item->data);
87 	}
88 
89 	return NULL;
90 }
91 
92 /*
93  * Free the memory used by a menu item. This includes the memory used by its
94  * key.
95  */
96 static inline void *menu_item_destroy(struct menu *m,
97 				struct menu_item *item,
98 				void *extra)
99 {
100 	if (item->key)
101 		free(item->key);
102 
103 	free(item);
104 
105 	return NULL;
106 }
107 
108 __weak void menu_display_statusline(struct menu *m)
109 {
110 }
111 
112 /*
113  * Display a menu so the user can make a choice of an item. First display its
114  * title, if any, and then each item in the menu.
115  */
116 static inline void menu_display(struct menu *m)
117 {
118 	if (m->title) {
119 		puts(m->title);
120 		putc('\n');
121 	}
122 	menu_display_statusline(m);
123 
124 	menu_items_iter(m, menu_item_print, NULL);
125 }
126 
127 /*
128  * Check if an item's key matches a provided string, pointed to by extra. If
129  * extra is NULL, an item with a NULL key will match. Otherwise, the item's
130  * key has to match according to strcmp.
131  *
132  * This is called via menu_items_iter, so it returns a pointer to the item if
133  * the key matches, and returns NULL otherwise.
134  */
135 static inline void *menu_item_key_match(struct menu *m,
136 			struct menu_item *item, void *extra)
137 {
138 	char *item_key = extra;
139 
140 	if (!item_key || !item->key) {
141 		if (item_key == item->key)
142 			return item;
143 
144 		return NULL;
145 	}
146 
147 	if (strcmp(item->key, item_key) == 0)
148 		return item;
149 
150 	return NULL;
151 }
152 
153 /*
154  * Find the first item with a key matching item_key, if any exists.
155  */
156 static inline struct menu_item *menu_item_by_key(struct menu *m,
157 							char *item_key)
158 {
159 	return menu_items_iter(m, menu_item_key_match, item_key);
160 }
161 
162 /*
163  * Set *choice to point to the default item's data, if any default item was
164  * set, and returns 1. If no default item was set, returns -ENOENT.
165  */
166 int menu_default_choice(struct menu *m, void **choice)
167 {
168 	if (m->default_item) {
169 		*choice = m->default_item->data;
170 		return 1;
171 	}
172 
173 	return -ENOENT;
174 }
175 
176 /*
177  * Displays the menu and asks the user to choose an item. *choice will point
178  * to the private data of the item the user chooses. The user makes a choice
179  * by inputting a string matching the key of an item. Invalid choices will
180  * cause the user to be prompted again, repeatedly, until the user makes a
181  * valid choice. The user can exit the menu without making a choice via ^c.
182  *
183  * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
184  */
185 static inline int menu_interactive_choice(struct menu *m, void **choice)
186 {
187 	char cbuf[CONFIG_SYS_CBSIZE];
188 	struct menu_item *choice_item = NULL;
189 	int readret;
190 
191 	while (!choice_item) {
192 		cbuf[0] = '\0';
193 
194 		menu_display(m);
195 
196 		if (!m->item_choice) {
197 			readret = cli_readline_into_buffer("Enter choice: ",
198 							   cbuf,
199 							   m->timeout / 10);
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  */
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  */
271 int menu_get_choice(struct menu *m, void **choice)
272 {
273 	if (!m || !choice)
274 		return -EINVAL;
275 
276 	if (!m->prompt)
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  */
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 
329 	return 1;
330 }
331 
332 /*
333  * menu_create() - Creates a menu handle with default settings
334  *
335  * title - If not NULL, points to a string that will be displayed before the
336  * list of menu items. It will be copied to internal storage, and is safe to
337  * discard after passing to menu_create().
338  *
339  * timeout - A delay in seconds to wait for user input. If 0, timeout is
340  * disabled, and the default choice will be returned unless prompt is 1.
341  *
342  * prompt - If 0, don't ask for user input unless there is an interrupted
343  * timeout. If 1, the user will be prompted for input regardless of the value
344  * of timeout.
345  *
346  * item_data_print - If not NULL, will be called for each item when the menu
347  * is displayed, with the pointer to the item's data passed as the argument.
348  * If NULL, each item's key will be printed instead.  Since an item's key is
349  * what must be entered to select an item, the item_data_print function should
350  * make it obvious what the key for each entry is.
351  *
352  * item_choice - If not NULL, will be called when asking the user to choose an
353  * item. Returns a key string corresponding to the choosen item or NULL if
354  * no item has been selected.
355  *
356  * item_choice_data - Will be passed as the argument to the item_choice function
357  *
358  * Returns a pointer to the menu if successful, or NULL if there is
359  * insufficient memory available to create the menu.
360  */
361 struct menu *menu_create(char *title, int timeout, int prompt,
362 				void (*item_data_print)(void *),
363 				char *(*item_choice)(void *),
364 				void *item_choice_data)
365 {
366 	struct menu *m;
367 
368 	m = malloc(sizeof *m);
369 
370 	if (!m)
371 		return NULL;
372 
373 	m->default_item = NULL;
374 	m->prompt = prompt;
375 	m->timeout = timeout;
376 	m->item_data_print = item_data_print;
377 	m->item_choice = item_choice;
378 	m->item_choice_data = item_choice_data;
379 
380 	if (title) {
381 		m->title = strdup(title);
382 		if (!m->title) {
383 			free(m);
384 			return NULL;
385 		}
386 	} else
387 		m->title = NULL;
388 
389 
390 	INIT_LIST_HEAD(&m->items);
391 
392 	return m;
393 }
394 
395 /*
396  * menu_destroy() - frees the memory used by a menu and its items.
397  *
398  * m - Points to a menu created by menu_create().
399  *
400  * Returns 1 if successful, or -EINVAL if m is NULL.
401  */
402 int menu_destroy(struct menu *m)
403 {
404 	if (!m)
405 		return -EINVAL;
406 
407 	menu_items_iter(m, menu_item_destroy, NULL);
408 
409 	if (m->title)
410 		free(m->title);
411 
412 	free(m);
413 
414 	return 1;
415 }
416