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*
/* -- 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
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);
}
}
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;
}
}
}
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
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
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);
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);
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);