xref: /netbsd-src/external/gpl3/gdb/dist/include/getopt.h (revision e663ba6e3a60083e70de702e9d54bf486a57b6a7)
198b9484cSchristos /* Declarations for getopt.
2*e663ba6eSchristos    Copyright (C) 1989-2024 Free Software Foundation, Inc.
398b9484cSchristos 
498b9484cSchristos    NOTE: The canonical source of this file is maintained with the GNU C Library.
598b9484cSchristos    Bugs can be reported to bug-glibc@gnu.org.
698b9484cSchristos 
798b9484cSchristos    This program is free software; you can redistribute it and/or modify it
898b9484cSchristos    under the terms of the GNU General Public License as published by the
998b9484cSchristos    Free Software Foundation; either version 2, or (at your option) any
1098b9484cSchristos    later version.
1198b9484cSchristos 
1298b9484cSchristos    This program is distributed in the hope that it will be useful,
1398b9484cSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1498b9484cSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1598b9484cSchristos    GNU General Public License for more details.
1698b9484cSchristos 
1798b9484cSchristos    You should have received a copy of the GNU General Public License
1898b9484cSchristos    along with this program; if not, write to the Free Software
1998b9484cSchristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
2098b9484cSchristos    USA.  */
2198b9484cSchristos 
2298b9484cSchristos #ifndef _GETOPT_H
2398b9484cSchristos #define _GETOPT_H 1
2498b9484cSchristos 
2598b9484cSchristos #ifdef	__cplusplus
2698b9484cSchristos extern "C" {
2798b9484cSchristos #endif
2898b9484cSchristos 
2998b9484cSchristos /* For communication from `getopt' to the caller.
3098b9484cSchristos    When `getopt' finds an option that takes an argument,
3198b9484cSchristos    the argument value is returned here.
3298b9484cSchristos    Also, when `ordering' is RETURN_IN_ORDER,
3398b9484cSchristos    each non-option ARGV-element is returned here.  */
3498b9484cSchristos 
3598b9484cSchristos extern char *optarg;
3698b9484cSchristos 
3798b9484cSchristos /* Index in ARGV of the next element to be scanned.
3898b9484cSchristos    This is used for communication to and from the caller
3998b9484cSchristos    and for communication between successive calls to `getopt'.
4098b9484cSchristos 
4198b9484cSchristos    On entry to `getopt', zero means this is the first call; initialize.
4298b9484cSchristos 
4398b9484cSchristos    When `getopt' returns -1, this is the index of the first of the
4498b9484cSchristos    non-option elements that the caller should itself scan.
4598b9484cSchristos 
4698b9484cSchristos    Otherwise, `optind' communicates from one call to the next
4798b9484cSchristos    how much of ARGV has been scanned so far.  */
4898b9484cSchristos 
4998b9484cSchristos extern int optind;
5098b9484cSchristos 
5198b9484cSchristos /* Callers store zero here to inhibit the error message `getopt' prints
5298b9484cSchristos    for unrecognized options.  */
5398b9484cSchristos 
5498b9484cSchristos extern int opterr;
5598b9484cSchristos 
5698b9484cSchristos /* Set to an option character which was unrecognized.  */
5798b9484cSchristos 
5898b9484cSchristos extern int optopt;
5998b9484cSchristos 
6098b9484cSchristos /* Describe the long-named options requested by the application.
6198b9484cSchristos    The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
6298b9484cSchristos    of `struct option' terminated by an element containing a name which is
6398b9484cSchristos    zero.
6498b9484cSchristos 
6598b9484cSchristos    The field `has_arg' is:
6698b9484cSchristos    no_argument		(or 0) if the option does not take an argument,
6798b9484cSchristos    required_argument	(or 1) if the option requires an argument,
6898b9484cSchristos    optional_argument 	(or 2) if the option takes an optional argument.
6998b9484cSchristos 
7098b9484cSchristos    If the field `flag' is not NULL, it points to a variable that is set
7198b9484cSchristos    to the value given in the field `val' when the option is found, but
7298b9484cSchristos    left unchanged if the option is not found.
7398b9484cSchristos 
7498b9484cSchristos    To have a long-named option do something other than set an `int' to
7598b9484cSchristos    a compiled-in constant, such as set a value from `optarg', set the
7698b9484cSchristos    option's `flag' field to zero and its `val' field to a nonzero
7798b9484cSchristos    value (the equivalent single-letter option character, if there is
7898b9484cSchristos    one).  For long options that have a zero `flag' field, `getopt'
7998b9484cSchristos    returns the contents of the `val' field.  */
8098b9484cSchristos 
8198b9484cSchristos struct option
8298b9484cSchristos {
8398b9484cSchristos #if defined (__STDC__) && __STDC__
8498b9484cSchristos   const char *name;
8598b9484cSchristos #else
8698b9484cSchristos   char *name;
8798b9484cSchristos #endif
8898b9484cSchristos   /* has_arg can't be an enum because some compilers complain about
8998b9484cSchristos      type mismatches in all the code that assumes it is an int.  */
9098b9484cSchristos   int has_arg;
9198b9484cSchristos   int *flag;
9298b9484cSchristos   int val;
9398b9484cSchristos };
9498b9484cSchristos 
9598b9484cSchristos /* Names for the values of the `has_arg' field of `struct option'.  */
9698b9484cSchristos 
9798b9484cSchristos #define	no_argument		0
9898b9484cSchristos #define required_argument	1
9998b9484cSchristos #define optional_argument	2
10098b9484cSchristos 
10198b9484cSchristos #if defined (__STDC__) && __STDC__
10298b9484cSchristos /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
10398b9484cSchristos    undefined, we haven't run the autoconf check so provide the
10498b9484cSchristos    declaration without arguments.  If it is 0, we checked and failed
10598b9484cSchristos    to find the declaration so provide a fully prototyped one.  If it
10698b9484cSchristos    is 1, we found it so don't provide any declaration at all.  */
10798b9484cSchristos #if !HAVE_DECL_GETOPT
10898b9484cSchristos #if defined (__GNU_LIBRARY__) || defined (HAVE_DECL_GETOPT)
10998b9484cSchristos /* Many other libraries have conflicting prototypes for getopt, with
11098b9484cSchristos    differences in the consts, in unistd.h.  To avoid compilation
11198b9484cSchristos    errors, only prototype getopt for the GNU C library.  */
11298b9484cSchristos extern int getopt (int argc, char *const *argv, const char *shortopts);
11398b9484cSchristos #else
11498b9484cSchristos #ifndef __cplusplus
11598b9484cSchristos extern int getopt ();
11698b9484cSchristos #endif /* __cplusplus */
11798b9484cSchristos #endif
11898b9484cSchristos #endif /* !HAVE_DECL_GETOPT */
11998b9484cSchristos 
12098b9484cSchristos extern int getopt_long (int argc, char *const *argv, const char *shortopts,
12198b9484cSchristos 		        const struct option *longopts, int *longind);
12298b9484cSchristos extern int getopt_long_only (int argc, char *const *argv,
12398b9484cSchristos 			     const char *shortopts,
12498b9484cSchristos 		             const struct option *longopts, int *longind);
12598b9484cSchristos 
12698b9484cSchristos /* Internal only.  Users should not call this directly.  */
12798b9484cSchristos extern int _getopt_internal (int argc, char *const *argv,
12898b9484cSchristos 			     const char *shortopts,
12998b9484cSchristos 		             const struct option *longopts, int *longind,
13098b9484cSchristos 			     int long_only);
13198b9484cSchristos #else /* not __STDC__ */
13298b9484cSchristos extern int getopt ();
13398b9484cSchristos extern int getopt_long ();
13498b9484cSchristos extern int getopt_long_only ();
13598b9484cSchristos 
13698b9484cSchristos extern int _getopt_internal ();
13798b9484cSchristos #endif /* __STDC__ */
13898b9484cSchristos 
13998b9484cSchristos #ifdef	__cplusplus
14098b9484cSchristos }
14198b9484cSchristos #endif
14298b9484cSchristos 
14398b9484cSchristos #endif /* getopt.h */
144