Support: quest -f cjk_ngram
[xapian.git] / xapian-core / common / getopt.cc
blob0001bbb58306235337b1a28bbe837e833c4fe087
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!
6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
7 Free Software Foundation, Inc.
8 Copyright (C) 2004,2009,2010,2015 Olly Betts (reworked to allow compilation as C++)
10 The GNU C Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 The GNU C Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with the GNU C Library; see the file COPYING.LIB. If not,
22 write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
30 #include "gnu_getopt.h"
32 /* #ifdef out all this code if we are using the GNU C Library. GNU getopt
33 is included in the GNU C Library, and linking in this code is a waste when
34 using the GNU C library (especially if it is a shared library). */
36 #ifndef USE_GLIBC_GNUGETOPT
38 #include <cstdio>
40 using std::fprintf;
42 #ifdef VMS
43 # include <unixlib.h>
44 #endif
46 #ifndef _
47 /* This is for other GNU distributions with internationalized messages. */
48 # if 0 //defined HAVE_LIBINTL_H || defined _LIBC
49 # include <libintl.h>
50 # ifndef _
51 # define _(msgid) gettext (msgid)
52 # endif
53 # else
54 # define _(msgid) (msgid)
55 # endif
56 #endif
58 #ifndef __CYGWIN__
59 /* This version of `getopt' appears to the caller like standard Unix `getopt'
60 but it behaves differently for the user, since it allows the user
61 to intersperse the options with the other arguments.
63 As `getopt' works, it permutes the elements of ARGV so that,
64 when it is done, all the options precede everything else. Thus
65 all application programs are extended to handle flexible argument order.
67 Setting the environment variable POSIXLY_CORRECT disables permutation.
68 Then the behavior is completely standard.
70 GNU application programs can use a third alternative mode in which
71 they can distinguish the relative order of options and other arguments. */
73 /* For communication from `getopt' to the caller.
74 When `getopt' finds an option that takes an argument,
75 the argument value is returned here.
76 Also, when `ordering' is RETURN_IN_ORDER,
77 each non-option ARGV-element is returned here. */
79 char *optarg;
81 /* Index in ARGV of the next element to be scanned.
82 This is used for communication to and from the caller
83 and for communication between successive calls to `getopt'.
85 On entry to `getopt', zero means this is the first call; initialize.
87 When `getopt' returns -1, this is the index of the first of the
88 non-option elements that the caller should itself scan.
90 Otherwise, `optind' communicates from one call to the next
91 how much of ARGV has been scanned so far. */
93 /* 1003.2 says this must be 1 before any call. */
94 int optind = 1;
96 /* Callers store zero here to inhibit the error message
97 for unrecognized options. */
99 int opterr = 1;
101 /* Set to an option character which was unrecognized.
102 This must be initialized on some systems to avoid linking in the
103 system's own getopt implementation. */
105 int optopt = '?';
106 #endif
108 /* Formerly, initialization of getopt depended on optind==0, which
109 causes problems with re-calling getopt as programs generally don't
110 know that. */
112 static int getopt_initialized;
114 /* The next char to be scanned in the option-element
115 in which the last option character we returned was found.
116 This allows us to pick up the scan where we left off.
118 If this is zero, or a null string, it means resume the scan
119 by advancing to the next ARGV-element. */
121 static char *nextchar;
123 /* Describe how to deal with options that follow non-option ARGV-elements.
125 If the caller did not specify anything,
126 the default is REQUIRE_ORDER if the environment variable
127 POSIXLY_CORRECT is defined, PERMUTE otherwise.
129 REQUIRE_ORDER means don't recognize them as options;
130 stop option processing when the first non-option is seen.
131 This is what Unix does.
132 This mode of operation is selected by either setting the environment
133 variable POSIXLY_CORRECT, or using `+' as the first character
134 of the list of option characters.
136 PERMUTE is the default. We permute the contents of ARGV as we scan,
137 so that eventually all the non-options are at the end. This allows options
138 to be given in any order, even with programs that were not written to
139 expect this.
141 RETURN_IN_ORDER is an option available to programs that were written
142 to expect options and other ARGV-elements in any order and that care about
143 the ordering of the two. We describe each non-option ARGV-element
144 as if it were the argument of an option with character code 1.
145 Using `-' as the first character of the list of option characters
146 selects this mode of operation.
148 The special argument `--' forces an end of option-scanning regardless
149 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
150 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
152 static enum
154 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
155 } ordering;
157 /* Value of POSIXLY_CORRECT environment variable. */
158 static char *posixly_correct;
160 #include <cstring>
161 using std::strlen;
162 using std::strcmp;
163 using std::strncmp;
164 using std::strchr;
166 #include <cstdlib>
167 using std::getenv;
170 /* Handle permutation of arguments. */
172 /* Describe the part of ARGV that contains non-options that have
173 been skipped. `first_nonopt' is the index in ARGV of the first of them;
174 `last_nonopt' is the index after the last of them. */
176 static int first_nonopt;
177 static int last_nonopt;
179 /* Exchange two adjacent subsequences of ARGV.
180 One subsequence is elements [first_nonopt,last_nonopt)
181 which contains all the non-options that have been skipped so far.
182 The other is elements [last_nonopt,optind), which contains all
183 the options processed since those non-options were skipped.
185 `first_nonopt' and `last_nonopt' are relocated so that they describe
186 the new indices of the non-options in ARGV after they are moved. */
188 static void
189 exchange (char **argv)
191 int bottom = first_nonopt;
192 int middle = last_nonopt;
193 int top = optind;
194 char *tem;
196 /* Exchange the shorter segment with the far end of the longer segment.
197 That puts the shorter segment into the right place.
198 It leaves the longer segment in the right place overall,
199 but it consists of two parts that need to be swapped next. */
201 while (top > middle && middle > bottom)
203 if (top - middle > middle - bottom)
205 /* Bottom segment is the short one. */
206 int len = middle - bottom;
207 int i;
209 /* Swap it with the top part of the top segment. */
210 for (i = 0; i < len; i++)
212 tem = argv[bottom + i];
213 argv[bottom + i] = argv[top - (middle - bottom) + i];
214 argv[top - (middle - bottom) + i] = tem;
216 /* Exclude the moved bottom segment from further swapping. */
217 top -= len;
219 else
221 /* Top segment is the short one. */
222 int len = top - middle;
223 int i;
225 /* Swap it with the bottom part of the bottom segment. */
226 for (i = 0; i < len; i++)
228 tem = argv[bottom + i];
229 argv[bottom + i] = argv[middle + i];
230 argv[middle + i] = tem;
232 /* Exclude the moved top segment from further swapping. */
233 bottom += len;
237 /* Update records for the slots the non-options now occupy. */
239 first_nonopt += (optind - last_nonopt);
240 last_nonopt = optind;
243 /* Initialize the internal data when the first call is made. */
245 static const char *
246 getopt_initialize (int argc, char *const *argv, const char *optstring)
248 /* Suppress possible unused warnings */
249 (void)argc;
250 (void)argv;
252 /* Start processing options with ARGV-element 1 (since ARGV-element 0
253 is the program name); the sequence of previously skipped
254 non-option ARGV-elements is empty. */
256 first_nonopt = last_nonopt = optind;
258 nextchar = NULL;
260 posixly_correct = getenv ("POSIXLY_CORRECT");
262 /* Determine how to handle the ordering of options and nonoptions. */
264 if (optstring[0] == '-')
266 ordering = RETURN_IN_ORDER;
267 ++optstring;
269 else if (optstring[0] == '+')
271 ordering = REQUIRE_ORDER;
272 ++optstring;
274 else if (posixly_correct != NULL)
275 ordering = REQUIRE_ORDER;
276 else
277 ordering = PERMUTE;
279 return optstring;
282 /* Scan elements of ARGV (whose length is ARGC) for option characters
283 given in OPTSTRING.
285 If an element of ARGV starts with '-', and is not exactly "-" or "--",
286 then it is an option element. The characters of this element
287 (aside from the initial '-') are option characters. If `getopt'
288 is called repeatedly, it returns successively each of the option characters
289 from each of the option elements.
291 If `getopt' finds another option character, it returns that character,
292 updating `optind' and `nextchar' so that the next call to `getopt' can
293 resume the scan with the following option character or ARGV-element.
295 If there are no more option characters, `getopt' returns -1.
296 Then `optind' is the index in ARGV of the first ARGV-element
297 that is not an option. (The ARGV-elements have been permuted
298 so that those that are not options now come last.)
300 OPTSTRING is a string containing the legitimate option characters.
301 If an option character is seen that is not listed in OPTSTRING,
302 return '?' after printing an error message. If you set `opterr' to
303 zero, the error message is suppressed but we still return '?'.
305 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
306 so the following text in the same ARGV-element, or the text of the following
307 ARGV-element, is returned in `optarg'. Two colons mean an option that
308 wants an optional arg; if there is text in the current ARGV-element,
309 it is returned in `optarg', otherwise `optarg' is set to zero.
311 If OPTSTRING starts with `-' or `+', it requests different methods of
312 handling the non-option ARGV-elements.
313 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
315 Long-named options begin with `--' instead of `-'.
316 Their names may be abbreviated as long as the abbreviation is unique
317 or is an exact match for some defined option. If they have an
318 argument, it follows the option name in the same ARGV-element, separated
319 from the option name by a `=', or else the in next ARGV-element.
320 When `getopt' finds a long-named option, it returns 0 if that option's
321 `flag' field is nonzero, the value of the option's `val' field
322 if the `flag' field is zero.
324 The elements of ARGV aren't really const, because we permute them.
325 But we pretend they're const in the prototype to be compatible
326 with other systems.
328 LONGOPTS is a vector of `struct option' terminated by an
329 element containing a name which is zero.
331 LONGIND returns the index in LONGOPT of the long-named option found.
332 It is only valid when a long-named option has been found by the most
333 recent call.
335 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
336 long-named options. */
339 gnu_getopt_internal_(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only)
341 int print_errors = opterr;
342 if (optstring[0] == ':')
343 print_errors = 0;
345 if (argc < 1)
346 return -1;
348 optarg = NULL;
350 if (optind == 0 || !getopt_initialized)
352 if (optind == 0)
353 optind = 1; /* Don't scan ARGV[0], the program name. */
354 optstring = getopt_initialize (argc, argv, optstring);
355 getopt_initialized = 1;
358 /* Test whether ARGV[optind] points to a non-option argument (i.e. it does
359 not have option syntax). */
360 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
362 if (nextchar == NULL || *nextchar == '\0')
364 /* Advance to the next ARGV-element. */
366 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
367 moved back by the user (who may also have changed the arguments). */
368 if (last_nonopt > optind)
369 last_nonopt = optind;
370 if (first_nonopt > optind)
371 first_nonopt = optind;
373 if (ordering == PERMUTE)
375 /* If we have just processed some options following some non-options,
376 exchange them so that the options come first. */
378 if (first_nonopt != last_nonopt && last_nonopt != optind)
379 exchange (const_cast<char **>(argv));
380 else if (last_nonopt != optind)
381 first_nonopt = optind;
383 /* Skip any additional non-options
384 and extend the range of non-options previously skipped. */
386 while (optind < argc && NONOPTION_P)
387 optind++;
388 last_nonopt = optind;
391 /* The special ARGV-element `--' means premature end of options.
392 Skip it like a null option,
393 then exchange with previous non-options as if it were an option,
394 then skip everything else like a non-option. */
396 if (optind != argc && !strcmp (argv[optind], "--"))
398 optind++;
400 if (first_nonopt != last_nonopt && last_nonopt != optind)
401 exchange (const_cast<char **>(argv));
402 else if (first_nonopt == last_nonopt)
403 first_nonopt = optind;
404 last_nonopt = argc;
406 optind = argc;
409 /* If we have done all the ARGV-elements, stop the scan
410 and back over any non-options that we skipped and permuted. */
412 if (optind == argc)
414 /* Set the next-arg-index to point at the non-options
415 that we previously skipped, so the caller will digest them. */
416 if (first_nonopt != last_nonopt)
417 optind = first_nonopt;
418 return -1;
421 /* If we have come to a non-option and did not permute it,
422 either stop the scan or describe it to the caller and pass it by. */
424 if (NONOPTION_P)
426 if (ordering == REQUIRE_ORDER)
427 return -1;
428 optarg = argv[optind++];
429 return 1;
432 /* We have found another option-ARGV-element.
433 Skip the initial punctuation. */
435 nextchar = (argv[optind] + 1
436 + (longopts != NULL && argv[optind][1] == '-'));
439 /* Decode the current option-ARGV-element. */
441 /* Check whether the ARGV-element is a long option.
443 If long_only and the ARGV-element has the form "-f", where f is
444 a valid short option, don't consider it an abbreviated form of
445 a long option that starts with f. Otherwise there would be no
446 way to give the -f short option.
448 On the other hand, if there's a long option "fubar" and
449 the ARGV-element is "-fu", do consider that an abbreviation of
450 the long option, just like "--fu", and not "-f" with arg "u".
452 This distinction seems to be the most useful approach. */
454 if (longopts != NULL
455 && (argv[optind][1] == '-'
456 || (long_only && (argv[optind][2] || !strchr (optstring, argv[optind][1])))))
458 char *nameend;
459 const struct option *p;
460 const struct option *pfound = NULL;
461 int exact = 0;
462 int ambig = 0;
463 int indfound = -1;
464 int option_index;
466 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
467 /* Do nothing. */ ;
469 /* Test all long options for either exact match
470 or abbreviated matches. */
471 for (p = longopts, option_index = 0; p->name; p++, option_index++)
472 if (!strncmp (p->name, nextchar, nameend - nextchar))
474 if (unsigned(nameend - nextchar) == unsigned(strlen(p->name)))
476 /* Exact match found. */
477 pfound = p;
478 indfound = option_index;
479 exact = 1;
480 break;
482 else if (pfound == NULL)
484 /* First nonexact match found. */
485 pfound = p;
486 indfound = option_index;
488 else if (long_only
489 || pfound->has_arg != p->has_arg
490 || pfound->flag != p->flag
491 || pfound->val != p->val)
492 /* Second or later nonexact match found. */
493 ambig = 1;
496 if (ambig && !exact)
498 if (print_errors)
499 fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
500 argv[0], argv[optind]);
501 nextchar += strlen (nextchar);
502 optind++;
503 optopt = 0;
504 return '?';
507 if (pfound != NULL)
509 option_index = indfound;
510 optind++;
511 if (*nameend)
513 /* Don't test has_arg with >, because some C compilers don't
514 allow it to be used on enums. */
515 if (pfound->has_arg)
516 optarg = nameend + 1;
517 else
519 if (print_errors)
521 if (argv[optind - 1][1] == '-')
522 /* --option */
523 fprintf (stderr,
524 _("%s: option '--%s' doesn't allow an argument\n"),
525 argv[0], pfound->name);
526 else
527 /* +option or -option */
528 fprintf (stderr,
529 _("%s: option '%c%s' doesn't allow an argument\n"),
530 argv[0], argv[optind - 1][0], pfound->name);
533 nextchar += strlen (nextchar);
535 optopt = pfound->val;
536 return '?';
539 else if (pfound->has_arg == 1)
541 if (optind < argc)
542 optarg = argv[optind++];
543 else
545 if (print_errors)
546 fprintf (stderr,
547 _("%s: option '%s' requires an argument\n"),
548 argv[0], argv[optind - 1]);
549 nextchar += strlen (nextchar);
550 optopt = pfound->val;
551 return optstring[0] == ':' ? ':' : '?';
554 nextchar += strlen (nextchar);
555 if (longind != NULL)
556 *longind = option_index;
557 if (pfound->flag)
559 *(pfound->flag) = pfound->val;
560 return 0;
562 return pfound->val;
565 /* Can't find it as a long option. If this is not getopt_long_only,
566 or the option starts with '--' or is not a valid short
567 option, then it's an error.
568 Otherwise interpret it as a short option. */
569 if (!long_only || argv[optind][1] == '-'
570 || strchr (optstring, *nextchar) == NULL)
572 if (print_errors)
574 if (argv[optind][1] == '-')
575 /* --option */
576 fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
577 argv[0], nextchar);
578 else
579 /* +option or -option */
580 fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
581 argv[0], argv[optind][0], nextchar);
583 nextchar = const_cast<char *>("");
584 optind++;
585 optopt = 0;
586 return '?';
590 /* Look at and handle the next short option-character. */
593 char c = *nextchar++;
594 const char *temp = strchr (optstring, c);
596 /* Increment `optind' when we start to process its last character. */
597 if (*nextchar == '\0')
598 ++optind;
600 if (temp == NULL || c == ':')
602 if (print_errors)
604 if (posixly_correct)
605 /* 1003.2 specifies the format of this message. */
606 fprintf (stderr, _("%s: illegal option -- %c\n"),
607 argv[0], c);
608 else
609 fprintf (stderr, _("%s: invalid option -- %c\n"),
610 argv[0], c);
612 optopt = c;
613 return '?';
615 /* Convenience. Treat POSIX -W foo same as long option --foo */
616 if (temp[0] == 'W' && temp[1] == ';')
618 char *nameend;
619 const struct option *p;
620 const struct option *pfound = NULL;
621 int exact = 0;
622 int ambig = 0;
623 int indfound = 0;
624 int option_index;
626 /* This is an option that requires an argument. */
627 if (*nextchar != '\0')
629 optarg = nextchar;
630 /* If we end this ARGV-element by taking the rest as an arg,
631 we must advance to the next element now. */
632 optind++;
634 else if (optind == argc)
636 if (print_errors)
638 /* 1003.2 specifies the format of this message. */
639 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
640 argv[0], c);
642 optopt = c;
643 if (optstring[0] == ':')
644 c = ':';
645 else
646 c = '?';
647 return c;
649 else
650 /* We already incremented `optind' once;
651 increment it again when taking next ARGV-elt as argument. */
652 optarg = argv[optind++];
654 /* optarg is now the argument, see if it's in the
655 table of longopts. */
657 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
658 /* Do nothing. */ ;
660 /* Test all long options for either exact match
661 or abbreviated matches. */
662 for (p = longopts, option_index = 0; p->name; p++, option_index++)
663 if (!strncmp (p->name, nextchar, nameend - nextchar))
665 if (unsigned(nameend - nextchar) == unsigned(strlen(p->name)))
667 /* Exact match found. */
668 pfound = p;
669 indfound = option_index;
670 exact = 1;
671 break;
673 else if (pfound == NULL)
675 /* First nonexact match found. */
676 pfound = p;
677 indfound = option_index;
679 else
680 /* Second or later nonexact match found. */
681 ambig = 1;
683 if (ambig && !exact)
685 if (print_errors)
686 fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
687 argv[0], argv[optind]);
688 nextchar += strlen (nextchar);
689 optind++;
690 return '?';
692 if (pfound != NULL)
694 option_index = indfound;
695 if (*nameend)
697 /* Don't test has_arg with >, because some C compilers don't
698 allow it to be used on enums. */
699 if (pfound->has_arg)
700 optarg = nameend + 1;
701 else
703 if (print_errors)
704 fprintf (stderr, _("\
705 %s: option '-W %s' doesn't allow an argument\n"),
706 argv[0], pfound->name);
708 nextchar += strlen (nextchar);
709 return '?';
712 else if (pfound->has_arg == 1)
714 if (optind < argc)
715 optarg = argv[optind++];
716 else
718 if (print_errors)
719 fprintf (stderr,
720 _("%s: option '%s' requires an argument\n"),
721 argv[0], argv[optind - 1]);
722 nextchar += strlen (nextchar);
723 return optstring[0] == ':' ? ':' : '?';
726 nextchar += strlen (nextchar);
727 if (longind != NULL)
728 *longind = option_index;
729 if (pfound->flag)
731 *(pfound->flag) = pfound->val;
732 return 0;
734 return pfound->val;
736 nextchar = NULL;
737 return 'W'; /* Let the application handle it. */
739 if (temp[1] == ':')
741 if (temp[2] == ':')
743 /* This is an option that accepts an argument optionally. */
744 if (*nextchar != '\0')
746 optarg = nextchar;
747 optind++;
749 else
750 optarg = NULL;
751 nextchar = NULL;
753 else
755 /* This is an option that requires an argument. */
756 if (*nextchar != '\0')
758 optarg = nextchar;
759 /* If we end this ARGV-element by taking the rest as an arg,
760 we must advance to the next element now. */
761 optind++;
763 else if (optind == argc)
765 if (print_errors)
767 /* 1003.2 specifies the format of this message. */
768 fprintf (stderr,
769 _("%s: option requires an argument -- %c\n"),
770 argv[0], c);
772 optopt = c;
773 if (optstring[0] == ':')
774 c = ':';
775 else
776 c = '?';
778 else
779 /* We already incremented `optind' once;
780 increment it again when taking next ARGV-elt as argument. */
781 optarg = argv[optind++];
782 nextchar = NULL;
785 return c;
789 #endif /* Not ELIDE_CODE. */