From 50a2ba82d9a667b8e7ad700be399318a8349d338 Mon Sep 17 00:00:00 2001 From: Dequis Date: Mon, 15 Jun 2009 01:09:40 -0300 Subject: [PATCH] Fix control_socket to use GIOChannel --- uzbl.c | 73 +++++++++++++++++++++++++--------------------------------------- uzbl.h | 3 +++ 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/uzbl.c b/uzbl.c index 1bb1e41..e953d3d 100644 --- a/uzbl.c +++ b/uzbl.c @@ -1791,65 +1791,48 @@ create_stdin () { static gboolean control_socket(GIOChannel *chan) { - char *ctl_line; - GString *result = g_string_new(""); -#if 1 struct sockaddr_un remote; - char buffer[512]; - char temp[128]; - int sock, clientsock, n, done; - unsigned int t; - - sock = g_io_channel_unix_get_fd(chan); - - memset (buffer, 0, sizeof (buffer)); - - t = sizeof (remote); - clientsock = accept (sock, (struct sockaddr *) &remote, &t); - - done = 0; - do { - memset (temp, 0, sizeof (temp)); - n = recv (clientsock, temp, 128, 0); - if (n == 0) { - buffer[strlen (buffer)] = '\0'; - done = 1; - } - if (!done) - strcat (buffer, temp); - } while (!done); + unsigned int t = sizeof(remote); + int clientsock; + GIOChannel *clientchan; - if (strcmp (buffer, "\n") < 0) { - buffer[strlen (buffer) - 1] = '\0'; - } else { - buffer[strlen (buffer)] = '\0'; + clientsock = accept (g_io_channel_unix_get_fd(chan), + (struct sockaddr *) &remote, &t); + + if ((clientchan = g_io_channel_unix_new(clientsock))) { + g_io_add_watch(clientchan, G_IO_IN|G_IO_HUP, + (GIOFunc) control_client_socket, clientchan); } - ctl_line = g_strdup(buffer); - parse_cmd_line (ctl_line, result); - - send (clientsock, result->str, result->len, 0); - - close (clientsock); -#else - /* TODO: we should be able to do it with this. but glib errors out with "Invalid argument" */ + return TRUE; +} + +static gboolean +control_client_socket(GIOChannel *clientchan) { + char *ctl_line; + GString *result = g_string_new(""); GError *error = NULL; - gsize len; GIOStatus ret; + gsize len; - ret = g_io_channel_read_line(chan, &ctl_line, &len, NULL, &error); - if (ret == G_IO_STATUS_ERROR) + ret = g_io_channel_read_line(clientchan, &ctl_line, &len, NULL, &error); + if (ret == G_IO_STATUS_ERROR) { g_error ("Error reading: %s\n", error->message); + return FALSE; + } else if (ret == G_IO_STATUS_EOF) { + /* socket closed, remove channel watch from main loop */ + return FALSE; + } - printf("Got line %s (%u bytes) \n",ctl_line, len); if (ctl_line) { parse_cmd_line (ctl_line, result); - ret = g_io_channel_write_chars (chan, result->str, result->len, &len, &error); + ret = g_io_channel_write_chars (clientchan, result->str, result->len, + &len, &error); if (ret == G_IO_STATUS_ERROR) { - g_error ("Error writing: %s", error->message) + g_error ("Error writing: %s", error->message); } } -#endif + g_string_free(result, TRUE); g_free(ctl_line); return TRUE; diff --git a/uzbl.h b/uzbl.h index 865b87e..21840f3 100644 --- a/uzbl.h +++ b/uzbl.h @@ -328,6 +328,9 @@ init_socket(gchar *dir); static gboolean control_socket(GIOChannel *chan); +static gboolean +control_client_socket(GIOChannel *chan); + static void update_title (void); -- 1.7.9.5