getopt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Getopt for GNU.
  2. Copyright (C) 1987, 88, 89, 90, 91, 1992 Free Software Foundation, Inc.
  3. Copyright (C) 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; If not, see <http://www.gnu.org/licenses/>. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <getopt.h>
  17. #if __MESC__
  18. #define static
  19. #endif
  20. /* For communication from `getopt' to the caller.
  21. When `getopt' finds an option that takes an argument,
  22. the argument value is returned here. */
  23. char *optarg = 0;
  24. /* Index in ARGV of the next element to be scanned.
  25. This is used for communication to and from the caller
  26. and for communication between successive calls to `getopt'.
  27. On entry to `getopt', zero means this is the first call; initialize.
  28. When `getopt' returns EOF, this is the index of the first of the
  29. non-option elements that the caller should itself scan.
  30. Otherwise, `optind' communicates from one call to the next
  31. how much of ARGV has been scanned so far. */
  32. int optind = 0;
  33. /* The next char to be scanned in the option-element
  34. in which the last option character we returned was found.
  35. This allows us to pick up the scan where we left off.
  36. If this is zero, or a null string, it means resume the scan
  37. by advancing to the next ARGV-element. */
  38. static char *nextchar;
  39. /* Callers store zero here to inhibit the error message
  40. for unrecognized options. */
  41. int opterr = 1;
  42. /* Handle permutation of arguments. */
  43. /* Describe the part of ARGV that contains non-options that have
  44. been skipped. `first_nonopt' is the index in ARGV of the first of them;
  45. `last_nonopt' is the index after the last of them. */
  46. static int first_nonopt;
  47. static int last_nonopt;
  48. /* Scan elements of ARGV (whose length is ARGC) for option characters
  49. given in OPTSTRING.
  50. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  51. then it is an option element. The characters of this element
  52. (aside from the initial '-') are option characters. If `getopt'
  53. is called repeatedly, it returns successively each of the option characters
  54. from each of the option elements.
  55. If `getopt' finds another option character, it returns that character,
  56. updating `optind' and `nextchar' so that the next call to `getopt' can
  57. resume the scan with the following option character or ARGV-element.
  58. If there are no more option characters, `getopt' returns `EOF'.
  59. Then `optind' is the index in ARGV of the first ARGV-element
  60. that is not an option. (The ARGV-elements have been permuted
  61. so that those that are not options now come last.)
  62. OPTSTRING is a string containing the legitimate option characters.
  63. If an option character is seen that is not listed in OPTSTRING,
  64. return '?' after printing an error message. If you set `opterr' to
  65. zero, the error message is suppressed but we still return '?'.
  66. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  67. so the following text in the same ARGV-element, or the text of the following
  68. ARGV-element, is returned in `optarg'. Two colons mean an option that
  69. wants an optional arg; if there is text in the current ARGV-element,
  70. it is returned in `optarg', otherwise `optarg' is set to zero.
  71. If OPTSTRING starts with `-' or `+', it requests different methods of
  72. handling the non-option ARGV-elements.
  73. Long-named options begin with `--' instead of `-'.
  74. Their names may be abbreviated as long as the abbreviation is unique
  75. or is an exact match for some defined option. If they have an
  76. argument, it follows the option name in the same ARGV-element, separated
  77. from the option name by a `=', or else the in next ARGV-element.
  78. When `getopt' finds a long-named option, it returns 0 if that option's
  79. `flag' field is nonzero, the value of the option's `val' field
  80. if the `flag' field is zero.
  81. The elements of ARGV aren't really const, because we permute them.
  82. But we pretend they're const in the prototype to be compatible
  83. with other systems.
  84. LONGOPTS is a vector of `struct option' terminated by an
  85. element containing a name which is zero.
  86. LONGIND returns the index in LONGOPT of the long-named option found.
  87. It is only valid when a long-named option has been found by the most
  88. recent call.
  89. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  90. long-named options. */
  91. int
  92. _getopt_internal (int argc, char *const
  93. *argv, char const *optstring, struct option const *longopts, int *longind, int long_only)
  94. {
  95. int option_index;
  96. optarg = 0;
  97. /* Initialize the internal data when the first call is made.
  98. Start processing options with ARGV-element 1 (since ARGV-element 0
  99. is the program name); the sequence of previously skipped
  100. non-option ARGV-elements is empty. */
  101. if (optind == 0)
  102. {
  103. first_nonopt = last_nonopt = optind = 1;
  104. nextchar = NULL;
  105. }
  106. if (nextchar == NULL || *nextchar == '\0')
  107. {
  108. /* If we have done all the ARGV-elements, stop the scan
  109. and back over any non-options that we skipped and permuted. */
  110. if (optind == argc)
  111. {
  112. /* Set the next-arg-index to point at the non-options
  113. that we previously skipped, so the caller will digest them. */
  114. if (first_nonopt != last_nonopt)
  115. optind = first_nonopt;
  116. return EOF;
  117. }
  118. /* If we have come to a non-option and did not permute it,
  119. either stop the scan or describe it to the caller and pass it by. */
  120. if ((argv[optind][0] != '-' || argv[optind][1] == '\0'))
  121. return EOF;
  122. /* We have found another option-ARGV-element.
  123. Start decoding its characters. */
  124. nextchar = (argv[optind] + 1 + (longopts != NULL && argv[optind][1] == '-'));
  125. }
  126. if (longopts != NULL && ((argv[optind][0] == '-' && (argv[optind][1] == '-' || long_only))))
  127. {
  128. const struct option *p;
  129. char *s = nextchar;
  130. int exact = 0;
  131. int ambig = 0;
  132. const struct option *pfound = NULL;
  133. int indfound;
  134. while (*s && *s != '=')
  135. s++;
  136. /* Test all options for either exact match or abbreviated matches. */
  137. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  138. if (!strncmp (p->name, nextchar, s - nextchar))
  139. {
  140. if (s - nextchar == strlen (p->name))
  141. {
  142. /* Exact match found. */
  143. pfound = p;
  144. indfound = option_index;
  145. exact = 1;
  146. break;
  147. }
  148. else if (pfound == NULL)
  149. {
  150. /* First nonexact match found. */
  151. pfound = p;
  152. indfound = option_index;
  153. }
  154. else
  155. /* Second nonexact match found. */
  156. ambig = 1;
  157. }
  158. if (ambig && !exact)
  159. {
  160. if (opterr)
  161. fprintf (stderr, "%s: option `%s' is ambiguous\n", argv[0], argv[optind]);
  162. nextchar += strlen (nextchar);
  163. optind++;
  164. return '?';
  165. }
  166. if (pfound != NULL)
  167. {
  168. option_index = indfound;
  169. optind++;
  170. if (*s)
  171. {
  172. /* Don't test has_arg with >, because some C compilers don't
  173. allow it to be used on enums. */
  174. if (pfound->has_arg)
  175. optarg = s + 1;
  176. else
  177. {
  178. if (opterr)
  179. {
  180. if (argv[optind - 1][1] == '-')
  181. /* --option */
  182. fprintf (stderr,
  183. "%s: option `--%s' doesn't allow an argument\n", argv[0], pfound->name);
  184. else
  185. /* +option or -option */
  186. fprintf (stderr,
  187. "%s: option `%c%s' doesn't allow an argument\n",
  188. argv[0], argv[optind - 1][0], pfound->name);
  189. }
  190. nextchar += strlen (nextchar);
  191. return '?';
  192. }
  193. }
  194. else if (pfound->has_arg == 1)
  195. {
  196. if (optind < argc)
  197. optarg = argv[optind++];
  198. else
  199. {
  200. if (opterr)
  201. fprintf (stderr, "%s: option `%s' requires an argument\n", argv[0], argv[optind - 1]);
  202. nextchar += strlen (nextchar);
  203. return '?';
  204. }
  205. }
  206. nextchar += strlen (nextchar);
  207. if (longind != NULL)
  208. *longind = option_index;
  209. if (pfound->flag)
  210. {
  211. *(pfound->flag) = pfound->val;
  212. return 0;
  213. }
  214. return pfound->val;
  215. }
  216. /* Can't find it as a long option. If this is not getopt_long_only,
  217. or the option starts with '--' or is not a valid short
  218. option, then it's an error.
  219. Otherwise interpret it as a short option. */
  220. if (!long_only || argv[optind][1] == '-' || strchr (optstring, *nextchar) == NULL)
  221. {
  222. if (opterr)
  223. {
  224. if (argv[optind][1] == '-')
  225. /* --option */
  226. fprintf (stderr, "%s: unrecognized option `--%s'\n", argv[0], nextchar);
  227. else
  228. /* +option or -option */
  229. fprintf (stderr, "%s: unrecognized option `%c%s'\n", argv[0], argv[optind][0], nextchar);
  230. }
  231. nextchar += strlen (nextchar);
  232. optind++;
  233. return '?';
  234. }
  235. }
  236. /* Look at and handle the next option-character. */
  237. {
  238. char c = *nextchar++;
  239. char *temp = strchr (optstring, c);
  240. /* Increment `optind' when we start to process its last character. */
  241. if (*nextchar == '\0')
  242. optind++;
  243. if (temp == NULL || c == ':')
  244. {
  245. if (opterr)
  246. {
  247. if (c < 040 || c >= 0177)
  248. fprintf (stderr, "%s: unrecognized option, character code 0%o\n", argv[0], c);
  249. else
  250. fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
  251. }
  252. return '?';
  253. }
  254. if (temp[1] == ':')
  255. {
  256. if (temp[2] == ':')
  257. {
  258. /* This is an option that accepts an argument optionally. */
  259. if (*nextchar != '\0')
  260. {
  261. optarg = nextchar;
  262. optind++;
  263. }
  264. else
  265. optarg = 0;
  266. nextchar = NULL;
  267. }
  268. else
  269. {
  270. /* This is an option that requires an argument. */
  271. if (*nextchar != 0)
  272. {
  273. optarg = nextchar;
  274. /* If we end this ARGV-element by taking the rest as an arg,
  275. we must advance to the next element now. */
  276. optind++;
  277. }
  278. else if (optind == argc)
  279. {
  280. if (opterr)
  281. fprintf (stderr, "%s: option `-%c' requires an argument\n", argv[0], c);
  282. c = '?';
  283. }
  284. else
  285. /* We already incremented `optind' once;
  286. increment it again when taking next ARGV-elt as argument. */
  287. optarg = argv[optind++];
  288. nextchar = NULL;
  289. }
  290. }
  291. return c;
  292. }
  293. }
  294. int
  295. getopt (int argc, char *const *argv, char const *options)
  296. {
  297. return _getopt_internal (argc, argv, options, (const struct option *) 0, (int *) 0, 0);
  298. }
  299. int
  300. getopt_long (int argc, char *const *argv, char const *options,
  301. struct option const *long_options, int *opt_index)
  302. {
  303. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  304. }
  305. int
  306. getopt_long_only (int argc, char *const *argv, char const *options,
  307. struct option const *long_options, int *opt_index)
  308. {
  309. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  310. }