00001 #ifndef __OOUC_PROG__ 00002 #define __OOUC_PROG__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d O u c P r o g . h h */ 00006 /* */ 00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00009 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */ 00010 /* */ 00011 /* This file is part of the XRootD software suite. */ 00012 /* */ 00013 /* XRootD is free software: you can redistribute it and/or modify it under */ 00014 /* the terms of the GNU Lesser General Public License as published by the */ 00015 /* Free Software Foundation, either version 3 of the License, or (at your */ 00016 /* option) any later version. */ 00017 /* */ 00018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00020 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00021 /* License for more details. */ 00022 /* */ 00023 /* You should have received a copy of the GNU Lesser General Public License */ 00024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00025 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00026 /* */ 00027 /* The copyright holder's institutional names and contributor's names may not */ 00028 /* be used to endorse or promote products derived from this software without */ 00029 /* specific prior written permission of the institution or contributor. */ 00030 /******************************************************************************/ 00031 00032 #include <sys/types.h> 00033 00034 class XrdSysError; 00035 class XrdOucStream; 00036 00037 class XrdOucProg 00038 { 00039 public: 00040 00041 // When creating an Prog object, you may pass an optional error routing object. 00042 // If you do so, error messages and all command output will be writen via the 00043 // error object. Otherwise, errors will be returned quietly. 00044 // 00045 XrdOucProg(XrdSysError *errobj=0, int efd=-1) 00046 : eDest(errobj), myStream(0), myProc(0), ArgBuff(0), 00047 numArgs(0), theEFD(efd) {Arg[0] = 0;} 00048 00049 ~XrdOucProg(); 00050 00051 // Feed() send a data to the program started by Start(). Several variations 00052 // exist to accomodate various needs. Note that should the program not be 00053 // running when Feed() is called, it is restarted. 00054 // 00055 int Feed(const char *data[], const int dlen[]); 00056 00057 int Feed(const char *data, int dlen) 00058 {const char *myData[2] = {data, 0}; 00059 const int myDlen[2] = {dlen, 0}; 00060 return Feed(myData, myDlen); 00061 } 00062 00063 int Feed(const char *data) {return Feed(data, (int)strlen(data));} 00064 00065 // getStream() returns the stream created by Start(). Use the object to get 00066 // lines written by the started program. 00067 // 00068 XrdOucStream *getStream() {return myStream;} 00069 00070 // Run executes the command that was passed via Setup(). You may pass 00071 // additional arguments to be appended to the existing ones. The 00072 // method also allows envars to be set in forked process via envV vector 00073 // which contains strings "var=val" and terminaes with a null pointer. 00074 // 00075 int Run(XrdOucStream *Sp, const char *argV[], int argc=0, 00076 const char *envV[]=0); 00077 00078 // Run executes the command that was passed via Setup(). You may pass 00079 // up to four additional arguments that will be added to the end of any 00080 // existing arguments. The ending status code of the program is returned. 00081 // 00082 int Run(XrdOucStream *Sp, const char *arg1=0, const char *arg2=0, 00083 const char *arg3=0, const char *arg4=0); 00084 00085 int Run(const char *arg1=0, const char *arg2=0, 00086 const char *arg3=0, const char *arg4=0); 00087 00088 int Run(char *outBuff, int outBsz, 00089 const char *arg1=0, const char *arg2=0, 00090 const char *arg3=0, const char *arg4=0); 00091 00092 // RunDone should be called to drain the output stream and get the ending 00093 // status of the running process. 00094 // 00095 int RunDone(XrdOucStream &cmd); 00096 00097 // Start executes the command that was passed via Setup(). The started 00098 // program is expected to linger so that you can send directives to it 00099 // via its standard in. Use Feed() to do this. If the output of the command 00100 // is wanted, use getStream() to get the stream object and use it to read 00101 // lines the program sends to standard out. 00102 // 00103 int Start(void); 00104 00105 // Setup takes a command string and sets up a parameter list. If a Proc pointer 00106 // is passed, then the command executes via that function. Otherwise, it checks 00107 // that the program (first token) is executable. 00108 // Zero is returned upon success, otherwise a -errno is returned, 00109 // 00110 int Setup(const char *prog, 00111 XrdSysError *errP=0, 00112 int (*Proc)(XrdOucStream *, char **, int)=0 00113 ); 00114 00115 /******************************************************************************/ 00116 00117 private: 00118 int Restart(); 00119 XrdSysError *eDest; 00120 XrdOucStream *myStream; 00121 int (*myProc)(XrdOucStream *, char **, int); 00122 char *ArgBuff; 00123 char *Arg[64]; 00124 int numArgs; 00125 int lenArgs; 00126 int theEFD; 00127 }; 00128 #endif