Monday 15 June 2015

c - How to spawn a new application from Gnome/GTK to a CLI application and read its output back into Gnome/GTK application? -


I am trying to create a new application in the CIO application from genome / GTK and its output to my genome / GTK Have read back in application.

The code expects most shell commands but does not do this with tail -f / var / log / messages and top . Is there any way to parse the output from these two commands?

Ubuntu 12.04.2 LTS and LibGetic 2.0 are running.

  #include> Gtk / gtk.h> Fixed GtkWidget * window; // Indicator window fixed GtkWidget * view; // Pointer TextVow Stable GtkTextBuffer * Buffer; // Indicator buffer static GTKetEtyiter Year; // gtktextview Iterator Fixed GtkWidget * scrolled_window; // pointer to scroll window FILE * fp; // Indicator studin / out file pipe care line [256]; // line buffer size reads stable gabolian on_delete_event (GtkWidget * widget, GdkEvent * event, gpointer data) {gtk_main_quit (); return false; } Zero create_gui () {window = gtk_window_new (GTK_WINDOW_TOPLEVEL); G_signal_connect (G_OBJECT (window), "deleted_atvent", golBlack (on_delete_event), null); Gtk_window_set_title (GTK_WINDOW (window), "GUI front end for CLI"); GtkWidget * box = gtk_vbox_new (FALSE, 10); // Create a box and gtk_container_add (GTK_CONTAINER (window), box); // Set it as the main child of the window Scrolled_window = gtk_scrolled_window_new (NULL, NULL); // Create scroll window now ... gtk_widget_set_usize (scroll_windows, 650, 450); // set scroll window size .. gtk_container_add (GTK_CONTAINER (box), scrolled_window); // Add scroll window to vbox. View = gtk_text_view_new (); // Create Text View .. gtk_container_add (GTK_CONTAINER (scroll_windows), see); // Add textview to scrolled window .. buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (see)); // denote text view buffer} populate zero () {fp = popen ("cat / var / log / messages", "r"); // while running pipe using run shell command (fgets (line, size, line, FP)) // gtk_text_buffer_get_end_iter (buffer, & iter); Gtk_text_buffer_insert (buffer, and ether, line, -1); } Pclose (fp); // CLOSE pipe} int main (int argc, char * argv []) {gtk_init (and argc, and argv); Create_gui (); Populate(); Gtk_widget_show_all (window); Gtk_main (); Return 0; } // TODO // fp = popen ("ls -al", "r"); // fp = popen ("cat / proc / cpuinfo", "r"); // fp = popen ("cat / var / log / message", "r"); // fp = popen ("lspci -v", "r"); // fp = popen ("w", "r"); // fp = popen ("last", "r"); // fp = popen ("pstree", "r"); // fp = popen ("tail -f / var / log / message"; "r"); this does not work ! // fp = popen ("top", "r"); There is no one here. Enter the code here    

top and tail -f are long-running programs (in fact, they never get out), so their strategy of reading the entire production and explaining the textbuff with it clearly Will not work.

Instead, you need to create an IO channel which is running with pipes, hooking the channel into an event loop, and adding it as a new buffer The new output comes in the form. At least the changes in your program will have to rewrite the ready function in this way:

  Fixed Gabolian data_ ready (GeoGangal * channel, geocoding bracket, gp, data) {FILE * Fp = data; Four line [256]; If (fgets (line, shape line, fp)) {gtk_text_buffer_get_end_iter (buffer, and iter); Gtk_text_buffer_insert (buffer, and ether, line, -1); Return TRUE; } Else {fclose (fp); return false; }} Zero populate () {FILE * fp = popen ("top -b", "r"); Geohennal * channel = g_o_chainel_inix_nu (filino (FP)); G_io_add_watch (channel, g_india, data_d, fp); }    

No comments:

Post a Comment