2005-09-15 09:58:31 -04:00
|
|
|
/* Guile scripting hooks */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <libguile.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2005-12-17 01:32:08 -05:00
|
|
|
#include "cache/cache.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "main/event.h"
|
2005-12-17 01:32:08 -05:00
|
|
|
#include "protocol/uri.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "scripting/guile/hooks.h"
|
|
|
|
#include "session/session.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
|
|
|
static SCM
|
|
|
|
internal_module(void)
|
|
|
|
{
|
|
|
|
/* XXX: should use internal_module from core.c instead of referring to
|
|
|
|
* the module by name, once I figure out how... --pw */
|
|
|
|
return scm_c_resolve_module("elinks internal");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We need to catch and handle errors because, otherwise, Guile will kill us.
|
|
|
|
* Unfortunately, I do not know Guile, but this seems to work... -- Miciah */
|
|
|
|
static SCM
|
|
|
|
error_handler(void *data, SCM tag, SCM throw_args)
|
|
|
|
{
|
|
|
|
return SCM_BOOL_F;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SCM
|
|
|
|
get_guile_hook_do(void *data)
|
|
|
|
{
|
2022-02-26 11:48:17 -05:00
|
|
|
char *hook = (char *)data;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
return scm_c_module_lookup(internal_module(), hook);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SCM
|
2022-02-26 11:48:17 -05:00
|
|
|
get_guile_hook(const char *hook)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2022-02-26 11:48:17 -05:00
|
|
|
return scm_internal_catch(SCM_BOOL_T, get_guile_hook_do, (void *)hook,
|
2005-09-15 09:58:31 -04:00
|
|
|
error_handler, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The events that will trigger the functions below and what they are expected
|
|
|
|
* to do is explained in doc/events.txt */
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_goto_url(va_list ap, void *data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char **url = va_arg(ap, char **);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct session *ses = va_arg(ap, struct session *);
|
|
|
|
SCM proc;
|
|
|
|
SCM x;
|
|
|
|
|
|
|
|
evhook_use_params(url && ses);
|
|
|
|
|
|
|
|
if (*url == NULL || !*url[0]) return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
|
|
|
|
proc = get_guile_hook("%goto-url-hook");
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_false(proc)) return EVENT_HOOK_STATUS_NEXT;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-02-24 15:05:45 -05:00
|
|
|
x = scm_call_1(SCM_VARIABLE_REF(proc), scm_from_locale_string(*url));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_string(x)) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *new_url;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
new_url = stracpy((char *)scm_to_locale_string(x));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (new_url) {
|
|
|
|
mem_free_set(url, new_url);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(*url)[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_follow_url(va_list ap, void *data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char **url = va_arg(ap, char **);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct session *ses = va_arg(ap, struct session *);
|
|
|
|
SCM proc;
|
|
|
|
SCM x;
|
|
|
|
|
|
|
|
evhook_use_params(url && ses);
|
|
|
|
|
|
|
|
if (*url == NULL || !*url[0]) return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
|
|
|
|
proc = get_guile_hook("%follow-url-hook");
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_false(proc)) return EVENT_HOOK_STATUS_NEXT;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-02-24 15:05:45 -05:00
|
|
|
x = scm_call_1(SCM_VARIABLE_REF(proc), scm_from_locale_string(*url));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_string(x)) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *new_url;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
new_url = stracpy((char *)scm_to_locale_string(x));
|
2005-09-15 09:58:31 -04:00
|
|
|
if (new_url) {
|
|
|
|
mem_free_set(url, new_url);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(*url)[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_pre_format_html(va_list ap, void *data)
|
|
|
|
{
|
|
|
|
struct session *ses = va_arg(ap, struct session *);
|
2005-12-17 01:32:08 -05:00
|
|
|
struct cache_entry *cached = va_arg(ap, struct cache_entry *);
|
|
|
|
struct fragment *fragment = get_cache_fragment(cached);
|
2021-01-02 10:20:27 -05:00
|
|
|
char *url = struri(cached->uri), *frag;
|
2018-02-25 11:14:46 -05:00
|
|
|
size_t len;
|
2005-09-15 09:58:31 -04:00
|
|
|
SCM proc;
|
|
|
|
SCM x;
|
|
|
|
|
2005-12-17 01:32:08 -05:00
|
|
|
evhook_use_params(ses && cached);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2005-12-17 01:32:08 -05:00
|
|
|
if (!cached->length || !*fragment->data) return EVENT_HOOK_STATUS_NEXT;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
proc = get_guile_hook("%pre-format-html-hook");
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_false(proc)) return EVENT_HOOK_STATUS_NEXT;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-02-24 15:05:45 -05:00
|
|
|
x = scm_call_2(SCM_VARIABLE_REF(proc), scm_from_locale_string(url),
|
|
|
|
scm_from_locale_stringn(fragment->data, fragment->length));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-02-24 14:18:25 -05:00
|
|
|
if (!scm_is_string(x)) return EVENT_HOOK_STATUS_NEXT;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
frag = (char *)scm_to_locale_stringn(x, &len);
|
2018-02-24 15:05:45 -05:00
|
|
|
add_fragment(cached, 0, frag, len);
|
2005-12-17 01:32:08 -05:00
|
|
|
normalize_cache_entry(cached, len);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The Guile function can return:
|
|
|
|
* - "PROXY:PORT" to use the specified proxy
|
|
|
|
* - "" to not use any proxy
|
|
|
|
* - nil to use the default proxies */
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_get_proxy(va_list ap, void *data)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char **retval = va_arg(ap, char **);
|
|
|
|
char *url = va_arg(ap, char *);
|
2005-09-15 09:58:31 -04:00
|
|
|
SCM proc = get_guile_hook("%get-proxy-hook");
|
|
|
|
SCM x;
|
|
|
|
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_false(proc)) return EVENT_HOOK_STATUS_NEXT;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2018-02-24 15:05:45 -05:00
|
|
|
x = scm_call_1(SCM_VARIABLE_REF(proc), scm_from_locale_string(url));
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
evhook_use_params(retval && url);
|
|
|
|
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_string(x)) {
|
2018-02-24 15:05:45 -05:00
|
|
|
mem_free_set(retval, stracpy(scm_to_locale_string(x)));
|
2018-02-24 14:18:25 -05:00
|
|
|
} else if (scm_is_null(x)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free_set(retval, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum evhook_status
|
|
|
|
script_hook_quit(va_list ap, void *data)
|
|
|
|
{
|
|
|
|
SCM proc = get_guile_hook("%quit-hook");
|
|
|
|
|
2018-02-24 14:18:25 -05:00
|
|
|
if (scm_is_false(proc)) return EVENT_HOOK_STATUS_NEXT;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
scm_call_0(SCM_VARIABLE_REF(proc));
|
|
|
|
|
|
|
|
return EVENT_HOOK_STATUS_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct event_hook_info guile_scripting_hooks[] = {
|
2022-01-30 13:19:20 -05:00
|
|
|
{ "goto-url", 0, script_hook_goto_url, {NULL} },
|
|
|
|
{ "follow-url", 0, script_hook_follow_url, {NULL} },
|
|
|
|
{ "pre-format-html", 0, script_hook_pre_format_html, {NULL} },
|
|
|
|
{ "get-proxy", 0, script_hook_get_proxy, {NULL} },
|
|
|
|
{ "quit", 0, script_hook_quit, {NULL} },
|
2005-09-15 09:58:31 -04:00
|
|
|
NULL_EVENT_HOOK_INFO,
|
|
|
|
};
|