+
+int
+ly_run_command (char *argv[], char **standard_output, char **standard_error)
+{
+ GError *error = 0;
+ int exit_status = 0;
+ int flags = G_SPAWN_SEARCH_PATH;
+ if (!standard_output)
+ flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
+ if (!standard_error)
+ flags |= G_SPAWN_STDERR_TO_DEV_NULL;
+ if (!g_spawn_sync (0, argv, 0, GSpawnFlags (flags),
+ 0, 0,
+ standard_output, standard_error,
+ &exit_status, &error))
+ {
+ warning (_f ("g_spawn_sync failed (%d): %s: %s",
+ exit_status, argv[0], error->message));
+ g_error_free (error);
+ if (!exit_status)
+ exit_status = -1;
+ }
+
+ return exit_status;
+}
+
+static char *
+ly_scm2utf8 (SCM str)
+{
+ char *p = ly_scm2str0 (str);
+ char *g = g_locale_to_utf8 (p, -1, 0, 0, 0);
+ free (p);
+ return g;
+}
+
+LY_DEFINE (ly_spawn, "ly:spawn",
+ 1, 0, 1, (SCM command, SCM rest),
+ "Simple interface to g_spawn_sync"
+ " @var{str}."
+ " The error is formatted with @code{format} and @var{rest}.")
+
+{
+ LY_ASSERT_TYPE (scm_is_string, command, 1);
+
+ int argc = scm_is_pair (rest) ? scm_ilength (rest) : 0;
+ char **argv = new char*[argc + 2];
+
+ int n = 0;
+ argv[n++] = ly_scm2utf8 (command);
+ for (SCM s = rest; scm_is_pair (s); s = scm_cdr (s))
+ argv[n++] = ly_scm2utf8 (scm_car (s));
+ argv[n] = 0;
+
+ char *standard_output = 0;
+ char *standard_error = 0;
+ // Always get the pointer to the stdout/stderr messages
+ int exit_status = ly_run_command (argv, &standard_output, &standard_error);
+
+ if (standard_output && standard_error)
+ {
+ // Print out stdout and stderr only in debug mode
+ debug_output (string ("\n") + standard_output + standard_error, true);
+ }
+
+ g_free (standard_error);
+ g_free (standard_output);
+
+ for (int i = 0; i < n; i++)
+ free (argv[i]);
+ delete[] argv;
+
+ return scm_from_int (exit_status);
+}