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 ***************************************************************************/
30 #include <sys/prctl.h>
40 #include <stringfunc.hxx>
44 /** if the program exited normally and returned a return code */
47 /** the real return code of the program, only set when normal_exit true */
50 /** if the program was terminated by a signal */
51 bool terminated_by_signal;
53 /** number of the signal that terminated the program, only valid when terminated_by_signal true */
56 /** errormessage if we have one */
57 std::string error_message;
59 inline std::string format (void) const
61 return std::string ("(")
62 + "(normal_exit " + (this->normal_exit ? "T" : "F") + ") "
63 "(return_code '" + I2n::to_string ((int)this->return_code) + "') "
64 "(signal " + (this->terminated_by_signal
65 ? strsignal (this->signal)
70 typedef struct ExecResult ExecResult;
72 namespace capture_flag {
74 static const int none = 0;
75 static const int collect_out = 1 << 0;
76 static const int collect_err = 1 << 1;
77 static const int search_path = 1 << 2;
78 static const int env_passthru = 1 << 3;
79 static const int no_new_privs = 1 << 4;
81 static const int dflt = collect_out;
82 static const int collect_any = collect_out | collect_err;
84 } /* [namespace capture_flag] */
86 std::string capture_exec(const std::string& command, ExecResult &rescode);
87 std::string capture_exec(const char *const *command, ExecResult &rescode,
88 const int flags=capture_flag::dflt);
89 std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode,
90 const int flags=capture_flag::dflt);
92 inline std::string capture_exec (const std::string &command)
95 return capture_exec(command,r);
98 inline std::string capture_exec(const char *const *command)
101 return capture_exec(command,r);
104 inline std::string capture_exec(const std::vector<std::string>& command)
107 return capture_exec(command,r);
110 /** @brief runs command and provides buffered input for it through pipe
112 * opens pipe to command using popen; exit status available after destruction
113 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
115 * ATTENTION: A lot of mysterious STL bugs occured
116 * with a "real" buffer (buffer larger than 1 byte and up to 100 bytes)
117 * -> Keep it slow and working!
119 class inpipebuf : public std::streambuf
127 // "callback" variables for destructor to store exit status
132 inpipebuf(const std::string& command, const int flags);
133 inpipebuf(const char *const *command, const int flags);
134 inpipebuf(const std::vector<std::string> &command, const int flags);
138 void store_exit_status(bool *_status_set, int *_exit_status);
141 virtual int_type underflow();
144 std::pair <pid_t, FILE *>
145 init_without_shell (const char *const *argv, const int flags) const;
148 /** @brief stream around inpipebuf -- see comment there */
149 class inpipestream : public std::istream
155 inpipestream(const std::string& command,
156 const int flags=capture_flag::dflt)
157 : std::istream(&buf), buf(command, flags)
160 inpipestream(const char *const command[],
161 const int flags=capture_flag::dflt)
162 : std::istream(&buf), buf(command, flags)
165 inpipestream(const std::vector<std::string> &command,
166 const int flags=capture_flag::dflt)
167 : std::istream(&buf), buf(command, flags)
170 void store_exit_status(bool *_status_set, int *_exit_status)
171 { buf.store_exit_status(_status_set, _exit_status); }
174 /** @brief runs command and provides buffered ouptput from it through pipe
176 * opens pipe to command using popen; exit status available after destruction
177 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
179 class outpipebuf : public std::streambuf
184 // "callback" variables for destructor to store exit status
189 outpipebuf(const std::string& command);
193 /** note: exit status only available after destruction */
194 void store_exit_status(bool *_status_set, int *_exit_status);
197 virtual int_type overflow(int_type c);
199 virtual std::streamsize xsputn(const char* s, std::streamsize num);
203 /** @brief stream around outpipebuf -- see comment there */
204 class outpipestream : public std::ostream
209 outpipestream(const std::string& command)
210 : std::ostream(&buf), buf(command)
213 void store_exit_status(bool *_status_set, int *_exit_status)
214 { buf.store_exit_status(_status_set, _exit_status); }