menu.c 11 KB

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