選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

parseopt.c 10KB

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