getopt.c 38 KB

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