pass pipestream flags as bitset
[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
ad4490f1 32#include <cstring>
e93545dd
GE
33#include <string>
34#include <streambuf>
4a56f9ac
GE
35#include <istream>
36#include <ostream>
c2c29997 37#include <vector>
e93545dd 38
8621037f
PG
39#include <stringfunc.hxx>
40
f8f119bf
GE
41struct ExecResult
42{
43 /** if the program exited normally and returned a return code */
44 bool normal_exit;
45
46 /** the real return code of the program, only set when normal_exit true */
47 char return_code;
48
49 /** if the program was terminated by a signal */
50 bool terminated_by_signal;
51
52 /** number of the signal that terminated the program, only valid when terminated_by_signal true */
53 int signal;
54
55 /** errormessage if we have one */
56 std::string error_message;
8621037f
PG
57
58 inline std::string format (void) const
59 {
60 return std::string ("(")
61 + "(normal_exit " + (this->normal_exit ? "T" : "F") + ") "
62 "(return_code '" + I2n::to_string ((int)this->return_code) + "') "
63 "(signal " + (this->terminated_by_signal
64 ? strsignal (this->signal)
65 : "<nil>") + "))"
66 ;
67 };
f8f119bf
GE
68};
69typedef struct ExecResult ExecResult;
70
825c519f
PG
71namespace capture_flag {
72
73 static const int none = 0;
74 static const int collect_out = 1 << 0;
75 static const int collect_err = 1 << 1;
76 static const int search_path = 1 << 2;
77 static const int env_passthru = 1 << 3;
78
79 static const int dflt = collect_out;
80 static const int collect_any = collect_out | collect_err;
81
82} /* [namespace capture_flag] */
83
f8f119bf 84std::string capture_exec(const std::string& command, ExecResult &rescode);
cc917897 85std::string capture_exec(const char *const *command, ExecResult &rescode,
825c519f 86 const int flags=capture_flag::dflt);
cc917897 87std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode,
825c519f 88 const int flags=capture_flag::dflt);
f8f119bf 89
9e322fb7
PG
90inline std::string capture_exec (const std::string &command)
91{
92 ExecResult r;
93 return capture_exec(command,r);
94}
95
96inline std::string capture_exec(const char *const *command)
f8f119bf
GE
97{
98 ExecResult r;
99 return capture_exec(command,r);
100}
101
c2c29997
PG
102inline std::string capture_exec(const std::vector<std::string>& command)
103{
104 ExecResult r;
105 return capture_exec(command,r);
106}
107
bf4c487c
CH
108/** @brief runs command and provides buffered input for it through pipe
109 *
110 * opens pipe to command using popen; exit status available after destruction
111 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
112 *
113 * ATTENTION: A lot of mysterious STL bugs occured
114 * with a "real" buffer (buffer larger than 1 byte and up to 100 bytes)
115 * -> Keep it slow and working!
116 */
e93545dd
GE
117class inpipebuf : public std::streambuf
118{
20a79471
TJ
119protected:
120 char buffer;
9e322fb7 121
20a79471 122 FILE *pipe;
17b459b3 123 pid_t pid;
20a79471
TJ
124
125 // "callback" variables for destructor to store exit status
126 bool *status_set;
127 int *exit_status;
128
129public:
825c519f
PG
130 inpipebuf(const std::string& command, const int flags);
131 inpipebuf(const char *const *command, const int flags);
132 inpipebuf(const std::vector<std::string> &command, const int flags);
20a79471 133
7e606af5 134 ~inpipebuf();
20a79471 135
7e606af5 136 void store_exit_status(bool *_status_set, int *_exit_status);
20a79471
TJ
137
138protected:
7e606af5 139 virtual int_type underflow();
9e322fb7
PG
140
141private:
17b459b3 142 std::pair <pid_t, FILE *>
825c519f 143 init_without_shell (const char *const *argv, const int flags) const;
20a79471 144};
e93545dd 145
bf4c487c 146/** @brief stream around inpipebuf -- see comment there */
e93545dd
GE
147class inpipestream : public std::istream
148{
20a79471
TJ
149protected:
150 inpipebuf buf;
151
152public:
cc917897 153 inpipestream(const std::string& command,
825c519f
PG
154 const int flags=capture_flag::dflt)
155 : std::istream(&buf), buf(command, flags)
20a79471 156 {}
cc917897
PG
157
158 inpipestream(const char *const command[],
825c519f
PG
159 const int flags=capture_flag::dflt)
160 : std::istream(&buf), buf(command, flags)
9e322fb7 161 {}
20a79471 162
cc917897 163 inpipestream(const std::vector<std::string> &command,
825c519f
PG
164 const int flags=capture_flag::dflt)
165 : std::istream(&buf), buf(command, flags)
c2c29997
PG
166 {}
167
20a79471
TJ
168 void store_exit_status(bool *_status_set, int *_exit_status)
169 { buf.store_exit_status(_status_set, _exit_status); }
e93545dd
GE
170};
171
bf4c487c
CH
172/** @brief runs command and provides buffered ouptput from it through pipe
173 *
174 * opens pipe to command using popen; exit status available after destruction
175 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
176 */
e93545dd
GE
177class outpipebuf : public std::streambuf
178{
20a79471
TJ
179protected:
180 FILE *pipe;
181
182 // "callback" variables for destructor to store exit status
183 bool *status_set;
184 int *exit_status;
185
186public:
7e606af5
GE
187 outpipebuf(const std::string& command);
188
189 ~outpipebuf();
20a79471 190
bf4c487c 191 /** note: exit status only available after destruction */
7e606af5 192 void store_exit_status(bool *_status_set, int *_exit_status);
20a79471
TJ
193
194protected:
7e606af5
GE
195 virtual int_type overflow(int_type c);
196
197 virtual std::streamsize xsputn(const char* s, std::streamsize num);
e93545dd
GE
198};
199
bf4c487c
CH
200
201/** @brief stream around outpipebuf -- see comment there */
e93545dd
GE
202class outpipestream : public std::ostream
203{
20a79471
TJ
204protected:
205 outpipebuf buf;
206public:
207 outpipestream(const std::string& command)
d8ffcdb0 208 : std::ostream(&buf), buf(command)
20a79471
TJ
209 {}
210
211 void store_exit_status(bool *_status_set, int *_exit_status)
212 { buf.store_exit_status(_status_set, _exit_status); }
e93545dd
GE
213};
214
215#endif