getopt.c 23 KB

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