xref: /openbsd-src/gnu/usr.bin/binutils/include/getopt.h (revision 007c2a4539b8b8aaa95c5e73e77620090abe113b)
12159047fSniklas /* Declarations for getopt.
2c074d1c9Sdrahn    Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 2000,
3c074d1c9Sdrahn    2002 Free Software Foundation, Inc.
42159047fSniklas 
5f7cc78ecSespie    NOTE: The canonical source of this file is maintained with the GNU C Library.
6f7cc78ecSespie    Bugs can be reported to bug-glibc@gnu.org.
7f7cc78ecSespie 
8f7cc78ecSespie    This program is free software; you can redistribute it and/or modify it
9f7cc78ecSespie    under the terms of the GNU General Public License as published by the
10f7cc78ecSespie    Free Software Foundation; either version 2, or (at your option) any
11f7cc78ecSespie    later version.
122159047fSniklas 
132159047fSniklas    This program is distributed in the hope that it will be useful,
142159047fSniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
152159047fSniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16f7cc78ecSespie    GNU General Public License for more details.
172159047fSniklas 
18f7cc78ecSespie    You should have received a copy of the GNU General Public License
192159047fSniklas    along with this program; if not, write to the Free Software
20f7cc78ecSespie    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21f7cc78ecSespie    USA.  */
222159047fSniklas 
232159047fSniklas #ifndef _GETOPT_H
242159047fSniklas #define _GETOPT_H 1
252159047fSniklas 
262159047fSniklas #ifdef	__cplusplus
272159047fSniklas extern "C" {
282159047fSniklas #endif
292159047fSniklas 
302159047fSniklas /* For communication from `getopt' to the caller.
312159047fSniklas    When `getopt' finds an option that takes an argument,
322159047fSniklas    the argument value is returned here.
332159047fSniklas    Also, when `ordering' is RETURN_IN_ORDER,
342159047fSniklas    each non-option ARGV-element is returned here.  */
352159047fSniklas 
362159047fSniklas extern char *optarg;
372159047fSniklas 
382159047fSniklas /* Index in ARGV of the next element to be scanned.
392159047fSniklas    This is used for communication to and from the caller
402159047fSniklas    and for communication between successive calls to `getopt'.
412159047fSniklas 
422159047fSniklas    On entry to `getopt', zero means this is the first call; initialize.
432159047fSniklas 
44f7cc78ecSespie    When `getopt' returns -1, this is the index of the first of the
452159047fSniklas    non-option elements that the caller should itself scan.
462159047fSniklas 
472159047fSniklas    Otherwise, `optind' communicates from one call to the next
482159047fSniklas    how much of ARGV has been scanned so far.  */
492159047fSniklas 
502159047fSniklas extern int optind;
512159047fSniklas 
522159047fSniklas /* Callers store zero here to inhibit the error message `getopt' prints
532159047fSniklas    for unrecognized options.  */
542159047fSniklas 
552159047fSniklas extern int opterr;
562159047fSniklas 
572159047fSniklas /* Set to an option character which was unrecognized.  */
582159047fSniklas 
592159047fSniklas extern int optopt;
602159047fSniklas 
612159047fSniklas /* Describe the long-named options requested by the application.
622159047fSniklas    The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
632159047fSniklas    of `struct option' terminated by an element containing a name which is
642159047fSniklas    zero.
652159047fSniklas 
662159047fSniklas    The field `has_arg' is:
672159047fSniklas    no_argument		(or 0) if the option does not take an argument,
682159047fSniklas    required_argument	(or 1) if the option requires an argument,
692159047fSniklas    optional_argument 	(or 2) if the option takes an optional argument.
702159047fSniklas 
712159047fSniklas    If the field `flag' is not NULL, it points to a variable that is set
722159047fSniklas    to the value given in the field `val' when the option is found, but
732159047fSniklas    left unchanged if the option is not found.
742159047fSniklas 
752159047fSniklas    To have a long-named option do something other than set an `int' to
762159047fSniklas    a compiled-in constant, such as set a value from `optarg', set the
772159047fSniklas    option's `flag' field to zero and its `val' field to a nonzero
782159047fSniklas    value (the equivalent single-letter option character, if there is
792159047fSniklas    one).  For long options that have a zero `flag' field, `getopt'
802159047fSniklas    returns the contents of the `val' field.  */
812159047fSniklas 
822159047fSniklas struct option
832159047fSniklas {
84f7cc78ecSespie #if defined (__STDC__) && __STDC__
852159047fSniklas   const char *name;
862159047fSniklas #else
872159047fSniklas   char *name;
882159047fSniklas #endif
892159047fSniklas   /* has_arg can't be an enum because some compilers complain about
902159047fSniklas      type mismatches in all the code that assumes it is an int.  */
912159047fSniklas   int has_arg;
922159047fSniklas   int *flag;
932159047fSniklas   int val;
942159047fSniklas };
952159047fSniklas 
962159047fSniklas /* Names for the values of the `has_arg' field of `struct option'.  */
972159047fSniklas 
982159047fSniklas #define	no_argument		0
992159047fSniklas #define required_argument	1
1002159047fSniklas #define optional_argument	2
1012159047fSniklas 
102f7cc78ecSespie #if defined (__STDC__) && __STDC__
103b55d4692Sfgsch /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
104b55d4692Sfgsch    undefined, we haven't run the autoconf check so provide the
105b55d4692Sfgsch    declaration without arguments.  If it is 0, we checked and failed
106b55d4692Sfgsch    to find the declaration so provide a fully prototyped one.  If it
107b55d4692Sfgsch    is 1, we found it so don't provide any declaration at all.  */
108*007c2a45Smiod #if !HAVE_DECL_GETOPT
109*007c2a45Smiod #if defined (__GNU_LIBRARY__) || defined (HAVE_DECL_GETOPT)
1102159047fSniklas /* Many other libraries have conflicting prototypes for getopt, with
111c074d1c9Sdrahn    differences in the consts, in unistd.h.  To avoid compilation
1122159047fSniklas    errors, only prototype getopt for the GNU C library.  */
1132159047fSniklas extern int getopt (int argc, char *const *argv, const char *shortopts);
114*007c2a45Smiod #else
115*007c2a45Smiod #ifndef __cplusplus
1162159047fSniklas extern int getopt ();
117*007c2a45Smiod #endif /* __cplusplus */
118b55d4692Sfgsch #endif
119*007c2a45Smiod #endif /* !HAVE_DECL_GETOPT */
120*007c2a45Smiod 
1212159047fSniklas extern int getopt_long (int argc, char *const *argv, const char *shortopts,
1222159047fSniklas 		        const struct option *longopts, int *longind);
1232159047fSniklas extern int getopt_long_only (int argc, char *const *argv,
1242159047fSniklas 			     const char *shortopts,
1252159047fSniklas 		             const struct option *longopts, int *longind);
1262159047fSniklas 
1272159047fSniklas /* Internal only.  Users should not call this directly.  */
1282159047fSniklas extern int _getopt_internal (int argc, char *const *argv,
1292159047fSniklas 			     const char *shortopts,
1302159047fSniklas 		             const struct option *longopts, int *longind,
1312159047fSniklas 			     int long_only);
1322159047fSniklas #else /* not __STDC__ */
1332159047fSniklas extern int getopt ();
1342159047fSniklas extern int getopt_long ();
1352159047fSniklas extern int getopt_long_only ();
1362159047fSniklas 
1372159047fSniklas extern int _getopt_internal ();
138f7cc78ecSespie #endif /* __STDC__ */
1392159047fSniklas 
1402159047fSniklas #ifdef	__cplusplus
1412159047fSniklas }
1422159047fSniklas #endif
1432159047fSniklas 
144f7cc78ecSespie #endif /* getopt.h */
145