Thread overview
regading detection of stdin
Dec 06, 2013
Hugo Florentino
Dec 06, 2013
Adam D. Ruppe
Dec 07, 2013
Chris Cain
December 06, 2013
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:

import std.stdio, std.file: readText;

int main(string[] args) {
  string s;
  switch (args.length) {
    case 1:
      if ((s = stdin.readln()) is null)
        writeln("No argument passed as parameter or from stdin.");
      else
        writeln("Argument passed from stdin succesfully stored in variable s.");
      scope (failure) {
        writeln("Error reading from stdin.");
        return -1;
      }
      break;
    case 2:
      s = readText(args[1]);
      scope (failure) {
        writeln("Error reading from file passed as parameter.");
        return -2;
      }
      writeln("Argument passed as parameter succesfully stored in variable s.");
      break;
    default:
      writeln("Incorrect number of parameters. Maximum is one.");
      return -3;
  }
  return 0;
}


Where is the problem?

Regards, Hugo
December 06, 2013
On Friday, 6 December 2013 at 00:24:22 UTC, Hugo Florentino wrote:
> if nothing is passed from stdin and no parameter is provided, the application freezes:

Does it freeze or just wait for you to press enter on the keyboard?
December 07, 2013
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?