1 // Generated by gmmproc 2.64.2 -- DO NOT MODIFY!
2
3
4 #include <glibmm.h>
5
6 #include <glibmm/spawn.h>
7 #include <glibmm/private/spawn_p.h>
8
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 Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 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 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include <glib.h>
27 #include <glibmm/exceptionhandler.h>
28 #include <glibmm/utility.h>
29
30 namespace
31 {
32
33 extern "C" {
34
35 /* Helper callback to invoke the actual sigc++ slot.
36 * We don't need to worry about (un)referencing, since the
37 * child process gets its own copy of the parent's memory anyway.
38 */
39 static void
child_setup_callback(void * user_data)40 child_setup_callback(void* user_data)
41 {
42 try
43 {
44 (*reinterpret_cast<Glib::SlotSpawnChildSetup*>(user_data))();
45 }
46 catch (...)
47 {
48 Glib::exception_handlers_invoke();
49 }
50 }
51
52 static void
copy_output_buf(std::string * dest,const char * buf)53 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 namespace Glib
69 {
70
71 /**** process spawning functions *******************************************/
72
73 void
spawn_async_with_pipes(const std::string & working_directory,const Glib::ArrayHandle<std::string> & argv,const Glib::ArrayHandle<std::string> & envp,SpawnFlags flags,const SlotSpawnChildSetup & child_setup,Pid * child_pid,int * standard_input,int * standard_output,int * standard_error)74 spawn_async_with_pipes(const std::string& working_directory,
75 const Glib::ArrayHandle<std::string>& argv, const Glib::ArrayHandle<std::string>& envp,
76 SpawnFlags flags, const SlotSpawnChildSetup& child_setup, Pid* child_pid, int* standard_input,
77 int* standard_output, int* standard_error)
78 {
79 const bool setup_slot = !child_setup.empty();
80 auto child_setup_ = child_setup;
81 GError* gerror = nullptr;
82
83 g_spawn_async_with_pipes(Glib::c_str_or_nullptr(working_directory),
84 const_cast<char**>(argv.data()), const_cast<char**>(envp.data()),
85 static_cast<GSpawnFlags>(unsigned(flags)), (setup_slot) ? &child_setup_callback : nullptr,
86 (setup_slot) ? &child_setup_ : nullptr, child_pid, standard_input, standard_output,
87 standard_error, &gerror);
88
89 if (gerror)
90 Glib::Error::throw_exception(gerror);
91 }
92
93 void
spawn_async_with_pipes(const std::string & working_directory,const Glib::ArrayHandle<std::string> & argv,SpawnFlags flags,const SlotSpawnChildSetup & child_setup,Pid * child_pid,int * standard_input,int * standard_output,int * standard_error)94 spawn_async_with_pipes(const std::string& working_directory,
95 const Glib::ArrayHandle<std::string>& argv, SpawnFlags flags,
96 const SlotSpawnChildSetup& child_setup, Pid* child_pid, int* standard_input, int* standard_output,
97 int* standard_error)
98 {
99 const bool setup_slot = !child_setup.empty();
100 auto child_setup_ = child_setup;
101 GError* gerror = nullptr;
102
103 g_spawn_async_with_pipes(Glib::c_str_or_nullptr(working_directory),
104 const_cast<char**>(argv.data()), nullptr, static_cast<GSpawnFlags>(unsigned(flags)),
105 (setup_slot) ? &child_setup_callback : nullptr, (setup_slot) ? &child_setup_ : nullptr,
106 child_pid, standard_input, standard_output, standard_error, &gerror);
107
108 if (gerror)
109 Glib::Error::throw_exception(gerror);
110 }
111
112 void
spawn_async(const std::string & working_directory,const Glib::ArrayHandle<std::string> & argv,const Glib::ArrayHandle<std::string> & envp,SpawnFlags flags,const SlotSpawnChildSetup & child_setup,Pid * child_pid)113 spawn_async(const std::string& working_directory, const Glib::ArrayHandle<std::string>& argv,
114 const Glib::ArrayHandle<std::string>& envp, SpawnFlags flags,
115 const SlotSpawnChildSetup& child_setup, Pid* child_pid)
116 {
117 const bool setup_slot = !child_setup.empty();
118 auto child_setup_ = child_setup;
119 GError* gerror = nullptr;
120
121 g_spawn_async(Glib::c_str_or_nullptr(working_directory), const_cast<char**>(argv.data()),
122 const_cast<char**>(envp.data()), static_cast<GSpawnFlags>(unsigned(flags)),
123 (setup_slot) ? &child_setup_callback : nullptr, (setup_slot) ? &child_setup_ : nullptr,
124 child_pid, &gerror);
125
126 if (gerror)
127 Glib::Error::throw_exception(gerror);
128 }
129
130 void
spawn_async(const std::string & working_directory,const Glib::ArrayHandle<std::string> & argv,SpawnFlags flags,const SlotSpawnChildSetup & child_setup,Pid * child_pid)131 spawn_async(const std::string& working_directory, const Glib::ArrayHandle<std::string>& argv,
132 SpawnFlags flags, const SlotSpawnChildSetup& child_setup, Pid* child_pid)
133 {
134 const bool setup_slot = !child_setup.empty();
135 auto child_setup_ = child_setup;
136 GError* gerror = nullptr;
137
138 g_spawn_async(Glib::c_str_or_nullptr(working_directory), const_cast<char**>(argv.data()), nullptr,
139 static_cast<GSpawnFlags>(unsigned(flags)), (setup_slot) ? &child_setup_callback : nullptr,
140 (setup_slot) ? &child_setup_ : nullptr, child_pid, &gerror);
141
142 if (gerror)
143 Glib::Error::throw_exception(gerror);
144 }
145
146 void
spawn_sync(const std::string & working_directory,const Glib::ArrayHandle<std::string> & argv,const Glib::ArrayHandle<std::string> & envp,SpawnFlags flags,const SlotSpawnChildSetup & child_setup,std::string * standard_output,std::string * standard_error,int * exit_status)147 spawn_sync(const std::string& working_directory, const Glib::ArrayHandle<std::string>& argv,
148 const Glib::ArrayHandle<std::string>& envp, SpawnFlags flags,
149 const SlotSpawnChildSetup& child_setup, std::string* standard_output, std::string* standard_error,
150 int* exit_status)
151 {
152 const bool setup_slot = !child_setup.empty();
153 auto child_setup_ = child_setup;
154
155 GError* gerror = nullptr;
156 char* pch_buf_standard_output = nullptr;
157 char* pch_buf_standard_error = nullptr;
158 g_spawn_sync(Glib::c_str_or_nullptr(working_directory), const_cast<char**>(argv.data()),
159 const_cast<char**>(envp.data()), static_cast<GSpawnFlags>(unsigned(flags)),
160 (setup_slot) ? &child_setup_callback : nullptr, (setup_slot) ? &child_setup_ : nullptr,
161 (standard_output) ? &pch_buf_standard_output : nullptr,
162 (standard_error) ? &pch_buf_standard_error : nullptr, exit_status, &gerror);
163 auto buf_standard_output = make_unique_ptr_gfree(pch_buf_standard_output);
164 auto buf_standard_error = make_unique_ptr_gfree(pch_buf_standard_error);
165
166 if (gerror)
167 Glib::Error::throw_exception(gerror);
168
169 copy_output_buf(standard_output, buf_standard_output.get());
170 copy_output_buf(standard_error, buf_standard_error.get());
171 }
172
173 void
spawn_sync(const std::string & working_directory,const Glib::ArrayHandle<std::string> & argv,SpawnFlags flags,const SlotSpawnChildSetup & child_setup,std::string * standard_output,std::string * standard_error,int * exit_status)174 spawn_sync(const std::string& working_directory, const Glib::ArrayHandle<std::string>& argv,
175 SpawnFlags flags, const SlotSpawnChildSetup& child_setup, std::string* standard_output,
176 std::string* standard_error, int* exit_status)
177 {
178 const bool setup_slot = !child_setup.empty();
179 auto child_setup_ = child_setup;
180
181 char* pch_buf_standard_output = nullptr;
182 char* pch_buf_standard_error = nullptr;
183 GError* gerror = nullptr;
184
185 g_spawn_sync(Glib::c_str_or_nullptr(working_directory), const_cast<char**>(argv.data()), nullptr,
186 static_cast<GSpawnFlags>(unsigned(flags)), (setup_slot) ? &child_setup_callback : nullptr,
187 (setup_slot) ? &child_setup_ : nullptr, (standard_output) ? &pch_buf_standard_output : nullptr,
188 (standard_error) ? &pch_buf_standard_error : nullptr, exit_status, &gerror);
189 auto buf_standard_output = make_unique_ptr_gfree(pch_buf_standard_output);
190 auto buf_standard_error = make_unique_ptr_gfree(pch_buf_standard_error);
191
192 if (gerror)
193 Glib::Error::throw_exception(gerror);
194
195 copy_output_buf(standard_output, buf_standard_output.get());
196 copy_output_buf(standard_error, buf_standard_error.get());
197 }
198
199 void
spawn_command_line_async(const std::string & command_line)200 spawn_command_line_async(const std::string& command_line)
201 {
202 GError* gerror = nullptr;
203 g_spawn_command_line_async(command_line.c_str(), &gerror);
204
205 if (gerror)
206 Glib::Error::throw_exception(gerror);
207 }
208
209 void
spawn_command_line_sync(const std::string & command_line,std::string * standard_output,std::string * standard_error,int * exit_status)210 spawn_command_line_sync(const std::string& command_line, std::string* standard_output,
211 std::string* standard_error, int* exit_status)
212 {
213 char* pch_buf_standard_output = nullptr;
214 char* pch_buf_standard_error = nullptr;
215 GError* gerror = nullptr;
216
217 g_spawn_command_line_sync(command_line.c_str(),
218 (standard_output) ? &pch_buf_standard_output : nullptr,
219 (standard_error) ? &pch_buf_standard_error : nullptr, exit_status, &gerror);
220 auto buf_standard_output = make_unique_ptr_gfree(pch_buf_standard_output);
221 auto buf_standard_error = make_unique_ptr_gfree(pch_buf_standard_error);
222
223 if (gerror)
224 Glib::Error::throw_exception(gerror);
225
226 copy_output_buf(standard_output, buf_standard_output.get());
227 copy_output_buf(standard_error, buf_standard_error.get());
228 }
229
230 void
spawn_close_pid(Pid pid)231 spawn_close_pid(Pid pid)
232 {
233 g_spawn_close_pid(pid);
234 }
235
236 } // namespace Glib
237
238 namespace
239 {
240 } // anonymous namespace
241
242
SpawnError(Glib::SpawnError::Code error_code,const Glib::ustring & error_message)243 Glib::SpawnError::SpawnError(Glib::SpawnError::Code error_code, const Glib::ustring& error_message)
244 :
245 Glib::Error (G_SPAWN_ERROR, error_code, error_message)
246 {}
247
SpawnError(GError * gobject)248 Glib::SpawnError::SpawnError(GError* gobject)
249 :
250 Glib::Error (gobject)
251 {}
252
code() const253 Glib::SpawnError::Code Glib::SpawnError::code() const
254 {
255 return static_cast<Code>(Glib::Error::code());
256 }
257
throw_func(GError * gobject)258 void Glib::SpawnError::throw_func(GError* gobject)
259 {
260 throw Glib::SpawnError(gobject);
261 }
262
263
264