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 class XrdOucEnv;
42 
44 {
45 public:
46 
47 // When creating a stream object, you may pass an optional error routing object.
48 // If you do so, error messages will be writen via the error object. Otherwise,
49 // errors will be returned quietly.
50 //
51  XrdOucStream(XrdSysError *erobj=0, const char *ifname=0,
52  XrdOucEnv *anEnv=0, const char *Pfx=0);
53 
54  ~XrdOucStream() {Close(); if (myInst) free(myInst);
55  if (varVal) delete [] varVal;
56  if (llBuff) free(llBuff);
57  }
58 
59 // Attach a file descriptor to an existing stream. Any curently associated
60 // stream is closed and detached. An optional buffer size can be specified.
61 // Zero is returned upon success, otherwise a -1 (use LastError to get rc).
62 //
63 int Attach(int FileDescriptor, int bsz=2047);
64 int AttachIO(int infd, int outfd, int bsz=2047);
65 
66 // Close the current stream and release the associated buffer.
67 //
68 void Close(int hold=0);
69 
70 // Detach a file descriptor from a stream. This should be called prior to
71 // close/delete when you are managing your own descriptors. Return the FD num.
72 //
73 int Detach() {int oldFD = FD; FD = FE = -1; return oldFD;}
74 
75 // Wait for an Exec() to finish and return the ending status. Use this
76 // function only when you need to find out the ending status of the command.
77 //
78 int Drain();
79 
80 // Display last valid line if variable substitution enabled. Fully formed
81 // input lines are displayed if 'set -v' was encountered (only when using
82 // the GetxxxWord() methods),
83 //
84 void Echo();
85 
86 // Execute a command on a stream. Returns 0 upon success or -1 otherwise.
87 // Use LastError() to get the actual error code. Subsequent Get() calls
88 // will return the standard output of the executed command. If inrd=1 then
89 // standardin is redirected so that subqseuent Put() calls write to the
90 // process via standard in. When inrd=-1 then the current attached FD's are
91 // used to redirect STDIN and STDOUT of the child process. Standard error
92 // is handled as determined by the efd argument:
93 // efd < 0 -> How to handle the current stderr file decriptor:
94 // -1 The current stderr file decriptor is unchanged.
95 // Output of only stdout is to be captured by this stream.
96 // -2 Output of only stderr is to be captured by this stream.
97 // -3 Output of stdout and stderr is to be captured by this stream.
98 // efd = 0 -> The stderr file descriptor is set to the original logging FD
99 // efd > 0 -> The stderr file descriptor is set to the value of efd.
100 //
101 int Exec(const char *, int inrd=0, int efd=0);
102 int Exec( char **, int inrd=0, int efd=0);
103 
104 // Get the file descriptor number associated with a stream
105 //
106 int FDNum() {return FD;}
107 int FENum() {return FE;}
108 
109 // Flush any remaining output queued on an output stream.
110 //
111 void Flush() {fsync(FD); if (FE != FD) fsync(FE);}
112 
113 // Get the next record from a stream. Return null upon eof or error. Use
114 // LastError() to determine which condition occurred (an error code of 0
115 // indicates that end of file has been reached). Upon success, a pointer
116 // to the next record is returned. The record is terminated by a null char.
117 //
118 char *GetLine();
119 
120 // Get the next blank-delimited token in the record returned by Getline(). A
121 // null pointer is returned if no more tokens remain. Each token is terminated
122 // a null byte. Note that the record buffer is modified during processing. The
123 // first form returns simply a token pointer. The second form returns a token
124 // pointer and a pointer to the remainder of the line with no leading blanks.
125 // The lowcase argument, if 1, converts all letters to lower case in the token.
126 // RetToken() simply backups the token scanner one token. None of these
127 // methods perform variable substitution (see GetxxxWord() below).
128 //
129 char *GetToken(int lowcase=0);
130 char *GetToken(char **rest, int lowcase=0);
131 void RetToken();
132 
133 // Get the next word, ignoring any blank lines and comment lines (lines whose
134 // first non-blank is a pound sign). Words are returned until logical end of
135 // line is encountered at which time, a null is returned. A subsequent call
136 // will return the next word on the next logical line. A physical line may be
137 // continued by placing a back slash at it's end (i.e., last non-blank char).
138 // GetFirstWord() always makes sure that the first word of a logical line is
139 // returned (useful for start afresh after a mid-sentence error). GetRest()
140 // places the remining tokens in the supplied buffer; returning 0 if the
141 // buffer was too small. All of these methods perform variable substitution
142 // should an XrdOucEnv object be passed to the constructor.
143 //
144 char *GetFirstWord(int lowcase=0);
145 char *GetMyFirstWord(int lowcase=0);
146 int GetRest(char *theBuf, int Blen, int lowcase=0);
147 char *GetWord(int lowcase=0);
148 
149 // Indicate wether there is an active program attached to the stream
150 //
151 #ifndef WIN32
152 inline int isAlive() {return (child ? kill(child,0) == 0 : 0);}
153 #else
154 inline int isAlive() {return (child ? 1 : 0);}
155 #endif
156 
157 // Return last error code encountered.
158 //
159 inline int LastError() {int n = ecode; ecode = 0; return n;}
160 
161 // Return the last input line
162 //
163 char *LastLine() {return recp;}
164 
165 // Suppress echoing the previous line when the next line is fetched.
166 //
167 int noEcho() {llBok = 0; return 0;}
168 
169 // Write a record to a stream, if a length is not given, then the buffer must
170 // be null terminated and this defines the length (the null is not written).
171 //
172 int Put(const char *data, const int dlen);
173 inline int Put(const char *data) {return Put(data, strlen(data));}
174 
175 // Write record fragments to a stream. The list of fragment/length pairs ends
176 // when a null pointer is encountered.
177 //
178 int Put(const char *data[], const int dlen[]);
179 
180 // Insert a line into the stream buffer. This replaces anything that was there.
181 //
182 int PutLine(const char *data, int dlen=0);
183 
184 // Set the Env (returning the old Env). This is useful for suppressing
185 // substitutions for a while.
186 //
188  {XrdOucEnv *oldEnv = myEnv; myEnv = newEnv; return oldEnv;}
189 
190 // Set error routing
191 //
192 void SetEroute(XrdSysError *eroute) {Eroute = eroute;}
193 
194 // A 0 indicates that tabs in the stream should be converted to spaces.
195 // A 1 inducates that tabs should be left alone (the default).
196 //
197 void Tabs(int x=1) {notabs = !x;}
198 
199 // Wait for inbound data to arrive. The argument is the max number of millisec
200 // to wait (-1 means wait forever). Returns 0 if data is present. Otherwise,
201 // -1 indicates that the connection timed out, a positive value indicates an
202 // error and the value is the errno describing the error.
203 //
204 int Wait4Data(int msMax=-1);
205 
206 /******************************************************************************/
207 
208 private:
209  char *add2llB(char *tok, int reset=0);
210  char *doelse();
211  char *doif();
212  int isSet(char *var);
213  char *vSubs(char *Var);
214  int xMsg(const char *txt1, const char *txt2=0, const char *txt3=0);
215 
216 static const int maxVLen = 512;
217 static const int llBsz = 1024;
218 
219  int FD;
220  int FE;
221  int bsize;
222  int bleft;
223  char *buff;
224  char *bnext;
225  char *recp;
226  char *token;
227  int flags;
228  pid_t child;
229  int ecode;
230  int notabs;
231  int xcont;
232  int xline;
233  char *myInst;
234  char *myHost;
235  char *myName;
236  char *myExec;
239  char *varVal;
240  const char *llPrefix;
241  char *llBuff;
242  char *llBcur;
243  int llBleft;
244  char Verbose;
245  char sawif;
246  char skpel;
247  char llBok;
248 };
249 #endif
int isSet(char *var)
char * GetToken(int lowcase=0)
int FE
Definition: XrdOucStream.hh:220
int llBleft
Definition: XrdOucStream.hh:243
char * varVal
Definition: XrdOucStream.hh:239
int Exec(const char *, int inrd=0, int efd=0)
int GetRest(char *theBuf, int Blen, int lowcase=0)
static const int llBsz
Definition: XrdOucStream.hh:217
int Detach()
Definition: XrdOucStream.hh:73
char * doelse()
XrdSysError * Eroute
Definition: XrdOucStream.hh:237
XrdOucEnv * myEnv
Definition: XrdOucStream.hh:238
char * GetWord(int lowcase=0)
~XrdOucStream()
Definition: XrdOucStream.hh:54
char * GetLine()
char skpel
Definition: XrdOucStream.hh:246
int noEcho()
Definition: XrdOucStream.hh:167
char * GetFirstWord(int lowcase=0)
Definition: XrdOucStream.hh:43
char * myName
Definition: XrdOucStream.hh:235
int flags
Definition: XrdOucStream.hh:227
char Verbose
Definition: XrdOucStream.hh:244
char * token
Definition: XrdOucStream.hh:226
char * doif()
char * bnext
Definition: XrdOucStream.hh:224
int FD
Definition: XrdOucStream.hh:219
int Attach(int FileDescriptor, int bsz=2047)
char * myHost
Definition: XrdOucStream.hh:234
char * add2llB(char *tok, int reset=0)
int LastError()
Definition: XrdOucStream.hh:159
int xcont
Definition: XrdOucStream.hh:231
Definition: XrdSysError.hh:89
XrdOucStream(XrdSysError *erobj=0, const char *ifname=0, XrdOucEnv *anEnv=0, const char *Pfx=0)
int ecode
Definition: XrdOucStream.hh:229
const char * llPrefix
Definition: XrdOucStream.hh:240
void Close(int hold=0)
char * buff
Definition: XrdOucStream.hh:223
XrdOucEnv * SetEnv(XrdOucEnv *newEnv)
Definition: XrdOucStream.hh:187
int Put(const char *data, const int dlen)
Definition: XrdOucEnv.hh:41
int FDNum()
Definition: XrdOucStream.hh:106
int AttachIO(int infd, int outfd, int bsz=2047)
pid_t child
Definition: XrdOucStream.hh:228
int xMsg(const char *txt1, const char *txt2=0, const char *txt3=0)
char * vSubs(char *Var)
int PutLine(const char *data, int dlen=0)
int isAlive()
Definition: XrdOucStream.hh:152
void RetToken()
void Tabs(int x=1)
Definition: XrdOucStream.hh:197
void SetEroute(XrdSysError *eroute)
Definition: XrdOucStream.hh:192
char * GetMyFirstWord(int lowcase=0)
char * recp
Definition: XrdOucStream.hh:225
char * llBcur
Definition: XrdOucStream.hh:242
int bleft
Definition: XrdOucStream.hh:222
char * LastLine()
Definition: XrdOucStream.hh:163
int bsize
Definition: XrdOucStream.hh:221
char sawif
Definition: XrdOucStream.hh:245
int Wait4Data(int msMax=-1)
static const int maxVLen
Definition: XrdOucStream.hh:216
char llBok
Definition: XrdOucStream.hh:247
int Put(const char *data)
Definition: XrdOucStream.hh:173
char * myExec
Definition: XrdOucStream.hh:236
int FENum()
Definition: XrdOucStream.hh:107
int xline
Definition: XrdOucStream.hh:232
char * myInst
Definition: XrdOucStream.hh:233
int notabs
Definition: XrdOucStream.hh:230
void Flush()
Definition: XrdOucStream.hh:111
char * llBuff
Definition: XrdOucStream.hh:241