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.

parseopt.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright (C) 2010 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. #include <stddef.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <getopt.h>
  31. #include <ipxe/netdevice.h>
  32. #include <ipxe/menu.h>
  33. #include <ipxe/settings.h>
  34. #include <ipxe/params.h>
  35. #include <ipxe/timer.h>
  36. #include <ipxe/parseopt.h>
  37. #include <config/branding.h>
  38. /** @file
  39. *
  40. * Command line option parsing
  41. *
  42. */
  43. /** Return status code for "--help" option */
  44. #define ECANCELED_NO_OP __einfo_error ( EINFO_ECANCELED_NO_OP )
  45. #define EINFO_ECANCELED_NO_OP \
  46. __einfo_uniqify ( EINFO_ECANCELED, 0x01, "Nothing to do" )
  47. /* Disambiguate the various error codes */
  48. #define EINVAL_INTEGER __einfo_error ( EINFO_EINVAL_INTEGER )
  49. #define EINFO_EINVAL_INTEGER \
  50. __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid integer value" )
  51. #define EINVAL_UNKNOWN_OPTION __einfo_error ( EINFO_EINVAL_UNKNOWN_OPTION )
  52. #define EINFO_EINVAL_UNKNOWN_OPTION \
  53. __einfo_uniqify ( EINFO_EINVAL, 0x02, "Unrecognised option" )
  54. #define EINVAL_MISSING_ARGUMENT __einfo_error ( EINFO_EINVAL_MISSING_ARGUMENT )
  55. #define EINFO_EINVAL_MISSING_ARGUMENT \
  56. __einfo_uniqify ( EINFO_EINVAL, 0x03, "Missing argument" )
  57. /**
  58. * Parse string value
  59. *
  60. * @v text Text
  61. * @ret value String value
  62. * @ret rc Return status code
  63. */
  64. int parse_string ( char *text, char **value ) {
  65. /* Sanity check */
  66. assert ( text != NULL );
  67. /* Parse string */
  68. *value = text;
  69. return 0;
  70. }
  71. /**
  72. * Parse integer value
  73. *
  74. * @v text Text
  75. * @ret value Integer value
  76. * @ret rc Return status code
  77. */
  78. int parse_integer ( char *text, unsigned int *value ) {
  79. char *endp;
  80. /* Sanity check */
  81. assert ( text != NULL );
  82. /* Parse integer */
  83. *value = strtoul ( text, &endp, 0 );
  84. if ( *endp ) {
  85. printf ( "\"%s\": invalid integer value\n", text );
  86. return -EINVAL_INTEGER;
  87. }
  88. return 0;
  89. }
  90. /**
  91. * Parse timeout value (in ms)
  92. *
  93. * @v text Text
  94. * @ret value Integer value
  95. * @ret rc Return status code
  96. */
  97. int parse_timeout ( char *text, unsigned long *value ) {
  98. unsigned int value_ms;
  99. int rc;
  100. /* Parse raw integer value */
  101. if ( ( rc = parse_integer ( text, &value_ms ) ) != 0 )
  102. return rc;
  103. /* Convert to a number of timer ticks */
  104. *value = ( value_ms * TICKS_PER_MS );
  105. return 0;
  106. }
  107. /**
  108. * Parse network device name
  109. *
  110. * @v text Text
  111. * @ret netdev Network device
  112. * @ret rc Return status code
  113. */
  114. int parse_netdev ( char *text, struct net_device **netdev ) {
  115. /* Sanity check */
  116. assert ( text != NULL );
  117. /* Find network device */
  118. *netdev = find_netdev ( text );
  119. if ( ! *netdev ) {
  120. printf ( "\"%s\": no such network device\n", text );
  121. return -ENODEV;
  122. }
  123. return 0;
  124. }
  125. /**
  126. * Parse network device configurator name
  127. *
  128. * @v text Text
  129. * @ret configurator Network device configurator
  130. * @ret rc Return status code
  131. */
  132. int parse_netdev_configurator ( char *text,
  133. struct net_device_configurator **configurator ){
  134. /* Sanity check */
  135. assert ( text != NULL );
  136. /* Find network device configurator */
  137. *configurator = find_netdev_configurator ( text );
  138. if ( ! *configurator ) {
  139. printf ( "\"%s\": no such configurator\n", text );
  140. return -ENOTSUP;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * Parse menu name
  146. *
  147. * @v text Text
  148. * @ret menu Menu
  149. * @ret rc Return status code
  150. */
  151. int parse_menu ( char *text, struct menu **menu ) {
  152. /* Find menu */
  153. *menu = find_menu ( text );
  154. if ( ! *menu ) {
  155. if ( text ) {
  156. printf ( "\"%s\": no such menu\n", text );
  157. } else {
  158. printf ( "No default menu\n" );
  159. }
  160. return -ENOENT;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * Parse flag
  166. *
  167. * @v text Text (ignored)
  168. * @ret flag Flag to set
  169. * @ret rc Return status code
  170. */
  171. int parse_flag ( char *text __unused, int *flag ) {
  172. /* Set flag */
  173. *flag = 1;
  174. return 0;
  175. }
  176. /**
  177. * Parse key
  178. *
  179. * @v text Text
  180. * @ret key Key
  181. * @ret rc Return status code
  182. */
  183. int parse_key ( char *text, unsigned int *key ) {
  184. /* Interpret single characters as being a literal key character */
  185. if ( text[0] && ! text[1] ) {
  186. *key = text[0];
  187. return 0;
  188. }
  189. /* Otherwise, interpret as an integer */
  190. return parse_integer ( text, key );
  191. }
  192. /**
  193. * Parse settings block name
  194. *
  195. * @v text Text
  196. * @ret value Integer value
  197. * @ret rc Return status code
  198. */
  199. int parse_settings ( char *text, struct settings **value ) {
  200. /* Sanity check */
  201. assert ( text != NULL );
  202. /* Parse scope name */
  203. *value = find_settings ( text );
  204. if ( ! *value ) {
  205. printf ( "\"%s\": no such scope\n", text );
  206. return -EINVAL;
  207. }
  208. return 0;
  209. }
  210. /**
  211. * Parse setting name
  212. *
  213. * @v text Text
  214. * @v setting Named setting to fill in
  215. * @v get_child Function to find or create child settings block
  216. * @ret rc Return status code
  217. *
  218. * Note that this function modifies the original @c text.
  219. */
  220. int parse_setting ( char *text, struct named_setting *setting,
  221. get_child_settings_t get_child ) {
  222. int rc;
  223. /* Sanity check */
  224. assert ( text != NULL );
  225. /* Parse setting name */
  226. if ( ( rc = parse_setting_name ( text, get_child, &setting->settings,
  227. &setting->setting ) ) != 0 ) {
  228. printf ( "\"%s\": invalid setting\n", text );
  229. return rc;
  230. }
  231. return 0;
  232. }
  233. /**
  234. * Parse existing setting name
  235. *
  236. * @v text Text
  237. * @v setting Named setting to fill in
  238. * @ret rc Return status code
  239. *
  240. * Note that this function modifies the original @c text.
  241. */
  242. int parse_existing_setting ( char *text, struct named_setting *setting ) {
  243. return parse_setting ( text, setting, find_child_settings );
  244. }
  245. /**
  246. * Parse and autovivify setting name
  247. *
  248. * @v text Text
  249. * @v setting Named setting to fill in
  250. * @ret rc Return status code
  251. *
  252. * Note that this function modifies the original @c text.
  253. */
  254. int parse_autovivified_setting ( char *text, struct named_setting *setting ) {
  255. return parse_setting ( text, setting, autovivify_child_settings );
  256. }
  257. /**
  258. * Parse form parameter list name
  259. *
  260. * @v text Text
  261. * @ret params Parameter list
  262. * @ret rc Return status code
  263. */
  264. int parse_parameters ( char *text, struct parameters **params ) {
  265. /* Find parameter list */
  266. *params = find_parameters ( text );
  267. if ( ! *params ) {
  268. if ( text ) {
  269. printf ( "\"%s\": no such parameter list\n", text );
  270. } else {
  271. printf ( "No default parameter list\n" );
  272. }
  273. return -ENOENT;
  274. }
  275. return 0;
  276. }
  277. /**
  278. * Print command usage message
  279. *
  280. * @v cmd Command descriptor
  281. * @v argv Argument list
  282. */
  283. void print_usage ( struct command_descriptor *cmd, char **argv ) {
  284. struct option_descriptor *option;
  285. unsigned int i;
  286. int is_optional;
  287. printf ( "Usage:\n\n %s", argv[0] );
  288. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  289. option = &cmd->options[i];
  290. printf ( " [-%c|--%s", option->shortopt, option->longopt );
  291. if ( option->has_arg ) {
  292. is_optional = ( option->has_arg == optional_argument );
  293. printf ( " %s<%s>%s", ( is_optional ? "[" : "" ),
  294. option->longopt, ( is_optional ? "]" : "" ) );
  295. }
  296. printf ( "]" );
  297. }
  298. if ( cmd->usage )
  299. printf ( " %s", cmd->usage );
  300. printf ( "\n\nSee " PRODUCT_COMMAND_URI " for further information\n",
  301. argv[0] );
  302. }
  303. /**
  304. * Reparse command-line options
  305. *
  306. * @v argc Argument count
  307. * @v argv Argument list
  308. * @v cmd Command descriptor
  309. * @v opts Options (already initialised with default values)
  310. * @ret rc Return status code
  311. */
  312. int reparse_options ( int argc, char **argv, struct command_descriptor *cmd,
  313. void *opts ) {
  314. struct option longopts[ cmd->num_options + 1 /* help */ + 1 /* end */ ];
  315. char shortopts[ cmd->num_options * 3 /* possible "::" */ + 1 /* "h" */
  316. + 1 /* NUL */ ];
  317. unsigned int shortopt_idx = 0;
  318. int ( * parse ) ( char *text, void *value );
  319. void *value;
  320. unsigned int i;
  321. unsigned int j;
  322. unsigned int num_args;
  323. int c;
  324. int rc;
  325. /* Construct long and short option lists for getopt_long() */
  326. memset ( longopts, 0, sizeof ( longopts ) );
  327. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  328. longopts[i].name = cmd->options[i].longopt;
  329. longopts[i].has_arg = cmd->options[i].has_arg;
  330. longopts[i].val = cmd->options[i].shortopt;
  331. shortopts[shortopt_idx++] = cmd->options[i].shortopt;
  332. assert ( cmd->options[i].has_arg <= optional_argument );
  333. for ( j = cmd->options[i].has_arg ; j > 0 ; j-- )
  334. shortopts[shortopt_idx++] = ':';
  335. }
  336. longopts[i].name = "help";
  337. longopts[i].val = 'h';
  338. shortopts[shortopt_idx++] = 'h';
  339. shortopts[shortopt_idx++] = '\0';
  340. assert ( shortopt_idx <= sizeof ( shortopts ) );
  341. DBGC ( cmd, "Command \"%s\" has options \"%s\", %d-%d args, len %d\n",
  342. argv[0], shortopts, cmd->min_args, cmd->max_args, cmd->len );
  343. /* Parse options */
  344. while ( ( c = getopt_long ( argc, argv, shortopts, longopts,
  345. NULL ) ) >= 0 ) {
  346. switch ( c ) {
  347. case 'h' :
  348. /* Print help */
  349. print_usage ( cmd, argv );
  350. return -ECANCELED_NO_OP;
  351. case '?' :
  352. /* Print usage message */
  353. print_usage ( cmd, argv );
  354. return -EINVAL_UNKNOWN_OPTION;
  355. case ':' :
  356. /* Print usage message */
  357. print_usage ( cmd, argv );
  358. return -EINVAL_MISSING_ARGUMENT;
  359. default:
  360. /* Search for an option to parse */
  361. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  362. if ( c != cmd->options[i].shortopt )
  363. continue;
  364. parse = cmd->options[i].parse;
  365. value = ( opts + cmd->options[i].offset );
  366. if ( ( rc = parse ( optarg, value ) ) != 0 )
  367. return rc;
  368. break;
  369. }
  370. assert ( i < cmd->num_options );
  371. }
  372. }
  373. /* Check remaining arguments */
  374. num_args = ( argc - optind );
  375. if ( ( num_args < cmd->min_args ) || ( num_args > cmd->max_args ) ) {
  376. print_usage ( cmd, argv );
  377. return -ERANGE;
  378. }
  379. return 0;
  380. }
  381. /**
  382. * Parse command-line options
  383. *
  384. * @v argc Argument count
  385. * @v argv Argument list
  386. * @v cmd Command descriptor
  387. * @v opts Options (may be uninitialised)
  388. * @ret rc Return status code
  389. */
  390. int parse_options ( int argc, char **argv, struct command_descriptor *cmd,
  391. void *opts ) {
  392. /* Clear options */
  393. memset ( opts, 0, cmd->len );
  394. return reparse_options ( argc, argv, cmd, opts );
  395. }