Skip to content

Commit

Permalink
Add (spawn|sh)_sync, make handle_cookies use run_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hmkemppainen committed May 23, 2009
1 parent c3bdf1c commit 3b9d97b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
3 changes: 1 addition & 2 deletions examples/configs/sampleconfig-dev
Expand Up @@ -12,8 +12,7 @@
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 cookie_handler = spawn_sync ./examples/scripts/cookies.sh
set minimum_font_size = 6
set font_size = 11
# monospace_size defaults to font_size, but you can alter it independently
Expand Down
70 changes: 53 additions & 17 deletions uzbl.c
Expand Up @@ -169,6 +169,9 @@ itos(int val) {
return g_strdup(tmp);
}

static gchar*
strfree(gchar *str) { g_free(str); return NULL; } // for freeing & setting to null in one go

static gchar*
argv_idx(const GArray *a, const guint idx) { return g_array_index(a, gchar*, idx); }

Expand Down Expand Up @@ -500,7 +503,9 @@ static struct {char *name; Command command[2];} cmdlist[] =
{ "script", {run_external_js, 0} },
{ "toggle_status", {toggle_status_cb, 0} },
{ "spawn", {spawn, 0} },
{ "spawn_sync", {spawn_sync, 0} }, // needed for cookie handler
{ "sh", {spawn_sh, 0} },
{ "sh_sync", {spawn_sh_sync, 0} }, // needed for cookie handler
{ "exit", {close_uzbl, 0} },
{ "search", {search_forward_text, NOSPLIT} },
{ "search_reverse", {search_reverse_text, NOSPLIT} },
Expand Down Expand Up @@ -907,10 +912,13 @@ run_command (const gchar *command, const guint npre, const gchar **args,
sharg_append(a, args[i]);

gboolean result;
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 (sync) {
if (*stdout) *stdout = strfree(*stdout);

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:");
Expand Down Expand Up @@ -978,6 +986,15 @@ spawn(WebKitWebView *web_view, GArray *argv) {
run_command(argv_idx(argv, 0), 0, ((const gchar **) (argv->data + sizeof(gchar*))), FALSE, NULL);
}

static void
spawn_sync(WebKitWebView *web_view, GArray *argv) {
(void)web_view;

if (argv_idx(argv, 0))
run_command(argv_idx(argv, 0), 0, ((const gchar **) (argv->data + sizeof(gchar*))),
TRUE, &uzbl.comm.sync_stdout);
}

static void
spawn_sh(WebKitWebView *web_view, GArray *argv) {
(void)web_view;
Expand All @@ -999,6 +1016,28 @@ spawn_sh(WebKitWebView *web_view, GArray *argv) {
g_strfreev (cmd);
}

static void
spawn_sh_sync(WebKitWebView *web_view, GArray *argv) {
(void)web_view;
if (!uzbl.behave.shell_cmd) {
g_printerr ("spawn_sh_sync: shell_cmd is not set!\n");
return;
}

guint i;
gchar *spacer = g_strdup("");
g_array_insert_val(argv, 1, spacer);
gchar **cmd = split_quoted(uzbl.behave.shell_cmd, TRUE);

for (i = 1; i < g_strv_length(cmd); i++)
g_array_prepend_val(argv, cmd[i]);

if (cmd) run_command(cmd[0], g_strv_length(cmd) + 1, (const gchar **) argv->data,
TRUE, &uzbl.comm.sync_stdout);
g_free (spacer);
g_strfreev (cmd);
}

static void
parse_command(const char *cmd, const char *param) {
Command *c;
Expand Down Expand Up @@ -1848,21 +1887,18 @@ static void handle_cookies (SoupSession *session, SoupMessage *msg, gpointer use
(void) user_data;
if (!uzbl.behave.cookie_handler) return;

gchar * stdout = NULL;
soup_message_add_header_handler(msg, "got-headers", "Set-Cookie", G_CALLBACK(save_cookies), NULL);
GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*));
gchar *action = g_strdup ("GET");
GString *s = g_string_new ("");
SoupURI * soup_uri = soup_message_get_uri(msg);
sharg_append(a, action);
sharg_append(a, soup_uri->host);
sharg_append(a, soup_uri->path);
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);
}
g_free (action);
g_array_free(a, TRUE);
g_string_printf(s, "GET '%s' '%s'", soup_uri->host, soup_uri->path);
run_handler(uzbl.behave.cookie_handler, s->str);

if(uzbl.comm.sync_stdout)
soup_message_headers_replace (msg->request_headers, "Cookie", uzbl.comm.sync_stdout);
printf("stdout: %s\n", uzbl.comm.sync_stdout); // debugging
if (uzbl.comm.sync_stdout) uzbl.comm.sync_stdout = strfree(uzbl.comm.sync_stdout);

g_string_free(s, TRUE);
}

static void
Expand Down
8 changes: 7 additions & 1 deletion uzbl.h
Expand Up @@ -95,7 +95,7 @@ typedef struct {
GRegex *keycmd_regex;
GRegex *get_regex;
GRegex *bind_regex;
gchar **sync_stdout;
gchar *sync_stdout;
} Communication;


Expand Down Expand Up @@ -291,6 +291,12 @@ spawn(WebKitWebView *web_view, GArray *argv);
static void
spawn_sh(WebKitWebView *web_view, GArray *argv);

static void
spawn_sync(WebKitWebView *web_view, GArray *argv);

static void
spawn_sh_sync(WebKitWebView *web_view, GArray *argv);

static void
parse_command(const char *cmd, const char *param);

Expand Down

0 comments on commit 3b9d97b

Please sign in to comment.