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