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 -> The current stderr file decriptor is unchanged.
00094 // efd = 0 -> The stderr file descriptor is set to the original logging FD
00095 // efd > 0 -> The stderr file descriptor is set to the value of efd.
00096 //
00097 int          Exec(const char *,  int inrd=0, int efd=0);
00098 int          Exec(      char **, int inrd=0, int efd=0);
00099 
00100 // Get the file descriptor number associated with a stream
00101 //
00102 int          FDNum() {return FD;}
00103 int          FENum() {return FE;}
00104 
00105 // Flush any remaining output queued on an output stream.
00106 //
00107 void         Flush() {fsync(FD); if (FE != FD) fsync(FE);}
00108 
00109 // Get the next record from a stream. Return null upon eof or error. Use
00110 // LastError() to determine which condition occurred (an error code of 0
00111 // indicates that end of file has been reached). Upon success, a pointer
00112 // to the next record is returned. The record is terminated by a null char.
00113 //
00114 char        *GetLine();
00115 
00116 // Get the next blank-delimited token in the record returned by Getline(). A
00117 // null pointer is returned if no more tokens remain. Each token is terminated
00118 // a null byte. Note that the record buffer is modified during processing. The
00119 // first form returns simply a token pointer. The second form returns a token
00120 // pointer and a pointer to the remainder of the line with no leading blanks.
00121 // The lowcase argument, if 1, converts all letters to lower case in the token.
00122 // RetToken() simply backups the token scanner one token. None of these
00123 // methods perform variable substitution (see GetxxxWord() below).
00124 //
00125 char        *GetToken(int lowcase=0);
00126 char        *GetToken(char **rest, int lowcase=0);
00127 void         RetToken();
00128 
00129 // Get the next word, ignoring any blank lines and comment lines (lines whose
00130 // first non-blank is a pound sign). Words are returned until logical end of
00131 // line is encountered at which time, a null is returned. A subsequent call
00132 // will return the next word on the next logical line. A physical line may be
00133 // continued by placing a back slash at it's end (i.e., last non-blank char).
00134 // GetFirstWord() always makes sure that the first word of a logical line is
00135 // returned (useful for start afresh after a mid-sentence error). GetRest()
00136 // places the remining tokens in the supplied buffer; returning 0 if the
00137 // buffer was too small. All of these methods perform variable substitution
00138 // should an XrdOucEnv object be passed to the constructor.
00139 //
00140 char        *GetFirstWord(int lowcase=0);
00141 char        *GetMyFirstWord(int lowcase=0);
00142 int          GetRest(char *theBuf, int Blen, int lowcase=0);
00143 char        *GetWord(int lowcase=0);
00144 
00145 // Indicate wether there is an active program attached to the stream
00146 //
00147 #ifndef WIN32
00148 inline int  isAlive() {return (child ? kill(child,0) == 0 : 0);}
00149 #else
00150 inline int  isAlive() {return (child ? 1 : 0);}
00151 #endif
00152 
00153 // Return last error code encountered.
00154 //
00155 inline int   LastError() {int n = ecode; ecode = 0; return n;}
00156 
00157 // Return the last input line
00158 //
00159 char        *LastLine() {return recp;}
00160 
00161 // Suppress echoing the previous line when the next line is fetched.
00162 //
00163 int          noEcho() {llBok = 0; return 0;}
00164 
00165 // Write a record to a stream, if a length is not given, then the buffer must
00166 // be null terminated and this defines the length (the null is not written).
00167 //
00168 int          Put(const char *data, const int dlen);
00169 inline int   Put(const char *data) {return Put(data, strlen(data));}
00170 
00171 // Write record fragments to a stream. The list of fragment/length pairs ends
00172 // when a null pointer is encountered.
00173 //
00174 int          Put(const char *data[], const int dlen[]);
00175 
00176 // Insert a line into the stream buffer. This replaces anything that was there.
00177 //
00178 int          PutLine(const char *data, int dlen=0);
00179 
00180 // Set the Env (returning the old Env). This is useful for suppressing
00181 // substitutions for a while.
00182 //
00183 XrdOucEnv   *SetEnv(XrdOucEnv *newEnv)
00184                    {XrdOucEnv *oldEnv = myEnv; myEnv = newEnv; return oldEnv;}
00185 
00186 // Set error routing
00187 //
00188 void         SetEroute(XrdSysError *eroute) {Eroute = eroute;}
00189 
00190 // A 0 indicates that tabs in the stream should be converted to spaces.
00191 // A 1 inducates that tabs should be left alone (the default).
00192 //
00193 void         Tabs(int x=1) {notabs = !x;}
00194 
00195 // Wait for inbound data to arrive. The argument is the max number of millisec
00196 // to wait (-1 means wait forever). Returns 0 if data is present. Otherwise,
00197 // -1 indicates that the connection timed out, a positive value indicates an
00198 // error and the value is the errno describing the error.
00199 //
00200 int          Wait4Data(int msMax=-1);
00201 
00202 /******************************************************************************/
00203   
00204 private:
00205         char *add2llB(char *tok, int reset=0);
00206         char *doelse();
00207         char *doif();
00208         int   isSet(char *var);
00209         char *vSubs(char *Var);
00210         int   xMsg(const char *txt1, const char *txt2=0, const char *txt3=0);
00211 
00212 static const int maxVLen = 512;
00213 static const int llBsz   = 1024;
00214 
00215         int   FD;
00216         int   FE;
00217         int   bsize;
00218         int   bleft;
00219         char *buff;
00220         char *bnext;
00221         char *recp;
00222         char *token;
00223         int   flags;
00224         pid_t child;
00225         int   ecode;
00226         int   notabs;
00227         int   xcont;
00228         int   xline;
00229         char *myInst;
00230         char *myHost;
00231         char *myName;
00232         char *myExec;
00233  XrdSysError *Eroute;
00234  XrdOucEnv   *myEnv;
00235         char *varVal;
00236  const  char *llPrefix;
00237         char *llBuff;
00238         char *llBcur;
00239         int   llBleft;
00240         char  Verbose;
00241         char  sawif;
00242         char  skpel;
00243         char  llBok;
00244 };
00245 #endif

Generated on 27 Jul 2013 for xrootd by  doxygen 1.4.7