On 16 April 2018 at 21:17, Alistair Grant <akgrant0710@gmail.com> wrote:
Hi Eliot & Subbu,

On 16 April 2018 at 05:50, K K Subbu <kksubbu.ml@gmail.com> wrote:
> On Sunday 15 April 2018 11:42 PM, Eliot Miranda wrote:
>>> On Windows in sqFileStdioHandlesInto() in sqWin32FilePrims.c:
>>> files[0].sessionID = thisSession;
>>> files[0].file = GetStdHandle(STD_INPUT_HANDLE);
>>> files[0].writable = false;
>>> files[0].lastOp = 0; /* unused on win32 */
>>> files[0].isStdioStream = GetConsoleMode(files[0].file, &mode) != 0;
>>> AddHandleToTable(win32Files, files[0].file);
>> This could be causing problems, because AFAIA
>> GetConsoleMode(files[0].file, &mode) is no longer reliable.  Can you test?
> I believe _isatty(fd) from io.h is the syscall on Windows:
> HTH .. Subbu

Right, _isatty sounds like the more sensible call.  Just looking at
the docs, the return value of GetConsoleMode() is simply whether it
succeeded or not.

However, in some quick tests it appears that both _isatty() and
GetConsoleMode() return the correct value when called from a cmd.exe
terminal window (I guess GetConsoleMode() fails if there isn't a
terminal).  They both fail when called from a cygwin shell (mintty).
It looks like vim (yes, the text editor) has some MIT licensed code
that does the check correctly from within mintty.

I've was reading around this topic to see if I could find anything useful.
I haven't really got my head around it yet to tell what is directly related,
but here is the most interesting links I found...

How can I tell whether my console program was launched from Explorer or from a command prompt?

What Is conhost.exe and Why Is It Running?

Both isatty() and GetConsoleScreenBufferInfo() fail with mintty 2.1.5 in MSYS2 [okay with Cygwin]

MSVC's native _isatty() thinks Cygwin Bash is a pipe, not an interactive shell, because it just uses GetFileType() deep under the hood.

ReadConsole and WriteConsole can only be used with console handles; 
ReadConsole and WriteConsole fail if used with a standard handle that has been redirected and is no longer a console handle.
ReadFile and WriteFile can be used with other handles (such as files or pipes). 

 "GetConsoleMode error: 6" when running under Unix emulation environments such as cygwin and msys.  
What this means is that GetConsoleMode is being called on an invalid file handle (STD_INPUT_HANDLE). 

Windows: Improve terminal detection mechanism

Cunningly conquering communicated console caveats. Comprende, mon Capitán?

What does CreateFile(“CONIN$” ..) do?

The CreateFile function enables a process to get a handle to its console's input buffer and active screen buffer, even if STDIN and STDOUT have been redirected. To open a handle to a console's input buffer, specify the CONIN$ value in a call to CreateFile

Where do writes to stdout go when launched from a cygwin shell, no redirection

How to create a Windows program that works both as a GUI and console application

cheers -ben