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

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