xref: /freebsd/contrib/diff/lib/getopt.h (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
118fd37a7SXin LI /* Declarations for getopt.
218fd37a7SXin LI    Copyright (C) 1989-1994,1996-1999,2001,2003,2004
318fd37a7SXin LI    Free Software Foundation, Inc.
418fd37a7SXin LI    This file is part of the GNU C Library.
518fd37a7SXin LI 
618fd37a7SXin LI    This program is free software; you can redistribute it and/or modify
718fd37a7SXin LI    it under the terms of the GNU General Public License as published by
818fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
918fd37a7SXin LI    any later version.
1018fd37a7SXin LI 
1118fd37a7SXin LI    This program is distributed in the hope that it will be useful,
1218fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
1318fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1418fd37a7SXin LI    GNU General Public License for more details.
1518fd37a7SXin LI 
1618fd37a7SXin LI    You should have received a copy of the GNU General Public License along
1718fd37a7SXin LI    with this program; if not, write to the Free Software Foundation,
1818fd37a7SXin LI    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
1918fd37a7SXin LI 
2018fd37a7SXin LI #ifndef _GETOPT_H
2118fd37a7SXin LI 
2218fd37a7SXin LI #ifndef __need_getopt
2318fd37a7SXin LI # define _GETOPT_H 1
2418fd37a7SXin LI #endif
2518fd37a7SXin LI 
2618fd37a7SXin LI /* If __GNU_LIBRARY__ is not already defined, either we are being used
2718fd37a7SXin LI    standalone, or this is the first header included in the source file.
2818fd37a7SXin LI    If we are being used with glibc, we need to include <features.h>, but
2918fd37a7SXin LI    that does not exist if we are standalone.  So: if __GNU_LIBRARY__ is
3018fd37a7SXin LI    not defined, include <ctype.h>, which will pull in <features.h> for us
3118fd37a7SXin LI    if it's from glibc.  (Why ctype.h?  It's guaranteed to exist and it
3218fd37a7SXin LI    doesn't flood the namespace with stuff the way some other headers do.)  */
3318fd37a7SXin LI #if !defined __GNU_LIBRARY__
3418fd37a7SXin LI # include <ctype.h>
3518fd37a7SXin LI #endif
3618fd37a7SXin LI 
3718fd37a7SXin LI #ifndef __THROW
3818fd37a7SXin LI # ifndef __GNUC_PREREQ
3918fd37a7SXin LI #  define __GNUC_PREREQ(maj, min) (0)
4018fd37a7SXin LI # endif
4118fd37a7SXin LI # if defined __cplusplus && __GNUC_PREREQ (2,8)
4218fd37a7SXin LI #  define __THROW	throw ()
4318fd37a7SXin LI # else
4418fd37a7SXin LI #  define __THROW
4518fd37a7SXin LI # endif
4618fd37a7SXin LI #endif
4718fd37a7SXin LI 
4818fd37a7SXin LI #ifdef	__cplusplus
4918fd37a7SXin LI extern "C" {
5018fd37a7SXin LI #endif
5118fd37a7SXin LI 
5218fd37a7SXin LI /* For communication from `getopt' to the caller.
5318fd37a7SXin LI    When `getopt' finds an option that takes an argument,
5418fd37a7SXin LI    the argument value is returned here.
5518fd37a7SXin LI    Also, when `ordering' is RETURN_IN_ORDER,
5618fd37a7SXin LI    each non-option ARGV-element is returned here.  */
5718fd37a7SXin LI 
5818fd37a7SXin LI extern char *optarg;
5918fd37a7SXin LI 
6018fd37a7SXin LI /* Index in ARGV of the next element to be scanned.
6118fd37a7SXin LI    This is used for communication to and from the caller
6218fd37a7SXin LI    and for communication between successive calls to `getopt'.
6318fd37a7SXin LI 
6418fd37a7SXin LI    On entry to `getopt', zero means this is the first call; initialize.
6518fd37a7SXin LI 
6618fd37a7SXin LI    When `getopt' returns -1, this is the index of the first of the
6718fd37a7SXin LI    non-option elements that the caller should itself scan.
6818fd37a7SXin LI 
6918fd37a7SXin LI    Otherwise, `optind' communicates from one call to the next
7018fd37a7SXin LI    how much of ARGV has been scanned so far.  */
7118fd37a7SXin LI 
7218fd37a7SXin LI extern int optind;
7318fd37a7SXin LI 
7418fd37a7SXin LI /* Callers store zero here to inhibit the error message `getopt' prints
7518fd37a7SXin LI    for unrecognized options.  */
7618fd37a7SXin LI 
7718fd37a7SXin LI extern int opterr;
7818fd37a7SXin LI 
7918fd37a7SXin LI /* Set to an option character which was unrecognized.  */
8018fd37a7SXin LI 
8118fd37a7SXin LI extern int optopt;
8218fd37a7SXin LI 
8318fd37a7SXin LI #ifndef __need_getopt
8418fd37a7SXin LI /* Describe the long-named options requested by the application.
8518fd37a7SXin LI    The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
8618fd37a7SXin LI    of `struct option' terminated by an element containing a name which is
8718fd37a7SXin LI    zero.
8818fd37a7SXin LI 
8918fd37a7SXin LI    The field `has_arg' is:
9018fd37a7SXin LI    no_argument		(or 0) if the option does not take an argument,
9118fd37a7SXin LI    required_argument	(or 1) if the option requires an argument,
9218fd37a7SXin LI    optional_argument 	(or 2) if the option takes an optional argument.
9318fd37a7SXin LI 
9418fd37a7SXin LI    If the field `flag' is not NULL, it points to a variable that is set
9518fd37a7SXin LI    to the value given in the field `val' when the option is found, but
9618fd37a7SXin LI    left unchanged if the option is not found.
9718fd37a7SXin LI 
9818fd37a7SXin LI    To have a long-named option do something other than set an `int' to
9918fd37a7SXin LI    a compiled-in constant, such as set a value from `optarg', set the
10018fd37a7SXin LI    option's `flag' field to zero and its `val' field to a nonzero
10118fd37a7SXin LI    value (the equivalent single-letter option character, if there is
10218fd37a7SXin LI    one).  For long options that have a zero `flag' field, `getopt'
10318fd37a7SXin LI    returns the contents of the `val' field.  */
10418fd37a7SXin LI 
10518fd37a7SXin LI struct option
10618fd37a7SXin LI {
10718fd37a7SXin LI   const char *name;
10818fd37a7SXin LI   /* has_arg can't be an enum because some compilers complain about
10918fd37a7SXin LI      type mismatches in all the code that assumes it is an int.  */
11018fd37a7SXin LI   int has_arg;
11118fd37a7SXin LI   int *flag;
11218fd37a7SXin LI   int val;
11318fd37a7SXin LI };
11418fd37a7SXin LI 
11518fd37a7SXin LI /* Names for the values of the `has_arg' field of `struct option'.  */
11618fd37a7SXin LI 
11718fd37a7SXin LI # define no_argument		0
11818fd37a7SXin LI # define required_argument	1
11918fd37a7SXin LI # define optional_argument	2
12018fd37a7SXin LI #endif	/* need getopt */
12118fd37a7SXin LI 
12218fd37a7SXin LI 
12318fd37a7SXin LI /* Get definitions and prototypes for functions to process the
12418fd37a7SXin LI    arguments in ARGV (ARGC of them, minus the program name) for
12518fd37a7SXin LI    options given in OPTS.
12618fd37a7SXin LI 
12718fd37a7SXin LI    Return the option character from OPTS just read.  Return -1 when
12818fd37a7SXin LI    there are no more options.  For unrecognized options, or options
12918fd37a7SXin LI    missing arguments, `optopt' is set to the option letter, and '?' is
13018fd37a7SXin LI    returned.
13118fd37a7SXin LI 
13218fd37a7SXin LI    The OPTS string is a list of characters which are recognized option
13318fd37a7SXin LI    letters, optionally followed by colons, specifying that that letter
13418fd37a7SXin LI    takes an argument, to be placed in `optarg'.
13518fd37a7SXin LI 
13618fd37a7SXin LI    If a letter in OPTS is followed by two colons, its argument is
13718fd37a7SXin LI    optional.  This behavior is specific to the GNU `getopt'.
13818fd37a7SXin LI 
13918fd37a7SXin LI    The argument `--' causes premature termination of argument
14018fd37a7SXin LI    scanning, explicitly telling `getopt' that there are no more
14118fd37a7SXin LI    options.
14218fd37a7SXin LI 
14318fd37a7SXin LI    If OPTS begins with `--', then non-option arguments are treated as
14418fd37a7SXin LI    arguments to the option '\0'.  This behavior is specific to the GNU
14518fd37a7SXin LI    `getopt'.  */
14618fd37a7SXin LI 
14718fd37a7SXin LI #ifdef __GNU_LIBRARY__
14818fd37a7SXin LI /* Many other libraries have conflicting prototypes for getopt, with
14918fd37a7SXin LI    differences in the consts, in stdlib.h.  To avoid compilation
15018fd37a7SXin LI    errors, only prototype getopt for the GNU C library.  */
15118fd37a7SXin LI extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
15218fd37a7SXin LI        __THROW;
15318fd37a7SXin LI #else /* not __GNU_LIBRARY__ */
15418fd37a7SXin LI extern int getopt ();
15518fd37a7SXin LI #endif /* __GNU_LIBRARY__ */
15618fd37a7SXin LI 
15718fd37a7SXin LI #ifndef __need_getopt
15818fd37a7SXin LI extern int getopt_long (int ___argc, char *const *___argv,
15918fd37a7SXin LI 			const char *__shortopts,
16018fd37a7SXin LI 		        const struct option *__longopts, int *__longind)
16118fd37a7SXin LI        __THROW;
16218fd37a7SXin LI extern int getopt_long_only (int ___argc, char *const *___argv,
16318fd37a7SXin LI 			     const char *__shortopts,
16418fd37a7SXin LI 		             const struct option *__longopts, int *__longind)
16518fd37a7SXin LI        __THROW;
16618fd37a7SXin LI 
16718fd37a7SXin LI #endif
16818fd37a7SXin LI 
16918fd37a7SXin LI #ifdef	__cplusplus
17018fd37a7SXin LI }
17118fd37a7SXin LI #endif
17218fd37a7SXin LI 
17318fd37a7SXin LI /* Make sure we later can get all the definitions and declarations.  */
17418fd37a7SXin LI #undef __need_getopt
17518fd37a7SXin LI 
17618fd37a7SXin LI #endif /* getopt.h */
177