blob: 507b122a51156be7da189b74a6cdc780e871fc3f [file] [log] [blame]
Jason Hobbscd823952011-06-29 11:25:13 -05001/*
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
25struct menu_item {
26 char *key;
27 void *data;
28 struct list_head list;
29};
30
31struct menu {
32 struct menu_item *default_item;
Jason Hobbs58493652011-06-29 11:25:15 -050033 int timeout;
Jason Hobbscd823952011-06-29 11:25:13 -050034 char *title;
35 int prompt;
36 void (*item_data_print)(void *);
37 struct list_head items;
38};
39
40static inline void *menu_items_iter(struct menu *m,
41 void *(*callback)(struct menu *, struct menu_item *, void *),
42 void *extra)
43{
44 struct list_head *pos, *n;
45 struct menu_item *item;
46 void *ret;
47
48 list_for_each_safe(pos, n, &m->items) {
49 item = list_entry(pos, struct menu_item, list);
50
51 ret = callback(m, item, extra);
52
53 if (ret)
54 return ret;
55 }
56
57 return NULL;
58}
59
60static inline void *menu_item_print(struct menu *m,
61 struct menu_item *item,
62 void *extra)
63{
64 if (!m->item_data_print)
65 printf("%s\n", item->key);
66 else
67 m->item_data_print(item->data);
68
69 return NULL;
70}
71
72static inline void *menu_item_destroy(struct menu *m,
73 struct menu_item *item,
74 void *extra)
75{
76 if (item->key)
77 free(item->key);
78
79 free(item);
80
81 return NULL;
82}
83
84static inline void menu_display(struct menu *m)
85{
86 if (m->title)
87 printf("%s:\n", m->title);
88
89 menu_items_iter(m, menu_item_print, NULL);
90}
91
92static inline void *menu_item_key_match(struct menu *m,
93 struct menu_item *item,
94 void *extra)
95{
96 char *item_key = extra;
97
98 if (!item_key || !item->key) {
99 if (item_key == item->key)
100 return item;
101
102 return NULL;
103 }
104
105 if (strcmp(item->key, item_key) == 0)
106 return item;
107
108 return NULL;
109}
110
111static inline struct menu_item *menu_item_by_key(struct menu *m,
112 char *item_key)
113{
114 return menu_items_iter(m, menu_item_key_match, item_key);
115}
116
Jason Hobbs58493652011-06-29 11:25:15 -0500117static inline int menu_interrupted(struct menu *m)
118{
119 if (!m->timeout)
120 return 0;
121
122 if (abortboot(m->timeout/10))
123 return 1;
124
125 return 0;
126}
127
Jason Hobbscd823952011-06-29 11:25:13 -0500128static inline int menu_use_default(struct menu *m)
129{
Jason Hobbs58493652011-06-29 11:25:15 -0500130 return !m->prompt && !menu_interrupted(m);
Jason Hobbscd823952011-06-29 11:25:13 -0500131}
132
133static inline int menu_default_choice(struct menu *m, void **choice)
134{
135 if (m->default_item) {
136 *choice = m->default_item->data;
137 return 1;
138 }
139
140 return -ENOENT;
141}
142
143static inline int menu_interactive_choice(struct menu *m, void **choice)
144{
145 char cbuf[CONFIG_SYS_CBSIZE];
146 struct menu_item *choice_item = NULL;
147 int readret;
148
149 while (!choice_item) {
150 cbuf[0] = '\0';
151
152 menu_display(m);
153
154 readret = readline_into_buffer("Enter choice: ", cbuf);
155
156 if (readret >= 0) {
157 choice_item = menu_item_by_key(m, cbuf);
158
159 if (!choice_item)
160 printf("%s not found\n", cbuf);
161 } else {
162 printf("^C\n");
163 return -EINTR;
164 }
165 }
166
167 *choice = choice_item->data;
168
169 return 1;
170}
171
172int menu_default_set(struct menu *m, char *item_key)
173{
174 struct menu_item *item;
175
176 if (!m)
177 return -EINVAL;
178
179 item = menu_item_by_key(m, item_key);
180
181 if (!item)
182 return -ENOENT;
183
184 m->default_item = item;
185
186 return 1;
187}
188
189int menu_get_choice(struct menu *m, void **choice)
190{
191 if (!m || !choice)
192 return -EINVAL;
193
194 if (menu_use_default(m))
195 return menu_default_choice(m, choice);
196
197 return menu_interactive_choice(m, choice);
198}
199
200/*
201 * note that this replaces the data of an item if it already exists, but
202 * doesn't change the order of the item.
203 */
204int menu_item_add(struct menu *m, char *item_key, void *item_data)
205{
206 struct menu_item *item;
207
208 if (!m)
209 return -EINVAL;
210
211 item = menu_item_by_key(m, item_key);
212
213 if (item) {
214 item->data = item_data;
215 return 1;
216 }
217
218 item = malloc(sizeof *item);
219 if (!item)
220 return -ENOMEM;
221
222 item->key = strdup(item_key);
223
224 if (!item->key) {
225 free(item);
226 return -ENOMEM;
227 }
228
229 item->data = item_data;
230
231 list_add_tail(&item->list, &m->items);
232
233 return 1;
234}
235
Jason Hobbs58493652011-06-29 11:25:15 -0500236struct menu *menu_create(char *title, int timeout, int prompt,
Jason Hobbscd823952011-06-29 11:25:13 -0500237 void (*item_data_print)(void *))
238{
239 struct menu *m;
240
241 m = malloc(sizeof *m);
242
243 if (!m)
244 return NULL;
245
246 m->default_item = NULL;
247 m->prompt = prompt;
Jason Hobbs58493652011-06-29 11:25:15 -0500248 m->timeout = timeout;
Jason Hobbscd823952011-06-29 11:25:13 -0500249 m->item_data_print = item_data_print;
250
251 if (title) {
252 m->title = strdup(title);
253 if (!m->title) {
254 free(m);
255 return NULL;
256 }
257 } else
258 m->title = NULL;
259
260
261 INIT_LIST_HEAD(&m->items);
262
263 return m;
264}
265
266int menu_destroy(struct menu *m)
267{
268 if (!m)
269 return -EINVAL;
270
271 menu_items_iter(m, menu_item_destroy, NULL);
272
273 if (m->title)
274 free(m->title);
275
276 free(m);
277
278 return 1;
279}