getopt.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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,2002,2003,2004
  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. /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
  25. Ditto for AIX 3.2 and <stdlib.h>. */
  26. #ifndef _NO_PROTO
  27. # define _NO_PROTO
  28. #endif
  29. #ifdef HAVE_CONFIG_H
  30. # include <config.h>
  31. #endif
  32. #define __FORCE_GLIBC
  33. #include <features.h>
  34. #include <stdio.h>
  35. /* Comment out all this code if we are using the GNU C Library, and are not
  36. actually compiling the library itself. This code is part of the GNU C
  37. Library, but also included in many other GNU distributions. Compiling
  38. and linking in this code is a waste when using the GNU C library
  39. (especially if it is a shared library). Rather than having every GNU
  40. program understand `configure --with-gnu-libc' and omit the object files,
  41. it is simpler to just do this in the source for each such file. */
  42. #define GETOPT_INTERFACE_VERSION 2
  43. #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
  44. # include <gnu-versions.h>
  45. # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
  46. # define ELIDE_CODE
  47. # endif
  48. #endif
  49. #ifndef ELIDE_CODE
  50. /* This needs to come after some library #include
  51. to get __GNU_LIBRARY__ defined. */
  52. #ifdef __GNU_LIBRARY__
  53. /* Don't include stdlib.h for non-GNU C libraries because some of them
  54. contain conflicting prototypes for getopt. */
  55. # include <stdlib.h>
  56. # include <unistd.h>
  57. #endif /* GNU C library. */
  58. #include <string.h>
  59. #ifdef VMS
  60. # include <unixlib.h>
  61. #endif
  62. #if !defined __UCLIBC__ && !defined __UCLIBC_HAS_GETTEXT_AWARENESS__
  63. #ifdef _LIBC
  64. # include <libintl.h>
  65. #else
  66. # include "gettext.h"
  67. # define _(msgid) gettext (msgid)
  68. #endif
  69. #else
  70. #ifdef __UCLIBC_MJN3_ONLY__
  71. #warning TODO: Enable gettext awareness.
  72. #endif /* __UCLIBC_MJN3_ONLY__ */
  73. #undef _
  74. #define _(X) X
  75. #endif
  76. /* Treat '-W foo' the same as the long option '--foo',
  77. * disabled for the moment since it costs about 2k... */
  78. #undef SPECIAL_TREATMENT_FOR_W
  79. #if defined _LIBC && defined USE_IN_LIBIO
  80. # include <wchar.h>
  81. #endif
  82. #ifndef attribute_hidden
  83. # define attribute_hidden
  84. #endif
  85. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  86. but it behaves differently for the user, since it allows the user
  87. to intersperse the options with the other arguments.
  88. As `getopt' works, it permutes the elements of ARGV so that,
  89. when it is done, all the options precede everything else. Thus
  90. all application programs are extended to handle flexible argument order.
  91. Setting the environment variable POSIXLY_CORRECT disables permutation.
  92. Then the behavior is completely standard.
  93. GNU application programs can use a third alternative mode in which
  94. they can distinguish the relative order of options and other arguments. */
  95. #include <getopt.h>
  96. #include "getopt_int.h"
  97. libc_hidden_proto(strchr)
  98. libc_hidden_proto(strcmp)
  99. libc_hidden_proto(strlen)
  100. libc_hidden_proto(strncmp)
  101. libc_hidden_proto(getenv)
  102. libc_hidden_proto(fprintf)
  103. /* For communication from `getopt' to the caller.
  104. When `getopt' finds an option that takes an argument,
  105. the argument value is returned here.
  106. Also, when `ordering' is RETURN_IN_ORDER,
  107. each non-option ARGV-element is returned here. */
  108. char *optarg;
  109. /* Index in ARGV of the next element to be scanned.
  110. This is used for communication to and from the caller
  111. and for communication between successive calls to `getopt'.
  112. On entry to `getopt', zero means this is the first call; initialize.
  113. When `getopt' returns -1, this is the index of the first of the
  114. non-option elements that the caller should itself scan.
  115. Otherwise, `optind' communicates from one call to the next
  116. how much of ARGV has been scanned so far. */
  117. /* 1003.2 says this must be 1 before any call. */
  118. int optind = 1;
  119. /* Callers store zero here to inhibit the error message
  120. for unrecognized options. */
  121. int opterr = 1;
  122. /* Set to an option character which was unrecognized.
  123. This must be initialized on some systems to avoid linking in the
  124. system's own getopt implementation. */
  125. int optopt = '?';
  126. /* Keep a global copy of all internal members of getopt_data. */
  127. static struct _getopt_data getopt_data;
  128. #ifndef __GNU_LIBRARY__
  129. /* Avoid depending on library functions or files
  130. whose names are inconsistent. */
  131. #ifndef getenv
  132. extern char *getenv ();
  133. #endif
  134. #endif /* not __GNU_LIBRARY__ */
  135. #ifdef _LIBC
  136. /* Stored original parameters.
  137. XXX This is no good solution. We should rather copy the args so
  138. that we can compare them later. But we must not use malloc(3). */
  139. # ifdef USE_NONOPTION_FLAGS
  140. extern int __libc_argc;
  141. extern char **__libc_argv;
  142. /* Bash 2.0 gives us an environment variable containing flags
  143. indicating ARGV elements that should not be considered arguments. */
  144. /* Defined in getopt_init.c */
  145. extern char *__getopt_nonoption_flags;
  146. # define SWAP_FLAGS(ch1, ch2) \
  147. if (d->__nonoption_flags_len > 0) \
  148. { \
  149. char __tmp = __getopt_nonoption_flags[ch1]; \
  150. __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
  151. __getopt_nonoption_flags[ch2] = __tmp; \
  152. }
  153. # else
  154. # define SWAP_FLAGS(ch1, ch2)
  155. # endif
  156. #else /* !_LIBC */
  157. # define SWAP_FLAGS(ch1, ch2)
  158. #endif /* _LIBC */
  159. /* Exchange two adjacent subsequences of ARGV.
  160. One subsequence is elements [first_nonopt,last_nonopt)
  161. which contains all the non-options that have been skipped so far.
  162. The other is elements [last_nonopt,optind), which contains all
  163. the options processed since those non-options were skipped.
  164. `first_nonopt' and `last_nonopt' are relocated so that they describe
  165. the new indices of the non-options in ARGV after they are moved. */
  166. static void
  167. exchange (char **argv, struct _getopt_data *d)
  168. {
  169. int bottom = d->__first_nonopt;
  170. int middle = d->__last_nonopt;
  171. int top = d->optind;
  172. char *tem;
  173. /* Exchange the shorter segment with the far end of the longer segment.
  174. That puts the shorter segment into the right place.
  175. It leaves the longer segment in the right place overall,
  176. but it consists of two parts that need to be swapped next. */
  177. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  178. /* First make sure the handling of the `__getopt_nonoption_flags'
  179. string can work normally. Our top argument must be in the range
  180. of the string. */
  181. if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
  182. {
  183. /* We must extend the array. The user plays games with us and
  184. presents new arguments. */
  185. char *new_str = malloc (top + 1);
  186. if (new_str == NULL)
  187. d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
  188. else
  189. {
  190. memset (__mempcpy (new_str, __getopt_nonoption_flags,
  191. d->__nonoption_flags_max_len),
  192. '\0', top + 1 - d->__nonoption_flags_max_len);
  193. d->__nonoption_flags_max_len = top + 1;
  194. __getopt_nonoption_flags = new_str;
  195. }
  196. }
  197. #endif
  198. while (top > middle && middle > bottom)
  199. {
  200. if (top - middle > middle - bottom)
  201. {
  202. /* Bottom segment is the short one. */
  203. int len = middle - bottom;
  204. register int i;
  205. /* Swap it with the top part of the top segment. */
  206. for (i = 0; i < len; i++)
  207. {
  208. tem = argv[bottom + i];
  209. argv[bottom + i] = argv[top - (middle - bottom) + i];
  210. argv[top - (middle - bottom) + i] = tem;
  211. SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
  212. }
  213. /* Exclude the moved bottom segment from further swapping. */
  214. top -= len;
  215. }
  216. else
  217. {
  218. /* Top segment is the short one. */
  219. int len = top - middle;
  220. register int i;
  221. /* Swap it with the bottom part of the bottom segment. */
  222. for (i = 0; i < len; i++)
  223. {
  224. tem = argv[bottom + i];
  225. argv[bottom + i] = argv[middle + i];
  226. argv[middle + i] = tem;
  227. SWAP_FLAGS (bottom + i, middle + i);
  228. }
  229. /* Exclude the moved top segment from further swapping. */
  230. bottom += len;
  231. }
  232. }
  233. /* Update records for the slots the non-options now occupy. */
  234. d->__first_nonopt += (d->optind - d->__last_nonopt);
  235. d->__last_nonopt = d->optind;
  236. }
  237. /* Initialize the internal data when the first call is made. */
  238. static const char *
  239. _getopt_initialize (attribute_unused int argc, attribute_unused char *const *argv, const char *optstring,
  240. struct _getopt_data *d)
  241. {
  242. /* Start processing options with ARGV-element 1 (since ARGV-element 0
  243. is the program name); the sequence of previously skipped
  244. non-option ARGV-elements is empty. */
  245. d->__first_nonopt = d->__last_nonopt = d->optind;
  246. d->__nextchar = NULL;
  247. d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
  248. /* Determine how to handle the ordering of options and nonoptions. */
  249. if (optstring[0] == '-')
  250. {
  251. d->__ordering = RETURN_IN_ORDER;
  252. ++optstring;
  253. }
  254. else if (optstring[0] == '+')
  255. {
  256. d->__ordering = REQUIRE_ORDER;
  257. ++optstring;
  258. }
  259. else if (d->__posixly_correct)
  260. d->__ordering = REQUIRE_ORDER;
  261. else
  262. d->__ordering = PERMUTE;
  263. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  264. if (!d->__posixly_correct
  265. && argc == __libc_argc && argv == __libc_argv)
  266. {
  267. if (d->__nonoption_flags_max_len == 0)
  268. {
  269. if (__getopt_nonoption_flags == NULL
  270. || __getopt_nonoption_flags[0] == '\0')
  271. d->__nonoption_flags_max_len = -1;
  272. else
  273. {
  274. const char *orig_str = __getopt_nonoption_flags;
  275. int len = d->__nonoption_flags_max_len = strlen (orig_str);
  276. if (d->__nonoption_flags_max_len < argc)
  277. d->__nonoption_flags_max_len = argc;
  278. __getopt_nonoption_flags =
  279. (char *) malloc (d->__nonoption_flags_max_len);
  280. if (__getopt_nonoption_flags == NULL)
  281. d->__nonoption_flags_max_len = -1;
  282. else
  283. memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
  284. '\0', d->__nonoption_flags_max_len - len);
  285. }
  286. }
  287. d->__nonoption_flags_len = d->__nonoption_flags_max_len;
  288. }
  289. else
  290. d->__nonoption_flags_len = 0;
  291. #endif
  292. return optstring;
  293. }
  294. /* Scan elements of ARGV (whose length is ARGC) for option characters
  295. given in OPTSTRING.
  296. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  297. then it is an option element. The characters of this element
  298. (aside from the initial '-') are option characters. If `getopt'
  299. is called repeatedly, it returns successively each of the option characters
  300. from each of the option elements.
  301. If `getopt' finds another option character, it returns that character,
  302. updating `optind' and `nextchar' so that the next call to `getopt' can
  303. resume the scan with the following option character or ARGV-element.
  304. If there are no more option characters, `getopt' returns -1.
  305. Then `optind' is the index in ARGV of the first ARGV-element
  306. that is not an option. (The ARGV-elements have been permuted
  307. so that those that are not options now come last.)
  308. OPTSTRING is a string containing the legitimate option characters.
  309. If an option character is seen that is not listed in OPTSTRING,
  310. return '?' after printing an error message. If you set `opterr' to
  311. zero, the error message is suppressed but we still return '?'.
  312. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  313. so the following text in the same ARGV-element, or the text of the following
  314. ARGV-element, is returned in `optarg'. Two colons mean an option that
  315. wants an optional arg; if there is text in the current ARGV-element,
  316. it is returned in `optarg', otherwise `optarg' is set to zero.
  317. If OPTSTRING starts with `-' or `+', it requests different methods of
  318. handling the non-option ARGV-elements.
  319. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  320. Long-named options begin with `--' instead of `-'.
  321. Their names may be abbreviated as long as the abbreviation is unique
  322. or is an exact match for some defined option. If they have an
  323. argument, it follows the option name in the same ARGV-element, separated
  324. from the option name by a `=', or else the in next ARGV-element.
  325. When `getopt' finds a long-named option, it returns 0 if that option's
  326. `flag' field is nonzero, the value of the option's `val' field
  327. if the `flag' field is zero.
  328. The elements of ARGV aren't really const, because we permute them.
  329. But we pretend they're const in the prototype to be compatible
  330. with other systems.
  331. LONGOPTS is a vector of `struct option' terminated by an
  332. element containing a name which is zero.
  333. LONGIND returns the index in LONGOPT of the long-named option found.
  334. It is only valid when a long-named option has been found by the most
  335. recent call.
  336. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  337. long-named options. */
  338. static int
  339. _getopt_internal_r (int argc, char *const *argv, const char *optstring,
  340. const struct option *longopts, int *longind,
  341. int long_only, struct _getopt_data *d)
  342. {
  343. int print_errors = d->opterr;
  344. if (optstring[0] == ':')
  345. print_errors = 0;
  346. if (argc < 1)
  347. return -1;
  348. d->optarg = NULL;
  349. if (d->optind == 0 || !d->__initialized)
  350. {
  351. if (d->optind == 0)
  352. d->optind = 1; /* Don't scan ARGV[0], the program name. */
  353. optstring = _getopt_initialize (argc, argv, optstring, d);
  354. d->__initialized = 1;
  355. }
  356. /* Test whether ARGV[optind] points to a non-option argument.
  357. Either it does not have option syntax, or there is an environment flag
  358. from the shell indicating it is not an option. The later information
  359. is only used when the used in the GNU libc. */
  360. #if defined _LIBC && defined USE_NONOPTION_FLAGS
  361. # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
  362. || (d->optind < d->__nonoption_flags_len \
  363. && __getopt_nonoption_flags[d->optind] == '1'))
  364. #else
  365. # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
  366. #endif
  367. if (d->__nextchar == NULL || *d->__nextchar == '\0')
  368. {
  369. /* Advance to the next ARGV-element. */
  370. /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
  371. moved back by the user (who may also have changed the arguments). */
  372. if (d->__last_nonopt > d->optind)
  373. d->__last_nonopt = d->optind;
  374. if (d->__first_nonopt > d->optind)
  375. d->__first_nonopt = d->optind;
  376. if (d->__ordering == PERMUTE)
  377. {
  378. /* If we have just processed some options following some non-options,
  379. exchange them so that the options come first. */
  380. if (d->__first_nonopt != d->__last_nonopt
  381. && d->__last_nonopt != d->optind)
  382. exchange ((char **) argv, d);
  383. else if (d->__last_nonopt != d->optind)
  384. d->__first_nonopt = d->optind;
  385. /* Skip any additional non-options
  386. and extend the range of non-options previously skipped. */
  387. while (d->optind < argc && NONOPTION_P)
  388. d->optind++;
  389. d->__last_nonopt = d->optind;
  390. }
  391. /* The special ARGV-element `--' means premature end of options.
  392. Skip it like a null option,
  393. then exchange with previous non-options as if it were an option,
  394. then skip everything else like a non-option. */
  395. if (d->optind != argc && !strcmp (argv[d->optind], "--"))
  396. {
  397. d->optind++;
  398. if (d->__first_nonopt != d->__last_nonopt
  399. && d->__last_nonopt != d->optind)
  400. exchange ((char **) argv, d);
  401. else if (d->__first_nonopt == d->__last_nonopt)
  402. d->__first_nonopt = d->optind;
  403. d->__last_nonopt = argc;
  404. d->optind = argc;
  405. }
  406. /* If we have done all the ARGV-elements, stop the scan
  407. and back over any non-options that we skipped and permuted. */
  408. if (d->optind == argc)
  409. {
  410. /* Set the next-arg-index to point at the non-options
  411. that we previously skipped, so the caller will digest them. */
  412. if (d->__first_nonopt != d->__last_nonopt)
  413. d->optind = d->__first_nonopt;
  414. return -1;
  415. }
  416. /* If we have come to a non-option and did not permute it,
  417. either stop the scan or describe it to the caller and pass it by. */
  418. if (NONOPTION_P)
  419. {
  420. if (d->__ordering == REQUIRE_ORDER)
  421. return -1;
  422. d->optarg = argv[d->optind++];
  423. return 1;
  424. }
  425. /* We have found another option-ARGV-element.
  426. Skip the initial punctuation. */
  427. d->__nextchar = (argv[d->optind] + 1
  428. + (longopts != NULL && argv[d->optind][1] == '-'));
  429. }
  430. /* Decode the current option-ARGV-element. */
  431. /* Check whether the ARGV-element is a long option.
  432. If long_only and the ARGV-element has the form "-f", where f is
  433. a valid short option, don't consider it an abbreviated form of
  434. a long option that starts with f. Otherwise there would be no
  435. way to give the -f short option.
  436. On the other hand, if there's a long option "fubar" and
  437. the ARGV-element is "-fu", do consider that an abbreviation of
  438. the long option, just like "--fu", and not "-f" with arg "u".
  439. This distinction seems to be the most useful approach. */
  440. if (longopts != NULL
  441. && (argv[d->optind][1] == '-'
  442. || (long_only && (argv[d->optind][2]
  443. || !strchr (optstring, argv[d->optind][1])))))
  444. {
  445. char *nameend;
  446. const struct option *p;
  447. const struct option *pfound = NULL;
  448. int exact = 0;
  449. int ambig = 0;
  450. int indfound = -1;
  451. int option_index;
  452. for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
  453. /* Do nothing. */ ;
  454. /* Test all long options for either exact match
  455. or abbreviated matches. */
  456. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  457. if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
  458. {
  459. if ((unsigned int) (nameend - d->__nextchar)
  460. == (unsigned int) strlen (p->name))
  461. {
  462. /* Exact match found. */
  463. pfound = p;
  464. indfound = option_index;
  465. exact = 1;
  466. break;
  467. }
  468. else if (pfound == NULL)
  469. {
  470. /* First nonexact match found. */
  471. pfound = p;
  472. indfound = option_index;
  473. }
  474. else if (long_only
  475. || pfound->has_arg != p->has_arg
  476. || pfound->flag != p->flag
  477. || pfound->val != p->val)
  478. /* Second or later nonexact match found. */
  479. ambig = 1;
  480. }
  481. if (ambig && !exact)
  482. {
  483. if (print_errors)
  484. {
  485. #if defined _LIBC && defined USE_IN_LIBIO
  486. char *buf;
  487. if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
  488. argv[0], argv[d->optind]) >= 0)
  489. {
  490. _IO_flockfile (stderr);
  491. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  492. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  493. __fxprintf (NULL, "%s", buf);
  494. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  495. _IO_funlockfile (stderr);
  496. free (buf);
  497. }
  498. #else
  499. fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
  500. argv[0], argv[d->optind]);
  501. #endif
  502. }
  503. d->__nextchar += strlen (d->__nextchar);
  504. d->optind++;
  505. d->optopt = 0;
  506. return '?';
  507. }
  508. if (pfound != NULL)
  509. {
  510. option_index = indfound;
  511. d->optind++;
  512. if (*nameend)
  513. {
  514. /* Don't test has_arg with >, because some C compilers don't
  515. allow it to be used on enums. */
  516. if (pfound->has_arg)
  517. d->optarg = nameend + 1;
  518. else
  519. {
  520. if (print_errors)
  521. {
  522. #if defined _LIBC && defined USE_IN_LIBIO
  523. char *buf;
  524. int n;
  525. #endif
  526. if (argv[d->optind - 1][1] == '-')
  527. {
  528. /* --option */
  529. #if defined _LIBC && defined USE_IN_LIBIO
  530. n = __asprintf (&buf, _("\
  531. %s: option `--%s' doesn't allow an argument\n"),
  532. argv[0], pfound->name);
  533. #else
  534. fprintf (stderr, _("\
  535. %s: option `--%s' doesn't allow an argument\n"),
  536. argv[0], pfound->name);
  537. #endif
  538. }
  539. else
  540. {
  541. /* +option or -option */
  542. #if defined _LIBC && defined USE_IN_LIBIO
  543. n = __asprintf (&buf, _("\
  544. %s: option `%c%s' doesn't allow an argument\n"),
  545. argv[0], argv[d->optind - 1][0],
  546. pfound->name);
  547. #else
  548. fprintf (stderr, _("\
  549. %s: option `%c%s' doesn't allow an argument\n"),
  550. argv[0], argv[d->optind - 1][0],
  551. pfound->name);
  552. #endif
  553. }
  554. #if defined _LIBC && defined USE_IN_LIBIO
  555. if (n >= 0)
  556. {
  557. _IO_flockfile (stderr);
  558. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  559. ((_IO_FILE *) stderr)->_flags2
  560. |= _IO_FLAGS2_NOTCANCEL;
  561. __fxprintf (NULL, "%s", buf);
  562. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  563. _IO_funlockfile (stderr);
  564. free (buf);
  565. }
  566. #endif
  567. }
  568. d->__nextchar += strlen (d->__nextchar);
  569. d->optopt = pfound->val;
  570. return '?';
  571. }
  572. }
  573. else if (pfound->has_arg == 1)
  574. {
  575. if (d->optind < argc)
  576. d->optarg = argv[d->optind++];
  577. else
  578. {
  579. if (print_errors)
  580. {
  581. #if defined _LIBC && defined USE_IN_LIBIO
  582. char *buf;
  583. if (__asprintf (&buf, _("\
  584. %s: option `%s' requires an argument\n"),
  585. argv[0], argv[d->optind - 1]) >= 0)
  586. {
  587. _IO_flockfile (stderr);
  588. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  589. ((_IO_FILE *) stderr)->_flags2
  590. |= _IO_FLAGS2_NOTCANCEL;
  591. __fxprintf (NULL, "%s", buf);
  592. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  593. _IO_funlockfile (stderr);
  594. free (buf);
  595. }
  596. #else
  597. fprintf (stderr,
  598. _("%s: option `%s' requires an argument\n"),
  599. argv[0], argv[d->optind - 1]);
  600. #endif
  601. }
  602. d->__nextchar += strlen (d->__nextchar);
  603. d->optopt = pfound->val;
  604. return optstring[0] == ':' ? ':' : '?';
  605. }
  606. }
  607. d->__nextchar += strlen (d->__nextchar);
  608. if (longind != NULL)
  609. *longind = option_index;
  610. if (pfound->flag)
  611. {
  612. *(pfound->flag) = pfound->val;
  613. return 0;
  614. }
  615. return pfound->val;
  616. }
  617. /* Can't find it as a long option. If this is not getopt_long_only,
  618. or the option starts with '--' or is not a valid short
  619. option, then it's an error.
  620. Otherwise interpret it as a short option. */
  621. if (!long_only || argv[d->optind][1] == '-'
  622. || strchr (optstring, *d->__nextchar) == NULL)
  623. {
  624. if (print_errors)
  625. {
  626. #if defined _LIBC && defined USE_IN_LIBIO
  627. char *buf;
  628. int n;
  629. #endif
  630. if (argv[d->optind][1] == '-')
  631. {
  632. /* --option */
  633. #if defined _LIBC && defined USE_IN_LIBIO
  634. n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
  635. argv[0], d->__nextchar);
  636. #else
  637. fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
  638. argv[0], d->__nextchar);
  639. #endif
  640. }
  641. else
  642. {
  643. /* +option or -option */
  644. #if defined _LIBC && defined USE_IN_LIBIO
  645. n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
  646. argv[0], argv[d->optind][0], d->__nextchar);
  647. #else
  648. fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
  649. argv[0], argv[d->optind][0], d->__nextchar);
  650. #endif
  651. }
  652. #if defined _LIBC && defined USE_IN_LIBIO
  653. if (n >= 0)
  654. {
  655. _IO_flockfile (stderr);
  656. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  657. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  658. __fxprintf (NULL, "%s", buf);
  659. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  660. _IO_funlockfile (stderr);
  661. free (buf);
  662. }
  663. #endif
  664. }
  665. d->__nextchar = (char *) "";
  666. d->optind++;
  667. d->optopt = 0;
  668. return '?';
  669. }
  670. }
  671. /* Look at and handle the next short option-character. */
  672. {
  673. char c = *d->__nextchar++;
  674. char *temp = strchr (optstring, c);
  675. /* Increment `optind' when we start to process its last character. */
  676. if (*d->__nextchar == '\0')
  677. ++d->optind;
  678. if (temp == NULL || c == ':')
  679. {
  680. if (print_errors)
  681. {
  682. #if defined _LIBC && defined USE_IN_LIBIO
  683. char *buf;
  684. int n;
  685. #endif
  686. if (d->__posixly_correct)
  687. {
  688. /* 1003.2 specifies the format of this message. */
  689. #if defined _LIBC && defined USE_IN_LIBIO
  690. n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
  691. argv[0], c);
  692. #else
  693. fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
  694. #endif
  695. }
  696. else
  697. {
  698. #if defined _LIBC && defined USE_IN_LIBIO
  699. n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
  700. argv[0], c);
  701. #else
  702. fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
  703. #endif
  704. }
  705. #if defined _LIBC && defined USE_IN_LIBIO
  706. if (n >= 0)
  707. {
  708. _IO_flockfile (stderr);
  709. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  710. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  711. __fxprintf (NULL, "%s", buf);
  712. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  713. _IO_funlockfile (stderr);
  714. free (buf);
  715. }
  716. #endif
  717. }
  718. d->optopt = c;
  719. return '?';
  720. }
  721. #ifdef SPECIAL_TREATMENT_FOR_W
  722. /* Convenience. Treat POSIX -W foo same as long option --foo */
  723. if (temp[0] == 'W' && temp[1] == ';')
  724. {
  725. char *nameend;
  726. const struct option *p;
  727. const struct option *pfound = NULL;
  728. int exact = 0;
  729. int ambig = 0;
  730. int indfound = 0;
  731. int option_index;
  732. /* This is an option that requires an argument. */
  733. if (*d->__nextchar != '\0')
  734. {
  735. d->optarg = d->__nextchar;
  736. /* If we end this ARGV-element by taking the rest as an arg,
  737. we must advance to the next element now. */
  738. d->optind++;
  739. }
  740. else if (d->optind == argc)
  741. {
  742. if (print_errors)
  743. {
  744. /* 1003.2 specifies the format of this message. */
  745. #if defined _LIBC && defined USE_IN_LIBIO
  746. char *buf;
  747. if (__asprintf (&buf,
  748. _("%s: option requires an argument -- %c\n"),
  749. argv[0], c) >= 0)
  750. {
  751. _IO_flockfile (stderr);
  752. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  753. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  754. __fxprintf (NULL, "%s", buf);
  755. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  756. _IO_funlockfile (stderr);
  757. free (buf);
  758. }
  759. #else
  760. fprintf (stderr, _("%s: option requires an argument -- %c\n"),
  761. argv[0], c);
  762. #endif
  763. }
  764. d->optopt = c;
  765. if (optstring[0] == ':')
  766. c = ':';
  767. else
  768. c = '?';
  769. return c;
  770. }
  771. else
  772. /* We already incremented `d->optind' once;
  773. increment it again when taking next ARGV-elt as argument. */
  774. d->optarg = argv[d->optind++];
  775. /* optarg is now the argument, see if it's in the
  776. table of longopts. */
  777. for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
  778. nameend++)
  779. /* Do nothing. */ ;
  780. /* Test all long options for either exact match
  781. or abbreviated matches. */
  782. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  783. if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
  784. {
  785. if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
  786. {
  787. /* Exact match found. */
  788. pfound = p;
  789. indfound = option_index;
  790. exact = 1;
  791. break;
  792. }
  793. else if (pfound == NULL)
  794. {
  795. /* First nonexact match found. */
  796. pfound = p;
  797. indfound = option_index;
  798. }
  799. else
  800. /* Second or later nonexact match found. */
  801. ambig = 1;
  802. }
  803. if (ambig && !exact)
  804. {
  805. if (print_errors)
  806. {
  807. #if defined _LIBC && defined USE_IN_LIBIO
  808. char *buf;
  809. if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
  810. argv[0], argv[d->optind]) >= 0)
  811. {
  812. _IO_flockfile (stderr);
  813. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  814. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  815. __fxprintf (NULL, "%s", buf);
  816. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  817. _IO_funlockfile (stderr);
  818. free (buf);
  819. }
  820. #else
  821. fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
  822. argv[0], argv[d->optind]);
  823. #endif
  824. }
  825. d->__nextchar += strlen (d->__nextchar);
  826. d->optind++;
  827. return '?';
  828. }
  829. if (pfound != NULL)
  830. {
  831. option_index = indfound;
  832. if (*nameend)
  833. {
  834. /* Don't test has_arg with >, because some C compilers don't
  835. allow it to be used on enums. */
  836. if (pfound->has_arg)
  837. d->optarg = nameend + 1;
  838. else
  839. {
  840. if (print_errors)
  841. {
  842. #if defined _LIBC && defined USE_IN_LIBIO
  843. char *buf;
  844. if (__asprintf (&buf, _("\
  845. %s: option `-W %s' doesn't allow an argument\n"),
  846. argv[0], pfound->name) >= 0)
  847. {
  848. _IO_flockfile (stderr);
  849. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  850. ((_IO_FILE *) stderr)->_flags2
  851. |= _IO_FLAGS2_NOTCANCEL;
  852. __fxprintf (NULL, "%s", buf);
  853. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  854. _IO_funlockfile (stderr);
  855. free (buf);
  856. }
  857. #else
  858. fprintf (stderr, _("\
  859. %s: option `-W %s' doesn't allow an argument\n"),
  860. argv[0], pfound->name);
  861. #endif
  862. }
  863. d->__nextchar += strlen (d->__nextchar);
  864. return '?';
  865. }
  866. }
  867. else if (pfound->has_arg == 1)
  868. {
  869. if (d->optind < argc)
  870. d->optarg = argv[d->optind++];
  871. else
  872. {
  873. if (print_errors)
  874. {
  875. #if defined _LIBC && defined USE_IN_LIBIO
  876. char *buf;
  877. if (__asprintf (&buf, _("\
  878. %s: option `%s' requires an argument\n"),
  879. argv[0], argv[d->optind - 1]) >= 0)
  880. {
  881. _IO_flockfile (stderr);
  882. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  883. ((_IO_FILE *) stderr)->_flags2
  884. |= _IO_FLAGS2_NOTCANCEL;
  885. __fxprintf (NULL, "%s", buf);
  886. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  887. _IO_funlockfile (stderr);
  888. free (buf);
  889. }
  890. #else
  891. fprintf (stderr,
  892. _("%s: option `%s' requires an argument\n"),
  893. argv[0], argv[d->optind - 1]);
  894. #endif
  895. }
  896. d->__nextchar += strlen (d->__nextchar);
  897. return optstring[0] == ':' ? ':' : '?';
  898. }
  899. }
  900. d->__nextchar += strlen (d->__nextchar);
  901. if (longind != NULL)
  902. *longind = option_index;
  903. if (pfound->flag)
  904. {
  905. *(pfound->flag) = pfound->val;
  906. return 0;
  907. }
  908. return pfound->val;
  909. }
  910. d->__nextchar = NULL;
  911. return 'W'; /* Let the application handle it. */
  912. }
  913. #endif
  914. if (temp[1] == ':')
  915. {
  916. if (temp[2] == ':')
  917. {
  918. /* This is an option that accepts an argument optionally. */
  919. if (*d->__nextchar != '\0')
  920. {
  921. d->optarg = d->__nextchar;
  922. d->optind++;
  923. }
  924. else
  925. d->optarg = NULL;
  926. d->__nextchar = NULL;
  927. }
  928. else
  929. {
  930. /* This is an option that requires an argument. */
  931. if (*d->__nextchar != '\0')
  932. {
  933. d->optarg = d->__nextchar;
  934. /* If we end this ARGV-element by taking the rest as an arg,
  935. we must advance to the next element now. */
  936. d->optind++;
  937. }
  938. else if (d->optind == argc)
  939. {
  940. if (print_errors)
  941. {
  942. /* 1003.2 specifies the format of this message. */
  943. #if defined _LIBC && defined USE_IN_LIBIO
  944. char *buf;
  945. if (__asprintf (&buf, _("\
  946. %s: option requires an argument -- %c\n"),
  947. argv[0], c) >= 0)
  948. {
  949. _IO_flockfile (stderr);
  950. int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
  951. ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  952. __fxprintf (NULL, "%s", buf);
  953. ((_IO_FILE *) stderr)->_flags2 = old_flags2;
  954. _IO_funlockfile (stderr);
  955. free (buf);
  956. }
  957. #else
  958. fprintf (stderr,
  959. _("%s: option requires an argument -- %c\n"),
  960. argv[0], c);
  961. #endif
  962. }
  963. d->optopt = c;
  964. if (optstring[0] == ':')
  965. c = ':';
  966. else
  967. c = '?';
  968. }
  969. else
  970. /* We already incremented `optind' once;
  971. increment it again when taking next ARGV-elt as argument. */
  972. d->optarg = argv[d->optind++];
  973. d->__nextchar = NULL;
  974. }
  975. }
  976. return c;
  977. }
  978. }
  979. int
  980. _getopt_internal (int argc, char *const *argv, const char *optstring,
  981. const struct option *longopts, int *longind, int long_only)
  982. {
  983. int result;
  984. getopt_data.optind = optind;
  985. getopt_data.opterr = opterr;
  986. result = _getopt_internal_r (argc, argv, optstring, longopts,
  987. longind, long_only, &getopt_data);
  988. optind = getopt_data.optind;
  989. optarg = getopt_data.optarg;
  990. optopt = getopt_data.optopt;
  991. return result;
  992. }
  993. int
  994. getopt (int argc, char *const *argv, const char *optstring)
  995. {
  996. return _getopt_internal (argc, argv, optstring,
  997. (const struct option *) 0,
  998. (int *) 0,
  999. 0);
  1000. }
  1001. int
  1002. getopt_long (int argc, char *const *argv, const char *options,
  1003. const struct option *long_options, int *opt_index)
  1004. {
  1005. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  1006. }
  1007. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  1008. If an option that starts with '-' (not '--') doesn't match a long option,
  1009. but does match a short option, it is parsed as a short option
  1010. instead. */
  1011. int
  1012. getopt_long_only (int argc, char *const *argv, const char *options,
  1013. const struct option *long_options, int *opt_index)
  1014. {
  1015. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  1016. }
  1017. #endif /* Not ELIDE_CODE. */