Re: The right way to get the ouput from a pipe into a widget



On Tue, Dec 10, 2002 at 07:21:54PM -0300, German Poo Caaman~o wrote:

I'm trying to connect the ouput from programs like ping and
traceroute and show its results into some GTK widgets.  
So, I use pipe, fork and exec.

Use g_spawn_async_with_pipes() instead, it'll immediately make this a
lot simpler.
 
I'd appreaciatte any hint or idea about what I missing,
or if my thought are right or wrong.

What you want to do is:

 - create the child process with g_spawn_async_with_pipes()
   creating a pipe on the child's stdout/stderr
 - use g_io_add_watch() to add a callback to be invoked 
   when there is input on the pipe (G_IO_IN)
 - when there's input, do a single call to read()
   and write the data that you read to the text view. 
   Don't read() twice, or you may block. (Unless you set the 
   pipe nonblocking)
 - when you get G_IO_HUP or G_IO_ERR passed to your IO watch 
   callback, the child process will have exited, so you want 
   to close your end of the pipe, and remove the IO watch.

Havoc



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]