getopt.c 33 KB

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