2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
20 /***************************************************************************
21 inpipestream.hxx - C++ streambuffer wrapper
23 begin : Thu Dec 27 2001
24 copyright : (C) 2001 by Intra2net AG
25 ***************************************************************************/
40 /** if the program exited normally and returned a return code */
43 /** the real return code of the program, only set when normal_exit true */
46 /** if the program was terminated by a signal */
47 bool terminated_by_signal;
49 /** number of the signal that terminated the program, only valid when terminated_by_signal true */
52 /** errormessage if we have one */
53 std::string error_message;
55 typedef struct ExecResult ExecResult;
57 std::string capture_exec(const std::string& command, ExecResult &rescode);
58 std::string capture_exec(const char *const *command, ExecResult &rescode,
59 const bool out=true, const bool err=false);
60 std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode,
61 const bool out=true, const bool err=false);
63 inline std::string capture_exec (const std::string &command)
66 return capture_exec(command,r);
69 inline std::string capture_exec(const char *const *command)
72 return capture_exec(command,r);
75 inline std::string capture_exec(const std::vector<std::string>& command)
78 return capture_exec(command,r);
81 /** @brief runs command and provides buffered input for it through pipe
83 * opens pipe to command using popen; exit status available after destruction
84 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
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!
90 class inpipebuf : public std::streambuf
97 // "callback" variables for destructor to store exit status
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);
111 void store_exit_status(bool *_status_set, int *_exit_status);
114 virtual int_type underflow();
117 FILE *init_without_shell (const char *const *argv,
118 const bool out, const bool err) const;
121 /** @brief stream around inpipebuf -- see comment there */
122 class inpipestream : public std::istream
128 inpipestream(const std::string& command,
129 const bool out=true, const bool err=false)
130 : std::istream(&buf), buf(command, out, err)
133 inpipestream(const char *const command[],
134 const bool out=true, const bool err=false)
135 : std::istream(&buf), buf(command, out, err)
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)
143 void store_exit_status(bool *_status_set, int *_exit_status)
144 { buf.store_exit_status(_status_set, _exit_status); }
147 /** @brief runs command and provides buffered ouptput from it through pipe
149 * opens pipe to command using popen; exit status available after destruction
150 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
152 class outpipebuf : public std::streambuf
157 // "callback" variables for destructor to store exit status
162 outpipebuf(const std::string& command);
166 /** note: exit status only available after destruction */
167 void store_exit_status(bool *_status_set, int *_exit_status);
170 virtual int_type overflow(int_type c);
172 virtual std::streamsize xsputn(const char* s, std::streamsize num);
176 /** @brief stream around outpipebuf -- see comment there */
177 class outpipestream : public std::ostream
182 outpipestream(const std::string& command)
183 : std::ostream(&buf), buf(command)
186 void store_exit_status(bool *_status_set, int *_exit_status)
187 { buf.store_exit_status(_status_set, _exit_status); }