aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar DuClare <akarinotengoku@gmail.com>2009-05-18 21:57:05 +0300
committerGravatar DuClare <akarinotengoku@gmail.com>2009-05-18 21:57:05 +0300
commit3bac839fad3d05f426bde550bbb5403f8a0ae09d (patch)
tree6c59b883f70617c295f34c90bc54fef48829f00f
parentadd440074269a4cbae51a181805840f74d7238a4 (diff)
parentfe14095ad924b92232fad1f810fa2223e474fb75 (diff)
Merge branch 'quotes' into experimental
-rw-r--r--examples/configs/sampleconfig-dev19
-rw-r--r--uzbl.c205
-rw-r--r--uzbl.h6
3 files changed, 160 insertions, 70 deletions
diff --git a/examples/configs/sampleconfig-dev b/examples/configs/sampleconfig-dev
index 337da7b..fa4be91 100644
--- a/examples/configs/sampleconfig-dev
+++ b/examples/configs/sampleconfig-dev
@@ -8,16 +8,17 @@
# from insert mode by combining them with the modkey
# TODO: ability to attach misc things (spawn <foo>, script <bar>,.. to internal events)
-set history_handler = ./examples/scripts/history.sh
-set download_handler = ./examples/scripts/download.sh
+# Usually you want to spawn a script to handle things, but any action (such as sh) can be used
+set history_handler = spawn ./examples/scripts/history.sh
+set download_handler = spawn ./examples/scripts/download.sh
+
+# TODO: you can't use actions in cookie handler yet.
set cookie_handler = ./examples/scripts/cookies.sh
set minimum_font_size = 6
set default_font_size = 11
# use with bind ... = sh <shell-oneliner>
-# notice the '' - it's a spacer to keep bash and sh from shifting the positional parameters
-# by one, so they will appear in the same position as with scripts invoked via spawn
-set shell_cmd = sh -c %s ''
+set shell_cmd = sh -c
@@ -79,6 +80,8 @@ bind :wiki _ = uri http://wiki.archlinux.org/index.php/Special:Search?searc
bind gg _ = uri http://www.google.com/search?q=%s
bind i = insert_mode
#TODO: no 'toggle' command?
+# Enclose the executable in double quotes if it has spaces. Any additional parameters you use will
+# appear AFTER the default parameters
bind B = spawn ./examples/scripts/insert_bookmark.sh
bind U = spawn ./examples/scripts/load_url_from_history.sh
bind u = spawn ./examples/scripts/load_url_from_bookmarks.sh
@@ -96,7 +99,11 @@ bind S = script alert("hi");
# example showing how to use sh
# it sends a command to the fifo, whose path is told via a positional param
# if fifo_dir is not set, it'll echo to a file named (null) somewhere >:) remember to delete it
-bind XS = sh 'echo "act script alert (\"This is sent by the shell via a fifo\")" > "$4"'
+# The body of the shell command should be one parameter, so if it has spaces like here,
+# you must enclose it in double 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\""
# 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);}}
diff --git a/uzbl.c b/uzbl.c
index 6dd1f2d..a801f3c 100644
--- a/uzbl.c
+++ b/uzbl.c
@@ -234,7 +234,8 @@ download_cb (WebKitWebView *web_view, GObject *download, gpointer user_data) {
const gchar* uri = webkit_download_get_uri ((WebKitDownload*)download);
if (uzbl.state.verbose)
printf("Download -> %s\n",uri);
- run_command(uzbl.behave.download_handler, uri, FALSE, NULL);
+ /* if urls not escaped, we may have to escape and quote uri before this call */
+ run_handler(uzbl.behave.download_handler, uri);
}
return (FALSE);
}
@@ -335,9 +336,8 @@ load_finish_cb (WebKitWebView* page, WebKitWebFrame* frame, gpointer data) {
(void) page;
(void) frame;
(void) data;
- if (uzbl.behave.load_finish_handler) {
- run_command(uzbl.behave.load_finish_handler, NULL, FALSE, NULL);
- }
+ if (uzbl.behave.load_finish_handler)
+ run_handler(uzbl.behave.load_finish_handler, "");
}
static void
@@ -369,11 +369,8 @@ log_history_cb () {
char date [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
- strftime (date, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
- GString* args = g_string_new ("");
- g_string_printf (args, "'%s'", date);
- run_command(uzbl.behave.history_handler, args->str, FALSE, NULL);
- g_string_free (args, TRUE);
+ strftime (date, 80, "\"%Y-%m-%d %H:%M:%S\"", timeinfo);
+ run_handler(uzbl.behave.history_handler, date);
}
}
@@ -732,72 +729,122 @@ expand_template(const char *template) {
}
/* --End Statusbar functions-- */
+static void
+sharg_append(GArray *a, const gchar *str) {
+ const gchar *s = (str ? str : "");
+ g_array_append_val(a, s);
+}
// make sure that the args string you pass can properly be interpreted (eg properly escaped against whitespace, quotes etc)
static gboolean
-run_command (const char *command, const char *args, const gboolean sync, char **stdout) {
+run_command (const gchar *command, const guint npre, const gchar **args,
+ const gboolean sync, char **stdout) {
//command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> <uzbl socket file> [args]
- GString *to_execute = g_string_new ("");
GError *err = NULL;
- gchar *cmd = g_strstrip(g_strdup(command));
- gchar *qcfg = (uzbl.state.config_file ? g_shell_quote(uzbl.state.config_file) : g_strdup("''"));
- gchar *qfifo = (uzbl.comm.fifo_path ? g_shell_quote(uzbl.comm.fifo_path) : g_strdup("''"));
- gchar *qsock = (uzbl.comm.socket_path ? g_shell_quote(uzbl.comm.socket_path) : g_strdup("''"));
- gchar *quri = (uzbl.state.uri ? g_shell_quote(uzbl.state.uri) : g_strdup("''"));
- gchar *qtitle = (uzbl.gui.main_title ? g_shell_quote(uzbl.gui.main_title) : g_strdup("''"));
-
+
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ gchar *pid = itos(getpid());
+ gchar *xwin = itos(uzbl.xwin);
+ guint i;
+ sharg_append(a, command);
+ for (i = 0; i < npre; i++) /* add n args before the default vars */
+ sharg_append(a, args[i]);
+ sharg_append(a, uzbl.state.config_file);
+ sharg_append(a, pid);
+ sharg_append(a, xwin);
+ sharg_append(a, uzbl.comm.fifo_path);
+ sharg_append(a, uzbl.comm.socket_path);
+ sharg_append(a, uzbl.state.uri);
+ sharg_append(a, uzbl.gui.main_title);
+
+ for (i = npre; i < g_strv_length((gchar**)args); i++)
+ sharg_append(a, args[i]);
gboolean result;
- g_string_printf (to_execute, "%s %s '%i' '%i' %s %s",
- cmd, qcfg, (int) getpid(), (int) uzbl.xwin, qfifo, qsock);
- g_string_append_printf (to_execute, " %s %s", quri, qtitle);
- if(args) g_string_append_printf (to_execute, " %s", args);
-
- if (sync) {
- result = g_spawn_command_line_sync (to_execute->str, stdout, NULL, NULL, &err);
- } else result = g_spawn_command_line_async (to_execute->str, &err);
- if (uzbl.state.verbose)
- printf("Called %s. Result: %s\n", to_execute->str, (result ? "TRUE" : "FALSE" ));
- g_string_free (to_execute, TRUE);
+ if (sync) result = g_spawn_sync(NULL, (gchar **)a->data, NULL, G_SPAWN_SEARCH_PATH,
+ NULL, NULL, stdout, NULL, NULL, &err);
+ else result = g_spawn_async(NULL, (gchar **)a->data, NULL, G_SPAWN_SEARCH_PATH,
+ NULL, NULL, NULL, &err);
+
+ if (uzbl.state.verbose) {
+ GString *s = g_string_new("spawned:");
+ for (i = 0; i < (a->len); i++) {
+ gchar *qarg = g_shell_quote(g_array_index(a, gchar*, i));
+ g_string_append_printf(s, " %s", qarg);
+ g_free (qarg);
+ }
+ g_string_append_printf(s, " -- result: %s", (result ? "true" : "false"));
+ printf("%s\n", s->str);
+ g_string_free(s, TRUE);
+ }
if (err) {
g_printerr("error on run_command: %s\n", err->message);
g_error_free (err);
}
-
- g_free (qcfg);
- g_free (qfifo);
- g_free (qsock);
- g_free (quri);
- g_free (qtitle);
- g_free (cmd);
+ g_free (pid);
+ g_free (xwin);
+ g_array_free (a, TRUE);
return result;
}
+static gchar**
+split_quoted(const gchar* src) { /* split on unquoted space, return array of strings */
+ gboolean quote = FALSE;
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ GString *s = g_string_new ("");
+ const gchar *p;
+ gchar **ret;
+ gchar *dup;
+ for (p = src; *p != '\0'; p++) {
+ if (*p == '\\') g_string_append_c(s, *++p);
+ else if (*p == '"') quote = !quote;
+ else if ((*p == ' ') && (!quote)) {
+ dup = g_strdup(s->str);
+ g_array_append_val(a, dup);
+ g_string_truncate(s, 0);
+ } else g_string_append_c(s, *p);
+ }
+ dup = g_strdup(s->str);
+ g_array_append_val(a, dup);
+ ret = (gchar**)a->data;
+ g_array_free (a, FALSE);
+ g_string_free (s, TRUE);
+ return ret;
+}
+
static void
spawn(WebKitWebView *web_view, const char *param) {
(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 = g_strsplit(param, " ", 2);
- gchar * args = NULL;
- if (cmd[1]) {
- args = g_shell_quote(cmd[1]);
- }
- if (cmd) {
- run_command(cmd[0], args, FALSE, NULL);
- }
- if (args) {
- g_free(args);
- }
-*/
- run_command(param, NULL, FALSE, NULL);
+ //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);
+ if (cmd) run_command(cmd[0], 0, &cmd[1], FALSE, NULL);
+ g_strfreev ((gchar**)cmd);
}
static void
spawn_sh(WebKitWebView *web_view, const char *param) {
(void)web_view;
- gchar *cmd = g_strdup_printf(uzbl.behave.shell_cmd, param);
- spawn(NULL, cmd);
- g_free(cmd);
+ if (!uzbl.behave.shell_cmd) {
+ g_printerr ("spawn_sh: shell_cmd is not set!\n");
+ return;
+ }
+
+ guint i;
+ gchar *spacer = g_strdup("");
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ gchar **cmd = split_quoted(uzbl.behave.shell_cmd);
+ gchar **p = split_quoted(param);
+ 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_free (spacer);
+ g_strfreev (cmd);
+ g_strfreev (p);
+ g_array_free (a, FALSE);
}
static void
@@ -1497,6 +1544,28 @@ GtkWidget* create_window () {
}
static void
+run_handler (const gchar *act, const gchar *args) {
+ char **parts = g_strsplit(act, " ", 2);
+ if (!parts) return;
+ else if ((g_strcmp0(parts[0], "spawn") == 0)
+ || (g_strcmp0(parts[0], "sh") == 0)) {
+ GString *a = g_string_new ("");
+ char **spawnparts;
+ spawnparts = split_quoted(parts[1]);
+ g_string_append_printf(a, "\"%s\"", spawnparts[0]);
+ if (args) g_string_append_printf(a, " %s", args); /* append handler args before user args */
+ guint i;
+ for (i = 1; i < g_strv_length(spawnparts); i++) /* user args */
+ g_string_append_printf(a, " \"%s\"", spawnparts[i]);
+ parse_command(parts[0], a->str);
+ g_string_free (a, TRUE);
+ g_strfreev (spawnparts);
+ } else
+ parse_command(parts[0], parts[1]);
+ g_strfreev (parts);
+}
+
+static void
add_binding (const gchar *key, const gchar *act) {
char **parts = g_strsplit(act, " ", 2);
Action *action;
@@ -1628,14 +1697,19 @@ static void handle_cookies (SoupSession *session, SoupMessage *msg, gpointer use
gchar * stdout = NULL;
soup_message_add_header_handler(msg, "got-headers", "Set-Cookie", G_CALLBACK(save_cookies), NULL);
- GString* args = g_string_new ("");
+ GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
+ gchar *action = g_strdup ("GET");
SoupURI * soup_uri = soup_message_get_uri(msg);
- g_string_printf (args, "GET %s %s", soup_uri->host, soup_uri->path);
- run_command(uzbl.behave.cookie_handler, args->str, TRUE, &stdout);
+ 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_handler(uzbl.behave.cookie_handler); /* TODO: global stdout pointer, spawn_sync */
if(stdout) {
soup_message_headers_replace (msg->request_headers, "Cookie", stdout);
}
- g_string_free(args, TRUE);
+ g_free (action);
+ g_array_free(a, TRUE);
}
static void
@@ -1645,12 +1719,17 @@ save_cookies (SoupMessage *msg, gpointer user_data){
char *cookie;
for (ck = soup_cookies_from_response(msg); ck; ck = ck->next){
cookie = soup_cookie_to_set_cookie_header(ck->data);
- GString* args = g_string_new ("");
+ GArray *a = g_array_new(TRUE, FALSE, sizeof(gchar*));
SoupURI * soup_uri = soup_message_get_uri(msg);
- g_string_printf (args, "PUT %s %s \"%s\"", soup_uri->host, soup_uri->path, cookie);
- run_command(uzbl.behave.cookie_handler, args->str, FALSE, NULL);
- g_string_free(args, TRUE);
- free(cookie);
+ gchar *action = strdup("PUT");
+ sharg_append(a, action);
+ 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);
+ g_free (cookie);
+ g_free (action);
+ g_array_free(a, TRUE);
}
g_slist_free(ck);
}
diff --git a/uzbl.h b/uzbl.h
index 539ab98..23c1da1 100644
--- a/uzbl.h
+++ b/uzbl.h
@@ -270,7 +270,8 @@ static void
close_uzbl (WebKitWebView *page, const char *param);
static gboolean
-run_command(const char *command, const char *args, const gboolean sync, char **stdout);
+run_command(const gchar *command, const guint npre,
+ const gchar **args, const gboolean sync, char **stdout);
static void
spawn(WebKitWebView *web_view, const char *param);
@@ -333,6 +334,9 @@ static
GtkWidget* create_window ();
static void
+run_handler (const gchar *act, const gchar *args);
+
+static void
add_binding (const gchar *key, const gchar *act);
static gchar*