Re: regading detection of stdin

2013-12-07 Thread Adam D. Ruppe

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.


Re: regading detection of stdin

2013-12-07 Thread Hugo Florentino

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).




Re: regading detection of stdin

2013-12-07 Thread Chris Cain

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?


Re: regading detection of stdin

2013-12-07 Thread Adam D. Ruppe

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

}


Re: regading detection of stdin

2013-12-07 Thread Hugo Florentino

On Fri, 06 Dec 2013 06:42:01 +0100, Jesse Phillips" wrote:


On Friday, 6 December 2013 at 02:41:28 UTC, Hugo Florentino wrote:


I see... so the "problem" simply was that function readln was
expecting user input. In that case, this is not what I intended. I
want the application to receive the stdout of another command as
stdin, and to detect if nothing is sent. How coud I accomplish this?


Then you'll want std.process[1]. This provides functions for
"piping" output[2]. As for detecting output, that can only be
verified once the other process has finished; to handle that it
should be reasonable to execute[3] and check the complete output.

1. http://dlang.org/phobos/std_process.html
2. http://dlang.org/phobos/std_process.html#.pipeProcess
3. http://dlang.org/phobos/std_process.html#.execute


Hmm... I am not too clear even after reading the documentation. From
what I could understand, these are all wrappers around spawnProcess and
spawnShell, which are intended to launch a child process from the
application, but that is not quite what I need.

I simply want to support this kind of use:

echo -n "This is just a test" | myapplication

The command echo here serves only as an example, it could be any other
command not necessarily open-source or programmed in D.

What can I do?

Regards, Hugo


Re: regading detection of stdin

2013-12-05 Thread Jesse Phillips

On Friday, 6 December 2013 at 02:41:28 UTC, Hugo Florentino wrote:
I see... so the "problem" simply was that function readln was 
expecting user input. In that case, this is not what I intended.
I want the application to receive the stdout of another command 
as stdin, and to detect if nothing is sent.

How coud I accomplish this?


Then you'll want std.process[1]. This provides functions for 
"piping" output[2]. As for detecting output, that can only be 
verified once the other process has finished; to handle that it 
should be reasonable to execute[3] and check the complete output.


1. http://dlang.org/phobos/std_process.html
2. http://dlang.org/phobos/std_process.html#.pipeProcess
3. http://dlang.org/phobos/std_process.html#.execute


Re: regading detection of stdin

2013-12-05 Thread Hugo Florentino

On Fri, 06 Dec 2013 01:26:28 +0100, Adam D. Ruppe wrote:


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?



I see... so the "problem" simply was that function readln was expecting 
user input. In that case, this is not what I intended.
I want the application to receive the stdout of another command as 
stdin, and to detect if nothing is sent.

How coud I accomplish this?


Re: regading detection of stdin

2013-12-05 Thread Adam D. Ruppe

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?


regading detection of stdin

2013-12-05 Thread Hugo Florentino

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