Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Menu commands
  23. *
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <getopt.h>
  30. #include <ipxe/menu.h>
  31. #include <ipxe/command.h>
  32. #include <ipxe/parseopt.h>
  33. #include <ipxe/settings.h>
  34. #include <ipxe/features.h>
  35. FEATURE ( FEATURE_MISC, "Menu", DHCP_EB_FEATURE_MENU, 1 );
  36. /** "menu" options */
  37. struct menu_options {
  38. /** Name */
  39. char *name;
  40. /** Delete */
  41. int delete;
  42. };
  43. /** "menu" option list */
  44. static struct option_descriptor menu_opts[] = {
  45. OPTION_DESC ( "name", 'n', required_argument,
  46. struct menu_options, name, parse_string ),
  47. OPTION_DESC ( "delete", 'd', no_argument,
  48. struct menu_options, delete, parse_flag ),
  49. };
  50. /** "menu" command descriptor */
  51. static struct command_descriptor menu_cmd =
  52. COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
  53. "[--name <name>] [--delete] [<title>]" );
  54. /**
  55. * The "menu" command
  56. *
  57. * @v argc Argument count
  58. * @v argv Argument list
  59. * @ret rc Return status code
  60. */
  61. static int menu_exec ( int argc, char **argv ) {
  62. struct menu_options opts;
  63. struct menu *menu;
  64. char *title;
  65. int rc;
  66. /* Parse options */
  67. if ( ( rc = parse_options ( argc, argv, &menu_cmd, &opts ) ) != 0 )
  68. goto err_parse_options;
  69. /* Parse title */
  70. title = concat_args ( &argv[optind] );
  71. if ( ! title ) {
  72. rc = -ENOMEM;
  73. goto err_parse_title;
  74. }
  75. /* Create menu */
  76. menu = create_menu ( opts.name, title );
  77. if ( ! menu ) {
  78. rc = -ENOMEM;
  79. goto err_create_menu;
  80. }
  81. /* Destroy menu, if applicable */
  82. if ( opts.delete )
  83. destroy_menu ( menu );
  84. /* Success */
  85. rc = 0;
  86. err_create_menu:
  87. free ( title );
  88. err_parse_title:
  89. err_parse_options:
  90. return rc;
  91. }
  92. /** "item" options */
  93. struct item_options {
  94. /** Menu name */
  95. char *menu;
  96. /** Shortcut key */
  97. unsigned int key;
  98. /** Use as default */
  99. int is_default;
  100. /** Use as a separator */
  101. int is_gap;
  102. };
  103. /** "item" option list */
  104. static struct option_descriptor item_opts[] = {
  105. OPTION_DESC ( "menu", 'm', required_argument,
  106. struct item_options, menu, parse_string ),
  107. OPTION_DESC ( "key", 'k', required_argument,
  108. struct item_options, key, parse_key ),
  109. OPTION_DESC ( "default", 'd', no_argument,
  110. struct item_options, is_default, parse_flag ),
  111. OPTION_DESC ( "gap", 'g', no_argument,
  112. struct item_options, is_gap, parse_flag ),
  113. };
  114. /** "item" command descriptor */
  115. static struct command_descriptor item_cmd =
  116. COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
  117. "[--menu <menu>] [--key <key>] [--default] "
  118. "[<label>|--gap [<text>]]" );
  119. /**
  120. * The "item" command
  121. *
  122. * @v argc Argument count
  123. * @v argv Argument list
  124. * @ret rc Return status code
  125. */
  126. static int item_exec ( int argc, char **argv ) {
  127. struct item_options opts;
  128. struct menu *menu;
  129. struct menu_item *item;
  130. char *label = NULL;
  131. char *text = NULL;
  132. int rc;
  133. /* Parse options */
  134. if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
  135. goto err_parse_options;
  136. /* Parse label, if present */
  137. if ( ! opts.is_gap )
  138. label = argv[optind++]; /* May be NULL */
  139. /* Parse text, if present */
  140. if ( optind < argc ) {
  141. text = concat_args ( &argv[optind] );
  142. if ( ! text ) {
  143. rc = -ENOMEM;
  144. goto err_parse_text;
  145. }
  146. }
  147. /* Identify menu */
  148. if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
  149. goto err_parse_menu;
  150. /* Add menu item */
  151. item = add_menu_item ( menu, label, ( text ? text : "" ),
  152. opts.key, opts.is_default );
  153. if ( ! item ) {
  154. rc = -ENOMEM;
  155. goto err_add_menu_item;
  156. }
  157. /* Success */
  158. rc = 0;
  159. err_add_menu_item:
  160. err_parse_menu:
  161. free ( text );
  162. err_parse_text:
  163. err_parse_options:
  164. return rc;
  165. }
  166. /** "choose" options */
  167. struct choose_options {
  168. /** Menu name */
  169. char *menu;
  170. /** Timeout */
  171. unsigned long timeout;
  172. /** Default selection */
  173. char *select;
  174. /** Keep menu */
  175. int keep;
  176. };
  177. /** "choose" option list */
  178. static struct option_descriptor choose_opts[] = {
  179. OPTION_DESC ( "menu", 'm', required_argument,
  180. struct choose_options, menu, parse_string ),
  181. OPTION_DESC ( "default", 'd', required_argument,
  182. struct choose_options, select, parse_string ),
  183. OPTION_DESC ( "timeout", 't', required_argument,
  184. struct choose_options, timeout, parse_timeout ),
  185. OPTION_DESC ( "keep", 'k', no_argument,
  186. struct choose_options, keep, parse_flag ),
  187. };
  188. /** "choose" command descriptor */
  189. static struct command_descriptor choose_cmd =
  190. COMMAND_DESC ( struct choose_options, choose_opts, 1, 1,
  191. "[--menu <menu>] [--default <label>] "
  192. "[--timeout <timeout>] [--keep] <setting>" );
  193. /**
  194. * The "choose" command
  195. *
  196. * @v argc Argument count
  197. * @v argv Argument list
  198. * @ret rc Return status code
  199. */
  200. static int choose_exec ( int argc, char **argv ) {
  201. struct choose_options opts;
  202. struct named_setting setting;
  203. struct menu *menu;
  204. struct menu_item *item;
  205. int rc;
  206. /* Parse options */
  207. if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
  208. goto err_parse_options;
  209. /* Parse setting name */
  210. if ( ( rc = parse_autovivified_setting ( argv[optind],
  211. &setting ) ) != 0 )
  212. goto err_parse_setting;
  213. /* Identify menu */
  214. if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
  215. goto err_parse_menu;
  216. /* Show menu */
  217. if ( ( rc = show_menu ( menu, opts.timeout, opts.select, &item ) ) != 0)
  218. goto err_show_menu;
  219. /* Apply default type if necessary */
  220. if ( ! setting.setting.type )
  221. setting.setting.type = &setting_type_string;
  222. /* Store setting */
  223. if ( ( rc = storef_setting ( setting.settings, &setting.setting,
  224. item->label ) ) != 0 ) {
  225. printf ( "Could not store \"%s\": %s\n",
  226. setting.setting.name, strerror ( rc ) );
  227. goto err_store;
  228. }
  229. /* Success */
  230. rc = 0;
  231. err_store:
  232. err_show_menu:
  233. /* Destroy menu, if applicable */
  234. if ( ! opts.keep )
  235. destroy_menu ( menu );
  236. err_parse_menu:
  237. err_parse_setting:
  238. err_parse_options:
  239. return rc;
  240. }
  241. /** Menu commands */
  242. struct command menu_commands[] __command = {
  243. {
  244. .name = "menu",
  245. .exec = menu_exec,
  246. },
  247. {
  248. .name = "item",
  249. .exec = item_exec,
  250. },
  251. {
  252. .name = "choose",
  253. .exec = choose_exec,
  254. },
  255. };