1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.SubprocessLauncher;
26 
27 private import gio.Subprocess;
28 private import glib.ConstructionException;
29 private import glib.ErrorG;
30 private import glib.GException;
31 private import glib.Str;
32 private import gobject.ObjectG;
33 private import gtkc.gio;
34 public  import gtkc.giotypes;
35 
36 
37 /**
38  * This class contains a set of options for launching child processes,
39  * such as where its standard input and output will be directed, the
40  * argument list, the environment, and more.
41  * 
42  * While the #GSubprocess class has high level functions covering
43  * popular cases, use of this class allows access to more advanced
44  * options.  It can also be used to launch multiple subprocesses with
45  * a similar configuration.
46  *
47  * Since: 2.40
48  */
49 public class SubprocessLauncher : ObjectG
50 {
51 	/** the main Gtk struct */
52 	protected GSubprocessLauncher* gSubprocessLauncher;
53 
54 	/** Get the main Gtk struct */
55 	public GSubprocessLauncher* getSubprocessLauncherStruct(bool transferOwnership = false)
56 	{
57 		if (transferOwnership)
58 			ownedRef = false;
59 		return gSubprocessLauncher;
60 	}
61 
62 	/** the main Gtk struct as a void* */
63 	protected override void* getStruct()
64 	{
65 		return cast(void*)gSubprocessLauncher;
66 	}
67 
68 	protected override void setStruct(GObject* obj)
69 	{
70 		gSubprocessLauncher = cast(GSubprocessLauncher*)obj;
71 		super.setStruct(obj);
72 	}
73 
74 	/**
75 	 * Sets our main struct and passes it to the parent class.
76 	 */
77 	public this (GSubprocessLauncher* gSubprocessLauncher, bool ownedRef = false)
78 	{
79 		this.gSubprocessLauncher = gSubprocessLauncher;
80 		super(cast(GObject*)gSubprocessLauncher, ownedRef);
81 	}
82 
83 
84 	/** */
85 	public static GType getType()
86 	{
87 		return g_subprocess_launcher_get_type();
88 	}
89 
90 	/**
91 	 * Creates a new #GSubprocessLauncher.
92 	 *
93 	 * The launcher is created with the default options.  A copy of the
94 	 * environment of the calling process is made at the time of this call
95 	 * and will be used as the environment that the process is launched in.
96 	 *
97 	 * Params:
98 	 *     flags = #GSubprocessFlags
99 	 *
100 	 * Since: 2.40
101 	 *
102 	 * Throws: ConstructionException GTK+ fails to create the object.
103 	 */
104 	public this(GSubprocessFlags flags)
105 	{
106 		auto p = g_subprocess_launcher_new(flags);
107 		
108 		if(p is null)
109 		{
110 			throw new ConstructionException("null returned by new");
111 		}
112 		
113 		this(cast(GSubprocessLauncher*) p, true);
114 	}
115 
116 	/**
117 	 * Returns the value of the environment variable @variable in the
118 	 * environment of processes launched from this launcher.
119 	 *
120 	 * On UNIX, the returned string can be an arbitrary byte string.
121 	 * On Windows, it will be UTF-8.
122 	 *
123 	 * Params:
124 	 *     variable = the environment variable to get
125 	 *
126 	 * Returns: the value of the environment variable, %NULL if unset
127 	 *
128 	 * Since: 2.40
129 	 */
130 	public string getenv(string variable)
131 	{
132 		return Str.toString(g_subprocess_launcher_getenv(gSubprocessLauncher, Str.toStringz(variable)));
133 	}
134 
135 	/**
136 	 * Sets up a child setup function.
137 	 *
138 	 * The child setup function will be called after fork() but before
139 	 * exec() on the child's side.
140 	 *
141 	 * @destroy_notify will not be automatically called on the child's side
142 	 * of the fork().  It will only be called when the last reference on the
143 	 * #GSubprocessLauncher is dropped or when a new child setup function is
144 	 * given.
145 	 *
146 	 * %NULL can be given as @child_setup to disable the functionality.
147 	 *
148 	 * Child setup functions are only available on UNIX.
149 	 *
150 	 * Params:
151 	 *     childSetup = a #GSpawnChildSetupFunc to use as the child setup function
152 	 *     userData = user data for @child_setup
153 	 *     destroyNotify = a #GDestroyNotify for @user_data
154 	 *
155 	 * Since: 2.40
156 	 */
157 	public void setChildSetup(GSpawnChildSetupFunc childSetup, void* userData, GDestroyNotify destroyNotify)
158 	{
159 		g_subprocess_launcher_set_child_setup(gSubprocessLauncher, childSetup, userData, destroyNotify);
160 	}
161 
162 	/**
163 	 * Sets the current working directory that processes will be launched
164 	 * with.
165 	 *
166 	 * By default processes are launched with the current working directory
167 	 * of the launching process at the time of launch.
168 	 *
169 	 * Params:
170 	 *     cwd = the cwd for launched processes
171 	 *
172 	 * Since: 2.40
173 	 */
174 	public void setCwd(string cwd)
175 	{
176 		g_subprocess_launcher_set_cwd(gSubprocessLauncher, Str.toStringz(cwd));
177 	}
178 
179 	/**
180 	 * Replace the entire environment of processes launched from this
181 	 * launcher with the given 'environ' variable.
182 	 *
183 	 * Typically you will build this variable by using g_listenv() to copy
184 	 * the process 'environ' and using the functions g_environ_setenv(),
185 	 * g_environ_unsetenv(), etc.
186 	 *
187 	 * As an alternative, you can use g_subprocess_launcher_setenv(),
188 	 * g_subprocess_launcher_unsetenv(), etc.
189 	 *
190 	 * Pass %NULL to inherit the parent  process' environment. Pass an
191 	 * empty array to set an empty environment.
192 	 *
193 	 * On UNIX, all strings in this array can be arbitrary byte strings.
194 	 * On Windows, they should be in UTF-8.
195 	 *
196 	 * Params:
197 	 *     env = the replacement environment
198 	 *
199 	 * Since: 2.40
200 	 */
201 	public void setEnviron(string[] env)
202 	{
203 		g_subprocess_launcher_set_environ(gSubprocessLauncher, Str.toStringzArray(env));
204 	}
205 
206 	/**
207 	 * Sets the flags on the launcher.
208 	 *
209 	 * The default flags are %G_SUBPROCESS_FLAGS_NONE.
210 	 *
211 	 * You may not set flags that specify conflicting options for how to
212 	 * handle a particular stdio stream (eg: specifying both
213 	 * %G_SUBPROCESS_FLAGS_STDIN_PIPE and
214 	 * %G_SUBPROCESS_FLAGS_STDIN_INHERIT).
215 	 *
216 	 * You may also not set a flag that conflicts with a previous call to a
217 	 * function like g_subprocess_launcher_set_stdin_file_path() or
218 	 * g_subprocess_launcher_take_stdout_fd().
219 	 *
220 	 * Params:
221 	 *     flags = #GSubprocessFlags
222 	 *
223 	 * Since: 2.40
224 	 */
225 	public void setFlags(GSubprocessFlags flags)
226 	{
227 		g_subprocess_launcher_set_flags(gSubprocessLauncher, flags);
228 	}
229 
230 	/**
231 	 * Sets the file path to use as the stderr for spawned processes.
232 	 *
233 	 * If @path is %NULL then any previously given path is unset.
234 	 *
235 	 * The file will be created or truncated when the process is spawned, as
236 	 * would be the case if using '2>' at the shell.
237 	 *
238 	 * If you want to send both stdout and stderr to the same file then use
239 	 * %G_SUBPROCESS_FLAGS_STDERR_MERGE.
240 	 *
241 	 * You may not set a stderr file path if a stderr fd is already set or
242 	 * if the launcher flags contain any flags directing stderr elsewhere.
243 	 *
244 	 * This feature is only available on UNIX.
245 	 *
246 	 * Params:
247 	 *     path = a filename or %NULL
248 	 *
249 	 * Since: 2.40
250 	 */
251 	public void setStderrFilePath(string path)
252 	{
253 		g_subprocess_launcher_set_stderr_file_path(gSubprocessLauncher, Str.toStringz(path));
254 	}
255 
256 	/**
257 	 * Sets the file path to use as the stdin for spawned processes.
258 	 *
259 	 * If @path is %NULL then any previously given path is unset.
260 	 *
261 	 * The file must exist or spawning the process will fail.
262 	 *
263 	 * You may not set a stdin file path if a stdin fd is already set or if
264 	 * the launcher flags contain any flags directing stdin elsewhere.
265 	 *
266 	 * This feature is only available on UNIX.
267 	 *
268 	 * Since: 2.40
269 	 */
270 	public void setStdinFilePath(string path)
271 	{
272 		g_subprocess_launcher_set_stdin_file_path(gSubprocessLauncher, Str.toStringz(path));
273 	}
274 
275 	/**
276 	 * Sets the file path to use as the stdout for spawned processes.
277 	 *
278 	 * If @path is %NULL then any previously given path is unset.
279 	 *
280 	 * The file will be created or truncated when the process is spawned, as
281 	 * would be the case if using '>' at the shell.
282 	 *
283 	 * You may not set a stdout file path if a stdout fd is already set or
284 	 * if the launcher flags contain any flags directing stdout elsewhere.
285 	 *
286 	 * This feature is only available on UNIX.
287 	 *
288 	 * Params:
289 	 *     path = a filename or %NULL
290 	 *
291 	 * Since: 2.40
292 	 */
293 	public void setStdoutFilePath(string path)
294 	{
295 		g_subprocess_launcher_set_stdout_file_path(gSubprocessLauncher, Str.toStringz(path));
296 	}
297 
298 	/**
299 	 * Sets the environment variable @variable in the environment of
300 	 * processes launched from this launcher.
301 	 *
302 	 * On UNIX, both the variable's name and value can be arbitrary byte
303 	 * strings, except that the variable's name cannot contain '='.
304 	 * On Windows, they should be in UTF-8.
305 	 *
306 	 * Params:
307 	 *     variable = the environment variable to set, must not contain '='
308 	 *     value = the new value for the variable
309 	 *     overwrite = whether to change the variable if it already exists
310 	 *
311 	 * Since: 2.40
312 	 */
313 	public void setenv(string variable, string value, bool overwrite)
314 	{
315 		g_subprocess_launcher_setenv(gSubprocessLauncher, Str.toStringz(variable), Str.toStringz(value), overwrite);
316 	}
317 
318 	/**
319 	 * Creates a #GSubprocess given a provided array of arguments.
320 	 *
321 	 * Params:
322 	 *     argv = Command line arguments
323 	 *
324 	 * Returns: A new #GSubprocess, or %NULL on error (and @error will be set)
325 	 *
326 	 * Since: 2.40
327 	 *
328 	 * Throws: GException on failure.
329 	 */
330 	public Subprocess spawnv(string[] argv)
331 	{
332 		GError* err = null;
333 		
334 		auto p = g_subprocess_launcher_spawnv(gSubprocessLauncher, Str.toStringzArray(argv), &err);
335 		
336 		if (err !is null)
337 		{
338 			throw new GException( new ErrorG(err) );
339 		}
340 		
341 		if(p is null)
342 		{
343 			return null;
344 		}
345 		
346 		return ObjectG.getDObject!(Subprocess)(cast(GSubprocess*) p, true);
347 	}
348 
349 	/**
350 	 * Transfer an arbitrary file descriptor from parent process to the
351 	 * child.  This function takes "ownership" of the fd; it will be closed
352 	 * in the parent when @self is freed.
353 	 *
354 	 * By default, all file descriptors from the parent will be closed.
355 	 * This function allows you to create (for example) a custom pipe() or
356 	 * socketpair() before launching the process, and choose the target
357 	 * descriptor in the child.
358 	 *
359 	 * An example use case is GNUPG, which has a command line argument
360 	 * --passphrase-fd providing a file descriptor number where it expects
361 	 * the passphrase to be written.
362 	 *
363 	 * Params:
364 	 *     sourceFd = File descriptor in parent process
365 	 *     targetFd = Target descriptor for child process
366 	 */
367 	public void takeFd(int sourceFd, int targetFd)
368 	{
369 		g_subprocess_launcher_take_fd(gSubprocessLauncher, sourceFd, targetFd);
370 	}
371 
372 	/**
373 	 * Sets the file descriptor to use as the stderr for spawned processes.
374 	 *
375 	 * If @fd is -1 then any previously given fd is unset.
376 	 *
377 	 * Note that the default behaviour is to pass stderr through to the
378 	 * stderr of the parent process.
379 	 *
380 	 * The passed @fd belongs to the #GSubprocessLauncher.  It will be
381 	 * automatically closed when the launcher is finalized.  The file
382 	 * descriptor will also be closed on the child side when executing the
383 	 * spawned process.
384 	 *
385 	 * You may not set a stderr fd if a stderr file path is already set or
386 	 * if the launcher flags contain any flags directing stderr elsewhere.
387 	 *
388 	 * This feature is only available on UNIX.
389 	 *
390 	 * Params:
391 	 *     fd = a file descriptor, or -1
392 	 *
393 	 * Since: 2.40
394 	 */
395 	public void takeStderrFd(int fd)
396 	{
397 		g_subprocess_launcher_take_stderr_fd(gSubprocessLauncher, fd);
398 	}
399 
400 	/**
401 	 * Sets the file descriptor to use as the stdin for spawned processes.
402 	 *
403 	 * If @fd is -1 then any previously given fd is unset.
404 	 *
405 	 * Note that if your intention is to have the stdin of the calling
406 	 * process inherited by the child then %G_SUBPROCESS_FLAGS_STDIN_INHERIT
407 	 * is a better way to go about doing that.
408 	 *
409 	 * The passed @fd is noted but will not be touched in the current
410 	 * process.  It is therefore necessary that it be kept open by the
411 	 * caller until the subprocess is spawned.  The file descriptor will
412 	 * also not be explicitly closed on the child side, so it must be marked
413 	 * O_CLOEXEC if that's what you want.
414 	 *
415 	 * You may not set a stdin fd if a stdin file path is already set or if
416 	 * the launcher flags contain any flags directing stdin elsewhere.
417 	 *
418 	 * This feature is only available on UNIX.
419 	 *
420 	 * Params:
421 	 *     fd = a file descriptor, or -1
422 	 *
423 	 * Since: 2.40
424 	 */
425 	public void takeStdinFd(int fd)
426 	{
427 		g_subprocess_launcher_take_stdin_fd(gSubprocessLauncher, fd);
428 	}
429 
430 	/**
431 	 * Sets the file descriptor to use as the stdout for spawned processes.
432 	 *
433 	 * If @fd is -1 then any previously given fd is unset.
434 	 *
435 	 * Note that the default behaviour is to pass stdout through to the
436 	 * stdout of the parent process.
437 	 *
438 	 * The passed @fd is noted but will not be touched in the current
439 	 * process.  It is therefore necessary that it be kept open by the
440 	 * caller until the subprocess is spawned.  The file descriptor will
441 	 * also not be explicitly closed on the child side, so it must be marked
442 	 * O_CLOEXEC if that's what you want.
443 	 *
444 	 * You may not set a stdout fd if a stdout file path is already set or
445 	 * if the launcher flags contain any flags directing stdout elsewhere.
446 	 *
447 	 * This feature is only available on UNIX.
448 	 *
449 	 * Params:
450 	 *     fd = a file descriptor, or -1
451 	 *
452 	 * Since: 2.40
453 	 */
454 	public void takeStdoutFd(int fd)
455 	{
456 		g_subprocess_launcher_take_stdout_fd(gSubprocessLauncher, fd);
457 	}
458 
459 	/**
460 	 * Removes the environment variable @variable from the environment of
461 	 * processes launched from this launcher.
462 	 *
463 	 * On UNIX, the variable's name can be an arbitrary byte string not
464 	 * containing '='. On Windows, it should be in UTF-8.
465 	 *
466 	 * Params:
467 	 *     variable = the environment variable to unset, must not contain '='
468 	 *
469 	 * Since: 2.40
470 	 */
471 	public void unsetenv(string variable)
472 	{
473 		g_subprocess_launcher_unsetenv(gSubprocessLauncher, Str.toStringz(variable));
474 	}
475 }