changeset 16209:93b0d3ad505e

New module 'isatty'. * lib/unistd.in.h (isatty): New declaration. * lib/isatty.c: New file, based on an idea of Bastien Roucariès <roucaries.bastien@gmail.com>. * m4/isatty.m4: New file. * m4/unistd_h.m4 (gl_UNISTD_H): Test whether isatty is declared. (gl_UNISTD_H_DEFAULTS): Initialize GNULIB_ISATTY, REPLACE_ISATTY. * modules/unistd (Makefile.am): Substitute GNULIB_ISATTY, REPLACE_ISATTY. * modules/isatty: New file. * doc/posix-functions/isatty.texi: Mention the new module. Suggested by Paolo Bonzini.
author Bruno Haible <bruno@clisp.org>
date Tue, 03 Jan 2012 03:54:08 +0100
parents b9e251ade13d
children 6281990d29da
files ChangeLog doc/posix-functions/isatty.texi lib/isatty.c lib/unistd.in.h m4/isatty.m4 m4/unistd_h.m4 modules/isatty modules/unistd
diffstat 8 files changed, 136 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2012-01-02  Bruno Haible  <bruno@clisp.org>
+
+	New module 'isatty'.
+	* lib/unistd.in.h (isatty): New declaration.
+	* lib/isatty.c: New file, based on an idea of
+	Bastien Roucariès <roucaries.bastien@gmail.com>.
+	* m4/isatty.m4: New file.
+	* m4/unistd_h.m4 (gl_UNISTD_H): Test whether isatty is declared.
+	(gl_UNISTD_H_DEFAULTS): Initialize GNULIB_ISATTY, REPLACE_ISATTY.
+	* modules/unistd (Makefile.am): Substitute GNULIB_ISATTY,
+	REPLACE_ISATTY.
+	* modules/isatty: New file.
+	* doc/posix-functions/isatty.texi: Mention the new module.
+	Suggested by Paolo Bonzini.
+
 2012-01-02  Bruno Haible  <bruno@clisp.org>
 
 	canonicalize: Tweak 2011-12-29 commit.
--- a/doc/posix-functions/isatty.texi
+++ b/doc/posix-functions/isatty.texi
@@ -4,15 +4,15 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/isatty.html}
 
-Gnulib module: ---
+Gnulib module: isatty
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+On native Windows, this function also returns true for character devices such
+as @file{NUL}.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-On Windows, @code{isatty} also returns true for character devices such as
-@file{NUL}.
 @end itemize
new file mode 100644
--- /dev/null
+++ b/lib/isatty.c
@@ -0,0 +1,44 @@
+/* isatty() replacement.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <unistd.h>
+
+/* This replacement is enabled on native Windows.  */
+
+/* Get declarations of the Win32 API functions.  */
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+/* Get _get_osfhandle().  */
+#include "msvc-nothrow.h"
+
+#define IsConsoleHandle(h) (((long) (h) & 3) == 3)
+
+int
+isatty (int fd)
+{
+  /* _isatty (fd) tests whether GetFileType of the handle is FILE_TYPE_CHAR.  */
+  if (_isatty (fd))
+    {
+      HANDLE h = (HANDLE) _get_osfhandle (fd);
+      return IsConsoleHandle (h);
+    }
+  else
+    return 0;
+}
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -935,6 +935,27 @@
 #endif
 
 
+#if @GNULIB_ISATTY@
+# if @REPLACE_ISATTY@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef isatty
+#   define isatty rpl_isatty
+#  endif
+_GL_FUNCDECL_RPL (isatty, int, (int fd));
+_GL_CXXALIAS_RPL (isatty, int, (int fd));
+# else
+_GL_CXXALIAS_SYS (isatty, int, (int fd));
+# endif
+_GL_CXXALIASWARN (isatty);
+#elif defined GNULIB_POSIXCHECK
+# undef isatty
+# if HAVE_RAW_DECL_ISATTY
+_GL_WARN_ON_USE (isatty, "isatty has portability problems on native Windows - "
+                 "use gnulib module isatty for portability");
+# endif
+#endif
+
+
 #if @GNULIB_LCHOWN@
 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
    to GID (if GID is not -1).  Do not follow symbolic links.
new file mode 100644
--- /dev/null
+++ b/m4/isatty.m4
@@ -0,0 +1,16 @@
+# isatty.m4 serial 1
+dnl Copyright (C) 2012 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_ISATTY],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
+  dnl On native Windows, the system's isatty() returns true for pipes and
+  dnl for the NUL device.
+  case $host_os in
+    mingw*) REPLACE_ISATTY=1 ;;
+  esac
+])
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 62
+# unistd_h.m4 serial 63
 dnl Copyright (C) 2006-2012 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -41,8 +41,9 @@
 #endif
     ]], [chdir chown dup dup2 dup3 environ euidaccess faccessat fchdir fchownat
     fdatasync fsync ftruncate getcwd getdomainname getdtablesize getgroups
-    gethostname getlogin getlogin_r getpagesize getusershell setusershell
-    endusershell group_member lchown link linkat lseek pipe pipe2 pread pwrite
+    gethostname getlogin getlogin_r getpagesize
+    getusershell setusershell endusershell
+    group_member isatty lchown link linkat lseek pipe pipe2 pread pwrite
     readlink readlinkat rmdir sethostname sleep symlink symlinkat ttyname_r
     unlink unlinkat usleep])
 ])
@@ -82,6 +83,7 @@
   GNULIB_GETPAGESIZE=0;          AC_SUBST([GNULIB_GETPAGESIZE])
   GNULIB_GETUSERSHELL=0;         AC_SUBST([GNULIB_GETUSERSHELL])
   GNULIB_GROUP_MEMBER=0;         AC_SUBST([GNULIB_GROUP_MEMBER])
+  GNULIB_ISATTY=0;               AC_SUBST([GNULIB_ISATTY])
   GNULIB_LCHOWN=0;               AC_SUBST([GNULIB_LCHOWN])
   GNULIB_LINK=0;                 AC_SUBST([GNULIB_LINK])
   GNULIB_LINKAT=0;               AC_SUBST([GNULIB_LINKAT])
@@ -158,6 +160,7 @@
   REPLACE_GETLOGIN_R=0;   AC_SUBST([REPLACE_GETLOGIN_R])
   REPLACE_GETGROUPS=0;    AC_SUBST([REPLACE_GETGROUPS])
   REPLACE_GETPAGESIZE=0;  AC_SUBST([REPLACE_GETPAGESIZE])
+  REPLACE_ISATTY=0;       AC_SUBST([REPLACE_ISATTY])
   REPLACE_LCHOWN=0;       AC_SUBST([REPLACE_LCHOWN])
   REPLACE_LINK=0;         AC_SUBST([REPLACE_LINK])
   REPLACE_LINKAT=0;       AC_SUBST([REPLACE_LINKAT])
new file mode 100644
--- /dev/null
+++ b/modules/isatty
@@ -0,0 +1,28 @@
+Description:
+Test whether a file descriptor is a terminal.
+
+Files:
+lib/isatty.c
+m4/isatty.m4
+
+Depends-on:
+unistd
+msvc-nothrow    [test $REPLACE_ISATTY = 1]
+
+configure.ac:
+gl_FUNC_ISATTY
+if test $REPLACE_ISATTY = 1; then
+  AC_LIBOBJ([isatty])
+fi
+gl_UNISTD_MODULE_INDICATOR([isatty])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
--- a/modules/unistd
+++ b/modules/unistd
@@ -54,6 +54,7 @@
 	      -e 's/@''GNULIB_GETPAGESIZE''@/$(GNULIB_GETPAGESIZE)/g' \
 	      -e 's/@''GNULIB_GETUSERSHELL''@/$(GNULIB_GETUSERSHELL)/g' \
 	      -e 's/@''GNULIB_GROUP_MEMBER''@/$(GNULIB_GROUP_MEMBER)/g' \
+	      -e 's/@''GNULIB_ISATTY''@/$(GNULIB_ISATTY)/g' \
 	      -e 's/@''GNULIB_LCHOWN''@/$(GNULIB_LCHOWN)/g' \
 	      -e 's/@''GNULIB_LINK''@/$(GNULIB_LINK)/g' \
 	      -e 's/@''GNULIB_LINKAT''@/$(GNULIB_LINKAT)/g' \
@@ -132,6 +133,7 @@
 	      -e 's|@''REPLACE_GETLOGIN_R''@|$(REPLACE_GETLOGIN_R)|g' \
 	      -e 's|@''REPLACE_GETGROUPS''@|$(REPLACE_GETGROUPS)|g' \
 	      -e 's|@''REPLACE_GETPAGESIZE''@|$(REPLACE_GETPAGESIZE)|g' \
+	      -e 's|@''REPLACE_ISATTY''@|$(REPLACE_ISATTY)|g' \
 	      -e 's|@''REPLACE_LCHOWN''@|$(REPLACE_LCHOWN)|g' \
 	      -e 's|@''REPLACE_LINK''@|$(REPLACE_LINK)|g' \
 	      -e 's|@''REPLACE_LINKAT''@|$(REPLACE_LINKAT)|g' \