menu.c 10.0 KB

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