Merge from trunk.
[bpt/emacs.git] / lib / getopt_.h
CommitLineData
f915f0f7
EZ
1/* Declarations for getopt.\r
2 Copyright (C) 1989-1994, 1996-1999, 2001, 2003-2007, 2009-2011 Free Software\r
3 Foundation, Inc.\r
4 This file is part of the GNU C Library.\r
5\r
6 This program is free software: you can redistribute it and/or modify\r
7 it under the terms of the GNU General Public License as published by\r
8 the Free Software Foundation; either version 3 of the License, or\r
9 (at your option) any later version.\r
10\r
11 This program is distributed in the hope that it will be useful,\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
14 GNU General Public License for more details.\r
15\r
16 You should have received a copy of the GNU General Public License\r
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */\r
18\r
19#ifndef _GL_GETOPT_H\r
20\r
21#if __GNUC__ >= 3\r
22#pragma GCC system_header\r
23#endif\r
24\r
25\r
26/* The include_next requires a split double-inclusion guard. We must\r
27 also inform the replacement unistd.h to not recursively use\r
28 <getopt.h>; our definitions will be present soon enough. */\r
29#if HAVE_GETOPT_H\r
30# define _GL_SYSTEM_GETOPT\r
31# ifndef __GNUC__\r
32# include <next_getopt.h>\r
33# else\r
34# include_next <getopt.h>\r
35# endif\r
36# undef _GL_SYSTEM_GETOPT\r
37#endif\r
38\r
39#ifndef _GL_GETOPT_H\r
40\r
41#ifndef __need_getopt\r
42# define _GL_GETOPT_H 1\r
43#endif\r
44\r
45/* Standalone applications should #define __GETOPT_PREFIX to an\r
46 identifier that prefixes the external functions and variables\r
47 defined in this header. When this happens, include the\r
48 headers that might declare getopt so that they will not cause\r
49 confusion if included after this file (if the system had <getopt.h>,\r
50 we have already included it). Then systematically rename\r
51 identifiers so that they do not collide with the system functions\r
52 and variables. Renaming avoids problems with some compilers and\r
53 linkers. */\r
54#if defined __GETOPT_PREFIX && !defined __need_getopt\r
55# if !HAVE_GETOPT_H\r
56# include <stdlib.h>\r
57# include <stdio.h>\r
58# include <unistd.h>\r
59# endif\r
60# undef __need_getopt\r
61# undef getopt\r
62# undef getopt_long\r
63# undef getopt_long_only\r
64# undef optarg\r
65# undef opterr\r
66# undef optind\r
67# undef optopt\r
68# undef option\r
69# define __GETOPT_CONCAT(x, y) x ## y\r
70# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)\r
71# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)\r
72# define getopt __GETOPT_ID (getopt)\r
73# define getopt_long __GETOPT_ID (getopt_long)\r
74# define getopt_long_only __GETOPT_ID (getopt_long_only)\r
75# define optarg __GETOPT_ID (optarg)\r
76# define opterr __GETOPT_ID (opterr)\r
77# define optind __GETOPT_ID (optind)\r
78# define optopt __GETOPT_ID (optopt)\r
79# define option __GETOPT_ID (option)\r
80# define _getopt_internal __GETOPT_ID (getopt_internal)\r
81#endif\r
82\r
83/* Standalone applications get correct prototypes for getopt_long and\r
84 getopt_long_only; they declare "char **argv". libc uses prototypes\r
85 with "char *const *argv" that are incorrect because getopt_long and\r
86 getopt_long_only can permute argv; this is required for backward\r
87 compatibility (e.g., for LSB 2.0.1).\r
88\r
89 This used to be `#if defined __GETOPT_PREFIX && !defined __need_getopt',\r
90 but it caused redefinition warnings if both unistd.h and getopt.h were\r
91 included, since unistd.h includes getopt.h having previously defined\r
92 __need_getopt.\r
93\r
94 The only place where __getopt_argv_const is used is in definitions\r
95 of getopt_long and getopt_long_only below, but these are visible\r
96 only if __need_getopt is not defined, so it is quite safe to rewrite\r
97 the conditional as follows:\r
98*/\r
99#if !defined __need_getopt\r
100# if defined __GETOPT_PREFIX\r
101# define __getopt_argv_const /* empty */\r
102# else\r
103# define __getopt_argv_const const\r
104# endif\r
105#endif\r
106\r
107/* If __GNU_LIBRARY__ is not already defined, either we are being used\r
108 standalone, or this is the first header included in the source file.\r
109 If we are being used with glibc, we need to include <features.h>, but\r
110 that does not exist if we are standalone. So: if __GNU_LIBRARY__ is\r
111 not defined, include <ctype.h>, which will pull in <features.h> for us\r
112 if it's from glibc. (Why ctype.h? It's guaranteed to exist and it\r
113 doesn't flood the namespace with stuff the way some other headers do.) */\r
114#if !defined __GNU_LIBRARY__\r
115# include <ctype.h>\r
116#endif\r
117\r
118#ifndef __THROW\r
119# ifndef __GNUC_PREREQ\r
120# define __GNUC_PREREQ(maj, min) (0)\r
121# endif\r
122# if defined __cplusplus && __GNUC_PREREQ (2,8)\r
123# define __THROW throw ()\r
124# else\r
125# define __THROW\r
126# endif\r
127#endif\r
128\r
129/* The definition of _GL_ARG_NONNULL is copied here. */\r
130/* A C macro for declaring that specific arguments must not be NULL.\r
131 Copyright (C) 2009-2011 Free Software Foundation, Inc.\r
132\r
133 This program is free software: you can redistribute it and/or modify it\r
134 under the terms of the GNU General Public License as published\r
135 by the Free Software Foundation; either version 3 of the License, or\r
136 (at your option) any later version.\r
137\r
138 This program is distributed in the hope that it will be useful,\r
139 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
140 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
3af31cd8 141 General Public License for more details.\r
f915f0f7
EZ
142\r
143 You should have received a copy of the GNU General Public License\r
144 along with this program. If not, see <http://www.gnu.org/licenses/>. */\r
145\r
146/* _GL_ARG_NONNULL((n,...,m)) tells the compiler and static analyzer tools\r
147 that the values passed as arguments n, ..., m must be non-NULL pointers.\r
148 n = 1 stands for the first argument, n = 2 for the second argument etc. */\r
149#ifndef _GL_ARG_NONNULL\r
150# if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || __GNUC__ > 3\r
151# define _GL_ARG_NONNULL(params) __attribute__ ((__nonnull__ params))\r
152# else\r
153# define _GL_ARG_NONNULL(params)\r
154# endif\r
155#endif\r
156\r
157#ifdef __cplusplus\r
158extern "C" {\r
159#endif\r
160\r
161/* For communication from `getopt' to the caller.\r
162 When `getopt' finds an option that takes an argument,\r
163 the argument value is returned here.\r
164 Also, when `ordering' is RETURN_IN_ORDER,\r
165 each non-option ARGV-element is returned here. */\r
166\r
167extern char *optarg;\r
168\r
169/* Index in ARGV of the next element to be scanned.\r
170 This is used for communication to and from the caller\r
171 and for communication between successive calls to `getopt'.\r
172\r
173 On entry to `getopt', zero means this is the first call; initialize.\r
174\r
175 When `getopt' returns -1, this is the index of the first of the\r
176 non-option elements that the caller should itself scan.\r
177\r
178 Otherwise, `optind' communicates from one call to the next\r
179 how much of ARGV has been scanned so far. */\r
180\r
181extern int optind;\r
182\r
183/* Callers store zero here to inhibit the error message `getopt' prints\r
184 for unrecognized options. */\r
185\r
186extern int opterr;\r
187\r
188/* Set to an option character which was unrecognized. */\r
189\r
190extern int optopt;\r
191\r
192#ifndef __need_getopt\r
193/* Describe the long-named options requested by the application.\r
194 The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector\r
195 of `struct option' terminated by an element containing a name which is\r
196 zero.\r
197\r
198 The field `has_arg' is:\r
199 no_argument (or 0) if the option does not take an argument,\r
200 required_argument (or 1) if the option requires an argument,\r
201 optional_argument (or 2) if the option takes an optional argument.\r
202\r
203 If the field `flag' is not NULL, it points to a variable that is set\r
204 to the value given in the field `val' when the option is found, but\r
205 left unchanged if the option is not found.\r
206\r
207 To have a long-named option do something other than set an `int' to\r
208 a compiled-in constant, such as set a value from `optarg', set the\r
209 option's `flag' field to zero and its `val' field to a nonzero\r
210 value (the equivalent single-letter option character, if there is\r
211 one). For long options that have a zero `flag' field, `getopt'\r
212 returns the contents of the `val' field. */\r
213\r
61c72b06 214# if !GNULIB_defined_struct_option\r
f915f0f7
EZ
215struct option\r
216{\r
217 const char *name;\r
218 /* has_arg can't be an enum because some compilers complain about\r
219 type mismatches in all the code that assumes it is an int. */\r
220 int has_arg;\r
221 int *flag;\r
222 int val;\r
223};\r
61c72b06
EZ
224# define GNULIB_defined_struct_option 1\r
225# endif\r
f915f0f7
EZ
226\r
227/* Names for the values of the `has_arg' field of `struct option'. */\r
228\r
229# define no_argument 0\r
230# define required_argument 1\r
231# define optional_argument 2\r
232#endif /* need getopt */\r
233\r
234\r
235/* Get definitions and prototypes for functions to process the\r
236 arguments in ARGV (ARGC of them, minus the program name) for\r
237 options given in OPTS.\r
238\r
239 Return the option character from OPTS just read. Return -1 when\r
240 there are no more options. For unrecognized options, or options\r
241 missing arguments, `optopt' is set to the option letter, and '?' is\r
242 returned.\r
243\r
244 The OPTS string is a list of characters which are recognized option\r
245 letters, optionally followed by colons, specifying that that letter\r
246 takes an argument, to be placed in `optarg'.\r
247\r
248 If a letter in OPTS is followed by two colons, its argument is\r
249 optional. This behavior is specific to the GNU `getopt'.\r
250\r
251 The argument `--' causes premature termination of argument\r
252 scanning, explicitly telling `getopt' that there are no more\r
253 options.\r
254\r
255 If OPTS begins with `-', then non-option arguments are treated as\r
256 arguments to the option '\1'. This behavior is specific to the GNU\r
257 `getopt'. If OPTS begins with `+', or POSIXLY_CORRECT is set in\r
258 the environment, then do not permute arguments. */\r
259\r
260extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)\r
261 __THROW _GL_ARG_NONNULL ((2, 3));\r
262\r
263#ifndef __need_getopt\r
264extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,\r
265 const char *__shortopts,\r
266 const struct option *__longopts, int *__longind)\r
267 __THROW _GL_ARG_NONNULL ((2, 3));\r
268extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,\r
269 const char *__shortopts,\r
270 const struct option *__longopts, int *__longind)\r
271 __THROW _GL_ARG_NONNULL ((2, 3));\r
272\r
273#endif\r
274\r
275#ifdef __cplusplus\r
276}\r
277#endif\r
278\r
279/* Make sure we later can get all the definitions and declarations. */\r
280#undef __need_getopt\r
281\r
5269d76a
JB
282#endif /* _GL_GETOPT_H */\r
283#endif /* _GL_GETOPT_H */\r