Blame m4/getopt.m4

Packit 33f14e
# getopt.m4 serial 46
Packit 33f14e
dnl Copyright (C) 2002-2006, 2008-2017 Free Software Foundation, Inc.
Packit 33f14e
dnl This file is free software; the Free Software Foundation
Packit 33f14e
dnl gives unlimited permission to copy and/or distribute it,
Packit 33f14e
dnl with or without modifications, as long as this notice is preserved.
Packit 33f14e
Packit 33f14e
# Request a POSIX compliant getopt function.
Packit 33f14e
AC_DEFUN([gl_FUNC_GETOPT_POSIX],
Packit 33f14e
[
Packit 33f14e
  m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
Packit 33f14e
  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
Packit 33f14e
  AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
Packit 33f14e
  dnl Other modules can request the gnulib implementation of the getopt
Packit 33f14e
  dnl functions unconditionally, by defining gl_REPLACE_GETOPT_ALWAYS.
Packit 33f14e
  dnl argp.m4 does this.
Packit 33f14e
  m4_ifdef([gl_REPLACE_GETOPT_ALWAYS], [
Packit 33f14e
    REPLACE_GETOPT=1
Packit 33f14e
  ], [
Packit 33f14e
    REPLACE_GETOPT=0
Packit 33f14e
    if test -n "$gl_replace_getopt"; then
Packit 33f14e
      REPLACE_GETOPT=1
Packit 33f14e
    fi
Packit 33f14e
  ])
Packit 33f14e
  if test $REPLACE_GETOPT = 1; then
Packit 33f14e
    dnl Arrange for getopt.h to be created.
Packit 33f14e
    gl_GETOPT_SUBSTITUTE_HEADER
Packit 33f14e
  fi
Packit 33f14e
])
Packit 33f14e
Packit 33f14e
# Request a POSIX compliant getopt function with GNU extensions (such as
Packit 33f14e
# options with optional arguments) and the functions getopt_long,
Packit 33f14e
# getopt_long_only.
Packit 33f14e
AC_DEFUN([gl_FUNC_GETOPT_GNU],
Packit 33f14e
[
Packit 33f14e
  dnl Set the variable gl_getopt_required, so that all invocations of
Packit 33f14e
  dnl gl_GETOPT_CHECK_HEADERS in the scope of the current configure file
Packit 33f14e
  dnl will check for getopt with GNU extensions.
Packit 33f14e
  dnl This means that if one gnulib-tool invocation requests getopt-posix
Packit 33f14e
  dnl and another gnulib-tool invocation requests getopt-gnu, it is as if
Packit 33f14e
  dnl both had requested getopt-gnu.
Packit 33f14e
  m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
Packit 33f14e
Packit 33f14e
  dnl No need to invoke gl_FUNC_GETOPT_POSIX here; this is automatically
Packit 33f14e
  dnl done through the module dependency getopt-gnu -> getopt-posix.
Packit 33f14e
])
Packit 33f14e
Packit 33f14e
# Determine whether to replace the entire getopt facility.
Packit 33f14e
AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
Packit 33f14e
[
Packit 33f14e
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit 33f14e
  AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
Packit 33f14e
Packit 33f14e
  dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
Packit 33f14e
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
Packit 33f14e
Packit 33f14e
  gl_CHECK_NEXT_HEADERS([getopt.h])
Packit 33f14e
  if test $ac_cv_header_getopt_h = yes; then
Packit 33f14e
    HAVE_GETOPT_H=1
Packit 33f14e
  else
Packit 33f14e
    HAVE_GETOPT_H=0
Packit 33f14e
  fi
Packit 33f14e
  AC_SUBST([HAVE_GETOPT_H])
Packit 33f14e
Packit 33f14e
  gl_replace_getopt=
Packit 33f14e
Packit 33f14e
  dnl Test whether <getopt.h> is available.
Packit 33f14e
  if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
Packit 33f14e
    AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  dnl Test whether the function getopt_long is available.
Packit 33f14e
  if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
Packit 33f14e
    AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  dnl POSIX 2008 does not specify leading '+' behavior, but see
Packit 33f14e
  dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
Packit 33f14e
  dnl the next version of POSIX.  For now, we only guarantee leading '+'
Packit 33f14e
  dnl behavior with getopt-gnu.
Packit 33f14e
  if test -z "$gl_replace_getopt"; then
Packit 33f14e
    AC_CACHE_CHECK([whether getopt is POSIX compatible],
Packit 33f14e
      [gl_cv_func_getopt_posix],
Packit 33f14e
      [
Packit 33f14e
        dnl Merging these three different test programs into a single one
Packit 33f14e
        dnl would require a reset mechanism. On BSD systems, it can be done
Packit 33f14e
        dnl through 'optreset'; on some others (glibc), it can be done by
Packit 33f14e
        dnl setting 'optind' to 0; on others again (HP-UX, IRIX, OSF/1,
Packit 33f14e
        dnl Solaris 9, musl libc), there is no such mechanism.
Packit 33f14e
        if test $cross_compiling = no; then
Packit 33f14e
          dnl Sanity check. Succeeds everywhere (except on MSVC,
Packit 33f14e
          dnl which lacks <unistd.h> and getopt() entirely).
Packit 33f14e
          AC_RUN_IFELSE(
Packit 33f14e
            [AC_LANG_SOURCE([[
Packit 33f14e
#include <unistd.h>
Packit 33f14e
#include <stdlib.h>
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
main ()
Packit 33f14e
{
Packit 33f14e
  static char program[] = "program";
Packit 33f14e
  static char a[] = "-a";
Packit 33f14e
  static char foo[] = "foo";
Packit 33f14e
  static char bar[] = "bar";
Packit 33f14e
  char *argv[] = { program, a, foo, bar, NULL };
Packit 33f14e
  int c;
Packit 33f14e
Packit 33f14e
  c = getopt (4, argv, "ab");
Packit 33f14e
  if (!(c == 'a'))
Packit 33f14e
    return 1;
Packit 33f14e
  c = getopt (4, argv, "ab");
Packit 33f14e
  if (!(c == -1))
Packit 33f14e
    return 2;
Packit 33f14e
  if (!(optind == 2))
Packit 33f14e
    return 3;
Packit 33f14e
  return 0;
Packit 33f14e
}
Packit 33f14e
]])],
Packit 33f14e
            [gl_cv_func_getopt_posix=maybe],
Packit 33f14e
            [gl_cv_func_getopt_posix=no])
Packit 33f14e
          if test $gl_cv_func_getopt_posix = maybe; then
Packit 33f14e
            dnl Sanity check with '+'. Succeeds everywhere (except on MSVC,
Packit 33f14e
            dnl which lacks <unistd.h> and getopt() entirely).
Packit 33f14e
            AC_RUN_IFELSE(
Packit 33f14e
              [AC_LANG_SOURCE([[
Packit 33f14e
#include <unistd.h>
Packit 33f14e
#include <stdlib.h>
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
main ()
Packit 33f14e
{
Packit 33f14e
  static char program[] = "program";
Packit 33f14e
  static char donald[] = "donald";
Packit 33f14e
  static char p[] = "-p";
Packit 33f14e
  static char billy[] = "billy";
Packit 33f14e
  static char duck[] = "duck";
Packit 33f14e
  static char a[] = "-a";
Packit 33f14e
  static char bar[] = "bar";
Packit 33f14e
  char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
Packit 33f14e
  int c;
Packit 33f14e
Packit 33f14e
  c = getopt (7, argv, "+abp:q:");
Packit 33f14e
  if (!(c == -1))
Packit 33f14e
    return 4;
Packit 33f14e
  if (!(strcmp (argv[0], "program") == 0))
Packit 33f14e
    return 5;
Packit 33f14e
  if (!(strcmp (argv[1], "donald") == 0))
Packit 33f14e
    return 6;
Packit 33f14e
  if (!(strcmp (argv[2], "-p") == 0))
Packit 33f14e
    return 7;
Packit 33f14e
  if (!(strcmp (argv[3], "billy") == 0))
Packit 33f14e
    return 8;
Packit 33f14e
  if (!(strcmp (argv[4], "duck") == 0))
Packit 33f14e
    return 9;
Packit 33f14e
  if (!(strcmp (argv[5], "-a") == 0))
Packit 33f14e
    return 10;
Packit 33f14e
  if (!(strcmp (argv[6], "bar") == 0))
Packit 33f14e
    return 11;
Packit 33f14e
  if (!(optind == 1))
Packit 33f14e
    return 12;
Packit 33f14e
  return 0;
Packit 33f14e
}
Packit 33f14e
]])],
Packit 33f14e
              [gl_cv_func_getopt_posix=maybe],
Packit 33f14e
              [gl_cv_func_getopt_posix=no])
Packit 33f14e
          fi
Packit 33f14e
          if test $gl_cv_func_getopt_posix = maybe; then
Packit 33f14e
            dnl Detect Mac OS X 10.5, AIX 7.1, mingw bug.
Packit 33f14e
            AC_RUN_IFELSE(
Packit 33f14e
              [AC_LANG_SOURCE([[
Packit 33f14e
#include <unistd.h>
Packit 33f14e
#include <stdlib.h>
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
main ()
Packit 33f14e
{
Packit 33f14e
  static char program[] = "program";
Packit 33f14e
  static char ab[] = "-ab";
Packit 33f14e
  char *argv[3] = { program, ab, NULL };
Packit 33f14e
  if (getopt (2, argv, "ab:") != 'a')
Packit 33f14e
    return 13;
Packit 33f14e
  if (getopt (2, argv, "ab:") != '?')
Packit 33f14e
    return 14;
Packit 33f14e
  if (optopt != 'b')
Packit 33f14e
    return 15;
Packit 33f14e
  if (optind != 2)
Packit 33f14e
    return 16;
Packit 33f14e
  return 0;
Packit 33f14e
}
Packit 33f14e
]])],
Packit 33f14e
              [gl_cv_func_getopt_posix=yes],
Packit 33f14e
              [gl_cv_func_getopt_posix=no])
Packit 33f14e
          fi
Packit 33f14e
        else
Packit 33f14e
          case "$host_os" in
Packit 33f14e
            darwin* | aix* | mingw*) gl_cv_func_getopt_posix="guessing no";;
Packit 33f14e
            *)                       gl_cv_func_getopt_posix="guessing yes";;
Packit 33f14e
          esac
Packit 33f14e
        fi
Packit 33f14e
      ])
Packit 33f14e
    case "$gl_cv_func_getopt_posix" in
Packit 33f14e
      *no) gl_replace_getopt=yes ;;
Packit 33f14e
    esac
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
Packit 33f14e
    AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
Packit 33f14e
      [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
Packit 33f14e
       # optstring is necessary for programs like m4 that have POSIX-mandated
Packit 33f14e
       # semantics for supporting options interspersed with files.
Packit 33f14e
       # Also, since getopt_long is a GNU extension, we require optind=0.
Packit 33f14e
       # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
Packit 33f14e
       # so take care to revert to the correct (non-)export state.
Packit 33f14e
dnl GNU Coding Standards currently allow awk but not env; besides, env
Packit 33f14e
dnl is ambiguous with environment values that contain newlines.
Packit 33f14e
       gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
Packit 33f14e
       case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" 
Packit 33f14e
         xx) gl_had_POSIXLY_CORRECT=exported ;;
Packit 33f14e
         x)  gl_had_POSIXLY_CORRECT=yes      ;;
Packit 33f14e
         *)  gl_had_POSIXLY_CORRECT=         ;;
Packit 33f14e
       esac
Packit 33f14e
       POSIXLY_CORRECT=1
Packit 33f14e
       export POSIXLY_CORRECT
Packit 33f14e
       AC_RUN_IFELSE(
Packit 33f14e
        [AC_LANG_PROGRAM([[#include <getopt.h>
Packit 33f14e
                           #include <stddef.h>
Packit 33f14e
                           #include <string.h>
Packit 33f14e
           ]GL_NOCRASH[
Packit 33f14e
           ]], [[
Packit 33f14e
             int result = 0;
Packit 33f14e
Packit 33f14e
             nocrash_init();
Packit 33f14e
Packit 33f14e
             /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
Packit 33f14e
                and fails on Mac OS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
Packit 33f14e
                OSF/1 5.1, Solaris 10.  */
Packit 33f14e
             {
Packit 33f14e
               static char conftest[] = "conftest";
Packit 33f14e
               static char plus[] = "-+";
Packit 33f14e
               char *argv[3] = { conftest, plus, NULL };
Packit 33f14e
               opterr = 0;
Packit 33f14e
               if (getopt (2, argv, "+a") != '?')
Packit 33f14e
                 result |= 1;
Packit 33f14e
             }
Packit 33f14e
             /* This code succeeds on glibc 2.8, mingw,
Packit 33f14e
                and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
Packit 33f14e
                IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x.  */
Packit 33f14e
             {
Packit 33f14e
               static char program[] = "program";
Packit 33f14e
               static char p[] = "-p";
Packit 33f14e
               static char foo[] = "foo";
Packit 33f14e
               static char bar[] = "bar";
Packit 33f14e
               char *argv[] = { program, p, foo, bar, NULL };
Packit 33f14e
Packit 33f14e
               optind = 1;
Packit 33f14e
               if (getopt (4, argv, "p::") != 'p')
Packit 33f14e
                 result |= 2;
Packit 33f14e
               else if (optarg != NULL)
Packit 33f14e
                 result |= 4;
Packit 33f14e
               else if (getopt (4, argv, "p::") != -1)
Packit 33f14e
                 result |= 6;
Packit 33f14e
               else if (optind != 2)
Packit 33f14e
                 result |= 8;
Packit 33f14e
             }
Packit 33f14e
             /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0.  */
Packit 33f14e
             {
Packit 33f14e
               static char program[] = "program";
Packit 33f14e
               static char foo[] = "foo";
Packit 33f14e
               static char p[] = "-p";
Packit 33f14e
               char *argv[] = { program, foo, p, NULL };
Packit 33f14e
               optind = 0;
Packit 33f14e
               if (getopt (3, argv, "-p") != 1)
Packit 33f14e
                 result |= 16;
Packit 33f14e
               else if (getopt (3, argv, "-p") != 'p')
Packit 33f14e
                 result |= 16;
Packit 33f14e
             }
Packit 33f14e
             /* This code fails on glibc 2.11.  */
Packit 33f14e
             {
Packit 33f14e
               static char program[] = "program";
Packit 33f14e
               static char b[] = "-b";
Packit 33f14e
               static char a[] = "-a";
Packit 33f14e
               char *argv[] = { program, b, a, NULL };
Packit 33f14e
               optind = opterr = 0;
Packit 33f14e
               if (getopt (3, argv, "+:a:b") != 'b')
Packit 33f14e
                 result |= 32;
Packit 33f14e
               else if (getopt (3, argv, "+:a:b") != ':')
Packit 33f14e
                 result |= 32;
Packit 33f14e
             }
Packit 33f14e
             /* This code dumps core on glibc 2.14.  */
Packit 33f14e
             {
Packit 33f14e
               static char program[] = "program";
Packit 33f14e
               static char w[] = "-W";
Packit 33f14e
               static char dummy[] = "dummy";
Packit 33f14e
               char *argv[] = { program, w, dummy, NULL };
Packit 33f14e
               optind = opterr = 1;
Packit 33f14e
               if (getopt (3, argv, "W;") != 'W')
Packit 33f14e
                 result |= 64;
Packit 33f14e
             }
Packit 33f14e
             return result;
Packit 33f14e
           ]])],
Packit 33f14e
        [gl_cv_func_getopt_gnu=yes],
Packit 33f14e
        [gl_cv_func_getopt_gnu=no],
Packit 33f14e
        [dnl Cross compiling. Assume the worst, even on glibc platforms.
Packit 33f14e
         gl_cv_func_getopt_gnu="guessing no"
Packit 33f14e
        ])
Packit 33f14e
       case $gl_had_POSIXLY_CORRECT in
Packit 33f14e
         exported) ;;
Packit 33f14e
         yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
Packit 33f14e
         *) AS_UNSET([POSIXLY_CORRECT]) ;;
Packit 33f14e
       esac
Packit 33f14e
      ])
Packit 33f14e
    if test "$gl_cv_func_getopt_gnu" != yes; then
Packit 33f14e
      gl_replace_getopt=yes
Packit 33f14e
    else
Packit 33f14e
      AC_CACHE_CHECK([for working GNU getopt_long function],
Packit 33f14e
        [gl_cv_func_getopt_long_gnu],
Packit 33f14e
        [AC_RUN_IFELSE(
Packit 33f14e
           [AC_LANG_PROGRAM(
Packit 33f14e
              [[#include <getopt.h>
Packit 33f14e
                #include <stddef.h>
Packit 33f14e
                #include <string.h>
Packit 33f14e
              ]],
Packit 33f14e
              [[static const struct option long_options[] =
Packit 33f14e
                  {
Packit 33f14e
                    { "xtremely-",no_argument,       NULL, 1003 },
Packit 33f14e
                    { "xtra",     no_argument,       NULL, 1001 },
Packit 33f14e
                    { "xtreme",   no_argument,       NULL, 1002 },
Packit 33f14e
                    { "xtremely", no_argument,       NULL, 1003 },
Packit 33f14e
                    { NULL,       0,                 NULL, 0 }
Packit 33f14e
                  };
Packit 33f14e
                /* This code fails on OpenBSD 5.0.  */
Packit 33f14e
                {
Packit 33f14e
                  static char program[] = "program";
Packit 33f14e
                  static char xtremel[] = "--xtremel";
Packit 33f14e
                  char *argv[] = { program, xtremel, NULL };
Packit 33f14e
                  int option_index;
Packit 33f14e
                  optind = 1; opterr = 0;
Packit 33f14e
                  if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
Packit 33f14e
                    return 1;
Packit 33f14e
                }
Packit 33f14e
                return 0;
Packit 33f14e
              ]])],
Packit 33f14e
           [gl_cv_func_getopt_long_gnu=yes],
Packit 33f14e
           [gl_cv_func_getopt_long_gnu=no],
Packit 33f14e
           [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
Packit 33f14e
            case "$host_os" in
Packit 33f14e
              openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
Packit 33f14e
              *)        gl_cv_func_getopt_long_gnu="guessing yes";;
Packit 33f14e
            esac
Packit 33f14e
           ])
Packit 33f14e
        ])
Packit 33f14e
      case "$gl_cv_func_getopt_long_gnu" in
Packit 33f14e
        *yes) ;;
Packit 33f14e
        *) gl_replace_getopt=yes ;;
Packit 33f14e
      esac
Packit 33f14e
    fi
Packit 33f14e
  fi
Packit 33f14e
])
Packit 33f14e
Packit 33f14e
AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
Packit 33f14e
[
Packit 33f14e
  AC_CHECK_HEADERS_ONCE([sys/cdefs.h])
Packit 33f14e
  if test $ac_cv_header_sys_cdefs_h = yes; then
Packit 33f14e
    HAVE_SYS_CDEFS_H=1
Packit 33f14e
  else
Packit 33f14e
    HAVE_SYS_CDEFS_H=0
Packit 33f14e
  fi
Packit 33f14e
  AC_SUBST([HAVE_SYS_CDEFS_H])
Packit 33f14e
Packit 33f14e
  AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
Packit 33f14e
    [Define to rpl_ if the getopt replacement functions and variables
Packit 33f14e
     should be used.])
Packit 33f14e
  GETOPT_H=getopt.h
Packit 33f14e
  GETOPT_CDEFS_H=getopt-cdefs.h
Packit 33f14e
  AC_SUBST([GETOPT_H])
Packit 33f14e
  AC_SUBST([GETOPT_CDEFS_H])
Packit 33f14e
])