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