Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

parseopt.c 6.9KB

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