Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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