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