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

parseopt.c 8.5KB

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