*** empty log message ***
[oweals/tinc.git] / 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.2 2002/04/16 17:20:46 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27
28 #include <avl_tree.h>
29 #include <hooks.h>
30
31 avl_tree_t *hooks_tree = NULL;
32
33 static int hook_type_compare(const void *a, const void *b)
34 {
35   return strcmp((const char*)a, (const char*)b);
36 }
37
38 static int hook_dummy_compare(const void *a, const void *b)
39 {
40   if(a < b)
41     return -1;
42   if(a > b)
43     return 1;
44   return 0;
45 }
46
47 void run_hooks(const char *type, ...)
48 {
49   avl_node_t *avlnode;
50   va_list args;
51   avl_tree_t *t;
52
53   if(!hooks_tree)
54     return;
55   t = avl_search(hooks_tree, type);
56   if(!t)
57     return;
58
59   va_start(args, type);
60   for(avlnode = t->head; avlnode; avlnode = avlnode->next)
61     {
62       assert(avlnode->data);
63       ((hook_function_t*)(avlnode->data))(type, args);
64     }
65   va_end(args);
66 }
67
68 void add_hook(const char *type, hook_function_t *hook)
69 {
70   avl_tree_t *t;
71   
72   if(!hooks_tree)
73     hooks_tree = avl_alloc_tree(hook_type_compare, NULL);
74
75   t = avl_search(hooks_tree, type);
76   if(!t)
77     {
78       t = avl_alloc_tree(hook_dummy_compare, NULL);
79       avl_insert(log_hooks_tree, (void*)t);
80     }
81   
82   avl_insert(t, (void*)hook);
83 }
84
85 void del_hook(const char *type, hook_function_t *hook)
86 {
87   avl_tree_t *t;
88
89   if(!hooks_tree)
90     return;
91
92   t = avl_search(hooks_tree, type);
93   if(!t)
94     return;
95
96   avl_delete(t, (void*)hook);
97 }