getopt.c 23 KB

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