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
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

spawn.c File Reference
#include <errno.h>
#include <string.h>
#include "spawn.h"
#include <signal.h>
#include "support.h"
#include "geany.h"
Include dependency graph for spawn.c:

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...
 

Macro Definition Documentation

◆ DEFAULT_IO_LENGTH

#define DEFAULT_IO_LENGTH   4096

Definition at line 82 of file spawn.c.

◆ G_SPAWN_ERROR_TOO_BIG

#define G_SPAWN_ERROR_TOO_BIG   G_SPAWN_ERROR_2BIG

Definition at line 74 of file spawn.c.

◆ SPAWN_CHANNEL_GIO_WATCH

#define SPAWN_CHANNEL_GIO_WATCH (   sc)    ((sc)->empty_gio_ins < 200)

Definition at line 851 of file spawn.c.

◆ SPAWN_IO_FAILURE

#define SPAWN_IO_FAILURE   (G_IO_ERR | G_IO_HUP | G_IO_NVAL) /* always used together */

Definition at line 102 of file spawn.c.

Typedef Documentation

◆ SpawnChannelData

◆ SpawnWatcherData

Function Documentation

◆ spawn_append_gstring_cb()

static void spawn_append_gstring_cb ( GString *  string,
GIOCondition  condition,
gpointer  data 
)
static

Definition at line 1318 of file spawn.c.

Referenced by spawn_sync().

◆ spawn_async()

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().

Parameters
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.
errorreturn location for error.
Returns
TRUE on success, FALSE on error.
Since
1.25

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().

◆ spawn_async_with_pipes()

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 
)
static

◆ spawn_check_command()

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:

  • any leading spaces, tabs and new lines are skipped
  • an empty command is invalid

Unix:

  • the standard shell quoting and escaping rules are used, see g_shell_parse_argv()
  • as a consequence, an unqouted # at the start of an argument comments to the end of line

Windows:

  • leading carriage returns are skipped too
  • a quoted program name must be entirely inside the quotes. No "C:\Foo\Bar".pdf or "C:\Foo\Bar".bat, which would be executed by Windows as C:\Foo\Bar.exe
  • an unquoted program name may not contain spaces. 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.
  • the program name must be separated from the arguments by at least one space or tab
  • the standard Windows quoting and escaping rules are used: double quote is escaped with backslash, and any literal backslashes before a double quote must be duplicated.

If execute is TRUE, also checks, using g_find_program_in_path(), if the program specified in command_line exists and is executable.

Parameters
command_linethe command line to check.
executewhether to check if the command line is really executable.
errorreturn location for error.
Returns
TRUE on success, FALSE on error.
Since
1.25

Definition at line 246 of file spawn.c.

References _, error(), and spawn_get_program_name().

Referenced by cc_dialog_update_row_status().

◆ spawn_destroy_cb()

static void spawn_destroy_cb ( gpointer  data)
static

◆ spawn_destroy_common()

static void spawn_destroy_common ( SpawnChannelData sc)
static

◆ spawn_finalize()

◆ spawn_get_exit_status_cb()

static void spawn_get_exit_status_cb ( G_GNUC_UNUSED GPid  pid,
gint  status,
gpointer  exit_status 
)
static

Definition at line 1325 of file spawn.c.

Referenced by spawn_sync().

◆ spawn_get_program_name()

static gchar * spawn_get_program_name ( const gchar *  command_line,
GError **  error 
)
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().

◆ spawn_kill_process()

gboolean spawn_kill_process ( GPid  pid,
GError **  error 
)

Kills a process.

Parameters
pidid of the process to kill.
errorreturn 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).

Returns
TRUE on success, FALSE on error.
Since
1.25

Definition at line 289 of file spawn.c.

References errno, and error().

Referenced by kill_process().

◆ spawn_parse_argv()

static gboolean spawn_parse_argv ( const gchar *  command_line,
gint *  argcp,
gchar ***  argvp,
GError **  error 
)
static

Definition at line 86 of file spawn.c.

References _, error(), and NULL.

Referenced by spawn_async_with_pipes(), and spawn_get_program_name().

◆ spawn_read_cb()

◆ spawn_sync()

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.

Parameters
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.
errorreturn location for error.
Returns
TRUE on success, FALSE on error.
Since
1.25

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().

◆ spawn_timeout_destroy_cb()

static void spawn_timeout_destroy_cb ( gpointer  data)
static

Definition at line 866 of file spawn.c.

References _SpawnChannelData::channel, NULL, and spawn_destroy_common().

Referenced by spawn_read_cb().

◆ spawn_timeout_read_cb()

static gboolean spawn_timeout_read_cb ( gpointer  data)
static

Definition at line 901 of file spawn.c.

References _SpawnChannelData::channel, and spawn_read_cb().

Referenced by spawn_read_cb().

◆ spawn_timeout_watch_cb()

static gboolean spawn_timeout_watch_cb ( gpointer  data)
static

Definition at line 1065 of file spawn.c.

References _SpawnChannelData::channel, _SpawnWatcherData::sc, and spawn_finalize().

Referenced by spawn_watch_cb().

◆ spawn_watch_cb()

static void spawn_watch_cb ( GPid  pid,
gint  status,
gpointer  data 
)
static

◆ 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.

Parameters
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_flagsflags from SpawnFlags.
stdin_cb callback to send data to childs's stdin, or NULL.
stdin_datadata to pass to stdin_cb.
stdout_cb callback to receive child's stdout, or NULL.
stdout_datadata to pass to stdout_cb.
stdout_max_lengthmaximum data length to pass to stdout_cb, 0 = default.
stderr_cb callback to receive child's stderr, or NULL.
stderr_datadata to pass to stderr_cb.
stderr_max_lengthmaximum data length to pass to stderr_cb, 0 = default.
exit_cb callback to invoke when the child exits, or NULL.
exit_datadata to pass to exit_cb.
child_pid (out) return location for child process ID, or NULL.
errorreturn location for error.
Returns
TRUE on success, FALSE on error.
Since
1.25

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().

◆ spawn_write_cb()

static gboolean spawn_write_cb ( GIOChannel *  channel,
GIOCondition  condition,
gpointer  data 
)
static

◆ spawn_write_data()

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.)

Parameters
channelthe channel to write data to.
conditioncondition to check for G_IO_OUT.
dataSpawnWriteData to write to channel.
Returns
TRUE if the remaining size is > 0 and condition does not indicate any error, FALSE otherwise.
Since
1.25

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().