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