1*1b8adde7SWilliam Kucharski /* Declarations for getopt. 2*1b8adde7SWilliam Kucharski Copyright (C) 1989,90,91,92,93,94,96,97 Free Software Foundation, Inc. 3*1b8adde7SWilliam Kucharski 4*1b8adde7SWilliam Kucharski NOTE: The canonical source of this file is maintained with the GNU C Library. 5*1b8adde7SWilliam Kucharski Bugs can be reported to bug-glibc@gnu.org. 6*1b8adde7SWilliam Kucharski 7*1b8adde7SWilliam Kucharski This program is free software; you can redistribute it and/or modify it 8*1b8adde7SWilliam Kucharski under the terms of the GNU General Public License as published by the 9*1b8adde7SWilliam Kucharski Free Software Foundation; either version 2, or (at your option) any 10*1b8adde7SWilliam Kucharski later version. 11*1b8adde7SWilliam Kucharski 12*1b8adde7SWilliam Kucharski This program is distributed in the hope that it will be useful, 13*1b8adde7SWilliam Kucharski but WITHOUT ANY WARRANTY; without even the implied warranty of 14*1b8adde7SWilliam Kucharski MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*1b8adde7SWilliam Kucharski GNU General Public License for more details. 16*1b8adde7SWilliam Kucharski 17*1b8adde7SWilliam Kucharski You should have received a copy of the GNU General Public License 18*1b8adde7SWilliam Kucharski along with this program; if not, write to the Free Software 19*1b8adde7SWilliam Kucharski Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 20*1b8adde7SWilliam Kucharski USA. */ 21*1b8adde7SWilliam Kucharski 22*1b8adde7SWilliam Kucharski #ifndef _GETOPT_H 23*1b8adde7SWilliam Kucharski #define _GETOPT_H 1 24*1b8adde7SWilliam Kucharski 25*1b8adde7SWilliam Kucharski #ifdef __cplusplus 26*1b8adde7SWilliam Kucharski extern "C" { 27*1b8adde7SWilliam Kucharski #endif 28*1b8adde7SWilliam Kucharski 29*1b8adde7SWilliam Kucharski /* For communication from `getopt' to the caller. 30*1b8adde7SWilliam Kucharski When `getopt' finds an option that takes an argument, 31*1b8adde7SWilliam Kucharski the argument value is returned here. 32*1b8adde7SWilliam Kucharski Also, when `ordering' is RETURN_IN_ORDER, 33*1b8adde7SWilliam Kucharski each non-option ARGV-element is returned here. */ 34*1b8adde7SWilliam Kucharski 35*1b8adde7SWilliam Kucharski extern char *optarg; 36*1b8adde7SWilliam Kucharski 37*1b8adde7SWilliam Kucharski /* Index in ARGV of the next element to be scanned. 38*1b8adde7SWilliam Kucharski This is used for communication to and from the caller 39*1b8adde7SWilliam Kucharski and for communication between successive calls to `getopt'. 40*1b8adde7SWilliam Kucharski 41*1b8adde7SWilliam Kucharski On entry to `getopt', zero means this is the first call; initialize. 42*1b8adde7SWilliam Kucharski 43*1b8adde7SWilliam Kucharski When `getopt' returns -1, this is the index of the first of the 44*1b8adde7SWilliam Kucharski non-option elements that the caller should itself scan. 45*1b8adde7SWilliam Kucharski 46*1b8adde7SWilliam Kucharski Otherwise, `optind' communicates from one call to the next 47*1b8adde7SWilliam Kucharski how much of ARGV has been scanned so far. */ 48*1b8adde7SWilliam Kucharski 49*1b8adde7SWilliam Kucharski extern int optind; 50*1b8adde7SWilliam Kucharski 51*1b8adde7SWilliam Kucharski /* Callers store zero here to inhibit the error message `getopt' prints 52*1b8adde7SWilliam Kucharski for unrecognized options. */ 53*1b8adde7SWilliam Kucharski 54*1b8adde7SWilliam Kucharski extern int opterr; 55*1b8adde7SWilliam Kucharski 56*1b8adde7SWilliam Kucharski /* Set to an option character which was unrecognized. */ 57*1b8adde7SWilliam Kucharski 58*1b8adde7SWilliam Kucharski extern int optopt; 59*1b8adde7SWilliam Kucharski 60*1b8adde7SWilliam Kucharski /* Describe the long-named options requested by the application. 61*1b8adde7SWilliam Kucharski The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector 62*1b8adde7SWilliam Kucharski of `struct option' terminated by an element containing a name which is 63*1b8adde7SWilliam Kucharski zero. 64*1b8adde7SWilliam Kucharski 65*1b8adde7SWilliam Kucharski The field `has_arg' is: 66*1b8adde7SWilliam Kucharski no_argument (or 0) if the option does not take an argument, 67*1b8adde7SWilliam Kucharski required_argument (or 1) if the option requires an argument, 68*1b8adde7SWilliam Kucharski optional_argument (or 2) if the option takes an optional argument. 69*1b8adde7SWilliam Kucharski 70*1b8adde7SWilliam Kucharski If the field `flag' is not NULL, it points to a variable that is set 71*1b8adde7SWilliam Kucharski to the value given in the field `val' when the option is found, but 72*1b8adde7SWilliam Kucharski left unchanged if the option is not found. 73*1b8adde7SWilliam Kucharski 74*1b8adde7SWilliam Kucharski To have a long-named option do something other than set an `int' to 75*1b8adde7SWilliam Kucharski a compiled-in constant, such as set a value from `optarg', set the 76*1b8adde7SWilliam Kucharski option's `flag' field to zero and its `val' field to a nonzero 77*1b8adde7SWilliam Kucharski value (the equivalent single-letter option character, if there is 78*1b8adde7SWilliam Kucharski one). For long options that have a zero `flag' field, `getopt' 79*1b8adde7SWilliam Kucharski returns the contents of the `val' field. */ 80*1b8adde7SWilliam Kucharski 81*1b8adde7SWilliam Kucharski struct option 82*1b8adde7SWilliam Kucharski { 83*1b8adde7SWilliam Kucharski #if defined (__STDC__) && __STDC__ 84*1b8adde7SWilliam Kucharski const char *name; 85*1b8adde7SWilliam Kucharski #else 86*1b8adde7SWilliam Kucharski char *name; 87*1b8adde7SWilliam Kucharski #endif 88*1b8adde7SWilliam Kucharski /* has_arg can't be an enum because some compilers complain about 89*1b8adde7SWilliam Kucharski type mismatches in all the code that assumes it is an int. */ 90*1b8adde7SWilliam Kucharski int has_arg; 91*1b8adde7SWilliam Kucharski int *flag; 92*1b8adde7SWilliam Kucharski int val; 93*1b8adde7SWilliam Kucharski }; 94*1b8adde7SWilliam Kucharski 95*1b8adde7SWilliam Kucharski /* Names for the values of the `has_arg' field of `struct option'. */ 96*1b8adde7SWilliam Kucharski 97*1b8adde7SWilliam Kucharski #define no_argument 0 98*1b8adde7SWilliam Kucharski #define required_argument 1 99*1b8adde7SWilliam Kucharski #define optional_argument 2 100*1b8adde7SWilliam Kucharski 101*1b8adde7SWilliam Kucharski #if defined (__STDC__) && __STDC__ 102*1b8adde7SWilliam Kucharski #ifdef __GNU_LIBRARY__ 103*1b8adde7SWilliam Kucharski /* Many other libraries have conflicting prototypes for getopt, with 104*1b8adde7SWilliam Kucharski differences in the consts, in stdlib.h. To avoid compilation 105*1b8adde7SWilliam Kucharski errors, only prototype getopt for the GNU C library. */ 106*1b8adde7SWilliam Kucharski extern int getopt (int argc, char *const *argv, const char *shortopts); 107*1b8adde7SWilliam Kucharski #else /* not __GNU_LIBRARY__ */ 108*1b8adde7SWilliam Kucharski extern int getopt (); 109*1b8adde7SWilliam Kucharski #endif /* __GNU_LIBRARY__ */ 110*1b8adde7SWilliam Kucharski extern int getopt_long (int argc, char *const *argv, const char *shortopts, 111*1b8adde7SWilliam Kucharski const struct option *longopts, int *longind); 112*1b8adde7SWilliam Kucharski extern int getopt_long_only (int argc, char *const *argv, 113*1b8adde7SWilliam Kucharski const char *shortopts, 114*1b8adde7SWilliam Kucharski const struct option *longopts, int *longind); 115*1b8adde7SWilliam Kucharski 116*1b8adde7SWilliam Kucharski /* Internal only. Users should not call this directly. */ 117*1b8adde7SWilliam Kucharski extern int _getopt_internal (int argc, char *const *argv, 118*1b8adde7SWilliam Kucharski const char *shortopts, 119*1b8adde7SWilliam Kucharski const struct option *longopts, int *longind, 120*1b8adde7SWilliam Kucharski int long_only); 121*1b8adde7SWilliam Kucharski #else /* not __STDC__ */ 122*1b8adde7SWilliam Kucharski extern int getopt (); 123*1b8adde7SWilliam Kucharski extern int getopt_long (); 124*1b8adde7SWilliam Kucharski extern int getopt_long_only (); 125*1b8adde7SWilliam Kucharski 126*1b8adde7SWilliam Kucharski extern int _getopt_internal (); 127*1b8adde7SWilliam Kucharski #endif /* __STDC__ */ 128*1b8adde7SWilliam Kucharski 129*1b8adde7SWilliam Kucharski #ifdef __cplusplus 130*1b8adde7SWilliam Kucharski } 131*1b8adde7SWilliam Kucharski #endif 132*1b8adde7SWilliam Kucharski 133*1b8adde7SWilliam Kucharski #endif /* getopt.h */ 134