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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 menu name
  122. *
  123. * @v text Text
  124. * @ret menu Menu
  125. * @ret rc Return status code
  126. */
  127. int parse_menu ( char *text, struct menu **menu ) {
  128. /* Find menu */
  129. *menu = find_menu ( text );
  130. if ( ! *menu ) {
  131. if ( text ) {
  132. printf ( "\"%s\": no such menu\n", text );
  133. } else {
  134. printf ( "No default menu\n" );
  135. }
  136. return -ENOENT;
  137. }
  138. return 0;
  139. }
  140. /**
  141. * Parse flag
  142. *
  143. * @v text Text (ignored)
  144. * @ret flag Flag to set
  145. * @ret rc Return status code
  146. */
  147. int parse_flag ( char *text __unused, int *flag ) {
  148. /* Set flag */
  149. *flag = 1;
  150. return 0;
  151. }
  152. /**
  153. * Parse key
  154. *
  155. * @v text Text
  156. * @ret key Key
  157. * @ret rc Return status code
  158. */
  159. int parse_key ( char *text, unsigned int *key ) {
  160. /* Interpret single characters as being a literal key character */
  161. if ( text[0] && ! text[1] ) {
  162. *key = text[0];
  163. return 0;
  164. }
  165. /* Otherwise, interpret as an integer */
  166. return parse_integer ( text, key );
  167. }
  168. /**
  169. * Parse settings block name
  170. *
  171. * @v text Text
  172. * @ret value Integer value
  173. * @ret rc Return status code
  174. */
  175. int parse_settings ( char *text, struct settings **value ) {
  176. /* Sanity check */
  177. assert ( text != NULL );
  178. /* Parse scope name */
  179. *value = find_settings ( text );
  180. if ( ! *value ) {
  181. printf ( "\"%s\": no such scope\n", text );
  182. return -EINVAL;
  183. }
  184. return 0;
  185. }
  186. /**
  187. * Parse setting name
  188. *
  189. * @v text Text
  190. * @v setting Named setting to fill in
  191. * @v get_child Function to find or create child settings block
  192. * @ret rc Return status code
  193. *
  194. * Note that this function modifies the original @c text.
  195. */
  196. int parse_setting ( char *text, struct named_setting *setting,
  197. get_child_settings_t get_child ) {
  198. int rc;
  199. /* Sanity check */
  200. assert ( text != NULL );
  201. /* Parse setting name */
  202. if ( ( rc = parse_setting_name ( text, get_child, &setting->settings,
  203. &setting->setting ) ) != 0 ) {
  204. printf ( "\"%s\": invalid setting\n", text );
  205. return rc;
  206. }
  207. return 0;
  208. }
  209. /**
  210. * Parse existing setting name
  211. *
  212. * @v text Text
  213. * @v setting Named setting to fill in
  214. * @ret rc Return status code
  215. *
  216. * Note that this function modifies the original @c text.
  217. */
  218. int parse_existing_setting ( char *text, struct named_setting *setting ) {
  219. return parse_setting ( text, setting, find_child_settings );
  220. }
  221. /**
  222. * Parse and autovivify setting name
  223. *
  224. * @v text Text
  225. * @v setting Named setting to fill in
  226. * @ret rc Return status code
  227. *
  228. * Note that this function modifies the original @c text.
  229. */
  230. int parse_autovivified_setting ( char *text, struct named_setting *setting ) {
  231. return parse_setting ( text, setting, autovivify_child_settings );
  232. }
  233. /**
  234. * Parse form parameter list name
  235. *
  236. * @v text Text
  237. * @ret params Parameter list
  238. * @ret rc Return status code
  239. */
  240. int parse_parameters ( char *text, struct parameters **params ) {
  241. /* Find parameter list */
  242. *params = find_parameters ( text );
  243. if ( ! *params ) {
  244. if ( text ) {
  245. printf ( "\"%s\": no such parameter list\n", text );
  246. } else {
  247. printf ( "No default parameter list\n" );
  248. }
  249. return -ENOENT;
  250. }
  251. return 0;
  252. }
  253. /**
  254. * Print command usage message
  255. *
  256. * @v cmd Command descriptor
  257. * @v argv Argument list
  258. */
  259. void print_usage ( struct command_descriptor *cmd, char **argv ) {
  260. printf ( "Usage:\n\n %s %s\n\nSee http://ipxe.org/cmd/%s for further "
  261. "information\n", argv[0], cmd->usage, argv[0] );
  262. }
  263. /**
  264. * Reparse command-line options
  265. *
  266. * @v argc Argument count
  267. * @v argv Argument list
  268. * @v cmd Command descriptor
  269. * @v opts Options (already initialised with default values)
  270. * @ret rc Return status code
  271. */
  272. int reparse_options ( int argc, char **argv, struct command_descriptor *cmd,
  273. void *opts ) {
  274. struct option longopts[ cmd->num_options + 1 /* help */ + 1 /* end */ ];
  275. char shortopts[ cmd->num_options * 3 /* possible "::" */ + 1 /* "h" */
  276. + 1 /* NUL */ ];
  277. unsigned int shortopt_idx = 0;
  278. int ( * parse ) ( char *text, void *value );
  279. void *value;
  280. unsigned int i;
  281. unsigned int j;
  282. unsigned int num_args;
  283. int c;
  284. int rc;
  285. /* Construct long and short option lists for getopt_long() */
  286. memset ( longopts, 0, sizeof ( longopts ) );
  287. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  288. longopts[i].name = cmd->options[i].longopt;
  289. longopts[i].has_arg = cmd->options[i].has_arg;
  290. longopts[i].val = cmd->options[i].shortopt;
  291. shortopts[shortopt_idx++] = cmd->options[i].shortopt;
  292. assert ( cmd->options[i].has_arg <= optional_argument );
  293. for ( j = cmd->options[i].has_arg ; j > 0 ; j-- )
  294. shortopts[shortopt_idx++] = ':';
  295. }
  296. longopts[i].name = "help";
  297. longopts[i].val = 'h';
  298. shortopts[shortopt_idx++] = 'h';
  299. shortopts[shortopt_idx++] = '\0';
  300. assert ( shortopt_idx <= sizeof ( shortopts ) );
  301. DBGC ( cmd, "Command \"%s\" has options \"%s\", %d-%d args, len %d\n",
  302. argv[0], shortopts, cmd->min_args, cmd->max_args, cmd->len );
  303. /* Parse options */
  304. while ( ( c = getopt_long ( argc, argv, shortopts, longopts,
  305. NULL ) ) >= 0 ) {
  306. switch ( c ) {
  307. case 'h' :
  308. /* Print help */
  309. print_usage ( cmd, argv );
  310. return -ECANCELED_NO_OP;
  311. case '?' :
  312. /* Print usage message */
  313. print_usage ( cmd, argv );
  314. return -EINVAL_UNKNOWN_OPTION;
  315. case ':' :
  316. /* Print usage message */
  317. print_usage ( cmd, argv );
  318. return -EINVAL_MISSING_ARGUMENT;
  319. default:
  320. /* Search for an option to parse */
  321. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  322. if ( c != cmd->options[i].shortopt )
  323. continue;
  324. parse = cmd->options[i].parse;
  325. value = ( opts + cmd->options[i].offset );
  326. if ( ( rc = parse ( optarg, value ) ) != 0 )
  327. return rc;
  328. break;
  329. }
  330. assert ( i < cmd->num_options );
  331. }
  332. }
  333. /* Check remaining arguments */
  334. num_args = ( argc - optind );
  335. if ( ( num_args < cmd->min_args ) || ( num_args > cmd->max_args ) ) {
  336. print_usage ( cmd, argv );
  337. return -ERANGE;
  338. }
  339. return 0;
  340. }
  341. /**
  342. * Parse command-line options
  343. *
  344. * @v argc Argument count
  345. * @v argv Argument list
  346. * @v cmd Command descriptor
  347. * @v opts Options (may be uninitialised)
  348. * @ret rc Return status code
  349. */
  350. int parse_options ( int argc, char **argv, struct command_descriptor *cmd,
  351. void *opts ) {
  352. /* Clear options */
  353. memset ( opts, 0, cmd->len );
  354. return reparse_options ( argc, argv, cmd, opts );
  355. }