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

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