From d7af94ea8b164efa0cc8e8618875a8e9c127ec3c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 3 Jan 2012 03:54:08 +0100 Subject: [PATCH] New module 'isatty'. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * lib/unistd.in.h (isatty): New declaration. * lib/isatty.c: New file, based on an idea of Bastien Roucariès . * 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. --- ChangeLog | 15 ++++++++++++++ doc/posix-functions/isatty.texi | 8 ++++---- lib/isatty.c | 44 +++++++++++++++++++++++++++++++++++++++++ lib/unistd.in.h | 21 ++++++++++++++++++++ m4/isatty.m4 | 16 +++++++++++++++ m4/unistd_h.m4 | 9 ++++++--- modules/isatty | 28 ++++++++++++++++++++++++++ modules/unistd | 2 ++ 8 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 lib/isatty.c create mode 100644 m4/isatty.m4 create mode 100644 modules/isatty diff --git a/ChangeLog b/ChangeLog index 2e2c71b07..8fdc3d514 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,20 @@ 2012-01-02 Bruno Haible + New module 'isatty'. + * lib/unistd.in.h (isatty): New declaration. + * lib/isatty.c: New file, based on an idea of + Bastien Roucariès . + * 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 + canonicalize: Tweak 2011-12-29 commit. * lib/canonicalize.c (canonicalize_filename_mode): Fix typo in comment. * lib/canonicalize.h (canonicalize_filename_mode): Update specification. diff --git a/doc/posix-functions/isatty.texi b/doc/posix-functions/isatty.texi index 0cf4a0457..4a5a0759b 100644 --- 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 diff --git a/lib/isatty.c b/lib/isatty.c new file mode 100644 index 000000000..ed8be82ef --- /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 . */ + +#include + +/* Specification. */ +#include + +/* This replacement is enabled on native Windows. */ + +/* Get declarations of the Win32 API functions. */ +#define WIN32_LEAN_AND_MEAN +#include + +/* 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; +} diff --git a/lib/unistd.in.h b/lib/unistd.in.h index a70fe4c91..c579cd46b 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -935,6 +935,27 @@ _GL_WARN_ON_USE (group_member, "group_member is unportable - " #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. diff --git a/m4/isatty.m4 b/m4/isatty.m4 new file mode 100644 index 000000000..5d866ff61 --- /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 +]) diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4 index 7dda69198..7595534fd 100644 --- 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 @@ AC_DEFUN([gl_UNISTD_H], #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 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], 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 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], 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]) diff --git a/modules/isatty b/modules/isatty new file mode 100644 index 000000000..8d596eed7 --- /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: + + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/unistd b/modules/unistd index de86020e1..38f051503 100644 --- a/modules/unistd +++ b/modules/unistd @@ -54,6 +54,7 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H -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 @@ unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H -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' \ -- 2.11.0