getopt.c 33 KB

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