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