xrootd
XrdOucStream.hh
Go to the documentation of this file.
1 #ifndef __OOUC_STREAM__
2 #define __OOUC_STREAM__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c S t r e a m . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <sys/types.h>
33 #include <signal.h>
34 #include <stdlib.h>
35 #ifdef WIN32
36 #include "XrdSys/XrdWin32.hh"
37 #endif
38 
39 #include "XrdSys/XrdSysError.hh"
40 
41 struct StreamInfo;
42 class XrdOucEnv;
43 class XrdOucTList;
44 
46 {
47 public:
48 
49 // When creating a stream object, you may pass an optional error routing object.
50 // If you do so, error messages will be writen via the error object. Otherwise,
51 // errors will be returned quietly.
52 //
53  XrdOucStream(XrdSysError *erobj=0, const char *ifname=0,
54  XrdOucEnv *anEnv=0, const char *Pfx=0);
55 
56  ~XrdOucStream() {Close(); if (myInst) free(myInst);
57  if (varVal) delete [] varVal;
58  if (llBuff) free(llBuff);
59  }
60 
61 // Attach a file descriptor to an existing stream. Any curently associated
62 // stream is closed and detached. An optional buffer size can be specified.
63 // Zero is returned upon success, otherwise a -1 (use LastError to get rc).
64 //
65 int Attach(int FileDescriptor, int bsz=2047);
66 int AttachIO(int infd, int outfd, int bsz=2047);
67 
68 // Close the current stream and release the associated buffer.
69 //
70 void Close(int hold=0);
71 
72 // Detach a file descriptor from a stream. This should be called prior to
73 // close/delete when you are managing your own descriptors. Return the FD num.
74 //
75 int Detach() {int oldFD = FD; FD = FE = -1; return oldFD;}
76 
77 // Wait for an Exec() to finish and return the ending status. Use this
78 // function only when you need to find out the ending status of the command.
79 //
80 int Drain();
81 
82 // Display last valid line if variable substitution enabled. Fully formed
83 // input lines are displayed if 'set -v' was encountered (only when using
84 // the GetxxxWord() methods),
85 //
86 void Echo();
87 
88 // Execute a command on a stream. Returns 0 upon success or -1 otherwise.
89 // Use LastError() to get the actual error code. Subsequent Get() calls
90 // will return the standard output of the executed command. If inrd=1 then
91 // standardin is redirected so that subqseuent Put() calls write to the
92 // process via standard in. When inrd=-1 then the current attached FD's are
93 // used to redirect STDIN and STDOUT of the child process. Standard error
94 // is handled as determined by the efd argument:
95 // efd < 0 -> How to handle the current stderr file decriptor:
96 // -1 The current stderr file decriptor is unchanged.
97 // Output of only stdout is to be captured by this stream.
98 // -2 Output of only stderr is to be captured by this stream.
99 // -3 Output of stdout and stderr is to be captured by this stream.
100 // efd = 0 -> The stderr file descriptor is set to the original logging FD
101 // efd > 0 -> The stderr file descriptor is set to the value of efd.
102 //
103 int Exec(const char *, int inrd=0, int efd=0);
104 int Exec( char **, int inrd=0, int efd=0);
105 
106 // Get the file descriptor number associated with a stream
107 //
108 int FDNum() {return FD;}
109 int FENum() {return FE;}
110 
111 // Flush any remaining output queued on an output stream.
112 //
113 void Flush() {fsync(FD); if (FE != FD) fsync(FE);}
114 
115 // Get the next record from a stream. Return null upon eof or error. Use
116 // LastError() to determine which condition occurred (an error code of 0
117 // indicates that end of file has been reached). Upon success, a pointer
118 // to the next record is returned. The record is terminated by a null char.
119 //
120 char *GetLine();
121 
122 // Get the next blank-delimited token in the record returned by Getline(). A
123 // null pointer is returned if no more tokens remain. Each token is terminated
124 // a null byte. Note that the record buffer is modified during processing. The
125 // first form returns simply a token pointer. The second form returns a token
126 // pointer and a pointer to the remainder of the line with no leading blanks.
127 // The lowcase argument, if 1, converts all letters to lower case in the token.
128 // RetToken() simply backups the token scanner one token. None of these
129 // methods perform variable substitution (see GetxxxWord() below).
130 //
131 char *GetToken(int lowcase=0);
132 char *GetToken(char **rest, int lowcase=0);
133 void RetToken();
134 
135 // Get the next word, ignoring any blank lines and comment lines (lines whose
136 // first non-blank is a pound sign). Words are returned until logical end of
137 // line is encountered at which time, a null is returned. A subsequent call
138 // will return the next word on the next logical line. A physical line may be
139 // continued by placing a back slash at it's end (i.e., last non-blank char).
140 // GetFirstWord() always makes sure that the first word of a logical line is
141 // returned (useful for start afresh after a mid-sentence error). GetRest()
142 // places the remining tokens in the supplied buffer; returning 0 if the
143 // buffer was too small. All of these methods perform variable substitution
144 // should an XrdOucEnv object be passed to the constructor.
145 //
146 char *GetFirstWord(int lowcase=0);
147 char *GetMyFirstWord(int lowcase=0);
148 int GetRest(char *theBuf, int Blen, int lowcase=0);
149 char *GetWord(int lowcase=0);
150 
151 // Indicate wether there is an active program attached to the stream
152 //
153 #ifndef WIN32
154 inline int isAlive() {return (child ? kill(child,0) == 0 : 0);}
155 #else
156 inline int isAlive() {return (child ? 1 : 0);}
157 #endif
158 
159 // Return last error code encountered.
160 //
161 inline int LastError() {int n = ecode; ecode = 0; return n;}
162 
163 // Return the last input line
164 //
165 char *LastLine() {return recp;}
166 
167 // Suppress echoing the previous line when the next line is fetched.
168 //
169 int noEcho() {llBok = 0; return 0;}
170 
171 // Write a record to a stream, if a length is not given, then the buffer must
172 // be null terminated and this defines the length (the null is not written).
173 //
174 int Put(const char *data, const int dlen);
175 inline int Put(const char *data) {return Put(data, strlen(data));}
176 
177 // Write record fragments to a stream. The list of fragment/length pairs ends
178 // when a null pointer is encountered.
179 //
180 int Put(const char *data[], const int dlen[]);
181 
182 // Insert a line into the stream buffer. This replaces anything that was there.
183 //
184 int PutLine(const char *data, int dlen=0);
185 
186 // Set the Env (returning the old Env). This is useful for suppressing
187 // substitutions for a while.
188 //
190  {XrdOucEnv *oldEnv = myEnv; myEnv = newEnv; return oldEnv;}
191 
192 // Set error routing
193 //
194 void SetEroute(XrdSysError *eroute) {Eroute = eroute;}
195 
196 // A 0 indicates that tabs in the stream should be converted to spaces.
197 // A 1 inducates that tabs should be left alone (the default).
198 //
199 void Tabs(int x=1) {notabs = !x;}
200 
201 // Wait for inbound data to arrive. The argument is the max number of millisec
202 // to wait (-1 means wait forever). Returns 0 if data is present. Otherwise,
203 // -1 indicates that the connection timed out, a positive value indicates an
204 // error and the value is the errno describing the error.
205 //
206 int Wait4Data(int msMax=-1);
207 
208 /******************************************************************************/
209 
210 private:
211  char *add2llB(char *tok, int reset=0);
212  bool docont();
213  bool docont( const char *path, XrdOucTList *tlP);
214  bool docontD(const char *path, XrdOucTList *tlP);
215  bool docontF(const char *path, bool noentok=false);
216  char *doelse();
217  char *doif();
218  bool Echo(int ec, const char *t1, const char *t2=0, const char *t3=0);
219  int isSet(char *var);
220  char *vSubs(char *Var);
221  int xMsg(const char *txt1, const char *txt2=0, const char *txt3=0);
222 
223 static const int maxVLen = 512;
224 static const int llBsz = 1024;
225 
226  int FD;
227  int FE;
228  int bsize;
229  int bleft;
230  char *buff;
231  char *bnext;
232  char *recp;
233  char *token;
234  int flags;
235  pid_t child;
236  int ecode;
237  int notabs;
238  int xcont;
239  int xline;
240  char *myInst;
241  StreamInfo *myInfo; // ABI compatible change!
242  char *myRsv1;
243  char *myRsv2;
246  char *varVal;
247  const char *llPrefix;
248  char *llBuff;
249  char *llBcur;
250  int llBleft;
251  char Verbose;
252  char sawif;
253  char skpel;
254  char llBok;
255 };
256 #endif
XrdOucStream::noEcho
int noEcho()
Definition: XrdOucStream.hh:169
XrdOucStream::Put
int Put(const char *data)
Definition: XrdOucStream.hh:175
XrdOucStream::child
pid_t child
Definition: XrdOucStream.hh:235
XrdOucStream::FD
int FD
Definition: XrdOucStream.hh:226
XrdOucStream::FENum
int FENum()
Definition: XrdOucStream.hh:109
XrdOucStream::Verbose
char Verbose
Definition: XrdOucStream.hh:251
XrdOucStream::add2llB
char * add2llB(char *tok, int reset=0)
fsync
#define fsync(a)
Definition: XrdPosix.hh:59
XrdOucStream::recp
char * recp
Definition: XrdOucStream.hh:232
XrdOucStream::llBcur
char * llBcur
Definition: XrdOucStream.hh:249
XrdOucStream::xMsg
int xMsg(const char *txt1, const char *txt2=0, const char *txt3=0)
XrdOucStream::Exec
int Exec(const char *, int inrd=0, int efd=0)
XrdOucStream::PutLine
int PutLine(const char *data, int dlen=0)
XrdOucStream::bleft
int bleft
Definition: XrdOucStream.hh:229
XrdOucStream::FE
int FE
Definition: XrdOucStream.hh:227
XrdOucStream::bsize
int bsize
Definition: XrdOucStream.hh:228
XrdOucStream::buff
char * buff
Definition: XrdOucStream.hh:230
XrdOucStream::GetRest
int GetRest(char *theBuf, int Blen, int lowcase=0)
XrdOucStream::docontF
bool docontF(const char *path, bool noentok=false)
XrdOucStream::doelse
char * doelse()
XrdOucStream
Definition: XrdOucStream.hh:45
XrdOucStream::myRsv2
char * myRsv2
Definition: XrdOucStream.hh:243
XrdOucStream::Wait4Data
int Wait4Data(int msMax=-1)
XrdOucStream::myEnv
XrdOucEnv * myEnv
Definition: XrdOucStream.hh:245
XrdOucStream::myRsv1
char * myRsv1
Definition: XrdOucStream.hh:242
XrdOucStream::Drain
int Drain()
XrdOucStream::SetEnv
XrdOucEnv * SetEnv(XrdOucEnv *newEnv)
Definition: XrdOucStream.hh:189
XrdOucStream::Put
int Put(const char *data, const int dlen)
XrdOucStream::AttachIO
int AttachIO(int infd, int outfd, int bsz=2047)
XrdOucStream::xline
int xline
Definition: XrdOucStream.hh:239
XrdOucStream::GetToken
char * GetToken(int lowcase=0)
XrdOucStream::maxVLen
static const int maxVLen
Definition: XrdOucStream.hh:223
XrdOucStream::myInst
char * myInst
Definition: XrdOucStream.hh:240
XrdOucStream::RetToken
void RetToken()
XrdOucStream::varVal
char * varVal
Definition: XrdOucStream.hh:246
XrdOucStream::Tabs
void Tabs(int x=1)
Definition: XrdOucStream.hh:199
XrdOucStream::vSubs
char * vSubs(char *Var)
XrdOucStream::GetMyFirstWord
char * GetMyFirstWord(int lowcase=0)
XrdOucEnv
Definition: XrdOucEnv.hh:41
XrdOucStream::notabs
int notabs
Definition: XrdOucStream.hh:237
XrdOucStream::isSet
int isSet(char *var)
XrdOucStream::ecode
int ecode
Definition: XrdOucStream.hh:236
XrdOucStream::llBsz
static const int llBsz
Definition: XrdOucStream.hh:224
XrdOucStream::Eroute
XrdSysError * Eroute
Definition: XrdOucStream.hh:244
XrdOucStream::Echo
void Echo()
XrdOucStream::Close
void Close(int hold=0)
XrdOucStream::sawif
char sawif
Definition: XrdOucStream.hh:252
XrdOucStream::flags
int flags
Definition: XrdOucStream.hh:234
XrdOucStream::GetLine
char * GetLine()
XrdOucStream::Detach
int Detach()
Definition: XrdOucStream.hh:75
XrdOucStream::GetFirstWord
char * GetFirstWord(int lowcase=0)
XrdOucStream::~XrdOucStream
~XrdOucStream()
Definition: XrdOucStream.hh:56
XrdOucStream::llBok
char llBok
Definition: XrdOucStream.hh:254
XrdOucStream::GetWord
char * GetWord(int lowcase=0)
XrdOucStream::FDNum
int FDNum()
Definition: XrdOucStream.hh:108
XrdOucStream::doif
char * doif()
XrdOucStream::llBuff
char * llBuff
Definition: XrdOucStream.hh:248
XrdOucStream::llPrefix
const char * llPrefix
Definition: XrdOucStream.hh:247
XrdOucStream::Flush
void Flush()
Definition: XrdOucStream.hh:113
XrdOucStream::token
char * token
Definition: XrdOucStream.hh:233
XrdOucStream::bnext
char * bnext
Definition: XrdOucStream.hh:231
XrdOucStream::LastError
int LastError()
Definition: XrdOucStream.hh:161
XrdOucStream::isAlive
int isAlive()
Definition: XrdOucStream.hh:154
XrdOucStream::Attach
int Attach(int FileDescriptor, int bsz=2047)
XrdOucStream::llBleft
int llBleft
Definition: XrdOucStream.hh:250
XrdSysError
Definition: XrdSysError.hh:89
XrdOucStream::SetEroute
void SetEroute(XrdSysError *eroute)
Definition: XrdOucStream.hh:194
XrdOucStream::docontD
bool docontD(const char *path, XrdOucTList *tlP)
XrdOucStream::myInfo
StreamInfo * myInfo
Definition: XrdOucStream.hh:241
XrdOucStream::xcont
int xcont
Definition: XrdOucStream.hh:238
XrdOucStream::docont
bool docont()
XrdOucStream::XrdOucStream
XrdOucStream(XrdSysError *erobj=0, const char *ifname=0, XrdOucEnv *anEnv=0, const char *Pfx=0)
XrdOucTList
Definition: XrdOucTList.hh:41
XrdOucStream::LastLine
char * LastLine()
Definition: XrdOucStream.hh:165
XrdOucStream::skpel
char skpel
Definition: XrdOucStream.hh:253
XrdSysError.hh