]>
Dogcows Code - chaz/yoink/blob - src/stlplus/portability/subprocesses.hpp
0369daad40b6b5c645c52e4533968ab5a54772bd
1 #ifndef STLPLUS_SUBPROCESSES
2 #define STLPLUS_SUBPROCESSES
3 ////////////////////////////////////////////////////////////////////////////////
5 // Author: Andy Rushton
6 // Copyright: (c) Southampton University 1999-2004
7 // (c) Andy Rushton 2004-2009
8 // License: BSD License, see ../docs/license.html
10 // Platform-independent wrapper around the very platform-specific handling of
11 // subprocesses. Uses the C++ convention that all resources must be contained in
12 // an object so that when a subprocess object goes out of scope the subprocess
13 // itself gets closed down.
15 ////////////////////////////////////////////////////////////////////////////////
16 #include "portability_fixes.hpp"
23 #include <map> // for std::pair - why is this not defined separately?
25 ////////////////////////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////////////////////////
31 // Argument vector class
32 // allows manipulation of argv-like vectors
33 // includes splitting of command lines into argvectors as per the shell
34 // (removing quotes) and the reverse conversion (adding quotes where necessary)
42 // create an empty vector
45 // copy constructor (yes it copies)
46 arg_vector (const arg_vector
&);
48 // construct from an argv
51 // construct from a command-line string
52 // includes de-quoting of values
53 arg_vector (const std::string
&);
54 arg_vector (const char*);
58 // assignment operators are compatible with the constructors
59 arg_vector
& operator = (const arg_vector
&);
60 arg_vector
& operator = (char**);
61 arg_vector
& operator = (const std::string
&);
62 arg_vector
& operator = (const char*);
64 // add an argument to the vector
65 arg_vector
& operator += (const std::string
&);
66 arg_vector
& operator -= (const std::string
&);
68 // insert/clear an argument at a certain index
69 // adding is like the other array classes - it moves the current item at index
70 // up one (and all subsequent values) to make room
71 void insert (unsigned index
, const std::string
&) throw(std::out_of_range
);
72 void clear (unsigned index
) throw(std::out_of_range
);
75 // number of values in the vector (including argv[0], the command itself
76 unsigned size (void) const;
78 // type conversion to the argv type
79 operator char** (void) const;
80 // function-based version of the above for people who don't like type conversions
81 char** argv (void) const;
83 // access individual values in the vector
84 char* operator [] (unsigned index
) const throw(std::out_of_range
);
86 // special-case access of the command name (e.g. to do path lookup on the command)
87 char* argv0 (void) const throw(std::out_of_range
);
89 // get the command-line string represented by this vector
90 // includes escaping of special characters and quoting
91 std::string
image (void) const;
94 ////////////////////////////////////////////////////////////////////////////////
96 // Allows manipulation of an environment vector
97 // This is typically used to create an environment to be used by a subprocess
98 // It does NOT modify the environment of the current process
101 #define ENVIRON_TYPE char*
103 #define ENVIRON_TYPE char**
112 // create an env_vector vector from the current process
114 env_vector (const env_vector
&);
117 env_vector
& operator = (const env_vector
&);
121 // manipulate the env_vector by adding or removing variables
122 // adding a name that already exists replaces its value
123 void add (const std::string
& name
, const std::string
& value
);
124 bool remove (const std::string
& name
);
126 // get the value associated with a name
127 // the first uses an indexed notation (e.g. env["PATH"] )
128 // the second is a function based form (e.g. env.get("PATH"))
129 std::string
operator [] (const std::string
& name
) const;
130 std::string
get (const std::string
& name
) const;
132 // number of name=value pairs in the env_vector
133 unsigned size (void) const;
135 // get the name=value pairs by index (in the range 0 to size()-1)
136 std::pair
<std::string
,std::string
> operator [] (unsigned index
) const throw(std::out_of_range
);
137 std::pair
<std::string
,std::string
> get (unsigned index
) const throw(std::out_of_range
);
139 // access the env_vector as an envp type - used for passing to subprocesses
140 ENVIRON_TYPE
envp (void) const;
143 ////////////////////////////////////////////////////////////////////////////////
146 #define PID_TYPE PROCESS_INFORMATION
147 #define PIPE_TYPE HANDLE
150 #define PIPE_TYPE int
153 ////////////////////////////////////////////////////////////////////////////////
154 // Synchronous subprocess
164 PIPE_TYPE m_child_in
;
165 PIPE_TYPE m_child_out
;
166 PIPE_TYPE m_child_err
;
173 virtual ~subprocess(void);
175 void add_variable(const std::string
& name
, const std::string
& value
);
176 bool remove_variable(const std::string
& name
);
178 bool spawn(const std::string
& path
, const arg_vector
& argv
,
179 bool connect_stdin
= false, bool connect_stdout
= false, bool connect_stderr
= false);
180 bool spawn(const std::string
& command_line
,
181 bool connect_stdin
= false, bool connect_stdout
= false, bool connect_stderr
= false);
183 virtual bool callback(void);
186 int write_stdin(std::string
& buffer
);
187 int read_stdout(std::string
& buffer
);
188 int read_stderr(std::string
& buffer
);
190 void close_stdin(void);
191 void close_stdout(void);
192 void close_stderr(void);
194 bool error(void) const;
195 int error_number(void) const;
196 std::string
error_text(void) const;
198 int exit_status(void) const;
202 subprocess(const subprocess
&);
203 subprocess
& operator=(const subprocess
&);
206 ////////////////////////////////////////////////////////////////////////////////
207 // Preconfigured subprocess which executes a command and captures its output
209 class backtick_subprocess
: public subprocess
214 backtick_subprocess(void);
215 virtual bool callback(void);
216 bool spawn(const std::string
& path
, const arg_vector
& argv
);
217 bool spawn(const std::string
& command_line
);
218 std::vector
<std::string
> text(void) const;
221 std::vector
<std::string
> backtick(const std::string
& path
, const arg_vector
& argv
);
222 std::vector
<std::string
> backtick(const std::string
& command_line
);
224 ////////////////////////////////////////////////////////////////////////////////
225 // Asynchronous subprocess
227 class async_subprocess
234 PIPE_TYPE m_child_in
;
235 PIPE_TYPE m_child_out
;
236 PIPE_TYPE m_child_err
;
243 async_subprocess(void);
244 virtual ~async_subprocess(void);
246 void add_variable(const std::string
& name
, const std::string
& value
);
247 bool remove_variable(const std::string
& name
);
249 bool spawn(const std::string
& path
, const arg_vector
& argv
,
250 bool connect_stdin
= false, bool connect_stdout
= false, bool connect_stderr
= false);
251 bool spawn(const std::string
& command_line
,
252 bool connect_stdin
= false, bool connect_stdout
= false, bool connect_stderr
= false);
254 virtual bool callback(void);
258 int write_stdin(std::string
& buffer
);
259 int read_stdout(std::string
& buffer
);
260 int read_stderr(std::string
& buffer
);
262 void close_stdin(void);
263 void close_stdout(void);
264 void close_stderr(void);
266 bool error(void) const;
267 int error_number(void) const;
268 std::string
error_text(void) const;
270 int exit_status(void) const;
274 async_subprocess(const async_subprocess
&);
275 async_subprocess
& operator=(const async_subprocess
&);
278 ////////////////////////////////////////////////////////////////////////////////
280 } // end namespace stlplus
This page took 0.047189 seconds and 4 git commands to generate.