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.

getopt.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2006 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. #include <stdint.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. /** @file
  23. *
  24. * Parse command-line options
  25. *
  26. */
  27. /**
  28. * Option argument
  29. *
  30. * This will point to the argument for the most recently returned
  31. * option, if applicable.
  32. */
  33. char *optarg;
  34. /**
  35. * Current option index
  36. *
  37. * This is an index into the argv[] array. When getopt() returns -1,
  38. * @c optind is the index to the first element that is not an option.
  39. */
  40. int optind;
  41. /**
  42. * Current option character index
  43. *
  44. * This is an index into the current element of argv[].
  45. */
  46. int nextchar;
  47. /**
  48. * Unrecognised option
  49. *
  50. * When an unrecognised option is encountered, the actual option
  51. * character is stored in @c optopt.
  52. */
  53. int optopt;
  54. /**
  55. * Get option argument from argv[] array
  56. *
  57. * @v argc Argument count
  58. * @v argv Argument list
  59. * @ret argument Option argument, or NULL
  60. *
  61. * Grab the next element of argv[], if it exists and is not an option.
  62. */
  63. static const char * get_argv_argument ( int argc, char * const argv[] ) {
  64. char *arg;
  65. /* Don't overrun argv[] */
  66. if ( optind >= argc )
  67. return NULL;
  68. arg = argv[optind];
  69. /* If next argv element is an option, then it's not usable as
  70. * an argument.
  71. */
  72. if ( *arg == '-' )
  73. return NULL;
  74. /** Consume this argv element, and return it */
  75. optind++;
  76. return arg;
  77. }
  78. /**
  79. * Match long option
  80. *
  81. * @v argc Argument count
  82. * @v argv Argument list
  83. * @v opttext Option text within current argv[] element
  84. * @v longopt Long option specification
  85. * @ret option Option to return from getopt()
  86. * @ret matched Found a match for this long option
  87. */
  88. static int match_long_option ( int argc, char * const argv[],
  89. const char *opttext,
  90. const struct option *longopt, int *option ) {
  91. size_t optlen;
  92. const char *argument = NULL;
  93. /* Compare option name */
  94. optlen = strlen ( longopt->name );
  95. if ( strncmp ( opttext, longopt->name, optlen ) != 0 )
  96. return 0;
  97. /* Check for inline argument */
  98. if ( opttext[optlen] == '=' ) {
  99. argument = &opttext[ optlen + 1 ];
  100. } else if ( opttext[optlen] ) {
  101. /* Long option with trailing garbage - no match */
  102. return 0;
  103. }
  104. /* Consume this argv element */
  105. optind++;
  106. /* If we want an argument but don't have one yet, try to grab
  107. * the next argv element
  108. */
  109. if ( ( longopt->has_arg != no_argument ) && ( ! argument ) )
  110. argument = get_argv_argument ( argc, argv );
  111. /* If we need an argument but don't have one, sulk */
  112. if ( ( longopt->has_arg == required_argument ) && ( ! argument ) ) {
  113. printf ( "Option \"%s\" requires an argument\n",
  114. longopt->name );
  115. *option = ':';
  116. return 1;
  117. }
  118. /* If we have an argument where we shouldn't have one, sulk */
  119. if ( ( longopt->has_arg == no_argument ) && argument ) {
  120. printf ( "Option \"%s\" takes no argument\n", longopt->name );
  121. *option = ':';
  122. return 1;
  123. }
  124. /* Store values and return success */
  125. optarg = ( char * ) argument;
  126. if ( longopt->flag ) {
  127. *(longopt->flag) = longopt->val;
  128. *option = 0;
  129. } else {
  130. *option = longopt->val;
  131. }
  132. return 1;
  133. }
  134. /**
  135. * Match short option
  136. *
  137. * @v argc Argument count
  138. * @v argv Argument list
  139. * @v opttext Option text within current argv[] element
  140. * @v shortopt Option character from option specification
  141. * @ret option Option to return from getopt()
  142. * @ret matched Found a match for this short option
  143. */
  144. static int match_short_option ( int argc, char * const argv[],
  145. const char *opttext, int shortopt,
  146. enum getopt_argument_requirement has_arg,
  147. int *option ) {
  148. const char *argument = NULL;
  149. /* Compare option character */
  150. if ( *opttext != shortopt )
  151. return 0;
  152. /* Consume option character */
  153. opttext++;
  154. nextchar++;
  155. if ( *opttext ) {
  156. if ( has_arg != no_argument ) {
  157. /* Consume remainder of element as inline argument */
  158. argument = opttext;
  159. optind++;
  160. nextchar = 0;
  161. }
  162. } else {
  163. /* Reached end of argv element */
  164. optind++;
  165. nextchar = 0;
  166. }
  167. /* If we want an argument but don't have one yet, try to grab
  168. * the next argv element
  169. */
  170. if ( ( has_arg != no_argument ) && ( ! argument ) )
  171. argument = get_argv_argument ( argc, argv );
  172. /* If we need an argument but don't have one, sulk */
  173. if ( ( has_arg == required_argument ) && ( ! argument ) ) {
  174. printf ( "Option \"%c\" requires an argument\n", shortopt );
  175. *option = ':';
  176. return 1;
  177. }
  178. /* Store values and return success */
  179. optarg = ( char * ) argument;
  180. *option = shortopt;
  181. return 1;
  182. }
  183. /**
  184. * Parse command-line options
  185. *
  186. * @v argc Argument count
  187. * @v argv Argument list
  188. * @v optstring Option specification string
  189. * @v longopts Long option specification table
  190. * @ret longindex Index of long option (or NULL)
  191. * @ret option Option found, or -1 for no more options
  192. *
  193. * Note that the caller must arrange for reset_getopt() to be called
  194. * before each set of calls to getopt_long(). In Etherboot, this is
  195. * done automatically by execv().
  196. */
  197. int getopt_long ( int argc, char * const argv[], const char *optstring,
  198. const struct option *longopts, int *longindex ) {
  199. const char *opttext = argv[optind];
  200. const struct option *longopt;
  201. int shortopt;
  202. enum getopt_argument_requirement has_arg;
  203. int option;
  204. /* Check for end of argv array */
  205. if ( optind >= argc )
  206. return -1;
  207. /* Check for end of options */
  208. if ( *(opttext++) != '-' )
  209. return -1;
  210. /* Check for long options */
  211. if ( *(opttext++) == '-' ) {
  212. for ( longopt = longopts ; longopt->name ; longopt++ ) {
  213. if ( ! match_long_option ( argc, argv, opttext,
  214. longopt, &option ) )
  215. continue;
  216. if ( longindex )
  217. *longindex = ( longopt - longopts );
  218. return option;
  219. }
  220. optopt = '?';
  221. printf ( "Unrecognised option \"--%s\"\n", opttext );
  222. return '?';
  223. }
  224. /* Check for short options */
  225. if ( nextchar < 1 )
  226. nextchar = 1;
  227. opttext = ( argv[optind] + nextchar );
  228. while ( ( shortopt = *(optstring++) ) ) {
  229. has_arg = no_argument;
  230. while ( *optstring == ':' ) {
  231. has_arg++;
  232. optstring++;
  233. }
  234. if ( match_short_option ( argc, argv, opttext, shortopt,
  235. has_arg, &option ) ) {
  236. return option;
  237. }
  238. }
  239. optopt = *opttext;
  240. printf ( "Unrecognised option \"-%c\"\n", optopt );
  241. return '?';
  242. }