From fc09b334f196b5abd6c6c88f82597e5d6ac71d0e Mon Sep 17 00:00:00 2001 From: David Keijser Date: Tue, 24 May 2011 18:13:47 +0200 Subject: split commands from uzbl-core.c --- src/callbacks.c | 23 --- src/callbacks.h | 6 - src/commands.c | 442 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/commands.h | 66 +++++++++ src/uzbl-core.c | 442 +------------------------------------------------------- src/uzbl-core.h | 42 +----- 6 files changed, 523 insertions(+), 498 deletions(-) create mode 100644 src/commands.c create mode 100644 src/commands.h (limited to 'src') diff --git a/src/callbacks.c b/src/callbacks.c index 548cf95..a996248 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -322,29 +322,6 @@ cmd_set_zoom_type () { webkit_web_view_set_full_content_zoom (uzbl.gui.web_view, FALSE); } -void -toggle_zoom_type (WebKitWebView* page, GArray *argv, GString *result) { - (void)argv; - (void)result; - - webkit_web_view_set_full_content_zoom (page, !webkit_web_view_get_full_content_zoom (page)); -} - -void -toggle_status_cb (WebKitWebView* page, GArray *argv, GString *result) { - (void)page; - (void)argv; - (void)result; - - if (uzbl.behave.show_status) { - gtk_widget_hide(uzbl.gui.mainbar); - } else { - gtk_widget_show(uzbl.gui.mainbar); - } - uzbl.behave.show_status = !uzbl.behave.show_status; - update_title(); -} - void link_hover_cb (WebKitWebView *page, const gchar *title, const gchar *link, gpointer data) { (void) page; (void) title; (void) data; diff --git a/src/callbacks.h b/src/callbacks.h index 87f9785..2b333a3 100644 --- a/src/callbacks.h +++ b/src/callbacks.h @@ -136,12 +136,6 @@ cmd_scrollbars_visibility(); void cmd_load_start(); -void -toggle_zoom_type (WebKitWebView* page, GArray *argv, GString *result); - -void -toggle_status_cb (WebKitWebView* page, GArray *argv, GString *result); - void link_hover_cb (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data); diff --git a/src/commands.c b/src/commands.c new file mode 100644 index 0000000..28a3b39 --- /dev/null +++ b/src/commands.c @@ -0,0 +1,442 @@ +#include "commands.h" +#include "uzbl-core.h" +#include "events.h" +#include "util.h" +#include "menu.h" +#include "callbacks.h" + +/* -- command to callback/function map for things we cannot attach to any signals */ +CommandInfo cmdlist[] = +{ /* key function no_split */ + { "back", view_go_back, 0 }, + { "forward", view_go_forward, 0 }, + { "scroll", scroll_cmd, 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 }, + { "toggle_zoom_type", toggle_zoom_type, 0 }, + { "uri", load_uri, TRUE }, + { "js", run_js, TRUE }, + { "script", run_external_js, 0 }, + { "toggle_status", toggle_status, 0 }, + { "spawn", spawn_async, 0 }, + { "sync_spawn", spawn_sync, 0 }, + { "sync_spawn_exec", spawn_sync_exec, 0 }, // needed for load_cookies.sh :( + { "sh", spawn_sh_async, 0 }, + { "sync_sh", spawn_sh_sync, 0 }, + { "exit", close_uzbl, 0 }, + { "search", search_forward_text, TRUE }, + { "search_reverse", search_reverse_text, TRUE }, + { "search_clear", search_clear, TRUE }, + { "dehilight", dehilight, 0 }, + { "set", set_var, TRUE }, + { "dump_config", act_dump_config, 0 }, + { "dump_config_as_events", act_dump_config_as_events, 0 }, + { "chain", chain, 0 }, + { "print", print, TRUE }, + { "event", event, TRUE }, + { "request", event, TRUE }, + { "menu_add", menu_add, TRUE }, + { "menu_link_add", menu_add_link, TRUE }, + { "menu_image_add", menu_add_image, TRUE }, + { "menu_editable_add", menu_add_edit, TRUE }, + { "menu_separator", menu_add_separator, TRUE }, + { "menu_link_separator", menu_add_separator_link, TRUE }, + { "menu_image_separator", menu_add_separator_image, TRUE }, + { "menu_editable_separator", menu_add_separator_edit, TRUE }, + { "menu_remove", menu_remove, TRUE }, + { "menu_link_remove", menu_remove_link, TRUE }, + { "menu_image_remove", menu_remove_image, TRUE }, + { "menu_editable_remove", menu_remove_edit, TRUE }, + { "hardcopy", hardcopy, TRUE }, + { "include", include, TRUE }, + { "show_inspector", show_inspector, 0 }, + { "add_cookie", add_cookie, 0 }, + { "delete_cookie", delete_cookie, 0 }, + { "clear_cookies", clear_cookies, 0 }, + { "download", download, 0 } +}; + +void +commands_hash() { + unsigned int i; + uzbl.behave.commands = g_hash_table_new(g_str_hash, g_str_equal); + + for (i = 0; i < LENGTH(cmdlist); i++) + g_hash_table_insert(uzbl.behave.commands, (gpointer) cmdlist[i].key, &cmdlist[i]); +} + +void +builtins() { + unsigned int i; + unsigned int len = LENGTH(cmdlist); + GString* command_list = g_string_new(""); + + for (i = 0; i < len; i++) { + g_string_append(command_list, cmdlist[i].key); + g_string_append_c(command_list, ' '); + } + + send_event(BUILTINS, NULL, TYPE_STR, command_list->str, NULL); + g_string_free(command_list, TRUE); +} + +/* VIEW funcs (little webkit wrappers) */ +#define VIEWFUNC(name) void view_##name(WebKitWebView *page, GArray *argv, GString *result){(void)argv; (void)result; webkit_web_view_##name(page);} +VIEWFUNC(reload) +VIEWFUNC(reload_bypass_cache) +VIEWFUNC(stop_loading) +VIEWFUNC(zoom_in) +VIEWFUNC(zoom_out) +VIEWFUNC(go_back) +VIEWFUNC(go_forward) +#undef VIEWFUNC + +void +toggle_zoom_type (WebKitWebView* page, GArray *argv, GString *result) { + (void)argv; (void)result; + webkit_web_view_set_full_content_zoom (page, !webkit_web_view_get_full_content_zoom (page)); +} + +void +toggle_status (WebKitWebView* page, GArray *argv, GString *result) { + (void)page; (void)argv; (void)result; + + if (uzbl.behave.show_status) { + gtk_widget_hide(uzbl.gui.mainbar); + } else { + gtk_widget_show(uzbl.gui.mainbar); + } + uzbl.behave.show_status = !uzbl.behave.show_status; + update_title(); +} + +/* + * scroll vertical 20 + * scroll vertical 20% + * scroll vertical -40 + * scroll vertical begin + * scroll vertical end + * scroll horizontal 10 + * scroll horizontal -500 + * scroll horizontal begin + * scroll horizontal end + */ +void +scroll_cmd(WebKitWebView* page, GArray *argv, GString *result) { + (void) page; (void) result; + gchar *direction = g_array_index(argv, gchar*, 0); + gchar *argv1 = g_array_index(argv, gchar*, 1); + GtkAdjustment *bar = NULL; + + if (g_strcmp0(direction, "horizontal") == 0) + bar = uzbl.gui.bar_h; + else if (g_strcmp0(direction, "vertical") == 0) + bar = uzbl.gui.bar_v; + else { + if(uzbl.state.verbose) + puts("Unrecognized scroll format"); + return; + } + + if (g_strcmp0(argv1, "begin") == 0) + gtk_adjustment_set_value(bar, gtk_adjustment_get_lower(bar)); + else if (g_strcmp0(argv1, "end") == 0) + gtk_adjustment_set_value (bar, gtk_adjustment_get_upper(bar) - + gtk_adjustment_get_page_size(bar)); + else + scroll(bar, argv1); +} + +void +set_var(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) result; + + if(!argv_idx(argv, 0)) + return; + + gchar **split = g_strsplit(argv_idx(argv, 0), "=", 2); + if (split[0] != NULL) { + gchar *value = split[1] ? g_strchug(split[1]) : " "; + set_var_value(g_strstrip(split[0]), value); + } + g_strfreev(split); +} + + +void +event(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) result; + GString *event_name; + gchar **split = NULL; + + if(!argv_idx(argv, 0)) + return; + + split = g_strsplit(argv_idx(argv, 0), " ", 2); + if(split[0]) + event_name = g_string_ascii_up(g_string_new(split[0])); + else + return; + + send_event(0, event_name->str, TYPE_FORMATTEDSTR, split[1] ? split[1] : "", NULL); + + g_string_free(event_name, TRUE); + g_strfreev(split); +} + +void +print(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) result; + gchar* buf; + + if(!result) + return; + + buf = expand(argv_idx(argv, 0), 0); + g_string_assign(result, buf); + g_free(buf); +} + +void +hardcopy(WebKitWebView *page, GArray *argv, GString *result) { + (void) argv; (void) result; + webkit_web_frame_print(webkit_web_view_get_main_frame(page)); +} + +void +include(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) result; + gchar *path = argv_idx(argv, 0); + + if(!path) + return; + + if((path = find_existing_file(path))) { + run_command_file(path); + send_event(FILE_INCLUDED, NULL, TYPE_STR, path, NULL); + g_free(path); + } +} + +void +show_inspector(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) argv; (void) result; + + webkit_web_inspector_show(uzbl.gui.inspector); +} + +void +add_cookie(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) result; + gchar *host, *path, *name, *value; + gboolean secure = 0; + SoupDate *expires = NULL; + + if(argv->len != 6) + return; + + // Parse with same syntax as ADD_COOKIE event + host = argv_idx (argv, 0); + path = argv_idx (argv, 1); + name = argv_idx (argv, 2); + value = argv_idx (argv, 3); + secure = strcmp (argv_idx (argv, 4), "https") == 0; + if (strlen (argv_idx (argv, 5)) != 0) + expires = soup_date_new_from_time_t ( + strtoul (argv_idx (argv, 5), NULL, 10)); + + // Create new cookie + SoupCookie * cookie = soup_cookie_new (name, value, host, path, -1); + soup_cookie_set_secure (cookie, secure); + if (expires) + soup_cookie_set_expires (cookie, expires); + + // Add cookie to jar + uzbl.net.soup_cookie_jar->in_manual_add = 1; + soup_cookie_jar_add_cookie (SOUP_COOKIE_JAR (uzbl.net.soup_cookie_jar), cookie); + uzbl.net.soup_cookie_jar->in_manual_add = 0; +} + +void +delete_cookie(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) result; + + if(argv->len < 4) + return; + + SoupCookie * cookie = soup_cookie_new ( + argv_idx (argv, 2), + argv_idx (argv, 3), + argv_idx (argv, 0), + argv_idx (argv, 1), + 0); + + uzbl.net.soup_cookie_jar->in_manual_add = 1; + soup_cookie_jar_delete_cookie (SOUP_COOKIE_JAR (uzbl.net.soup_cookie_jar), cookie); + uzbl.net.soup_cookie_jar->in_manual_add = 0; +} + +void +clear_cookies(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; (void) argv; (void) result; + + // Replace the current cookie jar with a new empty jar + soup_session_remove_feature (uzbl.net.soup_session, + SOUP_SESSION_FEATURE (uzbl.net.soup_cookie_jar)); + g_object_unref (G_OBJECT (uzbl.net.soup_cookie_jar)); + uzbl.net.soup_cookie_jar = uzbl_cookie_jar_new (); + soup_session_add_feature(uzbl.net.soup_session, + SOUP_SESSION_FEATURE (uzbl.net.soup_cookie_jar)); +} + +void +download(WebKitWebView *web_view, GArray *argv, GString *result) { + (void) result; + + const gchar *uri = argv_idx(argv, 0); + const gchar *destination = NULL; + if(argv->len > 1) + destination = argv_idx(argv, 1); + + WebKitNetworkRequest *req = webkit_network_request_new(uri); + WebKitDownload *download = webkit_download_new(req); + + download_cb(web_view, download, (gpointer)destination); + + if(webkit_download_get_destination_uri(download)) + webkit_download_start(download); + else + g_object_unref(download); + + g_object_unref(req); +} + +void +load_uri(WebKitWebView *web_view, GArray *argv, GString *result) { + (void) web_view; (void) result; + gchar * uri = argv_idx(argv, 0); + set_var_value("uri", uri ? uri : ""); +} + +void +run_js (WebKitWebView * web_view, GArray *argv, GString *result) { + if (argv_idx(argv, 0)) + eval_js(web_view, argv_idx(argv, 0), result, "(command)"); +} + +void +run_external_js (WebKitWebView * web_view, GArray *argv, GString *result) { + (void) result; + gchar *path = NULL; + + if (argv_idx(argv, 0) && + ((path = find_existing_file(argv_idx(argv, 0)))) ) { + gchar *file_contents = NULL; + + GIOChannel *chan = g_io_channel_new_file(path, "r", NULL); + if (chan) { + gsize len; + g_io_channel_read_to_end(chan, &file_contents, &len, NULL); + g_io_channel_unref (chan); + } + + if (uzbl.state.verbose) + printf ("External JavaScript file %s loaded\n", argv_idx(argv, 0)); + + gchar *js = str_replace("%s", argv_idx (argv, 1) ? argv_idx (argv, 1) : "", file_contents); + g_free (file_contents); + + eval_js (web_view, js, result, path); + g_free (js); + g_free(path); + } +} + +void +search_clear(WebKitWebView *page, GArray *argv, GString *result) { + (void) argv; (void) result; + webkit_web_view_unmark_text_matches (page); + g_free(uzbl.state.searchtx); + uzbl.state.searchtx = NULL; +} + +void +search_forward_text (WebKitWebView *page, GArray *argv, GString *result) { + (void) result; + search_text(page, argv, TRUE); +} + +void +search_reverse_text(WebKitWebView *page, GArray *argv, GString *result) { + (void) result; + search_text(page, argv, FALSE); +} + +void +dehilight(WebKitWebView *page, GArray *argv, GString *result) { + (void) argv; (void) result; + webkit_web_view_set_highlight_text_matches (page, FALSE); +} + +void +chain(WebKitWebView *page, GArray *argv, GString *result) { + (void) page; + guint i = 0; + const gchar *cmd; + GString *r = g_string_new (""); + while ((cmd = argv_idx(argv, i++))) { + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + const CommandInfo *c = parse_command_parts(cmd, a); + if (c) + run_parsed_command(c, a, r); + g_array_free (a, TRUE); + } + if(result) + g_string_assign (result, r->str); + + g_string_free(r, TRUE); +} + +void +close_uzbl (WebKitWebView *page, GArray *argv, GString *result) { + (void)page; (void)argv; (void)result; + gtk_main_quit (); +} + +void +spawn_async(WebKitWebView *web_view, GArray *argv, GString *result) { + (void)web_view; (void)result; + spawn(argv, NULL, FALSE); +} + +void +spawn_sync(WebKitWebView *web_view, GArray *argv, GString *result) { + (void)web_view; + spawn(argv, result, FALSE); +} + +void +spawn_sync_exec(WebKitWebView *web_view, GArray *argv, GString *result) { + (void)web_view; + if(!result) { + GString *force_result = g_string_new(""); + spawn(argv, force_result, TRUE); + g_string_free (force_result, TRUE); + } else + spawn(argv, result, TRUE); +} + +void +spawn_sh_async(WebKitWebView *web_view, GArray *argv, GString *result) { + (void)web_view; (void)result; + spawn_sh(argv, NULL); +} + +void +spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result) { + (void)web_view; (void)result; + spawn_sh(argv, result); +} diff --git a/src/commands.h b/src/commands.h new file mode 100644 index 0000000..6cb10b5 --- /dev/null +++ b/src/commands.h @@ -0,0 +1,66 @@ +/* + * Uzbl Commands + */ +#ifndef __COMMANDS__ +#define __COMMANDS__ + +#include + +typedef void (*Command)(WebKitWebView*, GArray *argv, GString *result); + +typedef struct { + const gchar *key; + Command function; + gboolean no_split; +} CommandInfo; + +/** + * Initialises the hash table uzbl.behave.commands with the available commands. + */ +void +commands_hash(); + +/** + * Sends the BUILTINS events with the available commands. + */ +void +builtins(); + + +void view_reload(WebKitWebView *page, GArray *argv, GString *result); +void view_reload_bypass_cache(WebKitWebView *page, GArray *argv, GString *result); +void view_stop_loading(WebKitWebView *page, GArray *argv, GString *result); +void view_zoom_in(WebKitWebView *page, GArray *argv, GString *result); +void view_zoom_out(WebKitWebView *page, GArray *argv, GString *result); +void view_go_back(WebKitWebView *page, GArray *argv, GString *result); +void view_go_forward(WebKitWebView *page, GArray *argv, GString *result); +void toggle_zoom_type (WebKitWebView* page, GArray *argv, GString *result); +void scroll_cmd(WebKitWebView* page, GArray *argv, GString *result); +void print(WebKitWebView *page, GArray *argv, GString *result); +void event(WebKitWebView *page, GArray *argv, GString *result); +void load_uri(WebKitWebView * web_view, GArray *argv, GString *result); +void chain(WebKitWebView *page, GArray *argv, GString *result); +void close_uzbl(WebKitWebView *page, GArray *argv, GString *result); +void spawn_async(WebKitWebView *web_view, GArray *argv, GString *result); +void spawn_sh_async(WebKitWebView *web_view, GArray *argv, GString *result); +void spawn_sync(WebKitWebView *web_view, GArray *argv, GString *result); +void spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result); +void spawn_sync_exec(WebKitWebView *web_view, GArray *argv, GString *result); +void search_forward_text (WebKitWebView *page, GArray *argv, GString *result); +void search_reverse_text (WebKitWebView *page, GArray *argv, GString *result); +void search_clear(WebKitWebView *page, GArray *argv, GString *result); +void dehilight (WebKitWebView *page, GArray *argv, GString *result); +void hardcopy(WebKitWebView *page, GArray *argv, GString *result); +void include(WebKitWebView *page, GArray *argv, GString *result); +void show_inspector(WebKitWebView *page, GArray *argv, GString *result); +void add_cookie(WebKitWebView *page, GArray *argv, GString *result); +void delete_cookie(WebKitWebView *page, GArray *argv, GString *result); +void clear_cookies(WebKitWebView *pag, GArray *argv, GString *result); +void download(WebKitWebView *pag, GArray *argv, GString *result); +void set_var(WebKitWebView *page, GArray *argv, GString *result); +void run_js (WebKitWebView * web_view, GArray *argv, GString *result); +void run_external_js (WebKitWebView * web_view, GArray *argv, GString *result); +void toggle_zoom_type (WebKitWebView* page, GArray *argv, GString *result); +void toggle_status (WebKitWebView* page, GArray *argv, GString *result); + +#endif diff --git a/src/uzbl-core.c b/src/uzbl-core.c index 693f3ce..d670774 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -454,194 +454,8 @@ scroll(GtkAdjustment* bar, gchar *amount_str) { gtk_adjustment_set_value (bar, value); } -/* - * scroll vertical 20 - * scroll vertical 20% - * scroll vertical -40 - * scroll vertical begin - * scroll vertical end - * scroll horizontal 10 - * scroll horizontal -500 - * scroll horizontal begin - * scroll horizontal end - */ -void -scroll_cmd(WebKitWebView* page, GArray *argv, GString *result) { - (void) page; (void) result; - gchar *direction = g_array_index(argv, gchar*, 0); - gchar *argv1 = g_array_index(argv, gchar*, 1); - GtkAdjustment *bar = NULL; - - if (g_strcmp0(direction, "horizontal") == 0) - bar = uzbl.gui.bar_h; - else if (g_strcmp0(direction, "vertical") == 0) - bar = uzbl.gui.bar_v; - else { - if(uzbl.state.verbose) - puts("Unrecognized scroll format"); - return; - } - - if (g_strcmp0(argv1, "begin") == 0) - gtk_adjustment_set_value(bar, gtk_adjustment_get_lower(bar)); - else if (g_strcmp0(argv1, "end") == 0) - gtk_adjustment_set_value (bar, gtk_adjustment_get_upper(bar) - - gtk_adjustment_get_page_size(bar)); - else - scroll(bar, argv1); -} - - -/* VIEW funcs (little webkit wrappers) */ -#define VIEWFUNC(name) void view_##name(WebKitWebView *page, GArray *argv, GString *result){(void)argv; (void)result; webkit_web_view_##name(page);} -VIEWFUNC(reload) -VIEWFUNC(reload_bypass_cache) -VIEWFUNC(stop_loading) -VIEWFUNC(zoom_in) -VIEWFUNC(zoom_out) -VIEWFUNC(go_back) -VIEWFUNC(go_forward) -#undef VIEWFUNC - -/* -- command to callback/function map for things we cannot attach to any signals */ -CommandInfo cmdlist[] = -{ /* key function no_split */ - { "back", view_go_back, 0 }, - { "forward", view_go_forward, 0 }, - { "scroll", scroll_cmd, 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 }, - { "toggle_zoom_type", toggle_zoom_type, 0 }, - { "uri", load_uri, TRUE }, - { "js", run_js, TRUE }, - { "script", run_external_js, 0 }, - { "toggle_status", toggle_status_cb, 0 }, - { "spawn", spawn_async, 0 }, - { "sync_spawn", spawn_sync, 0 }, - { "sync_spawn_exec", spawn_sync_exec, 0 }, // needed for load_cookies.sh :( - { "sh", spawn_sh_async, 0 }, - { "sync_sh", spawn_sh_sync, 0 }, - { "exit", close_uzbl, 0 }, - { "search", search_forward_text, TRUE }, - { "search_reverse", search_reverse_text, TRUE }, - { "search_clear", search_clear, TRUE }, - { "dehilight", dehilight, 0 }, - { "set", set_var, TRUE }, - { "dump_config", act_dump_config, 0 }, - { "dump_config_as_events", act_dump_config_as_events, 0 }, - { "chain", chain, 0 }, - { "print", print, TRUE }, - { "event", event, TRUE }, - { "request", event, TRUE }, - { "menu_add", menu_add, TRUE }, - { "menu_link_add", menu_add_link, TRUE }, - { "menu_image_add", menu_add_image, TRUE }, - { "menu_editable_add", menu_add_edit, TRUE }, - { "menu_separator", menu_add_separator, TRUE }, - { "menu_link_separator", menu_add_separator_link, TRUE }, - { "menu_image_separator", menu_add_separator_image, TRUE }, - { "menu_editable_separator", menu_add_separator_edit, TRUE }, - { "menu_remove", menu_remove, TRUE }, - { "menu_link_remove", menu_remove_link, TRUE }, - { "menu_image_remove", menu_remove_image, TRUE }, - { "menu_editable_remove", menu_remove_edit, TRUE }, - { "hardcopy", hardcopy, TRUE }, - { "include", include, TRUE }, - { "show_inspector", show_inspector, 0 }, - { "add_cookie", add_cookie, 0 }, - { "delete_cookie", delete_cookie, 0 }, - { "clear_cookies", clear_cookies, 0 }, - { "download", download, 0 } -}; - -void -commands_hash(void) { - unsigned int i; - uzbl.behave.commands = g_hash_table_new(g_str_hash, g_str_equal); - - for (i = 0; i < LENGTH(cmdlist); i++) - g_hash_table_insert(uzbl.behave.commands, (gpointer) cmdlist[i].key, &cmdlist[i]); -} - - -void -builtins() { - unsigned int i; - unsigned int len = LENGTH(cmdlist); - GString* command_list = g_string_new(""); - - for (i = 0; i < len; i++) { - g_string_append(command_list, cmdlist[i].key); - g_string_append_c(command_list, ' '); - } - - send_event(BUILTINS, NULL, TYPE_STR, command_list->str, NULL); - g_string_free(command_list, TRUE); -} - /* -- CORE FUNCTIONS -- */ -void -set_var(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) result; - - if(!argv_idx(argv, 0)) - return; - - gchar **split = g_strsplit(argv_idx(argv, 0), "=", 2); - if (split[0] != NULL) { - gchar *value = split[1] ? g_strchug(split[1]) : " "; - set_var_value(g_strstrip(split[0]), value); - } - g_strfreev(split); -} - - -void -event(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) result; - GString *event_name; - gchar **split = NULL; - - if(!argv_idx(argv, 0)) - return; - - split = g_strsplit(argv_idx(argv, 0), " ", 2); - if(split[0]) - event_name = g_string_ascii_up(g_string_new(split[0])); - else - return; - - send_event(0, event_name->str, TYPE_FORMATTEDSTR, split[1] ? split[1] : "", NULL); - - g_string_free(event_name, TRUE); - g_strfreev(split); -} - -void -print(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) result; - gchar* buf; - - if(!result) - return; - - buf = expand(argv_idx(argv, 0), 0); - g_string_assign(result, buf); - g_free(buf); -} - -void -hardcopy(WebKitWebView *page, GArray *argv, GString *result) { - (void) argv; - (void) result; - - webkit_web_frame_print(webkit_web_view_get_main_frame(page)); -} - /* just a wrapper so parse_cmd_line can be used with for_each_line_in_file */ static void parse_cmd_line_cb(const char *line, void *user_data) { @@ -650,118 +464,12 @@ parse_cmd_line_cb(const char *line, void *user_data) { } void -include(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; - (void) result; - gchar *path = argv_idx(argv, 0); - - if(!path) - return; - - if((path = find_existing_file(path))) { - if(!for_each_line_in_file(path, parse_cmd_line_cb, NULL)) { - gchar *tmp = g_strdup_printf("File %s can not be read.", path); - send_event(COMMAND_ERROR, NULL, TYPE_STR, tmp, NULL); - g_free(tmp); - } - - send_event(FILE_INCLUDED, NULL, TYPE_STR, path, NULL); - g_free(path); - } -} - -void -show_inspector(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) argv; (void) result; - - webkit_web_inspector_show(uzbl.gui.inspector); -} - -void -add_cookie(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) result; - gchar *host, *path, *name, *value; - gboolean secure = 0; - SoupDate *expires = NULL; - - if(argv->len != 6) - return; - - // Parse with same syntax as ADD_COOKIE event - host = argv_idx (argv, 0); - path = argv_idx (argv, 1); - name = argv_idx (argv, 2); - value = argv_idx (argv, 3); - secure = strcmp (argv_idx (argv, 4), "https") == 0; - if (strlen (argv_idx (argv, 5)) != 0) - expires = soup_date_new_from_time_t ( - strtoul (argv_idx (argv, 5), NULL, 10)); - - // Create new cookie - SoupCookie * cookie = soup_cookie_new (name, value, host, path, -1); - soup_cookie_set_secure (cookie, secure); - if (expires) - soup_cookie_set_expires (cookie, expires); - - // Add cookie to jar - uzbl.net.soup_cookie_jar->in_manual_add = 1; - soup_cookie_jar_add_cookie (SOUP_COOKIE_JAR (uzbl.net.soup_cookie_jar), cookie); - uzbl.net.soup_cookie_jar->in_manual_add = 0; -} - -void -delete_cookie(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) result; - - if(argv->len < 4) - return; - - SoupCookie * cookie = soup_cookie_new ( - argv_idx (argv, 2), - argv_idx (argv, 3), - argv_idx (argv, 0), - argv_idx (argv, 1), - 0); - - uzbl.net.soup_cookie_jar->in_manual_add = 1; - soup_cookie_jar_delete_cookie (SOUP_COOKIE_JAR (uzbl.net.soup_cookie_jar), cookie); - uzbl.net.soup_cookie_jar->in_manual_add = 0; -} - - -void -clear_cookies(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) argv; (void) result; - - // Replace the current cookie jar with a new empty jar - soup_session_remove_feature (uzbl.net.soup_session, - SOUP_SESSION_FEATURE (uzbl.net.soup_cookie_jar)); - g_object_unref (G_OBJECT (uzbl.net.soup_cookie_jar)); - uzbl.net.soup_cookie_jar = uzbl_cookie_jar_new (); - soup_session_add_feature(uzbl.net.soup_session, - SOUP_SESSION_FEATURE (uzbl.net.soup_cookie_jar)); -} - -void -download(WebKitWebView *web_view, GArray *argv, GString *result) { - (void) result; - - const gchar *uri = argv_idx(argv, 0); - const gchar *destination = NULL; - if(argv->len > 1) - destination = argv_idx(argv, 1); - - WebKitNetworkRequest *req = webkit_network_request_new(uri); - WebKitDownload *download = webkit_download_new(req); - - download_cb(web_view, download, (gpointer)destination); - - if(webkit_download_get_destination_uri(download)) - webkit_download_start(download); - else - g_object_unref(download); - - g_object_unref(req); +run_command_file(const gchar *path) { + if(!for_each_line_in_file(path, parse_cmd_line_cb, NULL)) { + gchar *tmp = g_strdup_printf("File %s can not be read.", path); + send_event(COMMAND_ERROR, NULL, TYPE_STR, tmp, NULL); + g_free(tmp); + } } void @@ -774,13 +482,6 @@ act_dump_config_as_events() { dump_config_as_events(); } -void -load_uri(WebKitWebView *web_view, GArray *argv, GString *result) { - (void) web_view; (void) result; - gchar * uri = argv_idx(argv, 0); - set_var_value("uri", uri ? uri : ""); -} - /* Javascript*/ void eval_js(WebKitWebView * web_view, const gchar *script, GString *result, const char *file) { @@ -861,40 +562,6 @@ eval_js(WebKitWebView * web_view, const gchar *script, GString *result, const ch JSStringRelease(js_file); } -void -run_js (WebKitWebView * web_view, GArray *argv, GString *result) { - if (argv_idx(argv, 0)) - eval_js(web_view, argv_idx(argv, 0), result, "(command)"); -} - -void -run_external_js (WebKitWebView * web_view, GArray *argv, GString *result) { - (void) result; - gchar *path = NULL; - - if (argv_idx(argv, 0) && - ((path = find_existing_file(argv_idx(argv, 0)))) ) { - gchar *file_contents = NULL; - - GIOChannel *chan = g_io_channel_new_file(path, "r", NULL); - if (chan) { - gsize len; - g_io_channel_read_to_end(chan, &file_contents, &len, NULL); - g_io_channel_unref (chan); - } - - if (uzbl.state.verbose) - printf ("External JavaScript file %s loaded\n", argv_idx(argv, 0)); - - gchar *js = str_replace("%s", argv_idx (argv, 1) ? argv_idx (argv, 1) : "", file_contents); - g_free (file_contents); - - eval_js (web_view, js, result, path); - g_free (js); - g_free(path); - } -} - void search_text (WebKitWebView *page, GArray *argv, const gboolean forward) { if (argv_idx(argv, 0) && (*argv_idx(argv, 0) != '\0')) { @@ -915,61 +582,6 @@ search_text (WebKitWebView *page, GArray *argv, const gboolean forward) { } } -void -search_clear(WebKitWebView *page, GArray *argv, GString *result) { - (void) argv; - (void) result; - - webkit_web_view_unmark_text_matches (page); - g_free(uzbl.state.searchtx); - uzbl.state.searchtx = NULL; -} - -void -search_forward_text (WebKitWebView *page, GArray *argv, GString *result) { - (void) result; - search_text(page, argv, TRUE); -} - -void -search_reverse_text(WebKitWebView *page, GArray *argv, GString *result) { - (void) result; - search_text(page, argv, FALSE); -} - -void -dehilight(WebKitWebView *page, GArray *argv, GString *result) { - (void) argv; (void) result; - webkit_web_view_set_highlight_text_matches (page, FALSE); -} - -void -chain(WebKitWebView *page, GArray *argv, GString *result) { - (void) page; - guint i = 0; - const gchar *cmd; - GString *r = g_string_new (""); - while ((cmd = argv_idx(argv, i++))) { - GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); - const CommandInfo *c = parse_command_parts(cmd, a); - if (c) - run_parsed_command(c, a, r); - g_array_free (a, TRUE); - } - if(result) - g_string_assign (result, r->str); - - g_string_free(r, TRUE); -} - -void -close_uzbl (WebKitWebView *page, GArray *argv, GString *result) { - (void)page; - (void)argv; - (void)result; - gtk_main_quit (); -} - void sharg_append(GArray *a, const gchar *str) { const gchar *s = (str ? str : ""); @@ -1088,29 +700,6 @@ spawn(GArray *argv, GString *result, gboolean exec) { } } -void -spawn_async(WebKitWebView *web_view, GArray *argv, GString *result) { - (void)web_view; (void)result; - spawn(argv, NULL, FALSE); -} - -void -spawn_sync(WebKitWebView *web_view, GArray *argv, GString *result) { - (void)web_view; - spawn(argv, result, FALSE); -} - -void -spawn_sync_exec(WebKitWebView *web_view, GArray *argv, GString *result) { - (void)web_view; - if(!result) { - GString *force_result = g_string_new(""); - spawn(argv, force_result, TRUE); - g_string_free (force_result, TRUE); - } else - spawn(argv, result, TRUE); -} - void spawn_sh(GArray *argv, GString *result) { if (!uzbl.behave.shell_cmd) { @@ -1141,18 +730,6 @@ spawn_sh(GArray *argv, GString *result) { g_strfreev (cmd); } -void -spawn_sh_async(WebKitWebView *web_view, GArray *argv, GString *result) { - (void)web_view; (void)result; - spawn_sh(argv, NULL); -} - -void -spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result) { - (void)web_view; (void)result; - spawn_sh(argv, result); -} - void run_parsed_command(const CommandInfo *c, GArray *a, GString *result) { /* send the COMMAND_EXECUTED event, except for set and event/request commands */ @@ -1254,7 +831,6 @@ parse_command(const char *cmd, const char *params, GString *result) { } } - void move_statusbar() { if (!uzbl.gui.scrolled_win && !uzbl.gui.mainbar) @@ -1587,11 +1163,7 @@ settings_init () { /* Load config file, if any */ if (s->config_file) { - if (!for_each_line_in_file(s->config_file, parse_cmd_line_cb, NULL)) { - gchar *tmp = g_strdup_printf("File %s can not be read.", s->config_file); - send_event(COMMAND_ERROR, NULL, TYPE_STR, tmp, NULL); - g_free(tmp); - } + run_command_file(s->config_file); g_setenv("UZBL_CONFIG", s->config_file, TRUE); } else if (uzbl.state.verbose) printf ("No configuration file loaded.\n"); diff --git a/src/uzbl-core.h b/src/uzbl-core.h index ce42408..8e322e6 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -48,6 +48,7 @@ #endif #include "cookie-jar.h" +#include "commands.h" #define LENGTH(x) (sizeof x / sizeof x[0]) @@ -253,20 +254,15 @@ void catch_sigterm(int s); sigfunc* setup_signal(int signe, sigfunc *shandler); gboolean set_var_value(const gchar *name, gchar *val); -void print(WebKitWebView *page, GArray *argv, GString *result); -void commands_hash(void); -void load_uri(WebKitWebView * web_view, GArray *argv, GString *result); -void chain(WebKitWebView *page, GArray *argv, GString *result); -void close_uzbl(WebKitWebView *page, GArray *argv, GString *result); + +/* Subprocess spawning */ +void spawn(GArray *argv, GString *result, gboolean exec); +void spawn_sh(GArray *argv, GString *result); /* Running commands */ gboolean run_command(const gchar *command, const gchar **args, const gboolean sync, char **output_stdout); -void spawn_async(WebKitWebView *web_view, GArray *argv, GString *result); -void spawn_sh_async(WebKitWebView *web_view, GArray *argv, GString *result); -void spawn_sync(WebKitWebView *web_view, GArray *argv, GString *result); -void spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result); -void spawn_sync_exec(WebKitWebView *web_view, GArray *argv, GString *result); +void run_command_file(const gchar *path); void parse_command(const char *cmd, const char *param, GString *result); void parse_cmd_line(const char *ctl_line, GString *result); @@ -285,14 +281,8 @@ void settings_init(); /* Search functions */ void search_text (WebKitWebView *page, GArray *argv, const gboolean forward); -void search_forward_text (WebKitWebView *page, GArray *argv, GString *result); -void search_reverse_text (WebKitWebView *page, GArray *argv, GString *result); -void search_clear(WebKitWebView *page, GArray *argv, GString *result); -void dehilight (WebKitWebView *page, GArray *argv, GString *result); /* Javascript functions */ -void run_js (WebKitWebView * web_view, GArray *argv, GString *result); -void run_external_js (WebKitWebView * web_view, GArray *argv, GString *result); void eval_js(WebKitWebView *web_view, const gchar *script, GString *result, const gchar *script_file); /* Network functions */ @@ -302,7 +292,6 @@ void handle_authentication (SoupSession *session, gboolean retrying, gpointer user_data); gboolean valid_name(const gchar* name); -void set_var(WebKitWebView *page, GArray *argv, GString *result); void act_dump_config(); void act_dump_config_as_events(); void dump_var_hash(gpointer k, gpointer v, gpointer ud); @@ -311,27 +300,12 @@ void dump_config(); void dump_config_as_events(); void retrieve_geometry(); -void event(WebKitWebView *page, GArray *argv, GString *result); void init_connect_socket(); gboolean remove_socket_from_array(GIOChannel *chan); +void scroll(GtkAdjustment* bar, gchar *amount_str); gint get_click_context(); -void hardcopy(WebKitWebView *page, GArray *argv, GString *result); -void include(WebKitWebView *page, GArray *argv, GString *result); -void show_inspector(WebKitWebView *page, GArray *argv, GString *result); -void add_cookie(WebKitWebView *page, GArray *argv, GString *result); -void delete_cookie(WebKitWebView *page, GArray *argv, GString *result); -void clear_cookies(WebKitWebView *pag, GArray *argv, GString *result); -void download(WebKitWebView *pag, GArray *argv, GString *result); -void builtins(); - -typedef void (*Command)(WebKitWebView*, GArray *argv, GString *result); - -typedef struct { - const gchar *key; - Command function; - gboolean no_split; -} CommandInfo; +gchar* expand(const char* s, guint recurse); const CommandInfo * parse_command_parts(const gchar *line, GArray *a); -- cgit v1.2.3