Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
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. | |
13 | ||
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. | |
16 | ||
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. | |
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> | |
e93545dd | 36 | |
f8f119bf GE |
37 | struct ExecResult |
38 | { | |
39 | /** if the program exited normally and returned a return code */ | |
40 | bool normal_exit; | |
41 | ||
42 | /** the real return code of the program, only set when normal_exit true */ | |
43 | char return_code; | |
44 | ||
45 | /** if the program was terminated by a signal */ | |
46 | bool terminated_by_signal; | |
47 | ||
48 | /** number of the signal that terminated the program, only valid when terminated_by_signal true */ | |
49 | int signal; | |
50 | ||
51 | /** errormessage if we have one */ | |
52 | std::string error_message; | |
53 | }; | |
54 | typedef struct ExecResult ExecResult; | |
55 | ||
56 | std::string capture_exec(const std::string& command, ExecResult &rescode); | |
57 | ||
4a56f9ac | 58 | inline std::string capture_exec(const std::string& command) |
f8f119bf GE |
59 | { |
60 | ExecResult r; | |
61 | return capture_exec(command,r); | |
62 | } | |
63 | ||
bf4c487c CH |
64 | /** @brief runs command and provides buffered input for it through pipe |
65 | * | |
66 | * opens pipe to command using popen; exit status available after destruction | |
67 | * (use WEXITSTATUS to get the "regular" return code (lowest byte)) | |
68 | * | |
69 | * ATTENTION: A lot of mysterious STL bugs occured | |
70 | * with a "real" buffer (buffer larger than 1 byte and up to 100 bytes) | |
71 | * -> Keep it slow and working! | |
72 | */ | |
e93545dd GE |
73 | class inpipebuf : public std::streambuf |
74 | { | |
20a79471 TJ |
75 | protected: |
76 | char buffer; | |
77 | FILE *pipe; | |
78 | ||
79 | // "callback" variables for destructor to store exit status | |
80 | bool *status_set; | |
81 | int *exit_status; | |
82 | ||
83 | public: | |
7e606af5 | 84 | inpipebuf(const std::string& command); |
20a79471 | 85 | |
7e606af5 | 86 | ~inpipebuf(); |
20a79471 | 87 | |
7e606af5 | 88 | void store_exit_status(bool *_status_set, int *_exit_status); |
20a79471 TJ |
89 | |
90 | protected: | |
7e606af5 | 91 | virtual int_type underflow(); |
20a79471 | 92 | }; |
e93545dd | 93 | |
bf4c487c | 94 | /** @brief stream around inpipebuf -- see comment there */ |
e93545dd GE |
95 | class inpipestream : public std::istream |
96 | { | |
20a79471 TJ |
97 | protected: |
98 | inpipebuf buf; | |
99 | ||
100 | public: | |
101 | inpipestream(const std::string& command) | |
d8ffcdb0 | 102 | : std::istream(&buf), buf(command) |
20a79471 TJ |
103 | {} |
104 | ||
105 | void store_exit_status(bool *_status_set, int *_exit_status) | |
106 | { buf.store_exit_status(_status_set, _exit_status); } | |
e93545dd GE |
107 | }; |
108 | ||
bf4c487c CH |
109 | /** @brief runs command and provides buffered ouptput from it through pipe |
110 | * | |
111 | * opens pipe to command using popen; exit status available after destruction | |
112 | * (use WEXITSTATUS to get the "regular" return code (lowest byte)) | |
113 | */ | |
e93545dd GE |
114 | class outpipebuf : public std::streambuf |
115 | { | |
20a79471 TJ |
116 | protected: |
117 | FILE *pipe; | |
118 | ||
119 | // "callback" variables for destructor to store exit status | |
120 | bool *status_set; | |
121 | int *exit_status; | |
122 | ||
123 | public: | |
7e606af5 GE |
124 | outpipebuf(const std::string& command); |
125 | ||
126 | ~outpipebuf(); | |
20a79471 | 127 | |
bf4c487c | 128 | /** note: exit status only available after destruction */ |
7e606af5 | 129 | void store_exit_status(bool *_status_set, int *_exit_status); |
20a79471 TJ |
130 | |
131 | protected: | |
7e606af5 GE |
132 | virtual int_type overflow(int_type c); |
133 | ||
134 | virtual std::streamsize xsputn(const char* s, std::streamsize num); | |
e93545dd GE |
135 | }; |
136 | ||
bf4c487c CH |
137 | |
138 | /** @brief stream around outpipebuf -- see comment there */ | |
e93545dd GE |
139 | class outpipestream : public std::ostream |
140 | { | |
20a79471 TJ |
141 | protected: |
142 | outpipebuf buf; | |
143 | public: | |
144 | outpipestream(const std::string& command) | |
d8ffcdb0 | 145 | : std::ostream(&buf), buf(command) |
20a79471 TJ |
146 | {} |
147 | ||
148 | void store_exit_status(bool *_status_set, int *_exit_status) | |
149 | { buf.store_exit_status(_status_set, _exit_status); } | |
e93545dd GE |
150 | }; |
151 | ||
152 | #endif |