getopt.c 39 KB

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