getopt.c 32 KB

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