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 void __menu_display_statusline(struct menu *m) 109 { 110 return; 111 } 112 void menu_display_statusline(struct menu *m) 113 __attribute__ ((weak, alias("__menu_display_statusline"))); 114 115 /* 116 * Display a menu so the user can make a choice of an item. First display its 117 * title, if any, and then each item in the menu. 118 */ 119 static inline void menu_display(struct menu *m) 120 { 121 if (m->title) { 122 puts(m->title); 123 putc('\n'); 124 } 125 menu_display_statusline(m); 126 127 menu_items_iter(m, menu_item_print, NULL); 128 } 129 130 /* 131 * Check if an item's key matches a provided string, pointed to by extra. If 132 * extra is NULL, an item with a NULL key will match. Otherwise, the item's 133 * key has to match according to strcmp. 134 * 135 * This is called via menu_items_iter, so it returns a pointer to the item if 136 * the key matches, and returns NULL otherwise. 137 */ 138 static inline void *menu_item_key_match(struct menu *m, 139 struct menu_item *item, void *extra) 140 { 141 char *item_key = extra; 142 143 if (!item_key || !item->key) { 144 if (item_key == item->key) 145 return item; 146 147 return NULL; 148 } 149 150 if (strcmp(item->key, item_key) == 0) 151 return item; 152 153 return NULL; 154 } 155 156 /* 157 * Find the first item with a key matching item_key, if any exists. 158 */ 159 static inline struct menu_item *menu_item_by_key(struct menu *m, 160 char *item_key) 161 { 162 return menu_items_iter(m, menu_item_key_match, item_key); 163 } 164 165 /* 166 * Set *choice to point to the default item's data, if any default item was 167 * set, and returns 1. If no default item was set, returns -ENOENT. 168 */ 169 int menu_default_choice(struct menu *m, void **choice) 170 { 171 if (m->default_item) { 172 *choice = m->default_item->data; 173 return 1; 174 } 175 176 return -ENOENT; 177 } 178 179 /* 180 * Displays the menu and asks the user to choose an item. *choice will point 181 * to the private data of the item the user chooses. The user makes a choice 182 * by inputting a string matching the key of an item. Invalid choices will 183 * cause the user to be prompted again, repeatedly, until the user makes a 184 * valid choice. The user can exit the menu without making a choice via ^c. 185 * 186 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c. 187 */ 188 static inline int menu_interactive_choice(struct menu *m, void **choice) 189 { 190 char cbuf[CONFIG_SYS_CBSIZE]; 191 struct menu_item *choice_item = NULL; 192 int readret; 193 194 while (!choice_item) { 195 cbuf[0] = '\0'; 196 197 menu_display(m); 198 199 if (!m->item_choice) { 200 readret = readline_into_buffer("Enter choice: ", cbuf, 201 m->timeout / 10); 202 203 if (readret >= 0) { 204 choice_item = menu_item_by_key(m, cbuf); 205 if (!choice_item) 206 printf("%s not found\n", cbuf); 207 } else { 208 return menu_default_choice(m, choice); 209 } 210 } else { 211 char *key = m->item_choice(m->item_choice_data); 212 213 if (key) 214 choice_item = menu_item_by_key(m, key); 215 } 216 217 if (!choice_item) 218 m->timeout = 0; 219 } 220 221 *choice = choice_item->data; 222 223 return 1; 224 } 225 226 /* 227 * menu_default_set() - Sets the default choice for the menu. This is safe to 228 * call more than once on a menu. 229 * 230 * m - Points to a menu created by menu_create(). 231 * 232 * item_key - Points to a string that, when compared using strcmp, matches the 233 * key for an existing item in the menu. 234 * 235 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a 236 * key matching item_key is found. 237 */ 238 int menu_default_set(struct menu *m, char *item_key) 239 { 240 struct menu_item *item; 241 242 if (!m) 243 return -EINVAL; 244 245 item = menu_item_by_key(m, item_key); 246 247 if (!item) 248 return -ENOENT; 249 250 m->default_item = item; 251 252 return 1; 253 } 254 255 /* 256 * menu_get_choice() - Returns the user's selected menu entry, or the default 257 * if the menu is set to not prompt or the timeout expires. This is safe to 258 * call more than once. 259 * 260 * m - Points to a menu created by menu_create(). 261 * 262 * choice - Points to a location that will store a pointer to the selected 263 * menu item. If no item is selected or there is an error, no value will be 264 * written at the location it points to. 265 * 266 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no 267 * default has been set and the menu is set to not prompt or the timeout 268 * expires, or -EINTR if the user exits the menu via ^c. 269 */ 270 int menu_get_choice(struct menu *m, void **choice) 271 { 272 if (!m || !choice) 273 return -EINVAL; 274 275 if (!m->prompt) 276 return menu_default_choice(m, choice); 277 278 return menu_interactive_choice(m, choice); 279 } 280 281 /* 282 * menu_item_add() - Adds or replaces a menu item. Note that this replaces the 283 * data of an item if it already exists, but doesn't change the order of the 284 * item. 285 * 286 * m - Points to a menu created by menu_create(). 287 * 288 * item_key - Points to a string that will uniquely identify the item. The 289 * string will be copied to internal storage, and is safe to discard after 290 * passing to menu_item_add. 291 * 292 * item_data - An opaque pointer associated with an item. It is never 293 * dereferenced internally, but will be passed to the item_data_print, and 294 * will be returned from menu_get_choice if the menu item is selected. 295 * 296 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is 297 * insufficient memory to add the menu item. 298 */ 299 int menu_item_add(struct menu *m, char *item_key, void *item_data) 300 { 301 struct menu_item *item; 302 303 if (!m) 304 return -EINVAL; 305 306 item = menu_item_by_key(m, item_key); 307 308 if (item) { 309 item->data = item_data; 310 return 1; 311 } 312 313 item = malloc(sizeof *item); 314 if (!item) 315 return -ENOMEM; 316 317 item->key = strdup(item_key); 318 319 if (!item->key) { 320 free(item); 321 return -ENOMEM; 322 } 323 324 item->data = item_data; 325 326 list_add_tail(&item->list, &m->items); 327 328 return 1; 329 } 330 331 /* 332 * menu_create() - Creates a menu handle with default settings 333 * 334 * title - If not NULL, points to a string that will be displayed before the 335 * list of menu items. It will be copied to internal storage, and is safe to 336 * discard after passing to menu_create(). 337 * 338 * timeout - A delay in seconds to wait for user input. If 0, timeout is 339 * disabled, and the default choice will be returned unless prompt is 1. 340 * 341 * prompt - If 0, don't ask for user input unless there is an interrupted 342 * timeout. If 1, the user will be prompted for input regardless of the value 343 * of timeout. 344 * 345 * item_data_print - If not NULL, will be called for each item when the menu 346 * is displayed, with the pointer to the item's data passed as the argument. 347 * If NULL, each item's key will be printed instead. Since an item's key is 348 * what must be entered to select an item, the item_data_print function should 349 * make it obvious what the key for each entry is. 350 * 351 * item_choice - If not NULL, will be called when asking the user to choose an 352 * item. Returns a key string corresponding to the choosen item or NULL if 353 * no item has been selected. 354 * 355 * item_choice_data - Will be passed as the argument to the item_choice function 356 * 357 * Returns a pointer to the menu if successful, or NULL if there is 358 * insufficient memory available to create the menu. 359 */ 360 struct menu *menu_create(char *title, int timeout, int prompt, 361 void (*item_data_print)(void *), 362 char *(*item_choice)(void *), 363 void *item_choice_data) 364 { 365 struct menu *m; 366 367 m = malloc(sizeof *m); 368 369 if (!m) 370 return NULL; 371 372 m->default_item = NULL; 373 m->prompt = prompt; 374 m->timeout = timeout; 375 m->item_data_print = item_data_print; 376 m->item_choice = item_choice; 377 m->item_choice_data = item_choice_data; 378 379 if (title) { 380 m->title = strdup(title); 381 if (!m->title) { 382 free(m); 383 return NULL; 384 } 385 } else 386 m->title = NULL; 387 388 389 INIT_LIST_HEAD(&m->items); 390 391 return m; 392 } 393 394 /* 395 * menu_destroy() - frees the memory used by a menu and its items. 396 * 397 * m - Points to a menu created by menu_create(). 398 * 399 * Returns 1 if successful, or -EINVAL if m is NULL. 400 */ 401 int menu_destroy(struct menu *m) 402 { 403 if (!m) 404 return -EINVAL; 405 406 menu_items_iter(m, menu_item_destroy, NULL); 407 408 if (m->title) 409 free(m->title); 410 411 free(m); 412 413 return 1; 414 } 415