Added simple command line option parsing to uzbl_tabbed.py
[uzbl-mobile] / uzbl.c
diff --git a/uzbl.c b/uzbl.c
index 96777ef..11162bb 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
@@ -54,6 +54,9 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <poll.h>
+#include <sys/uio.h>
+#include <sys/ioctl.h>
 #include "uzbl.h"
 #include "config.h"
 
@@ -64,13 +67,13 @@ const
 GOptionEntry entries[] =
 {
     { "uri",      'u', 0, G_OPTION_ARG_STRING, &uzbl.state.uri,
-        "Uri to load at startup (equivalent to 'set uri = URI')", "URI" },
+        "Uri to load at startup (equivalent to 'uzbl <uri>' or 'set uri = URI' after uzbl has launched)", "URI" },
     { "verbose",  'v', 0, G_OPTION_ARG_NONE,   &uzbl.state.verbose,
         "Whether to print all messages or just errors.", NULL },
     { "name",     'n', 0, G_OPTION_ARG_STRING, &uzbl.state.instance_name,
         "Name of the current instance (defaults to Xorg window id)", "NAME" },
     { "config",   'c', 0, G_OPTION_ARG_STRING, &uzbl.state.config_file,
-        "Config file (this is pretty much equivalent to uzbl < FILE )", "FILE" },
+        "Path to config file or '-' for stdin", "FILE" },
     { "socket",   's', 0, G_OPTION_ARG_INT, &uzbl.state.socket_id,
         "Socket ID", "SOCKET" },
     { "geometry", 'g', 0, G_OPTION_ARG_STRING, &uzbl.gui.geometry,
@@ -153,7 +156,7 @@ const struct {
     { "sans_serif_font_family", PTR_V(uzbl.behave.sans_serif_font_family, STR,  1,   cmd_sans_serif_font_family)},
     { "serif_font_family",      PTR_V(uzbl.behave.serif_font_family,      STR,  1,   cmd_serif_font_family)},
     { "fantasy_font_family",    PTR_V(uzbl.behave.fantasy_font_family,    STR,  1,   cmd_fantasy_font_family)},
-               { "monospace_size",         PTR_V(uzbl.behave.monospace_size,         INT,  1,   cmd_font_size)},
+    { "monospace_size",         PTR_V(uzbl.behave.monospace_size,         INT,  1,   cmd_font_size)},
     { "minimum_font_size",      PTR_V(uzbl.behave.minimum_font_size,      INT,  1,   cmd_minimum_font_size)},
     { "disable_plugins",        PTR_V(uzbl.behave.disable_plugins,        INT,  1,   cmd_disable_plugins)},
     { "disable_scripts",        PTR_V(uzbl.behave.disable_scripts,        INT,  1,   cmd_disable_scripts)},
@@ -645,7 +648,7 @@ cmd_set_geometry() {
        the above setting and we don't want to end up with
        wrong geometry information
     */
-    retreive_geometry();
+    retrieve_geometry();
 }
 
 void
@@ -814,6 +817,7 @@ struct {char *key; CommandInfo value;} cmdlist[] =
     { "sync_spawn",         {spawn_sync, 0}                }, // needed for cookie handler
     { "sh",                 {spawn_sh, 0}                  },
     { "sync_sh",            {spawn_sh_sync, 0}             }, // needed for cookie handler
+    { "talk_to_socket",     {talk_to_socket, 0}            },
     { "exit",               {close_uzbl, 0}                },
     { "search",             {search_forward_text, TRUE}    },
     { "search_reverse",     {search_reverse_text, TRUE}    },
@@ -1400,6 +1404,118 @@ spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result) {
 }
 
 void
+talk_to_socket(WebKitWebView *web_view, GArray *argv, GString *result) {
+    (void)web_view; (void)result;
+
+    int fd, len;
+    struct sockaddr_un sa;
+    char* sockpath;
+    ssize_t ret;
+    struct pollfd pfd;
+    struct iovec* iov;
+    guint i;
+
+    if(uzbl.comm.sync_stdout) uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
+
+    /* This function could be optimised by storing a hash table of socket paths
+       and associated connected file descriptors rather than closing and
+       re-opening for every call. Also we could launch a script if socket connect
+       fails. */
+
+    /* First element argv[0] is path to socket. Following elements are tokens to
+       write to the socket. We write them as a single packet with each token
+       separated by an ASCII nul (\0). */
+    if(argv->len < 2) {
+        g_printerr("talk_to_socket called with only %d args (need at least two).\n",
+            (int)argv->len);
+        return;
+    }
+
+    /* copy socket path, null terminate result */
+    sockpath = g_array_index(argv, char*, 0);
+    g_strlcpy(sa.sun_path, sockpath, sizeof(sa.sun_path));
+    sa.sun_family = AF_UNIX;
+
+    /* create socket file descriptor and connect it to path */
+    fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+    if(fd == -1) {
+        g_printerr("talk_to_socket: creating socket failed (%s)\n", strerror(errno));
+        return;
+    }
+    if(connect(fd, (struct sockaddr*)&sa, sizeof(sa))) {
+        g_printerr("talk_to_socket: connect failed (%s)\n", strerror(errno));
+        close(fd);
+        return;
+    }
+
+    /* build request vector */
+    iov = g_malloc(sizeof(struct iovec) * (argv->len - 1));
+    if(!iov) {
+        g_printerr("talk_to_socket: unable to allocated memory for token vector\n");
+        close(fd);
+        return;
+    }
+    for(i = 1; i < argv->len; ++i) {
+        iov[i - 1].iov_base = g_array_index(argv, char*, i);
+        iov[i - 1].iov_len = strlen(iov[i - 1].iov_base) + 1; /* string plus \0 */
+    }
+
+    /* write request */
+    ret = writev(fd, iov, argv->len - 1);
+    g_free(iov);
+    if(ret == -1) {
+        g_printerr("talk_to_socket: write failed (%s)\n", strerror(errno));
+        close(fd);
+        return;
+    }
+
+    /* wait for a response, with a 500ms timeout */
+    pfd.fd = fd;
+    pfd.events = POLLIN;
+    while(1) {
+        ret = poll(&pfd, 1, 500);
+        if(ret == 1) break;
+        if(ret == 0) errno = ETIMEDOUT;
+        if(errno == EINTR) continue;
+        g_printerr("talk_to_socket: poll failed while waiting for input (%s)\n",
+            strerror(errno));
+        close(fd);
+        return;
+    }
+
+    /* get length of response */
+    if(ioctl(fd, FIONREAD, &len) == -1) {
+        g_printerr("talk_to_socket: cannot find daemon response length, "
+            "ioctl failed (%s)\n", strerror(errno));
+        close(fd);
+        return;
+    }
+
+    /* if there is a response, read it */
+    if(len) {
+        uzbl.comm.sync_stdout = g_malloc(len + 1);
+        if(!uzbl.comm.sync_stdout) {
+            g_printerr("talk_to_socket: failed to allocate %d bytes\n", len);
+            close(fd);
+            return;
+        }
+        uzbl.comm.sync_stdout[len] = 0; /* ensure result is null terminated */
+
+        ret = read(fd, uzbl.comm.sync_stdout, len);
+        if(ret == -1) {
+            g_printerr("talk_to_socket: failed to read from socket (%s)\n",
+                strerror(errno));
+            close(fd);
+            return;
+        }
+    }
+
+    /* clean up */
+    close(fd);
+    return;
+}
+
+void
 parse_command(const char *cmd, const char *param, GString *result) {
     CommandInfo *c;
 
@@ -2075,7 +2191,7 @@ configure_event_cb(GtkWidget* window, GdkEventConfigure* event) {
     (void) window;
     (void) event;
 
-    retreive_geometry();
+    retrieve_geometry();
     return FALSE;
 }
 
@@ -2210,16 +2326,11 @@ exec_paramcmd(const Action *act, const guint i) {
 }
 
 
-GtkWidget*
+void
 create_browser () {
     GUI *g = &uzbl.gui;
 
-    GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-    //main_window_ref = g_object_ref(scrolled_window);
-    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_NEVER); //todo: some sort of display of position/total length. like what emacs does
-
     g->web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
-    gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (g->web_view));
 
     g_signal_connect (G_OBJECT (g->web_view), "notify::title", G_CALLBACK (title_change_cb), NULL);
     g_signal_connect (G_OBJECT (g->web_view), "load-progress-changed", G_CALLBACK (progress_change_cb), g->web_view);
@@ -2232,8 +2343,6 @@ create_browser () {
     g_signal_connect (G_OBJECT (g->web_view), "download-requested", G_CALLBACK (download_cb), g->web_view);
     g_signal_connect (G_OBJECT (g->web_view), "create-web-view", G_CALLBACK (create_web_view_cb), g->web_view);
     g_signal_connect (G_OBJECT (g->web_view), "mime-type-policy-decision-requested", G_CALLBACK (mime_policy_cb), g->web_view);
-
-    return scrolled_window;
 }
 
 GtkWidget*
@@ -2300,7 +2409,8 @@ inject_handler_args(const gchar *actname, const gchar *origargs, const gchar *ne
     if ((g_strcmp0(actname, "spawn") == 0) ||
         (g_strcmp0(actname, "sh") == 0) ||
         (g_strcmp0(actname, "sync_spawn") == 0) ||
-        (g_strcmp0(actname, "sync_sh") == 0)) {
+        (g_strcmp0(actname, "sync_sh") == 0) ||
+        (g_strcmp0(actname, "talk_to_socket") == 0)) {
         guint i;
         GString *a = g_string_new("");
         gchar **spawnparts = split_quoted(origargs, FALSE);
@@ -2651,7 +2761,7 @@ dump_config() {
 }
 
 void
-retreive_geometry() {
+retrieve_geometry() {
     int w, h, x, y;
     GString *buf = g_string_new("");
 
@@ -2669,7 +2779,6 @@ retreive_geometry() {
  * external applications need to do anyhow */
 void
 initialize(int argc, char *argv[]) {
-    gtk_init (&argc, &argv);
     if (!g_thread_supported ())
         g_thread_init (NULL);
     uzbl.state.executable_path = g_strdup(argv[0]);
@@ -2723,7 +2832,7 @@ initialize(int argc, char *argv[]) {
     commands_hash ();
     make_var_to_name_hash();
 
-    uzbl.gui.scrolled_win = create_browser();
+    create_browser();
 }
 
 #ifndef UZBL_LIBRARY
@@ -2732,6 +2841,16 @@ int
 main (int argc, char* argv[]) {
     initialize(argc, argv);
 
+    gtk_init (&argc, &argv);
+
+    uzbl.gui.scrolled_win = gtk_scrolled_window_new (NULL, NULL);
+    //main_window_ref = g_object_ref(scrolled_window);
+    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (uzbl.gui.scrolled_win),
+        GTK_POLICY_NEVER, GTK_POLICY_NEVER); //todo: some sort of display of position/total length. like what emacs does
+
+    gtk_container_add (GTK_CONTAINER (uzbl.gui.scrolled_win),
+        GTK_WIDGET (uzbl.gui.web_view));
+
     uzbl.gui.vbox = gtk_vbox_new (FALSE, 0);
 
     create_mainbar();
@@ -2772,10 +2891,13 @@ main (int argc, char* argv[]) {
     uzbl.gui.bar_h = gtk_range_get_adjustment((GtkRange*) uzbl.gui.scbar_h);
     gtk_widget_set_scroll_adjustments ((GtkWidget*) uzbl.gui.web_view, uzbl.gui.bar_h, uzbl.gui.bar_v);
 
-    if(uzbl.gui.geometry)
-        cmd_set_geometry();
-    else
-        retreive_geometry();
+    /* Check uzbl is in window mode before getting/setting geometry */
+    if (uzbl.gui.main_window) {
+        if(uzbl.gui.geometry)
+            cmd_set_geometry();
+        else
+            retrieve_geometry();
+    }
 
     gchar *uri_override = (uzbl.state.uri ? g_strdup(uzbl.state.uri) : NULL);
     if (argc > 1 && !uzbl.state.uri)