GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 22, 2014 10:19 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Execute a command into a GTK+ application's GtkTextView
PostPosted: Mon Jul 03, 2006 10:10 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
What would y'all think would be the best way to go about....

From a GTK+ application, execute a command without waiting for the command to complete, but polling until it's complete (so that the GUI can be updated while command is running) and then load the stderr output of the command into a GtkTextView.

The method I'm currently using is popen()... but since the command outputs to stderr, I have to use 2> to redirect it to a temporary file which I then load into the GtkTextView. The problem is, once popen() is called, my application is frozen until the called command is done running. I need to open the command in a child process (I'm assuming fork) yet still be able to stay in a loop until the command is done and ideally redirect the stderr output to the parent rather than a temporary file (I'm assuming pipe).

Any help?

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 1:35 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
No, you're going about it in the correct process ... I have to do the same thing in OpenLDev to keep output scrolling. You want to use this:

Code:
FILE *in;
gchar buff[512];

in = popen (comm.c_str(), "r");
 
while (fgets (buff, sizeof (buff), in))
{
  /* Add the line(s), until \n is not found */
  while (gtk_events_pending())
    gtk_main_iteration ();
}


That will read in the command output as you run it & then update the gui, which will avoid freezing until the command is complete. Also, as you said, you want to add '2>&1' to the end of each command before executing, which will return the stderr and stdout ...

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 1:42 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
That's how I have it now, however, is there any reason I shouldn't use output redirection 2>&1? I have started doing it myslef with fork, pipe, exec, and dup2 but haven't worked it all out yet. But if I don't need to...

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 12:19 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
No, there's really no reason not to do that. All standard Linux shells are going to understand that command and popen is just as efficient as fork(), pipe(), exec(), etc.

If you are using GTK+, you might want to check out cross-platform pipes with GLib IO Channels at http://developer.gnome.org/doc/API/2.0/ ... nnels.html

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 4:27 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Right on... thanks!

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group