getopt.c 33 KB

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