Moved event.c/h
[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.2 2002/05/02 11:50:07 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   return strcmp(((const struct hooks_node*)a)->type,
43                 ((const struct hooks_node*)b)->type);
44 }
45
46 static int hook_dummy_compare(const void *a, const void *b)
47 {
48   if(a < b)
49     return -1;
50   if(a > b)
51     return 1;
52   return 0;
53 }
54
55 void run_hooks(const char *type, ...)
56 {
57   avl_node_t *avlnode;
58   va_list args;
59   struct hooks_node *hn;
60   struct hooks_node target;
61
62   if(!hooks_tree)
63     return;
64   
65   target.type = type;
66   hn = (struct hooks_node*)avl_search(hooks_tree, &target);
67   if(!hn || !(hn->hooks->head))
68     {
69       fprintf(stderr, "Warning, no hooks found for `%s'\n", type);
70       return;
71     }
72
73   va_start(args, type);
74   for(avlnode = hn->hooks->head; avlnode; avlnode = avlnode->next)
75     {
76       assert(avlnode->data);
77       ((hook_function_t*)(avlnode->data))(type, args);
78     }
79   va_end(args);
80 }
81
82 void add_hook(const char *type, hook_function_t *hook)
83 {
84   struct hooks_node *hn;
85   struct hooks_node target;
86   
87   if(!hooks_tree)
88     hooks_tree = avl_alloc_tree(hook_type_compare, NULL);
89
90   target.type = type;
91   hn = avl_search(hooks_tree, &target);
92   if(!hn)
93     {
94       avl_tree_t *t;
95       
96       hn = xmalloc(sizeof(struct hooks_node));
97       t = avl_alloc_tree(hook_dummy_compare, NULL);
98       hn->type = type;
99       hn->hooks = t;
100       avl_insert(hooks_tree, (void*)hn);
101     }
102
103   avl_insert(hn->hooks, (void*)hook);
104 }
105
106 void del_hook(const char *type, hook_function_t *hook)
107 {
108   avl_tree_t *t;
109   struct hooks_node target;
110
111   if(!hooks_tree)
112     return;
113
114   target.type = type;
115   t = avl_search(hooks_tree, &target);
116   if(!t)
117     return;
118
119   avl_delete(t, (void*)hook);
120 }