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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 flag
  101. *
  102. * @v text Text (ignored)
  103. * @ret flag Flag to set
  104. * @ret rc Return status code
  105. */
  106. int parse_flag ( const char *text __unused, int *flag ) {
  107. /* Set flag */
  108. *flag = 1;
  109. return 0;
  110. }
  111. /**
  112. * Print command usage message
  113. *
  114. * @v cmd Command descriptor
  115. * @v argv Argument list
  116. */
  117. void print_usage ( struct command_descriptor *cmd, char **argv ) {
  118. printf ( "Usage:\n\n %s %s\n\nSee http://ipxe.org/cmd/%s for further "
  119. "information\n", argv[0], cmd->usage, argv[0] );
  120. }
  121. /**
  122. * Reparse command-line options
  123. *
  124. * @v argc Argument count
  125. * @v argv Argument list
  126. * @v cmd Command descriptor
  127. * @v opts Options (already initialised with default values)
  128. * @ret rc Return status code
  129. */
  130. int reparse_options ( int argc, char **argv, struct command_descriptor *cmd,
  131. void *opts ) {
  132. struct option longopts[ cmd->num_options + 1 /* help */ + 1 /* end */ ];
  133. char shortopts[ cmd->num_options * 3 /* possible "::" */ + 1 /* "h" */
  134. + 1 /* NUL */ ];
  135. unsigned int shortopt_idx = 0;
  136. int ( * parse ) ( const char *text, void *value );
  137. void *value;
  138. unsigned int i;
  139. unsigned int j;
  140. unsigned int num_args;
  141. int c;
  142. int rc;
  143. /* Construct long and short option lists for getopt_long() */
  144. memset ( longopts, 0, sizeof ( longopts ) );
  145. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  146. longopts[i].name = cmd->options[i].longopt;
  147. longopts[i].has_arg = cmd->options[i].has_arg;
  148. longopts[i].val = cmd->options[i].shortopt;
  149. shortopts[shortopt_idx++] = cmd->options[i].shortopt;
  150. assert ( cmd->options[i].has_arg <= optional_argument );
  151. for ( j = cmd->options[i].has_arg ; j > 0 ; j-- )
  152. shortopts[shortopt_idx++] = ':';
  153. }
  154. longopts[i].name = "help";
  155. longopts[i].val = 'h';
  156. shortopts[shortopt_idx++] = 'h';
  157. shortopts[shortopt_idx++] = '\0';
  158. assert ( shortopt_idx <= sizeof ( shortopts ) );
  159. DBGC ( cmd, "Command \"%s\" has options \"%s\", %d-%d args, len %d\n",
  160. argv[0], shortopts, cmd->min_args, cmd->max_args, cmd->len );
  161. /* Parse options */
  162. while ( ( c = getopt_long ( argc, argv, shortopts, longopts,
  163. NULL ) ) >= 0 ) {
  164. switch ( c ) {
  165. case 'h' :
  166. /* Print help */
  167. print_usage ( cmd, argv );
  168. return -ECANCELED_NO_OP;
  169. case '?' :
  170. /* Print usage message */
  171. print_usage ( cmd, argv );
  172. return -EINVAL_UNKNOWN_OPTION;
  173. case ':' :
  174. /* Print usage message */
  175. print_usage ( cmd, argv );
  176. return -EINVAL_MISSING_ARGUMENT;
  177. default:
  178. /* Search for an option to parse */
  179. for ( i = 0 ; i < cmd->num_options ; i++ ) {
  180. if ( c != cmd->options[i].shortopt )
  181. continue;
  182. parse = cmd->options[i].parse;
  183. value = ( opts + cmd->options[i].offset );
  184. if ( ( rc = parse ( optarg, value ) ) != 0 )
  185. return rc;
  186. break;
  187. }
  188. assert ( i < cmd->num_options );
  189. }
  190. }
  191. /* Check remaining arguments */
  192. num_args = ( argc - optind );
  193. if ( ( num_args < cmd->min_args ) || ( num_args > cmd->max_args ) ) {
  194. print_usage ( cmd, argv );
  195. return -ERANGE;
  196. }
  197. return 0;
  198. }
  199. /**
  200. * Parse command-line options
  201. *
  202. * @v argc Argument count
  203. * @v argv Argument list
  204. * @v cmd Command descriptor
  205. * @v opts Options (may be uninitialised)
  206. * @ret rc Return status code
  207. */
  208. int parse_options ( int argc, char **argv, struct command_descriptor *cmd,
  209. void *opts ) {
  210. /* Clear options */
  211. memset ( opts, 0, cmd->len );
  212. return reparse_options ( argc, argv, cmd, opts );
  213. }