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

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