getopt.c 31 KB

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