From 8fe2a9c3c458ef7205254f25e06ca5aa28bcb0f2 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Tue, 4 Nov 2008 09:14:58 +0000 Subject: Tunnel stderr. --- src/main.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++------- src/sphinx.h | 1 + 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 124aa26..000b1b9 100644 --- a/src/main.c +++ b/src/main.c @@ -411,6 +411,8 @@ execute_command_dlg (GtkAction *action, sphinx_t *sphinx) gtk_widget_show (w); w = gtk_entry_new (); + if (sphinx->last_command) + gtk_entry_set_text (GTK_ENTRY (w), sphinx->last_command); gtk_entry_set_activates_default (GTK_ENTRY (w), TRUE); gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), w, FALSE, FALSE, 0); @@ -961,14 +963,43 @@ done: return ret; } +static gboolean +stderr_read (GIOChannel *io, GIOCondition cond) +{ + int fd; + + fd = g_io_channel_unix_get_fd (io); + do { + char buf[4096]; + int len; + + len = read (fd, buf, sizeof (buf)); + if (len <= 0) { + int err = len ? errno : 0; + switch (err) { + case EAGAIN: + case EINTR: + return TRUE; + default: + g_io_channel_unref (io); + return FALSE; + } + } + + buf[len] = '\0'; + g_printerr ("%s", buf); + } while (TRUE); +} + static gboolean sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error) { - gint output; + gint stdout_fd, stderr_fd; GtkTextIter start, stop; long flags; char **argv, **argv0; int argc; + GIOChannel *io; gchar *str; if (! g_shell_parse_argv (command, &argc, &argv, error)) @@ -981,10 +1012,9 @@ sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error) g_free (argv); if (! g_spawn_async_with_pipes (NULL, argv0, NULL, - G_SPAWN_SEARCH_PATH | - G_SPAWN_STDERR_TO_DEV_NULL, + G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, - NULL, &output, NULL, + NULL, &stdout_fd, &stderr_fd, error)) { g_strfreev (argv0); @@ -1002,6 +1032,9 @@ sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error) str); g_free (str); + g_free (sphinx->last_command); + sphinx->last_command = g_strdup (command); + g_free (sphinx->trace_name); sphinx->trace_name = g_strdup (command); @@ -1009,13 +1042,21 @@ sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error) gtk_text_buffer_delete (GTK_TEXT_BUFFER (sphinx->buffer), &start, &stop); gtk_source_buffer_remove_source_marks (sphinx->buffer, &start, &stop, NULL); - flags = fcntl (output, F_GETFL); + flags = fcntl (stdout_fd, F_GETFL); if ((flags & O_NONBLOCK) == 0) - fcntl (output, F_SETFL, flags | O_NONBLOCK); + fcntl (stdout_fd, F_SETFL, flags | O_NONBLOCK); - sphinx->trace_io = g_io_channel_unix_new (output); + sphinx->trace_io = g_io_channel_unix_new (stdout_fd); g_io_add_watch (sphinx->trace_io, G_IO_IN | G_IO_HUP, (GIOFunc) trace_read, sphinx); + + flags = fcntl (stderr_fd, F_GETFL); + if ((flags & O_NONBLOCK) == 0) + fcntl (stderr_fd, F_SETFL, flags | O_NONBLOCK); + + io = g_io_channel_unix_new (stderr_fd); + g_io_add_watch (io, G_IO_IN | G_IO_HUP, (GIOFunc) stderr_read, NULL); + return TRUE; } diff --git a/src/sphinx.h b/src/sphinx.h index 23c70b3..e6fa306 100644 --- a/src/sphinx.h +++ b/src/sphinx.h @@ -35,6 +35,7 @@ typedef struct _sphinx { cairo_script_interpreter_t *csi; gchar *trace_name; + gchar *last_command; GtkWidget *tv; GtkSourceBuffer *buffer; -- cgit v1.2.3