Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:41

0001 #ifndef __OOUC_STREAM__
0002 #define __OOUC_STREAM__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                       X r d O u c S t r e a m . h h                        */
0006 /*                                                                            */
0007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0009 /*                DE-AC02-76-SFO0515 with the Deprtment of Energy             */
0010 /*                                                                            */
0011 /* This file is part of the XRootD software suite.                            */
0012 /*                                                                            */
0013 /* XRootD is free software: you can redistribute it and/or modify it under    */
0014 /* the terms of the GNU Lesser General Public License as published by the     */
0015 /* Free Software Foundation, either version 3 of the License, or (at your     */
0016 /* option) any later version.                                                 */
0017 /*                                                                            */
0018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0020 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0021 /* License for more details.                                                  */
0022 /*                                                                            */
0023 /* You should have received a copy of the GNU Lesser General Public License   */
0024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0025 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0026 /*                                                                            */
0027 /* The copyright holder's institutional names and contributor's names may not */
0028 /* be used to endorse or promote products derived from this software without  */
0029 /* specific prior written permission of the institution or contributor.       */
0030 /******************************************************************************/
0031 
0032 #include <sys/types.h>
0033 #include <signal.h>
0034 #include <cstdlib>
0035 #ifdef WIN32
0036 #include "XrdSys/XrdWin32.hh"
0037 #endif
0038 
0039 #include "XrdSys/XrdSysError.hh"
0040 
0041 struct StreamInfo;
0042 class XrdOucEnv;
0043 class XrdOucString;
0044 class XrdOucTList;
0045 
0046 class XrdOucStream
0047 {
0048 public:
0049 
0050 // When creating a stream object, you may pass an optional error routing object.
0051 // If you do so, error messages will be writen via the error object. Otherwise,
0052 // errors will be returned quietly.
0053 //
0054             XrdOucStream(XrdSysError *erobj=0, const char *ifname=0,
0055                          XrdOucEnv   *anEnv=0, const char *Pfx=0);
0056 
0057            ~XrdOucStream() {Close(); if (myInst) free(myInst);
0058                                      if (varVal) delete [] varVal;
0059                                      if (llBuff) free(llBuff);
0060                            }
0061 
0062 // Attach a file descriptor to an existing stream. Any curently associated
0063 // stream is closed and detached. An optional buffer size can be specified.
0064 // Zero is returned upon success, otherwise a -1 (use LastError to get rc).
0065 //
0066 int          Attach(int FileDescriptor, int bsz=2047);
0067 int          AttachIO(int infd, int outfd, int bsz=2047);
0068 
0069 // Close the current stream and release the associated buffer.
0070 //
0071 void         Close(int hold=0);
0072 
0073 // Detach a file descriptor from a stream. This should be called prior to
0074 // close/delete when you are managing your own descriptors. Return the FD num.
0075 //
0076 int          Detach() {int oldFD = FD; FD = FE = -1; return oldFD;}
0077 
0078 // Wait for an Exec() to finish and return the ending status. Use this
0079 // function only when you need to find out the ending status of the command.
0080 //
0081 int          Drain();
0082 
0083 // Display last valid line if variable substitution enabled. Fully formed
0084 // input lines are displayed if 'set -v' was encountered (only when using
0085 // the GetxxxWord() methods),
0086 //
0087 void         Echo();
0088 void         Echo(bool capture);
0089 
0090 // Execute a command on a stream. Returns 0 upon success or -1 otherwise.
0091 // Use LastError() to get the actual error code. Subsequent Get() calls
0092 // will return the standard output of the executed command. If inrd=1 then
0093 // standardin is redirected so that subqseuent Put() calls write to the
0094 // process via standard in. When inrd=-1 then the current attached FD's are
0095 // used to redirect STDIN and STDOUT of the child process. Standard error
0096 // is handled as determined by the efd argument:
0097 // efd < 0 -> How to handle the current stderr file decriptor:
0098 //            -1 The current stderr file decriptor is unchanged.
0099 //               Output of only stdout is to be captured by this stream.
0100 //            -2 Output of only stderr is to be captured by this stream.
0101 //            -3 Output of stdout and stderr is to be captured by this stream.
0102 // efd = 0 -> The stderr file descriptor is set to the original logging FD
0103 // efd > 0 -> The stderr file descriptor is set to the value of efd.
0104 //
0105 int          Exec(const char *,  int inrd=0, int efd=0);
0106 int          Exec(      char **, int inrd=0, int efd=0);
0107 
0108 // Get the file descriptor number associated with a stream
0109 //
0110 int          FDNum() {return FD;}
0111 int          FENum() {return FE;}
0112 
0113 // Flush any remaining output queued on an output stream.
0114 //
0115 void         Flush() {fsync(FD); if (FE != FD) fsync(FE);}
0116 
0117 // Get the next record from a stream. Return null upon eof or error. Use
0118 // LastError() to determine which condition occurred (an error code of 0
0119 // indicates that end of file has been reached). Upon success, a pointer
0120 // to the next record is returned. The record is terminated by a null char.
0121 //
0122 char        *GetLine();
0123 
0124 // Get the next blank-delimited token in the record returned by Getline(). A
0125 // null pointer is returned if no more tokens remain. Each token is terminated
0126 // a null byte. Note that the record buffer is modified during processing. The
0127 // first form returns simply a token pointer. The second form returns a token
0128 // pointer and a pointer to the remainder of the line with no leading blanks.
0129 // The lowcase argument, if 1, converts all letters to lower case in the token.
0130 // RetToken() simply backups the token scanner one token. None of these
0131 // methods perform variable substitution (see GetxxxWord() below).
0132 //
0133 char        *GetToken(int lowcase=0);
0134 char        *GetToken(char **rest, int lowcase=0);
0135 void         RetToken();
0136 
0137 // Get the next word, ignoring any blank lines and comment lines (lines whose
0138 // first non-blank is a pound sign). Words are returned until logical end of
0139 // line is encountered at which time, a null is returned. A subsequent call
0140 // will return the next word on the next logical line. A physical line may be
0141 // continued by placing a back slash at it's end (i.e., last non-blank char).
0142 // GetFirstWord() always makes sure that the first word of a logical line is
0143 // returned (useful for start afresh after a mid-sentence error). GetRest()
0144 // places the remining tokens in the supplied buffer; returning 0 if the
0145 // buffer was too small. All of these methods perform variable substitution
0146 // should an XrdOucEnv object be passed to the constructor.
0147 //
0148 char        *GetFirstWord(int lowcase=0);
0149 char        *GetMyFirstWord(int lowcase=0);
0150 int          GetRest(char *theBuf, int Blen, int lowcase=0);
0151 char        *GetWord(int lowcase=0);
0152 
0153 // Indicate wether there is an active program attached to the stream
0154 //
0155 #ifndef WIN32
0156 inline int  isAlive() {return (child ? kill(child,0) == 0 : 0);}
0157 #else
0158 inline int  isAlive() {return (child ? 1 : 0);}
0159 #endif
0160 
0161 // Return last error code encountered.
0162 //
0163 inline int   LastError() {int n = ecode; ecode = 0; return n;}
0164 
0165 // Return the last input line
0166 //
0167 char        *LastLine() {return recp;}
0168 
0169 // Suppress echoing the previous line when the next line is fetched.
0170 //
0171 int          noEcho() {llBok = 0; return 0;}
0172 
0173 // Write a record to a stream, if a length is not given, then the buffer must
0174 // be null terminated and this defines the length (the null is not written).
0175 //
0176 int          Put(const char *data, const int dlen);
0177 inline int   Put(const char *data) {return Put(data, strlen(data));}
0178 
0179 // Write record fragments to a stream. The list of fragment/length pairs ends
0180 // when a null pointer is encountered.
0181 //
0182 int          Put(const char *data[], const int dlen[]);
0183 
0184 // Insert a line into the stream buffer. This replaces anything that was there.
0185 //
0186 int          PutLine(const char *data, int dlen=0);
0187 
0188 // Set the Env (returning the old Env). This is useful for suppressing
0189 // substitutions for a while.
0190 //
0191 XrdOucEnv   *SetEnv(XrdOucEnv *newEnv)
0192                    {XrdOucEnv *oldEnv = myEnv; myEnv = newEnv; return oldEnv;}
0193 
0194 // Set error routing
0195 //
0196 void         SetEroute(XrdSysError *eroute) {Eroute = eroute;}
0197 
0198 // A 0 indicates that tabs in the stream should be converted to spaces.
0199 // A 1 inducates that tabs should be left alone (the default).
0200 //
0201 void         Tabs(int x=1) {notabs = !x;}
0202 
0203 // Wait for inbound data to arrive. The argument is the max number of millisec
0204 // to wait (-1 means wait forever). Returns 0 if data is present. Otherwise,
0205 // -1 indicates that the connection timed out, a positive value indicates an
0206 // error and the value is the errno describing the error.
0207 //
0208 int          Wait4Data(int msMax=-1);
0209 
0210 /******************************************************************************/
0211 
0212 // The following methods are norally used only during initial configuration
0213 // to capture the actual configuration being used by each component.
0214 
0215 // Capture a message (typically informational before the start of file
0216 // processing); which is added as a comment. Pass a vector of string whose
0217 // last element is 0.
0218 //
0219 static void   Capture(const char** cVec=0, bool linefeed=true);
0220 
0221 // Set the capture string object. A value of nil turns off capturing. The
0222 // current capture string pointer is returned.
0223 //
0224 static
0225 XrdOucString *Capture(XrdOucString *cfObj);
0226 
0227 // Return the current capture string object.
0228 //
0229 static
0230 XrdOucString *Capture();
0231 
0232 /******************************************************************************/
0233   
0234 private:
0235         void  add2CFG(const char *data, bool isCMT=false);
0236         char *add2llB(char *tok, int reset=0);
0237         bool  docont();
0238         bool  docont( const char *path, XrdOucTList *tlP);
0239         bool  docontD(const char *path, XrdOucTList *tlP);
0240         bool  docontF(const char *path, bool noentok=false);
0241         char *doelse();
0242         char *doif();
0243         bool  Echo(int ec, const char *t1, const char *t2=0, const char *t3=0);
0244         int   getValue(const char *path, char *vbuff, int  vbsz);
0245         int   isSet(char *var);
0246         char *vSubs(char *Var);
0247         int   xMsg(const char *txt1, const char *txt2=0, const char *txt3=0);
0248 
0249 static const int maxVLen = 512;
0250 static const int llBsz   = 1024;
0251 
0252         int   FD;
0253         int   FE;
0254         int   bsize;
0255         int   bleft;
0256         char *buff;
0257         char *bnext;
0258         char *recp;
0259         char *token;
0260         int   flags;
0261         pid_t child;
0262         int   ecode;
0263         int   notabs;
0264         int   xcont;
0265         int   xline;
0266         char *myInst;
0267  StreamInfo  *myInfo; // ABI compatible change!
0268         char *myRsv1;
0269         char *myRsv2;
0270  XrdSysError *Eroute;
0271  XrdOucEnv   *myEnv;
0272         char *varVal;
0273  const  char *llPrefix;
0274         char *llBuff;
0275         char *llBcur;
0276         int   llBleft;
0277         char  Verbose;
0278         char  sawif;
0279         char  skpel;
0280         char  llBok;
0281 static
0282 XrdOucString *theCFG;
0283 };
0284 #endif