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



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.  However, my variables
get each byte from the output, but the widgets only
show it after the process ends and I would like to
show its values as far as I read them (the main idea
of ping or traceroute).

Some alternatives that I discarded:
- I could try libzvt or vte, but, I'd would like to process
  the output nicely (ex. divide the values in columns
  to show them in a GtkTreeView)
- I could program the sockets directly, but, ping and 
  traceroute use ICMP, so, in this case SUID is required
  and we don't want this.

I ask for gtk_events_pending (), its TRUE, but the TextView
is not updated.  g_print works, so the values are read
every time.  g_print use fflush, but I'm expecting that
gtk_main_iteration do something like that on the widget.

So, my guess the main program is blocked until the
process ends.  

I'd appreaciatte any hint or idea about what I missing,
or if my thought are right or wrong.

Part of my code:

void
add_text_buffer_with_pipe (
        const gchar * host, 
        GtkTextBuffer * text_buffer)
{
[...]
   if (pipe (pfd) == 0) {
      if ((pid = fork ()) == 0) {
          /*  We're in the child */
          close (1);      /* close normal stdout */
          dup (pfd[1]);   /* make stdout same as pfd[1] */
          close (pfd[0]); /* we don't need this */

          execlp ("ping", "ping", "-c", "5", host, NULL);

          g_error ("Unable to execute 'ping'");

       } else {
          /* We're in the parent process. */
          close (0);       /* close normal stdin */
          dup (pfd[0]);    /* make stdin same as pfd[0] */
          close (pfd[1]);  /* we don't need this */

          while (TRUE) {
             gsize bytes_written;
             gssize n;
                                
             bzero (buffer, sizeof (buffer)); /* Clear buffer */

             if ((n = read (pfd[0], buffer, BUFSIZ)) == 0)
                break;

             text = g_locale_to_utf8 (buffer, n,
                         NULL, &bytes_written, NULL);

             gtk_text_buffer_insert_at_cursor (
                         text_buffer, text, bytes_written);

             if (gtk_events_pending ()) {
                g_print ("events pending...\n"); /* it works */
                gtk_main_iteration ();
             }
             g_free (text);
             g_print ("%s", buffer); /* it works */
          }
          close (pfd[0]);
       }
   }
}

Thanks in advace,

-- 
German Poo Caaman~o
mailto:gpoo ubiobio cl
http://www.ubiobio.cl/~gpoo/chilelindo.html
"Hay 10 tipos de personas: las que entienden binario y las que no."




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