Fix control_socket to use GIOChannel
authorDequis <dx@dxzone.com.ar>
Mon, 15 Jun 2009 04:09:40 +0000 (01:09 -0300)
committerDequis <dx@dxzone.com.ar>
Mon, 15 Jun 2009 04:09:40 +0000 (01:09 -0300)
uzbl.c
uzbl.h

diff --git a/uzbl.c b/uzbl.c
index 1bb1e41..e953d3d 100644 (file)
--- 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 (file)
--- 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);