Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members

FXProcess.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                  Child process object/interface                               *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2003 by Mathew Robertson.   All Rights Reserved.                *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************/
00022 #ifndef FXPROCESS_H
00023 #define FXPROCESS_H
00024 
00025 #include "FXBaseObject.h"
00026 namespace FXEX {
00027 class FXProcessManager;
00028 
00029 /*
00030  * FXProcess handles starting/stopping and communicating with child processes.
00031  *
00032  * If no command string is supplied, the current process is copied.
00033  * If you supplied a command string, that program is executed.
00034  *
00035  * The stdio streams, stdout, stderr of the child process, are connected to this
00036  * object, which then forwards the events to the app, via FOX events.  Any data written
00037  * to this object, is forwarded to the stdin of the child process, which ends up as a FOX
00038  * IOHandle read event.
00039  */
00040 class FXAPI FXProcess : public FXBaseObject {
00041   FXDECLARE(FXProcess)
00042 
00043 private:
00044   FXint         exitcode;   // exit code of the child process
00045   FXint         code;       // status of this child process handler
00046   FXint         pid;        // process ID of process (-1 if no process)
00047   FXbool  child;      // indicates if this instance is a child or parent
00048   FXString      command;    // command string
00049   FXStringList  arguments;  // arguments to the command
00050   FXIOHandle   *in;         // stdin of the child process
00051   FXIOHandle   *out;        // stdout of the child process
00052   FXIOHandle   *err;        // stderr of the child process
00053 
00054 protected:
00055   /// serialisation
00056   FXProcess();
00057 
00058   /// Return handle to the process manager
00059   FXProcessManager* getProcessManager();
00060 
00061 public:
00062   enum {
00063     ID_PROCESS=FXBaseObject::ID_LAST,
00064     ID_STDIN,
00065     ID_STDOUT,
00066     ID_STDERR,
00067     ID_LAST
00068     };
00069 
00070 public:
00071   long onChildDeath(FXObject*,FXSelector,void*);
00072   long onTerminate(FXObject*,FXSelector,void*);
00073   long onStdin(FXObject*,FXSelector,void*);
00074   long onStdout(FXObject*,FXSelector,void*);
00075   long onStderr(FXObject*,FXSelector,void*);
00076 
00077 public:
00078 
00079   /// Create a child instance of this process
00080   FXProcess(FXApp* a,FXObject* tgt=NULL,FXSelector sel=0);
00081 
00082   /// Create a child process which starts an external program
00083   FXProcess(FXApp* a,const FXString& cmd,const FXStringList& args,FXObject* tgt=NULL,FXSelector sel=0);
00084 
00085   /// start the child process
00086   FXbool start();
00087 
00088   /// Get PID of started process (-1 otherwise)
00089   FXint PID() const { return pid; }
00090 
00091   /// return the exit status
00092   FXint exitStatus() const { return exitcode; }
00093 
00094   /// return the status
00095   FXint status() const { return code; }
00096 
00097   /// Test if child process is running.
00098   FXbool isRunning() const;
00099 
00100   /// Show if this instance a child process
00101   FXbool isChild() const { return child; }
00102 
00103   /// Try to terminate the child process.
00104   FXbool terminate(FXint code=15);
00105 
00106   /**
00107    * Either:
00108    * - when in parent context
00109    *   - send some data to the child process via this child's stdin channel (in parent context)
00110    *   - outOfBand is ignored
00111    * - when in child context
00112    *   - send some data to parent via the child's stdout channel
00113    *     (this is equivalent to writing to stdout)
00114    *   - if outOfBand=TRUE, write data to child's error channel
00115    *     (this is equivalent to writing to stderr, such as using fxmessage())
00116    */
00117   void write(FXuchar *data,FXuint size,FXbool outOfBand=FALSE);
00118 
00119   /**
00120    * Either:
00121    * - when in parent context
00122    *   - read data from stdout of child
00123    *   - if outOfBand=TRUE, read data from stderr of child
00124    * - when in child context
00125    *   - read data from write() of parent
00126    *   - outOfBand is ignored
00127    */
00128   FXint read(FXuchar* data,FXuint size,FXbool outOfBand=FALSE);
00129 
00130   /// send some text to the child process
00131   void setText(const FXString& message);
00132 
00133   /// read some text from stdout from child
00134   FXString getText();
00135 
00136   /// destructor
00137   virtual ~FXProcess();
00138   };
00139 
00140 } // namespace FXEX
00141 #endif // FXPROCESS