-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tue, Dec 26, 2006 at 10:41:33PM -0500, Tony Freeman wrote: > Hello, > > I need some example code or a tutorial for how to pipe output from a > process to a GtkTextBuffer. > > The idea of this program is to spawn off 6 or more ssh commands and have > the output go to it's own textbuffer in it's assigned notebook page. > > This is what I have so far. Now I need the output from the process to > go to it's respective text buffer. > > > void on_confirm_okbutton_clicked (GtkWidget *widget, gpointer data) > { > /* **************************************** > * Globals used: > * serverlist : string array of server names > * command : the options to send to mainscript program > * notebook1 : notebook widget > */ [...] > /* now fire off the process and redirect output > * to our textbuffer */ > GPid pid; > g_spawn_async_with_pipes( NULL, > ssh_command, > NULL, > G_SPAWN_CHILD_INHERITS_STDIN, > NULL, > NULL, > &pid, > NULL, NULL, NULL, > NULL ); > }
This looks like the interesting part. First of all, I doubt G_SPAWN_CHILD_INHERITS_STDIN is what you are after. I guess you'll have to give each child its own file descriptors (possibly all three, to let the user see possible errors), like so: | GPid pid; | gint stdin, stdout, stderr; | g_spawn_async_with_pipes( NULL, /* workdir */ | ssh_command, /* argv */ | NULL, /* envp */ | NULL, /* flags */ | NULL, /* child setup func */ | NULL, /* --- udata */ | &pid, | &stdin, &stdout, &stderr, | NULL ); /* error */ Now you'll have to catch whatever comes from stdout and stderr and append it to the text buffer of the text widget (possibly making sure that the in and err part don't get too messed up, and possibly making nifty things like painting the errors in red :-). Likewise, you'll have to (somehow) collect user input, sending it via stdin to the process (when it is ready to take input). To this purpose, you may wrap the descriptors in GIOChannels and watch the channels with g_io_add_watch(), which is just a fancy way to use the select() system call. I'll sketch this with stdin (the others work analogously). In a full-fledged version you'll typically want to collect all this stuff in a struct which you may attach to the corresponding notebook widget, so you may easily find all those thingies in callbacks, etc -- like so: | typedef struct { | GIOChannel *stdin; | GIOChannel *stdout; | GIOChannel *stderr; | /* other useful things like the text widget go here */ | } ssh_slave; Now comes the fun part. After the spawn_async() (and after checking for errors ;-) you do: | ssh_slave *slave = g_new(ssh_slave, 1); | slave->stdin = g_io_channel_unix_new(stdin); | GIOFunc watch_slave; | g_io_channel_add_watch_full(ch_stdin, G_IO_IN, watch_slave, slave); Then you define your watch function (I'd use the same function for all three channels, but I'm a really, really lazy person ;-) | gboolean watch_slave(GIOChannel *src, GIOCondition *cond, gpointer udata) | { | ssh_slave *slave = (ssh_slave *) udata; /* hopefully ;-) */ | gchar buf[1024]; /* or another meaningful size */ | GIOstatus st; | GIOError *err; | | /* this is for stdin, stderr: */ | st = g_io_channel_read_chars(src, buf, sizeof(buf), &got, &err); | /* handle errors */ | ... | /* append whatever you got to the text widget, which you can hopefully | pull from slave */ | ... | /* Do likewise for stdout. You might have a buffer of pending chars | hidden in slave->xxx and add/remove the "write watch" depending | on whether there is write pending (otherwise the program will hog | CPU spinning here). */ | ... | return TRUE; /* typically. Return FALSE if you want the watch removed */ | } That's at least how I go about this. Hope it gives you some ideas. Regards - -- tomás -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFFkhAHBcgs9XrR2kYRAhHNAJ49QPnWhdsb6pq/E+U2IZZM4HNeTQCeLnWv tG4pXP/VVo+bJpjOp4DTQ0k= =je6g -----END PGP SIGNATURE----- _______________________________________________ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list