[elinks-dev] Add a data pointer to struct event_handler and event_hook.

Karsten Schölzel kuser at gmx.de
Wed Oct 1 02:05:26 PDT 2003


The first patch adds a data pointer to event_handler and event_hook,
which can be set through register_event_hook to save a hook specific
data pointer. It seems especially useful for binding keys from scripting
backends, e.g. if you register one callback to two different events, you
can distinguish both events with the data pointer, if you set them to
different values.

The second patch includes build fixes for guile backend (applies only
after the first one :( ).

Karsten Schölzel

PS: enabling guile and lua backends concurrently leads to memleaks :(
-------------- next part --------------
Index: src/sched/event.c
===================================================================
--- src/sched/event.c	(revision 353)
+++ src/sched/event.c	(revision 354)
@@ -35,6 +35,8 @@
 
 	/* The @priority of this handler. */
 	int priority;
+
+	void *data;
 };
 
 struct event {
@@ -161,7 +163,7 @@
 		enum evhook_status ret;
 
 		va_start(ap, id);
-		ret = ev_handler->callback(ap);
+		ret = ev_handler->callback(ap, ev_handler->data);
 		va_end(ap);
 
 		if (ret == EHS_LAST) return;
@@ -178,7 +180,7 @@
 }
 
 int
-register_event_hook(int id, event_hook callback, int priority)
+register_event_hook(int id, event_hook callback, int priority, void *data)
 {
 	struct event *event;
 	register int i;
@@ -214,6 +216,7 @@
 
 	event->handlers[i].callback = callback;
 	event->handlers[i].priority = priority;
+	event->handlers[i].data = data;
 
 	return id;
 }
Index: src/sched/event.h
===================================================================
--- src/sched/event.h	(revision 353)
+++ src/sched/event.h	(revision 354)
@@ -41,7 +41,7 @@
 };
 
 /* The event hook prototype. Abide. */
-typedef enum evhook_status (*event_hook)(va_list ap);
+typedef enum evhook_status (*event_hook)(va_list ap, void *data);
 
 
 /*** The life of events */
@@ -55,7 +55,7 @@
  * further recyclation. */
 int unregister_event(int event);
 
-int register_event_hook(int id, event_hook callback, int priority);
+int register_event_hook(int id, event_hook callback, int priority, void *data);
 
 void unregister_event_hook(int id, event_hook callback);
 
Index: src/scripting/lua/hooks.c
===================================================================
--- src/scripting/lua/hooks.c	(revision 353)
+++ src/scripting/lua/hooks.c	(revision 354)
@@ -21,7 +21,7 @@
  * to do is explained in doc/events.txt */
 
 static enum evhook_status
-script_hook_goto_url(va_list ap)
+script_hook_goto_url(va_list ap, void *data)
 {
 	lua_State *L = lua_state;
 	unsigned char **url = va_arg(ap, unsigned char **);
@@ -65,7 +65,7 @@
 }
 
 static enum evhook_status
-script_hook_follow_url(va_list ap)
+script_hook_follow_url(va_list ap, void *data)
 {
 	lua_State *L = lua_state;
 	unsigned char **url = va_arg(ap, unsigned char **);
@@ -103,7 +103,7 @@
 }
 
 static enum evhook_status
-script_hook_pre_format_html(va_list ap)
+script_hook_pre_format_html(va_list ap, void *data)
 {
 	lua_State *L = lua_state;
 	unsigned char **html = va_arg(ap, unsigned char **);
@@ -147,7 +147,7 @@
  *  - ""           to not use any proxy
  *  - nil          to use the default proxies */
 static enum evhook_status
-script_hook_get_proxy(va_list ap)
+script_hook_get_proxy(va_list ap, void *data)
 {
 	lua_State *L = lua_state;
 	unsigned char **new_proxy_url = va_arg(ap, unsigned char **);
@@ -184,7 +184,7 @@
 }
 
 static enum evhook_status
-script_hook_quit(va_list ap)
+script_hook_quit(va_list ap, void *data)
 {
 	if (!prepare_lua(NULL)) {
 		lua_dostring(lua_state, "if quit_hook then quit_hook() end");
@@ -195,12 +195,12 @@
 }
 
 struct scripting_hook lua_scripting_hooks[] = {
-	{ "goto-url", script_hook_goto_url },
-	{ "follow-url", script_hook_follow_url },
-	{ "pre-format-html", script_hook_pre_format_html },
-	{ "get-proxy", script_hook_get_proxy },
-	{ "quit", script_hook_quit },
-	{ NULL, NULL }
+	{ "goto-url", script_hook_goto_url, NULL },
+	{ "follow-url", script_hook_follow_url, NULL },
+	{ "pre-format-html", script_hook_pre_format_html, NULL },
+	{ "get-proxy", script_hook_get_proxy, NULL },
+	{ "quit", script_hook_quit, NULL },
+	{ NULL, NULL, NULL }
 };
 
 #endif
Index: src/scripting/scripting.c
===================================================================
--- src/scripting/scripting.c	(revision 353)
+++ src/scripting/scripting.c	(revision 354)
@@ -38,7 +38,7 @@
 
 		if (id == EVENT_NONE) continue;
 
-		register_event_hook(id, hooks[i].callback, 0);
+		register_event_hook(id, hooks[i].callback, 0, hooks[i].data);
 	}
 }
 
Index: src/scripting/guile/hooks.c
===================================================================
--- src/scripting/guile/hooks.c	(revision 353)
+++ src/scripting/guile/hooks.c	(revision 354)
@@ -29,7 +29,7 @@
  * to do is explained in doc/events.txt */
 
 static int
-script_hook_goto_url(va_list ap)
+script_hook_goto_url(va_list ap, void *data)
 {
 	unsigned char **url = va_arg(ap, unsigned char **);
 	struct session *ses = va_arg(ap, struct session *);
@@ -51,7 +51,7 @@
 }
 
 static int
-script_hook_follow_url(va_list ap)
+script_hook_follow_url(va_list ap, void *data)
 {
 	unsigned char **url = va_arg(ap, unsigned char **);
 	struct session *ses = va_arg(ap, struct session *);
@@ -72,7 +72,7 @@
 }
 
 static int
-script_hook_pre_format_html(va_list ap)
+script_hook_pre_format_html(va_list ap, void *data)
 {
 	unsigned char **html = va_arg(ap, unsigned char **);
 	int *html_len = va_arg(ap, int *);
@@ -100,7 +100,7 @@
  *  - ""           to not use any proxy
  *  - nil          to use the default proxies */
 static int
-script_hook_get_proxy(va_list ap)
+script_hook_get_proxy(va_list ap, void *data)
 {
 	unsigned char **retval = va_arg(ap, unsigned char **);
 	unsigned char *url = va_arg(ap, unsigned char *);
@@ -117,7 +117,7 @@
 }
 
 static int
-script_hook_quit(va_list ap)
+script_hook_quit(va_list ap, void *data)
 {
 	SCM proc = scm_c_module_lookup(internal_module(), "%quit-hook");
 
@@ -127,12 +127,12 @@
 }
 
 struct scripting_hook guile_scripting_hooks[] = {
-	{ "goto-url", script_hook_goto_url },
-	{ "follow-url", script_hook_follow_url },
-	{ "pre-format-html", script_hook_pre_format_html },
-	{ "get-proxy", script_hook_get_proxy },
-	{ "quit", script_hook_quit },
-	{ NULL, NULL }
+	{ "goto-url", script_hook_goto_url, NULL },
+	{ "follow-url", script_hook_follow_url, NULL },
+	{ "pre-format-html", script_hook_pre_format_html, NULL },
+	{ "get-proxy", script_hook_get_proxy, NULL },
+	{ "quit", script_hook_quit, NULL },
+	{ NULL, NULL, NULL }
 };
 
 #endif /* HAVE_GUILE */
Index: src/scripting/scripting.h
===================================================================
--- src/scripting/scripting.h	(revision 353)
+++ src/scripting/scripting.h	(revision 354)
@@ -10,6 +10,7 @@
 struct scripting_hook {
 	unsigned char *name;
 	event_hook callback;
+	void *data;
 };
 
 struct scripting_backend {
-------------- next part --------------
Index: src/scripting/guile/hooks.c
===================================================================
--- src/scripting/guile/hooks.c	(revision 354)
+++ src/scripting/guile/hooks.c	(revision 355)
@@ -28,11 +28,10 @@
 /* The events that will trigger the functions below and what they are expected
  * to do is explained in doc/events.txt */
 
-static int
+static enum evhook_status
 script_hook_goto_url(va_list ap, void *data)
 {
 	unsigned char **url = va_arg(ap, unsigned char **);
-	struct session *ses = va_arg(ap, struct session *);
 	SCM proc;
 	SCM x;
 
@@ -50,11 +49,10 @@
 	return EHS_NEXT;
 }
 
-static int
+static enum evhook_status
 script_hook_follow_url(va_list ap, void *data)
 {
 	unsigned char **url = va_arg(ap, unsigned char **);
-	struct session *ses = va_arg(ap, struct session *);
 	SCM proc;
 	SCM x;
 
@@ -71,7 +69,7 @@
 	return EHS_NEXT;
 }
 
-static int
+static enum evhook_status
 script_hook_pre_format_html(va_list ap, void *data)
 {
 	unsigned char **html = va_arg(ap, unsigned char **);
@@ -81,11 +79,12 @@
 	SCM proc;
 	SCM x;
 
+	if (0 && ses);
 	if (*html == NULL || *html_len == 0) return EHS_NEXT;
 
 	proc = scm_c_module_lookup(internal_module(), "%pre-format-html-hook");
 	x = scm_call_2(SCM_VARIABLE_REF(proc), scm_makfrom0str(url),
-		       scm_mem2string(*html, *len));
+		       scm_mem2string(*html, *html_len));
 
 	if (!SCM_STRINGP(x)) return EHS_NEXT;
 
@@ -99,7 +98,7 @@
  *  - "PROXY:PORT" to use the specified proxy
  *  - ""           to not use any proxy
  *  - nil          to use the default proxies */
-static int
+static enum evhook_status
 script_hook_get_proxy(va_list ap, void *data)
 {
 	unsigned char **retval = va_arg(ap, unsigned char **);
@@ -116,7 +115,7 @@
 	return EHS_NEXT;
 }
 
-static int
+static enum evhook_status
 script_hook_quit(va_list ap, void *data)
 {
 	SCM proc = scm_c_module_lookup(internal_module(), "%quit-hook");
Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(revision 354)
+++ src/Makefile.am	(revision 355)
@@ -52,9 +52,9 @@
  lowlevel/liblowlevel.a \
  terminal/libterminal.a \
  mime/libmime.a \
+ scripting/libscripting.a \
  scripting/guile/.libs/libscriptingguile.a \
  scripting/lua/.libs/libscriptinglua.a \
- scripting/libscripting.a \
  osdep/libosdep.a \
  protocol/libprotocol.a \
  protocol/http/libhttp.a \


More information about the elinks-dev mailing list