public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Cc: Rich Eizenhoefer <riche@microsoft.com>
Subject: Re: From Microsoft: Windows 10 Console and Cygwin
Date: Tue, 02 Jun 2015 16:50:00 -0000	[thread overview]
Message-ID: <20150602165013.GE741@calimero.vinschen.de> (raw)
In-Reply-To: <BN3PR03MB1430F49F8839B66558779F3AB4B50@BN3PR03MB1430.namprd03.prod.outlook.com>


[-- Attachment #1.1: Type: text/plain, Size: 799 bytes --]

Hi Rich,

On Jun  2 16:37, Rich Eizenhoefer wrote:
> Can you provide more detail on changing isatty function to support
> Cygwin PTY's? I need to be able to support the request in our backlog.

As I outlined in https://cygwin.com/ml/cygwin/2015-05/msg00029.html,
I'm wondering if the MSVCRT guys would really like to support this,
but if they are willing to consider that, I have a POC implementation
of a MSVCRT isatty replacement implementation I once created for a
customer.  I attached it to this mail.  It's entirely self-written,
free for any usage, no strings attached.  Feel free to use it as you
see fit.


Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #1.2: isatty-for-native-apps.c --]
[-- Type: text/plain, Size: 3122 bytes --]

#include <stdio.h>
#include <io.h>

#include <errno.h>
#include <wchar.h>
#include <windows.h>
#include <winternl.h>

#ifndef __MINGW64_VERSION_MAJOR
/* MS winternl.h defines FILE_INFORMATION_CLASS, but with only a
   different single member. */
enum FILE_INFORMATION_CLASSX
{
  FileNameInformation = 9
};

typedef struct _FILE_NAME_INFORMATION
{
  ULONG FileNameLength;
  WCHAR FileName[1];
} FILE_NAME_INFORMATION, *PFILE_NAME_INFORMATION;

NTSTATUS (NTAPI *pNtQueryInformationFile) (HANDLE, PIO_STATUS_BLOCK, PVOID,
					  ULONG, FILE_INFORMATION_CLASSX);
#else
NTSTATUS (NTAPI *pNtQueryInformationFile) (HANDLE, PIO_STATUS_BLOCK, PVOID,
					  ULONG, FILE_INFORMATION_CLASS);
#endif

int
isatty (int fd)
{
  HANDLE fh;
  NTSTATUS status;
  IO_STATUS_BLOCK io;
  long buf[66];	/* NAME_MAX + 1 + sizeof ULONG */
  PFILE_NAME_INFORMATION pfni = (PFILE_NAME_INFORMATION) buf;
  PWCHAR cp;

  /* First check using _isatty.
  
     Note that this returns the wrong result for NUL, for instance! 
     Workaround is not to use _isatty at all, but rather GetFileType
     plus object name checking. */
  if (_isatty (fd))
    return 1;

  /* Now fetch the underlying HANDLE. */
  fh = (HANDLE) _get_osfhandle (fd);
  if (!fh || fh == INVALID_HANDLE_VALUE)
    {
      errno = EBADF;
      return 0;
    }

  /* Must be a pipe. */
  if (GetFileType (fh) != FILE_TYPE_PIPE)
    goto no_tty;

  /* Calling the native NT function NtQueryInformationFile is required to
     support pre-Vista systems.  If that's of no concern, Vista introduced
     the GetFileInformationByHandleEx call with the FileNameInfo info class,
     which can be used instead. */
  if (!pNtQueryInformationFile)
    {
      pNtQueryInformationFile = (NTSTATUS (NTAPI *)(HANDLE, PIO_STATUS_BLOCK,
      				PVOID, ULONG, FILE_INFORMATION_CLASS))
			       GetProcAddress (GetModuleHandle ("ntdll.dll"),
					       "NtQueryInformationFile");
      if (!pNtQueryInformationFile)
      	goto no_tty;
    }
  if (!NT_SUCCESS (pNtQueryInformationFile (fh, &io, pfni, sizeof buf,
					   FileNameInformation)))
    goto no_tty;

  /* The filename is not guaranteed to be NUL-terminated. */
  pfni->FileName[pfni->FileNameLength / sizeof (WCHAR)] = L'\0';

  /* Now check the name pattern.  The filename of a Cygwin pseudo tty pipe
     looks like this:

       \cygwin-%16llx-pty%d-{to,from}-master
     
     %16llx is the hash of the Cygwin installation, (to support multiple
     parallel installations), %d is the pseudo tty number, "to" or "from"
     differs the pipe direction. "from" is a stdin, "to" a stdout-like
     pipe. */
  cp = pfni->FileName;
  if (!wcsncmp (cp, L"\\cygwin-", 8)
      && !wcsncmp (cp + 24, L"-pty", 4))
    {
      cp = wcschr (cp + 28, '-');
      if (!cp)
      	goto no_tty;
      if (!wcscmp (cp, L"-from-master") || !wcscmp (cp, L"-to-master"))
      	return 1;
    }
no_tty:
  errno = EINVAL;
  return 0;
}

int
main ()
{
  if (isatty(0))
    printf("tty\n");
  else
    printf("not a tty\n");
  return 0;
}

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2015-06-02 16:50 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-29 19:01 Rich Eizenhoefer
2015-04-29 19:50 ` Tim Prince
2015-04-29 20:06 ` Corinna Vinschen
2015-04-29 21:31   ` Rich Eizenhoefer
2015-04-30  8:22     ` Corinna Vinschen
2015-05-01 19:29       ` Rich Eizenhoefer
2015-05-02 13:47         ` Corinna Vinschen
2015-05-04 22:35           ` Rich Eizenhoefer
2015-05-05  8:22             ` Michael DePaulo
2015-05-31 20:07             ` Ismail Donmez
2015-06-01  8:24               ` Corinna Vinschen
2015-06-01 20:27                 ` Rich Eizenhoefer
2015-06-02  7:56                   ` Corinna Vinschen
2015-06-02  8:10                   ` İsmail Dönmez
2015-06-02 16:37                     ` Rich Eizenhoefer
2015-06-02 16:50                       ` Corinna Vinschen [this message]
2015-06-02 17:50                         ` Rich Eizenhoefer
2015-06-02 19:20                       ` Eric Blake
2015-06-02 20:36                         ` Vince Rice
2015-06-02 20:49                         ` Warren Young
2015-06-08 11:49                           ` Corinna Vinschen
2015-06-08 17:23                             ` Rich Eizenhoefer
2015-04-29 23:43   ` Michael DePaulo
2015-04-30  7:56     ` Corinna Vinschen
2015-04-30 11:27     ` LRN
2015-04-30 12:44       ` Tony Kelman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150602165013.GE741@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin@cygwin.com \
    --cc=riche@microsoft.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).