Fix comment, minor update.
[uzbl-mobile] / uzblctrl.c
index 5415247..e768b7f 100644 (file)
@@ -1,17 +1,10 @@
+/* -*- c-basic-offset: 4; -*- */
 /* 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>
@@ -22,7 +15,7 @@ static gchar* command;
 
 static GOptionEntry entries[] =
 {
-    { "socket",  's', 0, G_OPTION_ARG_STRING, &sockpath, "Socket path of the client uzbl", NULL },
+    { "socket",  's', 0, G_OPTION_ARG_STRING, &sockpath, "Path to the uzbl socket",        NULL },
     { "command", 'c', 0, G_OPTION_ARG_STRING, &command,  "The uzbl command to execute",    NULL },
     { NULL,       0,  0, 0,                    NULL,      NULL,                            NULL }
 };
@@ -30,34 +23,49 @@ static GOptionEntry entries[] =
 int
 main(int argc, char* argv[]) {
     GError *error = NULL;
-    GOptionContext* context = g_option_context_new ("- some stuff here maybe someday");
+    GOptionContext* context = g_option_context_new ("- utility for controlling and interacting with uzbl through its socket file"); /* TODO: get stuff back from uzbl */
     g_option_context_add_main_entries (context, entries, NULL);
     g_option_context_add_group        (context, gtk_get_option_group (TRUE));
     g_option_context_parse            (context, &argc, &argv, &error);
-    
-    int s, len;
-    struct sockaddr_un remote;
-    
-    if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
-        perror ("socket");
-        exit (1);
-    }
-    
-    remote.sun_family = AF_UNIX;
-    strcpy (remote.sun_path, (char *) sockpath);
-    len = strlen (remote.sun_path) + sizeof (remote.sun_family);
-
-    if (connect (s, (struct sockaddr *) &remote, len) == -1) {
-        perror ("connect");
-        exit (1);
-    }
-    
-    if (send (s, command, strlen (command), 0) == -1) {
-        perror ("send");
-        exit (1);
+
+
+    if (sockpath && command) {
+        int s, len;
+        struct sockaddr_un remote;
+        char tmp;
+
+        if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
+            perror ("socket");
+            exit (1);
+        }
+
+        remote.sun_family = AF_UNIX;
+        strcpy (remote.sun_path, (char *) sockpath);
+        len = strlen (remote.sun_path) + sizeof (remote.sun_family);
+
+        if (connect (s, (struct sockaddr *) &remote, len) == -1) {
+            perror ("connect");
+            exit (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;
+    } else {
+        fprintf(stderr, "Usage: uzblctrl -s /path/to/socket -c \"command\"");
+        return 1;
     }
-    
-    close(s);
-    
-    return 0;
 }
+/* vi: set et ts=4: */