Working print command with uzblctrl
authorDequis <dx@dxzone.com.ar>
Mon, 15 Jun 2009 06:31:58 +0000 (03:31 -0300)
committerDequis <dx@dxzone.com.ar>
Mon, 15 Jun 2009 06:49:46 +0000 (03:49 -0300)
Also: Changed a few g_error to g_warning to avoid crashes on broken
socket. Flush the channel after sending, shutdown before closing.
Removed unused headers from uzblctrl.c.

The socket protocol is straightforward: you send a command terminated
with a newline, you receive a reply terminated with a newline.
If you ever need newlines inside a reply, you'll have to change this.

uzblctrl closes the connection after getting the reply, but any other
client can stay connected and send/receive other commands.

uzbl.c
uzblctrl.c

diff --git a/uzbl.c b/uzbl.c
index e953d3d..407070e 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
@@ -706,7 +706,7 @@ print(WebKitWebView *page, GArray *argv, GString *result) {
     gchar* buf;
 
     buf = expand_vars(argv_idx(argv, 0));
-    puts(buf); /*TODO: result?*/
+    g_string_assign(result, buf);
     g_free(buf);
 }
 
@@ -1313,8 +1313,8 @@ parse_command(const char *cmd, const char *param, GString *result) {
                 GString *result_print = g_string_new("");
 
                 c->function(uzbl.gui.web_view, a, result_print);
-                if (uzbl.state.verbose)
-                    printf("%s returned %s\n", cmd, result_print->str);
+                if (result_print->len)
+                    printf("%*s\n", result_print->len, result_print->str);
 
                 g_string_free(result_print, TRUE);
             } else {
@@ -1817,22 +1817,27 @@ control_client_socket(GIOChannel *clientchan) {
 
     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);
+        g_warning ("Error reading: %s\n", error->message);
+        g_io_channel_shutdown(clientchan, TRUE, &error);
         return FALSE;
     } else if (ret == G_IO_STATUS_EOF) {
-        /* socket closed, remove channel watch from main loop */
+        /* shutdown and remove channel watch from main loop */
+        g_io_channel_shutdown(clientchan, TRUE, &error);
         return FALSE;
     }
 
     if (ctl_line) {
         parse_cmd_line (ctl_line, result);
+        g_string_append_c(result, '\n');
         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_warning ("Error writing: %s", error->message);
         }
+        g_io_channel_flush(clientchan, &error);
     }
 
+    if (error) g_error_free (error);
     g_string_free(result, TRUE);
     g_free(ctl_line);
     return TRUE;
index f0fe732..a4fcf1d 100644 (file)
@@ -2,17 +2,9 @@
 /* Socket code more or less completely copied from here: http://www.ecst.csuchico.edu/~beej/guide/ipc/usock.html */
 
 #include <gtk/gtk.h>
-#include <gdk/gdkx.h>
-#include <gdk/gdkkeysyms.h>
-#include <webkit/webkit.h>
-#include <pthread.h>
 #include <stdio.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
 #include <unistd.h>
 #include <stdlib.h>
-#include <errno.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -40,6 +32,7 @@ main(int argc, char* argv[]) {
     if (sockpath && command) {
         int s, len;
         struct sockaddr_un remote;
+        char tmp;
         
         if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
             perror ("socket");
@@ -55,11 +48,18 @@ main(int argc, char* argv[]) {
             exit (1);
         }
         
-        if (send (s, command, strlen (command), 0) == -1) {
+        if ((send (s, command, strlen (command), 0) == -1) ||
+            (send (s, "\n", 1, 0) == -1)) {
             perror ("send");
             exit (1);
         }
         
+        while ((len = recv (s, &tmp, 1, 0))) {
+            putchar(tmp);
+            if (tmp == '\n')
+                break;
+        }
+
         close(s);
         
         return 0;