You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parseopt.c 10KB

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