Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
e93545dd GE |
20 | /*************************************************************************** |
21 | inpipestream.hxx - C++ streambuffer wrapper | |
22 | ------------------- | |
23 | begin : Thu Dec 27 2001 | |
24 | copyright : (C) 2001 by Intra2net AG | |
e93545dd GE |
25 | ***************************************************************************/ |
26 | ||
27 | #ifndef _PIPESTREAM | |
28 | #define _PIPESTREAM | |
29 | ||
be3dbe32 TJ |
30 | #include <stdio.h> |
31 | ||
e93545dd GE |
32 | #include <string> |
33 | #include <streambuf> | |
4a56f9ac GE |
34 | #include <istream> |
35 | #include <ostream> | |
c2c29997 | 36 | #include <vector> |
e93545dd | 37 | |
8621037f PG |
38 | #include <stringfunc.hxx> |
39 | ||
f8f119bf GE |
40 | struct ExecResult |
41 | { | |
42 | /** if the program exited normally and returned a return code */ | |
43 | bool normal_exit; | |
44 | ||
45 | /** the real return code of the program, only set when normal_exit true */ | |
46 | char return_code; | |
47 | ||
48 | /** if the program was terminated by a signal */ | |
49 | bool terminated_by_signal; | |
50 | ||
51 | /** number of the signal that terminated the program, only valid when terminated_by_signal true */ | |
52 | int signal; | |
53 | ||
54 | /** errormessage if we have one */ | |
55 | std::string error_message; | |
8621037f PG |
56 | |
57 | inline std::string format (void) const | |
58 | { | |
59 | return std::string ("(") | |
60 | + "(normal_exit " + (this->normal_exit ? "T" : "F") + ") " | |
61 | "(return_code '" + I2n::to_string ((int)this->return_code) + "') " | |
62 | "(signal " + (this->terminated_by_signal | |
63 | ? strsignal (this->signal) | |
64 | : "<nil>") + "))" | |
65 | ; | |
66 | }; | |
f8f119bf GE |
67 | }; |
68 | typedef struct ExecResult ExecResult; | |
69 | ||
70 | std::string capture_exec(const std::string& command, ExecResult &rescode); | |
cc917897 PG |
71 | std::string capture_exec(const char *const *command, ExecResult &rescode, |
72 | const bool out=true, const bool err=false); | |
73 | std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode, | |
74 | const bool out=true, const bool err=false); | |
f8f119bf | 75 | |
9e322fb7 PG |
76 | inline std::string capture_exec (const std::string &command) |
77 | { | |
78 | ExecResult r; | |
79 | return capture_exec(command,r); | |
80 | } | |
81 | ||
82 | inline std::string capture_exec(const char *const *command) | |
f8f119bf GE |
83 | { |
84 | ExecResult r; | |
85 | return capture_exec(command,r); | |
86 | } | |
87 | ||
c2c29997 PG |
88 | inline std::string capture_exec(const std::vector<std::string>& command) |
89 | { | |
90 | ExecResult r; | |
91 | return capture_exec(command,r); | |
92 | } | |
93 | ||
bf4c487c CH |
94 | /** @brief runs command and provides buffered input for it through pipe |
95 | * | |
96 | * opens pipe to command using popen; exit status available after destruction | |
97 | * (use WEXITSTATUS to get the "regular" return code (lowest byte)) | |
98 | * | |
99 | * ATTENTION: A lot of mysterious STL bugs occured | |
100 | * with a "real" buffer (buffer larger than 1 byte and up to 100 bytes) | |
101 | * -> Keep it slow and working! | |
102 | */ | |
e93545dd GE |
103 | class inpipebuf : public std::streambuf |
104 | { | |
20a79471 TJ |
105 | protected: |
106 | char buffer; | |
9e322fb7 | 107 | |
20a79471 | 108 | FILE *pipe; |
17b459b3 | 109 | pid_t pid; |
20a79471 TJ |
110 | |
111 | // "callback" variables for destructor to store exit status | |
112 | bool *status_set; | |
113 | int *exit_status; | |
114 | ||
115 | public: | |
cc917897 PG |
116 | inpipebuf(const std::string& command, |
117 | const bool out, const bool err); | |
118 | inpipebuf(const char *const *command, | |
119 | const bool out, const bool err); | |
120 | inpipebuf(const std::vector<std::string> &command, | |
121 | const bool out, const bool err); | |
20a79471 | 122 | |
7e606af5 | 123 | ~inpipebuf(); |
20a79471 | 124 | |
7e606af5 | 125 | void store_exit_status(bool *_status_set, int *_exit_status); |
20a79471 TJ |
126 | |
127 | protected: | |
7e606af5 | 128 | virtual int_type underflow(); |
9e322fb7 PG |
129 | |
130 | private: | |
17b459b3 PG |
131 | std::pair <pid_t, FILE *> |
132 | init_without_shell (const char *const *argv, | |
133 | const bool out, const bool err) const; | |
20a79471 | 134 | }; |
e93545dd | 135 | |
bf4c487c | 136 | /** @brief stream around inpipebuf -- see comment there */ |
e93545dd GE |
137 | class inpipestream : public std::istream |
138 | { | |
20a79471 TJ |
139 | protected: |
140 | inpipebuf buf; | |
141 | ||
142 | public: | |
cc917897 PG |
143 | inpipestream(const std::string& command, |
144 | const bool out=true, const bool err=false) | |
145 | : std::istream(&buf), buf(command, out, err) | |
20a79471 | 146 | {} |
cc917897 PG |
147 | |
148 | inpipestream(const char *const command[], | |
149 | const bool out=true, const bool err=false) | |
150 | : std::istream(&buf), buf(command, out, err) | |
9e322fb7 | 151 | {} |
20a79471 | 152 | |
cc917897 PG |
153 | inpipestream(const std::vector<std::string> &command, |
154 | const bool out=true, const bool err=false) | |
155 | : std::istream(&buf), buf(command, out, err) | |
c2c29997 PG |
156 | {} |
157 | ||
20a79471 TJ |
158 | void store_exit_status(bool *_status_set, int *_exit_status) |
159 | { buf.store_exit_status(_status_set, _exit_status); } | |
e93545dd GE |
160 | }; |
161 | ||
bf4c487c CH |
162 | /** @brief runs command and provides buffered ouptput from it through pipe |
163 | * | |
164 | * opens pipe to command using popen; exit status available after destruction | |
165 | * (use WEXITSTATUS to get the "regular" return code (lowest byte)) | |
166 | */ | |
e93545dd GE |
167 | class outpipebuf : public std::streambuf |
168 | { | |
20a79471 TJ |
169 | protected: |
170 | FILE *pipe; | |
171 | ||
172 | // "callback" variables for destructor to store exit status | |
173 | bool *status_set; | |
174 | int *exit_status; | |
175 | ||
176 | public: | |
7e606af5 GE |
177 | outpipebuf(const std::string& command); |
178 | ||
179 | ~outpipebuf(); | |
20a79471 | 180 | |
bf4c487c | 181 | /** note: exit status only available after destruction */ |
7e606af5 | 182 | void store_exit_status(bool *_status_set, int *_exit_status); |
20a79471 TJ |
183 | |
184 | protected: | |
7e606af5 GE |
185 | virtual int_type overflow(int_type c); |
186 | ||
187 | virtual std::streamsize xsputn(const char* s, std::streamsize num); | |
e93545dd GE |
188 | }; |
189 | ||
bf4c487c CH |
190 | |
191 | /** @brief stream around outpipebuf -- see comment there */ | |
e93545dd GE |
192 | class outpipestream : public std::ostream |
193 | { | |
20a79471 TJ |
194 | protected: |
195 | outpipebuf buf; | |
196 | public: | |
197 | outpipestream(const std::string& command) | |
d8ffcdb0 | 198 | : std::ostream(&buf), buf(command) |
20a79471 TJ |
199 | {} |
200 | ||
201 | void store_exit_status(bool *_status_set, int *_exit_status) | |
202 | { buf.store_exit_status(_status_set, _exit_status); } | |
e93545dd GE |
203 | }; |
204 | ||
205 | #endif |