GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Execute a command into a GTK+ application's GtkTextView

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 462
Location: Portland, OR USA

PostPosted: Mon Jul 03, 2006 10:10 pm    Post subject: Execute a command into a GTK+ application's GtkTextView Reply with quote

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?
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Sun Jul 09, 2006 1:35 am    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
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 ...
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 462
Location: Portland, OR USA

PostPosted: Sun Jul 09, 2006 1:42 am    Post subject: Reply with quote

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...
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Sun Jul 09, 2006 12:19 pm    Post subject: Reply with quote

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/glib/glib-IO-Channels.html
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 462
Location: Portland, OR USA

PostPosted: Sun Jul 09, 2006 4:27 pm    Post subject: Reply with quote

Right on... thanks!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP