geany
1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
![]() ![]() |
#include <errno.h>
#include <string.h>
#include "spawn.h"
#include <signal.h>
#include "support.h"
#include "geany.h"
Go to the source code of this file.
Classes | |
struct | _SpawnChannelData |
struct | _SpawnWatcherData |
Macros | |
#define | G_SPAWN_ERROR_TOO_BIG G_SPAWN_ERROR_2BIG |
#define | DEFAULT_IO_LENGTH 4096 |
#define | SPAWN_IO_FAILURE (G_IO_ERR | G_IO_HUP | G_IO_NVAL) /* always used together */ |
#define | SPAWN_CHANNEL_GIO_WATCH(sc) ((sc)->empty_gio_ins < 200) |
Typedefs | |
typedef struct _SpawnChannelData | SpawnChannelData |
typedef struct _SpawnWatcherData | SpawnWatcherData |
Functions | |
static gboolean | spawn_parse_argv (const gchar *command_line, gint *argcp, gchar ***argvp, GError **error) |
static gchar * | spawn_get_program_name (const gchar *command_line, GError **error) |
gboolean | spawn_check_command (const gchar *command_line, gboolean execute, GError **error) |
Checks whether a command line is valid. More... | |
gboolean | spawn_kill_process (GPid pid, GError **error) |
Kills a process. More... | |
static gboolean | spawn_async_with_pipes (const gchar *working_directory, const gchar *command_line, gchar **argv, gchar **envp, GPid *child_pid, gint *stdin_fd, gint *stdout_fd, gint *stderr_fd, GError **error) |
gboolean | spawn_async (const gchar *working_directory, const gchar *command_line, gchar **argv, gchar **envp, GPid *child_pid, GError **error) |
Executes a child asynchronously. More... | |
static void | spawn_destroy_common (SpawnChannelData *sc) |
static void | spawn_timeout_destroy_cb (gpointer data) |
static void | spawn_destroy_cb (gpointer data) |
static gboolean | spawn_write_cb (GIOChannel *channel, GIOCondition condition, gpointer data) |
static gboolean | spawn_read_cb (GIOChannel *channel, GIOCondition condition, gpointer data) |
static gboolean | spawn_timeout_read_cb (gpointer data) |
static void | spawn_finalize (SpawnWatcherData *sw) |
static gboolean | spawn_timeout_watch_cb (gpointer data) |
static void | spawn_watch_cb (GPid pid, gint status, gpointer data) |
gboolean | spawn_with_callbacks (const gchar *working_directory, const gchar *command_line, gchar **argv, gchar **envp, SpawnFlags spawn_flags, GIOFunc stdin_cb, gpointer stdin_data, SpawnReadFunc stdout_cb, gpointer stdout_data, gsize stdout_max_length, SpawnReadFunc stderr_cb, gpointer stderr_data, gsize stderr_max_length, GChildWatchFunc exit_cb, gpointer exit_data, GPid *child_pid, GError **error) |
Executes a child program and setups callbacks. More... | |
gboolean | spawn_write_data (GIOChannel *channel, GIOCondition condition, SpawnWriteData *data) |
Writes (a portion of) the data pointed by data->ptr to the channel. More... | |
static void | spawn_append_gstring_cb (GString *string, GIOCondition condition, gpointer data) |
static void | spawn_get_exit_status_cb (G_GNUC_UNUSED GPid pid, gint status, gpointer exit_status) |
gboolean | spawn_sync (const gchar *working_directory, const gchar *command_line, gchar **argv, gchar **envp, SpawnWriteData *stdin_data, GString *stdout_data, GString *stderr_data, gint *exit_status, GError **error) |
Executes a child synchronously. More... | |
#define SPAWN_CHANNEL_GIO_WATCH | ( | sc | ) | ((sc)->empty_gio_ins < 200) |
#define SPAWN_IO_FAILURE (G_IO_ERR | G_IO_HUP | G_IO_NVAL) /* always used together */ |
typedef struct _SpawnChannelData SpawnChannelData |
typedef struct _SpawnWatcherData SpawnWatcherData |
|
static |
Definition at line 1318 of file spawn.c.
Referenced by spawn_sync().
gboolean spawn_async | ( | const gchar * | working_directory, |
const gchar * | command_line, | ||
gchar ** | argv, | ||
gchar ** | envp, | ||
GPid * | child_pid, | ||
GError ** | error | ||
) |
Executes a child asynchronously.
A command line or an argument vector must be passed. If both are present, the argument vector is appended to the command line. An empty command line is not allowed.
If a child_pid is passed, it's your responsibility to invoke g_spawn_close_pid()
.
working_directory | child's current working directory, or NULL . |
command_line | child program and arguments, or NULL . |
argv | child's argument vector, or NULL . |
envp | child's environment, or NULL . |
child_pid | (out) return location for child process ID, or NULL . |
error | return location for error. |
TRUE
on success, FALSE
on error.Definition at line 814 of file spawn.c.
References error(), NULL, and spawn_async_with_pipes().
Referenced by build_run_cmd(), on_context_action1_activate(), open_external(), print_external(), utils_open_browser(), and utils_spawn_async().
|
static |
Definition at line 519 of file spawn.c.
References _, command, error(), G_SPAWN_ERROR_TOO_BIG, NULL, spawn_get_program_name(), spawn_parse_argv(), and strstr().
Referenced by spawn_async(), and spawn_with_callbacks().
gboolean spawn_check_command | ( | const gchar * | command_line, |
gboolean | execute, | ||
GError ** | error | ||
) |
Checks whether a command line is valid.
Checks if command_line is syntactically valid.
All OS:
Unix:
g_shell_parse_argv()
Windows:
C:\Foo\Bar.exe
Foo Bar Qux
will not be considered "Foo Bar.exe" Qux
or "Foo Bar Qux.exe"
, depending on what executables exist, as Windows normally does.If execute is TRUE, also checks, using g_find_program_in_path()
, if the program specified in command_line exists and is executable.
command_line | the command line to check. |
execute | whether to check if the command line is really executable. |
error | return location for error. |
TRUE
on success, FALSE
on error.Definition at line 246 of file spawn.c.
References _, error(), and spawn_get_program_name().
Referenced by cc_dialog_update_row_status().
|
static |
Definition at line 876 of file spawn.c.
References _SpawnChannelData::channel, NULL, SPAWN_CHANNEL_GIO_WATCH, and spawn_destroy_common().
Referenced by spawn_with_callbacks().
|
static |
Definition at line 854 of file spawn.c.
References _SpawnChannelData::buffer, _SpawnChannelData::channel, _SpawnChannelData::line_buffer, and NULL.
Referenced by spawn_destroy_cb(), and spawn_timeout_destroy_cb().
|
static |
Definition at line 1049 of file spawn.c.
References _SpawnWatcherData::exit_cb, _SpawnWatcherData::exit_data, _SpawnWatcherData::exit_status, _SpawnWatcherData::main_loop, and _SpawnWatcherData::pid.
Referenced by spawn_timeout_watch_cb(), and spawn_watch_cb().
|
static |
Definition at line 1325 of file spawn.c.
Referenced by spawn_sync().
|
static |
Definition at line 115 of file spawn.c.
References _, error(), NULL, and spawn_parse_argv().
Referenced by spawn_async_with_pipes(), and spawn_check_command().
gboolean spawn_kill_process | ( | GPid | pid, |
GError ** | error | ||
) |
Kills a process.
pid | id of the process to kill. |
error | return location for error. |
On Unix, sends a SIGTERM to the process.
On Windows, terminates the process with exit code 255 (used sometimes as "generic" error code, or for programs terminated with Ctrl+C / Ctrl+Break).
TRUE
on success, FALSE
on error.Definition at line 289 of file spawn.c.
References errno, and error().
Referenced by kill_process().
|
static |
Definition at line 86 of file spawn.c.
References _, error(), and NULL.
Referenced by spawn_async_with_pipes(), and spawn_get_program_name().
|
static |
Definition at line 909 of file spawn.c.
References _SpawnChannelData::buffer, _SpawnChannelData::cb, _SpawnChannelData::cb_data, _SpawnChannelData::channel, DEFAULT_IO_LENGTH, _SpawnChannelData::empty_gio_ins, geany_debug(), _SpawnChannelData::line_buffer, _SpawnChannelData::max_length, NULL, _SpawnChannelData::read, SPAWN_CHANNEL_GIO_WATCH, SPAWN_IO_FAILURE, spawn_timeout_destroy_cb(), and spawn_timeout_read_cb().
Referenced by spawn_timeout_read_cb(), and spawn_with_callbacks().
gboolean spawn_sync | ( | const gchar * | working_directory, |
const gchar * | command_line, | ||
gchar ** | argv, | ||
gchar ** | envp, | ||
SpawnWriteData * | stdin_data, | ||
GString * | stdout_data, | ||
GString * | stderr_data, | ||
gint * | exit_status, | ||
GError ** | error | ||
) |
Executes a child synchronously.
A command line or an argument vector must be passed. If both are present, the argument vector is appended to the command line. An empty command line is not allowed.
The stdin_data is sent to the child with spawn_write_data()
.
All output from the child, including the nul characters, is stored in stdout_data and stderr_data (if non-NULL). Any existing data in these strings will be erased.
working_directory | child's current working directory, or NULL . |
command_line | child program and arguments, or NULL . |
argv | child's argument vector, or NULL . |
envp | child's environment, or NULL . |
stdin_data | data to send to childs's stdin, or NULL . |
stdout_data | GString location to receive the child's stdout, or NULL . |
stderr_data | GString location to receive the child's stderr, or NULL . |
exit_status | (out) return location for the child exit code, or NULL . |
error | return location for error. |
TRUE
on success, FALSE
on error.Definition at line 1357 of file spawn.c.
References error(), NULL, spawn_append_gstring_cb(), spawn_get_exit_status_cb(), SPAWN_SYNC, SPAWN_UNBUFFERED, spawn_with_callbacks(), and spawn_write_data().
Referenced by run_command(), tools_execute_custom_command(), and utils_spawn_sync().
|
static |
Definition at line 866 of file spawn.c.
References _SpawnChannelData::channel, NULL, and spawn_destroy_common().
Referenced by spawn_read_cb().
|
static |
Definition at line 901 of file spawn.c.
References _SpawnChannelData::channel, and spawn_read_cb().
Referenced by spawn_read_cb().
|
static |
Definition at line 1065 of file spawn.c.
References _SpawnChannelData::channel, _SpawnWatcherData::sc, and spawn_finalize().
Referenced by spawn_watch_cb().
|
static |
Definition at line 1079 of file spawn.c.
References _SpawnChannelData::channel, _SpawnWatcherData::exit_status, _SpawnWatcherData::main_context, NULL, _SpawnWatcherData::pid, _SpawnWatcherData::sc, spawn_finalize(), and spawn_timeout_watch_cb().
Referenced by spawn_with_callbacks().
gboolean spawn_with_callbacks | ( | const gchar * | working_directory, |
const gchar * | command_line, | ||
gchar ** | argv, | ||
gchar ** | envp, | ||
SpawnFlags | spawn_flags, | ||
GIOFunc | stdin_cb, | ||
gpointer | stdin_data, | ||
SpawnReadFunc | stdout_cb, | ||
gpointer | stdout_data, | ||
gsize | stdout_max_length, | ||
SpawnReadFunc | stderr_cb, | ||
gpointer | stderr_data, | ||
gsize | stderr_max_length, | ||
GChildWatchFunc | exit_cb, | ||
gpointer | exit_data, | ||
GPid * | child_pid, | ||
GError ** | error | ||
) |
Executes a child program and setups callbacks.
A command line or an argument vector must be passed. If both are present, the argument vector is appended to the command line. An empty command line is not allowed.
The synchronous execution may not be combined with recursive callbacks.
In line buffered mode, the child input is broken on \n
, \r\n
, \r
, \0
and max length.
All I/O callbacks are guaranteed to be invoked at least once with G_IO_ERR
, G_IO_HUP
or G_IO_NVAL
set (except for a stdin_cb which returns FALSE
before that). For the non-recursive callbacks, this is guaranteed to be the last call, and may be used to free any resources associated with the callback.
The stdin_cb may write to channel
only once per invocation, only if G_IO_OUT
is set, and only a non-zero number of characters.
stdout_cb
and stderr_cb
may modify the received strings in any way, but must not free them.
The default max lengths are 24K for line buffered stdout, 8K for line buffered stderr, 4K for unbuffered input under Unix, and 2K for unbuffered input under Windows.
Due to a bug in some glib versions, the sources for recursive stdout/stderr callbacks may be converted from child watch to timeout(50ms). No callback changes are required.
exit_cb
is always invoked last, after all I/O callbacks.
The child_pid will be closed automatically, after exit_cb is invoked.
working_directory | child's current working directory, or NULL . |
command_line | child program and arguments, or NULL . |
argv | child's argument vector, or NULL . |
envp | child's environment, or NULL . |
spawn_flags | flags from SpawnFlags. |
stdin_cb | callback to send data to childs's stdin, or NULL . |
stdin_data | data to pass to stdin_cb. |
stdout_cb | callback to receive child's stdout, or NULL . |
stdout_data | data to pass to stdout_cb. |
stdout_max_length | maximum data length to pass to stdout_cb, 0 = default. |
stderr_cb | callback to receive child's stderr, or NULL . |
stderr_data | data to pass to stderr_cb. |
stderr_max_length | maximum data length to pass to stderr_cb, 0 = default. |
exit_cb | callback to invoke when the child exits, or NULL . |
exit_data | data to pass to exit_cb. |
child_pid | (out) return location for child process ID, or NULL . |
error | return location for error. |
TRUE
on success, FALSE
on error.Definition at line 1158 of file spawn.c.
References _SpawnChannelData::buffer, _SpawnChannelData::cb, _SpawnChannelData::cb_data, _SpawnChannelData::channel, DEFAULT_IO_LENGTH, _SpawnChannelData::empty_gio_ins, error(), _SpawnWatcherData::exit_cb, _SpawnWatcherData::exit_data, _SpawnChannelData::line_buffer, _SpawnWatcherData::main_context, _SpawnWatcherData::main_loop, _SpawnChannelData::max_length, NULL, _SpawnChannelData::read, _SpawnWatcherData::sc, spawn_async_with_pipes(), spawn_destroy_cb(), SPAWN_IO_FAILURE, spawn_read_cb(), SPAWN_RECURSIVE, SPAWN_STDIN_RECURSIVE, SPAWN_STDOUT_UNBUFFERED, SPAWN_SYNC, spawn_watch_cb(), spawn_write_cb(), and _SpawnChannelData::write.
Referenced by build_spawn_cmd(), search_find_in_files(), and spawn_sync().
|
static |
Definition at line 888 of file spawn.c.
References _SpawnChannelData::cb, _SpawnChannelData::cb_data, SPAWN_IO_FAILURE, and _SpawnChannelData::write.
Referenced by spawn_with_callbacks().
gboolean spawn_write_data | ( | GIOChannel * | channel, |
GIOCondition | condition, | ||
SpawnWriteData * | data | ||
) |
Writes (a portion of) the data pointed by data->ptr to the channel.
If G_IO_OUT
in condition is set, and the data->size is > 0, attempts to write data->ptr (or a portion of it, depending on the size) to the channel. On success, increases ptr and decreases size with the number of characters written.
This function may converted to GIOFunc
and passed to spawn_with_callbacks()
as stdin_cb
, together with a SpawnWriteData
for stdin_data
. As with any other callback data, make sure that stdin_data
exists while the child is being executed. (For example, on asynchronous execution, you can allocate the data in the heap, and free it in your spawn_with_callbacks()
exit_cb
callback.)
channel | the channel to write data to. |
condition | condition to check for G_IO_OUT . |
data | SpawnWriteData to write to channel. |
TRUE
if the remaining size is > 0 and condition does not indicate any error, FALSE
otherwise.Definition at line 1297 of file spawn.c.
References DEFAULT_IO_LENGTH, NULL, _SpawnWriteData::ptr, _SpawnWriteData::size, and SPAWN_IO_FAILURE.
Referenced by spawn_sync().