xrootd
XrdOucProg.hh
Go to the documentation of this file.
1 #ifndef __OOUC_PROG__
2 #define __OOUC_PROG__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c P r o g . 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 
34 class XrdSysError;
35 class XrdOucStream;
36 
38 {
39 public:
40 
41 // When creating an Prog object, you may pass an optional error routing object.
42 // If you do so, error messages and all command output will be writen via the
43 // error object. Otherwise, errors will be returned quietly.
44 //
46  : eDest(errobj), myStream(0), myProc(0), ArgBuff(0),
47  numArgs(0), theEFD(-1) {Arg[0] = 0;}
48 
49  ~XrdOucProg();
50 
51 // Feed() send a data to the program started by Start(). Several variations
52 // exist to accomodate various needs. Note that should the program not be
53 // running when Feed() is called, it is restarted.
54 //
55 int Feed(const char *data[], const int dlen[]);
56 
57 int Feed(const char *data, int dlen)
58  {const char *myData[2] = {data, 0};
59  const int myDlen[2] = {dlen, 0};
60  return Feed(myData, myDlen);
61  }
62 
63 int Feed(const char *data) {return Feed(data, (int)strlen(data));}
64 
65 // getStream() returns the stream created by Start(). Use the object to get
66 // lines written by the started program.
67 //
69 
70 // Run executes the command that was passed via Setup(). You may pass
71 // up to four additional arguments that will be added to the end of any
72 // existing arguments. The ending status code of the program is returned.
73 //
74 int Run(XrdOucStream *Sp, const char *arg1=0, const char *arg2=0,
75  const char *arg3=0, const char *arg4=0);
76 
77 int Run(const char *arg1=0, const char *arg2=0,
78  const char *arg3=0, const char *arg4=0);
79 
80 int Run(char *outBuff, int outBsz,
81  const char *arg1=0, const char *arg2=0,
82  const char *arg3=0, const char *arg4=0);
83 
84 // RunDone should be called to drain the output stream and get the ending
85 // status of the running process.
86 //
87 int RunDone(XrdOucStream &cmd);
88 
89 // Start executes the command that was passed via Setup(). The started
90 // program is expected to linger so that you can send directives to it
91 // via its standard in. Use Feed() to do this. If the output of the command
92 // is wanted, use getStream() to get the stream object and use it to read
93 // lines the program sends to standard out.
94 //
95 int Start(void);
96 
97 // Setup takes a command string and sets up a parameter list. If a Proc pointer
98 // is passed, then the command executes via that function. Otherwise, it checks
99 // that the program (first token) is executable.
100 // Zero is returned upon success, otherwise a -errno is returned,
101 //
102 int Setup(const char *prog,
103  XrdSysError *errP=0,
104  int (*Proc)(XrdOucStream *, char **, int)=0
105  );
106 
107 /******************************************************************************/
108 
109 private:
110  int Restart();
113  int (*myProc)(XrdOucStream *, char **, int);
114  char *ArgBuff;
115  char *Arg[64];
116  int numArgs;
117  int lenArgs;
118  int theEFD;
119 };
120 #endif