getopt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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
  6. Free Software Foundation, Inc.
  7. This file is part of the GNU C Library.
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with the GNU C Library; if not, write to the Free
  18. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. 02111-1307 USA. */
  20. /*
  21. * Modified for uClibc by Manuel Novoa III on 1/5/01.
  22. * Modified once again for uClibc by Erik Andersen 8/7/02
  23. */
  24. #include <features.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #ifdef __UCLIBC_HAS_GETTEXT_AWARENESS__
  29. #include <libintl.h>
  30. #endif
  31. #ifdef __UCLIBC_MJN3_ONLY__
  32. #warning TODO: Enable gettext awareness.
  33. #endif /* __UCLIBC_MJN3_ONLY__ */
  34. #undef _
  35. #define _(X) X
  36. /* Treat '-W foo' the same as the long option '--foo',
  37. * disabled for the moment since it costs about 2k... */
  38. #undef SPECIAL_TREATMENT_FOR_W
  39. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  40. but it behaves differently for the user, since it allows the user
  41. to intersperse the options with the other arguments.
  42. As `getopt' works, it permutes the elements of ARGV so that,
  43. when it is done, all the options precede everything else. Thus
  44. all application programs are extended to handle flexible argument order.
  45. Setting the environment variable POSIXLY_CORRECT disables permutation.
  46. Then the behavior is completely standard.
  47. GNU application programs can use a third alternative mode in which
  48. they can distinguish the relative order of options and other arguments. */
  49. #include <getopt.h>
  50. libc_hidden_proto(strchr)
  51. libc_hidden_proto(strcmp)
  52. libc_hidden_proto(strlen)
  53. libc_hidden_proto(strncmp)
  54. libc_hidden_proto(getenv)
  55. libc_hidden_proto(fprintf)
  56. libc_hidden_proto(optarg)
  57. libc_hidden_proto(opterr)
  58. libc_hidden_proto(optind)
  59. libc_hidden_proto(optopt)
  60. libc_hidden_proto(stderr)
  61. extern int _getopt_internal (int argc, char *const *argv, const char *optstring,
  62. const struct option *longopts, int *longind, int long_only) attribute_hidden;
  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 = NULL;
  69. libc_hidden_data_def(optarg)
  70. /* Index in ARGV of the next element to be scanned.
  71. This is used for communication to and from the caller
  72. and for communication between successive calls to `getopt'.
  73. On entry to `getopt', zero means this is the first call; initialize.
  74. When `getopt' returns -1, this is the index of the first of the
  75. non-option elements that the caller should itself scan.
  76. Otherwise, `optind' communicates from one call to the next
  77. how much of ARGV has been scanned so far. */
  78. /* 1003.2 says this must be 1 before any call. */
  79. int optind = 1;
  80. libc_hidden_data_def(optind)
  81. /* Callers store zero here to inhibit the error message
  82. for unrecognized options. */
  83. int opterr = 1;
  84. libc_hidden_data_def(opterr)
  85. /* Set to an option character which was unrecognized.
  86. This must be initialized on some systems to avoid linking in the
  87. system's own getopt implementation. */
  88. int optopt = '?';
  89. libc_hidden_data_def(optopt)
  90. /* The next char to be scanned in the option-element
  91. in which the last option character we returned was found.
  92. This allows us to pick up the scan where we left off.
  93. If this is zero, or a null string, it means resume the scan
  94. by advancing to the next ARGV-element. */
  95. static char *nextchar;
  96. /* Formerly, initialization of getopt depended on optind==0, which
  97. causes problems with re-calling getopt as programs generally don't
  98. know that. */
  99. static int __getopt_initialized;
  100. /* Describe how to deal with options that follow non-option ARGV-elements.
  101. If the caller did not specify anything,
  102. the default is REQUIRE_ORDER if the environment variable
  103. POSIXLY_CORRECT is defined, PERMUTE otherwise.
  104. REQUIRE_ORDER means don't recognize them as options;
  105. stop option processing when the first non-option is seen.
  106. This is what Unix does.
  107. This mode of operation is selected by either setting the environment
  108. variable POSIXLY_CORRECT, or using `+' as the first character
  109. of the list of option characters.
  110. PERMUTE is the default. We permute the contents of ARGV as we scan,
  111. so that eventually all the non-options are at the end. This allows options
  112. to be given in any order, even with programs that were not written to
  113. expect this.
  114. RETURN_IN_ORDER is an option available to programs that were written
  115. to expect options and other ARGV-elements in any order and that care about
  116. the ordering of the two. We describe each non-option ARGV-element
  117. as if it were the argument of an option with character code 1.
  118. Using `-' as the first character of the list of option characters
  119. selects this mode of operation.
  120. The special argument `--' forces an end of option-scanning regardless
  121. of the value of `ordering'. In the case of RETURN_IN_ORDER, only
  122. `--' can cause `getopt' to return -1 with `optind' != ARGC. */
  123. static enum
  124. {
  125. REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  126. } ordering;
  127. # include <string.h>
  128. # define my_index strchr
  129. /* Handle permutation of arguments. */
  130. /* Describe the part of ARGV that contains non-options that have
  131. been skipped. `first_nonopt' is the index in ARGV of the first of them;
  132. `last_nonopt' is the index after the last of them. */
  133. static int first_nonopt;
  134. static int last_nonopt;
  135. /* Exchange two adjacent subsequences of ARGV.
  136. One subsequence is elements [first_nonopt,last_nonopt)
  137. which contains all the non-options that have been skipped so far.
  138. The other is elements [last_nonopt,optind), which contains all
  139. the options processed since those non-options were skipped.
  140. `first_nonopt' and `last_nonopt' are relocated so that they describe
  141. the new indices of the non-options in ARGV after they are moved. */
  142. static void exchange (char **argv)
  143. {
  144. int bottom = first_nonopt;
  145. int middle = last_nonopt;
  146. int top = optind;
  147. char *tem;
  148. /* Exchange the shorter segment with the far end of the longer segment.
  149. That puts the shorter segment into the right place.
  150. It leaves the longer segment in the right place overall,
  151. but it consists of two parts that need to be swapped next. */
  152. while (top > middle && middle > bottom)
  153. {
  154. if (top - middle > middle - bottom)
  155. {
  156. /* Bottom segment is the short one. */
  157. int len = middle - bottom;
  158. register int i;
  159. /* Swap it with the top part of the top segment. */
  160. for (i = 0; i < len; i++)
  161. {
  162. tem = argv[bottom + i];
  163. argv[bottom + i] = argv[top - (middle - bottom) + i];
  164. argv[top - (middle - bottom) + i] = tem;
  165. }
  166. /* Exclude the moved bottom segment from further swapping. */
  167. top -= len;
  168. }
  169. else
  170. {
  171. /* Top segment is the short one. */
  172. int len = top - middle;
  173. register int i;
  174. /* Swap it with the bottom part of the bottom segment. */
  175. for (i = 0; i < len; i++)
  176. {
  177. tem = argv[bottom + i];
  178. argv[bottom + i] = argv[middle + i];
  179. argv[middle + i] = tem;
  180. }
  181. /* Exclude the moved top segment from further swapping. */
  182. bottom += len;
  183. }
  184. }
  185. /* Update records for the slots the non-options now occupy. */
  186. first_nonopt += (optind - last_nonopt);
  187. last_nonopt = optind;
  188. }
  189. /* Initialize the internal data when the first call is made. */
  190. static const char *_getopt_initialize (attribute_unused int argc, attribute_unused char *const * argv, const char *optstring)
  191. {
  192. /* Start processing options with ARGV-element 1 (since ARGV-element 0
  193. is the program name); the sequence of previously skipped
  194. non-option ARGV-elements is empty. */
  195. first_nonopt = last_nonopt = optind;
  196. nextchar = NULL;
  197. /* Determine how to handle the ordering of options and nonoptions. */
  198. if (optstring[0] == '-')
  199. {
  200. ordering = RETURN_IN_ORDER;
  201. ++optstring;
  202. }
  203. else if (optstring[0] == '+')
  204. {
  205. ordering = REQUIRE_ORDER;
  206. ++optstring;
  207. }
  208. else if (getenv ("POSIXLY_CORRECT") != NULL)
  209. ordering = REQUIRE_ORDER;
  210. else
  211. ordering = PERMUTE;
  212. return optstring;
  213. }
  214. /* Scan elements of ARGV (whose length is ARGC) for option characters
  215. given in OPTSTRING.
  216. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  217. then it is an option element. The characters of this element
  218. (aside from the initial '-') are option characters. If `getopt'
  219. is called repeatedly, it returns successively each of the option characters
  220. from each of the option elements.
  221. If `getopt' finds another option character, it returns that character,
  222. updating `optind' and `nextchar' so that the next call to `getopt' can
  223. resume the scan with the following option character or ARGV-element.
  224. If there are no more option characters, `getopt' returns -1.
  225. Then `optind' is the index in ARGV of the first ARGV-element
  226. that is not an option. (The ARGV-elements have been permuted
  227. so that those that are not options now come last.)
  228. OPTSTRING is a string containing the legitimate option characters.
  229. If an option character is seen that is not listed in OPTSTRING,
  230. return '?' after printing an error message. If you set `opterr' to
  231. zero, the error message is suppressed but we still return '?'.
  232. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  233. so the following text in the same ARGV-element, or the text of the following
  234. ARGV-element, is returned in `optarg'. Two colons mean an option that
  235. wants an optional arg; if there is text in the current ARGV-element,
  236. it is returned in `optarg', otherwise `optarg' is set to zero.
  237. If OPTSTRING starts with `-' or `+', it requests different methods of
  238. handling the non-option ARGV-elements.
  239. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  240. Long-named options begin with `--' instead of `-'.
  241. Their names may be abbreviated as long as the abbreviation is unique
  242. or is an exact match for some defined option. If they have an
  243. argument, it follows the option name in the same ARGV-element, separated
  244. from the option name by a `=', or else the in next ARGV-element.
  245. When `getopt' finds a long-named option, it returns 0 if that option's
  246. `flag' field is nonzero, the value of the option's `val' field
  247. if the `flag' field is zero.
  248. The elements of ARGV aren't really const, because we permute them.
  249. But we pretend they're const in the prototype to be compatible
  250. with other systems.
  251. LONGOPTS is a vector of `struct option' terminated by an
  252. element containing a name which is zero.
  253. LONGIND returns the index in LONGOPT of the long-named option found.
  254. It is only valid when a long-named option has been found by the most
  255. recent call.
  256. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  257. long-named options. */
  258. int attribute_hidden _getopt_internal (int argc, char *const *argv, const char *optstring,
  259. const struct option *longopts, int *longind, int long_only)
  260. {
  261. int print_errors = opterr;
  262. if (optstring[0] == ':')
  263. print_errors = 0;
  264. if (argc < 1)
  265. return -1;
  266. optarg = NULL;
  267. if (optind == 0 || !__getopt_initialized)
  268. {
  269. if (optind == 0)
  270. optind = 1; /* Don't scan ARGV[0], the program name. */
  271. optstring = _getopt_initialize (argc, argv, optstring);
  272. __getopt_initialized = 1;
  273. }
  274. /* Test whether ARGV[optind] points to a non-option argument.
  275. Either it does not have option syntax, or there is an environment flag
  276. from the shell indicating it is not an option. The later information
  277. is only used when the used in the GNU libc. */
  278. #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
  279. if (nextchar == NULL || *nextchar == '\0')
  280. {
  281. /* Advance to the next ARGV-element. */
  282. /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
  283. moved back by the user (who may also have changed the arguments). */
  284. if (last_nonopt > optind)
  285. last_nonopt = optind;
  286. if (first_nonopt > optind)
  287. first_nonopt = optind;
  288. if (ordering == PERMUTE)
  289. {
  290. /* If we have just processed some options following some non-options,
  291. exchange them so that the options come first. */
  292. if (first_nonopt != last_nonopt && last_nonopt != optind)
  293. exchange ((char **) argv);
  294. else if (last_nonopt != optind)
  295. first_nonopt = optind;
  296. /* Skip any additional non-options
  297. and extend the range of non-options previously skipped. */
  298. while (optind < argc && NONOPTION_P)
  299. optind++;
  300. last_nonopt = optind;
  301. }
  302. /* The special ARGV-element `--' means premature end of options.
  303. Skip it like a null option,
  304. then exchange with previous non-options as if it were an option,
  305. then skip everything else like a non-option. */
  306. if (optind != argc && !strcmp (argv[optind], "--"))
  307. {
  308. optind++;
  309. if (first_nonopt != last_nonopt && last_nonopt != optind)
  310. exchange ((char **) argv);
  311. else if (first_nonopt == last_nonopt)
  312. first_nonopt = optind;
  313. last_nonopt = argc;
  314. optind = argc;
  315. }
  316. /* If we have done all the ARGV-elements, stop the scan
  317. and back over any non-options that we skipped and permuted. */
  318. if (optind == argc)
  319. {
  320. /* Set the next-arg-index to point at the non-options
  321. that we previously skipped, so the caller will digest them. */
  322. if (first_nonopt != last_nonopt)
  323. optind = first_nonopt;
  324. return -1;
  325. }
  326. /* If we have come to a non-option and did not permute it,
  327. either stop the scan or describe it to the caller and pass it by. */
  328. if (NONOPTION_P)
  329. {
  330. if (ordering == REQUIRE_ORDER)
  331. return -1;
  332. optarg = argv[optind++];
  333. return 1;
  334. }
  335. /* We have found another option-ARGV-element.
  336. Skip the initial punctuation. */
  337. nextchar = (argv[optind] + 1
  338. + (longopts != NULL && argv[optind][1] == '-'));
  339. }
  340. /* Decode the current option-ARGV-element. */
  341. /* Check whether the ARGV-element is a long option.
  342. If long_only and the ARGV-element has the form "-f", where f is
  343. a valid short option, don't consider it an abbreviated form of
  344. a long option that starts with f. Otherwise there would be no
  345. way to give the -f short option.
  346. On the other hand, if there's a long option "fubar" and
  347. the ARGV-element is "-fu", do consider that an abbreviation of
  348. the long option, just like "--fu", and not "-f" with arg "u".
  349. This distinction seems to be the most useful approach. */
  350. if (longopts != NULL
  351. && (argv[optind][1] == '-'
  352. || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
  353. {
  354. char *nameend;
  355. const struct option *p;
  356. const struct option *pfound = NULL;
  357. int exact = 0;
  358. int ambig = 0;
  359. int indfound = -1;
  360. int option_index;
  361. for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
  362. /* Do nothing. */ ;
  363. /* Test all long options for either exact match
  364. or abbreviated matches. */
  365. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  366. if (!strncmp (p->name, nextchar, nameend - nextchar))
  367. {
  368. if ((unsigned int) (nameend - nextchar)
  369. == (unsigned int) strlen (p->name))
  370. {
  371. /* Exact match found. */
  372. pfound = p;
  373. indfound = option_index;
  374. exact = 1;
  375. break;
  376. }
  377. else if (pfound == NULL)
  378. {
  379. /* First nonexact match found. */
  380. pfound = p;
  381. indfound = option_index;
  382. }
  383. else if (long_only
  384. || pfound->has_arg != p->has_arg
  385. || pfound->flag != p->flag
  386. || pfound->val != p->val)
  387. /* Second or later nonexact match found. */
  388. ambig = 1;
  389. }
  390. if (ambig && !exact)
  391. {
  392. if (print_errors)
  393. {
  394. fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
  395. argv[0], argv[optind]);
  396. }
  397. nextchar += strlen (nextchar);
  398. optind++;
  399. optopt = 0;
  400. return '?';
  401. }
  402. if (pfound != NULL)
  403. {
  404. option_index = indfound;
  405. optind++;
  406. if (*nameend)
  407. {
  408. /* Don't test has_arg with >, because some C compilers don't
  409. allow it to be used on enums. */
  410. if (pfound->has_arg)
  411. optarg = nameend + 1;
  412. else
  413. {
  414. if (print_errors)
  415. {
  416. if (argv[optind - 1][1] == '-')
  417. {
  418. /* --option */
  419. fprintf (stderr, _("\
  420. %s: option `--%s' doesn't allow an argument\n"),
  421. argv[0], pfound->name);
  422. }
  423. else
  424. {
  425. /* +option or -option */
  426. fprintf (stderr, _("\
  427. %s: option `%c%s' doesn't allow an argument\n"),
  428. argv[0], argv[optind - 1][0], pfound->name);
  429. }
  430. }
  431. nextchar += strlen (nextchar);
  432. optopt = pfound->val;
  433. return '?';
  434. }
  435. }
  436. else if (pfound->has_arg == 1)
  437. {
  438. if (optind < argc)
  439. optarg = argv[optind++];
  440. else
  441. {
  442. if (print_errors)
  443. {
  444. fprintf (stderr,
  445. _("%s: option `%s' requires an argument\n"),
  446. argv[0], argv[optind - 1]);
  447. }
  448. nextchar += strlen (nextchar);
  449. optopt = pfound->val;
  450. return optstring[0] == ':' ? ':' : '?';
  451. }
  452. }
  453. nextchar += strlen (nextchar);
  454. if (longind != NULL)
  455. *longind = option_index;
  456. if (pfound->flag)
  457. {
  458. *(pfound->flag) = pfound->val;
  459. return 0;
  460. }
  461. return pfound->val;
  462. }
  463. /* Can't find it as a long option. If this is not getopt_long_only,
  464. or the option starts with '--' or is not a valid short
  465. option, then it's an error.
  466. Otherwise interpret it as a short option. */
  467. if (!long_only || argv[optind][1] == '-'
  468. || my_index (optstring, *nextchar) == NULL)
  469. {
  470. if (print_errors)
  471. {
  472. if (argv[optind][1] == '-')
  473. {
  474. /* --option */
  475. fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
  476. argv[0], nextchar);
  477. }
  478. else
  479. {
  480. /* +option or -option */
  481. fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
  482. argv[0], argv[optind][0], nextchar);
  483. }
  484. }
  485. nextchar = (char *) "";
  486. optind++;
  487. optopt = 0;
  488. return '?';
  489. }
  490. }
  491. /* Look at and handle the next short option-character. */
  492. {
  493. char c = *nextchar++;
  494. char *temp = my_index (optstring, c);
  495. /* Increment `optind' when we start to process its last character. */
  496. if (*nextchar == '\0')
  497. ++optind;
  498. if (temp == NULL || c == ':')
  499. {
  500. if (print_errors)
  501. {
  502. /* 1003.2 specifies the format of this message. */
  503. fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
  504. }
  505. optopt = c;
  506. return '?';
  507. }
  508. #ifdef SPECIAL_TREATMENT_FOR_W
  509. /* Convenience. Treat POSIX -W foo same as long option --foo */
  510. if (temp[0] == 'W' && temp[1] == ';')
  511. {
  512. char *nameend;
  513. const struct option *p;
  514. const struct option *pfound = NULL;
  515. int exact = 0;
  516. int ambig = 0;
  517. int indfound = 0;
  518. int option_index;
  519. /* This is an option that requires an argument. */
  520. if (*nextchar != '\0')
  521. {
  522. optarg = nextchar;
  523. /* If we end this ARGV-element by taking the rest as an arg,
  524. we must advance to the next element now. */
  525. optind++;
  526. }
  527. else if (optind == argc)
  528. {
  529. if (print_errors)
  530. {
  531. /* 1003.2 specifies the format of this message. */
  532. fprintf (stderr, _("%s: option requires an argument -- %c\n"),
  533. argv[0], c);
  534. }
  535. optopt = c;
  536. if (optstring[0] == ':')
  537. c = ':';
  538. else
  539. c = '?';
  540. return c;
  541. }
  542. else
  543. /* We already incremented `optind' once;
  544. increment it again when taking next ARGV-elt as argument. */
  545. optarg = argv[optind++];
  546. /* optarg is now the argument, see if it's in the
  547. table of longopts. */
  548. for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
  549. /* Do nothing. */ ;
  550. /* Test all long options for either exact match
  551. or abbreviated matches. */
  552. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  553. if (!strncmp (p->name, nextchar, nameend - nextchar))
  554. {
  555. if ((unsigned int) (nameend - nextchar) == strlen (p->name))
  556. {
  557. /* Exact match found. */
  558. pfound = p;
  559. indfound = option_index;
  560. exact = 1;
  561. break;
  562. }
  563. else if (pfound == NULL)
  564. {
  565. /* First nonexact match found. */
  566. pfound = p;
  567. indfound = option_index;
  568. }
  569. else
  570. /* Second or later nonexact match found. */
  571. ambig = 1;
  572. }
  573. if (ambig && !exact)
  574. {
  575. if (print_errors)
  576. {
  577. fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
  578. argv[0], argv[optind]);
  579. }
  580. nextchar += strlen (nextchar);
  581. optind++;
  582. return '?';
  583. }
  584. if (pfound != NULL)
  585. {
  586. option_index = indfound;
  587. if (*nameend)
  588. {
  589. /* Don't test has_arg with >, because some C compilers don't
  590. allow it to be used on enums. */
  591. if (pfound->has_arg)
  592. optarg = nameend + 1;
  593. else
  594. {
  595. if (print_errors)
  596. {
  597. fprintf (stderr, _("\
  598. %s: option `-W %s' doesn't allow an argument\n"),
  599. argv[0], pfound->name);
  600. }
  601. nextchar += strlen (nextchar);
  602. return '?';
  603. }
  604. }
  605. else if (pfound->has_arg == 1)
  606. {
  607. if (optind < argc)
  608. optarg = argv[optind++];
  609. else
  610. {
  611. if (print_errors)
  612. {
  613. fprintf (stderr,
  614. _("%s: option `%s' requires an argument\n"),
  615. argv[0], argv[optind - 1]);
  616. }
  617. nextchar += strlen (nextchar);
  618. return optstring[0] == ':' ? ':' : '?';
  619. }
  620. }
  621. nextchar += strlen (nextchar);
  622. if (longind != NULL)
  623. *longind = option_index;
  624. if (pfound->flag)
  625. {
  626. *(pfound->flag) = pfound->val;
  627. return 0;
  628. }
  629. return pfound->val;
  630. }
  631. nextchar = NULL;
  632. return 'W'; /* Let the application handle it. */
  633. }
  634. #endif
  635. if (temp[1] == ':')
  636. {
  637. if (temp[2] == ':')
  638. {
  639. /* This is an option that accepts an argument optionally. */
  640. if (*nextchar != '\0')
  641. {
  642. optarg = nextchar;
  643. optind++;
  644. }
  645. else
  646. optarg = NULL;
  647. nextchar = NULL;
  648. }
  649. else
  650. {
  651. /* This is an option that requires an argument. */
  652. if (*nextchar != '\0')
  653. {
  654. optarg = nextchar;
  655. /* If we end this ARGV-element by taking the rest as an arg,
  656. we must advance to the next element now. */
  657. optind++;
  658. }
  659. else if (optind == argc)
  660. {
  661. if (print_errors)
  662. {
  663. /* 1003.2 specifies the format of this message. */
  664. fprintf (stderr,
  665. _("%s: option requires an argument -- %c\n"),
  666. argv[0], c);
  667. }
  668. optopt = c;
  669. if (optstring[0] == ':')
  670. c = ':';
  671. else
  672. c = '?';
  673. }
  674. else
  675. /* We already incremented `optind' once;
  676. increment it again when taking next ARGV-elt as argument. */
  677. optarg = argv[optind++];
  678. nextchar = NULL;
  679. }
  680. }
  681. return c;
  682. }
  683. }
  684. int getopt (int argc, char *const *argv, const char *optstring)
  685. {
  686. return _getopt_internal (argc, argv, optstring,
  687. (const struct option *) 0, (int *) 0, 0);
  688. }
  689. int getopt_long (int argc, char *const *argv, const char *options,
  690. const struct option *long_options, int *opt_index)
  691. {
  692. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  693. }
  694. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  695. If an option that starts with '-' (not '--') doesn't match a long option,
  696. but does match a short option, it is parsed as a short option
  697. instead. */
  698. int getopt_long_only (int argc, char *const *argv, const char *options,
  699. const struct option *long_options, int *opt_index)
  700. {
  701. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  702. }