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