Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JProcess.hh
Go to the documentation of this file.
1#ifndef __JSYSTEM__JPROCESS__
2#define __JSYSTEM__JPROCESS__
3
4#include <unistd.h>
5
6#include "JLang/JException.hh"
8
9
10/**
11 * \author mdejong
12 */
13
14namespace JSYSTEM {}
15namespace JPP { using namespace JSYSTEM; }
16
17namespace JSYSTEM {
18
24
25
26 /**
27 * Streaming of input and output from Linux command.
28 */
29 class JProcess
30 {
31 public:
32 /**
33 * Default constructor.
34 * This constructor starts an interactive shell.
35 */
37 out (-1),
38 err (-1),
39 in (-1),
40 gOut(NULL),
41 gErr(NULL),
42 gIn (NULL)
43 {
44 execute("/bin/tcsh", "tcsh", "-i", "-f");
45 }
46
47
48 /**
49 * Constructor.
50 *
51 * \param command Linux command
52 */
53 JProcess(const std::string& command) :
54 out (-1),
55 err (-1),
56 in (-1),
57 gOut(NULL),
58 gErr(NULL),
59 gIn (NULL)
60 {
61 execute("/bin/tcsh", "tcsh", "-f", "-c", command.c_str());
62 }
63
64
65 /**
66 * Destructor.
67 */
69 {
70 close(err);
71 close(out);
72 close(in);
73
74 if (gOut != NULL) { delete gOut; }
75 if (gErr != NULL) { delete gErr; }
76 if (gIn != NULL) { delete gIn; }
77 }
78
79
80 /**
81 * Get pointer to input stream buffer connected to the normal output stream.
82 *
83 * \param size size of internal buffers
84 * \return pointer input stream buffer
85 */
86 JFileInputStreamBuffer* getInputStreamBuffer(const std::size_t size = 65536)
87 {
88 if (gOut == NULL) {
89 gOut = new JFileInputStreamBuffer(out, size);
90 }
91
92 return gOut;
93 }
94
95
96 /**
97 * Get pointer to input stream buffer connected to the error output stream.
98 *
99 * \param size size of internal buffers
100 * \return pointer to input stream buffer
101 */
102 JFileInputStreamBuffer* getErrorStreamBuffer(const std::size_t size = 65536)
103 {
104 if (gErr == NULL) {
105 gErr = new JFileInputStreamBuffer(err, size);
106 }
107
108 return gErr;
109 }
110
111
112 /**
113 * Get pointer to output stream buffer connected to the normal input stream.
114 *
115 * \param size size of internal buffers
116 * \return pointer to output stream buffer
117 */
118 JFileOutputStreamBuffer* getOutputStreamBuffer(const std::size_t size = 65536)
119 {
120 if (gIn == NULL) {
121 gIn = new JFileOutputStreamBuffer(in, size);
122 }
123
124 return gIn;
125 }
126
127 protected:
128 /**
129 * Execute command.
130 *
131 * \param path path
132 * \param arg0 comma separted argument list
133 * \param arg1 comma separted argument list
134 * \param arg2 comma separted argument list
135 * \param arg3 comma separted argument list
136 * \param arg4 comma separted argument list
137 * \param arg5 comma separted argument list
138 * \param arg6 comma separted argument list
139 * \param arg7 comma separted argument list
140 * \param arg8 comma separted argument list
141 * \param arg9 comma separted argument list
142 */
143 void execute(const char* path,
144 const char* arg0,
145 const char* arg1 = (char*) NULL,
146 const char* arg2 = (char*) NULL,
147 const char* arg3 = (char*) NULL,
148 const char* arg4 = (char*) NULL,
149 const char* arg5 = (char*) NULL,
150 const char* arg6 = (char*) NULL,
151 const char* arg7 = (char*) NULL,
152 const char* arg8 = (char*) NULL,
153 const char* arg9 = (char*) NULL)
154 {
155 int pipe_stdin [2];
156 int pipe_stdout[2];
157 int pipe_stderr[2];
158
159 if (pipe(pipe_stdin)) { THROW(JPipeOpenException, "Error at pipe of standard input."); }
160 if (pipe(pipe_stdout)) { THROW(JPipeOpenException, "Error at pipe of standard output."); }
161 if (pipe(pipe_stderr)) { THROW(JPipeOpenException, "Error at pipe of standard error."); }
162
163 const pid_t pid = fork();
164
165 if (pid < 0)
166
167 // error
168
169 THROW(JForkException, "JProcess::fork() failed");
170
171 else if (pid == 0) {
172
173 // child
174
175 close(pipe_stdin [1]);
176 if (dup2 (pipe_stdin [0], STDIN_FILENO) == -1) { JSystemException("dup2-stdin"); }
177
178 close(pipe_stdout[0]);
179 if (dup2 (pipe_stdout[1], STDOUT_FILENO) == -1) { JSystemException("dup2-stdout"); }
180
181 close(pipe_stdout[0]);
182 if (dup2 (pipe_stdout[1], STDERR_FILENO) == -1) { JSystemException("dup2-stderr"); }
183
184 execl(path, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, (char*) NULL);
185
186 THROW(JSystemException, path);
187
188 } else {
189
190 // parent
191
192 in = pipe_stdin [1];
193 out = pipe_stdout[0];
194 err = pipe_stderr[0];
195
196 close(pipe_stdin [0]);
197 close(pipe_stdout[1]);
198 close(pipe_stderr[1]);
199 }
200 }
201
202 int out;
203 int err;
204 int in;
205
206 private:
210
215 };
216}
217
218#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Input file stream buffer.
Output file stream buffer.
Exception for creation of fork.
Exception for opening of pipe.
Exception for system call.
Streaming of input and output from Linux command.
Definition JProcess.hh:30
JFileInputStreamBuffer * getInputStreamBuffer(const std::size_t size=65536)
Get pointer to input stream buffer connected to the normal output stream.
Definition JProcess.hh:86
JFileOutputStreamBuffer * gIn
Definition JProcess.hh:209
JFileOutputStreamBuffer * getOutputStreamBuffer(const std::size_t size=65536)
Get pointer to output stream buffer connected to the normal input stream.
Definition JProcess.hh:118
~JProcess()
Destructor.
Definition JProcess.hh:68
JProcess(const std::string &command)
Constructor.
Definition JProcess.hh:53
JProcess & operator=(const JProcess &)
JFileInputStreamBuffer * getErrorStreamBuffer(const std::size_t size=65536)
Get pointer to input stream buffer connected to the error output stream.
Definition JProcess.hh:102
JProcess(const JProcess &)
void execute(const char *path, const char *arg0, const char *arg1=(char *) NULL, const char *arg2=(char *) NULL, const char *arg3=(char *) NULL, const char *arg4=(char *) NULL, const char *arg5=(char *) NULL, const char *arg6=(char *) NULL, const char *arg7=(char *) NULL, const char *arg8=(char *) NULL, const char *arg9=(char *) NULL)
Execute command.
Definition JProcess.hh:143
JProcess & operator=(JProcess &&)
JProcess(JProcess &&)
JFileInputStreamBuffer * gErr
Definition JProcess.hh:208
JFileInputStreamBuffer * gOut
Definition JProcess.hh:207
JProcess()
Default constructor.
Definition JProcess.hh:36
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.