getopt.c 31 KB

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