getopt.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /* Getopt for GNU.
  2. NOTE: getopt is now part of the C library, so if you don't know what
  3. "Keep this file name-space clean" means, talk to drepper@gnu.org
  4. before changing it!
  5. Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
  6. Free Software Foundation, Inc.
  7. This file is part of the GNU C Library.
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with the GNU C Library; if not, write to the Free
  18. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. 02111-1307 USA. */
  20. /*
  21. * Modified for uClibc by Manuel Novoa III on 1/5/01.
  22. * Modified once again for uClibc by Erik Andersen 8/7/02
  23. */
  24. /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
  25. Ditto for AIX 3.2 and <stdlib.h>. */
  26. #ifndef _NO_PROTO
  27. # define _NO_PROTO
  28. #endif
  29. #ifdef HAVE_CONFIG_H
  30. # include <config.h>
  31. #endif
  32. #include <stdio.h>
  33. /* Comment out all this code if we are using the GNU C Library, and are not
  34. actually compiling the library itself. This code is part of the GNU C
  35. Library, but also included in many other GNU distributions. Compiling
  36. and linking in this code is a waste when using the GNU C library
  37. (especially if it is a shared library). Rather than having every GNU
  38. program understand `configure --with-gnu-libc' and omit the object files,
  39. it is simpler to just do this in the source for each such file. */
  40. #define GETOPT_INTERFACE_VERSION 2
  41. #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
  42. # include <gnu-versions.h>
  43. # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
  44. # define ELIDE_CODE
  45. # endif
  46. #endif
  47. #ifndef ELIDE_CODE
  48. /* This needs to come after some library #include
  49. to get __GNU_LIBRARY__ defined. */
  50. #if defined __GNU_LIBRARY__ || defined __UCLIBC__
  51. /* Don't include stdlib.h for non-GNU C libraries because some of them
  52. contain conflicting prototypes for getopt. */
  53. # include <stdlib.h>
  54. # include <unistd.h>
  55. #endif /* GNU C library. */
  56. #include <string.h>
  57. #ifdef VMS
  58. # include <unixlib.h>
  59. #endif
  60. #ifdef _LIBC
  61. # include <libintl.h>
  62. #else
  63. # include "gettext.h"
  64. # define _(msgid) gettext (msgid)
  65. #endif
  66. /* Treat '-W foo' the same as the long option '--foo',
  67. * disabled for the moment since it costs about 2k... */
  68. #undef SPECIAL_TREATMENT_FOR_W
  69. #if defined _LIBC && defined USE_IN_LIBIO
  70. # include <wchar.h>
  71. #endif
  72. #ifndef attribute_hidden
  73. # define attribute_hidden
  74. #endif
  75. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  76. but it behaves differently for the user, since it allows the user
  77. to intersperse the options with the other arguments.
  78. As `getopt' works, it permutes the elements of ARGV so that,
  79. when it is done, all the options precede everything else. Thus
  80. all application programs are extended to handle flexible argument order.
  81. Setting the environment variable POSIXLY_CORRECT disables permutation.
  82. Then the behavior is completely standard.
  83. GNU application programs can use a third alternative mode in which
  84. they can distinguish the relative order of options and other arguments. */
  85. #include <getopt.h>
  86. #include "getopt_int.h"
  87. /* For communication from `getopt' to the caller.
  88. When `getopt' finds an option that takes an argument,
  89. the argument value is returned here.
  90. Also, when `ordering' is RETURN_IN_ORDER,
  91. each non-option ARGV-element is returned here. */
  92. char *optarg;
  93. /* Index in ARGV of the next element to be scanned.
  94. This is used for communication to and from the caller
  95. and for communication between successive calls to `getopt'.
  96. On entry to `getopt', zero means this is the first call; initialize.
  97. When `getopt' returns -1, this is the index of the first of the
  98. non-option elements that the caller should itself scan.
  99. Otherwise, `optind' communicates from one call to the next
  100. how much of ARGV has been scanned so far. */
  101. /* 1003.2 says this must be 1 before any call. */
  102. int optind = 1;
  103. /* Callers store zero here to inhibit the error message
  104. for unrecognized options. */
  105. int opterr = 1;
  106. /* Set to an option character which was unrecognized.
  107. This must be initialized on some systems to avoid linking in the
  108. system's own getopt implementation. */
  109. int optopt = '?';
  110. /* Keep a global copy of all internal members of getopt_data. */
  111. static struct _getopt_data getopt_data;
  112. #if !defined __GNU_LIBRARY__ && !defined __UCLIBC__
  113. /* Avoid depending on library functions or files
  114. whose names are inconsistent. */
  115. #ifndef getenv
  116. extern char *getenv ();
  117. #endif
  118. #endif /* not __GNU_LIBRARY__ */
  119. #ifdef _LIBC
  120. /* Stored original parameters.
  121. XXX This is no good solution. We should rather copy the args so
  122. that we can compare them later. But we must not use malloc(3). */
  123. # ifdef USE_NONOPTION_FLAGS
  124. extern int __libc_argc;
  125. extern char **__libc_argv;
  126. /* Bash 2.0 gives us an environment variable containing flags
  127. indicating ARGV elements that should not be considered arguments. */
  128. /* Defined in getopt_init.c */
  129. extern char *__getopt_nonoption_flags;
  130. # define SWAP_FLAGS(ch1, ch2) \
  131. if (d->__nonoption_flags_len > 0) \
  132. { \
  133. char __tmp = __getopt_nonoption_flags[ch1]; \
  134. __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
  135. __getopt_nonoption_flags[ch2] = __tmp; \
  136. }
  137. # else
  138. # define SWAP_FLAGS(ch1, ch2)
  139. # endif
  140. #else /* !_LIBC */
  141. # define SWAP_FLAGS(ch1, ch2)
  142. #endif /* _LIBC */
  143. /* Exchange two adjacent subsequences of ARGV.
  144. One subsequence is elements [first_nonopt,last_nonopt)
  145. which contains all the non-options that have been skipped so far.
  146. The other is elements [last_nonopt,optind), which contains all
  147. the options processed since those non-options were skipped.
  148. `first_nonopt' and `last_nonopt' are relocated so that they describe
  149. the new indices of the non-options in ARGV after they are moved. */
  150. static void
  151. exchange (char **argv, struct _getopt_data *d)
  152. {
  153. int bottom = d->__first_nonopt;
  154. int middle = d->__last_nonopt;
  155. int top = d->optind;
  156. char *tem;
  157. /* Exchange the shorter segment with the far end of the longer segment.
  158. That puts the shorter segment into the right place.
  159. It leaves the longer segment in the right place overall,
  160. but it consists of two parts that need to be swapped next. */
  161. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  162. /* First make sure the handling of the `__getopt_nonoption_flags'
  163. string can work normally. Our top argument must be in the range
  164. of the string. */
  165. if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
  166. {
  167. /* We must extend the array. The user plays games with us and
  168. presents new arguments. */
  169. char *new_str = malloc (top + 1);
  170. if (new_str == NULL)
  171. d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
  172. else
  173. {
  174. memset (mempcpy (new_str, __getopt_nonoption_flags,
  175. d->__nonoption_flags_max_len),
  176. '\0', top + 1 - d->__nonoption_flags_max_len);
  177. d->__nonoption_flags_max_len = top + 1;
  178. __getopt_nonoption_flags = new_str;
  179. }
  180. }
  181. #endif
  182. while (top > middle && middle > bottom)
  183. {
  184. if (top - middle > middle - bottom)
  185. {
  186. /* Bottom segment is the short one. */
  187. int len = middle - bottom;
  188. register int i;
  189. /* Swap it with the top part of the top segment. */
  190. for (i = 0; i < len; i++)
  191. {
  192. tem = argv[bottom + i];
  193. argv[bottom + i] = argv[top - (middle - bottom) + i];
  194. argv[top - (middle - bottom) + i] = tem;
  195. SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
  196. }
  197. /* Exclude the moved bottom segment from further swapping. */
  198. top -= len;
  199. }
  200. else
  201. {
  202. /* Top segment is the short one. */
  203. int len = top - middle;
  204. register int i;
  205. /* Swap it with the bottom part of the bottom segment. */
  206. for (i = 0; i < len; i++)
  207. {
  208. tem = argv[bottom + i];
  209. argv[bottom + i] = argv[middle + i];
  210. argv[middle + i] = tem;
  211. SWAP_FLAGS (bottom + i, middle + i);
  212. }
  213. /* Exclude the moved top segment from further swapping. */
  214. bottom += len;
  215. }
  216. }
  217. /* Update records for the slots the non-options now occupy. */
  218. d->__first_nonopt += (d->optind - d->__last_nonopt);
  219. d->__last_nonopt = d->optind;
  220. }
  221. /* Initialize the internal data when the first call is made. */
  222. static const char *
  223. _getopt_initialize (attribute_unused int argc, attribute_unused char *const *argv, const char *optstring,
  224. struct _getopt_data *d)
  225. {
  226. /* Start processing options with ARGV-element 1 (since ARGV-element 0
  227. is the program name); the sequence of previously skipped
  228. non-option ARGV-elements is empty. */
  229. d->__first_nonopt = d->__last_nonopt = d->optind;
  230. d->__nextchar = NULL;
  231. d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
  232. /* Determine how to handle the ordering of options and nonoptions. */
  233. if (optstring[0] == '-')
  234. {
  235. d->__ordering = RETURN_IN_ORDER;
  236. ++optstring;
  237. }
  238. else if (optstring[0] == '+')
  239. {
  240. d->__ordering = REQUIRE_ORDER;
  241. ++optstring;
  242. }
  243. else if (d->__posixly_correct)
  244. d->__ordering = REQUIRE_ORDER;
  245. else
  246. d->__ordering = PERMUTE;
  247. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  248. if (!d->__posixly_correct
  249. && argc == __libc_argc && argv == __libc_argv)
  250. {
  251. if (d->__nonoption_flags_max_len == 0)
  252. {
  253. if (__getopt_nonoption_flags == NULL
  254. || __getopt_nonoption_flags[0] == '\0')
  255. d->__nonoption_flags_max_len = -1;
  256. else
  257. {
  258. const char *orig_str = __getopt_nonoption_flags;
  259. int len = d->__nonoption_flags_max_len = strlen (orig_str);
  260. if (d->__nonoption_flags_max_len < argc)
  261. d->__nonoption_flags_max_len = argc;
  262. __getopt_nonoption_flags =
  263. (char *) malloc (d->__nonoption_flags_max_len);
  264. if (__getopt_nonoption_flags == NULL)
  265. d->__nonoption_flags_max_len = -1;
  266. else
  267. memset (mempcpy (__getopt_nonoption_flags, orig_str, len),
  268. '\0', d->__nonoption_flags_max_len - len);
  269. }
  270. }
  271. d->__nonoption_flags_len = d->__nonoption_flags_max_len;
  272. }
  273. else
  274. d->__nonoption_flags_len = 0;
  275. #endif
  276. return optstring;
  277. }
  278. /* Scan elements of ARGV (whose length is ARGC) for option characters
  279. given in OPTSTRING.
  280. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  281. then it is an option element. The characters of this element
  282. (aside from the initial '-') are option characters. If `getopt'
  283. is called repeatedly, it returns successively each of the option characters
  284. from each of the option elements.
  285. If `getopt' finds another option character, it returns that character,
  286. updating `optind' and `nextchar' so that the next call to `getopt' can
  287. resume the scan with the following option character or ARGV-element.
  288. If there are no more option characters, `getopt' returns -1.
  289. Then `optind' is the index in ARGV of the first ARGV-element
  290. that is not an option. (The ARGV-elements have been permuted
  291. so that those that are not options now come last.)
  292. OPTSTRING is a string containing the legitimate option characters.
  293. If an option character is seen that is not listed in OPTSTRING,
  294. return '?' after printing an error message. If you set `opterr' to
  295. zero, the error message is suppressed but we still return '?'.
  296. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  297. so the following text in the same ARGV-element, or the text of the following
  298. ARGV-element, is returned in `optarg'. Two colons mean an option that
  299. wants an optional arg; if there is text in the current ARGV-element,
  300. it is returned in `optarg', otherwise `optarg' is set to zero.
  301. If OPTSTRING starts with `-' or `+', it requests different methods of
  302. handling the non-option ARGV-elements.
  303. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  304. Long-named options begin with `--' instead of `-'.
  305. Their names may be abbreviated as long as the abbreviation is unique
  306. or is an exact match for some defined option. If they have an
  307. argument, it follows the option name in the same ARGV-element, separated
  308. from the option name by a `=', or else the in next ARGV-element.
  309. When `getopt' finds a long-named option, it returns 0 if that option's
  310. `flag' field is nonzero, the value of the option's `val' field
  311. if the `flag' field is zero.
  312. The elements of ARGV aren't really const, because we permute them.
  313. But we pretend they're const in the prototype to be compatible
  314. with other systems.
  315. LONGOPTS is a vector of `struct option' terminated by an
  316. element containing a name which is zero.
  317. LONGIND returns the index in LONGOPT of the long-named option found.
  318. It is only valid when a long-named option has been found by the most
  319. recent call.
  320. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  321. long-named options. */
  322. static int
  323. _getopt_internal_r (int argc, char *const *argv, const char *optstring,
  324. const struct option *longopts, int *longind,
  325. int long_only, struct _getopt_data *d)
  326. {
  327. int print_errors = d->opterr;
  328. if (optstring[0] == ':')
  329. print_errors = 0;
  330. if (argc < 1)
  331. return -1;
  332. d->optarg = NULL;
  333. if (d->optind == 0 || !d->__initialized)
  334. {
  335. if (d->optind == 0)
  336. d->optind = 1; /* Don't scan ARGV[0], the program name. */
  337. optstring = _getopt_initialize (argc, argv, optstring, d);
  338. d->__initialized = 1;
  339. }
  340. /* Test whether ARGV[optind] points to a non-option argument.
  341. Either it does not have option syntax, or there is an environment flag
  342. from the shell indicating it is not an option. The later information
  343. is only used when the used in the GNU libc. */
  344. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  345. # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
  346. || (d->optind < d->__nonoption_flags_len \
  347. && __getopt_nonoption_flags[d->optind] == '1'))
  348. #else
  349. # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
  350. #endif
  351. if (d->__nextchar == NULL || *d->__nextchar == '\0')
  352. {
  353. /* Advance to the next ARGV-element. */
  354. /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
  355. moved back by the user (who may also have changed the arguments). */
  356. if (d->__last_nonopt > d->optind)
  357. d->__last_nonopt = d->optind;
  358. if (d->__first_nonopt > d->optind)
  359. d->__first_nonopt = d->optind;
  360. if (d->__ordering == PERMUTE)
  361. {
  362. /* If we have just processed some options following some non-options,
  363. exchange them so that the options come first. */
  364. if (d->__first_nonopt != d->__last_nonopt
  365. && d->__last_nonopt != d->optind)
  366. exchange ((char **) argv, d);
  367. else if (d->__last_nonopt != d->optind)
  368. d->__first_nonopt = d->optind;
  369. /* Skip any additional non-options
  370. and extend the range of non-options previously skipped. */
  371. while (d->optind < argc && NONOPTION_P)
  372. d->optind++;
  373. d->__last_nonopt = d->optind;
  374. }
  375. /* The special ARGV-element `--' means premature end of options.
  376. Skip it like a null option,
  377. then exchange with previous non-options as if it were an option,
  378. then skip everything else like a non-option. */
  379. if (d->optind != argc && !strcmp (argv[d->optind], "--"))
  380. {
  381. d->optind++;
  382. if (d->__first_nonopt != d->__last_nonopt
  383. && d->__last_nonopt != d->optind)
  384. exchange ((char **) argv, d);
  385. else if (d->__first_nonopt == d->__last_nonopt)
  386. d->__first_nonopt = d->optind;
  387. d->__last_nonopt = argc;
  388. d->optind = argc;
  389. }
  390. /* If we have done all the ARGV-elements, stop the scan
  391. and back over any non-options that we skipped and permuted. */
  392. if (d->optind == argc)
  393. {
  394. /* Set the next-arg-index to point at the non-options
  395. that we previously skipped, so the caller will digest them. */
  396. if (d->__first_nonopt != d->__last_nonopt)
  397. d->optind = d->__first_nonopt;
  398. return -1;
  399. }
  400. /* If we have come to a non-option and did not permute it,
  401. either stop the scan or describe it to the caller and pass it by. */
  402. if (NONOPTION_P)
  403. {
  404. if (d->__ordering == REQUIRE_ORDER)
  405. return -1;
  406. d->optarg = argv[d->optind++];
  407. return 1;
  408. }
  409. /* We have found another option-ARGV-element.
  410. Skip the initial punctuation. */
  411. d->__nextchar = (argv[d->optind] + 1
  412. + (longopts != NULL && argv[d->optind][1] == '-'));
  413. }
  414. /* Decode the current option-ARGV-element. */
  415. /* Check whether the ARGV-element is a long option.
  416. If long_only and the ARGV-element has the form "-f", where f is
  417. a valid short option, don't consider it an abbreviated form of
  418. a long option that starts with f. Otherwise there would be no
  419. way to give the -f short option.
  420. On the other hand, if there's a long option "fubar" and
  421. the ARGV-element is "-fu", do consider that an abbreviation of
  422. the long option, just like "--fu", and not "-f" with arg "u".
  423. This distinction seems to be the most useful approach. */
  424. if (longopts != NULL
  425. && (argv[d->optind][1] == '-'
  426. || (long_only && (argv[d->optind][2]
  427. || !strchr (optstring, argv[d->optind][1])))))
  428. {
  429. char *nameend;
  430. const struct option *p;
  431. const struct option *pfound = NULL;
  432. int exact = 0;
  433. int ambig = 0;
  434. int indfound = -1;
  435. int option_index;
  436. for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
  437. /* Do nothing. */ ;
  438. /* Test all long options for either exact match
  439. or abbreviated matches. */
  440. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  441. if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
  442. {
  443. if ((unsigned int) (nameend - d->__nextchar)
  444. == (unsigned int) strlen (p->name))
  445. {
  446. /* Exact match found. */
  447. pfound = p;
  448. indfound = option_index;
  449. exact = 1;
  450. break;
  451. }
  452. else if (pfound == NULL)
  453. {
  454. /* First nonexact match found. */
  455. pfound = p;
  456. indfound = option_index;
  457. }
  458. else if (long_only
  459. || pfound->has_arg != p->has_arg
  460. || pfound->flag != p->flag
  461. || pfound->val != p->val)
  462. /* Second or later nonexact match found. */
  463. ambig = 1;
  464. }
  465. if (ambig && !exact)
  466. {
  467. if (print_errors)
  468. {
  469. #if defined _LIBC && defined USE_IN_LIBIO
  470. char *buf;
  471. if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
  472. argv[0], argv[d->optind]) >= 0)
  473. {
  474. _IO_flockfile (stderr);
  475. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  476. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  477. __fxprintf (NULL, "%s", buf);
  478. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  479. _IO_funlockfile (stderr);
  480. free (buf);
  481. }
  482. #else
  483. fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
  484. argv[0], argv[d->optind]);
  485. #endif
  486. }
  487. d->__nextchar += strlen (d->__nextchar);
  488. d->optind++;
  489. d->optopt = 0;
  490. return '?';
  491. }
  492. if (pfound != NULL)
  493. {
  494. option_index = indfound;
  495. d->optind++;
  496. if (*nameend)
  497. {
  498. /* Don't test has_arg with >, because some C compilers don't
  499. allow it to be used on enums. */
  500. if (pfound->has_arg)
  501. d->optarg = nameend + 1;
  502. else
  503. {
  504. if (print_errors)
  505. {
  506. #if defined _LIBC && defined USE_IN_LIBIO
  507. char *buf;
  508. int n;
  509. #endif
  510. if (argv[d->optind - 1][1] == '-')
  511. {
  512. /* --option */
  513. #if defined _LIBC && defined USE_IN_LIBIO
  514. n = __asprintf (&buf, _("\
  515. %s: option `--%s' doesn't allow an argument\n"),
  516. argv[0], pfound->name);
  517. #else
  518. fprintf (stderr, _("\
  519. %s: option `--%s' doesn't allow an argument\n"),
  520. argv[0], pfound->name);
  521. #endif
  522. }
  523. else
  524. {
  525. /* +option or -option */
  526. #if defined _LIBC && defined USE_IN_LIBIO
  527. n = __asprintf (&buf, _("\
  528. %s: option `%c%s' doesn't allow an argument\n"),
  529. argv[0], argv[d->optind - 1][0],
  530. pfound->name);
  531. #else
  532. fprintf (stderr, _("\
  533. %s: option `%c%s' doesn't allow an argument\n"),
  534. argv[0], argv[d->optind - 1][0],
  535. pfound->name);
  536. #endif
  537. }
  538. #if defined _LIBC && defined USE_IN_LIBIO
  539. if (n >= 0)
  540. {
  541. _IO_flockfile (stderr);
  542. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  543. ((_IO_FILE *) stderr)->_flags2
  544. |= _IO_FLAGS2_NOTCANCEL;
  545. __fxprintf (NULL, "%s", buf);
  546. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  547. _IO_funlockfile (stderr);
  548. free (buf);
  549. }
  550. #endif
  551. }
  552. d->__nextchar += strlen (d->__nextchar);
  553. d->optopt = pfound->val;
  554. return '?';
  555. }
  556. }
  557. else if (pfound->has_arg == 1)
  558. {
  559. if (d->optind < argc)
  560. d->optarg = argv[d->optind++];
  561. else
  562. {
  563. if (print_errors)
  564. {
  565. #if defined _LIBC && defined USE_IN_LIBIO
  566. char *buf;
  567. if (__asprintf (&buf, _("\
  568. %s: option `%s' requires an argument\n"),
  569. argv[0], argv[d->optind - 1]) >= 0)
  570. {
  571. _IO_flockfile (stderr);
  572. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  573. ((_IO_FILE *) stderr)->_flags2
  574. |= _IO_FLAGS2_NOTCANCEL;
  575. __fxprintf (NULL, "%s", buf);
  576. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  577. _IO_funlockfile (stderr);
  578. free (buf);
  579. }
  580. #else
  581. fprintf (stderr,
  582. _("%s: option `%s' requires an argument\n"),
  583. argv[0], argv[d->optind - 1]);
  584. #endif
  585. }
  586. d->__nextchar += strlen (d->__nextchar);
  587. d->optopt = pfound->val;
  588. return optstring[0] == ':' ? ':' : '?';
  589. }
  590. }
  591. d->__nextchar += strlen (d->__nextchar);
  592. if (longind != NULL)
  593. *longind = option_index;
  594. if (pfound->flag)
  595. {
  596. *(pfound->flag) = pfound->val;
  597. return 0;
  598. }
  599. return pfound->val;
  600. }
  601. /* Can't find it as a long option. If this is not getopt_long_only,
  602. or the option starts with '--' or is not a valid short
  603. option, then it's an error.
  604. Otherwise interpret it as a short option. */
  605. if (!long_only || argv[d->optind][1] == '-'
  606. || strchr (optstring, *d->__nextchar) == NULL)
  607. {
  608. if (print_errors)
  609. {
  610. #if defined _LIBC && defined USE_IN_LIBIO
  611. char *buf;
  612. int n;
  613. #endif
  614. if (argv[d->optind][1] == '-')
  615. {
  616. /* --option */
  617. #if defined _LIBC && defined USE_IN_LIBIO
  618. n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
  619. argv[0], d->__nextchar);
  620. #else
  621. fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
  622. argv[0], d->__nextchar);
  623. #endif
  624. }
  625. else
  626. {
  627. /* +option or -option */
  628. #if defined _LIBC && defined USE_IN_LIBIO
  629. n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
  630. argv[0], argv[d->optind][0], d->__nextchar);
  631. #else
  632. fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
  633. argv[0], argv[d->optind][0], d->__nextchar);
  634. #endif
  635. }
  636. #if defined _LIBC && defined USE_IN_LIBIO
  637. if (n >= 0)
  638. {
  639. _IO_flockfile (stderr);
  640. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  641. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  642. __fxprintf (NULL, "%s", buf);
  643. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  644. _IO_funlockfile (stderr);
  645. free (buf);
  646. }
  647. #endif
  648. }
  649. d->__nextchar = (char *) "";
  650. d->optind++;
  651. d->optopt = 0;
  652. return '?';
  653. }
  654. }
  655. /* Look at and handle the next short option-character. */
  656. {
  657. char c = *d->__nextchar++;
  658. char *temp = strchr (optstring, c);
  659. /* Increment `optind' when we start to process its last character. */
  660. if (*d->__nextchar == '\0')
  661. ++d->optind;
  662. if (temp == NULL || c == ':')
  663. {
  664. if (print_errors)
  665. {
  666. #if defined _LIBC && defined USE_IN_LIBIO
  667. char *buf;
  668. int n;
  669. #endif
  670. if (d->__posixly_correct)
  671. {
  672. /* 1003.2 specifies the format of this message. */
  673. #if defined _LIBC && defined USE_IN_LIBIO
  674. n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
  675. argv[0], c);
  676. #else
  677. fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
  678. #endif
  679. }
  680. else
  681. {
  682. #if defined _LIBC && defined USE_IN_LIBIO
  683. n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
  684. argv[0], c);
  685. #else
  686. fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
  687. #endif
  688. }
  689. #if defined _LIBC && defined USE_IN_LIBIO
  690. if (n >= 0)
  691. {
  692. _IO_flockfile (stderr);
  693. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  694. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  695. __fxprintf (NULL, "%s", buf);
  696. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  697. _IO_funlockfile (stderr);
  698. free (buf);
  699. }
  700. #endif
  701. }
  702. d->optopt = c;
  703. return '?';
  704. }
  705. #ifdef SPECIAL_TREATMENT_FOR_W
  706. /* Convenience. Treat POSIX -W foo same as long option --foo */
  707. if (temp[0] == 'W' && temp[1] == ';')
  708. {
  709. char *nameend;
  710. const struct option *p;
  711. const struct option *pfound = NULL;
  712. int exact = 0;
  713. int ambig = 0;
  714. int indfound = 0;
  715. int option_index;
  716. /* This is an option that requires an argument. */
  717. if (*d->__nextchar != '\0')
  718. {
  719. d->optarg = d->__nextchar;
  720. /* If we end this ARGV-element by taking the rest as an arg,
  721. we must advance to the next element now. */
  722. d->optind++;
  723. }
  724. else if (d->optind == argc)
  725. {
  726. if (print_errors)
  727. {
  728. /* 1003.2 specifies the format of this message. */
  729. #if defined _LIBC && defined USE_IN_LIBIO
  730. char *buf;
  731. if (__asprintf (&buf,
  732. _("%s: option requires an argument -- %c\n"),
  733. argv[0], c) >= 0)
  734. {
  735. _IO_flockfile (stderr);
  736. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  737. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  738. __fxprintf (NULL, "%s", buf);
  739. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  740. _IO_funlockfile (stderr);
  741. free (buf);
  742. }
  743. #else
  744. fprintf (stderr, _("%s: option requires an argument -- %c\n"),
  745. argv[0], c);
  746. #endif
  747. }
  748. d->optopt = c;
  749. if (optstring[0] == ':')
  750. c = ':';
  751. else
  752. c = '?';
  753. return c;
  754. }
  755. else
  756. /* We already incremented `d->optind' once;
  757. increment it again when taking next ARGV-elt as argument. */
  758. d->optarg = argv[d->optind++];
  759. /* optarg is now the argument, see if it's in the
  760. table of longopts. */
  761. for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
  762. nameend++)
  763. /* Do nothing. */ ;
  764. /* Test all long options for either exact match
  765. or abbreviated matches. */
  766. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  767. if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
  768. {
  769. if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
  770. {
  771. /* Exact match found. */
  772. pfound = p;
  773. indfound = option_index;
  774. exact = 1;
  775. break;
  776. }
  777. else if (pfound == NULL)
  778. {
  779. /* First nonexact match found. */
  780. pfound = p;
  781. indfound = option_index;
  782. }
  783. else
  784. /* Second or later nonexact match found. */
  785. ambig = 1;
  786. }
  787. if (ambig && !exact)
  788. {
  789. if (print_errors)
  790. {
  791. #if defined _LIBC && defined USE_IN_LIBIO
  792. char *buf;
  793. if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
  794. argv[0], argv[d->optind]) >= 0)
  795. {
  796. _IO_flockfile (stderr);
  797. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  798. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  799. __fxprintf (NULL, "%s", buf);
  800. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  801. _IO_funlockfile (stderr);
  802. free (buf);
  803. }
  804. #else
  805. fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
  806. argv[0], argv[d->optind]);
  807. #endif
  808. }
  809. d->__nextchar += strlen (d->__nextchar);
  810. d->optind++;
  811. return '?';
  812. }
  813. if (pfound != NULL)
  814. {
  815. option_index = indfound;
  816. if (*nameend)
  817. {
  818. /* Don't test has_arg with >, because some C compilers don't
  819. allow it to be used on enums. */
  820. if (pfound->has_arg)
  821. d->optarg = nameend + 1;
  822. else
  823. {
  824. if (print_errors)
  825. {
  826. #if defined _LIBC && defined USE_IN_LIBIO
  827. char *buf;
  828. if (__asprintf (&buf, _("\
  829. %s: option `-W %s' doesn't allow an argument\n"),
  830. argv[0], pfound->name) >= 0)
  831. {
  832. _IO_flockfile (stderr);
  833. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  834. ((_IO_FILE *) stderr)->_flags2
  835. |= _IO_FLAGS2_NOTCANCEL;
  836. __fxprintf (NULL, "%s", buf);
  837. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  838. _IO_funlockfile (stderr);
  839. free (buf);
  840. }
  841. #else
  842. fprintf (stderr, _("\
  843. %s: option `-W %s' doesn't allow an argument\n"),
  844. argv[0], pfound->name);
  845. #endif
  846. }
  847. d->__nextchar += strlen (d->__nextchar);
  848. return '?';
  849. }
  850. }
  851. else if (pfound->has_arg == 1)
  852. {
  853. if (d->optind < argc)
  854. d->optarg = argv[d->optind++];
  855. else
  856. {
  857. if (print_errors)
  858. {
  859. #if defined _LIBC && defined USE_IN_LIBIO
  860. char *buf;
  861. if (__asprintf (&buf, _("\
  862. %s: option `%s' requires an argument\n"),
  863. argv[0], argv[d->optind - 1]) >= 0)
  864. {
  865. _IO_flockfile (stderr);
  866. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  867. ((_IO_FILE *) stderr)->_flags2
  868. |= _IO_FLAGS2_NOTCANCEL;
  869. __fxprintf (NULL, "%s", buf);
  870. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  871. _IO_funlockfile (stderr);
  872. free (buf);
  873. }
  874. #else
  875. fprintf (stderr,
  876. _("%s: option `%s' requires an argument\n"),
  877. argv[0], argv[d->optind - 1]);
  878. #endif
  879. }
  880. d->__nextchar += strlen (d->__nextchar);
  881. return optstring[0] == ':' ? ':' : '?';
  882. }
  883. }
  884. d->__nextchar += strlen (d->__nextchar);
  885. if (longind != NULL)
  886. *longind = option_index;
  887. if (pfound->flag)
  888. {
  889. *(pfound->flag) = pfound->val;
  890. return 0;
  891. }
  892. return pfound->val;
  893. }
  894. d->__nextchar = NULL;
  895. return 'W'; /* Let the application handle it. */
  896. }
  897. #endif
  898. if (temp[1] == ':')
  899. {
  900. if (temp[2] == ':')
  901. {
  902. /* This is an option that accepts an argument optionally. */
  903. if (*d->__nextchar != '\0')
  904. {
  905. d->optarg = d->__nextchar;
  906. d->optind++;
  907. }
  908. else
  909. d->optarg = NULL;
  910. d->__nextchar = NULL;
  911. }
  912. else
  913. {
  914. /* This is an option that requires an argument. */
  915. if (*d->__nextchar != '\0')
  916. {
  917. d->optarg = d->__nextchar;
  918. /* If we end this ARGV-element by taking the rest as an arg,
  919. we must advance to the next element now. */
  920. d->optind++;
  921. }
  922. else if (d->optind == argc)
  923. {
  924. if (print_errors)
  925. {
  926. /* 1003.2 specifies the format of this message. */
  927. #if defined _LIBC && defined USE_IN_LIBIO
  928. char *buf;
  929. if (__asprintf (&buf, _("\
  930. %s: option requires an argument -- %c\n"),
  931. argv[0], c) >= 0)
  932. {
  933. _IO_flockfile (stderr);
  934. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  935. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  936. __fxprintf (NULL, "%s", buf);
  937. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  938. _IO_funlockfile (stderr);
  939. free (buf);
  940. }
  941. #else
  942. fprintf (stderr,
  943. _("%s: option requires an argument -- %c\n"),
  944. argv[0], c);
  945. #endif
  946. }
  947. d->optopt = c;
  948. if (optstring[0] == ':')
  949. c = ':';
  950. else
  951. c = '?';
  952. }
  953. else
  954. /* We already incremented `optind' once;
  955. increment it again when taking next ARGV-elt as argument. */
  956. d->optarg = argv[d->optind++];
  957. d->__nextchar = NULL;
  958. }
  959. }
  960. return c;
  961. }
  962. }
  963. int
  964. _getopt_internal (int argc, char *const *argv, const char *optstring,
  965. const struct option *longopts, int *longind, int long_only)
  966. {
  967. int result;
  968. getopt_data.optind = optind;
  969. getopt_data.opterr = opterr;
  970. result = _getopt_internal_r (argc, argv, optstring, longopts,
  971. longind, long_only, &getopt_data);
  972. optind = getopt_data.optind;
  973. optarg = getopt_data.optarg;
  974. optopt = getopt_data.optopt;
  975. return result;
  976. }
  977. int
  978. getopt (int argc, char *const *argv, const char *optstring)
  979. {
  980. return _getopt_internal (argc, argv, optstring,
  981. (const struct option *) 0,
  982. (int *) 0,
  983. 0);
  984. }
  985. libc_hidden_def(getopt)
  986. int
  987. getopt_long (int argc, char *const *argv, const char *options,
  988. const struct option *long_options, int *opt_index)
  989. {
  990. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  991. }
  992. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  993. If an option that starts with '-' (not '--') doesn't match a long option,
  994. but does match a short option, it is parsed as a short option
  995. instead. */
  996. int
  997. getopt_long_only (int argc, char *const *argv, const char *options,
  998. const struct option *long_options, int *opt_index)
  999. {
  1000. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  1001. }
  1002. #endif /* Not ELIDE_CODE. */