Initial import (again)
[srvx.git] / src / conf.c
1 /* conf.c - Config file reader
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.  Important limitations are
8  * listed in the COPYING file that accompanies this software.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, email srvx-maintainers@srvx.net.
17  */
18
19 #include "conf.h"
20 #include "log.h"
21
22 static dict_t conf_db;
23 static conf_reload_func *reload_funcs;
24 static int num_rfs, size_rfs;
25
26 void
27 conf_register_reload(conf_reload_func crf)
28 {
29     if (num_rfs >= size_rfs) {
30         if (reload_funcs) {
31             size_rfs <<= 1;
32             reload_funcs = realloc(reload_funcs, size_rfs*sizeof(conf_reload_func));
33         } else {
34             size_rfs = 8;
35             reload_funcs = calloc(size_rfs, sizeof(conf_reload_func));
36         }
37     }
38     reload_funcs[num_rfs++] = crf;
39     if (conf_db) {
40         crf();
41     }
42 }
43
44 void
45 conf_call_reload_funcs(void)
46 {
47     int i;
48     for (i=0; i<num_rfs; i++) reload_funcs[i]();
49 }
50
51 int
52 conf_read(const char *conf_file_name)
53 {
54     dict_t old_conf = conf_db;
55     if (!(conf_db = parse_database(conf_file_name))) {
56         goto fail;
57     }
58     if (reload_funcs) {
59         conf_call_reload_funcs();
60     }
61     if (old_conf && old_conf != conf_db) {
62         free_database(old_conf);
63     }
64     return 1;
65
66 fail:
67     log_module(MAIN_LOG, LOG_ERROR, "Reverting to previous configuration.");
68     free_database(conf_db);
69     conf_db = old_conf;
70     return 0;
71 }
72
73 void
74 conf_close(void)
75 {
76     free_database(conf_db);
77     free(reload_funcs);
78 }
79
80 struct record_data *
81 conf_get_node(const char *full_path)
82 {
83     return database_get_path(conf_db, full_path);
84 }
85
86 void *
87 conf_get_data(const char *full_path, enum recdb_type type)
88 {
89     return database_get_data(conf_db, full_path, type);
90 }
91
92 const char*
93 conf_enum_root(dict_iterator_f it, void *extra)
94 {
95     return dict_foreach(conf_db, it, extra);
96 }