cctools
getopt.h
1 /*
2 We include a standard implementation of getopt.c,
3 so as to support platforms where it is not available.
4 Amazingly, this version appears to result in SIGBUS
5 errors when used on Darwin. In that case, we just
6 make use of the local implementation.
7 */
8 
9 #if defined(CCTOOLS_OPSYS_DARWIN)
10 #include </usr/include/getopt.h>
11 #else
12 
13 /* Declarations for getopt.
14  Copyright (C) 1989-1994,1996-1999,2001,2003,2004
15  Free Software Foundation, Inc.
16  This file is part of the GNU C Library.
17 
18  The GNU C Library is free software; you can redistribute it and/or
19  modify it under the terms of the GNU Lesser General Public
20  License as published by the Free Software Foundation; either
21  version 2.1 of the License, or (at your option) any later version.
22 
23  The GNU C Library is distributed in the hope that it will be useful,
24  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26  Lesser General Public License for more details.
27 
28  You should have received a copy of the GNU Lesser General Public
29  License along with the GNU C Library; if not, write to the Free
30  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31  02111-1307 USA. */
32 
33 #ifndef _GETOPT_H
34 #define _GETOPT_H 1
35 
36 /* If __GNU_LIBRARY__ is not already defined, either we are being used
37  standalone, or this is the first header included in the source file.
38  If we are being used with glibc, we need to include <features.h>, but
39  that does not exist if we are standalone. So: if __GNU_LIBRARY__ is
40  not defined, include <ctype.h>, which will pull in <features.h> for us
41  if it's from glibc. (Why ctype.h? It's guaranteed to exist and it
42  doesn't flood the namespace with stuff the way some other headers do.) */
43 #if !defined __GNU_LIBRARY__
44 # include <ctype.h>
45 #endif
46 
47 #ifndef __THROW
48 # ifndef __GNUC_PREREQ
49 # define __GNUC_PREREQ(maj, min) (0)
50 # endif
51 # if defined __cplusplus && __GNUC_PREREQ (2,8)
52 # define __THROW throw ()
53 # else
54 # define __THROW
55 # endif
56 #endif
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
62 /* For communication from `getopt' to the caller.
63  When `getopt' finds an option that takes an argument,
64  the argument value is returned here.
65  Also, when `ordering' is RETURN_IN_ORDER,
66  each non-option ARGV-element is returned here. */
67 
68 extern char *optarg;
69 
70 /* Index in ARGV of the next element to be scanned.
71  This is used for communication to and from the caller
72  and for communication between successive calls to `getopt'.
73 
74  On entry to `getopt', zero means this is the first call; initialize.
75 
76  When `getopt' returns -1, this is the index of the first of the
77  non-option elements that the caller should itself scan.
78 
79  Otherwise, `optind' communicates from one call to the next
80  how much of ARGV has been scanned so far. */
81 
82 extern int optind;
83 
84 /* Callers store zero here to inhibit the error message `getopt' prints
85  for unrecognized options. */
86 
87 extern int opterr;
88 
89 /* Set to an option character which was unrecognized. */
90 
91 extern int optopt;
92 
93 /* Describe the long-named options requested by the application.
94  The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
95  of `struct option' terminated by an element containing a name which is
96  zero.
97 
98  The field `has_arg' is:
99  no_argument (or 0) if the option does not take an argument,
100  required_argument (or 1) if the option requires an argument,
101  optional_argument (or 2) if the option takes an optional argument.
102 
103  If the field `flag' is not NULL, it points to a variable that is set
104  to the value given in the field `val' when the option is found, but
105  left unchanged if the option is not found.
106 
107  To have a long-named option do something other than set an `int' to
108  a compiled-in constant, such as set a value from `optarg', set the
109  option's `flag' field to zero and its `val' field to a nonzero
110  value (the equivalent single-letter option character, if there is
111  one). For long options that have a zero `flag' field, `getopt'
112  returns the contents of the `val' field. */
113 
114 struct option
115 {
116  const char *name;
117  /* has_arg can't be an enum because some compilers complain about
118  type mismatches in all the code that assumes it is an int. */
119  int has_arg;
120  int *flag;
121  int val;
122 };
123 
124 /* Names for the values of the `has_arg' field of `struct option'. */
125 
126 # define no_argument 0
127 # define required_argument 1
128 # define optional_argument 2
129 
130 /* Get definitions and prototypes for functions to process the
131  arguments in ARGV (ARGC of them, minus the program name) for
132  options given in OPTS.
133 
134  Return the option character from OPTS just read. Return -1 when
135  there are no more options. For unrecognized options, or options
136  missing arguments, `optopt' is set to the option letter, and '?' is
137  returned.
138 
139  The OPTS string is a list of characters which are recognized option
140  letters, optionally followed by colons, specifying that that letter
141  takes an argument, to be placed in `optarg'.
142 
143  If a letter in OPTS is followed by two colons, its argument is
144  optional. This behavior is specific to the GNU `getopt'.
145 
146  The argument `--' causes premature termination of argument
147  scanning, explicitly telling `getopt' that there are no more
148  options.
149 
150  If OPTS begins with `--', then non-option arguments are treated as
151  arguments to the option '\0'. This behavior is specific to the GNU
152  `getopt'. */
153 
154 #if defined (__STDC__) && __STDC__
155 #ifdef __GNU_LIBRARY__
156 /* Many other libraries have conflicting prototypes for getopt, with
157  differences in the consts, in stdlib.h. To avoid compilation
158  errors, only prototype getopt for the GNU C library. */
159 extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
160  __THROW;
161 #else /* not __GNU_LIBRARY__ */
162 extern int getopt ();
163 #endif /* __GNU_LIBRARY__ */
164 extern int getopt_long (int ___argc, char *const *___argv,
165  const char *__shortopts,
166  const struct option *__longopts, int *__longind)
167  __THROW;
168 extern int getopt_long_only (int ___argc, char *const *___argv,
169  const char *__shortopts,
170  const struct option *__longopts, int *__longind)
171  __THROW;
172 #else /* not __STDC__ */
173 extern int getopt ();
174 extern int getopt_long ();
175 extern int getopt_long_only ();
176 #endif /* __STDC__ */
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif /* getopt.h */
183 
184 #endif /* ! CCTOOLS_OPSYS_DARWIN */
185 
Definition: getopt.h:114