xref: /openbmc/u-boot/common/menu.c (revision 63495ad7)
1 /*
2  * Copyright 2010-2011 Calxeda, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <common.h>
19 #include <malloc.h>
20 #include <errno.h>
21 #include <linux/list.h>
22 
23 #include "menu.h"
24 
25 /*
26  * Internally, each item in a menu is represented by a struct menu_item.
27  *
28  * These items will be alloc'd and initialized by menu_item_add and destroyed
29  * by menu_item_destroy, and the consumer of the interface never sees that
30  * this struct is used at all.
31  */
32 struct menu_item {
33 	char *key;
34 	void *data;
35 	struct list_head list;
36 };
37 
38 /*
39  * The menu is composed of a list of items along with settings and callbacks
40  * provided by the user. An incomplete definition of this struct is available
41  * in menu.h, but the full definition is here to prevent consumers from
42  * relying on its contents.
43  */
44 struct menu {
45 	struct menu_item *default_item;
46 	int timeout;
47 	char *title;
48 	int prompt;
49 	void (*item_data_print)(void *);
50 	char *(*item_choice)(void *);
51 	void *item_choice_data;
52 	struct list_head items;
53 };
54 
55 /*
56  * An iterator function for menu items. callback will be called for each item
57  * in m, with m, a pointer to the item, and extra being passed to callback. If
58  * callback returns a value other than NULL, iteration stops and the value
59  * return by callback is returned from menu_items_iter.  This allows it to be
60  * used for search type operations. It is also safe for callback to remove the
61  * item from the list of items.
62  */
63 static inline void *menu_items_iter(struct menu *m,
64 		void *(*callback)(struct menu *, struct menu_item *, void *),
65 		void *extra)
66 {
67 	struct list_head *pos, *n;
68 	struct menu_item *item;
69 	void *ret;
70 
71 	list_for_each_safe(pos, n, &m->items) {
72 		item = list_entry(pos, struct menu_item, list);
73 
74 		ret = callback(m, item, extra);
75 
76 		if (ret)
77 			return ret;
78 	}
79 
80 	return NULL;
81 }
82 
83 /*
84  * Print a menu_item. If the consumer provided an item_data_print function
85  * when creating the menu, call it with a pointer to the item's private data.
86  * Otherwise, print the key of the item.
87  */
88 static inline void *menu_item_print(struct menu *m,
89 				struct menu_item *item,
90 				void *extra)
91 {
92 	if (!m->item_data_print) {
93 		puts(item->key);
94 		putc('\n');
95 	} else {
96 		m->item_data_print(item->data);
97 	}
98 
99 	return NULL;
100 }
101 
102 /*
103  * Free the memory used by a menu item. This includes the memory used by its
104  * key.
105  */
106 static inline void *menu_item_destroy(struct menu *m,
107 				struct menu_item *item,
108 				void *extra)
109 {
110 	if (item->key)
111 		free(item->key);
112 
113 	free(item);
114 
115 	return NULL;
116 }
117 
118 void __menu_display_statusline(struct menu *m)
119 {
120 	return;
121 }
122 void menu_display_statusline(struct menu *m)
123 	__attribute__ ((weak, alias("__menu_display_statusline")));
124 
125 /*
126  * Display a menu so the user can make a choice of an item. First display its
127  * title, if any, and then each item in the menu.
128  */
129 static inline void menu_display(struct menu *m)
130 {
131 	if (m->title) {
132 		puts(m->title);
133 		putc('\n');
134 	}
135 	menu_display_statusline(m);
136 
137 	menu_items_iter(m, menu_item_print, NULL);
138 }
139 
140 /*
141  * Check if an item's key matches a provided string, pointed to by extra. If
142  * extra is NULL, an item with a NULL key will match. Otherwise, the item's
143  * key has to match according to strcmp.
144  *
145  * This is called via menu_items_iter, so it returns a pointer to the item if
146  * the key matches, and returns NULL otherwise.
147  */
148 static inline void *menu_item_key_match(struct menu *m,
149 			struct menu_item *item, void *extra)
150 {
151 	char *item_key = extra;
152 
153 	if (!item_key || !item->key) {
154 		if (item_key == item->key)
155 			return item;
156 
157 		return NULL;
158 	}
159 
160 	if (strcmp(item->key, item_key) == 0)
161 		return item;
162 
163 	return NULL;
164 }
165 
166 /*
167  * Find the first item with a key matching item_key, if any exists.
168  */
169 static inline struct menu_item *menu_item_by_key(struct menu *m,
170 							char *item_key)
171 {
172 	return menu_items_iter(m, menu_item_key_match, item_key);
173 }
174 
175 /*
176  * Set *choice to point to the default item's data, if any default item was
177  * set, and returns 1. If no default item was set, returns -ENOENT.
178  */
179 int menu_default_choice(struct menu *m, void **choice)
180 {
181 	if (m->default_item) {
182 		*choice = m->default_item->data;
183 		return 1;
184 	}
185 
186 	return -ENOENT;
187 }
188 
189 /*
190  * Displays the menu and asks the user to choose an item. *choice will point
191  * to the private data of the item the user chooses. The user makes a choice
192  * by inputting a string matching the key of an item. Invalid choices will
193  * cause the user to be prompted again, repeatedly, until the user makes a
194  * valid choice. The user can exit the menu without making a choice via ^c.
195  *
196  * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
197  */
198 static inline int menu_interactive_choice(struct menu *m, void **choice)
199 {
200 	char cbuf[CONFIG_SYS_CBSIZE];
201 	struct menu_item *choice_item = NULL;
202 	int readret;
203 
204 	while (!choice_item) {
205 		cbuf[0] = '\0';
206 
207 		menu_display(m);
208 
209 		if (!m->item_choice) {
210 			readret = readline_into_buffer("Enter choice: ", cbuf,
211 					m->timeout / 10);
212 
213 			if (readret >= 0) {
214 				choice_item = menu_item_by_key(m, cbuf);
215 				if (!choice_item)
216 					printf("%s not found\n", cbuf);
217 			} else {
218 				return menu_default_choice(m, choice);
219 			}
220 		} else {
221 			char *key = m->item_choice(m->item_choice_data);
222 
223 			if (key)
224 				choice_item = menu_item_by_key(m, key);
225 		}
226 
227 		if (!choice_item)
228 			m->timeout = 0;
229 	}
230 
231 	*choice = choice_item->data;
232 
233 	return 1;
234 }
235 
236 /*
237  * menu_default_set() - Sets the default choice for the menu. This is safe to
238  * call more than once on a menu.
239  *
240  * m - Points to a menu created by menu_create().
241  *
242  * item_key - Points to a string that, when compared using strcmp, matches the
243  * key for an existing item in the menu.
244  *
245  * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
246  * key matching item_key is found.
247  */
248 int menu_default_set(struct menu *m, char *item_key)
249 {
250 	struct menu_item *item;
251 
252 	if (!m)
253 		return -EINVAL;
254 
255 	item = menu_item_by_key(m, item_key);
256 
257 	if (!item)
258 		return -ENOENT;
259 
260 	m->default_item = item;
261 
262 	return 1;
263 }
264 
265 /*
266  * menu_get_choice() - Returns the user's selected menu entry, or the default
267  * if the menu is set to not prompt or the timeout expires. This is safe to
268  * call more than once.
269  *
270  * m - Points to a menu created by menu_create().
271  *
272  * choice - Points to a location that will store a pointer to the selected
273  * menu item. If no item is selected or there is an error, no value will be
274  * written at the location it points to.
275  *
276  * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
277  * default has been set and the menu is set to not prompt or the timeout
278  * expires, or -EINTR if the user exits the menu via ^c.
279  */
280 int menu_get_choice(struct menu *m, void **choice)
281 {
282 	if (!m || !choice)
283 		return -EINVAL;
284 
285 	if (!m->prompt)
286 		return menu_default_choice(m, choice);
287 
288 	return menu_interactive_choice(m, choice);
289 }
290 
291 /*
292  * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
293  * data of an item if it already exists, but doesn't change the order of the
294  * item.
295  *
296  * m - Points to a menu created by menu_create().
297  *
298  * item_key - Points to a string that will uniquely identify the item.  The
299  * string will be copied to internal storage, and is safe to discard after
300  * passing to menu_item_add.
301  *
302  * item_data - An opaque pointer associated with an item. It is never
303  * dereferenced internally, but will be passed to the item_data_print, and
304  * will be returned from menu_get_choice if the menu item is selected.
305  *
306  * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
307  * insufficient memory to add the menu item.
308  */
309 int menu_item_add(struct menu *m, char *item_key, void *item_data)
310 {
311 	struct menu_item *item;
312 
313 	if (!m)
314 		return -EINVAL;
315 
316 	item = menu_item_by_key(m, item_key);
317 
318 	if (item) {
319 		item->data = item_data;
320 		return 1;
321 	}
322 
323 	item = malloc(sizeof *item);
324 	if (!item)
325 		return -ENOMEM;
326 
327 	item->key = strdup(item_key);
328 
329 	if (!item->key) {
330 		free(item);
331 		return -ENOMEM;
332 	}
333 
334 	item->data = item_data;
335 
336 	list_add_tail(&item->list, &m->items);
337 
338 	return 1;
339 }
340 
341 /*
342  * menu_create() - Creates a menu handle with default settings
343  *
344  * title - If not NULL, points to a string that will be displayed before the
345  * list of menu items. It will be copied to internal storage, and is safe to
346  * discard after passing to menu_create().
347  *
348  * timeout - A delay in seconds to wait for user input. If 0, timeout is
349  * disabled, and the default choice will be returned unless prompt is 1.
350  *
351  * prompt - If 0, don't ask for user input unless there is an interrupted
352  * timeout. If 1, the user will be prompted for input regardless of the value
353  * of timeout.
354  *
355  * item_data_print - If not NULL, will be called for each item when the menu
356  * is displayed, with the pointer to the item's data passed as the argument.
357  * If NULL, each item's key will be printed instead.  Since an item's key is
358  * what must be entered to select an item, the item_data_print function should
359  * make it obvious what the key for each entry is.
360  *
361  * item_choice - If not NULL, will be called when asking the user to choose an
362  * item. Returns a key string corresponding to the choosen item or NULL if
363  * no item has been selected.
364  *
365  * item_choice_data - Will be passed as the argument to the item_choice function
366  *
367  * Returns a pointer to the menu if successful, or NULL if there is
368  * insufficient memory available to create the menu.
369  */
370 struct menu *menu_create(char *title, int timeout, int prompt,
371 				void (*item_data_print)(void *),
372 				char *(*item_choice)(void *),
373 				void *item_choice_data)
374 {
375 	struct menu *m;
376 
377 	m = malloc(sizeof *m);
378 
379 	if (!m)
380 		return NULL;
381 
382 	m->default_item = NULL;
383 	m->prompt = prompt;
384 	m->timeout = timeout;
385 	m->item_data_print = item_data_print;
386 	m->item_choice = item_choice;
387 	m->item_choice_data = item_choice_data;
388 
389 	if (title) {
390 		m->title = strdup(title);
391 		if (!m->title) {
392 			free(m);
393 			return NULL;
394 		}
395 	} else
396 		m->title = NULL;
397 
398 
399 	INIT_LIST_HEAD(&m->items);
400 
401 	return m;
402 }
403 
404 /*
405  * menu_destroy() - frees the memory used by a menu and its items.
406  *
407  * m - Points to a menu created by menu_create().
408  *
409  * Returns 1 if successful, or -EINVAL if m is NULL.
410  */
411 int menu_destroy(struct menu *m)
412 {
413 	if (!m)
414 		return -EINVAL;
415 
416 	menu_items_iter(m, menu_item_destroy, NULL);
417 
418 	if (m->title)
419 		free(m->title);
420 
421 	free(m);
422 
423 	return 1;
424 }
425