Merge commit 'rob/master' into experimental
[uzbl-mobile] / uzbl.c
diff --git a/uzbl.c b/uzbl.c
index 78760b0..bb11fcf 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
@@ -190,48 +190,44 @@ make_var_to_name_hash() {
 /* --- UTILITY FUNCTIONS --- */
 static gchar *
 expand_vars(char *s) {
-    char ret[256],  /* 256 chars per var name should be safe */
-         *vend;
     uzbl_cmdprop *c;
     char upto = ' ';
+    char ret[256],  *vend;
     GString *buf = g_string_new("");
 
     while(*s) {
-        /* found quotation char */
-        if(*s == '\\') {
-            g_string_append_c(buf, *++s);
-            s++;
-        }
-        /* found variable */
-        else if(*s == '@') {
-            if(*(s+1) == '{') {
-                upto = '}'; s++;
-            }
-            s++;
-            if( (vend = strchr(s, upto)) ||
-                (vend = strchr(s, '\0')) ) {
-                strncpy(ret, s, vend-s);
-                ret[vend-s] = '\0';
-                if( (c = g_hash_table_lookup(uzbl.comm.proto_var, ret)) ) {
-                    if(c->type == TYPE_STR)
-                        g_string_append(buf, (gchar *)*c->ptr);
-                    else if(c->type == TYPE_INT) {
-                        char *b = itos((int)*c->ptr);
-                        g_string_append(buf, b);
-                        g_free(b);
+        switch(*s) {
+            case '\\':
+                g_string_append_c(buf, *++s);
+                s++;
+                break;
+            case '@':
+                if(*(s+1) == '{') {
+                    upto = '}'; s++;
+                }
+                s++;
+                if( (vend = strchr(s, upto)) ||
+                        (vend = strchr(s, '\0')) ) {
+                    strncpy(ret, s, vend-s);
+                    ret[vend-s] = '\0';
+                    if( (c = g_hash_table_lookup(uzbl.comm.proto_var, ret)) ) {
+                        if(c->type == TYPE_STR)
+                            g_string_append(buf, (gchar *)*c->ptr);
+                        else if(c->type == TYPE_INT) {
+                            char *b = itos((int)*c->ptr);
+                            g_string_append(buf, b);
+                            g_free(b);
+                        }
                     }
+                    if(upto == ' ') s = vend;
+                    else s = vend+1;
+                    upto = ' ';
                 }
-                if(upto == ' ')
-                    s = vend;
-                else
-                    s = vend+1;
-                upto = ' ';
-            }
-        }
-        /* every other char */
-        else {
-            g_string_append_c(buf, *s);
-            s++;
+                break;
+            default:
+                g_string_append_c(buf, *s);
+                s++;
+                break;
         }
     }
     return g_string_free(buf, FALSE);
@@ -614,13 +610,14 @@ static struct {char *name; Command command[2];} cmdlist[] =
     { "dehilight",          {dehilight, 0}                 },
     { "toggle_insert_mode", {toggle_insert_mode, 0}        },
     { "set",                {set_var, NOSPLIT}             },
-    { "get",                {get_var, NOSPLIT}             },
+    //{ "get",                {get_var, NOSPLIT}             },
     { "bind",               {act_bind, NOSPLIT}            },
     { "dump_config",        {act_dump_config, 0}           },
     { "keycmd",             {keycmd, NOSPLIT}              },
     { "keycmd_nl",          {keycmd_nl, NOSPLIT}           },
     { "keycmd_bs",          {keycmd_bs, 0}                 },
-    { "chain",              {chain, 0}                     }
+    { "chain",              {chain, 0}                     },
+    { "print",              {print, NOSPLIT}               }
 };
 
 static void
@@ -673,9 +670,13 @@ set_var(WebKitWebView *page, GArray *argv) {
 }
 
 static void
-get_var(WebKitWebView *page, GArray *argv) {
+print(WebKitWebView *page, GArray *argv) {
     (void) page;
-    get_var_value(argv_idx(argv, 0));
+    gchar* buf;
+
+    buf = expand_vars(argv_idx(argv, 0));
+    puts(buf);
+    g_free(buf);
 }
 
 static void
@@ -1135,6 +1136,9 @@ run_command (const gchar *command, const guint npre, const gchar **args,
         g_string_append_printf(s, " -- result: %s", (result ? "true" : "false"));
         printf("%s\n", s->str);
         g_string_free(s, TRUE);
+        if(stdout) {
+            printf("Stdout: %s\n", *stdout);
+        }
     }
     if (err) {
         g_printerr("error on run_command: %s\n", err->message);
@@ -1267,19 +1271,6 @@ parse_command(const char *cmd, const char *param) {
         g_printerr ("command \"%s\" not understood. ignoring.\n", cmd);
 }
 
-static gboolean
-get_var_value(const gchar *name) {
-    uzbl_cmdprop *c;
-
-    if( (c = g_hash_table_lookup(uzbl.comm.proto_var, name)) ) {
-        if(c->type == TYPE_STR)
-            printf("VAR: %s VALUE: (%s)\n", name, (char *)*c->ptr);
-        else if(c->type == TYPE_INT)
-            printf("VAR: %s VALUE: %d\n", name, (int)*c->ptr);
-    }
-    return TRUE;
-}
-
 static void
 set_proxy_url() {
     SoupURI *suri;
@@ -1587,9 +1578,10 @@ parse_cmd_line(const char *ctl_line) {
     else { /* parse a command */
         gchar *ctlstrip;
         gchar **tokens = NULL;
+        len = strlen(ctl_line);
 
-        if (ctl_line[strlen(ctl_line) - 1] == '\n') /* strip trailing newline */
-            ctlstrip = g_strndup(ctl_line, strlen(ctl_line) - 1);
+        if (ctl_line[len - 1] == '\n') /* strip trailing newline */
+            ctlstrip = g_strndup(ctl_line, len - 1);
         else ctlstrip = g_strdup(ctl_line);
 
         tokens = g_strsplit(ctlstrip, " ", 2);
@@ -2224,7 +2216,8 @@ settings_init () {
 static void handle_cookies (SoupSession *session, SoupMessage *msg, gpointer user_data){
     (void) session;
     (void) user_data;
-    if (!uzbl.behave.cookie_handler) return;
+    if (!uzbl.behave.cookie_handler)
+         return;
 
     soup_message_add_header_handler(msg, "got-headers", "Set-Cookie", G_CALLBACK(save_cookies), NULL);
     GString *s = g_string_new ("");
@@ -2232,10 +2225,13 @@ static void handle_cookies (SoupSession *session, SoupMessage *msg, gpointer use
     g_string_printf(s, "GET '%s' '%s'", soup_uri->host, soup_uri->path);
     run_handler(uzbl.behave.cookie_handler, s->str);
 
-    if(uzbl.comm.sync_stdout)
-        soup_message_headers_replace (msg->request_headers, "Cookie", uzbl.comm.sync_stdout);
-    //printf("stdout: %s\n", uzbl.comm.sync_stdout);   // debugging
-    if (uzbl.comm.sync_stdout) uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
+    if(uzbl.comm.sync_stdout && strcmp (uzbl.comm.sync_stdout, "") != 0) {
+        char *p = strchr(uzbl.comm.sync_stdout, '\n' );
+        if ( p != NULL ) *p = '\0';
+        soup_message_headers_replace (msg->request_headers, "Cookie", (const char *) uzbl.comm.sync_stdout);
+    }
+    if (uzbl.comm.sync_stdout)
+        uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);
         
     g_string_free(s, TRUE);
 }