2 dnl Copyright (C) 2002-2006, 2008-2011 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
30 # Request a POSIX compliant getopt function with GNU extensions (such as
31 # options with optional arguments) and the functions getopt_long,
33 AC_DEFUN([gl_FUNC_GETOPT_GNU],
35 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
37 AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
40 # Request the gnulib implementation of the getopt functions unconditionally.
41 AC_DEFUN([gl_REPLACE_GETOPT],
43 dnl Arrange for getopt.h to be created.
44 gl_GETOPT_SUBSTITUTE_HEADER
45 dnl Arrange for unistd.h to include getopt.h.
46 GNULIB_UNISTD_H_GETOPT=1
47 dnl Arrange to compile the getopt implementation.
53 # emacs' configure.in uses this.
54 AC_DEFUN([gl_GETOPT_IFELSE],
56 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
57 AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
60 # Determine whether to replace the entire getopt facility.
61 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
63 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
64 AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
66 dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
67 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
69 gl_CHECK_NEXT_HEADERS([getopt.h])
70 if test $ac_cv_header_getopt_h = yes; then
75 AC_SUBST([HAVE_GETOPT_H])
79 dnl Test whether <getopt.h> is available.
80 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
81 AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
84 dnl Test whether the function getopt_long is available.
85 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
86 AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
89 dnl mingw's getopt (in libmingwex.a) does weird things when the options
90 dnl strings starts with '+' and it's not the first call. Some internal state
91 dnl is left over from earlier calls, and neither setting optind = 0 nor
92 dnl setting optreset = 1 get rid of this internal state.
93 dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
94 dnl POSIX 2008 does not specify leading '+' behavior, but see
95 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
96 dnl the next version of POSIX. For now, we only guarantee leading '+'
97 dnl behavior with getopt-gnu.
98 if test -z "$gl_replace_getopt"; then
99 AC_CACHE_CHECK([whether getopt is POSIX compatible],
100 [gl_cv_func_getopt_posix],
102 dnl BSD getopt_long uses an incompatible method to reset
103 dnl option processing. Existence of the variable, in and of
104 dnl itself, is not a reason to replace getopt, but knowledge
105 dnl of the variable is needed to determine how to reset and
106 dnl whether a reset reparses the environment. Solaris
107 dnl supports neither optreset nor optind=0, but keeps no state
108 dnl that needs a reset beyond setting optind=1; detect Solaris
112 [[#include <unistd.h>]],
113 [[int *p = &optreset; return optreset;]])],
117 [[#include <getopt.h>]],
118 [[return !getopt_clip;]])],
122 dnl This test fails on mingw and succeeds on many other platforms.
123 gl_save_CPPFLAGS=$CPPFLAGS
124 CPPFLAGS="$CPPFLAGS -DOPTIND_MIN=$gl_optind_min"
125 AC_RUN_IFELSE([AC_LANG_SOURCE([[
138 argv[argc++] = "program";
140 argv[argc++] = "foo";
141 argv[argc++] = "bar";
146 c = getopt (argc, argv, "ab");
149 c = getopt (argc, argv, "ab");
155 /* Some internal state exists at this point. */
161 argv[argc++] = "program";
162 argv[argc++] = "donald";
164 argv[argc++] = "billy";
165 argv[argc++] = "duck";
167 argv[argc++] = "bar";
172 c = getopt (argc, argv, "+abp:q:");
175 if (!(strcmp (argv[0], "program") == 0))
177 if (!(strcmp (argv[1], "donald") == 0))
179 if (!(strcmp (argv[2], "-p") == 0))
181 if (!(strcmp (argv[3], "billy") == 0))
183 if (!(strcmp (argv[4], "duck") == 0))
185 if (!(strcmp (argv[5], "-a") == 0))
187 if (!(strcmp (argv[6], "bar") == 0))
192 /* Detect MacOS 10.5, AIX 7.1 bug. */
194 char *argv[3] = { "program", "-ab", NULL };
197 if (getopt (2, argv, "ab:") != 'a')
199 if (getopt (2, argv, "ab:") != '?')
210 [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
212 mingw*) gl_cv_func_getopt_posix="guessing no";;
213 darwin* | aix*) gl_cv_func_getopt_posix="guessing no";;
214 *) gl_cv_func_getopt_posix="guessing yes";;
217 CPPFLAGS=$gl_save_CPPFLAGS
219 case "$gl_cv_func_getopt_posix" in
220 *no) gl_replace_getopt=yes ;;
224 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
225 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
226 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
227 # optstring is necessary for programs like m4 that have POSIX-mandated
228 # semantics for supporting options interspersed with files.
229 # Also, since getopt_long is a GNU extension, we require optind=0.
230 # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
231 # so take care to revert to the correct (non-)export state.
232 dnl GNU Coding Standards currently allow awk but not env; besides, env
233 dnl is ambiguous with environment values that contain newlines.
234 gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
235 case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
236 xx) gl_had_POSIXLY_CORRECT=exported ;;
237 x) gl_had_POSIXLY_CORRECT=yes ;;
238 *) gl_had_POSIXLY_CORRECT= ;;
241 export POSIXLY_CORRECT
243 [AC_LANG_PROGRAM([[#include <getopt.h>
248 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
249 and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
250 OSF/1 5.1, Solaris 10. */
253 myargv[0] = "conftest";
257 if (getopt (2, myargv, "+a") != '?')
260 /* This code succeeds on glibc 2.8, mingw,
261 and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
262 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
264 char *argv[] = { "program", "-p", "foo", "bar", NULL };
267 if (getopt (4, argv, "p::") != 'p')
269 else if (optarg != NULL)
271 else if (getopt (4, argv, "p::") != -1)
273 else if (optind != 2)
276 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
278 char *argv[] = { "program", "foo", "-p", NULL };
280 if (getopt (3, argv, "-p") != 1)
282 else if (getopt (3, argv, "-p") != 'p')
285 /* This code fails on glibc 2.11. */
287 char *argv[] = { "program", "-b", "-a", NULL };
289 if (getopt (3, argv, "+:a:b") != 'b')
291 else if (getopt (3, argv, "+:a:b") != ':')
296 [gl_cv_func_getopt_gnu=yes],
297 [gl_cv_func_getopt_gnu=no],
298 [dnl Cross compiling. Guess based on host and declarations.
299 case $host_os:$ac_cv_have_decl_optreset in
300 *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
301 *:yes) gl_cv_func_getopt_gnu=no;;
302 *) gl_cv_func_getopt_gnu=yes;;
305 case $gl_had_POSIXLY_CORRECT in
307 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
308 *) AS_UNSET([POSIXLY_CORRECT]) ;;
311 if test "$gl_cv_func_getopt_gnu" = "no"; then
312 gl_replace_getopt=yes
317 # emacs' configure.in uses this.
318 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
321 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
322 [Define to rpl_ if the getopt replacement functions and variables
327 # Prerequisites of lib/getopt*.
328 # emacs' configure.in uses this.
329 AC_DEFUN([gl_PREREQ_GETOPT],
331 AC_CHECK_DECLS_ONCE([getenv])