execbox.h

Go to the documentation of this file.
00001 
00007 #ifndef INCL_EXECBOX_H
00008 #define INCL_EXECBOX_H  1
00009 
00010 /*
00011 Copyright (C) 2002 The Exult Team
00012 
00013 This program is free software; you can redistribute it and/or
00014 modify it under the terms of the GNU General Public License
00015 as published by the Free Software Foundation; either version 2
00016 of the License, or (at your option) any later version.
00017 
00018 This program is distributed in the hope that it will be useful,
00019 but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 GNU General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; if not, write to the Free Software
00025 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00026 */
00027 
00028 #include <gtk/gtk.h>
00029 
00030 class Exec_box;
00031 class Exec_process;
00032           // Called when child is done:
00033 typedef void (*Exec_done_fun)(int exit_code, Exec_box *box, 
00034               gpointer user_data);
00035 
00036 
00037 #ifndef WIN32
00038 
00039 /*
00040  *  A class for executing a child process and capturing its output in a
00041  *  GTK program.
00042  */
00043 class Exec_process
00044   {
00045 public:
00046           // Function called when data is read
00047           //   from child.  If datalen == 0,
00048           //   child is done & exit_code is set.
00049   typedef void (*Reader_fun)(char *data, int datalen, int exit_code,
00050               gpointer user_data);
00051 private:
00052           // Pipes for talking to child:
00053   int child_stdin, child_stdout, child_stderr;
00054   int child_pid;      // Child's process ID.
00055   gint stdout_tag, stderr_tag;  // GDK tags for getting child's output.
00056   Reader_fun reader;    // Called when data read from child.
00057   void *reader_data;    // User data passed back.
00058 public:
00059   Exec_process();
00060   ~Exec_process();
00061   void kill_child();    // Kill process.
00062   void read_from_child(int id); // Read and call 'reader'.
00063           // Execute.
00064   bool exec(const char *file, char *argv[], Reader_fun rfun,
00065               void *udata);
00066   bool check_child(int& exit_code); // Is child still running?
00067   };
00068 
00069 #else
00070 
00071 class Exec_process
00072 {
00073  public:
00074   typedef void (*Reader_fun)(char *data, int datalen, int exit_code,
00075               gpointer user_data);
00076   Exec_process() {}
00077   ~Exec_process() {}
00078   void kill_child() {}
00079   void read_from_child(int id) {}
00080   bool exec(const char *file, char *argv[], Reader_fun rfun,
00081                   void *udata) { return false; }
00082   bool check_child(int& exit_code) { return false; }
00083   };
00084 
00085 #endif
00086 
00087 /*
00088  *  This class can execute a command-line program and display its output.
00089  */
00090 class Exec_box
00091   {
00092   Exec_process *executor;   // Handles child process.
00093   GtkTextView *box;   // Where we show responses.
00094   GtkStatusbar *status;   // For showing status.
00095   guint status_ctx;   // Context for status.
00096   Exec_done_fun done_fun;   // Called when child has exited.
00097   gpointer user_data;   // Passed to done_fun.
00098 public:
00099   Exec_box(GtkTextView *b, GtkStatusbar *s, Exec_done_fun dfun = 0,
00100               gpointer udata = 0);
00101   ~Exec_box();
00102   void show_status(const char *msg);  // Set status bar.
00103           // Handle data from child.
00104   void read_from_child(char *data, int datalen, int exit_code);
00105   void add_message(const char *txt);  // Add text to box.
00106           // Execute.
00107   bool exec(const char *file, char *argv[]);
00108   void kill_child();    // End.
00109   };
00110 
00111 #endif

Generated on Mon Jul 9 14:42:47 2007 for ExultEngine by  doxygen 1.5.1