Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2010-2011 Calxeda, Inc. |
Leon Yu | dfaad82 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 4 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 8 | #include <cli.h> |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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; |
Jason Hobbs | b41bc5a | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 36 | int timeout; |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 37 | char *title; |
| 38 | int prompt; |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 39 | void (*display_statusline)(struct menu *); |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 40 | void (*item_data_print)(void *); |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 41 | char *(*item_choice)(void *); |
| 42 | void *item_choice_data; |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 43 | struct list_head items; |
Leon Yu | dfaad82 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 44 | int item_cnt; |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * An iterator function for menu items. callback will be called for each item |
| 49 | * in m, with m, a pointer to the item, and extra being passed to callback. If |
| 50 | * callback returns a value other than NULL, iteration stops and the value |
| 51 | * return by callback is returned from menu_items_iter. This allows it to be |
| 52 | * used for search type operations. It is also safe for callback to remove the |
| 53 | * item from the list of items. |
| 54 | */ |
| 55 | static inline void *menu_items_iter(struct menu *m, |
| 56 | void *(*callback)(struct menu *, struct menu_item *, void *), |
| 57 | void *extra) |
| 58 | { |
| 59 | struct list_head *pos, *n; |
| 60 | struct menu_item *item; |
| 61 | void *ret; |
| 62 | |
| 63 | list_for_each_safe(pos, n, &m->items) { |
| 64 | item = list_entry(pos, struct menu_item, list); |
| 65 | |
| 66 | ret = callback(m, item, extra); |
| 67 | |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Print a menu_item. If the consumer provided an item_data_print function |
| 77 | * when creating the menu, call it with a pointer to the item's private data. |
| 78 | * Otherwise, print the key of the item. |
| 79 | */ |
| 80 | static inline void *menu_item_print(struct menu *m, |
| 81 | struct menu_item *item, |
| 82 | void *extra) |
| 83 | { |
Wolfgang Denk | d887ad5 | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 84 | if (!m->item_data_print) { |
Anatolij Gustschin | 2157497 | 2011-12-03 06:46:07 +0000 | [diff] [blame] | 85 | puts(item->key); |
Wolfgang Denk | d887ad5 | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 86 | putc('\n'); |
| 87 | } else { |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 88 | m->item_data_print(item->data); |
Wolfgang Denk | d887ad5 | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 89 | } |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 90 | |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Free the memory used by a menu item. This includes the memory used by its |
| 96 | * key. |
| 97 | */ |
| 98 | static inline void *menu_item_destroy(struct menu *m, |
| 99 | struct menu_item *item, |
| 100 | void *extra) |
| 101 | { |
| 102 | if (item->key) |
| 103 | free(item->key); |
| 104 | |
| 105 | free(item); |
| 106 | |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Display a menu so the user can make a choice of an item. First display its |
| 112 | * title, if any, and then each item in the menu. |
| 113 | */ |
| 114 | static inline void menu_display(struct menu *m) |
| 115 | { |
Wolfgang Denk | d887ad5 | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 116 | if (m->title) { |
| 117 | puts(m->title); |
| 118 | putc('\n'); |
| 119 | } |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 120 | if (m->display_statusline) |
| 121 | m->display_statusline(m); |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 122 | |
| 123 | menu_items_iter(m, menu_item_print, NULL); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Check if an item's key matches a provided string, pointed to by extra. If |
| 128 | * extra is NULL, an item with a NULL key will match. Otherwise, the item's |
| 129 | * key has to match according to strcmp. |
| 130 | * |
| 131 | * This is called via menu_items_iter, so it returns a pointer to the item if |
| 132 | * the key matches, and returns NULL otherwise. |
| 133 | */ |
| 134 | static inline void *menu_item_key_match(struct menu *m, |
| 135 | struct menu_item *item, void *extra) |
| 136 | { |
| 137 | char *item_key = extra; |
| 138 | |
| 139 | if (!item_key || !item->key) { |
| 140 | if (item_key == item->key) |
| 141 | return item; |
| 142 | |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | if (strcmp(item->key, item_key) == 0) |
| 147 | return item; |
| 148 | |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Find the first item with a key matching item_key, if any exists. |
| 154 | */ |
| 155 | static inline struct menu_item *menu_item_by_key(struct menu *m, |
| 156 | char *item_key) |
| 157 | { |
| 158 | return menu_items_iter(m, menu_item_key_match, item_key); |
| 159 | } |
| 160 | |
| 161 | /* |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 162 | * Set *choice to point to the default item's data, if any default item was |
| 163 | * set, and returns 1. If no default item was set, returns -ENOENT. |
| 164 | */ |
Anatolij Gustschin | 6a3439f | 2013-03-23 14:52:04 +0000 | [diff] [blame] | 165 | int menu_default_choice(struct menu *m, void **choice) |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 166 | { |
| 167 | if (m->default_item) { |
| 168 | *choice = m->default_item->data; |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | return -ENOENT; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Displays the menu and asks the user to choose an item. *choice will point |
| 177 | * to the private data of the item the user chooses. The user makes a choice |
| 178 | * by inputting a string matching the key of an item. Invalid choices will |
| 179 | * cause the user to be prompted again, repeatedly, until the user makes a |
| 180 | * valid choice. The user can exit the menu without making a choice via ^c. |
| 181 | * |
| 182 | * Returns 1 if the user made a choice, or -EINTR if they bail via ^c. |
| 183 | */ |
| 184 | static inline int menu_interactive_choice(struct menu *m, void **choice) |
| 185 | { |
| 186 | char cbuf[CONFIG_SYS_CBSIZE]; |
| 187 | struct menu_item *choice_item = NULL; |
| 188 | int readret; |
| 189 | |
| 190 | while (!choice_item) { |
| 191 | cbuf[0] = '\0'; |
| 192 | |
| 193 | menu_display(m); |
| 194 | |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 195 | if (!m->item_choice) { |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 196 | readret = cli_readline_into_buffer("Enter choice: ", |
Masahiro Yamada | 86fbad2 | 2018-05-24 17:04:57 +0900 | [diff] [blame] | 197 | cbuf, m->timeout); |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 198 | |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 199 | if (readret >= 0) { |
| 200 | choice_item = menu_item_by_key(m, cbuf); |
| 201 | if (!choice_item) |
| 202 | printf("%s not found\n", cbuf); |
Tuomas Tynkkynen | 9b081d8 | 2015-05-07 21:29:19 +0300 | [diff] [blame] | 203 | } else if (readret == -1) { |
| 204 | printf("<INTERRUPT>\n"); |
| 205 | return -EINTR; |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 206 | } else { |
| 207 | return menu_default_choice(m, choice); |
Heiko Schocher | fc4fa6a | 2012-01-16 22:24:29 +0000 | [diff] [blame] | 208 | } |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 209 | } else { |
| 210 | char *key = m->item_choice(m->item_choice_data); |
| 211 | |
| 212 | if (key) |
| 213 | choice_item = menu_item_by_key(m, key); |
| 214 | } |
| 215 | |
| 216 | if (!choice_item) |
| 217 | m->timeout = 0; |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | *choice = choice_item->data; |
| 221 | |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * menu_default_set() - Sets the default choice for the menu. This is safe to |
| 227 | * call more than once on a menu. |
| 228 | * |
| 229 | * m - Points to a menu created by menu_create(). |
| 230 | * |
| 231 | * item_key - Points to a string that, when compared using strcmp, matches the |
| 232 | * key for an existing item in the menu. |
| 233 | * |
| 234 | * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a |
| 235 | * key matching item_key is found. |
| 236 | */ |
| 237 | int menu_default_set(struct menu *m, char *item_key) |
| 238 | { |
| 239 | struct menu_item *item; |
| 240 | |
| 241 | if (!m) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | item = menu_item_by_key(m, item_key); |
| 245 | |
| 246 | if (!item) |
| 247 | return -ENOENT; |
| 248 | |
| 249 | m->default_item = item; |
| 250 | |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * menu_get_choice() - Returns the user's selected menu entry, or the default |
Jason Hobbs | b41bc5a | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 256 | * if the menu is set to not prompt or the timeout expires. This is safe to |
| 257 | * call more than once. |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 258 | * |
| 259 | * m - Points to a menu created by menu_create(). |
| 260 | * |
| 261 | * choice - Points to a location that will store a pointer to the selected |
| 262 | * menu item. If no item is selected or there is an error, no value will be |
| 263 | * written at the location it points to. |
| 264 | * |
| 265 | * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no |
Jason Hobbs | b41bc5a | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 266 | * default has been set and the menu is set to not prompt or the timeout |
| 267 | * expires, or -EINTR if the user exits the menu via ^c. |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 268 | */ |
| 269 | int menu_get_choice(struct menu *m, void **choice) |
| 270 | { |
| 271 | if (!m || !choice) |
| 272 | return -EINVAL; |
| 273 | |
Leon Yu | dfaad82 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 274 | if (!m->prompt || m->item_cnt == 1) |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 275 | return menu_default_choice(m, choice); |
| 276 | |
| 277 | return menu_interactive_choice(m, choice); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * menu_item_add() - Adds or replaces a menu item. Note that this replaces the |
| 282 | * data of an item if it already exists, but doesn't change the order of the |
| 283 | * item. |
| 284 | * |
| 285 | * m - Points to a menu created by menu_create(). |
| 286 | * |
| 287 | * item_key - Points to a string that will uniquely identify the item. The |
| 288 | * string will be copied to internal storage, and is safe to discard after |
| 289 | * passing to menu_item_add. |
| 290 | * |
| 291 | * item_data - An opaque pointer associated with an item. It is never |
| 292 | * dereferenced internally, but will be passed to the item_data_print, and |
| 293 | * will be returned from menu_get_choice if the menu item is selected. |
| 294 | * |
| 295 | * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is |
| 296 | * insufficient memory to add the menu item. |
| 297 | */ |
| 298 | int menu_item_add(struct menu *m, char *item_key, void *item_data) |
| 299 | { |
| 300 | struct menu_item *item; |
| 301 | |
| 302 | if (!m) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | item = menu_item_by_key(m, item_key); |
| 306 | |
| 307 | if (item) { |
| 308 | item->data = item_data; |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | item = malloc(sizeof *item); |
| 313 | if (!item) |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | item->key = strdup(item_key); |
| 317 | |
| 318 | if (!item->key) { |
| 319 | free(item); |
| 320 | return -ENOMEM; |
| 321 | } |
| 322 | |
| 323 | item->data = item_data; |
| 324 | |
| 325 | list_add_tail(&item->list, &m->items); |
Leon Yu | dfaad82 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 326 | m->item_cnt++; |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | * |
Jason Hobbs | b41bc5a | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 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. |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 344 | * |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 345 | * display_statusline - If not NULL, will be called to show a statusline when |
| 346 | * the menu is displayed. |
| 347 | * |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 348 | * item_data_print - If not NULL, will be called for each item when the menu |
| 349 | * is displayed, with the pointer to the item's data passed as the argument. |
| 350 | * If NULL, each item's key will be printed instead. Since an item's key is |
| 351 | * what must be entered to select an item, the item_data_print function should |
| 352 | * make it obvious what the key for each entry is. |
| 353 | * |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 354 | * item_choice - If not NULL, will be called when asking the user to choose an |
Alexander Merkle | dd8d8da | 2016-03-17 15:44:47 +0100 | [diff] [blame] | 355 | * item. Returns a key string corresponding to the chosen item or NULL if |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 356 | * no item has been selected. |
| 357 | * |
| 358 | * item_choice_data - Will be passed as the argument to the item_choice function |
| 359 | * |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 360 | * Returns a pointer to the menu if successful, or NULL if there is |
| 361 | * insufficient memory available to create the menu. |
| 362 | */ |
Jason Hobbs | b41bc5a | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 363 | struct menu *menu_create(char *title, int timeout, int prompt, |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 364 | void (*display_statusline)(struct menu *), |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 365 | void (*item_data_print)(void *), |
| 366 | char *(*item_choice)(void *), |
| 367 | void *item_choice_data) |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 368 | { |
| 369 | struct menu *m; |
| 370 | |
| 371 | m = malloc(sizeof *m); |
| 372 | |
| 373 | if (!m) |
| 374 | return NULL; |
| 375 | |
| 376 | m->default_item = NULL; |
| 377 | m->prompt = prompt; |
Jason Hobbs | b41bc5a | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 378 | m->timeout = timeout; |
Thirupathaiah Annapureddy | 5168d7a | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 379 | m->display_statusline = display_statusline; |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 380 | m->item_data_print = item_data_print; |
Pali Rohár | fc9d64f | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 381 | m->item_choice = item_choice; |
| 382 | m->item_choice_data = item_choice_data; |
Leon Yu | dfaad82 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 383 | m->item_cnt = 0; |
Jason Hobbs | b69bf52 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 384 | |
| 385 | if (title) { |
| 386 | m->title = strdup(title); |
| 387 | if (!m->title) { |
| 388 | free(m); |
| 389 | return NULL; |
| 390 | } |
| 391 | } else |
| 392 | m->title = NULL; |
| 393 | |
| 394 | |
| 395 | INIT_LIST_HEAD(&m->items); |
| 396 | |
| 397 | return m; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * menu_destroy() - frees the memory used by a menu and its items. |
| 402 | * |
| 403 | * m - Points to a menu created by menu_create(). |
| 404 | * |
| 405 | * Returns 1 if successful, or -EINVAL if m is NULL. |
| 406 | */ |
| 407 | int menu_destroy(struct menu *m) |
| 408 | { |
| 409 | if (!m) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | menu_items_iter(m, menu_item_destroy, NULL); |
| 413 | |
| 414 | if (m->title) |
| 415 | free(m->title); |
| 416 | |
| 417 | free(m); |
| 418 | |
| 419 | return 1; |
| 420 | } |