xrootd
XrdOucStream.hh
Go to the documentation of this file.
00001 #ifndef __OOUC_STREAM__
00002 #define __OOUC_STREAM__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                       X r d O u c S t r e a m . h h                        */
00006 /*                                                                            */
00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*       All Rights Reserved. See XrdInfo.cc for complete License Terms       */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*                DE-AC03-76-SFO0515 with the Deprtment of Energy             */
00011 /******************************************************************************/
00012 
00013 #include <sys/types.h>
00014 #include <signal.h>
00015 #include <stdlib.h>
00016 #ifdef WIN32
00017 #include "XrdSys/XrdWin32.hh"
00018 #endif
00019 
00020 #include "XrdSys/XrdSysError.hh"
00021 
00022 class XrdOucEnv;
00023 
00024 class XrdOucStream
00025 {
00026 public:
00027 
00028 // When creating a stream object, you may pass an optional error routing object.
00029 // If you do so, error messages will be writen via the error object. Otherwise,
00030 // errors will be returned quietly.
00031 //
00032             XrdOucStream(XrdSysError *erobj=0, const char *ifname=0,
00033                          XrdOucEnv   *anEnv=0, const char *Pfx=0);
00034 
00035            ~XrdOucStream() {Close(); if (myInst) free(myInst);
00036                                      if (varVal) delete [] varVal;
00037                                      if (llBuff) free(llBuff);
00038                            }
00039 
00040 // Attach a file descriptor to an existing stream. Any curently associated
00041 // stream is closed and detached. An optional buffer size can be specified.
00042 // Zero is returned upon success, otherwise a -1 (use LastError to get rc).
00043 //
00044 int          Attach(int FileDescriptor, int bsz=2047);
00045 int          AttachIO(int infd, int outfd, int bsz=2047);
00046 
00047 // Close the current stream and release the associated buffer.
00048 //
00049 void         Close(int hold=0);
00050 
00051 // Detach a file descriptor from a stream. This should be called prior to
00052 // close/delete when you are managing your own descriptors. Return the FD num.
00053 //
00054 int          Detach() {int oldFD = FD; FD = FE = -1; return oldFD;}
00055 
00056 // Wait for an Exec() to finish and return the ending status. Use this
00057 // function only when you need to find out the ending status of the command.
00058 //
00059 int          Drain();
00060 
00061 // Display last valid line if variable substitution enabled. Fully formed
00062 // input lines are displayed if 'set -v' was encountered (only when using
00063 // the GetxxxWord() methods),
00064 //
00065 void         Echo();
00066 
00067 // Execute a command on a stream. Returns 0 upon success or -1 otherwise.
00068 // Use LastError() to get the actual error code. Subsequent Get() calls
00069 // will return the standard output of the executed command. If inrd=1 then
00070 // standardin is redirected so that subqseuent Put() calls write to the
00071 // process via standard in. When inrd=-1 then the current attached FD's are
00072 // used to redirect STDIN and STDOUT of the child process. Standard error
00073 // is handled as determined by the efd argument:
00074 // efd < 0 -> The current stderr file decriptor is unchanged.
00075 // efd = 0 -> The stderr file descriptor is set to the original logging FD
00076 // efd > 0 -> The stderr file descriptor is set to the value of efd.
00077 //
00078 int          Exec(const char *,  int inrd=0, int efd=0);
00079 int          Exec(      char **, int inrd=0, int efd=0);
00080 
00081 // Get the file descriptor number associated with a stream
00082 //
00083 int          FDNum() {return FD;}
00084 int          FENum() {return FE;}
00085 
00086 // Flush any remaining output queued on an output stream.
00087 //
00088 void         Flush() {fsync(FD); if (FE != FD) fsync(FE);}
00089 
00090 // Get the next record from a stream. Return null upon eof or error. Use
00091 // LastError() to determine which condition occurred (an error code of 0
00092 // indicates that end of file has been reached). Upon success, a pointer
00093 // to the next record is returned. The record is terminated by a null char.
00094 //
00095 char        *GetLine();
00096 
00097 // Get the next blank-delimited token in the record returned by Getline(). A
00098 // null pointer is returned if no more tokens remain. Each token is terminated
00099 // a null byte. Note that the record buffer is modified during processing. The
00100 // first form returns simply a token pointer. The second form returns a token
00101 // pointer and a pointer to the remainder of the line with no leading blanks.
00102 // The lowcase argument, if 1, converts all letters to lower case in the token.
00103 // RetToken() simply backups the token scanner one token. None of these
00104 // methods perform variable substitution (see GetxxxWord() below).
00105 //
00106 char        *GetToken(int lowcase=0);
00107 char        *GetToken(char **rest, int lowcase=0);
00108 void         RetToken();
00109 
00110 // Get the next word, ignoring any blank lines and comment lines (lines whose
00111 // first non-blank is a pound sign). Words are returned until logical end of
00112 // line is encountered at which time, a null is returned. A subsequent call
00113 // will return the next word on the next logical line. A physical line may be
00114 // continued by placing a back slash at it's end (i.e., last non-blank char).
00115 // GetFirstWord() always makes sure that the first word of a logical line is
00116 // returned (useful for start afresh after a mid-sentence error). GetRest()
00117 // places the remining tokens in the supplied buffer; returning 0 if the
00118 // buffer was too small. All of these methods perform variable substitution
00119 // should an XrdOucEnv object be passed to the constructor.
00120 //
00121 char        *GetFirstWord(int lowcase=0);
00122 char        *GetMyFirstWord(int lowcase=0);
00123 int          GetRest(char *theBuf, int Blen, int lowcase=0);
00124 char        *GetWord(int lowcase=0);
00125 
00126 // Indicate wether there is an active program attached to the stream
00127 //
00128 #ifndef WIN32
00129 inline int  isAlive() {return (child ? kill(child,0) == 0 : 0);}
00130 #else
00131 inline int  isAlive() {return (child ? 1 : 0);}
00132 #endif
00133 
00134 // Return last error code encountered.
00135 //
00136 inline int   LastError() {int n = ecode; ecode = 0; return n;}
00137 
00138 // Return the last input line
00139 //
00140 char        *LastLine() {return recp;}
00141 
00142 // Suppress echoing the previous line when the next line is fetched.
00143 //
00144 int          noEcho() {llBok = 0; return 0;}
00145 
00146 // Write a record to a stream, if a length is not given, then the buffer must
00147 // be null terminated and this defines the length (the null is not written).
00148 //
00149 int          Put(const char *data, const int dlen);
00150 inline int   Put(const char *data) {return Put(data, strlen(data));}
00151 
00152 // Write record fragments to a stream. The list of fragment/length pairs ends
00153 // when a null pointer is encountered.
00154 //
00155 int          Put(const char *data[], const int dlen[]);
00156 
00157 // Insert a line into the stream buffer. This replaces anything that was there.
00158 //
00159 int          PutLine(const char *data, int dlen=0);
00160 
00161 // Set the Env (returning the old Env). This is useful for suppressing
00162 // substitutions for a while.
00163 //
00164 XrdOucEnv   *SetEnv(XrdOucEnv *newEnv)
00165                    {XrdOucEnv *oldEnv = myEnv; myEnv = newEnv; return oldEnv;}
00166 
00167 // Set error routing
00168 //
00169 void         SetEroute(XrdSysError *eroute) {Eroute = eroute;}
00170 
00171 // A 0 indicates that tabs in the stream should be converted to spaces.
00172 // A 1 inducates that tabs should be left alone (the default).
00173 //
00174 void         Tabs(int x=1) {notabs = !x;}
00175 
00176 // Wait for inbound data to arrive. The argument is the max number of millisec
00177 // to wait (-1 means wait forever). Returns 0 if data is present. Otherwise,
00178 // -1 indicates that the connection timed out, a positive value indicates an
00179 // error and the value is the errno describing the error.
00180 //
00181 int          Wait4Data(int msMax=-1);
00182 
00183 /******************************************************************************/
00184   
00185 private:
00186         char *add2llB(char *tok, int reset=0);
00187         char *doelse();
00188         char *doif();
00189         int   isSet(char *var);
00190         char *vSubs(char *Var);
00191         int   xMsg(const char *txt1, const char *txt2=0, const char *txt3=0);
00192 
00193 static const int maxVLen = 512;
00194 static const int llBsz   = 1024;
00195 
00196         int   FD;
00197         int   FE;
00198         int   bsize;
00199         int   bleft;
00200         char *buff;
00201         char *bnext;
00202         char *recp;
00203         char *token;
00204         int   flags;
00205         pid_t child;
00206         int   ecode;
00207         int   notabs;
00208         int   xcont;
00209         int   xline;
00210         char *myInst;
00211         char *myHost;
00212         char *myName;
00213         char *myExec;
00214  XrdSysError *Eroute;
00215  XrdOucEnv   *myEnv;
00216         char *varVal;
00217  const  char *llPrefix;
00218         char *llBuff;
00219         char *llBcur;
00220         int   llBleft;
00221         char  Verbose;
00222         char  sawif;
00223         char  skpel;
00224         char  llBok;
00225 };
00226 #endif