ripgrep: windows doesn't detect stdin automatically
Namely, running rg pat < file will recursively search the current directory instead of file. A workaround is to do rg pat - < file, which will search file.
This is a consequence of fixing #19. Maybe there is a better way to detect whether stdin is a pipe automatically, but I don’t know it. @vadz in particular points out that it is quite hairy:
Oh, sorry, I should have thought about this, this is actually a pretty well-known issue – but without any good solution, unfortunately. The problem is that Windows doesn’t have any concept of PTY, so when a program is running in any kind of terminal emulator, and not the standard console window, its stdin is always connected to a pipe, as far as Windows is concerned. Cygwin applications can distinguish between “real” pipes and normal input from the terminal, but I don’t know of any way to do this without linking to cygwin1.dll.
I think you should still be able to detect whether stdin is a TTY when running inside the native console and you should be able to check for this (running inside console, I mean). But everything console-related in Windows is pretty hairy, just look at our own code for detecting whether we can write to a console…
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 19 (10 by maintainers)
Commits related to this issue
- Completely re-work colored output and tty handling. This commit completely guts all of the color handling code and replaces most of it with two new crates: wincolor and termcolor. wincolor provides a... — committed to BurntSushi/ripgrep by BurntSushi 8 years ago
- Detect mintty as a terminal Explanations https://fossies.org/linux/vim/src/iscygpty.c https://github.com/BurntSushi/ripgrep/issues/94#issuecomment-261745480 — committed to Marwes/neovim by Marwes 7 years ago
- Detect mintty as a terminal Explanations https://fossies.org/linux/vim/src/iscygpty.c https://github.com/BurntSushi/ripgrep/issues/94#issuecomment-261745480 — committed to Marwes/neovim by Marwes 7 years ago
- Detect mintty as a terminal Explanations https://fossies.org/linux/vim/src/iscygpty.c https://github.com/BurntSushi/ripgrep/issues/94#issuecomment-261745480 — committed to Marwes/neovim by Marwes 7 years ago
- Refuse to run on Cygwin Heatseeker attempts to use the Windows console API when running in Cygwin-type environments, which results in the process hanging. This change causes the command to fail fast ... — committed to rschmitt/heatseeker by rschmitt 6 years ago
This is the golden ticket for posterity:
With that bit done, everything seems to work perfectly now in both
cmd.exeand mintty.Conceptually it seems simple to me: we just check if we have a pipe and if the name of the pipe fits the pattern used by MSYS for the pipes it uses for terminal emulation.
I think the confusing part could be all this
_pioinfostuff, but we wouldn’t need this, the original patch probably was done like this to minimize changes to the existing code, but we don’t have such constraints. So all we need to do is to callNtQueryObject()(which is almost like any other Win32 API, except it’s a wrapper for a kernel function and not a Win32 call) and match the name returned by it again"msys-XXXX-ptyN-XX".BTW, notice that if we don’t need to support XP (do we?), then we could use Win32 GetFileInformationByHandleEx() function instead. But OTOH
NtQueryObject(), despite being officially internal is surely not going anywhere any time this millennium as there is so much code using it…P.S. Just got the page update with your latest reply. Unfortunately here it’s my lack of Rust knowledge that works against me. I hoped that these function could be used in the same way as
GetConsoleMode(), for example. What’s the difference between them from your point of view?P.P.S. I do understand being sick of it very well though.
@vadz I don’t really understand it and I need to understand it before figuring out how to write it in Rust.
(I am somewhat at the end of my rope on this issue too. For now, at least.)