xrootd
XrdOucProg.hh
Go to the documentation of this file.
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)
00046                       : eDest(errobj), myStream(0), myProc(0), ArgBuff(0),
00047                         numArgs(0), theEFD(-1) {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 // up to four additional arguments that will be added to the end of any
00072 // existing arguments. The ending status code of the program is returned.
00073 //
00074 int          Run(XrdOucStream *Sp,  const char *arg1=0, const char *arg2=0,
00075                                     const char *arg3=0, const char *arg4=0);
00076 
00077 int          Run(const char *arg1=0, const char *arg2=0,
00078                  const char *arg3=0, const char *arg4=0);
00079 
00080 int          Run(char *outBuff, int outBsz,
00081                  const char *arg1=0, const char *arg2=0,
00082                  const char *arg3=0, const char *arg4=0);
00083 
00084 // RunDone should be called to drain the output stream and get the ending
00085 // status of the running process.
00086 //
00087 int          RunDone(XrdOucStream &cmd);
00088 
00089 // Start executes the command that was passed via Setup(). The started
00090 // program is expected to linger so that you can send directives to it
00091 // via its standard in. Use Feed() to do this. If the output of the command
00092 // is wanted, use getStream() to get the stream object and use it to read
00093 // lines the program sends to standard out.
00094 //
00095 int          Start(void);
00096 
00097 // Setup takes a command string and sets up a parameter list. If a Proc pointer
00098 // is passed, then the command executes via that function. Otherwise, it checks
00099 // that the program (first token) is executable.
00100 // Zero is returned upon success, otherwise a -errno is returned,
00101 //
00102 int          Setup(const char *prog, 
00103                    XrdSysError *errP=0,
00104                    int (*Proc)(XrdOucStream *, char **, int)=0
00105                   );
00106 
00107 /******************************************************************************/
00108   
00109 private:
00110   int           Restart();
00111   XrdSysError  *eDest;
00112   XrdOucStream *myStream;
00113   int           (*myProc)(XrdOucStream *, char **, int);
00114   char         *ArgBuff;
00115   char         *Arg[64];
00116   int           numArgs;
00117   int           lenArgs;
00118   int           theEFD;
00119 };
00120 #endif