From f6bd9e08bf16165f1ae2a7c98255fd89709a72a9 Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Mon, 27 Dec 2010 21:40:22 -0700 Subject: remove talk_to_socket and all its associated bits --- src/uzbl-core.h | 1 - 1 file changed, 1 deletion(-) (limited to 'src/uzbl-core.h') diff --git a/src/uzbl-core.h b/src/uzbl-core.h index 98ae342..14f03a6 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -124,7 +124,6 @@ typedef struct { gchar* status_background; gchar* fifo_dir; gchar* socket_dir; - gchar* cookie_handler; gchar* authentication_handler; gchar* default_font_family; gchar* monospace_font_family; -- cgit v1.2.3 From 9340e4f23264c7e820b52f18de5ca29132c68e72 Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Thu, 13 Jan 2011 13:46:31 -0700 Subject: refactor parse_command and parse_cmd_line --- src/uzbl-core.c | 241 +++++++++++++++++++++++++++++++------------------------- src/uzbl-core.h | 15 +++- 2 files changed, 145 insertions(+), 111 deletions(-) (limited to 'src/uzbl-core.h') diff --git a/src/uzbl-core.c b/src/uzbl-core.c index 8f9ff88..ba6b4de 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -571,55 +571,55 @@ VIEWFUNC(go_forward) #undef VIEWFUNC /* -- command to callback/function map for things we cannot attach to any signals */ -struct {const char *key; CommandInfo value;} cmdlist[] = +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} }, // needed for cookie handler - { "sync_spawn_exec", {spawn_sync_exec, 0} }, // needed for load_cookies.sh :( - { "sh", {spawn_sh_async, 0} }, - { "sync_sh", {spawn_sh_sync, 0} }, // needed for cookie handler - { "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} } + { "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 }, // needed for cookie handler + { "sync_spawn_exec", spawn_sync_exec, 0 }, // needed for load_cookies.sh :( + { "sh", spawn_sh_async, 0 }, + { "sync_sh", spawn_sh_sync, 0 }, // needed for cookie handler + { "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 } }; void @@ -629,7 +629,7 @@ commands_hash(void) 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].value); + g_hash_table_insert(uzbl.behave.commands, (gpointer) cmdlist[i].key, &cmdlist[i]); } void @@ -1141,15 +1141,15 @@ dehilight (WebKitWebView *page, GArray *argv, GString *result) { void chain (WebKitWebView *page, GArray *argv, GString *result) { - (void) page; (void) result; - gchar *a = NULL; - gchar **parts = NULL; + (void) page; guint i = 0; - while ((a = argv_idx(argv, i++))) { - parts = g_strsplit (a, " ", 2); - if (parts[0]) - parse_command(parts[0], parts[1], result); - g_strfreev (parts); + const gchar *cmd; + while ((cmd = argv_idx(argv, i++))) { + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + CommandInfo *c = parse_command_parts(cmd, a); + if (c) + run_parsed_command(c, a, result); + g_array_free (a, TRUE); } } @@ -1332,48 +1332,81 @@ spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result) { } void -parse_command(const char *cmd, const char *param, GString *result) { - CommandInfo *c; - GString *tmp = g_string_new(""); +run_parsed_command(CommandInfo *c, GArray *a, GString *result) { + c->function(uzbl.gui.web_view, a, result); - if ((c = g_hash_table_lookup(uzbl.behave.commands, cmd))) { - guint i; - gchar **par = split_quoted(param, TRUE); - GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + /* send the COMMAND_EXECUTED event, except for set and event/request commands */ + if(strcmp("set", c->key) && + strcmp("event", c->key) && + strcmp("request", c->key)) { + GString *tmp = g_string_new(c->key); + const gchar *p; + guint i = 0; + while ((p = argv_idx(a, i++))) + g_string_append_printf(tmp, " '%s'", p); + send_event(COMMAND_EXECUTED, tmp->str, NULL); + g_string_free(tmp, TRUE); + } +} - if (c->no_split) { /* don't split */ - sharg_append(a, param); - } else if (par) { - for (i = 0; i < g_strv_length(par); i++) - sharg_append(a, par[i]); - } +void +parse_command_arguments(const gchar *p, GArray *a, gboolean no_split) { + if (no_split && p) { /* pass the parameters through in one chunk */ + sharg_append(a, g_strdup(p)); + return; + } + + gchar **par = split_quoted(p, TRUE); + if (par) { + guint i; + for (i = 0; i < g_strv_length(par); i++) + sharg_append(a, g_strdup(par[i])); + g_strfreev (par); + } +} - if (result == NULL) { - GString *result_print = g_string_new(""); +CommandInfo * +parse_command_parts(const gchar *line, GArray *a) { + CommandInfo *c = NULL; - c->function(uzbl.gui.web_view, a, result_print); - if (result_print->len) - printf("%*s\n", (int)result_print->len, result_print->str); + gchar *exp_line = expand(line, 0); + if(exp_line[0] == '\0') + return NULL; - g_string_free(result_print, TRUE); - } else { - c->function(uzbl.gui.web_view, a, result); - } - g_strfreev (par); - g_array_free (a, TRUE); + /* separate the line into the command and its parameters */ + gchar **tokens = g_strsplit(exp_line, " ", 2); - if(strcmp("set", cmd) && - strcmp("event", cmd) && - strcmp("request", cmd)) { - g_string_printf(tmp, "%s %s", cmd, param?param:""); - send_event(COMMAND_EXECUTED, tmp->str, NULL); - } - } - else { - g_string_printf (tmp, "%s %s", cmd, param?param:""); - send_event(COMMAND_ERROR, tmp->str, NULL); + /* look up the command */ + c = g_hash_table_lookup(uzbl.behave.commands, tokens[0]); + + if(!c) { + send_event(COMMAND_ERROR, exp_line, NULL); + g_free(exp_line); + g_strfreev(tokens); + return NULL; } - g_string_free(tmp, TRUE); + + gchar *p = g_strdup(tokens[1]); + g_free(exp_line); + g_strfreev(tokens); + + /* parse the arguments */ + parse_command_arguments(p, a, c->no_split); + g_free(p); + + return c; +} + +void +parse_command(const char *cmd, const char *params, GString *result) { + CommandInfo *c = g_hash_table_lookup(uzbl.behave.commands, cmd); + + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + + parse_command_arguments(params, a, c->no_split); + run_parsed_command(c, a, result); + + g_array_free (a, TRUE); } @@ -1467,28 +1500,18 @@ set_var_value(const gchar *name, gchar *val) { void parse_cmd_line(const char *ctl_line, GString *result) { - gchar *exp_line = NULL; gchar *work_string = g_strdup(ctl_line); /* strip trailing newline, and any other whitespace in front */ g_strstrip(work_string); - if( strcmp(work_string, "") && - strcmp(exp_line = expand(work_string, 0), "") - ) { - /* ignore comments */ - if((exp_line[0] == '#')) - ; - - /* parse a command */ - else { - gchar **tokens = NULL; - - tokens = g_strsplit(exp_line, " ", 2); - parse_command(tokens[0], tokens[1], result); - g_strfreev(tokens); - } - g_free(exp_line); + if( strcmp(work_string, "") ) { + if((work_string[0] != '#')) { /* ignore comments */ + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + CommandInfo *c = parse_command_parts(work_string, a); + run_parsed_command(c, a, result); + g_array_free (a, TRUE); + } } g_free(work_string); diff --git a/src/uzbl-core.h b/src/uzbl-core.h index f81722d..ef948ca 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -479,10 +479,21 @@ builtins(); typedef void (*Command)(WebKitWebView*, GArray *argv, GString *result); typedef struct { - Command function; - gboolean no_split; + const gchar *key; + Command function; + gboolean no_split; } CommandInfo; +CommandInfo * +parse_command_parts(const gchar *line, GArray *a); + +void +parse_command_arguments(const gchar *p, GArray *a, gboolean no_split); + +void +run_parsed_command(CommandInfo *c, GArray *a, GString *result); + + typedef struct { gchar *name; gchar *cmd; -- cgit v1.2.3 From 975e322d37afaca6e565b37282f2d9e6038124bc Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Thu, 13 Jan 2011 12:11:19 -0700 Subject: add a variable @_ that expands to the result of the last sync command --- src/uzbl-core.c | 9 ++++++++- src/uzbl-core.h | 3 ++- tests/test-command.c | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) (limited to 'src/uzbl-core.h') diff --git a/src/uzbl-core.c b/src/uzbl-core.c index ba6b4de..8e14982 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -145,6 +145,7 @@ const struct var_name_to_ptr_t { { "SELECTED_URI", PTR_C_STR(uzbl.state.selected_url, NULL)}, { "NAME", PTR_C_STR(uzbl.state.instance_name, NULL)}, { "PID", PTR_C_STR(uzbl.info.pid_str, NULL)}, + { "_", PTR_C_STR(uzbl.state.last_result, NULL)}, { NULL, {.ptr.s = NULL, .type = TYPE_INT, .dump = 0, .writeable = 0, .func = NULL}} }; @@ -1347,6 +1348,11 @@ run_parsed_command(CommandInfo *c, GArray *a, GString *result) { send_event(COMMAND_EXECUTED, tmp->str, NULL); g_string_free(tmp, TRUE); } + + if(result) { + g_free(uzbl.state.last_result); + uzbl.state.last_result = g_strdup(result->str); + } } void @@ -2294,7 +2300,8 @@ initialize(int argc, char *argv[]) { uzbl.state.executable_path = g_strdup(argv[0]); uzbl.state.selected_url = NULL; - uzbl.state.searchtx = NULL; + uzbl.state.searchtx = NULL; + uzbl.state.last_result = NULL; GOptionContext* context = g_option_context_new ("[ uri ] - load a uri by default"); g_option_context_add_main_entries (context, entries, NULL); diff --git a/src/uzbl-core.h b/src/uzbl-core.h index ef948ca..049053a 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -98,7 +98,8 @@ typedef struct { gchar *selected_url; gchar *last_selected_url; gchar *executable_path; - gchar* searchtx; + gchar *searchtx; + gchar *last_result; gboolean verbose; gboolean events_stdout; GPtrArray *event_buffer; diff --git a/tests/test-command.c b/tests/test-command.c index 7b33405..20c6aa0 100644 --- a/tests/test-command.c +++ b/tests/test-command.c @@ -281,6 +281,21 @@ test_js (void) { g_string_free(result, TRUE); } +void +test_last_result (void) { + GString *result = g_string_new(""); + + /* the last result gets set */ + parse_cmd_line("js -1", result); + g_assert_cmpstr("-1", ==, uzbl.state.last_result); + + /* the last result can be used in a chain */ + parse_cmd_line("chain 'js 1' 'js \\@_ + 1'", result); + g_assert_cmpstr("2", ==, uzbl.state.last_result); + + g_string_free(result, TRUE); +} + void test_run_handler_arg_order (void) { run_handler("sync_spawn echo uvw xyz", "abc def"); @@ -320,6 +335,8 @@ main (int argc, char *argv[]) { g_test_add_func("/test-command/js", test_js); + g_test_add_func("/test-command/last-result", test_last_result); + /* the following aren't really "command" tests, but they're not worth * splitting into a separate file yet */ g_test_add_func("/test-command/run_handler/arg-order", test_run_handler_arg_order); -- cgit v1.2.3 From 99961c4561f478523048ed69b37b37c432c42b8a Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Thu, 13 Jan 2011 17:41:08 -0700 Subject: kill run_handler, inject_handler_args, strfree and sync_stdout --- src/callbacks.c | 51 ++++++---- src/cookie-jar.c | 56 ++++++++--- src/uzbl-core.c | 271 ++++++++++++++++----------------------------------- src/uzbl-core.h | 12 +-- tests/test-command.c | 37 +------ 5 files changed, 166 insertions(+), 261 deletions(-) (limited to 'src/uzbl-core.h') diff --git a/src/callbacks.c b/src/callbacks.c index 7b06873..bc06a45 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -641,23 +641,26 @@ navigation_decision_cb (WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNe printf("Navigation requested -> %s\n", uri); if (uzbl.behave.scheme_handler) { - GString *s = g_string_new (""); - g_string_printf(s, "'%s'", uri); + GString *result = g_string_new (""); + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + const CommandInfo *c = parse_command_parts(uzbl.behave.scheme_handler, a); - run_handler(uzbl.behave.scheme_handler, s->str); + if(c) { + g_array_append_val(a, uri); + run_parsed_command(c, a, result); + } + g_array_free(a, TRUE); - if(uzbl.comm.sync_stdout && strcmp (uzbl.comm.sync_stdout, "") != 0) { - char *p = strchr(uzbl.comm.sync_stdout, '\n' ); + if(result->len > 0) { + char *p = strchr(result->str, '\n' ); if ( p != NULL ) *p = '\0'; - if (!strcmp(uzbl.comm.sync_stdout, "USED")) { + if (!strcmp(result->str, "USED")) { webkit_web_policy_decision_ignore(policy_decision); decision_made = TRUE; } } - if (uzbl.comm.sync_stdout) - uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout); - g_string_free(s, TRUE); + g_string_free(result, TRUE); } if (!decision_made) webkit_web_policy_decision_use(policy_decision); @@ -841,27 +844,35 @@ download_cb(WebKitWebView *web_view, WebKitDownload *download, gpointer user_dat (this may be inaccurate, there's nothing we can do about that.) */ unsigned int total_size = webkit_download_get_total_size(download); - gchar *ev = g_strdup_printf("'%s' '%s' '%s' %d", uri, suggested_filename, - content_type, total_size); - run_handler(uzbl.behave.download_handler, ev); - g_free(ev); - - /* no response, cancel the download */ - if(!uzbl.comm.sync_stdout) { + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + const CommandInfo *c = parse_command_parts(uzbl.behave.download_handler, a); + if(!c) { webkit_download_cancel(download); + g_array_free(a, TRUE); return FALSE; } + g_array_append_val(a, uri); + g_array_append_val(a, suggested_filename); + g_array_append_val(a, content_type); + gchar *total_size_s = g_strdup_printf("%d", total_size); + g_array_append_val(a, total_size_s); + + GString *result = g_string_new (""); + run_parsed_command(c, a, result); + + g_free(total_size_s); + g_array_free(a, TRUE); + /* no response, cancel the download */ - if(uzbl.comm.sync_stdout[0] == 0) { + if(result->len == 0) { webkit_download_cancel(download); - uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout); return FALSE; } /* we got a response, it's the path we should download the file to */ - gchar *destination_path = uzbl.comm.sync_stdout; - uzbl.comm.sync_stdout = NULL; + gchar *destination_path = result->str; + g_string_free(result, FALSE); /* presumably people don't need newlines in their filenames. */ char *p = strchr(destination_path, '\n'); diff --git a/src/cookie-jar.c b/src/cookie-jar.c index 626e454..9ddef3b 100644 --- a/src/cookie-jar.c +++ b/src/cookie-jar.c @@ -74,11 +74,11 @@ uzbl_cookie_jar_set_handler(UzblCookieJar *jar, const gchar* handler) { char *get_cookies(UzblCookieJar *jar, SoupURI *uri) { gchar *result, *path; - GString *s = g_string_new ("GET"); path = uri->path[0] ? uri->path : "/"; if(has_socket_handler(jar)) { + GString *s = g_string_new ("GET"); g_string_append_c(s, 0); /* null-terminate the GET */ g_string_append_len(s, uri->scheme, strlen(uri->scheme)+1); g_string_append_len(s, uri->host, strlen(uri->host)+1 ); @@ -88,13 +88,30 @@ char *get_cookies(UzblCookieJar *jar, SoupURI *uri) { /* try it again; older cookie daemons closed the connection after each request */ if(result == NULL) result = do_socket_request(jar, s->str, s->len); + + g_string_free(s, TRUE); } else { - g_string_append_printf(s, " '%s' '%s' '%s'", uri->scheme, uri->host, uri->path); + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + + const CommandInfo *c = parse_command_parts(jar->handler, a); + if(!c) { + g_array_free(a, TRUE); + return NULL; + } - run_handler(jar->handler, s->str); - result = g_strdup(uzbl.comm.sync_stdout); + /* add our handler args */ + g_array_append_val(a, "GET"); + g_array_append_val(a, uri->scheme); + g_array_append_val(a, uri->host); + g_array_append_val(a, uri->path); + + GString *r = g_string_new (""); + run_parsed_command(c, a, r); + result = r->str; + g_string_free(r, FALSE); + g_array_free(a, TRUE); } - g_string_free(s, TRUE); + return result; } @@ -168,7 +185,7 @@ changed(SoupCookieJar *jar, SoupCookie *old_cookie, SoupCookie *new_cookie) { gchar * eventstr = g_strdup_printf ("'%s' '%s' '%s' '%s' '%s' '%s'", cookie->domain, cookie->path, cookie->name, cookie->value, scheme, expires?expires:""); - if(new_cookie) + if(new_cookie) send_event(ADD_COOKIE, eventstr, NULL); else send_event(DELETE_COOKIE, eventstr, NULL); @@ -182,9 +199,8 @@ changed(SoupCookieJar *jar, SoupCookie *old_cookie, SoupCookie *new_cookie) { if(!new_cookie) return; - GString *s = g_string_new ("PUT"); - if(has_socket_handler(uzbl_jar)) { + GString *s = g_string_new ("PUT"); g_string_append_c(s, 0); /* null-terminate the PUT */ g_string_append_len(s, scheme, strlen(scheme)+1); g_string_append_len(s, new_cookie->domain, strlen(new_cookie->domain)+1 ); @@ -197,13 +213,29 @@ changed(SoupCookieJar *jar, SoupCookie *old_cookie, SoupCookie *new_cookie) { result = do_socket_request(uzbl_jar, s->str, s->len+1); g_free(result); + g_string_free(s, TRUE); } else { - g_string_append_printf(s, " '%s' '%s' '%s' '%s=%s'", scheme, new_cookie->domain, new_cookie->path, new_cookie->name, new_cookie->value); + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); - run_handler(uzbl_jar->handler, s->str); - } + const CommandInfo *c = parse_command_parts(uzbl_jar->handler, a); + if(!c) { + g_array_free(a, TRUE); + return; + } + + g_array_append_val(a, "PUT"); + g_array_append_val(a, scheme); + g_array_append_val(a, new_cookie->domain); + g_array_append_val(a, new_cookie->path); - g_string_free(s, TRUE); + gchar *kv = g_strconcat(new_cookie->name, "=", new_cookie->value, NULL); + g_array_append_val(a, kv); + + run_parsed_command(c, a, NULL); + + g_free(kv); + g_array_free(a, TRUE); + } } static void diff --git a/src/uzbl-core.c b/src/uzbl-core.c index 8e14982..0a90bb9 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -360,12 +360,6 @@ itos(int val) { return g_strdup(tmp); } -gchar* -strfree(gchar *str) { - g_free(str); - return NULL; -} - gchar* argv_idx(const GArray *a, const guint idx) { return g_array_index(a, gchar*, idx); } @@ -1119,7 +1113,8 @@ search_clear(WebKitWebView *page, GArray *argv, GString *result) { (void) result; webkit_web_view_unmark_text_matches (page); - uzbl.state.searchtx = strfree (uzbl.state.searchtx); + g_free(uzbl.state.searchtx); + uzbl.state.searchtx = NULL; } void @@ -1145,13 +1140,18 @@ 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*)); - CommandInfo *c = parse_command_parts(cmd, a); + const CommandInfo *c = parse_command_parts(cmd, a); if (c) - run_parsed_command(c, a, result); + 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 @@ -1185,7 +1185,8 @@ run_command (const gchar *command, const gchar **args, const gboolean sync, gboolean result; if (sync) { - if (*output_stdout) *output_stdout = strfree(*output_stdout); + if (*output_stdout) + g_free(*output_stdout); result = g_spawn_sync(NULL, (gchar **)a->data, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, output_stdout, NULL, NULL, &err); @@ -1253,25 +1254,28 @@ split_quoted(const gchar* src, const gboolean unquote) { } void -spawn(GArray *argv, gboolean sync, gboolean exec) { +spawn(GArray *argv, GString *result, gboolean exec) { gchar *path = NULL; gchar *arg_car = argv_idx(argv, 0); const gchar **arg_cdr = &g_array_index(argv, const gchar *, 1); if (arg_car && (path = find_existing_file(arg_car))) { - if (uzbl.comm.sync_stdout) - uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout); - run_command(path, arg_cdr, sync, sync?&uzbl.comm.sync_stdout:NULL); - // run each line of output from the program as a command - if (sync && exec && uzbl.comm.sync_stdout) { - gchar *head = uzbl.comm.sync_stdout; - gchar *tail; - while ((tail = strchr (head, '\n'))) { - *tail = '\0'; - parse_cmd_line(head, NULL); - head = tail + 1; + gchar *r = NULL; + run_command(path, arg_cdr, result != NULL, result ? &r : NULL); + if(result) { + g_string_assign(result, r); + // run each line of output from the program as a command + if (exec && r) { + gchar *head = r; + gchar *tail; + while ((tail = strchr (head, '\n'))) { + *tail = '\0'; + parse_cmd_line(head, NULL); + head = tail + 1; + } } } + g_free(r); g_free(path); } } @@ -1279,23 +1283,28 @@ spawn(GArray *argv, gboolean sync, gboolean exec) { void spawn_async(WebKitWebView *web_view, GArray *argv, GString *result) { (void)web_view; (void)result; - spawn(argv, FALSE, FALSE); + spawn(argv, NULL, FALSE); } void spawn_sync(WebKitWebView *web_view, GArray *argv, GString *result) { - (void)web_view; (void)result; - spawn(argv, TRUE, FALSE); + (void)web_view; + spawn(argv, result, FALSE); } void spawn_sync_exec(WebKitWebView *web_view, GArray *argv, GString *result) { - (void)web_view; (void)result; - spawn(argv, TRUE, TRUE); + (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, gboolean sync) { +spawn_sh(GArray *argv, GString *result) { if (!uzbl.behave.shell_cmd) { g_printerr ("spawn_sh: shell_cmd is not set!\n"); return; @@ -1303,19 +1312,23 @@ spawn_sh(GArray *argv, gboolean sync) { guint i; gchar **cmd = split_quoted(uzbl.behave.shell_cmd, TRUE); + if(!cmd) + return; + gchar *cmdname = g_strdup(cmd[0]); g_array_insert_val(argv, 1, cmdname); for (i = 1; i < g_strv_length(cmd); i++) g_array_prepend_val(argv, cmd[i]); - if (cmd) { - if (uzbl.comm.sync_stdout) - uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout); + if (result) { + gchar *r = NULL; + run_command(cmd[0], (const gchar **) argv->data, TRUE, &r); + g_string_assign(result, r); + g_free(r); + } else + run_command(cmd[0], (const gchar **) argv->data, FALSE, NULL); - run_command(cmd[0], (const gchar **) argv->data, - sync, sync?&uzbl.comm.sync_stdout:NULL); - } g_free (cmdname); g_strfreev (cmd); } @@ -1323,17 +1336,17 @@ spawn_sh(GArray *argv, gboolean sync) { void spawn_sh_async(WebKitWebView *web_view, GArray *argv, GString *result) { (void)web_view; (void)result; - spawn_sh(argv, FALSE); + spawn_sh(argv, NULL); } void spawn_sh_sync(WebKitWebView *web_view, GArray *argv, GString *result) { (void)web_view; (void)result; - spawn_sh(argv, TRUE); + spawn_sh(argv, result); } void -run_parsed_command(CommandInfo *c, GArray *a, GString *result) { +run_parsed_command(const CommandInfo *c, GArray *a, GString *result) { c->function(uzbl.gui.web_view, a, result); /* send the COMMAND_EXECUTED event, except for set and event/request commands */ @@ -1350,8 +1363,8 @@ run_parsed_command(CommandInfo *c, GArray *a, GString *result) { } if(result) { - g_free(uzbl.state.last_result); - uzbl.state.last_result = g_strdup(result->str); + g_free(uzbl.state.last_result); + uzbl.state.last_result = g_strdup(result->str); } } @@ -1371,7 +1384,7 @@ parse_command_arguments(const gchar *p, GArray *a, gboolean no_split) { } } -CommandInfo * +const CommandInfo * parse_command_parts(const gchar *line, GArray *a) { CommandInfo *c = NULL; @@ -1406,13 +1419,18 @@ parse_command_parts(const gchar *line, GArray *a) { void parse_command(const char *cmd, const char *params, GString *result) { CommandInfo *c = g_hash_table_lookup(uzbl.behave.commands, cmd); + if(c) { + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); - GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); - - parse_command_arguments(params, a, c->no_split); - run_parsed_command(c, a, result); + parse_command_arguments(params, a, c->no_split); + run_parsed_command(c, a, result); - g_array_free (a, TRUE); + g_array_free (a, TRUE); + } else { + gchar *tmp = g_strconcat(cmd, " ", params, NULL); + send_event(COMMAND_ERROR, tmp, NULL); + g_free(tmp); + } } @@ -1514,7 +1532,7 @@ parse_cmd_line(const char *ctl_line, GString *result) { if( strcmp(work_string, "") ) { if((work_string[0] != '#')) { /* ignore comments */ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); - CommandInfo *c = parse_command_parts(work_string, a); + const CommandInfo *c = parse_command_parts(work_string, a); run_parsed_command(c, a, result); g_array_free (a, TRUE); } @@ -1985,126 +2003,6 @@ create_plug () { return plug; } - -gchar** -inject_handler_args(const gchar *actname, const gchar *origargs, const gchar *newargs) { - /* - If actname is one that calls an external command, this function will inject - newargs in front of the user-provided args in that command line. They will - come become after the body of the script (in sh) or after the name of - the command to execute (in spawn). - i.e. sh becomes sh and - spawn becomes spawn . - - The return value consist of two strings: the action (sh, ...) and its args. - - If act is not one that calls an external command, then the given action merely - gets duplicated. - */ - GArray *rets = g_array_new(TRUE, FALSE, sizeof(gchar*)); - /* Arrr! Here be memory leaks */ - gchar *actdup = g_strdup(actname); - g_array_append_val(rets, actdup); - - if ((g_strcmp0(actname, "spawn") == 0) || - (g_strcmp0(actname, "sh") == 0) || - (g_strcmp0(actname, "sync_spawn") == 0) || - (g_strcmp0(actname, "sync_sh") == 0)) { - guint i; - GString *a = g_string_new(""); - gchar **spawnparts = split_quoted(origargs, FALSE); - g_string_append_printf(a, "%s", spawnparts[0]); /* sh body or script name */ - if (newargs) g_string_append_printf(a, " %s", newargs); /* handler args */ - - for (i = 1; i < g_strv_length(spawnparts); i++) /* user args */ - if (spawnparts[i]) g_string_append_printf(a, " %s", spawnparts[i]); - - g_array_append_val(rets, a->str); - g_string_free(a, FALSE); - g_strfreev(spawnparts); - } else { - gchar *origdup = g_strdup(origargs); - g_array_append_val(rets, origdup); - } - return (gchar**)g_array_free(rets, FALSE); -} - -void -run_handler (const gchar *act, const gchar *args) { - /* Consider this code a temporary hack to make the handlers usable. - In practice, all this splicing, injection, and reconstruction is - inefficient, annoying and hard to manage. Potential pitfalls arise - when the handler specific args 1) are not quoted (the handler - callbacks should take care of this) 2) are quoted but interfere - with the users' own quotation. A more ideal solution is - to refactor parse_command so that it doesn't just take a string - and execute it; rather than that, we should have a function which - returns the argument vector parsed from the string. This vector - could be modified (e.g. insert additional args into it) before - passing it to the next function that actually executes it. Though - it still isn't perfect for chain actions.. will reconsider & re- - factor when I have the time. -duc */ - - if (!act) return; - char **parts = g_strsplit(act, " ", 2); - if (!parts || !parts[0]) return; - if (g_strcmp0(parts[0], "chain") == 0) { - GString *newargs = g_string_new(""); - gchar **chainparts = split_quoted(parts[1], FALSE); - - /* for every argument in the chain, inject the handler args - and make sure the new parts are wrapped in quotes */ - gchar **cp = chainparts; - gchar quot = '\''; - gchar *quotless = NULL; - gchar **spliced_quotless = NULL; // sigh -_-; - gchar **inpart = NULL; - - while (*cp) { - if ((**cp == '\'') || (**cp == '\"')) { /* strip old quotes */ - quot = **cp; - quotless = g_strndup(&(*cp)[1], strlen(*cp) - 2); - } else quotless = g_strdup(*cp); - - spliced_quotless = g_strsplit(quotless, " ", 2); - inpart = inject_handler_args(spliced_quotless[0], spliced_quotless[1], args); - g_strfreev(spliced_quotless); - - g_string_append_printf(newargs, " %c%s %s%c", quot, inpart[0], inpart[1], quot); - g_free(quotless); - g_strfreev(inpart); - cp++; - } - - parse_command(parts[0], &(newargs->str[1]), NULL); - g_string_free(newargs, TRUE); - g_strfreev(chainparts); - - } else { - gchar **inparts; - gchar *inparts_[2]; - if (parts[1]) { - /* expand the user-specified arguments */ - gchar* expanded = expand(parts[1], 0); - inparts = inject_handler_args(parts[0], expanded, args); - g_free(expanded); - } else { - inparts_[0] = parts[0]; - inparts_[1] = g_strdup(args); - inparts = inparts_; - } - - parse_command(inparts[0], inparts[1], NULL); - - if (inparts != inparts_) { - g_free(inparts[0]); - g_free(inparts[1]); - } else - g_free(inparts[1]); - } - g_strfreev(parts); -} - void settings_init () { State *s = &uzbl.state; @@ -2145,32 +2043,34 @@ void handle_authentication (SoupSession *session, SoupMessage *msg, SoupAuth *au (void) user_data; if(uzbl.behave.authentication_handler && *uzbl.behave.authentication_handler != 0) { - gchar *info, *host, *realm; - gchar *p; - soup_session_pause_message(session, msg); - /* Sanitize strings */ - info = g_strdup(soup_auth_get_info(auth)); - host = g_strdup(soup_auth_get_host(auth)); - realm = g_strdup(soup_auth_get_realm(auth)); - for (p = info; *p; p++) if (*p == '\'') *p = '\"'; - for (p = host; *p; p++) if (*p == '\'') *p = '\"'; - for (p = realm; *p; p++) if (*p == '\'') *p = '\"'; + GString *result = g_string_new (""); - GString *s = g_string_new (""); - g_string_printf(s, "'%s' '%s' '%s' '%s'", - info, host, realm, retrying?"TRUE":"FALSE"); + gchar *info = g_strdup(soup_auth_get_info(auth)); + gchar *host = g_strdup(soup_auth_get_host(auth)); + gchar *realm = g_strdup(soup_auth_get_realm(auth)); - run_handler(uzbl.behave.authentication_handler, s->str); + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + const CommandInfo *c = parse_command_parts(uzbl.behave.authentication_handler, a); + if(c) { + sharg_append(a, info); + sharg_append(a, host); + sharg_append(a, realm); + sharg_append(a, retrying ? "TRUE" : "FALSE"); - if (uzbl.comm.sync_stdout && strcmp (uzbl.comm.sync_stdout, "") != 0) { + run_parsed_command(c, a, result); + } + g_array_free(a, TRUE); + + if (result->len > 0) { char *username, *password; int number_of_endls=0; - username = uzbl.comm.sync_stdout; + username = result->str; - for (p = uzbl.comm.sync_stdout; *p; p++) { + gchar *p; + for (p = result->str; *p; p++) { if (*p == '\n') { *p = '\0'; if (++number_of_endls == 1) @@ -2184,12 +2084,9 @@ void handle_authentication (SoupSession *session, SoupMessage *msg, SoupAuth *au soup_auth_authenticate(auth, username, password); } - if (uzbl.comm.sync_stdout) - uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout); - soup_session_unpause_message(session, msg); - g_string_free(s, TRUE); + g_string_free(result, TRUE); g_free(info); g_free(host); g_free(realm); diff --git a/src/uzbl-core.h b/src/uzbl-core.h index 049053a..14b0571 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -83,7 +83,6 @@ typedef struct { /* stores (key)"variable name" -> (value)"pointer to var*/ GHashTable *proto_var; - gchar *sync_stdout; GPtrArray *connect_chan; GPtrArray *client_chan; } Communication; @@ -237,9 +236,6 @@ typedef struct { char * itos(int val); -gchar* -strfree(gchar *str); - void clean_up(void); @@ -343,9 +339,6 @@ create_window (); GtkPlug* create_plug (); -void -run_handler (const gchar *act, const gchar *args); - void settings_init (); @@ -485,15 +478,14 @@ typedef struct { gboolean no_split; } CommandInfo; -CommandInfo * +const CommandInfo * parse_command_parts(const gchar *line, GArray *a); void parse_command_arguments(const gchar *p, GArray *a, gboolean no_split); void -run_parsed_command(CommandInfo *c, GArray *a, GString *result); - +run_parsed_command(const CommandInfo *c, GArray *a, GString *result); typedef struct { gchar *name; diff --git a/tests/test-command.c b/tests/test-command.c index 20c6aa0..d07bf79 100644 --- a/tests/test-command.c +++ b/tests/test-command.c @@ -263,11 +263,12 @@ test_toggle_status (void) { void test_sync_sh (void) { - parse_cmd_line("sync_sh 'echo Test echo.'", NULL); - g_assert_cmpstr("Test echo.\n", ==, uzbl.comm.sync_stdout); + GString *result = g_string_new(""); + + parse_cmd_line("sync_sh 'echo Test echo.'", result); + g_assert_cmpstr("Test echo.\n", ==, result->str); - /* clean up after ourselves */ - uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout); + g_string_free(result, TRUE); } void @@ -296,29 +297,6 @@ test_last_result (void) { g_string_free(result, TRUE); } -void -test_run_handler_arg_order (void) { - run_handler("sync_spawn echo uvw xyz", "abc def"); - - assert(uzbl.comm.sync_stdout); - - /* the rest of the result should be the arguments passed to run_handler. */ - /* the arguments in the second argument to run_handler should be placed before any - * included in the first argument to run handler. */ - g_assert_cmpstr("abc def uvw xyz\n", ==, uzbl.comm.sync_stdout); -} - -void -test_run_handler_expand (void) { - uzbl.net.useragent = "Test uzbl uzr agent"; - run_handler("sync_spawn echo @useragent", "result:"); - - assert(uzbl.comm.sync_stdout); - - /* the user-specified arguments to the handler should have been expanded */ - g_assert_cmpstr("result: Test uzbl uzr agent\n", ==, uzbl.comm.sync_stdout); -} - int main (int argc, char *argv[]) { /* set up tests */ @@ -337,11 +315,6 @@ main (int argc, char *argv[]) { g_test_add_func("/test-command/last-result", test_last_result); - /* the following aren't really "command" tests, but they're not worth - * splitting into a separate file yet */ - g_test_add_func("/test-command/run_handler/arg-order", test_run_handler_arg_order); - g_test_add_func("/test-command/run_handler/expand", test_run_handler_expand); - /* set up uzbl */ initialize(argc, argv); -- cgit v1.2.3 From d9a413d5a1a80b86c94bb38b278e60ac1d0656c8 Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Mon, 14 Feb 2011 21:39:16 -0700 Subject: gtk3 needs us to include gtk/gtkx.h --- src/uzbl-core.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/uzbl-core.h') diff --git a/src/uzbl-core.h b/src/uzbl-core.h index f81722d..7a2adca 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -40,6 +40,10 @@ #include #include +#if GTK_CHECK_VERSION(2,91,0) + #include +#endif + #include "cookie-jar.h" #define LENGTH(x) (sizeof x / sizeof x[0]) -- cgit v1.2.3