fixed failing to load argument to --uri
[uzbl-mobile] / uzbl.c
diff --git a/uzbl.c b/uzbl.c
index 899a923..43178e9 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
@@ -149,7 +149,14 @@ itos(int val) {
 
 static char *
 str_replace (const char* search, const char* replace, const char* string) {
-    return g_strjoinv (replace, g_strsplit (string, search, -1));
+    gchar **buf;
+    char *ret;
+
+    buf = g_strsplit (string, search, -1);
+    ret = g_strjoinv (replace, buf);
+    g_strfreev(buf);
+
+    return ret;
 }
 
 static sigfunc*
@@ -453,7 +460,7 @@ new_action(const gchar *name, const gchar *param) {
 
 static bool
 file_exists (const char * filename) {
-       return (access(filename, F_OK) == 0);
+    return (access(filename, F_OK) == 0);
 }
 
 static void
@@ -471,7 +478,7 @@ load_uri (WebKitWebView * web_view, const gchar *param) {
         GString* newuri = g_string_new (param);
         if (g_strrstr (param, "://") == NULL)
             g_string_prepend (newuri, "http://");
-               /* if we do handle cookies, ask our handler for them */
+        /* if we do handle cookies, ask our handler for them */
         webkit_web_view_load_uri (web_view, newuri->str);
         g_string_free (newuri, TRUE);
     }
@@ -626,9 +633,11 @@ expand_template(const char *template) {
              sym = (int)g_scanner_cur_value(uzbl.scan).v_symbol;
              switch(sym) {
                  case SYM_URI:
-                     g_string_append(ret,
-                         uzbl.state.uri?
-                         g_markup_printf_escaped("%s", uzbl.state.uri):"");
+                     buf = uzbl.state.uri?
+                         g_markup_printf_escaped("%s", uzbl.state.uri) :
+                         g_strdup("");
+                     g_string_append(ret, buf);
+                     free(buf);
                      break;
                  case SYM_LOADPRGS:
                      buf = itos(uzbl.gui.sbar.load_progress);
@@ -641,14 +650,18 @@ expand_template(const char *template) {
                      g_free(buf);
                      break;
                  case SYM_TITLE:
-                     g_string_append(ret,
-                         uzbl.gui.main_title?
-                         g_markup_printf_escaped("%s", uzbl.gui.main_title):"");
+                     buf = uzbl.gui.main_title?
+                         g_markup_printf_escaped("%s", uzbl.gui.main_title) :
+                         g_strdup("");
+                     g_string_append(ret, buf);
+                     free(buf);
                      break;
                  case SYM_SELECTED_URI:
-                     g_string_append(ret,
-                         uzbl.state.selected_url?
-                         g_markup_printf_escaped("%s", uzbl.state.selected_url):"");
+                     buf = uzbl.state.selected_url?
+                         g_markup_printf_escaped("%s", uzbl.state.selected_url) :
+                         g_strdup("");
+                     g_string_append(ret, buf);
+                     free(buf);
                     break;
                  case SYM_NAME:
                      buf = itos(uzbl.xwin);
@@ -657,9 +670,11 @@ expand_template(const char *template) {
                      free(buf);
                      break;
                  case SYM_KEYCMD:
-                     g_string_append(ret,
-                         uzbl.state.keycmd->str ?
-                         g_markup_printf_escaped("%s", uzbl.state.keycmd->str):"");
+                     buf = uzbl.state.keycmd->str?
+                         g_markup_printf_escaped("%s", uzbl.state.keycmd->str) :
+                         g_strdup("");
+                     g_string_append(ret, buf);
+                     free(buf);
                      break;
                  case SYM_MODE:
                      g_string_append(ret,
@@ -902,6 +917,7 @@ static gboolean
 set_var_value(gchar *name, gchar *val) {
     void **p = NULL;
     char *endp = NULL;
+    char *buf=NULL;
 
     if( (p = g_hash_table_lookup(uzbl.comm.proto_var, name)) ) {
         if(var_is("status_message", name)
@@ -930,11 +946,13 @@ set_var_value(gchar *name, gchar *val) {
         }
         else if(var_is("fifo_dir", name)) {
             if(*p) free(*p);
-            *p = init_fifo(g_strdup(val));
+            buf = init_fifo(val);
+            *p = buf?buf:g_strdup("");
         }
         else if(var_is("socket_dir", name)) {
             if(*p) free(*p);
-            *p = init_socket(g_strdup(val));
+            buf = init_socket(val);
+            *p = buf?buf:g_strdup("");
         }
         else if(var_is("modkey", name)) {
             if(*p) free(*p);
@@ -948,7 +966,8 @@ set_var_value(gchar *name, gchar *val) {
         }
         else if(var_is("useragent", name)) {
             if(*p) free(*p);
-            *p = set_useragent(g_strdup(val));
+            buf = set_useragent(val);
+            *p = buf?buf:g_strdup("");
         }
         else if(var_is("shell_cmd", name)) {
             if(*p) free(*p);
@@ -976,8 +995,6 @@ set_var_value(gchar *name, gchar *val) {
                              SOUP_SESSION_MAX_CONNS_PER_HOST, uzbl.net.max_conns_host, NULL);
             }
             else if (var_is("http_debug", name)) {
-                //soup_session_remove_feature
-                //    (uzbl.net.soup_session, uzbl.net.soup_logger);
                 soup_session_remove_feature
                     (uzbl.net.soup_session, SOUP_SESSION_FEATURE(uzbl.net.soup_logger));
                 /* do we leak if this doesn't get freed? why does it occasionally crash if freed? */
@@ -1132,7 +1149,6 @@ init_fifo(gchar *dir) { /* return dir or, on error, free dir and return NULL */
     }
 
     if (*dir == ' ') { /* space unsets the variable */
-        g_free(dir);
         return NULL;
     }
 
@@ -1158,7 +1174,6 @@ init_fifo(gchar *dir) { /* return dir or, on error, free dir and return NULL */
     /* if we got this far, there was an error; cleanup */
     if (error) g_error_free (error);
     g_free(path);
-    g_free(dir);
     return NULL;
 }
 
@@ -1166,10 +1181,9 @@ static gboolean
 control_stdin(GIOChannel *gio, GIOCondition condition) {
     (void) condition;
     gchar *ctl_line = NULL;
-    gsize ctl_line_len = 0;
     GIOStatus ret;
 
-    ret = g_io_channel_read_line(gio, &ctl_line, &ctl_line_len, NULL, NULL);
+    ret = g_io_channel_read_line(gio, &ctl_line, NULL, NULL, NULL);
     if ( (ret == G_IO_STATUS_ERROR) || (ret == G_IO_STATUS_EOF) )
         return FALSE;
 
@@ -1507,9 +1521,6 @@ add_binding (const gchar *key, const gchar *act) {
         printf ("Binding %-10s : %s\n", key, act);
 
     action = new_action(parts[0], parts[1]);
-    /* TODO: create hash table with valid destroy functions 
-     *       so we do not leak in this place
-     */
     g_hash_table_replace(uzbl.bindings, g_strdup(key), action);
 
     g_strfreev(parts);
@@ -1520,16 +1531,15 @@ get_xdg_var (XDG_Var xdg) {
     const gchar* actual_value = getenv (xdg.environmental);
     const gchar* home         = getenv ("HOME");
 
-    gchar* return_value = str_replace ("~", home, g_strdup (actual_value));
+    gchar* return_value = str_replace ("~", home, actual_value);
 
     if (! actual_value || strcmp (actual_value, "") == 0) {
         if (xdg.default_value) {
-            return_value = str_replace ("~", home, g_strdup (xdg.default_value));
+            return_value = str_replace ("~", home, xdg.default_value);
         } else {
             return_value = NULL;
         }
     }
-
     return return_value;
 }
 
@@ -1539,17 +1549,21 @@ find_xdg_file (int xdg_type, char* filename) {
        xdg_type = 1 => data
        xdg_type = 2 => cache*/
 
-    gchar* temporary_file   = (char *)malloc (1024);
+    gchar* temporary_file   = malloc (1024);
     gchar* temporary_string = NULL;
     char*  saveptr;
+    char*  buf;
 
-    strcpy (temporary_file, get_xdg_var (XDG[xdg_type]));
-
+    buf = get_xdg_var (XDG[xdg_type]);
+    strcpy (temporary_file, buf);
     strcat (temporary_file, filename);
+    free(buf);
 
     if (! file_exists (temporary_file) && xdg_type != 2) {
-        temporary_string = (char *) strtok_r (get_xdg_var (XDG[3 + xdg_type]), ":", &saveptr);
-        
+        buf = get_xdg_var (XDG[3 + xdg_type]);
+        temporary_string = (char *) strtok_r (buf, ":", &saveptr);
+        free(buf);
+
         while (temporary_string && ! file_exists (temporary_file)) {
             strcpy (temporary_file, temporary_string);
             strcat (temporary_file, filename);
@@ -1572,7 +1586,7 @@ settings_init () {
     uzbl.behave.reset_command_mode = 1;
 
     if (!s->config_file) {
-        s->config_file = g_strdup (find_xdg_file (0, "/uzbl/config"));
+        s->config_file = find_xdg_file (0, "/uzbl/config");
     }
 
     if (s->config_file) {
@@ -1669,6 +1683,7 @@ main (int argc, char* argv[]) {
     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, NULL);
+    g_option_context_free(context);
     /* initialize hash table */
     uzbl.bindings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_action);
 
@@ -1701,7 +1716,6 @@ main (int argc, char* argv[]) {
     uzbl.gui.main_window = create_window ();
     gtk_container_add (GTK_CONTAINER (uzbl.gui.main_window), uzbl.gui.vbox);
 
-    load_uri (uzbl.gui.web_view, uzbl.state.uri); //TODO: is this needed?
 
     gtk_widget_grab_focus (GTK_WIDGET (uzbl.gui.web_view));
     gtk_widget_show_all (uzbl.gui.main_window);
@@ -1729,6 +1743,10 @@ main (int argc, char* argv[]) {
 
     create_stdin();
 
+    if(uzbl.state.uri)
+        load_uri (uzbl.gui.web_view, uzbl.state.uri);
+
+
     gtk_main ();
     clean_up();