menu.c 9.9 KB

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