2 dnl Copyright (C) 2002-2006, 2008-2009 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])
18 # Request a POSIX compliant getopt function with GNU extensions (such as
19 # options with optional arguments) and the functions getopt_long,
21 AC_DEFUN([gl_FUNC_GETOPT_GNU],
23 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
25 AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
28 # Request the gnulib implementation of the getopt functions unconditionally.
30 AC_DEFUN([gl_REPLACE_GETOPT],
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.
42 # emacs' configure.in uses this.
43 AC_DEFUN([gl_GETOPT_IFELSE],
45 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
46 AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
49 # Determine whether to replace the entire getopt facility.
50 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
52 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
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 AC_CHECK_HEADERS_ONCE([getopt.h])
59 if test $ac_cv_header_getopt_h = yes; then
64 AC_SUBST([HAVE_GETOPT_H])
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])
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])
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 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
83 AC_CHECK_DECLS([optreset], [], [],
84 [[#include <getopt.h>]])
87 dnl mingw's getopt (in libmingwex.a) does weird things when the options
88 dnl strings starts with '+' and it's not the first call. Some internal state
89 dnl is left over from earlier calls, and neither setting optind = 0 nor
90 dnl setting optreset = 1 get rid of this internal state.
91 if test -z "$gl_replace_getopt"; then
92 AC_CACHE_CHECK([whether getopt is POSIX compatible],
93 [gl_cv_func_getopt_posix],
95 dnl This test fails on mingw and succeeds on all other platforms.
96 AC_RUN_IFELSE([AC_LANG_SOURCE([[
101 #if !HAVE_DECL_OPTRESET
102 # define OPTIND_MIN 0
104 # define OPTIND_MIN 1
115 argv[argc++] = "program";
117 argv[argc++] = "foo";
118 argv[argc++] = "bar";
123 c = getopt (argc, argv, "ab");
126 c = getopt (argc, argv, "ab");
132 /* Some internal state exists at this point. */
138 argv[argc++] = "program";
139 argv[argc++] = "donald";
141 argv[argc++] = "billy";
142 argv[argc++] = "duck";
144 argv[argc++] = "bar";
149 c = getopt (argc, argv, "+abp:q:");
152 if (!(strcmp (argv[0], "program") == 0))
154 if (!(strcmp (argv[1], "donald") == 0))
156 if (!(strcmp (argv[2], "-p") == 0))
158 if (!(strcmp (argv[3], "billy") == 0))
160 if (!(strcmp (argv[4], "duck") == 0))
162 if (!(strcmp (argv[5], "-a") == 0))
164 if (!(strcmp (argv[6], "bar") == 0))
173 [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
175 mingw*) gl_cv_func_getopt_posix="guessing no";;
176 *) gl_cv_func_getopt_posix="guessing yes";;
180 case "$gl_cv_func_getopt_posix" in
181 *no) gl_replace_getopt=yes ;;
185 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
186 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
187 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
188 # optstring is necessary for programs like m4 that have POSIX-mandated
189 # semantics for supporting options interspersed with files.
190 gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes}
192 export POSIXLY_CORRECT
194 [AC_LANG_PROGRAM([[#include <getopt.h>
197 #if !HAVE_DECL_OPTRESET
198 # define OPTIND_MIN 0
200 # define OPTIND_MIN (optreset = 1)
203 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
204 and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
205 OSF/1 5.1, Solaris 10. */
208 myargv[0] = "conftest";
212 if (getopt (2, myargv, "+a") != '?')
215 /* This code succeeds on glibc 2.8, mingw,
216 and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
217 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
219 char *argv[] = { "program", "-p", "foo", "bar", NULL };
222 if (getopt (4, argv, "p::") != 'p')
226 if (getopt (4, argv, "p::") != -1)
231 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
233 char *argv[] = { "program", "foo", "-p", NULL };
235 if (getopt (3, argv, "-p") != 1)
237 if (getopt (3, argv, "-p") != 'p')
242 [gl_cv_func_getopt_gnu=yes],
243 [gl_cv_func_getopt_gnu=no],
244 [dnl Cross compiling. Guess based on host and declarations.
245 case $host_os:$ac_cv_have_decl_optreset in
246 *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
247 *:yes) gl_cv_func_getopt_gnu=no;;
248 *) gl_cv_func_getopt_gnu=yes;;
251 if test "$gl_had_POSIXLY_CORRECT" != yes; then
252 AS_UNSET([POSIXLY_CORRECT])
255 if test "$gl_cv_func_getopt_gnu" = "no"; then
256 gl_replace_getopt=yes
261 # emacs' configure.in uses this.
262 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
265 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
266 [Define to rpl_ if the getopt replacement functions and variables
271 # Prerequisites of lib/getopt*.
272 # emacs' configure.in uses this.
273 AC_DEFUN([gl_PREREQ_GETOPT],
275 AC_CHECK_DECLS_ONCE([getenv])