Merge branch 'experimental' of git://github.com/DuClare/uzbl into experimental
authorBarrucadu <mike@barrucadu.co.uk>
Tue, 19 May 2009 13:19:39 +0000 (14:19 +0100)
committerBarrucadu <mike@barrucadu.co.uk>
Tue, 19 May 2009 13:19:39 +0000 (14:19 +0100)
Conflicts:
uzbl.c
uzbl.h

This breaks run_external_js() - segfault

1  2 
examples/configs/sampleconfig-dev
uzbl.c
uzbl.h

@@@ -103,12 -103,13 +103,13 @@@ bind    S         = js alert("hi")
  # you must enclose it in quotes.  Remember to escape (and double-escape) quotes and backslashes
  # in the body.  Any additional parameters you use will appear AFTER the default parameters (cfg file
  # path, fifo & socket dirs, etc.)
- bind    XS        = sh 'echo "act script alert (\'This is sent by the shell via a fifo\')" > "$4"'
+ bind    XS        = sh 'echo "act script alert (\\"This is sent by the shell via a fifo\\")" > "$4"'
  # Keyboard based link following: work in progress! No C DOM bindings yet, no click() event for hyperlinks so no referrer set..Quite basic but does the job for now...
  #hit F to toggle the Hints (now in form of link numbering)
 -bind    F= script for (var i=0; i < document.links.length; i++) {var uzblid = 'uzbl_link_hint_';var li = document.links[i];var pre = document.getElementById(uzblid+i);if (pre) {li.removeChild(pre);} else {var hint = document.createElement('div');hint.setAttribute('id',uzblid+i);hint.innerHTML = i;hint.style.display='inline';hint.style.lineHeight='90%';hint.style.backgroundColor='red';hint.style.color='white';hint.style.fontSize='small-xx';hint.style.fontWeight='light';hint.style.margin='0px';hint.style.padding='2px';hint.style.position='absolute';hint.style.textDecoration='none';hint.style.left=li.style.left;hint.style.top=li.style.top;li.insertAdjacentElement('afterBegin',hint);}}
 +bind    F= script ./examples/scripts/hint.js
  #hit f followed by linknumber and ENTER to follow that link
 -bind    f_        = script window.location = document.links[%s].href;
 +bind    f_        = js window.location = document.links[%s].href;
  
  # "home" page if you will
  set uri = uzbl.org
diff --cc uzbl.c
--- 1/uzbl.c
--- 2/uzbl.c
+++ b/uzbl.c
@@@ -149,59 -149,19 +149,62 @@@ itos(int val) 
      return g_strdup(tmp);
  }
  
+ static gchar*
+ argv_idx(const GArray *a, const guint idx) { return g_array_index(a, gchar*, idx); }
  static char *
  str_replace (const char* search, const char* replace, const char* string) {
 -    gchar **buf;
 -    char *ret;
 +    return g_strjoinv (replace, g_strsplit (string, search, -1));
 +}
 +
 +static gchar**
 +read_file_by_line (gchar *path) {
 +    GIOChannel *chan = NULL;
 +    gchar *readbuf = NULL;
 +    gsize len;
 +    gchar *lines[512];
 +    int i;
 +    
 +    chan = g_io_channel_new_file(path, "r", NULL);
 +    
 +    if (chan) {
 +        while (g_io_channel_read_line(chan, &readbuf, &len, NULL, NULL)
 +               == G_IO_STATUS_NORMAL) {
 +            lines[i] = g_strdup (readbuf);
 +            g_free (readbuf);
 +            i ++;
 +        }
 +        
 +        g_io_channel_unref (chan);
 +    } else {
 +        fprintf(stderr, "File '%s' not be read.\n", path);
 +    }
 +    
 +    lines[i] = NULL;
 +    return lines;
 +}
  
 -    buf = g_strsplit (string, search, -1);
 -    ret = g_strjoinv (replace, buf);
 -    g_strfreev(buf);
 +static
 +gchar* parseenv (const char* string) {
 +    extern char** environ;
 +    gchar* newstring = g_strdup (string);
 +    int i = 0;
  
 -    return ret;
 +    while (environ[i] != NULL) {
 +        gchar** env = g_strsplit (environ[i], "=", 0);
 +        gchar* envname = malloc (strlen (env[0]) + 1);
 +
 +        strcat (envname, "$");
 +        strcat (envname, env[0]);
 +
 +        newstring = str_replace(envname, env[1], newstring);
 +
 +        g_free (envname);
 +        //g_strfreev (env); - This still breaks uzbl, but shouldn't. The mystery thickens...
 +        i ++;
 +    }
 +
 +    return newstring;
  }
  
  static sigfunc*
@@@ -448,31 -410,30 +451,30 @@@ VIEWFUNC(go_forward
  
  /* -- command to callback/function map for things we cannot attach to any signals */
  // TODO: reload
--
- static struct {char *name; Command command;} cmdlist[] =
- {
-     { "back",               view_go_back            },
-     { "forward",            view_go_forward         },
-     { "scroll_vert",        scroll_vert             },
-     { "scroll_horz",        scroll_horz             },
-     { "scroll_begin",       scroll_begin            },
-     { "scroll_end",         scroll_end              },
-     { "reload",             view_reload,            },
-     { "reload_ign_cache",   view_reload_bypass_cache},
-     { "stop",               view_stop_loading,      },
-     { "zoom_in",            view_zoom_in,           }, //Can crash (when max zoom reached?).
-     { "zoom_out",           view_zoom_out,          },
-     { "uri",                load_uri                },
-     { "js",                 run_js                  },
-     { "script",             run_external_js         },
-     { "toggle_status",      toggle_status_cb        },
-     { "spawn",              spawn                   },
-     { "sh",                 spawn_sh                },
-     { "exit",               close_uzbl              },
-     { "search",             search_forward_text     },
-     { "search_reverse",     search_reverse_text     },
-     { "toggle_insert_mode", toggle_insert_mode      },
-     { "runcmd",             runcmd                  }
+ static struct {char *name; Command command[2];} cmdlist[] =
+ {   /* key                 function      no_split      */
+     { "back",             {view_go_back, 0}              },
+     { "forward",          {view_go_forward, 0}           },
+     { "scroll_vert",      {scroll_vert, 0}               },
+     { "scroll_horz",      {scroll_horz, 0}               },
+     { "scroll_begin",     {scroll_begin, 0}              },
+     { "scroll_end",       {scroll_end, 0}                },
+     { "reload",           {view_reload, 0},              },
+     { "reload_ign_cache", {view_reload_bypass_cache, 0}  },
+     { "stop",             {view_stop_loading, 0},        },
+     { "zoom_in",          {view_zoom_in, 0},             }, //Can crash (when max zoom reached?).
+     { "zoom_out",         {view_zoom_out, 0},            },
+     { "uri",              {load_uri, NOSPLIT}            },
 -    { "script",           {run_js, NOSPLIT}              },
++    { "js",               {run_js, NOSPLIT}              },
++    { "script",           {run_external_js, 0}           },
+     { "toggle_status",    {toggle_status_cb, 0}          },
+     { "spawn",            {spawn, 0}                     },
+     { "sh",               {spawn_sh, 0}                  },
+     { "exit",             {close_uzbl, 0}                },
+     { "search",           {search_forward_text, NOSPLIT} },
+     { "search_reverse",   {search_reverse_text, NOSPLIT} },
 -    { "insert_mode",      {set_insert_mode, 0}           },
++    { "insert_mode",      {toggle_insert_mode, 0}        },
+     { "runcmd",           {runcmd, NOSPLIT}              }
  };
  
  static void
@@@ -511,34 -472,25 +513,34 @@@ new_action(const gchar *name, const gch
  
  static bool
  file_exists (const char * filename) {
 -    return (access(filename, F_OK) == 0);
 +      return (access(filename, F_OK) == 0);
  }
  
- void
- toggle_insert_mode(WebKitWebView *page, const gchar *param) {
+ static void
 -set_insert_mode(WebKitWebView *page, GArray *argv) {
++toggle_insert_mode(WebKitWebView *page, GArray *argv) {
      (void)page;
-     (void)param;
+     (void)argv;
  
-     if (param != NULL) {
-         if (strcmp (param, "0") == 0) {
 -    uzbl.behave.insert_mode = TRUE;
++    if (argv_idx(argv, 0)) {
++        if (strcmp (argv_idx(argv, 0), "0") == 0) {
 +            uzbl.behave.insert_mode = FALSE;
 +        } else {
 +            uzbl.behave.insert_mode = TRUE;
 +        }
 +    } else {
 +        uzbl.behave.insert_mode = ! uzbl.behave.insert_mode;
 +    }
 +
      update_title();
  }
  
  static void
- load_uri (WebKitWebView * web_view, const gchar *param) {
-     if (param) {
-         GString* newuri = g_string_new (param);
-         if (g_strrstr (param, "://") == NULL)
+ load_uri (WebKitWebView *web_view, GArray *argv) {
+     if (argv_idx(argv, 0)) {
+         GString* newuri = g_string_new (argv_idx(argv, 0));
+         if (g_strrstr (argv_idx(argv, 0), "://") == 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);
      }
@@@ -551,60 -503,19 +553,59 @@@ run_js (WebKitWebView * web_view, GArra
  }
  
  static void
- run_external_js (WebKitWebView * web_view, const gchar *param) {
-     if (param) {
-         gchar** splitted = g_strsplit (param, " ", 2);
-         gchar** lines = read_file_by_line (splitted[0]);
++run_external_js (WebKitWebView * web_view, GArray *argv) {
++    if (argv_idx(argv, 0)) {
++        gchar** lines = read_file_by_line (argv_idx (argv, 0));
 +        gchar*  js = NULL;
 +        int i;
 +
 +        if (lines[0] != NULL) {
 +            for (i = 0; lines[i] != NULL; i ++) {
 +                if (js == NULL) {
 +                    js = g_strdup (lines[i]);
 +                } else {
 +                    gchar* newjs = g_strconcat (js, lines[i], NULL);
 +                    js = newjs;
 +                }
 +                //g_free (lines[i]); - Another mysterious breakage
 +            }
 +
 +            if (uzbl.state.verbose)
-                 printf ("External JavaScript file %s loaded\n", splitted[0]);
++                printf ("External JavaScript file %s loaded\n", argv_idx(argv, 0));
 +
-             if (splitted[1]) {
-                 gchar* newjs = str_replace("%s", splitted[1], js);
++            if (argv_idx (argv, 1)) {
++                gchar* newjs = str_replace("%s", argv_idx (argv, 1), js);
 +                js = newjs;
 +            }
 +            webkit_web_view_execute_script (web_view, js);
 +            g_free (js);
 +        } else {
-             fprintf(stderr, "JavaScript file '%s' not be read.\n", splitted[0]);
++            fprintf(stderr, "JavaScript file '%s' not be read.\n", argv_idx(argv, 0));
 +        }
 +    }
 +}
 +
 +static void
- search_text (WebKitWebView *page, const char *param, const gboolean forward) {
-     if ((param) && (param[0] != '\0')) {
-         strcpy(uzbl.state.searchtx, param);
-     }
-     if (uzbl.state.searchtx[0] != '\0') {
+ search_text (WebKitWebView *page, GArray *argv, const gboolean forward) {
+     if (argv_idx(argv, 0) && (*argv_idx(argv, 0) != '\0'))
+         uzbl.state.searchtx = g_strdup(argv_idx(argv, 0));
+     if (uzbl.state.searchtx != NULL) {
          if (uzbl.state.verbose)
              printf ("Searching: %s\n", uzbl.state.searchtx);
 -        webkit_web_view_unmark_text_matches (page);
 -        webkit_web_view_mark_text_matches (page, uzbl.state.searchtx, FALSE, 0);
 +
 +        if (g_strcmp0 (uzbl.state.searchtx, uzbl.state.searchold) != 0) {
 +            webkit_web_view_unmark_text_matches (page);
 +            webkit_web_view_mark_text_matches (page, uzbl.state.searchtx, FALSE, 0);
 +
 +            if (uzbl.state.searchold != NULL)
 +                g_free (uzbl.state.searchold);
 +
 +            uzbl.state.searchold = g_strdup (uzbl.state.searchtx);
 +        }
 +
          webkit_web_view_set_highlight_text_matches (page, TRUE);
          webkit_web_view_search_text (page, uzbl.state.searchtx, FALSE, forward, TRUE);
 -        g_free(uzbl.state.searchtx);
 -        uzbl.state.searchtx = NULL;
      }
  }
  
@@@ -935,12 -856,10 +938,10 @@@ split_quoted(const gchar* src, const gb
  }
  
  static void
- spawn(WebKitWebView *web_view, const char *param) {
+ spawn(WebKitWebView *web_view, GArray *argv) {
      (void)web_view;
      //TODO: allow more control over argument order so that users can have some arguments before the default ones from run_command, and some after
-     gchar **cmd = split_quoted(param, TRUE);
-     if (cmd) run_command(cmd[0], 0, &cmd[1], FALSE, NULL);
-     g_strfreev ((gchar**)cmd);
 -    if (argv_idx(argv, 0)) run_command(argv_idx(argv, 0), 0, argv->data + sizeof(gchar*), FALSE, NULL);
++    if (argv_idx(argv, 0)) run_command(argv_idx(argv, 0), 0, (const gchar **) argv->data + sizeof(gchar*), FALSE, NULL);
  }
  
  static void
@@@ -953,21 -872,15 +954,15 @@@ spawn_sh(WebKitWebView *web_view, GArra
      
      guint i;
      gchar *spacer = g_strdup("");
-     GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+     g_array_insert_val(argv, 1, spacer);
      gchar **cmd = split_quoted(uzbl.behave.shell_cmd, TRUE);
-     gchar **p = split_quoted(param, TRUE);
      for (i = 1; i < g_strv_length(cmd); i++)
-         sharg_append(a, cmd[i]);
-     sharg_append(a, p[0]); /* the first param comes right after shell_cmd;
-                               the rest come after default args */
-     sharg_append(a, spacer);
-     for (i = 1; i < g_strv_length(p); i++)
-         sharg_append(a, p[i]);
-     if (cmd) run_command(cmd[0], g_strv_length(cmd) + 1, a->data, FALSE, NULL);
+         g_array_prepend_val(argv, cmd[i]);
 -    if (cmd) run_command(cmd[0], g_strv_length(cmd) + 1, argv->data, FALSE, NULL);
++    if (cmd) run_command(cmd[0], g_strv_length(cmd) + 1, (const gchar **) argv->data, FALSE, NULL);
      g_free (spacer);
      g_strfreev (cmd);
-     g_strfreev (p);
-     g_array_free (a, FALSE);
  }
  
  static void
@@@ -1843,7 -1763,7 +1855,7 @@@ static void handle_cookies (SoupSessio
      sharg_append(a, action);
      sharg_append(a, soup_uri->host);
      sharg_append(a, soup_uri->path);
--    run_command(uzbl.behave.cookie_handler, 0, a->data, TRUE, &stdout); /* TODO: use handler */
++    run_command(uzbl.behave.cookie_handler, 0, (const gchar **) a->data, TRUE, &stdout); /* TODO: use handler */
      //run_handler(uzbl.behave.cookie_handler); /* TODO: global stdout pointer, spawn_sync */
      if(stdout) {
          soup_message_headers_replace (msg->request_headers, "Cookie", stdout);
@@@ -1866,7 -1786,7 +1878,7 @@@ save_cookies (SoupMessage *msg, gpointe
          sharg_append(a, soup_uri->host);
          sharg_append(a, soup_uri->path);
          sharg_append(a, cookie);
--        run_command(uzbl.behave.cookie_handler, 0, a->data, FALSE, NULL);
++        run_command(uzbl.behave.cookie_handler, 0, (const gchar **) a->data, FALSE, NULL);
          g_free (cookie);
          g_free (action);
          g_array_free(a, TRUE);
@@@ -1917,7 -1842,6 +1929,7 @@@ 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?
++    //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);
diff --cc uzbl.h
--- 1/uzbl.h
--- 2/uzbl.h
+++ b/uzbl.h
@@@ -104,12 -105,10 +105,12 @@@ typedef struct 
      gchar    *uri;
      gchar    *config_file;
      char    *instance_name;
 -    gchar    *selected_url;
 -    gchar    *executable_path;
 +    gchar    config_file_path[500];
 +    gchar    selected_url[500];
 +    char     executable_path[500];
      GString* keycmd;
-     gchar    searchtx[500];
 -    gchar    *searchtx;
++    gchar*   searchtx;
 +    gchar*   searchold;
      struct utsname unameinfo; /* system info */
      gboolean verbose;
  } State;
@@@ -269,11 -262,11 +270,11 @@@ new_action(const gchar *name, const gch
  static bool
  file_exists (const char * filename);
  
- void
- toggle_insert_mode(WebKitWebView *page, const gchar *param);
+ static void
 -set_insert_mode(WebKitWebView *page, GArray *argv);
++toggle_insert_mode(WebKitWebView *page, GArray *argv);
  
  static void
- load_uri (WebKitWebView * web_view, const gchar *param);
+ load_uri (WebKitWebView * web_view, GArray *argv);
  
  static void
  new_window_load_uri (const gchar * uri);
@@@ -361,20 -354,17 +362,20 @@@ static voi
  settings_init ();
  
  static void
- search_text (WebKitWebView *page, const char *param, const gboolean forward);
+ search_text (WebKitWebView *page, GArray *argv, const gboolean forward);
  
  static void
- search_forward_text (WebKitWebView *page, const char *param);
+ search_forward_text (WebKitWebView *page, GArray *argv);
  
  static void
- search_reverse_text (WebKitWebView *page, const char *param);
+ search_reverse_text (WebKitWebView *page, GArray *argv);
  
  static void
- run_js (WebKitWebView * web_view, const gchar *param);
+ run_js (WebKitWebView * web_view, GArray *argv);
  
 +static void
- run_external_js (WebKitWebView * web_view, const gchar *param);
++run_external_js (WebKitWebView * web_view, GArray *argv);
 +
  static void handle_cookies (SoupSession *session,
                                                        SoupMessage *msg,
                                                        gpointer     user_data);