Thread overview
Re: regading detection of stdin
Re: Digitalmars-d-learn Digest, Vol 95, Issue 30
Dec 08, 2013
Hugo Florentino
Dec 08, 2013
Hugo Florentino
Dec 08, 2013
Adam D. Ruppe
December 08, 2013
On Sat, 07 Dec 2013 23:03:10 +0100 Chris Cain wrote:
>
> On Friday, 6 December 2013 at 00:24:22 UTC, Hugo Florentino wrote:
>> Hi,
>>
>> I was trying to do something like this (using dmd.2.064.2 both
>> from Windows and Linux), but if nothing is passed from stdin
>> and no parameter is provided, the application freezes:
>>
>> ...snip...
>>
>>
>> Where is the problem?
>>
>> Regards, Hugo
>
> I'm not sure I understand the problem. Your program works as I
> think you describe it should on my Linux box:
>
> ---
> zshazz@manjarox ~/projects/explore % cat /dev/null | rdmd
> inputStuff.d
> No argument passed as parameter or from stdin.
> zshazz@manjarox ~/projects/explore % echo -n "" | rdmd
> inputStuff.d
> No argument passed as parameter or from stdin.
> zshazz@manjarox ~/projects/explore % echo -n "test" | rdmd
> inputStuff.d
> Argument passed from stdin succesfully stored in variable s.
> zshazz@manjarox ~/projects/explore % rdmd inputStuff.d blah.txt
> Argument passed as parameter succesfully stored in variable s.
> ---
>
> If you just run it (via `rdmd inputStuff.d`) it pauses and waits
> for some sort of input, but that's expected. Using `ctrl-d`
> results in "No argument passed as parameter or from stdin." as
> expected in that case.
>
> Could you describe what you're doing exactly that causes it to
> freeze?

Actually it does not freeze, it is expecting user input as you said. I simply didn't realize this (that's the reason behind my choice of words).
What I intend is for my application to simply display a message if no parameter was given and it was not piped after another command. You know, like the average messages you get when typing alone a command which expects some form of input.
I don't know how detect input sent to it through a pipe without requiring user interaction, I have not been able to find a working example in the documentation, in the source code or using search engines.
December 08, 2013
On Sat, 07 Dec 2013 21:33:56 +0100, Adam D. Ruppe wrote:
>
> Sounds like what you need is to see if stdin is a tty.
>
> import core.sys.posix.unistd; // has isatty()
>
> void main() {
>          import std.stdio;
>          writeln(isatty(0)); // 0 is stdin, so this will show 1 if
> keyboard or 0 if pope
> }

Interesting, thanks for pointing that (this function is not described at all in the source code)
Now, would that work regardless of the terminal number ther user is in (ie tty1, tty2...)?
Also, is there a way to do accomplish the same in Windows? I would prefer my application to support both platforms if possible (unless this posix unit is cross-platform, which seems unlikely).

December 08, 2013
On Sunday, 8 December 2013 at 02:11:01 UTC, Hugo Florentino wrote:
> Interesting, thanks for pointing that (this function is not described at all in the source code)

Yeah, it is a standard unix function so D  itself doesn't document it, but you can run "man isatty" in Linux or search for isatty on the web and get info that way.

> Now, would that work regardless of the terminal number ther user is in (ie tty1, tty2...)?

Yes.

> Also, is there a way to do accomplish the same in Windows?'

I don't think so, but not sure. This is a bit of a strange way to use this, since commonly when something wants info from stdin, taking it from the keyboard is perfectly acceptable.