]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/cvs/lib/getopt.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / cvs / lib / getopt.h
1 /* Declarations for getopt.
2    Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 2, or (at your option) any
7    later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.  */
13
14 #ifndef _GETOPT_H
15 #define _GETOPT_H 1
16
17 /* CVS - DRP
18  *
19  * If the OS defines this, just redefine the names to avoid namespace
20  * clashes.  In theory, we should be testing the built in functions to
21  * see if they do what we want and use them if possible, but this is
22  * easier...
23  *
24  * Namely, this was occurring under Mac OS X.  This is a Mac OS X (or
25  * OS X related) bug.
26  *
27  * Oops.  We avoid compiling this with ifdefs because pretty much all of
28  * getopt.c is switched on the same macros...  this isn't right, but I think
29  * this isn't our file.  Probably best not to mess with it too much.
30  */
31 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
32 # ifdef HAVE_GETOPT
33 #   define getopt               cvs_getopt
34 #   define optarg               cvs_optarg
35 #   define opterr               cvs_opterr
36 #   define optind               cvs_optind
37 #   define optopt               cvs_optopt
38 # endif /* HAVE_GETOPT */
39 #endif  /* _LIBC or not __GNU_LIBRARY__.  */
40
41 #ifdef  __cplusplus
42 extern "C" {
43 #endif
44
45 /* For communication from `getopt' to the caller.
46    When `getopt' finds an option that takes an argument,
47    the argument value is returned here.
48    Also, when `ordering' is RETURN_IN_ORDER,
49    each non-option ARGV-element is returned here.  */
50
51 extern char *optarg;
52
53 /* Index in ARGV of the next element to be scanned.
54    This is used for communication to and from the caller
55    and for communication between successive calls to `getopt'.
56
57    On entry to `getopt', zero means this is the first call; initialize.
58
59    When `getopt' returns EOF, this is the index of the first of the
60    non-option elements that the caller should itself scan.
61
62    Otherwise, `optind' communicates from one call to the next
63    how much of ARGV has been scanned so far.  */
64
65 extern int optind;
66
67 /* Callers store zero here to inhibit the error message `getopt' prints
68    for unrecognized options.  */
69
70 extern int opterr;
71
72 /* Set to an option character which was unrecognized.  */
73
74 extern int optopt;
75
76 /* Describe the long-named options requested by the application.
77    The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
78    of `struct option' terminated by an element containing a name which is
79    zero.
80
81    The field `has_arg' is:
82    no_argument          (or 0) if the option does not take an argument,
83    required_argument    (or 1) if the option requires an argument,
84    optional_argument    (or 2) if the option takes an optional argument.
85
86    If the field `flag' is not NULL, it points to a variable that is set
87    to the value given in the field `val' when the option is found, but
88    left unchanged if the option is not found.
89
90    To have a long-named option do something other than set an `int' to
91    a compiled-in constant, such as set a value from `optarg', set the
92    option's `flag' field to zero and its `val' field to a nonzero
93    value (the equivalent single-letter option character, if there is
94    one).  For long options that have a zero `flag' field, `getopt'
95    returns the contents of the `val' field.  */
96
97 struct option
98 {
99 #if     __STDC__
100   const char *name;
101 #else
102   char *name;
103 #endif
104   /* has_arg can't be an enum because some compilers complain about
105      type mismatches in all the code that assumes it is an int.  */
106   int has_arg;
107   int *flag;
108   int val;
109 };
110
111 /* Names for the values of the `has_arg' field of `struct option'.  */
112
113 #define no_argument             0
114 #define required_argument       1
115 #define optional_argument       2
116
117 #if __STDC__
118 /* Many other libraries have conflicting prototypes for getopt, with
119    differences in the consts, in stdlib.h.  We used to try to prototype
120    it if __GNU_LIBRARY__ but that wasn't problem free either (I'm not sure
121    exactly why), and there is no particular need to prototype it.
122    We really shouldn't be trampling on the system's namespace at all by
123    declaring getopt() but that is a bigger issue.  */
124 extern int getopt ();
125
126 extern int getopt_long (int argc, char *const *argv, const char *shortopts,
127                         const struct option *longopts, int *longind);
128 extern int getopt_long_only (int argc, char *const *argv,
129                              const char *shortopts,
130                              const struct option *longopts, int *longind);
131
132 /* Internal only.  Users should not call this directly.  */
133 extern int _getopt_internal (int argc, char *const *argv,
134                              const char *shortopts,
135                              const struct option *longopts, int *longind,
136                              int long_only);
137 #else /* not __STDC__ */
138 extern int getopt ();
139 extern int getopt_long ();
140 extern int getopt_long_only ();
141
142 extern int _getopt_internal ();
143 #endif /* not __STDC__ */
144
145 #ifdef  __cplusplus
146 }
147 #endif
148
149 #endif /* _GETOPT_H */