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