1*29619d2aSchristos /* $NetBSD: getopt_.h,v 1.1.1.1 2016/01/14 00:11:29 christos Exp $ */ 2*29619d2aSchristos 3*29619d2aSchristos /* Declarations for getopt. 4*29619d2aSchristos Copyright (C) 1989-1994,1996-1999,2001,2003,2004 5*29619d2aSchristos Free Software Foundation, Inc. 6*29619d2aSchristos This file is part of the GNU C Library. 7*29619d2aSchristos 8*29619d2aSchristos This program is free software; you can redistribute it and/or modify 9*29619d2aSchristos it under the terms of the GNU General Public License as published by 10*29619d2aSchristos the Free Software Foundation; either version 2, or (at your option) 11*29619d2aSchristos any later version. 12*29619d2aSchristos 13*29619d2aSchristos This program is distributed in the hope that it will be useful, 14*29619d2aSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 15*29619d2aSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*29619d2aSchristos GNU General Public License for more details. 17*29619d2aSchristos 18*29619d2aSchristos You should have received a copy of the GNU General Public License along 19*29619d2aSchristos with this program; if not, write to the Free Software Foundation, 20*29619d2aSchristos Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 21*29619d2aSchristos 22*29619d2aSchristos #ifndef _GETOPT_H 23*29619d2aSchristos 24*29619d2aSchristos #ifndef __need_getopt 25*29619d2aSchristos # define _GETOPT_H 1 26*29619d2aSchristos #endif 27*29619d2aSchristos 28*29619d2aSchristos /* If __GNU_LIBRARY__ is not already defined, either we are being used 29*29619d2aSchristos standalone, or this is the first header included in the source file. 30*29619d2aSchristos If we are being used with glibc, we need to include <features.h>, but 31*29619d2aSchristos that does not exist if we are standalone. So: if __GNU_LIBRARY__ is 32*29619d2aSchristos not defined, include <ctype.h>, which will pull in <features.h> for us 33*29619d2aSchristos if it's from glibc. (Why ctype.h? It's guaranteed to exist and it 34*29619d2aSchristos doesn't flood the namespace with stuff the way some other headers do.) */ 35*29619d2aSchristos #if !defined __GNU_LIBRARY__ 36*29619d2aSchristos # include <ctype.h> 37*29619d2aSchristos #endif 38*29619d2aSchristos 39*29619d2aSchristos #ifndef __THROW 40*29619d2aSchristos # ifndef __GNUC_PREREQ 41*29619d2aSchristos # define __GNUC_PREREQ(maj, min) (0) 42*29619d2aSchristos # endif 43*29619d2aSchristos # if defined __cplusplus && __GNUC_PREREQ (2,8) 44*29619d2aSchristos # define __THROW throw () 45*29619d2aSchristos # else 46*29619d2aSchristos # define __THROW 47*29619d2aSchristos # endif 48*29619d2aSchristos #endif 49*29619d2aSchristos 50*29619d2aSchristos #ifdef __cplusplus 51*29619d2aSchristos extern "C" { 52*29619d2aSchristos #endif 53*29619d2aSchristos 54*29619d2aSchristos /* For communication from `getopt' to the caller. 55*29619d2aSchristos When `getopt' finds an option that takes an argument, 56*29619d2aSchristos the argument value is returned here. 57*29619d2aSchristos Also, when `ordering' is RETURN_IN_ORDER, 58*29619d2aSchristos each non-option ARGV-element is returned here. */ 59*29619d2aSchristos 60*29619d2aSchristos extern char *optarg; 61*29619d2aSchristos 62*29619d2aSchristos /* Index in ARGV of the next element to be scanned. 63*29619d2aSchristos This is used for communication to and from the caller 64*29619d2aSchristos and for communication between successive calls to `getopt'. 65*29619d2aSchristos 66*29619d2aSchristos On entry to `getopt', zero means this is the first call; initialize. 67*29619d2aSchristos 68*29619d2aSchristos When `getopt' returns -1, this is the index of the first of the 69*29619d2aSchristos non-option elements that the caller should itself scan. 70*29619d2aSchristos 71*29619d2aSchristos Otherwise, `optind' communicates from one call to the next 72*29619d2aSchristos how much of ARGV has been scanned so far. */ 73*29619d2aSchristos 74*29619d2aSchristos extern int optind; 75*29619d2aSchristos 76*29619d2aSchristos /* Callers store zero here to inhibit the error message `getopt' prints 77*29619d2aSchristos for unrecognized options. */ 78*29619d2aSchristos 79*29619d2aSchristos extern int opterr; 80*29619d2aSchristos 81*29619d2aSchristos /* Set to an option character which was unrecognized. */ 82*29619d2aSchristos 83*29619d2aSchristos extern int optopt; 84*29619d2aSchristos 85*29619d2aSchristos #ifndef __need_getopt 86*29619d2aSchristos /* Describe the long-named options requested by the application. 87*29619d2aSchristos The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector 88*29619d2aSchristos of `struct option' terminated by an element containing a name which is 89*29619d2aSchristos zero. 90*29619d2aSchristos 91*29619d2aSchristos The field `has_arg' is: 92*29619d2aSchristos no_argument (or 0) if the option does not take an argument, 93*29619d2aSchristos required_argument (or 1) if the option requires an argument, 94*29619d2aSchristos optional_argument (or 2) if the option takes an optional argument. 95*29619d2aSchristos 96*29619d2aSchristos If the field `flag' is not NULL, it points to a variable that is set 97*29619d2aSchristos to the value given in the field `val' when the option is found, but 98*29619d2aSchristos left unchanged if the option is not found. 99*29619d2aSchristos 100*29619d2aSchristos To have a long-named option do something other than set an `int' to 101*29619d2aSchristos a compiled-in constant, such as set a value from `optarg', set the 102*29619d2aSchristos option's `flag' field to zero and its `val' field to a nonzero 103*29619d2aSchristos value (the equivalent single-letter option character, if there is 104*29619d2aSchristos one). For long options that have a zero `flag' field, `getopt' 105*29619d2aSchristos returns the contents of the `val' field. */ 106*29619d2aSchristos 107*29619d2aSchristos struct option 108*29619d2aSchristos { 109*29619d2aSchristos const char *name; 110*29619d2aSchristos /* has_arg can't be an enum because some compilers complain about 111*29619d2aSchristos type mismatches in all the code that assumes it is an int. */ 112*29619d2aSchristos int has_arg; 113*29619d2aSchristos int *flag; 114*29619d2aSchristos int val; 115*29619d2aSchristos }; 116*29619d2aSchristos 117*29619d2aSchristos /* Names for the values of the `has_arg' field of `struct option'. */ 118*29619d2aSchristos 119*29619d2aSchristos # define no_argument 0 120*29619d2aSchristos # define required_argument 1 121*29619d2aSchristos # define optional_argument 2 122*29619d2aSchristos #endif /* need getopt */ 123*29619d2aSchristos 124*29619d2aSchristos 125*29619d2aSchristos /* Get definitions and prototypes for functions to process the 126*29619d2aSchristos arguments in ARGV (ARGC of them, minus the program name) for 127*29619d2aSchristos options given in OPTS. 128*29619d2aSchristos 129*29619d2aSchristos Return the option character from OPTS just read. Return -1 when 130*29619d2aSchristos there are no more options. For unrecognized options, or options 131*29619d2aSchristos missing arguments, `optopt' is set to the option letter, and '?' is 132*29619d2aSchristos returned. 133*29619d2aSchristos 134*29619d2aSchristos The OPTS string is a list of characters which are recognized option 135*29619d2aSchristos letters, optionally followed by colons, specifying that that letter 136*29619d2aSchristos takes an argument, to be placed in `optarg'. 137*29619d2aSchristos 138*29619d2aSchristos If a letter in OPTS is followed by two colons, its argument is 139*29619d2aSchristos optional. This behavior is specific to the GNU `getopt'. 140*29619d2aSchristos 141*29619d2aSchristos The argument `--' causes premature termination of argument 142*29619d2aSchristos scanning, explicitly telling `getopt' that there are no more 143*29619d2aSchristos options. 144*29619d2aSchristos 145*29619d2aSchristos If OPTS begins with `--', then non-option arguments are treated as 146*29619d2aSchristos arguments to the option '\0'. This behavior is specific to the GNU 147*29619d2aSchristos `getopt'. */ 148*29619d2aSchristos 149*29619d2aSchristos #ifdef __GNU_LIBRARY__ 150*29619d2aSchristos /* Many other libraries have conflicting prototypes for getopt, with 151*29619d2aSchristos differences in the consts, in stdlib.h. To avoid compilation 152*29619d2aSchristos errors, only prototype getopt for the GNU C library. */ 153*29619d2aSchristos extern int getopt (int ___argc, char *const *___argv, const char *__shortopts) 154*29619d2aSchristos __THROW; 155*29619d2aSchristos #else /* not __GNU_LIBRARY__ */ 156*29619d2aSchristos extern int getopt (); 157*29619d2aSchristos #endif /* __GNU_LIBRARY__ */ 158*29619d2aSchristos 159*29619d2aSchristos #ifndef __need_getopt 160*29619d2aSchristos extern int getopt_long (int ___argc, char *const *___argv, 161*29619d2aSchristos const char *__shortopts, 162*29619d2aSchristos const struct option *__longopts, int *__longind) 163*29619d2aSchristos __THROW; 164*29619d2aSchristos extern int getopt_long_only (int ___argc, char *const *___argv, 165*29619d2aSchristos const char *__shortopts, 166*29619d2aSchristos const struct option *__longopts, int *__longind) 167*29619d2aSchristos __THROW; 168*29619d2aSchristos 169*29619d2aSchristos #endif 170*29619d2aSchristos 171*29619d2aSchristos #ifdef __cplusplus 172*29619d2aSchristos } 173*29619d2aSchristos #endif 174*29619d2aSchristos 175*29619d2aSchristos /* Make sure we later can get all the definitions and declarations. */ 176*29619d2aSchristos #undef __need_getopt 177*29619d2aSchristos 178*29619d2aSchristos #endif /* getopt.h */ 179