getopt.c 32 KB

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