2 dnl Copyright (C) 2002-2006, 2008-2018 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 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
13 dnl Other modules can request the gnulib implementation of the getopt
14 dnl functions unconditionally, by defining gl_REPLACE_GETOPT_ALWAYS.
15 dnl argp.m4 does this.
16 m4_ifdef([gl_REPLACE_GETOPT_ALWAYS], [
20 if test -n "$gl_replace_getopt"; then
24 if test $REPLACE_GETOPT = 1; then
25 dnl Arrange for getopt.h to be created.
26 gl_GETOPT_SUBSTITUTE_HEADER
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 dnl Set the variable gl_getopt_required, so that all invocations of
36 dnl gl_GETOPT_CHECK_HEADERS in the scope of the current configure file
37 dnl will check for getopt with GNU extensions.
38 dnl This means that if one gnulib-tool invocation requests getopt-posix
39 dnl and another gnulib-tool invocation requests getopt-gnu, it is as if
40 dnl both had requested getopt-gnu.
41 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
43 dnl No need to invoke gl_FUNC_GETOPT_POSIX here; this is automatically
44 dnl done through the module dependency getopt-gnu -> getopt-posix.
47 # Determine whether to replace the entire getopt facility.
48 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
50 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
51 AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
53 dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
54 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
56 gl_CHECK_NEXT_HEADERS([getopt.h])
57 if test $ac_cv_header_getopt_h = yes; then
62 AC_SUBST([HAVE_GETOPT_H])
66 dnl Test whether <getopt.h> is available.
67 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
68 AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
71 dnl Test whether the function getopt_long is available.
72 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
73 AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
76 dnl POSIX 2008 does not specify leading '+' behavior, but see
77 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
78 dnl the next version of POSIX. For now, we only guarantee leading '+'
79 dnl behavior with getopt-gnu.
80 if test -z "$gl_replace_getopt"; then
81 AC_CACHE_CHECK([whether getopt is POSIX compatible],
82 [gl_cv_func_getopt_posix],
84 dnl Merging these three different test programs into a single one
85 dnl would require a reset mechanism. On BSD systems, it can be done
86 dnl through 'optreset'; on some others (glibc), it can be done by
87 dnl setting 'optind' to 0; on others again (HP-UX, IRIX, OSF/1,
88 dnl Solaris 9, musl libc), there is no such mechanism.
89 if test $cross_compiling = no; then
90 dnl Sanity check. Succeeds everywhere (except on MSVC,
91 dnl which lacks <unistd.h> and getopt() entirely).
101 static char program[] = "program";
102 static char a[] = "-a";
103 static char foo[] = "foo";
104 static char bar[] = "bar";
105 char *argv[] = { program, a, foo, bar, NULL };
108 c = getopt (4, argv, "ab");
111 c = getopt (4, argv, "ab");
119 [gl_cv_func_getopt_posix=maybe],
120 [gl_cv_func_getopt_posix=no])
121 if test $gl_cv_func_getopt_posix = maybe; then
122 dnl Sanity check with '+'. Succeeds everywhere (except on MSVC,
123 dnl which lacks <unistd.h> and getopt() entirely).
133 static char program[] = "program";
134 static char donald[] = "donald";
135 static char p[] = "-p";
136 static char billy[] = "billy";
137 static char duck[] = "duck";
138 static char a[] = "-a";
139 static char bar[] = "bar";
140 char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
143 c = getopt (7, argv, "+abp:q:");
146 if (!(strcmp (argv[0], "program") == 0))
148 if (!(strcmp (argv[1], "donald") == 0))
150 if (!(strcmp (argv[2], "-p") == 0))
152 if (!(strcmp (argv[3], "billy") == 0))
154 if (!(strcmp (argv[4], "duck") == 0))
156 if (!(strcmp (argv[5], "-a") == 0))
158 if (!(strcmp (argv[6], "bar") == 0))
165 [gl_cv_func_getopt_posix=maybe],
166 [gl_cv_func_getopt_posix=no])
168 if test $gl_cv_func_getopt_posix = maybe; then
169 dnl Detect Mac OS X 10.5, AIX 7.1, mingw bug.
179 static char program[] = "program";
180 static char ab[] = "-ab";
181 char *argv[3] = { program, ab, NULL };
182 if (getopt (2, argv, "ab:") != 'a')
184 if (getopt (2, argv, "ab:") != '?')
193 [gl_cv_func_getopt_posix=yes],
194 [gl_cv_func_getopt_posix=no])
198 darwin* | aix* | mingw*) gl_cv_func_getopt_posix="guessing no";;
199 *) gl_cv_func_getopt_posix="guessing yes";;
203 case "$gl_cv_func_getopt_posix" in
204 *no) gl_replace_getopt=yes ;;
208 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
209 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
210 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
211 # optstring is necessary for programs like m4 that have POSIX-mandated
212 # semantics for supporting options interspersed with files.
213 # Also, since getopt_long is a GNU extension, we require optind=0.
214 # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
215 # so take care to revert to the correct (non-)export state.
216 dnl GNU Coding Standards currently allow awk but not env; besides, env
217 dnl is ambiguous with environment values that contain newlines.
218 gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
219 case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
220 xx) gl_had_POSIXLY_CORRECT=exported ;;
221 x) gl_had_POSIXLY_CORRECT=yes ;;
222 *) gl_had_POSIXLY_CORRECT= ;;
225 export POSIXLY_CORRECT
227 [AC_LANG_PROGRAM([[#include <getopt.h>
236 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
237 and fails on Mac OS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
238 OSF/1 5.1, Solaris 10. */
240 static char conftest[] = "conftest";
241 static char plus[] = "-+";
242 char *argv[3] = { conftest, plus, NULL };
244 if (getopt (2, argv, "+a") != '?')
247 /* This code succeeds on glibc 2.8, mingw,
248 and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
249 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
251 static char program[] = "program";
252 static char p[] = "-p";
253 static char foo[] = "foo";
254 static char bar[] = "bar";
255 char *argv[] = { program, p, foo, bar, NULL };
258 if (getopt (4, argv, "p::") != 'p')
260 else if (optarg != NULL)
262 else if (getopt (4, argv, "p::") != -1)
264 else if (optind != 2)
267 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
269 static char program[] = "program";
270 static char foo[] = "foo";
271 static char p[] = "-p";
272 char *argv[] = { program, foo, p, NULL };
274 if (getopt (3, argv, "-p") != 1)
276 else if (getopt (3, argv, "-p") != 'p')
279 /* This code fails on glibc 2.11. */
281 static char program[] = "program";
282 static char b[] = "-b";
283 static char a[] = "-a";
284 char *argv[] = { program, b, a, NULL };
286 if (getopt (3, argv, "+:a:b") != 'b')
288 else if (getopt (3, argv, "+:a:b") != ':')
291 /* This code dumps core on glibc 2.14. */
293 static char program[] = "program";
294 static char w[] = "-W";
295 static char dummy[] = "dummy";
296 char *argv[] = { program, w, dummy, NULL };
298 if (getopt (3, argv, "W;") != 'W')
303 [gl_cv_func_getopt_gnu=yes],
304 [gl_cv_func_getopt_gnu=no],
305 [dnl Cross compiling. Assume the worst, even on glibc platforms.
306 gl_cv_func_getopt_gnu="guessing no"
308 case $gl_had_POSIXLY_CORRECT in
310 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
311 *) AS_UNSET([POSIXLY_CORRECT]) ;;
314 if test "$gl_cv_func_getopt_gnu" != yes; then
315 gl_replace_getopt=yes
317 AC_CACHE_CHECK([for working GNU getopt_long function],
318 [gl_cv_func_getopt_long_gnu],
321 [[#include <getopt.h>
325 [[static const struct option long_options[] =
327 { "xtremely-",no_argument, NULL, 1003 },
328 { "xtra", no_argument, NULL, 1001 },
329 { "xtreme", no_argument, NULL, 1002 },
330 { "xtremely", no_argument, NULL, 1003 },
333 /* This code fails on OpenBSD 5.0. */
335 static char program[] = "program";
336 static char xtremel[] = "--xtremel";
337 char *argv[] = { program, xtremel, NULL };
339 optind = 1; opterr = 0;
340 if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
345 [gl_cv_func_getopt_long_gnu=yes],
346 [gl_cv_func_getopt_long_gnu=no],
347 [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
349 openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
350 *) gl_cv_func_getopt_long_gnu="guessing yes";;
354 case "$gl_cv_func_getopt_long_gnu" in
356 *) gl_replace_getopt=yes ;;
362 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
364 AC_CHECK_HEADERS_ONCE([sys/cdefs.h])
365 if test $ac_cv_header_sys_cdefs_h = yes; then
370 AC_SUBST([HAVE_SYS_CDEFS_H])
372 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
373 [Define to rpl_ if the getopt replacement functions and variables
376 GETOPT_CDEFS_H=getopt-cdefs.h
378 AC_SUBST([GETOPT_CDEFS_H])