getopt.c 33 KB

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