00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 ************************************************************************/ 00021 00022 00023 // OSCommand.h: interface for the OSCommand class. 00024 // 00025 ////////////////////////////////////////////////////////////////////// 00026 00027 #if !defined(AFX_OSCOMMAND_H__1682DCA5_6086_49F9_8026_B40B6B373A56__INCLUDED_) 00028 #define AFX_OSCOMMAND_H__1682DCA5_6086_49F9_8026_B40B6B373A56__INCLUDED_ 00029 00030 #include "Debug.h" 00031 00032 #ifndef WIN32 00033 #ifdef _WIN32 00034 #define WIN32 00035 #endif 00036 #endif 00037 00038 #ifdef WIN32 00039 #include <windows.h> 00040 #define popen _popen 00041 #define pclose _pclose 00042 #else 00043 #include <errno.h> 00044 #include <sys/wait.h> 00045 #include <fcntl.h> 00046 #endif 00047 00048 #include "Object.h" 00049 #include "JString.h" 00050 #include "JTime.h" 00051 #include "Collections.h" 00052 00053 namespace cmlabs { 00054 00055 class OSCommand : public Object 00056 { 00057 public: 00058 OSCommand(); 00059 OSCommand(const JString& cmdline, const JString& startupdir = ""); 00060 ~OSCommand(); 00061 00062 Object* clone() const {return NULL;} 00063 bool unitTest(); 00064 00065 bool setBinary(); 00066 bool setASCII(); 00067 JString getCommand(); 00068 00069 bool start(); 00070 bool terminate(); 00071 bool restart(); 00072 00073 // Queries 00074 bool hasBeenStarted(); 00075 bool isRunning(); 00076 bool hasTerminated(); 00077 bool waitForProcessToFinish(int timeout); 00078 int getProcID(); 00079 00080 00081 // Standard Input, Output and Error 00082 bool writeToStandardInput(const JString& text); 00083 bool writeToStandardInput(char* bytes, int len); 00084 00085 bool hasOutput(); 00086 int getOutputCharCount(); 00087 bool hasErrors(); 00088 int getErrorCharCount(); 00089 int getReturnCode(); 00090 00091 JString getStartupDir(); 00092 00093 JString getCurrentOutput(); 00094 JString getCurrentError(); 00095 JString getNewOutput(); 00096 JString getNewError(); 00097 00098 JString readOutputLine(int timeout); 00099 Collection readCurrentLines(); 00100 char* readOutputBytes(int length, int* readcount, int timeout = 0); 00101 00102 JString waitForProcessToFinishAndGetOutput(int timeout); 00103 JString waitForProcessToFinishAndGetError(int timeout); 00104 00105 JTime getStartedTime(); 00106 JTime getEndedTime(); 00107 00108 JString alias; 00109 bool consoleOutput; 00110 bool autoRestart; 00111 JString status; 00112 JString startupDir; 00113 00114 private: 00115 JString command; 00116 int procID; 00117 int returncode; 00118 bool binary; 00119 00120 bool init(); 00121 int readIntoBuffers(); 00122 bool resizeOutputBuffer(int max); 00123 bool resizeErrorBuffer(int max); 00124 bool cleanUp(); 00125 00126 char* outbuffer; 00127 int outbuffercount; 00128 int outbuffermax; 00129 char* errbuffer; 00130 int errbuffercount; 00131 int errbuffermax; 00132 00133 JString outputHistory; 00134 JString errorHistory; 00135 JTime startedTime; 00136 JTime endedTime; 00137 00138 // OS specific stuff... 00139 #ifdef WIN32 00140 PROCESS_INFORMATION procInfo; 00141 HANDLE proc_stdin; 00142 HANDLE proc_stdout; 00143 HANDLE proc_stderr; 00144 #else //WIN32 00145 FILE* proc_stdin; 00146 FILE* proc_stdout; 00147 FILE* proc_stderr; 00148 00149 int stdin_pipe[2]; 00150 int stdout_pipe[2]; 00151 int stderr_pipe[2]; 00152 #endif //WIN32 00153 00154 00155 00156 00157 00158 }; 00159 00160 } // namespace cmlabs 00161 00162 #endif // !defined(AFX_OSCOMMAND_H__1682DCA5_6086_49F9_8026_B40B6B373A56__INCLUDED_)