getopt.c 31 KB

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