00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Mark Elendt 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_WritePipe.h ( UT Library, C++) 00015 * 00016 * COMMENTS: This is a write pipe which allows you to poll the pipe to see 00017 * if the child process has completed. It works almost exactly 00018 * like popen/pclose except for a few things: 00019 * 1: You can easily get the pid of the child 00020 * 2: The close() method has an option to not block 00021 * and return even if the child is running. 00022 * 00023 * CAVEATS: This class does not work for read pipes, but then again, read 00024 * pipes don't have the problem of blocking on the child 00025 * process... 00026 */ 00027 00028 #ifndef __UT_WritePipe__ 00029 #define __UT_WritePipe__ 00030 00031 #include "UT_API.h" 00032 #include <sys/types.h> 00033 #include <stdio.h> 00034 #include "UT_Lock.h" 00035 #include "UT_SysClone.h" 00036 00037 class UT_API UT_WritePipe { 00038 public: 00039 explicit UT_WritePipe(const char *cmd = 0, int rmanproc = 0); 00040 00041 // If the pipe is open and destructed, the process will be blocked until 00042 // the child is terminated. 00043 ~UT_WritePipe(); 00044 00045 FILE *open(const char *cmd); 00046 00047 // Close: Close will return 00048 // -1 - Error closing the pipe 00049 // 0 - Child is still running (only returned if the arg is 0) 00050 // >0 - Child is terminated 00051 // After close is called, it is not safe to write to the pipe anymore 00052 // (since the file descriptor is closed). Please use isComplete() to see 00053 // if the child process has terminated. 00054 int close(int wait_for_child_to_terminate = 0); 00055 00056 // Call this to find out whether it's safe to close (i.e. the child process 00057 // has terminated). This can be called before, or after the pipe has been 00058 // closed. 00059 int isComplete(int block=0); 00060 00061 void suspend(); 00062 void restart(); 00063 void terminate(); 00064 int isSuspended() const; 00065 00066 FILE *getFilePtr() { return myFilePtr; } 00067 00068 // If close() fails, you can get the errno of the fail call by calling 00069 // this method. If the child is still running, the error will be 00070 // set to EWOULDBLOCK 00071 int getErrno() const { return myErrno; } 00072 00073 // If close() succeeds, you can get the exit status of the child process by 00074 // calling the following method. 00075 int getStatus() const { return myExitStatus; } 00076 00077 // This method will return the child process id. 00078 pid_t getPid() const { return myPid; } 00079 00080 #ifdef WIN32 00081 void setRmanProc(int on) { myRmanProc = on; } 00082 FILE *doPOpen( char *cmd ); 00083 #endif 00084 00085 private: 00086 FILE *myFilePtr; 00087 int myErrno; 00088 int myExitStatus; 00089 unsigned myFlag; 00090 pid_t myPid; 00091 #ifdef WIN32 00092 void *myHThread; 00093 void *myHProcess; 00094 void *myPipe; 00095 int myRmanProc; 00096 #else 00097 UT_Lock myCompleteLock; 00098 #endif 00099 }; 00100 00101 #endif 00102
1.5.9