Several fixes; store hook functions in a struct together with the hook
[tinc] / lib / hooks.c
1 /*
2     hooks.c -- hooks management
3     Copyright (C) 2002 Guus Sliepen <guus@sliepen.warande.net>,
4                   2002 Ivo Timmermans <ivo@o2w.nl>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: hooks.c,v 1.1.2.3 2002/04/16 21:24:55 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include <avl_tree.h>
30 #include <hooks.h>
31 #include <xalloc.h>
32
33 avl_tree_t *hooks_tree = NULL;
34
35 struct hooks_node {
36   const char* type;
37   avl_tree_t *hooks;
38 } hooks_node;
39
40 static int hook_type_compare(const void *a, const void *b)
41 {
42   /* This is not exactly how AVL was meant to be used, but since we
43      only use a limited number of functions, I do this anyway.  Assume
44      the left parameter (a) is the value we give as target, the right
45      (b) will be the value in the data pointer of an avl_node_t
46      struct, which is a struct hooks_node in our case. */
47   /* So you should FIXME. */
48   return strcmp((const char*)a,
49                 /* ((const struct hooks_node*)a)->type, */
50                 ((const struct hooks_node*)b)->type);
51 }
52
53 static int hook_dummy_compare(const void *a, const void *b)
54 {
55   if(a < b)
56     return -1;
57   if(a > b)
58     return 1;
59   return 0;
60 }
61
62 void run_hooks(const char *type, ...)
63 {
64   avl_node_t *avlnode;
65   va_list args;
66   struct hooks_node *hn;
67
68   if(!hooks_tree)
69     return;
70   hn = (struct hooks_node*)avl_search(hooks_tree, type);
71   if(!hn)
72     {
73       fprintf(stderr, "Warning, no hooks found for `%s'\n", type);
74       return;
75     }
76
77   va_start(args, type);
78   for(avlnode = hn->hooks->head; avlnode; avlnode = avlnode->next)
79     {
80       assert(avlnode->data);
81       ((hook_function_t*)(avlnode->data))(type, args);
82     }
83   va_end(args);
84 }
85
86 void add_hook(const char *type, hook_function_t *hook)
87 {
88   struct hooks_node *hn;
89   
90   if(!hooks_tree)
91     hooks_tree = avl_alloc_tree(hook_type_compare, NULL);
92
93   hn = avl_search(hooks_tree, type);
94   if(!hn)
95     {
96       avl_tree_t *t;
97       
98       hn = xmalloc(sizeof(struct hooks_node));
99       t = avl_alloc_tree(hook_dummy_compare, NULL);
100       hn->type = type;
101       hn->hooks = t;
102       avl_insert(hooks_tree, (void*)hn);
103     }
104
105   avl_insert(hn->hooks, (void*)hook);
106 }
107
108 void del_hook(const char *type, hook_function_t *hook)
109 {
110   avl_tree_t *t;
111
112   if(!hooks_tree)
113     return;
114
115   t = avl_search(hooks_tree, type);
116   if(!t)
117     return;
118
119   avl_delete(t, (void*)hook);
120 }