bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new module 'isatty' (was: Re: MS-Windows build of Grep [2/4])


From: Bruno Haible
Subject: new module 'isatty' (was: Re: MS-Windows build of Grep [2/4])
Date: Tue, 03 Jan 2012 03:56:56 +0100
User-agent: KMail/4.7.4 (Linux/3.1.0-1.2-desktop; KDE/4.7.4; x86_64; ; )

bastien ROUCARIES wrote:
> The right fix is :
> DWORD st;
> /* note use _ version for porting to vc */
> if(!_isattty(fd))
>       return 0;
> if (!GetConsoleMode(_get_osfhandle(fd), &st)) 
>       return 0;
> return 1;
> 
> This is the idea

Thanks for the idea. 

> it is a little bit more complicated due to INVALID_HANDLE exception
> raising in _get_osfhandle(fd) but the idea

I think that in this case _isatty(fd) will already return 0.

I'm adding this new module. Feel free to use it in 'grep'.


2012-01-02  Bruno Haible  <address@hidden>

        New module 'isatty'.
        * lib/unistd.in.h (isatty): New declaration.
        * lib/isatty.c: New file, based on an idea of
        Bastien Roucariès <address@hidden>.
        * 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.

================================ lib/isatty.c ================================
/* 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;
}
================================ m4/isatty.m4 ================================
# 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
])
=============================== modules/isatty ===============================
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
==============================================================================
*** doc/posix-functions/isatty.texi.orig        Tue Jan  3 03:40:10 2012
--- doc/posix-functions/isatty.texi     Tue Jan  3 02:17:20 2012
***************
*** 4,18 ****
  
  POSIX specification:@* 
@url{http://www.opengroup.org/onlinepubs/9699919799/functions/isatty.html}
  
! Gnulib module: ---
  
  Portability problems fixed by Gnulib:
  @itemize
  @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
--- 4,18 ----
  
  POSIX specification:@* 
@url{http://www.opengroup.org/onlinepubs/9699919799/functions/isatty.html}
  
! 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
  @end itemize
*** lib/unistd.in.h.orig        Tue Jan  3 03:40:10 2012
--- lib/unistd.in.h     Tue Jan  3 02:48:07 2012
***************
*** 935,940 ****
--- 935,961 ----
  #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.
*** m4/unistd_h.m4.orig Tue Jan  3 03:40:10 2012
--- m4/unistd_h.m4      Tue Jan  3 02:22:49 2012
***************
*** 1,4 ****
! # unistd_h.m4 serial 62
  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,
--- 1,4 ----
! # 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,48 ****
  #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
      readlink readlinkat rmdir sethostname sleep symlink symlinkat ttyname_r
      unlink unlinkat usleep])
  ])
--- 41,49 ----
  #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 isatty lchown link linkat lseek pipe pipe2 pread pwrite
      readlink readlinkat rmdir sethostname sleep symlink symlinkat ttyname_r
      unlink unlinkat usleep])
  ])
***************
*** 82,87 ****
--- 83,89 ----
    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,163 ****
--- 160,166 ----
    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])
*** modules/unistd.orig Tue Jan  3 03:40:10 2012
--- modules/unistd      Tue Jan  3 02:23:46 2012
***************
*** 54,59 ****
--- 54,60 ----
              -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,137 ****
--- 133,139 ----
              -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' \




reply via email to

[Prev in Thread] Current Thread [Next in Thread]