Merge from gnulib.
[bpt/emacs.git] / m4 / getopt.m4
CommitLineData
47d0c011 1# getopt.m4 serial 42
caf8a9b2 2dnl Copyright (C) 2002-2006, 2008-2012 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])
8aeb5be9
PE
12 dnl Other modules can request the gnulib implementation of the getopt
13 dnl functions unconditionally, by defining gl_REPLACE_GETOPT_ALWAYS.
14 dnl argp.m4 does this.
15 m4_ifdef([gl_REPLACE_GETOPT_ALWAYS], [
16 gl_GETOPT_IFELSE([], [])
17 REPLACE_GETOPT=1
18 ], [
19 REPLACE_GETOPT=0
20 gl_GETOPT_IFELSE([
21 REPLACE_GETOPT=1
22 ],
23 [])
24 ])
25 if test $REPLACE_GETOPT = 1; then
26 dnl Arrange for getopt.h to be created.
27 gl_GETOPT_SUBSTITUTE_HEADER
8aeb5be9 28 fi
e275c824
PE
29])
30
31# Request a POSIX compliant getopt function with GNU extensions (such as
32# options with optional arguments) and the functions getopt_long,
33# getopt_long_only.
34AC_DEFUN([gl_FUNC_GETOPT_GNU],
35[
36 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
37
38 AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
39])
9eff9fe3 40
e275c824
PE
41# emacs' configure.in uses this.
42AC_DEFUN([gl_GETOPT_IFELSE],
9eff9fe3 43[
e275c824
PE
44 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
45 AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
9eff9fe3
PE
46])
47
e275c824 48# Determine whether to replace the entire getopt facility.
9eff9fe3
PE
49AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
50[
e275c824
PE
51 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
52 AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
53
54 dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
55 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
56
57 gl_CHECK_NEXT_HEADERS([getopt.h])
e275c824
PE
58 if test $ac_cv_header_getopt_h = yes; then
59 HAVE_GETOPT_H=1
60 else
61 HAVE_GETOPT_H=0
62 fi
63 AC_SUBST([HAVE_GETOPT_H])
64
65 gl_replace_getopt=
66
67 dnl Test whether <getopt.h> is available.
68 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
69 AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
70 fi
71
72 dnl Test whether the function getopt_long is available.
73 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
74 AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
9eff9fe3
PE
75 fi
76
e275c824
PE
77 dnl mingw's getopt (in libmingwex.a) does weird things when the options
78 dnl strings starts with '+' and it's not the first call. Some internal state
79 dnl is left over from earlier calls, and neither setting optind = 0 nor
80 dnl setting optreset = 1 get rid of this internal state.
81 dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
82 dnl POSIX 2008 does not specify leading '+' behavior, but see
83 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
84 dnl the next version of POSIX. For now, we only guarantee leading '+'
85 dnl behavior with getopt-gnu.
86 if test -z "$gl_replace_getopt"; then
87 AC_CACHE_CHECK([whether getopt is POSIX compatible],
88 [gl_cv_func_getopt_posix],
89 [
4a621aae
PE
90 dnl BSD getopt_long uses an incompatible method to reset option
91 dnl processing. Existence of the optreset variable, in and of
d6974efa
PE
92 dnl itself, is not a reason to replace getopt, but knowledge
93 dnl of the variable is needed to determine how to reset and
94 dnl whether a reset reparses the environment. Solaris
95 dnl supports neither optreset nor optind=0, but keeps no state
96 dnl that needs a reset beyond setting optind=1; detect Solaris
97 dnl by getopt_clip.
4a621aae 98 AC_LINK_IFELSE(
d6974efa
PE
99 [AC_LANG_PROGRAM(
100 [[#include <unistd.h>]],
101 [[int *p = &optreset; return optreset;]])],
102 [gl_optind_min=1],
103 [AC_COMPILE_IFELSE(
104 [AC_LANG_PROGRAM(
105 [[#include <getopt.h>]],
106 [[return !getopt_clip;]])],
107 [gl_optind_min=1],
108 [gl_optind_min=0])])
109
e275c824 110 dnl This test fails on mingw and succeeds on many other platforms.
d6974efa
PE
111 gl_save_CPPFLAGS=$CPPFLAGS
112 CPPFLAGS="$CPPFLAGS -DOPTIND_MIN=$gl_optind_min"
e275c824
PE
113 AC_RUN_IFELSE([AC_LANG_SOURCE([[
114#include <unistd.h>
115#include <stdlib.h>
116#include <string.h>
117
e275c824
PE
118int
119main ()
120{
121 {
4a621aae
PE
122 static char program[] = "program";
123 static char a[] = "-a";
124 static char foo[] = "foo";
125 static char bar[] = "bar";
126 char *argv[] = { program, a, foo, bar, NULL };
e275c824
PE
127 int c;
128
e275c824
PE
129 optind = OPTIND_MIN;
130 opterr = 0;
131
4a621aae 132 c = getopt (4, argv, "ab");
e275c824
PE
133 if (!(c == 'a'))
134 return 1;
4a621aae 135 c = getopt (4, argv, "ab");
e275c824
PE
136 if (!(c == -1))
137 return 2;
138 if (!(optind == 2))
139 return 3;
140 }
141 /* Some internal state exists at this point. */
142 {
4a621aae
PE
143 static char program[] = "program";
144 static char donald[] = "donald";
145 static char p[] = "-p";
146 static char billy[] = "billy";
147 static char duck[] = "duck";
148 static char a[] = "-a";
149 static char bar[] = "bar";
150 char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
e275c824
PE
151 int c;
152
e275c824
PE
153 optind = OPTIND_MIN;
154 opterr = 0;
155
4a621aae 156 c = getopt (7, argv, "+abp:q:");
e275c824
PE
157 if (!(c == -1))
158 return 4;
159 if (!(strcmp (argv[0], "program") == 0))
160 return 5;
161 if (!(strcmp (argv[1], "donald") == 0))
162 return 6;
163 if (!(strcmp (argv[2], "-p") == 0))
164 return 7;
165 if (!(strcmp (argv[3], "billy") == 0))
166 return 8;
167 if (!(strcmp (argv[4], "duck") == 0))
168 return 9;
169 if (!(strcmp (argv[5], "-a") == 0))
170 return 10;
171 if (!(strcmp (argv[6], "bar") == 0))
172 return 11;
173 if (!(optind == 1))
174 return 12;
175 }
05730648 176 /* Detect Mac OS X 10.5, AIX 7.1 bug. */
e275c824 177 {
4a621aae
PE
178 static char program[] = "program";
179 static char ab[] = "-ab";
180 char *argv[3] = { program, ab, NULL };
e275c824
PE
181 optind = OPTIND_MIN;
182 opterr = 0;
183 if (getopt (2, argv, "ab:") != 'a')
184 return 13;
185 if (getopt (2, argv, "ab:") != '?')
186 return 14;
187 if (optopt != 'b')
188 return 15;
189 if (optind != 2)
190 return 16;
191 }
192
193 return 0;
194}
195]])],
196 [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
197 [case "$host_os" in
198 mingw*) gl_cv_func_getopt_posix="guessing no";;
199 darwin* | aix*) gl_cv_func_getopt_posix="guessing no";;
200 *) gl_cv_func_getopt_posix="guessing yes";;
201 esac
202 ])
d6974efa 203 CPPFLAGS=$gl_save_CPPFLAGS
e275c824
PE
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>
24e0f6b1 232 ]GL_NOCRASH[
e275c824
PE
233 ]], [[
234 int result = 0;
24e0f6b1
PE
235
236 nocrash_init();
237
e275c824 238 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
05730648 239 and fails on Mac OS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
e275c824
PE
240 OSF/1 5.1, Solaris 10. */
241 {
4a621aae
PE
242 static char conftest[] = "conftest";
243 static char plus[] = "-+";
244 char *argv[3] = { conftest, plus, NULL };
e275c824 245 opterr = 0;
4a621aae 246 if (getopt (2, argv, "+a") != '?')
e275c824
PE
247 result |= 1;
248 }
249 /* This code succeeds on glibc 2.8, mingw,
05730648 250 and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
e275c824
PE
251 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
252 {
4a621aae
PE
253 static char program[] = "program";
254 static char p[] = "-p";
255 static char foo[] = "foo";
256 static char bar[] = "bar";
257 char *argv[] = { program, p, foo, bar, NULL };
e275c824
PE
258
259 optind = 1;
260 if (getopt (4, argv, "p::") != 'p')
261 result |= 2;
262 else if (optarg != NULL)
263 result |= 4;
264 else if (getopt (4, argv, "p::") != -1)
265 result |= 6;
266 else if (optind != 2)
267 result |= 8;
268 }
269 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
270 {
4a621aae
PE
271 static char program[] = "program";
272 static char foo[] = "foo";
273 static char p[] = "-p";
274 char *argv[] = { program, foo, p, NULL };
e275c824
PE
275 optind = 0;
276 if (getopt (3, argv, "-p") != 1)
277 result |= 16;
278 else if (getopt (3, argv, "-p") != 'p')
05730648 279 result |= 16;
e275c824
PE
280 }
281 /* This code fails on glibc 2.11. */
282 {
4a621aae
PE
283 static char program[] = "program";
284 static char b[] = "-b";
285 static char a[] = "-a";
286 char *argv[] = { program, b, a, NULL };
e275c824
PE
287 optind = opterr = 0;
288 if (getopt (3, argv, "+:a:b") != 'b')
05730648 289 result |= 32;
e275c824 290 else if (getopt (3, argv, "+:a:b") != ':')
05730648 291 result |= 32;
e275c824 292 }
4a621aae
PE
293 /* This code dumps core on glibc 2.14. */
294 {
295 static char program[] = "program";
296 static char w[] = "-W";
297 static char dummy[] = "dummy";
298 char *argv[] = { program, w, dummy, NULL };
299 optind = opterr = 1;
300 if (getopt (3, argv, "W;") != 'W')
05730648 301 result |= 64;
4a621aae 302 }
e275c824
PE
303 return result;
304 ]])],
305 [gl_cv_func_getopt_gnu=yes],
306 [gl_cv_func_getopt_gnu=no],
307 [dnl Cross compiling. Guess based on host and declarations.
308 case $host_os:$ac_cv_have_decl_optreset in
309 *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
310 *:yes) gl_cv_func_getopt_gnu=no;;
311 *) gl_cv_func_getopt_gnu=yes;;
312 esac
313 ])
314 case $gl_had_POSIXLY_CORRECT in
315 exported) ;;
316 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
317 *) AS_UNSET([POSIXLY_CORRECT]) ;;
318 esac
319 ])
320 if test "$gl_cv_func_getopt_gnu" = "no"; then
321 gl_replace_getopt=yes
47d0c011
PE
322 else
323 AC_CACHE_CHECK([for working GNU getopt_long function],
324 [gl_cv_func_getopt_long_gnu],
325 [AC_RUN_IFELSE(
326 [AC_LANG_PROGRAM(
327 [[#include <getopt.h>
328 #include <stddef.h>
329 #include <string.h>
330 ]],
331 [[static const struct option long_options[] =
332 {
333 { "xtremely-",no_argument, NULL, 1003 },
334 { "xtra", no_argument, NULL, 1001 },
335 { "xtreme", no_argument, NULL, 1002 },
336 { "xtremely", no_argument, NULL, 1003 },
337 { NULL, 0, NULL, 0 }
338 };
339 /* This code fails on OpenBSD 5.0. */
340 {
341 static char program[] = "program";
342 static char xtremel[] = "--xtremel";
343 char *argv[] = { program, xtremel, NULL };
344 int option_index;
345 optind = 1; opterr = 0;
346 if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
347 return 1;
348 }
349 return 0;
350 ]])],
351 [gl_cv_func_getopt_long_gnu=yes],
352 [gl_cv_func_getopt_long_gnu=no],
353 [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
354 case "$host_os" in
355 openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
356 *) gl_cv_func_getopt_long_gnu="guessing yes";;
357 esac
358 ])
359 ])
360 case "$gl_cv_func_getopt_long_gnu" in
361 *yes) ;;
362 *) gl_replace_getopt=yes ;;
363 esac
9eff9fe3
PE
364 fi
365 fi
366])
367
e275c824
PE
368# emacs' configure.in uses this.
369AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
9eff9fe3 370[
e275c824
PE
371 GETOPT_H=getopt.h
372 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
373 [Define to rpl_ if the getopt replacement functions and variables
374 should be used.])
375 AC_SUBST([GETOPT_H])
9eff9fe3
PE
376])
377
9eff9fe3 378# Prerequisites of lib/getopt*.
e275c824
PE
379# emacs' configure.in uses this.
380AC_DEFUN([gl_PREREQ_GETOPT],
381[
382 AC_CHECK_DECLS_ONCE([getenv])
383])