<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 16 April 2018 at 21:17, Alistair Grant <span dir="ltr"><<a href="mailto:akgrant0710@gmail.com" target="_blank">akgrant0710@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <br>
Hi Eliot & Subbu,<br>
<div><div class="gmail-h5"><br>
On 16 April 2018 at 05:50, K K Subbu <<a href="mailto:kksubbu.ml@gmail.com">kksubbu.ml@gmail.com</a>> wrote:<br>
><br>
> On Sunday 15 April 2018 11:42 PM, Eliot Miranda wrote:<br>
>>><br>
>>><br>
>>> On Windows in sqFileStdioHandlesInto() in sqWin32FilePrims.c:<br>
>>><br>
>>> files[0].sessionID = thisSession;<br>
>>> files[0].file = GetStdHandle(STD_INPUT_HANDLE)<wbr>;<br>
>>> files[0].writable = false;<br>
>>> files[0].lastOp = 0; /* unused on win32 */<br>
>>> files[0].isStdioStream = GetConsoleMode(files[0].file, &mode) != 0;<br>
>>> AddHandleToTable(win32Files, files[0].file);<br>
>><br>
>><br>
>> This could be causing problems, because AFAIA<br>
>> GetConsoleMode(files[0].file, &mode) is no longer reliable.  Can you test?<br>
><br>
><br>
> I believe _isatty(fd) from io.h is the syscall on Windows:<br>
><br>
>  <a href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/isatty" rel="noreferrer" target="_blank">https://docs.microsoft.com/en-<wbr>us/cpp/c-runtime-library/<wbr>reference/isatty</a><br>
><br>
> HTH .. Subbu<br>
<br>
</div></div>Right, _isatty sounds like the more sensible call.  Just looking at<br>
the docs, the return value of GetConsoleMode() is simply whether it<br>
succeeded or not.<br>
<br>
However, in some quick tests it appears that both _isatty() and<br>
GetConsoleMode() return the correct value when called from a cmd.exe<br>
terminal window (I guess GetConsoleMode() fails if there isn't a<br>
terminal).  They both fail when called from a cygwin shell (mintty).<br>
It looks like vim (yes, the text editor) has some MIT licensed code<br>
that does the check correctly from within mintty.</blockquote><div><br></div><div><br></div><div>I've was reading around this topic to see if I could find anything useful.</div><div>I haven't really got my head around it yet to tell what is directly related,<br>but here is the most interesting links I found...</div><div><br></div><div>How can I tell whether my console program was launched from Explorer or from a command prompt?<br><a href="https://blogs.msdn.microsoft.com/oldnewthing/20160125-00/?p=92922">https://blogs.msdn.microsoft.com/oldnewthing/20160125-00/?p=92922</a><br></div><div><br></div><div>What Is conhost.exe and Why Is It Running?<br></div><div><a href="https://www.howtogeek.com/howto/4996/what-is-conhost.exe-and-why-is-it-running/">https://www.howtogeek.com/howto/4996/what-is-conhost.exe-and-why-is-it-running/</a><br><div><br></div><div>

<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255)"><div>GetConsoleMode fails when input piped</div><a href="http://comp.os.ms-windows.programmer.win32.narkive.com/xiPamaWm/getconsolemode-fails-when-input-piped">http://comp.os.ms-windows.programmer.win32.narkive.com/xiPamaWm/getconsolemode-fails-when-input-piped</a></div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255)"><br></div><div>Both isatty() and GetConsoleScreenBufferInfo() fail with mintty 2.1.5 in MSYS2 [okay with Cygwin]<br></div></div><div><div style="text-align:start;text-indent:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><a href="https://github.com/mintty/mintty/issues/482">https://github.com/mintty/mintty/issues/482</a><br></div><div style="text-align:start;text-indent:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><br></div><div>MSVC's native _isatty() thinks Cygwin Bash is a pipe, not an interactive shell, because it just uses GetFileType() deep under the hood.<br><a href="https://stackoverflow.com/questions/45223868/how-do-i-play-nice-with-cygwin">https://stackoverflow.com/questions/45223868/how-do-i-play-nice-with-cygwin</a><br></div><div><br></div><div>

<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><div>ReadConsole and WriteConsole can only be used with console handles; <br></div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">ReadConsole and WriteConsole fail if used with a standard handle that has been redirected and is no longer a console handle.<br></div>ReadFile and WriteFile can be used with other handles (such as files or pipes). </div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><a href="https://docs.microsoft.com/en-us/windows/console/high-level-console-input-and-output-functions">https://docs.microsoft.com/en-us/windows/console/high-level-console-input-and-output-functions</a><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><br></div></div><div><div> "GetConsoleMode error: 6" when running under Unix emulation environments such as cygwin and msys.  </div><div>What this means is that GetConsoleMode is being called on an invalid file handle (STD_INPUT_HANDLE). </div></div><div><a href="https://lists.osuosl.org/pipermail/darcs-users/2004-June/002104.html">https://lists.osuosl.org/pipermail/darcs-users/2004-June/002104.html</a><br></div><div><br></div><div>Windows: Improve terminal detection mechanism<br></div><div><a href="https://phabricator.haskell.org/D2809">https://phabricator.haskell.org/D2809</a><br></div><div><a href="https://github.com/git/git/blob/0767172b9068b225c06fd7ce66422e5936ec60a2/compat/winansi.c#L534-L571">https://github.com/git/git/blob/0767172b9068b225c06fd7ce66422e5936ec60a2/compat/winansi.c#L534-L571</a><br></div><div><a href="https://fossies.org/linux/vim/src/iscygpty.c">https://fossies.org/linux/vim/src/iscygpty.c</a><br></div><div><a href="https://hackage.haskell.org/package/base-4.6.0.0/src/cbits/consUtils.c">https://hackage.haskell.org/package/base-4.6.0.0/src/cbits/consUtils.c</a><br></div><div><br></div>

Cunningly conquering communicated console caveats. Comprende, mon Capitán?<br></div><div><a href="http://archives.miloush.net/michkap/archive/2010/05/07/10008232.html">http://archives.miloush.net/michkap/archive/2010/05/07/10008232.html</a></div><div><br></div><div><div>What does CreateFile(“CONIN$” ..) do?</div></div><div><a href="https://stackoverflow.com/questions/377152/what-does-createfileconin-do?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa">https://stackoverflow.com/questions/377152/what-does-createfileconin-do?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa</a><br></div><div><br></div><div>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<br></div><div><a href="https://docs.microsoft.com/en-us/windows/console/console-handles">https://docs.microsoft.com/en-us/windows/console/console-handles</a><br></div><div><br></div><div><div>Where do writes to stdout go when launched from a cygwin shell, no redirection</div><div><a href="https://stackoverflow.com/questions/4028353/where-do-writes-to-stdout-go-when-launched-from-a-cygwin-shell-no-redirection">https://stackoverflow.com/questions/4028353/where-do-writes-to-stdout-go-when-launched-from-a-cygwin-shell-no-redirection</a><br></div><div><br></div><div><br></div><div>How to create a Windows program that works both as a GUI and console application<br></div><a href="https://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/">https://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/</a><br></div><div><br></div><div><br></div><div>cheers -ben</div></div></div></div>