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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <getopt.h>
  26. #include <ipxe/netdevice.h>
  27. #include <ipxe/parseopt.h>
  28. /** @file
  29. *
  30. * Command line option parsing
  31. *
  32. */
  33. /** Return status code for "--help" option */
  34. #define ECANCELED_NO_OP __einfo_error ( EINFO_ECANCELED_NO_OP )
  35. #define EINFO_ECANCELED_NO_OP \
  36. __einfo_uniqify ( EINFO_ECANCELED, 0x01, "Nothing to do" )
  37. /* Disambiguate the various error codes */
  38. #define EINVAL_INTEGER __einfo_error ( EINFO_EINVAL_INTEGER )
  39. #define EINFO_EINVAL_INTEGER \
  40. __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid integer value" )
  41. #define EINVAL_UNKNOWN_OPTION __einfo_error ( EINFO_EINVAL_UNKNOWN_OPTION )
  42. #define EINFO_EINVAL_UNKNOWN_OPTION \
  43. __einfo_uniqify ( EINFO_EINVAL, 0x02, "Unrecognised option" )
  44. #define EINVAL_MISSING_ARGUMENT __einfo_error ( EINFO_EINVAL_MISSING_ARGUMENT )
  45. #define EINFO_EINVAL_MISSING_ARGUMENT \
  46. __einfo_uniqify ( EINFO_EINVAL, 0x03, "Missing argument" )
  47. /**
  48. * Parse string value
  49. *
  50. * @v text Text
  51. * @ret value String value
  52. * @ret rc Return status code
  53. */
  54. int parse_string ( const char *text, const char **value ) {
  55. /* Sanity check */
  56. assert ( text != NULL );
  57. /* Parse string */
  58. *value = text;
  59. return 0;
  60. }
  61. /**
  62. * Parse integer value
  63. *
  64. * @v text Text
  65. * @ret value Integer value
  66. * @ret rc Return status code
  67. */
  68. int parse_integer ( const char *text, unsigned int *value ) {
  69. char *endp;
  70. /* Sanity check */
  71. assert ( text != NULL );
  72. /* Parse integer */
  73. *value = strtoul ( text, &endp, 0 );
  74. if ( *endp ) {
  75. printf ( "\"%s\": invalid integer value\n", text );
  76. return -EINVAL_INTEGER;
  77. }
  78. return 0;
  79. }
  80. /**
  81. * Parse network device name
  82. *
  83. * @v text Text
  84. * @ret netdev Network device
  85. * @ret rc Return status code
  86. */
  87. int parse_netdev ( const char *text, struct net_device **netdev ) {
  88. /* Sanity check */
  89. assert ( text != NULL );
  90. /* Find network device */
  91. *netdev = find_netdev ( text );
  92. if ( ! *netdev ) {
  93. printf ( "\"%s\": no such network device\n", text );
  94. return -ENODEV;
  95. }
  96. return 0;
  97. }
  98. /**
  99. * Parse flag
  100. *
  101. * @v text Text (ignored)
  102. * @ret flag Flag to set
  103. * @ret rc Return status code
  104. */
  105. int parse_flag ( const char *text __unused, int *flag ) {
  106. /* Set flag */
  107. *flag = 1;
  108. return 0;
  109. }
  110. /**
  111. * Parse key
  112. *
  113. * @v text Text
  114. * @ret key Key
  115. * @ret rc Return status code
  116. */
  117. int parse_key ( const char *text, unsigned int *key ) {
  118. /* Interpret single characters as being a literal key character */
  119. if ( text[0] && ! text[1] ) {
  120. *key = text[0];
  121. return 0;
  122. }
  123. /* Otherwise, interpret as an integer */
  124. return parse_integer ( text, key );
  125. }
  126. /**
  127. * Print command usage message
  128. *
  129. * @v cmd Command descriptor
  130. * @v argv Argument list
  131. */
  132. void print_usage ( struct command_descriptor *cmd, char **argv ) {
  133. printf ( "Usage:\n\n %s %s\n\nSee http://ipxe.org/cmd/%s for further "
  134. "information\n", argv[0], cmd->usage, argv[0] );
  135. }
  136. /**
  137. * Reparse command-line options
  138. *
  139. * @v argc Argument count
  140. * @v argv Argument list
  141. * @v cmd Command descriptor
  142. * @v opts Options (already initialised with default values)
  143. * @ret rc Return status code
  144. */
  145. int reparse_options ( int argc, char **argv, struct command_descriptor *cmd,
  146. void *opts ) {
  147. struct option longopts[ cmd->num_options + 1 /* help */ + 1 /* end */ ];
  148. char shortopts[ cmd->num_options * 3 /* possible "::" */ + 1 /* "h" */
  149. + 1 /* NUL */ ];
  150. unsigned int shortopt_idx = 0;
  151. int ( * parse ) ( const char *text, void *value );
  152. void *value;
  153. unsigned int i;
  154. unsigned int j;
  155. unsigned int num_args;
  156. int c;
  157. int rc;
  158. /* Construct long and short option lists for getopt_long() */
  159. memset ( longopts, 0, sizeof ( longopts ) );
  160. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  161. longopts[i].name = cmd->options[i].longopt;
  162. longopts[i].has_arg = cmd->options[i].has_arg;
  163. longopts[i].val = cmd->options[i].shortopt;
  164. shortopts[shortopt_idx++] = cmd->options[i].shortopt;
  165. assert ( cmd->options[i].has_arg <= optional_argument );
  166. for ( j = cmd->options[i].has_arg ; j > 0 ; j-- )
  167. shortopts[shortopt_idx++] = ':';
  168. }
  169. longopts[i].name = "help";
  170. longopts[i].val = 'h';
  171. shortopts[shortopt_idx++] = 'h';
  172. shortopts[shortopt_idx++] = '\0';
  173. assert ( shortopt_idx <= sizeof ( shortopts ) );
  174. DBGC ( cmd, "Command \"%s\" has options \"%s\", %d-%d args, len %d\n",
  175. argv[0], shortopts, cmd->min_args, cmd->max_args, cmd->len );
  176. /* Parse options */
  177. while ( ( c = getopt_long ( argc, argv, shortopts, longopts,
  178. NULL ) ) >= 0 ) {
  179. switch ( c ) {
  180. case 'h' :
  181. /* Print help */
  182. print_usage ( cmd, argv );
  183. return -ECANCELED_NO_OP;
  184. case '?' :
  185. /* Print usage message */
  186. print_usage ( cmd, argv );
  187. return -EINVAL_UNKNOWN_OPTION;
  188. case ':' :
  189. /* Print usage message */
  190. print_usage ( cmd, argv );
  191. return -EINVAL_MISSING_ARGUMENT;
  192. default:
  193. /* Search for an option to parse */
  194. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  195. if ( c != cmd->options[i].shortopt )
  196. continue;
  197. parse = cmd->options[i].parse;
  198. value = ( opts + cmd->options[i].offset );
  199. if ( ( rc = parse ( optarg, value ) ) != 0 )
  200. return rc;
  201. break;
  202. }
  203. assert ( i < cmd->num_options );
  204. }
  205. }
  206. /* Check remaining arguments */
  207. num_args = ( argc - optind );
  208. if ( ( num_args < cmd->min_args ) || ( num_args > cmd->max_args ) ) {
  209. print_usage ( cmd, argv );
  210. return -ERANGE;
  211. }
  212. return 0;
  213. }
  214. /**
  215. * Parse command-line options
  216. *
  217. * @v argc Argument count
  218. * @v argv Argument list
  219. * @v cmd Command descriptor
  220. * @v opts Options (may be uninitialised)
  221. * @ret rc Return status code
  222. */
  223. int parse_options ( int argc, char **argv, struct command_descriptor *cmd,
  224. void *opts ) {
  225. /* Clear options */
  226. memset ( opts, 0, cmd->len );
  227. return reparse_options ( argc, argv, cmd, opts );
  228. }