[fxruby-users] capturing stdout, stdin and stderr

Jeroen van der Zijp jeroen at fox-toolkit.org
Thu Nov 6 08:13:33 EST 2008


On Thursday 06 November 2008, angico wrote:
> Em Qui, 2008-11-06 às 09:16 +0400, Rovshan Baghirov escreveu:
> > Example from Programming Ruby book:
> > 
> > pig = IO.popen("/usr/local/bin/pig", "w+")
> > pig.puts "ice cream after they go to bed"
> > pig.close_write
> > puts pig.gets
> 
> Thank you, guys. I guess it's a starting point.
> 
> But what I really need is to have an application's stdin "connected" to
> an FXInputDialog, in order to get user input, instead of the console.
> 
> Let's say I want to create a front-end to scp. The user will enter the
> name of the host and the full filename in a dialog. Then I pass in these
> data to scp through, say, popen. Ok. Then, scp will need to ask the user
> for their password. Instead of the console asking the user to enter that
> password, I'd like to catch this (including the message
> "angico at 192.168.1.3's password: ") and present them to the user in an
> FXInputDialog. I'm not sure if I'm being clear.
> 
> Anyway, does anybody know how can I achieve this? Any idea? Any
> tutorial?
> 
> Thanks, again,
> 

You can open two pipes and pass these along as stdin and stdout of the
program you're starting:

	pipe(in);
	pipe(out);

	pid=fork();

	if(pid==0){
	  ::close(out[0]);
	  ::dup2(out[1],STDOUT_FILENO);
	  ::close(in[1]);
	  ::dup2(in[0],STDIN_FILENO);
	  ::execl(path,...,NULL);
	  ::exit(1);
	  }
        else if(pid>0){
          ::close(out[1]);
          ::close(in[0]);
          getApp()->addInput(obj,ID_INPUTREAD,out[0],INPUT_READ,(void*)(FXival)out[0]);
	  }

In a nutshell.  There are details omitted for brevity's sake.

The handler for ID_INPUT_READ will get invoked when the program "path" writes to
stdout.  It will then have to present this text to the user and write to in[1]
to talk back to the program (using write()).

Hope this helps,

		- Jeroen





-- 
+----------------------------------------------------------------------------+
| Copyright (C) 07:00 11/ 6/2008 Jeroen van der Zijp.   All Rights Reserved. |
+----------------------------------------------------------------------------+


More information about the fxruby-users mailing list