2N/A# getopt.m4 serial 31
2N/Adnl Copyright (C) 2002-2006, 2008-2010 Free Software Foundation, Inc.
2N/Adnl This file is free software; the Free Software Foundation
2N/Adnl gives unlimited permission to copy and/or distribute it,
2N/Adnl with or without modifications, as long as this notice is preserved.
2N/A
2N/A# Request a POSIX compliant getopt function.
2N/AAC_DEFUN([gl_FUNC_GETOPT_POSIX],
2N/A[
2N/A m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
2N/A AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
2N/A gl_GETOPT_IFELSE([
2N/A gl_REPLACE_GETOPT
2N/A ],
2N/A [])
2N/A])
2N/A
2N/A# Request a POSIX compliant getopt function with GNU extensions (such as
2N/A# options with optional arguments) and the functions getopt_long,
2N/A# getopt_long_only.
2N/AAC_DEFUN([gl_FUNC_GETOPT_GNU],
2N/A[
2N/A m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
2N/A
2N/A AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
2N/A])
2N/A
2N/A# Request the gnulib implementation of the getopt functions unconditionally.
2N/A# argp.m4 uses this.
2N/AAC_DEFUN([gl_REPLACE_GETOPT],
2N/A[
2N/A dnl Arrange for getopt.h to be created.
2N/A gl_GETOPT_SUBSTITUTE_HEADER
2N/A dnl Arrange for unistd.h to include getopt.h.
2N/A GNULIB_UNISTD_H_GETOPT=1
2N/A dnl Arrange to compile the getopt implementation.
2N/A AC_LIBOBJ([getopt])
2N/A AC_LIBOBJ([getopt1])
2N/A gl_PREREQ_GETOPT
2N/A])
2N/A
2N/A# emacs' configure.in uses this.
2N/AAC_DEFUN([gl_GETOPT_IFELSE],
2N/A[
2N/A AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
2N/A AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
2N/A])
2N/A
2N/A# Determine whether to replace the entire getopt facility.
2N/AAC_DEFUN([gl_GETOPT_CHECK_HEADERS],
2N/A[
2N/A AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
2N/A AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
2N/A
2N/A dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
2N/A AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
2N/A
2N/A gl_CHECK_NEXT_HEADERS([getopt.h])
2N/A AC_CHECK_HEADERS_ONCE([getopt.h])
2N/A if test $ac_cv_header_getopt_h = yes; then
2N/A HAVE_GETOPT_H=1
2N/A else
2N/A HAVE_GETOPT_H=0
2N/A fi
2N/A AC_SUBST([HAVE_GETOPT_H])
2N/A
2N/A gl_replace_getopt=
2N/A
2N/A dnl Test whether <getopt.h> is available.
2N/A if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
2N/A AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
2N/A fi
2N/A
2N/A dnl Test whether the function getopt_long is available.
2N/A if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
2N/A AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
2N/A fi
2N/A
2N/A dnl BSD getopt_long uses an incompatible method to reset option processing.
2N/A dnl Existence of the variable, in and of itself, is not a reason to replace
2N/A dnl getopt, but knowledge of the variable is needed to determine how to
2N/A dnl reset and whether a reset reparses the environment.
2N/A dnl Solaris supports neither optreset nor optind=0, but keeps no state that
2N/A dnl needs a reset beyond setting optind=1; detect Solaris by getopt_clip.
2N/A if test -z "$gl_replace_getopt"; then
2N/A AC_CHECK_DECLS([optreset], [],
2N/A [AC_CHECK_DECLS([getopt_clip], [], [],
2N/A [[#include <getopt.h>]])
2N/A ],
2N/A [[#include <getopt.h>]])
2N/A fi
2N/A
2N/A dnl mingw's getopt (in libmingwex.a) does weird things when the options
2N/A dnl strings starts with '+' and it's not the first call. Some internal state
2N/A dnl is left over from earlier calls, and neither setting optind = 0 nor
2N/A dnl setting optreset = 1 get rid of this internal state.
2N/A dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
2N/A dnl POSIX 2008 does not specify leading '+' behavior, but see
2N/A dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
2N/A dnl the next version of POSIX. For now, we only guarantee leading '+'
2N/A dnl behavior with getopt-gnu.
2N/A if test -z "$gl_replace_getopt"; then
2N/A AC_CACHE_CHECK([whether getopt is POSIX compatible],
2N/A [gl_cv_func_getopt_posix],
2N/A [
2N/A dnl This test fails on mingw and succeeds on many other platforms.
2N/A AC_RUN_IFELSE([AC_LANG_SOURCE([[
2N/A#include <unistd.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/A#if !HAVE_DECL_OPTRESET && !HAVE_DECL_GETOPT_CLIP
2N/A# define OPTIND_MIN 0
2N/A#else
2N/A# define OPTIND_MIN 1
2N/A#endif
2N/A
2N/Aint
2N/Amain ()
2N/A{
2N/A {
2N/A int argc = 0;
2N/A char *argv[10];
2N/A int c;
2N/A
2N/A argv[argc++] = "program";
2N/A argv[argc++] = "-a";
2N/A argv[argc++] = "foo";
2N/A argv[argc++] = "bar";
2N/A argv[argc] = NULL;
2N/A optind = OPTIND_MIN;
2N/A opterr = 0;
2N/A
2N/A c = getopt (argc, argv, "ab");
2N/A if (!(c == 'a'))
2N/A return 1;
2N/A c = getopt (argc, argv, "ab");
2N/A if (!(c == -1))
2N/A return 2;
2N/A if (!(optind == 2))
2N/A return 3;
2N/A }
2N/A /* Some internal state exists at this point. */
2N/A {
2N/A int argc = 0;
2N/A char *argv[10];
2N/A int c;
2N/A
2N/A argv[argc++] = "program";
2N/A argv[argc++] = "donald";
2N/A argv[argc++] = "-p";
2N/A argv[argc++] = "billy";
2N/A argv[argc++] = "duck";
2N/A argv[argc++] = "-a";
2N/A argv[argc++] = "bar";
2N/A argv[argc] = NULL;
2N/A optind = OPTIND_MIN;
2N/A opterr = 0;
2N/A
2N/A c = getopt (argc, argv, "+abp:q:");
2N/A if (!(c == -1))
2N/A return 4;
2N/A if (!(strcmp (argv[0], "program") == 0))
2N/A return 5;
2N/A if (!(strcmp (argv[1], "donald") == 0))
2N/A return 6;
2N/A if (!(strcmp (argv[2], "-p") == 0))
2N/A return 7;
2N/A if (!(strcmp (argv[3], "billy") == 0))
2N/A return 8;
2N/A if (!(strcmp (argv[4], "duck") == 0))
2N/A return 9;
2N/A if (!(strcmp (argv[5], "-a") == 0))
2N/A return 10;
2N/A if (!(strcmp (argv[6], "bar") == 0))
2N/A return 11;
2N/A if (!(optind == 1))
2N/A return 12;
2N/A }
2N/A /* Detect MacOS 10.5, AIX 7.1 bug. */
2N/A {
2N/A char *argv[3] = { "program", "-ab", NULL };
2N/A optind = OPTIND_MIN;
2N/A opterr = 0;
2N/A if (getopt (2, argv, "ab:") != 'a')
2N/A return 13;
2N/A if (getopt (2, argv, "ab:") != '?')
2N/A return 14;
2N/A if (optopt != 'b')
2N/A return 15;
2N/A if (optind != 2)
2N/A return 16;
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A]])],
2N/A [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
2N/A [case "$host_os" in
2N/A mingw*) gl_cv_func_getopt_posix="guessing no";;
2N/A darwin* | aix*) gl_cv_func_getopt_posix="guessing no";;
2N/A *) gl_cv_func_getopt_posix="guessing yes";;
2N/A esac
2N/A ])
2N/A ])
2N/A case "$gl_cv_func_getopt_posix" in
2N/A *no) gl_replace_getopt=yes ;;
2N/A esac
2N/A fi
2N/A
2N/A if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
2N/A AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
2N/A [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
2N/A # optstring is necessary for programs like m4 that have POSIX-mandated
2N/A # semantics for supporting options interspersed with files.
2N/A # Also, since getopt_long is a GNU extension, we require optind=0.
2N/A # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
2N/A # so take care to revert to the correct (non-)export state.
2N/Adnl GNU Coding Standards currently allow awk but not env; besides, env
2N/Adnl is ambiguous with environment values that contain newlines.
2N/A gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
2N/A case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
2N/A xx) gl_had_POSIXLY_CORRECT=exported ;;
2N/A x) gl_had_POSIXLY_CORRECT=yes ;;
2N/A *) gl_had_POSIXLY_CORRECT= ;;
2N/A esac
2N/A POSIXLY_CORRECT=1
2N/A export POSIXLY_CORRECT
2N/A AC_RUN_IFELSE(
2N/A [AC_LANG_PROGRAM([[#include <getopt.h>
2N/A #include <stddef.h>
2N/A #include <string.h>
2N/A ]], [[
2N/A /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
2N/A and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
2N/A OSF/1 5.1, Solaris 10. */
2N/A {
2N/A char *myargv[3];
2N/A myargv[0] = "conftest";
2N/A myargv[1] = "-+";
2N/A myargv[2] = 0;
2N/A opterr = 0;
2N/A if (getopt (2, myargv, "+a") != '?')
2N/A return 1;
2N/A }
2N/A /* This code succeeds on glibc 2.8, mingw,
2N/A and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
2N/A IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
2N/A {
2N/A char *argv[] = { "program", "-p", "foo", "bar", NULL };
2N/A
2N/A optind = 1;
2N/A if (getopt (4, argv, "p::") != 'p')
2N/A return 2;
2N/A if (optarg != NULL)
2N/A return 3;
2N/A if (getopt (4, argv, "p::") != -1)
2N/A return 4;
2N/A if (optind != 2)
2N/A return 5;
2N/A }
2N/A /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
2N/A {
2N/A char *argv[] = { "program", "foo", "-p", NULL };
2N/A optind = 0;
2N/A if (getopt (3, argv, "-p") != 1)
2N/A return 6;
2N/A if (getopt (3, argv, "-p") != 'p')
2N/A return 7;
2N/A }
2N/A /* This code fails on glibc 2.11. */
2N/A {
2N/A char *argv[] = { "program", "-b", "-a", NULL };
2N/A optind = opterr = 0;
2N/A if (getopt (3, argv, "+:a:b") != 'b')
2N/A return 8;
2N/A if (getopt (3, argv, "+:a:b") != ':')
2N/A return 9;
2N/A }
2N/A return 0;
2N/A ]])],
2N/A [gl_cv_func_getopt_gnu=yes],
2N/A [gl_cv_func_getopt_gnu=no],
2N/A [dnl Cross compiling. Guess based on host and declarations.
2N/A case $host_os:$ac_cv_have_decl_optreset in
2N/A *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
2N/A *:yes) gl_cv_func_getopt_gnu=no;;
2N/A *) gl_cv_func_getopt_gnu=yes;;
2N/A esac
2N/A ])
2N/A case $gl_had_POSIXLY_CORRECT in
2N/A exported) ;;
2N/A yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
2N/A *) AS_UNSET([POSIXLY_CORRECT]) ;;
2N/A esac
2N/A ])
2N/A if test "$gl_cv_func_getopt_gnu" = "no"; then
2N/A gl_replace_getopt=yes
2N/A fi
2N/A fi
2N/A])
2N/A
2N/A# emacs' configure.in uses this.
2N/AAC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
2N/A[
2N/A GETOPT_H=getopt.h
2N/A AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
2N/A [Define to rpl_ if the getopt replacement functions and variables
2N/A should be used.])
2N/A AC_SUBST([GETOPT_H])
2N/A])
2N/A
2N/A# Prerequisites of lib/getopt*.
2N/A# emacs' configure.in uses this.
2N/AAC_DEFUN([gl_PREREQ_GETOPT],
2N/A[
2N/A AC_CHECK_DECLS_ONCE([getenv])
2N/A])