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

Generated on 5 Oct 2016 for xrootd by  doxygen 1.4.7