getopt.c 33 KB

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