allow path lookup for pipestream
[libi2ncommon] / src / pipestream.hxx
... / ...
CommitLineData
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*/
20/***************************************************************************
21 inpipestream.hxx - C++ streambuffer wrapper
22 -------------------
23 begin : Thu Dec 27 2001
24 copyright : (C) 2001 by Intra2net AG
25 ***************************************************************************/
26
27#ifndef _PIPESTREAM
28#define _PIPESTREAM
29
30#include <stdio.h>
31
32#include <cstring>
33#include <string>
34#include <streambuf>
35#include <istream>
36#include <ostream>
37#include <vector>
38
39#include <stringfunc.hxx>
40
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;
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 };
68};
69typedef struct ExecResult ExecResult;
70
71std::string capture_exec(const std::string& command, ExecResult &rescode);
72std::string capture_exec(const char *const *command, ExecResult &rescode,
73 const bool out=true, const bool err=false,
74 const bool path=false);
75std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode,
76 const bool out=true, const bool err=false,
77 const bool path=false);
78
79inline std::string capture_exec (const std::string &command)
80{
81 ExecResult r;
82 return capture_exec(command,r);
83}
84
85inline std::string capture_exec(const char *const *command)
86{
87 ExecResult r;
88 return capture_exec(command,r);
89}
90
91inline std::string capture_exec(const std::vector<std::string>& command)
92{
93 ExecResult r;
94 return capture_exec(command,r);
95}
96
97/** @brief runs command and provides buffered input for it through pipe
98 *
99 * opens pipe to command using popen; exit status available after destruction
100 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
101 *
102 * ATTENTION: A lot of mysterious STL bugs occured
103 * with a "real" buffer (buffer larger than 1 byte and up to 100 bytes)
104 * -> Keep it slow and working!
105 */
106class inpipebuf : public std::streambuf
107{
108protected:
109 char buffer;
110
111 FILE *pipe;
112 pid_t pid;
113
114 // "callback" variables for destructor to store exit status
115 bool *status_set;
116 int *exit_status;
117
118public:
119 inpipebuf(const std::string& command,
120 const bool out, const bool err, const bool path);
121 inpipebuf(const char *const *command,
122 const bool out, const bool err, const bool path);
123 inpipebuf(const std::vector<std::string> &command,
124 const bool out, const bool err, const bool path);
125
126 ~inpipebuf();
127
128 void store_exit_status(bool *_status_set, int *_exit_status);
129
130protected:
131 virtual int_type underflow();
132
133private:
134 std::pair <pid_t, FILE *>
135 init_without_shell (const char *const *argv,
136 const bool out, const bool err,
137 const bool path) const;
138};
139
140/** @brief stream around inpipebuf -- see comment there */
141class inpipestream : public std::istream
142{
143protected:
144 inpipebuf buf;
145
146public:
147 inpipestream(const std::string& command,
148 const bool out=true, const bool err=false,
149 const bool path=false)
150 : std::istream(&buf), buf(command, out, err, path)
151 {}
152
153 inpipestream(const char *const command[],
154 const bool out=true, const bool err=false,
155 const bool path=false)
156 : std::istream(&buf), buf(command, out, err, path)
157 {}
158
159 inpipestream(const std::vector<std::string> &command,
160 const bool out=true, const bool err=false,
161 const bool path=false)
162 : std::istream(&buf), buf(command, out, err, path)
163 {}
164
165 void store_exit_status(bool *_status_set, int *_exit_status)
166 { buf.store_exit_status(_status_set, _exit_status); }
167};
168
169/** @brief runs command and provides buffered ouptput from it through pipe
170 *
171 * opens pipe to command using popen; exit status available after destruction
172 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
173 */
174class outpipebuf : public std::streambuf
175{
176protected:
177 FILE *pipe;
178
179 // "callback" variables for destructor to store exit status
180 bool *status_set;
181 int *exit_status;
182
183public:
184 outpipebuf(const std::string& command);
185
186 ~outpipebuf();
187
188 /** note: exit status only available after destruction */
189 void store_exit_status(bool *_status_set, int *_exit_status);
190
191protected:
192 virtual int_type overflow(int_type c);
193
194 virtual std::streamsize xsputn(const char* s, std::streamsize num);
195};
196
197
198/** @brief stream around outpipebuf -- see comment there */
199class outpipestream : public std::ostream
200{
201protected:
202 outpipebuf buf;
203public:
204 outpipestream(const std::string& command)
205 : std::ostream(&buf), buf(command)
206 {}
207
208 void store_exit_status(bool *_status_set, int *_exit_status)
209 { buf.store_exit_status(_status_set, _exit_status); }
210};
211
212#endif