xref: /openbsd/gnu/usr.bin/cvs/lib/getopt.h (revision 892c0aad)
11e72d8d2Sderaadt /* Declarations for getopt.
21e72d8d2Sderaadt    Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
31e72d8d2Sderaadt 
41e72d8d2Sderaadt    This program is free software; you can redistribute it and/or modify it
51e72d8d2Sderaadt    under the terms of the GNU General Public License as published by the
61e72d8d2Sderaadt    Free Software Foundation; either version 2, or (at your option) any
71e72d8d2Sderaadt    later version.
81e72d8d2Sderaadt 
91e72d8d2Sderaadt    This program is distributed in the hope that it will be useful,
101e72d8d2Sderaadt    but WITHOUT ANY WARRANTY; without even the implied warranty of
111e72d8d2Sderaadt    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12461cc63eStholo    GNU General Public License for more details.  */
131e72d8d2Sderaadt 
141e72d8d2Sderaadt #ifndef _GETOPT_H
151e72d8d2Sderaadt #define _GETOPT_H 1
161e72d8d2Sderaadt 
171e72d8d2Sderaadt #ifdef	__cplusplus
181e72d8d2Sderaadt extern "C" {
191e72d8d2Sderaadt #endif
201e72d8d2Sderaadt 
211e72d8d2Sderaadt /* For communication from `getopt' to the caller.
221e72d8d2Sderaadt    When `getopt' finds an option that takes an argument,
231e72d8d2Sderaadt    the argument value is returned here.
241e72d8d2Sderaadt    Also, when `ordering' is RETURN_IN_ORDER,
251e72d8d2Sderaadt    each non-option ARGV-element is returned here.  */
261e72d8d2Sderaadt 
271e72d8d2Sderaadt extern char *optarg;
281e72d8d2Sderaadt 
291e72d8d2Sderaadt /* Index in ARGV of the next element to be scanned.
301e72d8d2Sderaadt    This is used for communication to and from the caller
311e72d8d2Sderaadt    and for communication between successive calls to `getopt'.
321e72d8d2Sderaadt 
331e72d8d2Sderaadt    On entry to `getopt', zero means this is the first call; initialize.
341e72d8d2Sderaadt 
351e72d8d2Sderaadt    When `getopt' returns EOF, this is the index of the first of the
361e72d8d2Sderaadt    non-option elements that the caller should itself scan.
371e72d8d2Sderaadt 
381e72d8d2Sderaadt    Otherwise, `optind' communicates from one call to the next
391e72d8d2Sderaadt    how much of ARGV has been scanned so far.  */
401e72d8d2Sderaadt 
411e72d8d2Sderaadt extern int optind;
421e72d8d2Sderaadt 
431e72d8d2Sderaadt /* Callers store zero here to inhibit the error message `getopt' prints
441e72d8d2Sderaadt    for unrecognized options.  */
451e72d8d2Sderaadt 
461e72d8d2Sderaadt extern int opterr;
471e72d8d2Sderaadt 
481e72d8d2Sderaadt /* Set to an option character which was unrecognized.  */
491e72d8d2Sderaadt 
501e72d8d2Sderaadt extern int optopt;
511e72d8d2Sderaadt 
521e72d8d2Sderaadt /* Describe the long-named options requested by the application.
531e72d8d2Sderaadt    The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
541e72d8d2Sderaadt    of `struct option' terminated by an element containing a name which is
551e72d8d2Sderaadt    zero.
561e72d8d2Sderaadt 
571e72d8d2Sderaadt    The field `has_arg' is:
581e72d8d2Sderaadt    no_argument		(or 0) if the option does not take an argument,
591e72d8d2Sderaadt    required_argument	(or 1) if the option requires an argument,
601e72d8d2Sderaadt    optional_argument 	(or 2) if the option takes an optional argument.
611e72d8d2Sderaadt 
621e72d8d2Sderaadt    If the field `flag' is not NULL, it points to a variable that is set
631e72d8d2Sderaadt    to the value given in the field `val' when the option is found, but
641e72d8d2Sderaadt    left unchanged if the option is not found.
651e72d8d2Sderaadt 
661e72d8d2Sderaadt    To have a long-named option do something other than set an `int' to
671e72d8d2Sderaadt    a compiled-in constant, such as set a value from `optarg', set the
681e72d8d2Sderaadt    option's `flag' field to zero and its `val' field to a nonzero
691e72d8d2Sderaadt    value (the equivalent single-letter option character, if there is
701e72d8d2Sderaadt    one).  For long options that have a zero `flag' field, `getopt'
711e72d8d2Sderaadt    returns the contents of the `val' field.  */
721e72d8d2Sderaadt 
731e72d8d2Sderaadt struct option
741e72d8d2Sderaadt {
751e72d8d2Sderaadt #if	__STDC__
761e72d8d2Sderaadt   const char *name;
771e72d8d2Sderaadt #else
781e72d8d2Sderaadt   char *name;
791e72d8d2Sderaadt #endif
801e72d8d2Sderaadt   /* has_arg can't be an enum because some compilers complain about
811e72d8d2Sderaadt      type mismatches in all the code that assumes it is an int.  */
821e72d8d2Sderaadt   int has_arg;
831e72d8d2Sderaadt   int *flag;
841e72d8d2Sderaadt   int val;
851e72d8d2Sderaadt };
861e72d8d2Sderaadt 
871e72d8d2Sderaadt /* Names for the values of the `has_arg' field of `struct option'.  */
881e72d8d2Sderaadt 
891e72d8d2Sderaadt #define	no_argument		0
901e72d8d2Sderaadt #define required_argument	1
911e72d8d2Sderaadt #define optional_argument	2
921e72d8d2Sderaadt 
931e72d8d2Sderaadt #if __STDC__
941e72d8d2Sderaadt /* Many other libraries have conflicting prototypes for getopt, with
95*892c0aadStholo    differences in the consts, in stdlib.h.  We used to try to prototype
96*892c0aadStholo    it if __GNU_LIBRARY__ but that wasn't problem free either (I'm not sure
97*892c0aadStholo    exactly why), and there is no particular need to prototype it.
98*892c0aadStholo    We really shouldn't be trampling on the system's namespace at all by
99*892c0aadStholo    declaring getopt() but that is a bigger issue.  */
1001e72d8d2Sderaadt extern int getopt ();
101*892c0aadStholo 
1021e72d8d2Sderaadt extern int getopt_long (int argc, char *const *argv, const char *shortopts,
1031e72d8d2Sderaadt 		        const struct option *longopts, int *longind);
1041e72d8d2Sderaadt extern int getopt_long_only (int argc, char *const *argv,
1051e72d8d2Sderaadt 			     const char *shortopts,
1061e72d8d2Sderaadt 		             const struct option *longopts, int *longind);
1071e72d8d2Sderaadt 
1081e72d8d2Sderaadt /* Internal only.  Users should not call this directly.  */
1091e72d8d2Sderaadt extern int _getopt_internal (int argc, char *const *argv,
1101e72d8d2Sderaadt 			     const char *shortopts,
1111e72d8d2Sderaadt 		             const struct option *longopts, int *longind,
1121e72d8d2Sderaadt 			     int long_only);
1131e72d8d2Sderaadt #else /* not __STDC__ */
1141e72d8d2Sderaadt extern int getopt ();
1151e72d8d2Sderaadt extern int getopt_long ();
1161e72d8d2Sderaadt extern int getopt_long_only ();
1171e72d8d2Sderaadt 
1181e72d8d2Sderaadt extern int _getopt_internal ();
1191e72d8d2Sderaadt #endif /* not __STDC__ */
1201e72d8d2Sderaadt 
1211e72d8d2Sderaadt #ifdef	__cplusplus
1221e72d8d2Sderaadt }
1231e72d8d2Sderaadt #endif
1241e72d8d2Sderaadt 
1251e72d8d2Sderaadt #endif /* _GETOPT_H */
126