cbc204cd700476efca304c2ebe698f41e49f03d8
[ardour.git] / libs / glibmm2 / glibmm / spawn.cc
1 // Generated by gtkmmproc -- DO NOT MODIFY!
2
3 #include <glibmm/spawn.h>
4 #include <glibmm/private/spawn_p.h>
5
6 // -*- c++ -*-
7 /* $Id$ */
8
9 /* Copyright (C) 2002 The gtkmm Development Team
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the Free
23  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <glib/gspawn.h>
27 #include <glibmm/exceptionhandler.h>
28 #include <glibmm/utility.h>
29
30
31 namespace
32 {
33
34 extern "C"
35 {
36
37 /* Helper callback to invoke the actual sigc++ slot.
38  * We don't need to worry about (un)referencing, since the
39  * child process gets its own copy of the parent's memory anyway.
40  */
41 void child_setup_callback(void* user_data)
42 {
43   try
44   {
45     (*reinterpret_cast<sigc::slot<void>*>(user_data))();
46   }
47   catch(...)
48   {
49     Glib::exception_handlers_invoke();
50   }
51 }
52
53 void copy_output_buf(std::string* dest, const char* buf)
54 {
55   if(dest)
56   {
57     if(buf)
58       *dest = buf;
59     else
60       dest->erase();
61   }
62 }
63
64 } //extern "C"
65
66 } //anonymous namespace
67
68
69 namespace Glib
70 {
71
72 /**** process spawning functions *******************************************/
73
74 void spawn_async_with_pipes(const std::string& working_directory,
75                             const Glib::ArrayHandle<std::string>& argv,
76                             const Glib::ArrayHandle<std::string>& envp,
77                             SpawnFlags flags,
78                             const sigc::slot<void>& child_setup,
79                             Pid* child_pid,
80                             int* standard_input,
81                             int* standard_output,
82                             int* standard_error)
83 {
84   const bool setup_slot = !child_setup.empty();
85   sigc::slot<void> child_setup_ = child_setup;
86   GError* error = 0;
87
88   g_spawn_async_with_pipes(
89       working_directory.c_str(),
90       const_cast<char**>(argv.data()),
91       const_cast<char**>(envp.data()),
92       static_cast<GSpawnFlags>(unsigned(flags)),
93       (setup_slot) ? &child_setup_callback : 0,
94       (setup_slot) ? &child_setup_         : 0,
95       child_pid,
96       standard_input, standard_output, standard_error,
97       &error);
98
99   if(error)
100     Glib::Error::throw_exception(error);
101 }
102
103 void spawn_async_with_pipes(const std::string& working_directory,
104                             const Glib::ArrayHandle<std::string>& argv,
105                             SpawnFlags flags,
106                             const sigc::slot<void>& child_setup,
107                             Pid* child_pid,
108                             int* standard_input,
109                             int* standard_output,
110                             int* standard_error)
111 {
112   const bool setup_slot = !child_setup.empty();
113   sigc::slot<void> child_setup_ = child_setup;
114   GError* error = 0;
115
116   g_spawn_async_with_pipes(
117       working_directory.c_str(),
118       const_cast<char**>(argv.data()), 0,
119       static_cast<GSpawnFlags>(unsigned(flags)),
120       (setup_slot) ? &child_setup_callback : 0,
121       (setup_slot) ? &child_setup_         : 0,
122       child_pid,
123       standard_input, standard_output, standard_error,
124       &error);
125
126   if(error)
127     Glib::Error::throw_exception(error);
128 }
129
130 void spawn_async(const std::string& working_directory,
131                  const Glib::ArrayHandle<std::string>& argv,
132                  const Glib::ArrayHandle<std::string>& envp,
133                  SpawnFlags flags,
134                  const sigc::slot<void>& child_setup,
135                  Pid* child_pid)
136 {
137   const bool setup_slot = !child_setup.empty();
138   sigc::slot<void> child_setup_ = child_setup;
139   GError* error = 0;
140
141   g_spawn_async(
142       working_directory.c_str(),
143       const_cast<char**>(argv.data()),
144       const_cast<char**>(envp.data()),
145       static_cast<GSpawnFlags>(unsigned(flags)),
146       (setup_slot) ? &child_setup_callback : 0,
147       (setup_slot) ? &child_setup_         : 0,
148       child_pid,
149       &error);
150
151   if(error)
152     Glib::Error::throw_exception(error);
153 }
154
155 void spawn_async(const std::string& working_directory,
156                  const Glib::ArrayHandle<std::string>& argv,
157                  SpawnFlags flags,
158                  const sigc::slot<void>& child_setup,
159                  Pid* child_pid)
160 {
161   const bool setup_slot = !child_setup.empty();
162   sigc::slot<void> child_setup_ = child_setup;
163   GError* error = 0;
164
165   g_spawn_async(
166       working_directory.c_str(),
167       const_cast<char**>(argv.data()), 0,
168       static_cast<GSpawnFlags>(unsigned(flags)),
169       (setup_slot) ? &child_setup_callback : 0,
170       (setup_slot) ? &child_setup_         : 0,
171       child_pid,
172       &error);
173
174   if(error)
175     Glib::Error::throw_exception(error);
176 }
177
178 void spawn_sync(const std::string& working_directory,
179                 const Glib::ArrayHandle<std::string>& argv,
180                 const Glib::ArrayHandle<std::string>& envp,
181                 SpawnFlags flags,
182                 const sigc::slot<void>& child_setup,
183                 std::string* standard_output,
184                 std::string* standard_error,
185                 int* exit_status)
186 {
187   const bool setup_slot = !child_setup.empty();
188   sigc::slot<void> child_setup_ = child_setup;
189
190   Glib::ScopedPtr<char> buf_standard_output;
191   Glib::ScopedPtr<char> buf_standard_error;
192   GError* error = 0;
193
194   g_spawn_sync(
195       working_directory.c_str(),
196       const_cast<char**>(argv.data()),
197       const_cast<char**>(envp.data()),
198       static_cast<GSpawnFlags>(unsigned(flags)),
199       (setup_slot) ? &child_setup_callback : 0,
200       (setup_slot) ? &child_setup_         : 0,
201       (standard_output) ? buf_standard_output.addr() : 0,
202       (standard_error)  ? buf_standard_error.addr()  : 0,
203       exit_status,
204       &error);
205
206   if(error)
207     Glib::Error::throw_exception(error);
208
209   copy_output_buf(standard_output, buf_standard_output.get());
210   copy_output_buf(standard_error, buf_standard_error.get());
211 }
212
213 void spawn_sync(const std::string& working_directory,
214                 const Glib::ArrayHandle<std::string>& argv,
215                 SpawnFlags flags,
216                 const sigc::slot<void>& child_setup,
217                 std::string* standard_output,
218                 std::string* standard_error,
219                 int* exit_status)
220 {
221   const bool setup_slot = !child_setup.empty();
222   sigc::slot<void> child_setup_ = child_setup;
223
224   Glib::ScopedPtr<char> buf_standard_output;
225   Glib::ScopedPtr<char> buf_standard_error;
226   GError* error = 0;
227
228   g_spawn_sync(
229       working_directory.c_str(),
230       const_cast<char**>(argv.data()), 0,
231       static_cast<GSpawnFlags>(unsigned(flags)),
232       (setup_slot) ? &child_setup_callback : 0,
233       (setup_slot) ? &child_setup_         : 0,
234       (standard_output) ? buf_standard_output.addr() : 0,
235       (standard_error)  ? buf_standard_error.addr()  : 0,
236       exit_status,
237       &error);
238
239   if(error)
240     Glib::Error::throw_exception(error);
241
242   copy_output_buf(standard_output, buf_standard_output.get());
243   copy_output_buf(standard_error, buf_standard_error.get());
244 }
245
246 void spawn_command_line_async(const std::string& command_line)
247 {
248   GError* error = 0;
249   g_spawn_command_line_async(command_line.c_str(), &error);
250
251   if(error)
252     Glib::Error::throw_exception(error);
253 }
254
255 void spawn_command_line_sync(const std::string& command_line,
256                              std::string* standard_output,
257                              std::string* standard_error,
258                              int* exit_status)
259 {
260   Glib::ScopedPtr<char> buf_standard_output;
261   Glib::ScopedPtr<char> buf_standard_error;
262   GError* error = 0;
263
264   g_spawn_command_line_sync(
265       command_line.c_str(),
266       (standard_output) ? buf_standard_output.addr() : 0,
267       (standard_error)  ? buf_standard_error.addr()  : 0,
268       exit_status,
269       &error);
270
271   if(error)
272     Glib::Error::throw_exception(error);
273
274   copy_output_buf(standard_output, buf_standard_output.get());
275   copy_output_buf(standard_error, buf_standard_error.get());
276 }
277
278 void spawn_close_pid(Pid pid)
279 {
280   g_spawn_close_pid(pid);
281 }
282
283 } // namespace Glib
284
285
286 namespace
287 {
288 } // anonymous namespace
289
290
291 Glib::SpawnError::SpawnError(Glib::SpawnError::Code error_code, const Glib::ustring& error_message)
292 :
293   Glib::Error (G_SPAWN_ERROR, error_code, error_message)
294 {}
295
296 Glib::SpawnError::SpawnError(GError* gobject)
297 :
298   Glib::Error (gobject)
299 {}
300
301 Glib::SpawnError::Code Glib::SpawnError::code() const
302 {
303   return static_cast<Code>(Glib::Error::code());
304 }
305
306 void Glib::SpawnError::throw_func(GError* gobject)
307 {
308   throw Glib::SpawnError(gobject);
309 }
310
311