getopt: handle POSIXLY_CORRECT set but not exported
authorEric Blake <eblake@redhat.com>
Tue, 7 Sep 2010 18:10:38 +0000 (12:10 -0600)
committerEric Blake <eblake@redhat.com>
Tue, 7 Sep 2010 23:08:34 +0000 (17:08 -0600)
* m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Restore pre-existing
export state of POSIXLY_CORRECT.
Reported by Dustin J. Mitchell.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
m4/getopt.m4

index 4d27d6a..f3a5ffb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2010-09-07  Eric Blake  <eblake@redhat.com>
+
+       getopt: handle POSIXLY_CORRECT set but not exported
+       * m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Restore pre-existing
+       export state of POSIXLY_CORRECT, due to bash set -o posix.
+       Reported by Dustin J. Mitchell.
+
 2010-09-05  Bruno Haible  <bruno@clisp.org>
 
        gnulib-tool: Highlight the changed options.
index caa5df4..720f048 100644 (file)
@@ -1,4 +1,4 @@
-# getopt.m4 serial 29
+# getopt.m4 serial 30
 dnl Copyright (C) 2002-2006, 2008-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -50,6 +50,7 @@ AC_DEFUN([gl_GETOPT_IFELSE],
 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
 [
   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
+  AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
 
   dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
@@ -213,7 +214,17 @@ main ()
        # optstring is necessary for programs like m4 that have POSIX-mandated
        # semantics for supporting options interspersed with files.
        # Also, since getopt_long is a GNU extension, we require optind=0.
-       gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes}
+       # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
+       # so take care to revert to the correct (non-)export state.
+dnl GNU Coding Standards currently allow awk but not env; besides, env
+dnl is ambiguous with environment values that contain newlines.
+       gl_awk_probe='BEGIN { for (v in ENVIRON)
+         if (v == "POSIXLY_CORRECT") print "x" }'
+       case ${POSIXLY_CORRECT:+x}`$AWK "$gl_awk_probe" </dev/null` in
+         xx) gl_had_POSIXLY_CORRECT=exported ;;
+         x)  gl_had_POSIXLY_CORRECT=yes      ;;
+         *)  gl_had_POSIXLY_CORRECT=         ;;
+       esac
        POSIXLY_CORRECT=1
        export POSIXLY_CORRECT
        AC_RUN_IFELSE(
@@ -278,9 +289,11 @@ main ()
            *)                   gl_cv_func_getopt_gnu=yes;;
          esac
         ])
-       if test "$gl_had_POSIXLY_CORRECT" != yes; then
-         AS_UNSET([POSIXLY_CORRECT])
-       fi
+       case $gl_had_POSIXLY_CORRECT in
+         exported) ;;
+         yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
+         *) AS_UNSET([POSIXLY_CORRECT]) ;;
+       esac
       ])
     if test "$gl_cv_func_getopt_gnu" = "no"; then
       gl_replace_getopt=yes