1169695Skan/* Getopt for GNU. 2169695Skan NOTE: getopt is now part of the C library, so if you don't know what 3169695Skan "Keep this file name-space clean" means, talk to drepper@gnu.org 4169695Skan before changing it! 5169695Skan 6169695Skan Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 7169695Skan 1996, 1997, 1998, 2005 Free Software Foundation, Inc. 8169695Skan 9169695Skan NOTE: This source is derived from an old version taken from the GNU C 10169695Skan Library (glibc). 11169695Skan 12169695Skan This program is free software; you can redistribute it and/or modify it 13169695Skan under the terms of the GNU General Public License as published by the 14169695Skan Free Software Foundation; either version 2, or (at your option) any 15169695Skan later version. 16169695Skan 17169695Skan This program is distributed in the hope that it will be useful, 18169695Skan but WITHOUT ANY WARRANTY; without even the implied warranty of 19169695Skan MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20169695Skan GNU General Public License for more details. 21169695Skan 22169695Skan You should have received a copy of the GNU General Public License 23169695Skan along with this program; if not, write to the Free Software 24169695Skan Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 25169695Skan USA. */ 26169695Skan 27169695Skan/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 28169695Skan Ditto for AIX 3.2 and <stdlib.h>. */ 29169695Skan#ifndef _NO_PROTO 30169695Skan# define _NO_PROTO 31169695Skan#endif 32169695Skan 33169695Skan#ifdef HAVE_CONFIG_H 34169695Skan# include <config.h> 35169695Skan#endif 36169695Skan 37169695Skan#if !defined __STDC__ || !__STDC__ 38169695Skan/* This is a separate conditional since some stdc systems 39169695Skan reject `defined (const)'. */ 40169695Skan# ifndef const 41169695Skan# define const 42169695Skan# endif 43169695Skan#endif 44169695Skan 45169695Skan#include "ansidecl.h" 46169695Skan#include <stdio.h> 47169695Skan 48169695Skan/* Comment out all this code if we are using the GNU C Library, and are not 49169695Skan actually compiling the library itself. This code is part of the GNU C 50169695Skan Library, but also included in many other GNU distributions. Compiling 51169695Skan and linking in this code is a waste when using the GNU C library 52169695Skan (especially if it is a shared library). Rather than having every GNU 53169695Skan program understand `configure --with-gnu-libc' and omit the object files, 54169695Skan it is simpler to just do this in the source for each such file. */ 55169695Skan 56169695Skan#define GETOPT_INTERFACE_VERSION 2 57169695Skan#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 58169695Skan# include <gnu-versions.h> 59169695Skan# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 60169695Skan# define ELIDE_CODE 61169695Skan# endif 62169695Skan#endif 63169695Skan 64169695Skan#ifndef ELIDE_CODE 65169695Skan 66169695Skan 67169695Skan/* This needs to come after some library #include 68169695Skan to get __GNU_LIBRARY__ defined. */ 69169695Skan#ifdef __GNU_LIBRARY__ 70169695Skan/* Don't include stdlib.h for non-GNU C libraries because some of them 71169695Skan contain conflicting prototypes for getopt. */ 72169695Skan# include <stdlib.h> 73169695Skan# include <unistd.h> 74169695Skan#endif /* GNU C library. */ 75169695Skan 76169695Skan#ifdef VMS 77169695Skan# include <unixlib.h> 78169695Skan# if HAVE_STRING_H - 0 79169695Skan# include <string.h> 80169695Skan# endif 81169695Skan#endif 82169695Skan 83169695Skan#ifndef _ 84169695Skan/* This is for other GNU distributions with internationalized messages. 85169695Skan When compiling libc, the _ macro is predefined. */ 86169695Skan# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC 87169695Skan# include <libintl.h> 88169695Skan# define _(msgid) gettext (msgid) 89169695Skan# else 90169695Skan# define _(msgid) (msgid) 91169695Skan# endif 92169695Skan#endif 93169695Skan 94169695Skan/* This version of `getopt' appears to the caller like standard Unix `getopt' 95169695Skan but it behaves differently for the user, since it allows the user 96169695Skan to intersperse the options with the other arguments. 97169695Skan 98169695Skan As `getopt' works, it permutes the elements of ARGV so that, 99169695Skan when it is done, all the options precede everything else. Thus 100169695Skan all application programs are extended to handle flexible argument order. 101169695Skan 102169695Skan Setting the environment variable POSIXLY_CORRECT disables permutation. 103169695Skan Then the behavior is completely standard. 104169695Skan 105169695Skan GNU application programs can use a third alternative mode in which 106169695Skan they can distinguish the relative order of options and other arguments. */ 107169695Skan 108169695Skan#include "getopt.h" 109169695Skan 110169695Skan/* For communication from `getopt' to the caller. 111169695Skan When `getopt' finds an option that takes an argument, 112169695Skan the argument value is returned here. 113169695Skan Also, when `ordering' is RETURN_IN_ORDER, 114169695Skan each non-option ARGV-element is returned here. */ 115169695Skan 116169695Skanchar *optarg = NULL; 117169695Skan 118169695Skan/* Index in ARGV of the next element to be scanned. 119169695Skan This is used for communication to and from the caller 120169695Skan and for communication between successive calls to `getopt'. 121169695Skan 122169695Skan On entry to `getopt', zero means this is the first call; initialize. 123169695Skan 124169695Skan When `getopt' returns -1, this is the index of the first of the 125169695Skan non-option elements that the caller should itself scan. 126169695Skan 127169695Skan Otherwise, `optind' communicates from one call to the next 128169695Skan how much of ARGV has been scanned so far. */ 129169695Skan 130169695Skan/* 1003.2 says this must be 1 before any call. */ 131169695Skanint optind = 1; 132169695Skan 133169695Skan/* Formerly, initialization of getopt depended on optind==0, which 134169695Skan causes problems with re-calling getopt as programs generally don't 135169695Skan know that. */ 136169695Skan 137169695Skanint __getopt_initialized = 0; 138169695Skan 139169695Skan/* The next char to be scanned in the option-element 140169695Skan in which the last option character we returned was found. 141169695Skan This allows us to pick up the scan where we left off. 142169695Skan 143169695Skan If this is zero, or a null string, it means resume the scan 144169695Skan by advancing to the next ARGV-element. */ 145169695Skan 146169695Skanstatic char *nextchar; 147169695Skan 148169695Skan/* Callers store zero here to inhibit the error message 149169695Skan for unrecognized options. */ 150169695Skan 151169695Skanint opterr = 1; 152169695Skan 153169695Skan/* Set to an option character which was unrecognized. 154169695Skan This must be initialized on some systems to avoid linking in the 155169695Skan system's own getopt implementation. */ 156169695Skan 157169695Skanint optopt = '?'; 158169695Skan 159169695Skan/* Describe how to deal with options that follow non-option ARGV-elements. 160169695Skan 161169695Skan If the caller did not specify anything, 162169695Skan the default is REQUIRE_ORDER if the environment variable 163169695Skan POSIXLY_CORRECT is defined, PERMUTE otherwise. 164169695Skan 165169695Skan REQUIRE_ORDER means don't recognize them as options; 166169695Skan stop option processing when the first non-option is seen. 167169695Skan This is what Unix does. 168169695Skan This mode of operation is selected by either setting the environment 169169695Skan variable POSIXLY_CORRECT, or using `+' as the first character 170169695Skan of the list of option characters. 171169695Skan 172169695Skan PERMUTE is the default. We permute the contents of ARGV as we scan, 173169695Skan so that eventually all the non-options are at the end. This allows options 174169695Skan to be given in any order, even with programs that were not written to 175169695Skan expect this. 176169695Skan 177169695Skan RETURN_IN_ORDER is an option available to programs that were written 178169695Skan to expect options and other ARGV-elements in any order and that care about 179169695Skan the ordering of the two. We describe each non-option ARGV-element 180169695Skan as if it were the argument of an option with character code 1. 181169695Skan Using `-' as the first character of the list of option characters 182169695Skan selects this mode of operation. 183169695Skan 184169695Skan The special argument `--' forces an end of option-scanning regardless 185169695Skan of the value of `ordering'. In the case of RETURN_IN_ORDER, only 186169695Skan `--' can cause `getopt' to return -1 with `optind' != ARGC. */ 187169695Skan 188169695Skanstatic enum 189169695Skan{ 190169695Skan REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER 191169695Skan} ordering; 192169695Skan 193169695Skan/* Value of POSIXLY_CORRECT environment variable. */ 194169695Skanstatic char *posixly_correct; 195169695Skan 196169695Skan#ifdef __GNU_LIBRARY__ 197169695Skan/* We want to avoid inclusion of string.h with non-GNU libraries 198169695Skan because there are many ways it can cause trouble. 199169695Skan On some systems, it contains special magic macros that don't work 200169695Skan in GCC. */ 201169695Skan# include <string.h> 202169695Skan# define my_index strchr 203169695Skan#else 204169695Skan 205169695Skan# if HAVE_STRING_H 206169695Skan# include <string.h> 207169695Skan# else 208169695Skan# if HAVE_STRINGS_H 209169695Skan# include <strings.h> 210169695Skan# endif 211169695Skan# endif 212169695Skan 213169695Skan/* Avoid depending on library functions or files 214169695Skan whose names are inconsistent. */ 215169695Skan 216169695Skan#if HAVE_STDLIB_H && HAVE_DECL_GETENV 217169695Skan# include <stdlib.h> 218169695Skan#elif !defined(getenv) 219169695Skan# ifdef __cplusplus 220169695Skanextern "C" { 221169695Skan# endif /* __cplusplus */ 222169695Skanextern char *getenv (const char *); 223169695Skan# ifdef __cplusplus 224169695Skan} 225169695Skan# endif /* __cplusplus */ 226169695Skan#endif 227169695Skan 228169695Skanstatic char * 229169695Skanmy_index (const char *str, int chr) 230169695Skan{ 231169695Skan while (*str) 232169695Skan { 233169695Skan if (*str == chr) 234169695Skan return (char *) str; 235169695Skan str++; 236169695Skan } 237169695Skan return 0; 238169695Skan} 239169695Skan 240169695Skan/* If using GCC, we can safely declare strlen this way. 241169695Skan If not using GCC, it is ok not to declare it. */ 242169695Skan#ifdef __GNUC__ 243169695Skan/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. 244169695Skan That was relevant to code that was here before. */ 245169695Skan# if (!defined __STDC__ || !__STDC__) && !defined strlen 246169695Skan/* gcc with -traditional declares the built-in strlen to return int, 247169695Skan and has done so at least since version 2.4.5. -- rms. */ 248169695Skanextern int strlen (const char *); 249169695Skan# endif /* not __STDC__ */ 250169695Skan#endif /* __GNUC__ */ 251169695Skan 252169695Skan#endif /* not __GNU_LIBRARY__ */ 253169695Skan 254169695Skan/* Handle permutation of arguments. */ 255169695Skan 256169695Skan/* Describe the part of ARGV that contains non-options that have 257169695Skan been skipped. `first_nonopt' is the index in ARGV of the first of them; 258169695Skan `last_nonopt' is the index after the last of them. */ 259169695Skan 260169695Skanstatic int first_nonopt; 261169695Skanstatic int last_nonopt; 262169695Skan 263169695Skan#ifdef _LIBC 264169695Skan/* Bash 2.0 gives us an environment variable containing flags 265169695Skan indicating ARGV elements that should not be considered arguments. */ 266169695Skan 267169695Skan/* Defined in getopt_init.c */ 268169695Skanextern char *__getopt_nonoption_flags; 269169695Skan 270169695Skanstatic int nonoption_flags_max_len; 271169695Skanstatic int nonoption_flags_len; 272169695Skan 273169695Skanstatic int original_argc; 274169695Skanstatic char *const *original_argv; 275169695Skan 276169695Skan/* Make sure the environment variable bash 2.0 puts in the environment 277169695Skan is valid for the getopt call we must make sure that the ARGV passed 278169695Skan to getopt is that one passed to the process. */ 279169695Skanstatic void 280169695Skan__attribute__ ((unused)) 281169695Skanstore_args_and_env (int argc, char *const *argv) 282169695Skan{ 283169695Skan /* XXX This is no good solution. We should rather copy the args so 284169695Skan that we can compare them later. But we must not use malloc(3). */ 285169695Skan original_argc = argc; 286169695Skan original_argv = argv; 287169695Skan} 288169695Skan# ifdef text_set_element 289169695Skantext_set_element (__libc_subinit, store_args_and_env); 290169695Skan# endif /* text_set_element */ 291169695Skan 292169695Skan# define SWAP_FLAGS(ch1, ch2) \ 293169695Skan if (nonoption_flags_len > 0) \ 294169695Skan { \ 295169695Skan char __tmp = __getopt_nonoption_flags[ch1]; \ 296169695Skan __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 297169695Skan __getopt_nonoption_flags[ch2] = __tmp; \ 298169695Skan } 299169695Skan#else /* !_LIBC */ 300169695Skan# define SWAP_FLAGS(ch1, ch2) 301169695Skan#endif /* _LIBC */ 302169695Skan 303169695Skan/* Exchange two adjacent subsequences of ARGV. 304169695Skan One subsequence is elements [first_nonopt,last_nonopt) 305169695Skan which contains all the non-options that have been skipped so far. 306169695Skan The other is elements [last_nonopt,optind), which contains all 307169695Skan the options processed since those non-options were skipped. 308169695Skan 309169695Skan `first_nonopt' and `last_nonopt' are relocated so that they describe 310169695Skan the new indices of the non-options in ARGV after they are moved. */ 311169695Skan 312169695Skan#if defined __STDC__ && __STDC__ 313169695Skanstatic void exchange (char **); 314169695Skan#endif 315169695Skan 316169695Skanstatic void 317169695Skanexchange (char **argv) 318169695Skan{ 319169695Skan int bottom = first_nonopt; 320169695Skan int middle = last_nonopt; 321169695Skan int top = optind; 322169695Skan char *tem; 323169695Skan 324169695Skan /* Exchange the shorter segment with the far end of the longer segment. 325169695Skan That puts the shorter segment into the right place. 326169695Skan It leaves the longer segment in the right place overall, 327169695Skan but it consists of two parts that need to be swapped next. */ 328169695Skan 329169695Skan#ifdef _LIBC 330169695Skan /* First make sure the handling of the `__getopt_nonoption_flags' 331169695Skan string can work normally. Our top argument must be in the range 332169695Skan of the string. */ 333169695Skan if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 334169695Skan { 335169695Skan /* We must extend the array. The user plays games with us and 336169695Skan presents new arguments. */ 337169695Skan char *new_str = (char *) malloc (top + 1); 338169695Skan if (new_str == NULL) 339169695Skan nonoption_flags_len = nonoption_flags_max_len = 0; 340169695Skan else 341169695Skan { 342169695Skan memset (mempcpy (new_str, __getopt_nonoption_flags, 343169695Skan nonoption_flags_max_len), 344169695Skan '\0', top + 1 - nonoption_flags_max_len); 345169695Skan nonoption_flags_max_len = top + 1; 346169695Skan __getopt_nonoption_flags = new_str; 347169695Skan } 348169695Skan } 349169695Skan#endif 350169695Skan 351169695Skan while (top > middle && middle > bottom) 352169695Skan { 353169695Skan if (top - middle > middle - bottom) 354169695Skan { 355169695Skan /* Bottom segment is the short one. */ 356169695Skan int len = middle - bottom; 357169695Skan register int i; 358169695Skan 359169695Skan /* Swap it with the top part of the top segment. */ 360169695Skan for (i = 0; i < len; i++) 361169695Skan { 362169695Skan tem = argv[bottom + i]; 363169695Skan argv[bottom + i] = argv[top - (middle - bottom) + i]; 364169695Skan argv[top - (middle - bottom) + i] = tem; 365169695Skan SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); 366169695Skan } 367169695Skan /* Exclude the moved bottom segment from further swapping. */ 368169695Skan top -= len; 369169695Skan } 370169695Skan else 371169695Skan { 372169695Skan /* Top segment is the short one. */ 373169695Skan int len = top - middle; 374169695Skan register int i; 375169695Skan 376169695Skan /* Swap it with the bottom part of the bottom segment. */ 377169695Skan for (i = 0; i < len; i++) 378169695Skan { 379169695Skan tem = argv[bottom + i]; 380169695Skan argv[bottom + i] = argv[middle + i]; 381169695Skan argv[middle + i] = tem; 382169695Skan SWAP_FLAGS (bottom + i, middle + i); 383169695Skan } 384169695Skan /* Exclude the moved top segment from further swapping. */ 385169695Skan bottom += len; 386169695Skan } 387169695Skan } 388169695Skan 389169695Skan /* Update records for the slots the non-options now occupy. */ 390169695Skan 391169695Skan first_nonopt += (optind - last_nonopt); 392169695Skan last_nonopt = optind; 393169695Skan} 394169695Skan 395169695Skan/* Initialize the internal data when the first call is made. */ 396169695Skan 397169695Skan#if defined __STDC__ && __STDC__ 398169695Skanstatic const char *_getopt_initialize (int, char *const *, const char *); 399169695Skan#endif 400169695Skanstatic const char * 401169695Skan_getopt_initialize (int argc ATTRIBUTE_UNUSED, 402169695Skan char *const *argv ATTRIBUTE_UNUSED, 403169695Skan const char *optstring) 404169695Skan{ 405169695Skan /* Start processing options with ARGV-element 1 (since ARGV-element 0 406169695Skan is the program name); the sequence of previously skipped 407169695Skan non-option ARGV-elements is empty. */ 408169695Skan 409169695Skan first_nonopt = last_nonopt = optind; 410169695Skan 411169695Skan nextchar = NULL; 412169695Skan 413169695Skan posixly_correct = getenv ("POSIXLY_CORRECT"); 414169695Skan 415169695Skan /* Determine how to handle the ordering of options and nonoptions. */ 416169695Skan 417169695Skan if (optstring[0] == '-') 418169695Skan { 419169695Skan ordering = RETURN_IN_ORDER; 420169695Skan ++optstring; 421169695Skan } 422169695Skan else if (optstring[0] == '+') 423169695Skan { 424169695Skan ordering = REQUIRE_ORDER; 425169695Skan ++optstring; 426169695Skan } 427169695Skan else if (posixly_correct != NULL) 428169695Skan ordering = REQUIRE_ORDER; 429169695Skan else 430169695Skan ordering = PERMUTE; 431169695Skan 432169695Skan#ifdef _LIBC 433169695Skan if (posixly_correct == NULL 434169695Skan && argc == original_argc && argv == original_argv) 435169695Skan { 436169695Skan if (nonoption_flags_max_len == 0) 437169695Skan { 438169695Skan if (__getopt_nonoption_flags == NULL 439169695Skan || __getopt_nonoption_flags[0] == '\0') 440169695Skan nonoption_flags_max_len = -1; 441169695Skan else 442169695Skan { 443169695Skan const char *orig_str = __getopt_nonoption_flags; 444169695Skan int len = nonoption_flags_max_len = strlen (orig_str); 445169695Skan if (nonoption_flags_max_len < argc) 446169695Skan nonoption_flags_max_len = argc; 447169695Skan __getopt_nonoption_flags = 448169695Skan (char *) malloc (nonoption_flags_max_len); 449169695Skan if (__getopt_nonoption_flags == NULL) 450169695Skan nonoption_flags_max_len = -1; 451169695Skan else 452169695Skan memset (mempcpy (__getopt_nonoption_flags, orig_str, len), 453169695Skan '\0', nonoption_flags_max_len - len); 454169695Skan } 455169695Skan } 456169695Skan nonoption_flags_len = nonoption_flags_max_len; 457169695Skan } 458169695Skan else 459169695Skan nonoption_flags_len = 0; 460169695Skan#endif 461169695Skan 462169695Skan return optstring; 463169695Skan} 464169695Skan 465169695Skan/* Scan elements of ARGV (whose length is ARGC) for option characters 466169695Skan given in OPTSTRING. 467169695Skan 468169695Skan If an element of ARGV starts with '-', and is not exactly "-" or "--", 469169695Skan then it is an option element. The characters of this element 470169695Skan (aside from the initial '-') are option characters. If `getopt' 471169695Skan is called repeatedly, it returns successively each of the option characters 472169695Skan from each of the option elements. 473169695Skan 474169695Skan If `getopt' finds another option character, it returns that character, 475169695Skan updating `optind' and `nextchar' so that the next call to `getopt' can 476169695Skan resume the scan with the following option character or ARGV-element. 477169695Skan 478169695Skan If there are no more option characters, `getopt' returns -1. 479169695Skan Then `optind' is the index in ARGV of the first ARGV-element 480169695Skan that is not an option. (The ARGV-elements have been permuted 481169695Skan so that those that are not options now come last.) 482169695Skan 483169695Skan OPTSTRING is a string containing the legitimate option characters. 484169695Skan If an option character is seen that is not listed in OPTSTRING, 485169695Skan return '?' after printing an error message. If you set `opterr' to 486169695Skan zero, the error message is suppressed but we still return '?'. 487169695Skan 488169695Skan If a char in OPTSTRING is followed by a colon, that means it wants an arg, 489169695Skan so the following text in the same ARGV-element, or the text of the following 490169695Skan ARGV-element, is returned in `optarg'. Two colons mean an option that 491169695Skan wants an optional arg; if there is text in the current ARGV-element, 492169695Skan it is returned in `optarg', otherwise `optarg' is set to zero. 493169695Skan 494169695Skan If OPTSTRING starts with `-' or `+', it requests different methods of 495169695Skan handling the non-option ARGV-elements. 496169695Skan See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. 497169695Skan 498169695Skan Long-named options begin with `--' instead of `-'. 499169695Skan Their names may be abbreviated as long as the abbreviation is unique 500169695Skan or is an exact match for some defined option. If they have an 501169695Skan argument, it follows the option name in the same ARGV-element, separated 502169695Skan from the option name by a `=', or else the in next ARGV-element. 503169695Skan When `getopt' finds a long-named option, it returns 0 if that option's 504169695Skan `flag' field is nonzero, the value of the option's `val' field 505169695Skan if the `flag' field is zero. 506169695Skan 507169695Skan The elements of ARGV aren't really const, because we permute them. 508169695Skan But we pretend they're const in the prototype to be compatible 509169695Skan with other systems. 510169695Skan 511169695Skan LONGOPTS is a vector of `struct option' terminated by an 512169695Skan element containing a name which is zero. 513169695Skan 514169695Skan LONGIND returns the index in LONGOPT of the long-named option found. 515169695Skan It is only valid when a long-named option has been found by the most 516169695Skan recent call. 517169695Skan 518169695Skan If LONG_ONLY is nonzero, '-' as well as '--' can introduce 519169695Skan long-named options. */ 520169695Skan 521169695Skanint 522169695Skan_getopt_internal (int argc, char *const *argv, const char *optstring, 523169695Skan const struct option *longopts, 524169695Skan int *longind, int long_only) 525169695Skan{ 526169695Skan optarg = NULL; 527169695Skan 528169695Skan if (optind == 0 || !__getopt_initialized) 529169695Skan { 530169695Skan if (optind == 0) 531169695Skan optind = 1; /* Don't scan ARGV[0], the program name. */ 532169695Skan optstring = _getopt_initialize (argc, argv, optstring); 533169695Skan __getopt_initialized = 1; 534169695Skan } 535169695Skan 536169695Skan /* Test whether ARGV[optind] points to a non-option argument. 537169695Skan Either it does not have option syntax, or there is an environment flag 538169695Skan from the shell indicating it is not an option. The later information 539169695Skan is only used when the used in the GNU libc. */ 540169695Skan#ifdef _LIBC 541169695Skan# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 542169695Skan || (optind < nonoption_flags_len \ 543169695Skan && __getopt_nonoption_flags[optind] == '1')) 544169695Skan#else 545169695Skan# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 546169695Skan#endif 547169695Skan 548169695Skan if (nextchar == NULL || *nextchar == '\0') 549169695Skan { 550169695Skan /* Advance to the next ARGV-element. */ 551169695Skan 552169695Skan /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 553169695Skan moved back by the user (who may also have changed the arguments). */ 554169695Skan if (last_nonopt > optind) 555169695Skan last_nonopt = optind; 556169695Skan if (first_nonopt > optind) 557169695Skan first_nonopt = optind; 558169695Skan 559169695Skan if (ordering == PERMUTE) 560169695Skan { 561169695Skan /* If we have just processed some options following some non-options, 562169695Skan exchange them so that the options come first. */ 563169695Skan 564169695Skan if (first_nonopt != last_nonopt && last_nonopt != optind) 565169695Skan exchange ((char **) argv); 566169695Skan else if (last_nonopt != optind) 567169695Skan first_nonopt = optind; 568169695Skan 569169695Skan /* Skip any additional non-options 570169695Skan and extend the range of non-options previously skipped. */ 571169695Skan 572169695Skan while (optind < argc && NONOPTION_P) 573169695Skan optind++; 574169695Skan last_nonopt = optind; 575169695Skan } 576169695Skan 577169695Skan /* The special ARGV-element `--' means premature end of options. 578169695Skan Skip it like a null option, 579169695Skan then exchange with previous non-options as if it were an option, 580169695Skan then skip everything else like a non-option. */ 581169695Skan 582169695Skan if (optind != argc && !strcmp (argv[optind], "--")) 583169695Skan { 584169695Skan optind++; 585169695Skan 586169695Skan if (first_nonopt != last_nonopt && last_nonopt != optind) 587169695Skan exchange ((char **) argv); 588169695Skan else if (first_nonopt == last_nonopt) 589169695Skan first_nonopt = optind; 590169695Skan last_nonopt = argc; 591169695Skan 592169695Skan optind = argc; 593169695Skan } 594169695Skan 595169695Skan /* If we have done all the ARGV-elements, stop the scan 596169695Skan and back over any non-options that we skipped and permuted. */ 597169695Skan 598169695Skan if (optind == argc) 599169695Skan { 600169695Skan /* Set the next-arg-index to point at the non-options 601169695Skan that we previously skipped, so the caller will digest them. */ 602169695Skan if (first_nonopt != last_nonopt) 603169695Skan optind = first_nonopt; 604169695Skan return -1; 605169695Skan } 606169695Skan 607169695Skan /* If we have come to a non-option and did not permute it, 608169695Skan either stop the scan or describe it to the caller and pass it by. */ 609169695Skan 610169695Skan if (NONOPTION_P) 611169695Skan { 612169695Skan if (ordering == REQUIRE_ORDER) 613169695Skan return -1; 614169695Skan optarg = argv[optind++]; 615169695Skan return 1; 616169695Skan } 617169695Skan 618169695Skan /* We have found another option-ARGV-element. 619169695Skan Skip the initial punctuation. */ 620169695Skan 621169695Skan nextchar = (argv[optind] + 1 622169695Skan + (longopts != NULL && argv[optind][1] == '-')); 623169695Skan } 624169695Skan 625169695Skan /* Decode the current option-ARGV-element. */ 626169695Skan 627169695Skan /* Check whether the ARGV-element is a long option. 628169695Skan 629169695Skan If long_only and the ARGV-element has the form "-f", where f is 630169695Skan a valid short option, don't consider it an abbreviated form of 631169695Skan a long option that starts with f. Otherwise there would be no 632169695Skan way to give the -f short option. 633169695Skan 634169695Skan On the other hand, if there's a long option "fubar" and 635169695Skan the ARGV-element is "-fu", do consider that an abbreviation of 636169695Skan the long option, just like "--fu", and not "-f" with arg "u". 637169695Skan 638169695Skan This distinction seems to be the most useful approach. */ 639169695Skan 640169695Skan if (longopts != NULL 641169695Skan && (argv[optind][1] == '-' 642169695Skan || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) 643169695Skan { 644169695Skan char *nameend; 645169695Skan const struct option *p; 646169695Skan const struct option *pfound = NULL; 647169695Skan int exact = 0; 648169695Skan int ambig = 0; 649169695Skan int indfound = -1; 650169695Skan int option_index; 651169695Skan 652169695Skan for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 653169695Skan /* Do nothing. */ ; 654169695Skan 655169695Skan /* Test all long options for either exact match 656169695Skan or abbreviated matches. */ 657169695Skan for (p = longopts, option_index = 0; p->name; p++, option_index++) 658169695Skan if (!strncmp (p->name, nextchar, nameend - nextchar)) 659169695Skan { 660169695Skan if ((unsigned int) (nameend - nextchar) 661169695Skan == (unsigned int) strlen (p->name)) 662169695Skan { 663169695Skan /* Exact match found. */ 664169695Skan pfound = p; 665169695Skan indfound = option_index; 666169695Skan exact = 1; 667169695Skan break; 668169695Skan } 669169695Skan else if (pfound == NULL) 670169695Skan { 671169695Skan /* First nonexact match found. */ 672169695Skan pfound = p; 673169695Skan indfound = option_index; 674169695Skan } 675169695Skan else 676169695Skan /* Second or later nonexact match found. */ 677169695Skan ambig = 1; 678169695Skan } 679169695Skan 680169695Skan if (ambig && !exact) 681169695Skan { 682169695Skan if (opterr) 683169695Skan fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 684169695Skan argv[0], argv[optind]); 685169695Skan nextchar += strlen (nextchar); 686169695Skan optind++; 687169695Skan optopt = 0; 688169695Skan return '?'; 689169695Skan } 690169695Skan 691169695Skan if (pfound != NULL) 692169695Skan { 693169695Skan option_index = indfound; 694169695Skan optind++; 695169695Skan if (*nameend) 696169695Skan { 697169695Skan /* Don't test has_arg with >, because some C compilers don't 698169695Skan allow it to be used on enums. */ 699169695Skan if (pfound->has_arg) 700169695Skan optarg = nameend + 1; 701169695Skan else 702169695Skan { 703169695Skan if (opterr) 704169695Skan { 705169695Skan if (argv[optind - 1][1] == '-') 706169695Skan /* --option */ 707169695Skan fprintf (stderr, 708169695Skan _("%s: option `--%s' doesn't allow an argument\n"), 709169695Skan argv[0], pfound->name); 710169695Skan else 711169695Skan /* +option or -option */ 712169695Skan fprintf (stderr, 713169695Skan _("%s: option `%c%s' doesn't allow an argument\n"), 714169695Skan argv[0], argv[optind - 1][0], pfound->name); 715169695Skan 716169695Skan nextchar += strlen (nextchar); 717169695Skan 718169695Skan optopt = pfound->val; 719169695Skan return '?'; 720169695Skan } 721169695Skan } 722169695Skan } 723169695Skan else if (pfound->has_arg == 1) 724169695Skan { 725169695Skan if (optind < argc) 726169695Skan optarg = argv[optind++]; 727169695Skan else 728169695Skan { 729169695Skan if (opterr) 730169695Skan fprintf (stderr, 731169695Skan _("%s: option `%s' requires an argument\n"), 732169695Skan argv[0], argv[optind - 1]); 733169695Skan nextchar += strlen (nextchar); 734169695Skan optopt = pfound->val; 735169695Skan return optstring[0] == ':' ? ':' : '?'; 736169695Skan } 737169695Skan } 738169695Skan nextchar += strlen (nextchar); 739169695Skan if (longind != NULL) 740169695Skan *longind = option_index; 741169695Skan if (pfound->flag) 742169695Skan { 743169695Skan *(pfound->flag) = pfound->val; 744169695Skan return 0; 745169695Skan } 746169695Skan return pfound->val; 747169695Skan } 748169695Skan 749169695Skan /* Can't find it as a long option. If this is not getopt_long_only, 750169695Skan or the option starts with '--' or is not a valid short 751169695Skan option, then it's an error. 752169695Skan Otherwise interpret it as a short option. */ 753169695Skan if (!long_only || argv[optind][1] == '-' 754169695Skan || my_index (optstring, *nextchar) == NULL) 755169695Skan { 756169695Skan if (opterr) 757169695Skan { 758169695Skan if (argv[optind][1] == '-') 759169695Skan /* --option */ 760169695Skan fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 761169695Skan argv[0], nextchar); 762169695Skan else 763169695Skan /* +option or -option */ 764169695Skan fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 765169695Skan argv[0], argv[optind][0], nextchar); 766169695Skan } 767169695Skan nextchar = (char *) ""; 768169695Skan optind++; 769169695Skan optopt = 0; 770169695Skan return '?'; 771169695Skan } 772169695Skan } 773169695Skan 774169695Skan /* Look at and handle the next short option-character. */ 775169695Skan 776169695Skan { 777169695Skan char c = *nextchar++; 778169695Skan char *temp = my_index (optstring, c); 779169695Skan 780169695Skan /* Increment `optind' when we start to process its last character. */ 781169695Skan if (*nextchar == '\0') 782169695Skan ++optind; 783169695Skan 784169695Skan if (temp == NULL || c == ':') 785169695Skan { 786169695Skan if (opterr) 787169695Skan { 788169695Skan if (posixly_correct) 789169695Skan /* 1003.2 specifies the format of this message. */ 790169695Skan fprintf (stderr, _("%s: illegal option -- %c\n"), 791169695Skan argv[0], c); 792169695Skan else 793169695Skan fprintf (stderr, _("%s: invalid option -- %c\n"), 794169695Skan argv[0], c); 795169695Skan } 796169695Skan optopt = c; 797169695Skan return '?'; 798169695Skan } 799169695Skan /* Convenience. Treat POSIX -W foo same as long option --foo */ 800169695Skan if (temp[0] == 'W' && temp[1] == ';') 801169695Skan { 802169695Skan char *nameend; 803169695Skan const struct option *p; 804169695Skan const struct option *pfound = NULL; 805169695Skan int exact = 0; 806169695Skan int ambig = 0; 807169695Skan int indfound = 0; 808169695Skan int option_index; 809169695Skan 810169695Skan /* This is an option that requires an argument. */ 811169695Skan if (*nextchar != '\0') 812169695Skan { 813169695Skan optarg = nextchar; 814169695Skan /* If we end this ARGV-element by taking the rest as an arg, 815169695Skan we must advance to the next element now. */ 816169695Skan optind++; 817169695Skan } 818169695Skan else if (optind == argc) 819169695Skan { 820169695Skan if (opterr) 821169695Skan { 822169695Skan /* 1003.2 specifies the format of this message. */ 823169695Skan fprintf (stderr, _("%s: option requires an argument -- %c\n"), 824169695Skan argv[0], c); 825169695Skan } 826169695Skan optopt = c; 827169695Skan if (optstring[0] == ':') 828169695Skan c = ':'; 829169695Skan else 830169695Skan c = '?'; 831169695Skan return c; 832169695Skan } 833169695Skan else 834169695Skan /* We already incremented `optind' once; 835169695Skan increment it again when taking next ARGV-elt as argument. */ 836169695Skan optarg = argv[optind++]; 837169695Skan 838169695Skan /* optarg is now the argument, see if it's in the 839169695Skan table of longopts. */ 840169695Skan 841169695Skan for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) 842169695Skan /* Do nothing. */ ; 843169695Skan 844169695Skan /* Test all long options for either exact match 845169695Skan or abbreviated matches. */ 846169695Skan for (p = longopts, option_index = 0; p->name; p++, option_index++) 847169695Skan if (!strncmp (p->name, nextchar, nameend - nextchar)) 848169695Skan { 849169695Skan if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 850169695Skan { 851169695Skan /* Exact match found. */ 852169695Skan pfound = p; 853169695Skan indfound = option_index; 854169695Skan exact = 1; 855169695Skan break; 856169695Skan } 857169695Skan else if (pfound == NULL) 858169695Skan { 859169695Skan /* First nonexact match found. */ 860169695Skan pfound = p; 861169695Skan indfound = option_index; 862169695Skan } 863169695Skan else 864169695Skan /* Second or later nonexact match found. */ 865169695Skan ambig = 1; 866169695Skan } 867169695Skan if (ambig && !exact) 868169695Skan { 869169695Skan if (opterr) 870169695Skan fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 871169695Skan argv[0], argv[optind]); 872169695Skan nextchar += strlen (nextchar); 873169695Skan optind++; 874169695Skan return '?'; 875169695Skan } 876169695Skan if (pfound != NULL) 877169695Skan { 878169695Skan option_index = indfound; 879169695Skan if (*nameend) 880169695Skan { 881169695Skan /* Don't test has_arg with >, because some C compilers don't 882169695Skan allow it to be used on enums. */ 883169695Skan if (pfound->has_arg) 884169695Skan optarg = nameend + 1; 885169695Skan else 886169695Skan { 887169695Skan if (opterr) 888169695Skan fprintf (stderr, _("\ 889169695Skan%s: option `-W %s' doesn't allow an argument\n"), 890169695Skan argv[0], pfound->name); 891169695Skan 892169695Skan nextchar += strlen (nextchar); 893169695Skan return '?'; 894169695Skan } 895169695Skan } 896169695Skan else if (pfound->has_arg == 1) 897169695Skan { 898169695Skan if (optind < argc) 899169695Skan optarg = argv[optind++]; 900169695Skan else 901169695Skan { 902169695Skan if (opterr) 903169695Skan fprintf (stderr, 904169695Skan _("%s: option `%s' requires an argument\n"), 905169695Skan argv[0], argv[optind - 1]); 906169695Skan nextchar += strlen (nextchar); 907169695Skan return optstring[0] == ':' ? ':' : '?'; 908169695Skan } 909169695Skan } 910169695Skan nextchar += strlen (nextchar); 911169695Skan if (longind != NULL) 912169695Skan *longind = option_index; 913169695Skan if (pfound->flag) 914169695Skan { 915169695Skan *(pfound->flag) = pfound->val; 916169695Skan return 0; 917169695Skan } 918169695Skan return pfound->val; 919169695Skan } 920169695Skan nextchar = NULL; 921169695Skan return 'W'; /* Let the application handle it. */ 922169695Skan } 923169695Skan if (temp[1] == ':') 924169695Skan { 925169695Skan if (temp[2] == ':') 926169695Skan { 927169695Skan /* This is an option that accepts an argument optionally. */ 928169695Skan if (*nextchar != '\0') 929169695Skan { 930169695Skan optarg = nextchar; 931169695Skan optind++; 932169695Skan } 933169695Skan else 934169695Skan optarg = NULL; 935169695Skan nextchar = NULL; 936169695Skan } 937169695Skan else 938169695Skan { 939169695Skan /* This is an option that requires an argument. */ 940169695Skan if (*nextchar != '\0') 941169695Skan { 942169695Skan optarg = nextchar; 943169695Skan /* If we end this ARGV-element by taking the rest as an arg, 944169695Skan we must advance to the next element now. */ 945169695Skan optind++; 946169695Skan } 947169695Skan else if (optind == argc) 948169695Skan { 949169695Skan if (opterr) 950169695Skan { 951169695Skan /* 1003.2 specifies the format of this message. */ 952169695Skan fprintf (stderr, 953169695Skan _("%s: option requires an argument -- %c\n"), 954169695Skan argv[0], c); 955169695Skan } 956169695Skan optopt = c; 957169695Skan if (optstring[0] == ':') 958169695Skan c = ':'; 959169695Skan else 960169695Skan c = '?'; 961169695Skan } 962169695Skan else 963169695Skan /* We already incremented `optind' once; 964169695Skan increment it again when taking next ARGV-elt as argument. */ 965169695Skan optarg = argv[optind++]; 966169695Skan nextchar = NULL; 967169695Skan } 968169695Skan } 969169695Skan return c; 970169695Skan } 971169695Skan} 972169695Skan 973169695Skanint 974169695Skangetopt (int argc, char *const *argv, const char *optstring) 975169695Skan{ 976169695Skan return _getopt_internal (argc, argv, optstring, 977169695Skan (const struct option *) 0, 978169695Skan (int *) 0, 979169695Skan 0); 980169695Skan} 981169695Skan 982169695Skan#endif /* Not ELIDE_CODE. */ 983169695Skan 984169695Skan#ifdef TEST 985169695Skan 986169695Skan/* Compile with -DTEST to make an executable for use in testing 987169695Skan the above definition of `getopt'. */ 988169695Skan 989169695Skanint 990169695Skanmain (int argc, char **argv) 991169695Skan{ 992169695Skan int c; 993169695Skan int digit_optind = 0; 994169695Skan 995169695Skan while (1) 996169695Skan { 997169695Skan int this_option_optind = optind ? optind : 1; 998169695Skan 999169695Skan c = getopt (argc, argv, "abc:d:0123456789"); 1000169695Skan if (c == -1) 1001169695Skan break; 1002169695Skan 1003169695Skan switch (c) 1004169695Skan { 1005169695Skan case '0': 1006169695Skan case '1': 1007169695Skan case '2': 1008169695Skan case '3': 1009169695Skan case '4': 1010169695Skan case '5': 1011169695Skan case '6': 1012169695Skan case '7': 1013169695Skan case '8': 1014169695Skan case '9': 1015169695Skan if (digit_optind != 0 && digit_optind != this_option_optind) 1016169695Skan printf ("digits occur in two different argv-elements.\n"); 1017169695Skan digit_optind = this_option_optind; 1018169695Skan printf ("option %c\n", c); 1019169695Skan break; 1020169695Skan 1021169695Skan case 'a': 1022169695Skan printf ("option a\n"); 1023169695Skan break; 1024169695Skan 1025169695Skan case 'b': 1026169695Skan printf ("option b\n"); 1027169695Skan break; 1028169695Skan 1029169695Skan case 'c': 1030169695Skan printf ("option c with value `%s'\n", optarg); 1031169695Skan break; 1032169695Skan 1033169695Skan case '?': 1034169695Skan break; 1035169695Skan 1036169695Skan default: 1037169695Skan printf ("?? getopt returned character code 0%o ??\n", c); 1038169695Skan } 1039169695Skan } 1040169695Skan 1041169695Skan if (optind < argc) 1042169695Skan { 1043169695Skan printf ("non-option ARGV-elements: "); 1044169695Skan while (optind < argc) 1045169695Skan printf ("%s ", argv[optind++]); 1046169695Skan printf ("\n"); 1047169695Skan } 1048169695Skan 1049169695Skan exit (0); 1050169695Skan} 1051169695Skan 1052169695Skan#endif /* TEST */ 1053