getopt.c 23 KB

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