2 dnl Copyright (C) 2002-2006, 2008-2012 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 # Request a POSIX compliant getopt function.
8 AC_DEFUN([gl_FUNC_GETOPT_POSIX],
10 m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
11 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
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([], [])
25 if test $REPLACE_GETOPT = 1; then
26 dnl Arrange for getopt.h to be created.
27 gl_GETOPT_SUBSTITUTE_HEADER
31 # Request a POSIX compliant getopt function with GNU extensions (such as
32 # options with optional arguments) and the functions getopt_long,
34 AC_DEFUN([gl_FUNC_GETOPT_GNU],
36 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
38 AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
41 # emacs' configure.in uses this.
42 AC_DEFUN([gl_GETOPT_IFELSE],
44 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
45 AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
48 # Determine whether to replace the entire getopt facility.
49 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
51 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
52 AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
54 dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
55 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
57 gl_CHECK_NEXT_HEADERS([getopt.h])
58 if test $ac_cv_header_getopt_h = yes; then
63 AC_SUBST([HAVE_GETOPT_H])
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])
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])
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],
90 dnl BSD getopt_long uses an incompatible method to reset option
91 dnl processing. Existence of the optreset variable, in and of
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
100 [[#include <unistd.h>]],
101 [[int *p = &optreset; return optreset;]])],
105 [[#include <getopt.h>]],
106 [[return !getopt_clip;]])],
110 dnl This test fails on mingw and succeeds on many other platforms.
111 gl_save_CPPFLAGS=$CPPFLAGS
112 CPPFLAGS="$CPPFLAGS -DOPTIND_MIN=$gl_optind_min"
113 AC_RUN_IFELSE([AC_LANG_SOURCE([[
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 };
132 c = getopt (4, argv, "ab");
135 c = getopt (4, argv, "ab");
141 /* Some internal state exists at this point. */
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 };
156 c = getopt (7, argv, "+abp:q:");
159 if (!(strcmp (argv[0], "program") == 0))
161 if (!(strcmp (argv[1], "donald") == 0))
163 if (!(strcmp (argv[2], "-p") == 0))
165 if (!(strcmp (argv[3], "billy") == 0))
167 if (!(strcmp (argv[4], "duck") == 0))
169 if (!(strcmp (argv[5], "-a") == 0))
171 if (!(strcmp (argv[6], "bar") == 0))
176 /* Detect MacOS 10.5, AIX 7.1 bug. */
178 static char program[] = "program";
179 static char ab[] = "-ab";
180 char *argv[3] = { program, ab, NULL };
183 if (getopt (2, argv, "ab:") != 'a')
185 if (getopt (2, argv, "ab:") != '?')
196 [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
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";;
203 CPPFLAGS=$gl_save_CPPFLAGS
205 case "$gl_cv_func_getopt_posix" in
206 *no) gl_replace_getopt=yes ;;
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.
218 dnl GNU Coding Standards currently allow awk but not env; besides, env
219 dnl 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= ;;
227 export POSIXLY_CORRECT
229 [AC_LANG_PROGRAM([[#include <getopt.h>
238 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
239 and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
240 OSF/1 5.1, Solaris 10. */
242 static char conftest[] = "conftest";
243 static char plus[] = "-+";
244 char *argv[3] = { conftest, plus, NULL };
246 if (getopt (2, argv, "+a") != '?')
249 /* This code succeeds on glibc 2.8, mingw,
250 and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
251 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
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 };
260 if (getopt (4, argv, "p::") != 'p')
262 else if (optarg != NULL)
264 else if (getopt (4, argv, "p::") != -1)
266 else if (optind != 2)
269 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
271 static char program[] = "program";
272 static char foo[] = "foo";
273 static char p[] = "-p";
274 char *argv[] = { program, foo, p, NULL };
276 if (getopt (3, argv, "-p") != 1)
278 else if (getopt (3, argv, "-p") != 'p')
281 /* This code fails on glibc 2.11. */
283 static char program[] = "program";
284 static char b[] = "-b";
285 static char a[] = "-a";
286 char *argv[] = { program, b, a, NULL };
288 if (getopt (3, argv, "+:a:b") != 'b')
290 else if (getopt (3, argv, "+:a:b") != ':')
293 /* This code dumps core on glibc 2.14. */
295 static char program[] = "program";
296 static char w[] = "-W";
297 static char dummy[] = "dummy";
298 char *argv[] = { program, w, dummy, NULL };
300 if (getopt (3, argv, "W;") != 'W')
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;;
314 case $gl_had_POSIXLY_CORRECT in
316 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
317 *) AS_UNSET([POSIXLY_CORRECT]) ;;
320 if test "$gl_cv_func_getopt_gnu" = "no"; then
321 gl_replace_getopt=yes
326 # emacs' configure.in uses this.
327 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
330 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
331 [Define to rpl_ if the getopt replacement functions and variables
336 # Prerequisites of lib/getopt*.
337 # emacs' configure.in uses this.
338 AC_DEFUN([gl_PREREQ_GETOPT],
340 AC_CHECK_DECLS_ONCE([getenv])