2
dnl Copyright (C) 2002-2006, 2008-2010 Free Software Foundation, Inc.
2
dnl Copyright (C) 2002-2006, 2008-2011 Free Software Foundation, Inc.
3
3
dnl This file is free software; the Free Software Foundation
4
4
dnl gives unlimited permission to copy and/or distribute it,
5
5
dnl with or without modifications, as long as this notice is preserved.
50
50
AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
52
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.
55
56
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
57
58
gl_CHECK_NEXT_HEADERS([getopt.h])
58
AC_CHECK_HEADERS_ONCE([getopt.h])
59
59
if test $ac_cv_header_getopt_h = yes; then
75
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
78
dnl mingw's getopt (in libmingwex.a) does weird things when the options
88
79
dnl strings starts with '+' and it's not the first call. Some internal state
89
80
dnl is left over from earlier calls, and neither setting optind = 0 nor
90
81
dnl setting optreset = 1 get rid of this internal state.
91
82
dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
83
dnl POSIX 2008 does not specify leading '+' behavior, but see
84
dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
85
dnl the next version of POSIX. For now, we only guarantee leading '+'
86
dnl behavior with getopt-gnu.
92
87
if test -z "$gl_replace_getopt"; then
93
88
AC_CACHE_CHECK([whether getopt is POSIX compatible],
94
89
[gl_cv_func_getopt_posix],
96
dnl This test fails on mingw and succeeds on all other platforms.
91
dnl BSD getopt_long uses an incompatible method to reset
92
dnl option processing. Existence of the variable, in and of
93
dnl itself, is not a reason to replace getopt, but knowledge
94
dnl of the variable is needed to determine how to reset and
95
dnl whether a reset reparses the environment. Solaris
96
dnl supports neither optreset nor optind=0, but keeps no state
97
dnl that needs a reset beyond setting optind=1; detect Solaris
101
[[#include <unistd.h>]],
102
[[int *p = &optreset; return optreset;]])],
106
[[#include <getopt.h>]],
107
[[return !getopt_clip;]])],
111
dnl This test fails on mingw and succeeds on many other platforms.
112
gl_save_CPPFLAGS=$CPPFLAGS
113
CPPFLAGS="$CPPFLAGS -DOPTIND_MIN=$gl_optind_min"
97
114
AC_RUN_IFELSE([AC_LANG_SOURCE([[
98
115
#include <unistd.h>
99
116
#include <stdlib.h>
100
117
#include <string.h>
102
#if !HAVE_DECL_OPTRESET
103
# define OPTIND_MIN 0
105
# define OPTIND_MIN 1
167
178
if (!(optind == 1))
181
/* Detect MacOS 10.5, AIX 7.1 bug. */
183
char *argv[3] = { "program", "-ab", NULL };
186
if (getopt (2, argv, "ab:") != 'a')
188
if (getopt (2, argv, "ab:") != '?')
174
199
[gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
175
200
[case "$host_os" in
176
mingw*) gl_cv_func_getopt_posix="guessing no";;
177
*) gl_cv_func_getopt_posix="guessing yes";;
201
mingw*) gl_cv_func_getopt_posix="guessing no";;
202
darwin* | aix*) gl_cv_func_getopt_posix="guessing no";;
203
*) gl_cv_func_getopt_posix="guessing yes";;
206
CPPFLAGS=$gl_save_CPPFLAGS
181
208
case "$gl_cv_func_getopt_posix" in
182
209
*no) gl_replace_getopt=yes ;;
189
216
# optstring is necessary for programs like m4 that have POSIX-mandated
190
217
# semantics for supporting options interspersed with files.
191
218
# Also, since getopt_long is a GNU extension, we require optind=0.
192
gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes}
219
# Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
220
# so take care to revert to the correct (non-)export state.
221
dnl GNU Coding Standards currently allow awk but not env; besides, env
222
dnl is ambiguous with environment values that contain newlines.
223
gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
224
case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
225
xx) gl_had_POSIXLY_CORRECT=exported ;;
226
x) gl_had_POSIXLY_CORRECT=yes ;;
227
*) gl_had_POSIXLY_CORRECT= ;;
193
229
POSIXLY_CORRECT=1
194
230
export POSIXLY_CORRECT
197
233
#include <stddef.h>
198
234
#include <string.h>
200
237
/* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
201
238
and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
202
239
OSF/1 5.1, Solaris 10. */
209
246
if (getopt (2, myargv, "+a") != '?')
212
249
/* This code succeeds on glibc 2.8, mingw,
213
250
and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
219
256
if (getopt (4, argv, "p::") != 'p')
223
if (getopt (4, argv, "p::") != -1)
258
else if (optarg != NULL)
260
else if (getopt (4, argv, "p::") != -1)
262
else if (optind != 2)
228
265
/* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
230
267
char *argv[] = { "program", "foo", "-p", NULL };
232
269
if (getopt (3, argv, "-p") != 1)
234
if (getopt (3, argv, "-p") != 'p')
271
else if (getopt (3, argv, "-p") != 'p')
274
/* This code fails on glibc 2.11. */
276
char *argv[] = { "program", "-b", "-a", NULL };
278
if (getopt (3, argv, "+:a:b") != 'b')
280
else if (getopt (3, argv, "+:a:b") != ':')
239
285
[gl_cv_func_getopt_gnu=yes],
240
286
[gl_cv_func_getopt_gnu=no],
245
291
*) gl_cv_func_getopt_gnu=yes;;
248
if test "$gl_had_POSIXLY_CORRECT" != yes; then
249
AS_UNSET([POSIXLY_CORRECT])
294
case $gl_had_POSIXLY_CORRECT in
296
yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
297
*) AS_UNSET([POSIXLY_CORRECT]) ;;
252
300
if test "$gl_cv_func_getopt_gnu" = "no"; then
253
301
gl_replace_getopt=yes