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