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 POSIX 2008 does not specify leading '+' behavior, but see
78 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
79 dnl the next version of POSIX. For now, we only guarantee leading '+'
80 dnl behavior with getopt-gnu.
81 if test -z "$gl_replace_getopt"; then
82 AC_CACHE_CHECK([whether getopt is POSIX compatible],
83 [gl_cv_func_getopt_posix],
85 dnl Merging these three different test programs into a single one
86 dnl would require a reset mechanism. On BSD systems, it can be done
87 dnl through 'optreset'; on some others (glibc), it can be done by
88 dnl setting 'optind' to 0; on others again (HP-UX, IRIX, OSF/1,
89 dnl Solaris 9, musl libc), there is no such mechanism.
90 if test $cross_compiling = no; then
91 dnl Sanity check. Succeeds everywhere (except on MSVC,
92 dnl which lacks <unistd.h> and getopt() entirely).
102 static char program[] = "program";
103 static char a[] = "-a";
104 static char foo[] = "foo";
105 static char bar[] = "bar";
106 char *argv[] = { program, a, foo, bar, NULL };
109 c = getopt (4, argv, "ab");
112 c = getopt (4, argv, "ab");
120 [gl_cv_func_getopt_posix=maybe],
121 [gl_cv_func_getopt_posix=no])
122 if test $gl_cv_func_getopt_posix = maybe; then
123 dnl Sanity check with '+'. Succeeds everywhere (except on MSVC,
124 dnl which lacks <unistd.h> and getopt() entirely).
134 static char program[] = "program";
135 static char donald[] = "donald";
136 static char p[] = "-p";
137 static char billy[] = "billy";
138 static char duck[] = "duck";
139 static char a[] = "-a";
140 static char bar[] = "bar";
141 char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
144 c = getopt (7, argv, "+abp:q:");
147 if (!(strcmp (argv[0], "program") == 0))
149 if (!(strcmp (argv[1], "donald") == 0))
151 if (!(strcmp (argv[2], "-p") == 0))
153 if (!(strcmp (argv[3], "billy") == 0))
155 if (!(strcmp (argv[4], "duck") == 0))
157 if (!(strcmp (argv[5], "-a") == 0))
159 if (!(strcmp (argv[6], "bar") == 0))
166 [gl_cv_func_getopt_posix=maybe],
167 [gl_cv_func_getopt_posix=no])
169 if test $gl_cv_func_getopt_posix = maybe; then
170 dnl Detect Mac OS X 10.5, AIX 7.1, mingw bug.
180 static char program[] = "program";
181 static char ab[] = "-ab";
182 char *argv[3] = { program, ab, NULL };
183 if (getopt (2, argv, "ab:") != 'a')
185 if (getopt (2, argv, "ab:") != '?')
194 [gl_cv_func_getopt_posix=yes],
195 [gl_cv_func_getopt_posix=no])
199 darwin* | aix* | mingw*) gl_cv_func_getopt_posix="guessing no";;
200 *) gl_cv_func_getopt_posix="guessing yes";;
204 case "$gl_cv_func_getopt_posix" in
205 *no) gl_replace_getopt=yes ;;
209 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
210 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
211 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
212 # optstring is necessary for programs like m4 that have POSIX-mandated
213 # semantics for supporting options interspersed with files.
214 # Also, since getopt_long is a GNU extension, we require optind=0.
215 # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
216 # so take care to revert to the correct (non-)export state.
217 dnl GNU Coding Standards currently allow awk but not env; besides, env
218 dnl is ambiguous with environment values that contain newlines.
219 gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
220 case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
221 xx) gl_had_POSIXLY_CORRECT=exported ;;
222 x) gl_had_POSIXLY_CORRECT=yes ;;
223 *) gl_had_POSIXLY_CORRECT= ;;
226 export POSIXLY_CORRECT
228 [AC_LANG_PROGRAM([[#include <getopt.h>
237 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
238 and fails on Mac OS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
239 OSF/1 5.1, Solaris 10. */
241 static char conftest[] = "conftest";
242 static char plus[] = "-+";
243 char *argv[3] = { conftest, plus, NULL };
245 if (getopt (2, argv, "+a") != '?')
248 /* This code succeeds on glibc 2.8, mingw,
249 and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
250 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
252 static char program[] = "program";
253 static char p[] = "-p";
254 static char foo[] = "foo";
255 static char bar[] = "bar";
256 char *argv[] = { program, p, foo, bar, NULL };
259 if (getopt (4, argv, "p::") != 'p')
261 else if (optarg != NULL)
263 else if (getopt (4, argv, "p::") != -1)
265 else if (optind != 2)
268 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
270 static char program[] = "program";
271 static char foo[] = "foo";
272 static char p[] = "-p";
273 char *argv[] = { program, foo, p, NULL };
275 if (getopt (3, argv, "-p") != 1)
277 else if (getopt (3, argv, "-p") != 'p')
280 /* This code fails on glibc 2.11. */
282 static char program[] = "program";
283 static char b[] = "-b";
284 static char a[] = "-a";
285 char *argv[] = { program, b, a, NULL };
287 if (getopt (3, argv, "+:a:b") != 'b')
289 else if (getopt (3, argv, "+:a:b") != ':')
292 /* This code dumps core on glibc 2.14. */
294 static char program[] = "program";
295 static char w[] = "-W";
296 static char dummy[] = "dummy";
297 char *argv[] = { program, w, dummy, NULL };
299 if (getopt (3, argv, "W;") != 'W')
304 [gl_cv_func_getopt_gnu=yes],
305 [gl_cv_func_getopt_gnu=no],
306 [dnl Cross compiling. Assume the worst, even on glibc platforms.
307 gl_cv_func_getopt_gnu="guessing no"
309 case $gl_had_POSIXLY_CORRECT in
311 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
312 *) AS_UNSET([POSIXLY_CORRECT]) ;;
315 if test "$gl_cv_func_getopt_gnu" != yes; then
316 gl_replace_getopt=yes
318 AC_CACHE_CHECK([for working GNU getopt_long function],
319 [gl_cv_func_getopt_long_gnu],
322 [[#include <getopt.h>
326 [[static const struct option long_options[] =
328 { "xtremely-",no_argument, NULL, 1003 },
329 { "xtra", no_argument, NULL, 1001 },
330 { "xtreme", no_argument, NULL, 1002 },
331 { "xtremely", no_argument, NULL, 1003 },
334 /* This code fails on OpenBSD 5.0. */
336 static char program[] = "program";
337 static char xtremel[] = "--xtremel";
338 char *argv[] = { program, xtremel, NULL };
340 optind = 1; opterr = 0;
341 if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
346 [gl_cv_func_getopt_long_gnu=yes],
347 [gl_cv_func_getopt_long_gnu=no],
348 [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
350 openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
351 *) gl_cv_func_getopt_long_gnu="guessing yes";;
355 case "$gl_cv_func_getopt_long_gnu" in
357 *) gl_replace_getopt=yes ;;
363 # emacs' configure.in uses this.
364 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
367 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
368 [Define to rpl_ if the getopt replacement functions and variables
373 # Prerequisites of lib/getopt*.
374 # emacs' configure.in uses this.
375 AC_DEFUN([gl_PREREQ_GETOPT],
377 AC_CHECK_DECLS_ONCE([getenv])