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