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.9KB

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