* nsterm.m (x_set_offset): Set dont_constrain to 0 so the call to
[bpt/emacs.git] / m4 / getopt.m4
CommitLineData
b06b1098 1# getopt.m4 serial 33
e275c824 2dnl Copyright (C) 2002-2006, 2008-2011 Free Software Foundation, Inc.
9eff9fe3
PE
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
e275c824
PE
7# Request a POSIX compliant getopt function.
8AC_DEFUN([gl_FUNC_GETOPT_POSIX],
9[
10 m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
11 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
12 gl_GETOPT_IFELSE([
13 gl_REPLACE_GETOPT
14 ],
15 [])
16])
17
18# Request a POSIX compliant getopt function with GNU extensions (such as
19# options with optional arguments) and the functions getopt_long,
20# getopt_long_only.
21AC_DEFUN([gl_FUNC_GETOPT_GNU],
22[
23 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
24
25 AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
26])
9eff9fe3 27
e275c824
PE
28# Request the gnulib implementation of the getopt functions unconditionally.
29# argp.m4 uses this.
30AC_DEFUN([gl_REPLACE_GETOPT],
9eff9fe3 31[
e275c824
PE
32 dnl Arrange for getopt.h to be created.
33 gl_GETOPT_SUBSTITUTE_HEADER
34 dnl Arrange for unistd.h to include getopt.h.
35 GNULIB_UNISTD_H_GETOPT=1
36 dnl Arrange to compile the getopt implementation.
9eff9fe3
PE
37 AC_LIBOBJ([getopt])
38 AC_LIBOBJ([getopt1])
9eff9fe3
PE
39 gl_PREREQ_GETOPT
40])
41
e275c824
PE
42# emacs' configure.in uses this.
43AC_DEFUN([gl_GETOPT_IFELSE],
9eff9fe3 44[
e275c824
PE
45 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
46 AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
9eff9fe3
PE
47])
48
e275c824 49# Determine whether to replace the entire getopt facility.
9eff9fe3
PE
50AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
51[
e275c824
PE
52 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
53 AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
54
55 dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
56 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
57
58 gl_CHECK_NEXT_HEADERS([getopt.h])
e275c824
PE
59 if test $ac_cv_header_getopt_h = yes; then
60 HAVE_GETOPT_H=1
61 else
62 HAVE_GETOPT_H=0
63 fi
64 AC_SUBST([HAVE_GETOPT_H])
65
66 gl_replace_getopt=
67
68 dnl Test whether <getopt.h> is available.
69 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
70 AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
71 fi
72
73 dnl Test whether the function getopt_long is available.
74 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
75 AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
9eff9fe3
PE
76 fi
77
e275c824
PE
78 dnl BSD getopt_long uses an incompatible method to reset option processing.
79 dnl Existence of the variable, in and of itself, is not a reason to replace
80 dnl getopt, but knowledge of the variable is needed to determine how to
81 dnl reset and whether a reset reparses the environment.
82 dnl Solaris supports neither optreset nor optind=0, but keeps no state that
83 dnl needs a reset beyond setting optind=1; detect Solaris by getopt_clip.
84 if test -z "$gl_replace_getopt"; then
85 AC_CHECK_DECLS([optreset], [],
86 [AC_CHECK_DECLS([getopt_clip], [], [],
87 [[#include <getopt.h>]])
88 ],
89 [[#include <getopt.h>]])
9eff9fe3
PE
90 fi
91
e275c824
PE
92 dnl mingw's getopt (in libmingwex.a) does weird things when the options
93 dnl strings starts with '+' and it's not the first call. Some internal state
94 dnl is left over from earlier calls, and neither setting optind = 0 nor
95 dnl setting optreset = 1 get rid of this internal state.
96 dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
97 dnl POSIX 2008 does not specify leading '+' behavior, but see
98 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
99 dnl the next version of POSIX. For now, we only guarantee leading '+'
100 dnl behavior with getopt-gnu.
101 if test -z "$gl_replace_getopt"; then
102 AC_CACHE_CHECK([whether getopt is POSIX compatible],
103 [gl_cv_func_getopt_posix],
104 [
105 dnl This test fails on mingw and succeeds on many other platforms.
106 AC_RUN_IFELSE([AC_LANG_SOURCE([[
107#include <unistd.h>
108#include <stdlib.h>
109#include <string.h>
110
111#if !HAVE_DECL_OPTRESET && !HAVE_DECL_GETOPT_CLIP
112# define OPTIND_MIN 0
113#else
114# define OPTIND_MIN 1
115#endif
116
117int
118main ()
119{
120 {
121 int argc = 0;
122 char *argv[10];
123 int c;
124
125 argv[argc++] = "program";
126 argv[argc++] = "-a";
127 argv[argc++] = "foo";
128 argv[argc++] = "bar";
129 argv[argc] = NULL;
130 optind = OPTIND_MIN;
131 opterr = 0;
132
133 c = getopt (argc, argv, "ab");
134 if (!(c == 'a'))
135 return 1;
136 c = getopt (argc, argv, "ab");
137 if (!(c == -1))
138 return 2;
139 if (!(optind == 2))
140 return 3;
141 }
142 /* Some internal state exists at this point. */
143 {
144 int argc = 0;
145 char *argv[10];
146 int c;
147
148 argv[argc++] = "program";
149 argv[argc++] = "donald";
150 argv[argc++] = "-p";
151 argv[argc++] = "billy";
152 argv[argc++] = "duck";
153 argv[argc++] = "-a";
154 argv[argc++] = "bar";
155 argv[argc] = NULL;
156 optind = OPTIND_MIN;
157 opterr = 0;
158
159 c = getopt (argc, argv, "+abp:q:");
160 if (!(c == -1))
161 return 4;
162 if (!(strcmp (argv[0], "program") == 0))
163 return 5;
164 if (!(strcmp (argv[1], "donald") == 0))
165 return 6;
166 if (!(strcmp (argv[2], "-p") == 0))
167 return 7;
168 if (!(strcmp (argv[3], "billy") == 0))
169 return 8;
170 if (!(strcmp (argv[4], "duck") == 0))
171 return 9;
172 if (!(strcmp (argv[5], "-a") == 0))
173 return 10;
174 if (!(strcmp (argv[6], "bar") == 0))
175 return 11;
176 if (!(optind == 1))
177 return 12;
178 }
179 /* Detect MacOS 10.5, AIX 7.1 bug. */
180 {
181 char *argv[3] = { "program", "-ab", NULL };
182 optind = OPTIND_MIN;
183 opterr = 0;
184 if (getopt (2, argv, "ab:") != 'a')
185 return 13;
186 if (getopt (2, argv, "ab:") != '?')
187 return 14;
188 if (optopt != 'b')
189 return 15;
190 if (optind != 2)
191 return 16;
192 }
193
194 return 0;
195}
196]])],
197 [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
198 [case "$host_os" in
199 mingw*) gl_cv_func_getopt_posix="guessing no";;
200 darwin* | aix*) gl_cv_func_getopt_posix="guessing no";;
201 *) gl_cv_func_getopt_posix="guessing yes";;
202 esac
203 ])
204 ])
205 case "$gl_cv_func_getopt_posix" in
206 *no) gl_replace_getopt=yes ;;
207 esac
208 fi
209
210 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
211 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
212 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
213 # optstring is necessary for programs like m4 that have POSIX-mandated
214 # semantics for supporting options interspersed with files.
215 # Also, since getopt_long is a GNU extension, we require optind=0.
216 # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
217 # so take care to revert to the correct (non-)export state.
218dnl GNU Coding Standards currently allow awk but not env; besides, env
219dnl is ambiguous with environment values that contain newlines.
220 gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
221 case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
222 xx) gl_had_POSIXLY_CORRECT=exported ;;
223 x) gl_had_POSIXLY_CORRECT=yes ;;
224 *) gl_had_POSIXLY_CORRECT= ;;
225 esac
226 POSIXLY_CORRECT=1
227 export POSIXLY_CORRECT
228 AC_RUN_IFELSE(
229 [AC_LANG_PROGRAM([[#include <getopt.h>
230 #include <stddef.h>
231 #include <string.h>
232 ]], [[
233 int result = 0;
234 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
235 and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
236 OSF/1 5.1, Solaris 10. */
237 {
238 char *myargv[3];
239 myargv[0] = "conftest";
240 myargv[1] = "-+";
241 myargv[2] = 0;
242 opterr = 0;
243 if (getopt (2, myargv, "+a") != '?')
244 result |= 1;
245 }
246 /* This code succeeds on glibc 2.8, mingw,
247 and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
248 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
249 {
250 char *argv[] = { "program", "-p", "foo", "bar", NULL };
251
252 optind = 1;
253 if (getopt (4, argv, "p::") != 'p')
254 result |= 2;
255 else if (optarg != NULL)
256 result |= 4;
257 else if (getopt (4, argv, "p::") != -1)
258 result |= 6;
259 else if (optind != 2)
260 result |= 8;
261 }
262 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
263 {
264 char *argv[] = { "program", "foo", "-p", NULL };
265 optind = 0;
266 if (getopt (3, argv, "-p") != 1)
267 result |= 16;
268 else if (getopt (3, argv, "-p") != 'p')
269 result |= 32;
270 }
271 /* This code fails on glibc 2.11. */
272 {
273 char *argv[] = { "program", "-b", "-a", NULL };
274 optind = opterr = 0;
275 if (getopt (3, argv, "+:a:b") != 'b')
276 result |= 64;
277 else if (getopt (3, argv, "+:a:b") != ':')
278 result |= 64;
279 }
280 return result;
281 ]])],
282 [gl_cv_func_getopt_gnu=yes],
283 [gl_cv_func_getopt_gnu=no],
284 [dnl Cross compiling. Guess based on host and declarations.
285 case $host_os:$ac_cv_have_decl_optreset in
286 *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
287 *:yes) gl_cv_func_getopt_gnu=no;;
288 *) gl_cv_func_getopt_gnu=yes;;
289 esac
290 ])
291 case $gl_had_POSIXLY_CORRECT in
292 exported) ;;
293 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
294 *) AS_UNSET([POSIXLY_CORRECT]) ;;
295 esac
296 ])
297 if test "$gl_cv_func_getopt_gnu" = "no"; then
298 gl_replace_getopt=yes
9eff9fe3
PE
299 fi
300 fi
301])
302
e275c824
PE
303# emacs' configure.in uses this.
304AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
9eff9fe3 305[
e275c824
PE
306 GETOPT_H=getopt.h
307 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
308 [Define to rpl_ if the getopt replacement functions and variables
309 should be used.])
310 AC_SUBST([GETOPT_H])
9eff9fe3
PE
311])
312
9eff9fe3 313# Prerequisites of lib/getopt*.
e275c824
PE
314# emacs' configure.in uses this.
315AC_DEFUN([gl_PREREQ_GETOPT],
316[
317 AC_CHECK_DECLS_ONCE([getenv])
318])