getopt.c 23 KB

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