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