/xbmc/screensavers/rsxs-0.9/lib/getopt.c

http://github.com/xbmc/xbmc · C · 1191 lines · 810 code · 154 blank · 227 comment · 243 complexity · a1ad9f59ddeb25c14f00e21f0e53d46a MD5 · raw file

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