Merge branch 'collectd-4.5'
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  *   Sebastian tokkee Harl <sh at tokkee.org>
22  **/
23
24 #include "collectd.h"
25
26 #include "liboconfig/oconfig.h"
27
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "utils_threshold.h"
33 #include "filter_chain.h"
34
35 #if HAVE_WORDEXP_H
36 # include <wordexp.h>
37 #endif /* HAVE_WORDEXP_H */
38
39 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
40
41 /*
42  * Private types
43  */
44 typedef struct cf_callback
45 {
46         const char  *type;
47         int  (*callback) (const char *, const char *);
48         const char **keys;
49         int    keys_num;
50         struct cf_callback *next;
51 } cf_callback_t;
52
53 typedef struct cf_complex_callback_s
54 {
55         char *type;
56         int (*callback) (oconfig_item_t *);
57         struct cf_complex_callback_s *next;
58 } cf_complex_callback_t;
59
60 typedef struct cf_value_map_s
61 {
62         char *key;
63         int (*func) (const oconfig_item_t *);
64 } cf_value_map_t;
65
66 typedef struct cf_global_option_s
67 {
68         char *key;
69         char *value;
70         char *def;
71 } cf_global_option_t;
72
73 /*
74  * Prototypes of callback functions
75  */
76 static int dispatch_value_typesdb (const oconfig_item_t *ci);
77 static int dispatch_value_plugindir (const oconfig_item_t *ci);
78 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
79
80 /*
81  * Private variables
82  */
83 static cf_callback_t *first_callback = NULL;
84 static cf_complex_callback_t *complex_callback_head = NULL;
85
86 static cf_value_map_t cf_value_map[] =
87 {
88         {"TypesDB",    dispatch_value_typesdb},
89         {"PluginDir",  dispatch_value_plugindir},
90         {"LoadPlugin", dispatch_value_loadplugin}
91 };
92 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
93
94 static cf_global_option_t cf_global_options[] =
95 {
96         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
97         {"PIDFile",     NULL, PIDFILE},
98         {"Hostname",    NULL, NULL},
99         {"FQDNLookup",  NULL, "false"},
100         {"Interval",    NULL, "10"},
101         {"ReadThreads", NULL, "5"}
102 };
103 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
104
105 static int cf_default_typesdb = 1;
106
107 /*
108  * Functions to handle register/unregister, search, and other plugin related
109  * stuff
110  */
111 static cf_callback_t *cf_search (const char *type)
112 {
113         cf_callback_t *cf_cb;
114
115         if (type == NULL)
116                 return (NULL);
117
118         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
119                 if (strcasecmp (cf_cb->type, type) == 0)
120                         break;
121
122         return (cf_cb);
123 }
124
125 static int cf_dispatch (const char *type, const char *orig_key,
126                 const char *orig_value)
127 {
128         cf_callback_t *cf_cb;
129         char *key;
130         char *value;
131         int ret;
132         int i;
133
134         DEBUG ("type = %s, key = %s, value = %s",
135                         ESCAPE_NULL(type),
136                         ESCAPE_NULL(orig_key),
137                         ESCAPE_NULL(orig_value));
138
139         if ((cf_cb = cf_search (type)) == NULL)
140         {
141                 WARNING ("Found a configuration for the `%s' plugin, but "
142                                 "the plugin isn't loaded or didn't register "
143                                 "a configuration callback.", type);
144                 return (-1);
145         }
146
147         if ((key = strdup (orig_key)) == NULL)
148                 return (1);
149         if ((value = strdup (orig_value)) == NULL)
150         {
151                 free (key);
152                 return (2);
153         }
154
155         ret = -1;
156
157         for (i = 0; i < cf_cb->keys_num; i++)
158         {
159                 if (strcasecmp (cf_cb->keys[i], key) == 0)
160                 {
161                         ret = (*cf_cb->callback) (key, value);
162                         break;
163                 }
164         }
165
166         if (i >= cf_cb->keys_num)
167                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
168
169         free (key);
170         free (value);
171
172         DEBUG ("return (%i)", ret);
173
174         return (ret);
175 } /* int cf_dispatch */
176
177 static int dispatch_global_option (const oconfig_item_t *ci)
178 {
179         if (ci->values_num != 1)
180                 return (-1);
181         if (ci->values[0].type == OCONFIG_TYPE_STRING)
182                 return (global_option_set (ci->key, ci->values[0].value.string));
183         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
184         {
185                 char tmp[128];
186                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
187                 return (global_option_set (ci->key, tmp));
188         }
189         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
190         {
191                 if (ci->values[0].value.boolean)
192                         return (global_option_set (ci->key, "true"));
193                 else
194                         return (global_option_set (ci->key, "false"));
195         }
196
197         return (-1);
198 } /* int dispatch_global_option */
199
200 static int dispatch_value_typesdb (const oconfig_item_t *ci)
201 {
202         int i = 0;
203
204         assert (strcasecmp (ci->key, "TypesDB") == 0);
205
206         cf_default_typesdb = 0;
207
208         if (ci->values_num < 1)
209                 return (-1);
210
211         for (i = 0; i < ci->values_num; ++i)
212         {
213                 if (OCONFIG_TYPE_STRING != ci->values[i].type)
214                         continue;
215
216                 read_types_list (ci->values[i].value.string);
217         }
218         return (0);
219 } /* int dispatch_value_typesdb */
220
221 static int dispatch_value_plugindir (const oconfig_item_t *ci)
222 {
223         assert (strcasecmp (ci->key, "PluginDir") == 0);
224         
225         if (ci->values_num != 1)
226                 return (-1);
227         if (ci->values[0].type != OCONFIG_TYPE_STRING)
228                 return (-1);
229
230         plugin_set_dir (ci->values[0].value.string);
231         return (0);
232 }
233
234 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
235 {
236         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
237
238         if (ci->values_num != 1)
239                 return (-1);
240         if (ci->values[0].type != OCONFIG_TYPE_STRING)
241                 return (-1);
242
243         return (plugin_load (ci->values[0].value.string));
244 } /* int dispatch_value_loadplugin */
245
246 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
247 {
248         char  buffer[4096];
249         char *buffer_ptr;
250         int   buffer_free;
251         int i;
252
253         buffer_ptr = buffer;
254         buffer_free = sizeof (buffer);
255
256         for (i = 0; i < ci->values_num; i++)
257         {
258                 int status = -1;
259
260                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
261                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
262                                         ci->values[i].value.string);
263                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
264                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
265                                         ci->values[i].value.number);
266                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
267                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
268                                         ci->values[i].value.boolean
269                                         ? "true" : "false");
270
271                 if ((status < 0) || (status >= buffer_free))
272                         return (-1);
273                 buffer_free -= status;
274                 buffer_ptr  += status;
275         }
276         /* skip the initial space */
277         buffer_ptr = buffer + 1;
278
279         return (cf_dispatch (plugin, ci->key, buffer_ptr));
280 } /* int dispatch_value_plugin */
281
282 static int dispatch_value (const oconfig_item_t *ci)
283 {
284         int ret = -2;
285         int i;
286
287         for (i = 0; i < cf_value_map_num; i++)
288                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
289                 {
290                         ret = cf_value_map[i].func (ci);
291                         break;
292                 }
293
294         for (i = 0; i < cf_global_options_num; i++)
295                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
296                 {
297                         ret = dispatch_global_option (ci);
298                         break;
299                 }
300
301         return (ret);
302 } /* int dispatch_value */
303
304 static int dispatch_block_plugin (oconfig_item_t *ci)
305 {
306         int i;
307         char *name;
308
309         cf_complex_callback_t *cb;
310
311         if (strcasecmp (ci->key, "Plugin") != 0)
312                 return (-1);
313         if (ci->values_num < 1)
314                 return (-1);
315         if (ci->values[0].type != OCONFIG_TYPE_STRING)
316                 return (-1);
317
318         name = ci->values[0].value.string;
319
320         /* Check for a complex callback first */
321         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
322                 if (strcasecmp (name, cb->type) == 0)
323                         return (cb->callback (ci));
324
325         /* Hm, no complex plugin found. Dispatch the values one by one */
326         for (i = 0; i < ci->children_num; i++)
327         {
328                 if (ci->children[i].children == NULL)
329                         dispatch_value_plugin (name, ci->children + i);
330                 else
331                         {DEBUG ("No nested config blocks allowed for this plugin.");}
332         }
333
334         return (0);
335 }
336
337
338 static int dispatch_block (oconfig_item_t *ci)
339 {
340         if (strcasecmp (ci->key, "Plugin") == 0)
341                 return (dispatch_block_plugin (ci));
342         else if (strcasecmp (ci->key, "Threshold") == 0)
343                 return (ut_config (ci));
344         else if (strcasecmp (ci->key, "Chain") == 0)
345                 return (fc_configure (ci));
346
347         return (0);
348 }
349
350 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
351                 int offset)
352 {
353         oconfig_item_t *temp;
354         int i;
355
356         assert (offset >= 0);
357         assert (dst->children_num > offset);
358
359         /* Free the memory used by the replaced child. Usually that's the
360          * `Include "blah"' statement. */
361         temp = dst->children + offset;
362         for (i = 0; i < temp->values_num; i++)
363         {
364                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
365                 {
366                         sfree (temp->values[i].value.string);
367                 }
368         }
369         sfree (temp->values);
370         temp = NULL;
371
372         /* If (src->children_num == 0) the array size is decreased. If offset
373          * is _not_ the last element, (offset < (src->children_num - 1)), then
374          * we need to move the trailing elements before resizing the array. */
375         if ((src->children_num == 0) && (offset < (src->children_num - 1)))
376         {
377                 int nmemb = src->children_num - (offset + 1);
378                 memmove (src->children + offset, src->children + offset + 1,
379                                 sizeof (oconfig_item_t) * nmemb);
380         }
381
382         /* Resize the memory containing the children to be big enough to hold
383          * all children. */
384         temp = (oconfig_item_t *) realloc (dst->children,
385                         sizeof (oconfig_item_t)
386                         * (dst->children_num + src->children_num - 1));
387         if (temp == NULL)
388         {
389                 ERROR ("configfile: realloc failed.");
390                 return (-1);
391         }
392         dst->children = temp;
393
394         /* If there are children behind the include statement, and they have
395          * not yet been moved because (src->children_num == 0), then move them
396          * to the end of the list, so that the new children have room before
397          * them. */
398         if ((src->children_num > 0)
399                         && ((dst->children_num - (offset + 1)) > 0))
400         {
401                 int nmemb = dst->children_num - (offset + 1);
402                 int old_offset = offset + 1;
403                 int new_offset = offset + src->children_num;
404
405                 memmove (dst->children + new_offset,
406                                 dst->children + old_offset,
407                                 sizeof (oconfig_item_t) * nmemb);
408         }
409
410         /* Last but not least: If there are new childrem, copy them to the
411          * memory reserved for them. */
412         if (src->children_num > 0)
413         {
414                 memcpy (dst->children + offset,
415                                 src->children,
416                                 sizeof (oconfig_item_t) * src->children_num);
417         }
418
419         /* Update the number of children. */
420         dst->children_num += (src->children_num - 1);
421
422         return (0);
423 } /* int cf_ci_replace_child */
424
425 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
426 {
427         oconfig_item_t *temp;
428
429         if ((src == NULL) || (src->children_num == 0))
430                 return (0);
431
432         temp = (oconfig_item_t *) realloc (dst->children,
433                         sizeof (oconfig_item_t)
434                         * (dst->children_num + src->children_num));
435         if (temp == NULL)
436         {
437                 ERROR ("configfile: realloc failed.");
438                 return (-1);
439         }
440         dst->children = temp;
441
442         memcpy (dst->children + dst->children_num,
443                         src->children,
444                         sizeof (oconfig_item_t)
445                         * src->children_num);
446         dst->children_num += src->children_num;
447
448         return (0);
449 } /* int cf_ci_append_children */
450
451 #define CF_MAX_DEPTH 8
452 static oconfig_item_t *cf_read_generic (const char *path, int depth);
453
454 static int cf_include_all (oconfig_item_t *root, int depth)
455 {
456         int i;
457
458         for (i = 0; i < root->children_num; i++)
459         {
460                 oconfig_item_t *new;
461                 oconfig_item_t *old;
462
463                 /* Ignore all blocks, including `Include' blocks. */
464                 if (root->children[i].children_num != 0)
465                         continue;
466
467                 if (strcasecmp (root->children[i].key, "Include") != 0)
468                         continue;
469
470                 old = root->children + i;
471
472                 if ((old->values_num != 1)
473                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
474                 {
475                         ERROR ("configfile: `Include' needs exactly one string argument.");
476                         continue;
477                 }
478
479                 new = cf_read_generic (old->values[0].value.string, depth + 1);
480                 if (new == NULL)
481                         continue;
482
483                 /* Now replace the i'th child in `root' with `new'. */
484                 cf_ci_replace_child (root, new, i);
485
486                 sfree (new->values);
487                 sfree (new);
488         } /* for (i = 0; i < root->children_num; i++) */
489
490         return (0);
491 } /* int cf_include_all */
492
493 static oconfig_item_t *cf_read_file (const char *file, int depth)
494 {
495         oconfig_item_t *root;
496
497         assert (depth < CF_MAX_DEPTH);
498
499         root = oconfig_parse_file (file);
500         if (root == NULL)
501         {
502                 ERROR ("configfile: Cannot read file `%s'.", file);
503                 return (NULL);
504         }
505
506         cf_include_all (root, depth);
507
508         return (root);
509 } /* oconfig_item_t *cf_read_file */
510
511 static int cf_compare_string (const void *p1, const void *p2)
512 {
513         return strcmp (*(const char **) p1, *(const char **) p2);
514 }
515
516 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
517 {
518         oconfig_item_t *root = NULL;
519         DIR *dh;
520         struct dirent *de;
521         char **filenames = NULL;
522         int filenames_num = 0;
523         int status;
524         int i;
525
526         assert (depth < CF_MAX_DEPTH);
527
528         dh = opendir (dir);
529         if (dh == NULL)
530         {
531                 char errbuf[1024];
532                 ERROR ("configfile: opendir failed: %s",
533                                 sstrerror (errno, errbuf, sizeof (errbuf)));
534                 return (NULL);
535         }
536
537         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
538         if (root == NULL)
539         {
540                 ERROR ("configfile: malloc failed.");
541                 return (NULL);
542         }
543         memset (root, '\0', sizeof (oconfig_item_t));
544
545         while ((de = readdir (dh)) != NULL)
546         {
547                 char   name[1024];
548                 char **tmp;
549
550                 if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
551                         continue;
552
553                 status = ssnprintf (name, sizeof (name), "%s/%s",
554                                 dir, de->d_name);
555                 if (status >= sizeof (name))
556                 {
557                         ERROR ("configfile: Not including `%s/%s' because its"
558                                         " name is too long.",
559                                         dir, de->d_name);
560                         for (i = 0; i < filenames_num; ++i)
561                                 free (filenames[i]);
562                         free (filenames);
563                         free (root);
564                         return (NULL);
565                 }
566
567                 ++filenames_num;
568                 tmp = (char **) realloc (filenames,
569                                 filenames_num * sizeof (*filenames));
570                 if (tmp == NULL) {
571                         ERROR ("configfile: realloc failed.");
572                         for (i = 0; i < filenames_num - 1; ++i)
573                                 free (filenames[i]);
574                         free (filenames);
575                         free (root);
576                         return (NULL);
577                 }
578                 filenames = tmp;
579
580                 filenames[filenames_num - 1] = sstrdup (name);
581         }
582
583         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
584                         cf_compare_string);
585
586         for (i = 0; i < filenames_num; ++i)
587         {
588                 oconfig_item_t *temp;
589                 char *name = filenames[i];
590
591                 temp = cf_read_generic (name, depth);
592                 if (temp == NULL) {
593                         int j;
594                         for (j = i; j < filenames_num; ++j)
595                                 free (filenames[j]);
596                         free (filenames);
597                         oconfig_free (root);
598                         return (NULL);
599                 }
600
601                 cf_ci_append_children (root, temp);
602                 sfree (temp->children);
603                 sfree (temp);
604
605                 free (name);
606         }
607
608         free(filenames);
609         return (root);
610 } /* oconfig_item_t *cf_read_dir */
611
612 /* 
613  * cf_read_generic
614  *
615  * Path is stat'ed and either cf_read_file or cf_read_dir is called
616  * accordingly.
617  *
618  * There are two versions of this function: If `wordexp' exists shell wildcards
619  * will be expanded and the function will include all matches found. If
620  * `wordexp' (or, more precisely, it's header file) is not available the
621  * simpler function is used which does not do any such expansion.
622  */
623 #if HAVE_WORDEXP_H
624 static oconfig_item_t *cf_read_generic (const char *path, int depth)
625 {
626         oconfig_item_t *root = NULL;
627         int status;
628         const char *path_ptr;
629         wordexp_t we;
630         int i;
631
632         if (depth >= CF_MAX_DEPTH)
633         {
634                 ERROR ("configfile: Not including `%s' because the maximum "
635                                 "nesting depth has been reached.", path);
636                 return (NULL);
637         }
638
639         status = wordexp (path, &we, WRDE_NOCMD);
640         if (status != 0)
641         {
642                 ERROR ("configfile: wordexp (%s) failed.", path);
643                 return (NULL);
644         }
645
646         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
647         if (root == NULL)
648         {
649                 ERROR ("configfile: malloc failed.");
650                 return (NULL);
651         }
652         memset (root, '\0', sizeof (oconfig_item_t));
653
654         /* wordexp() might return a sorted list already. That's not
655          * documented though, so let's make sure we get what we want. */
656         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
657                         cf_compare_string);
658
659         for (i = 0; i < we.we_wordc; i++)
660         {
661                 oconfig_item_t *temp;
662                 struct stat statbuf;
663
664                 path_ptr = we.we_wordv[i];
665
666                 status = stat (path_ptr, &statbuf);
667                 if (status != 0)
668                 {
669                         char errbuf[1024];
670                         ERROR ("configfile: stat (%s) failed: %s",
671                                         path_ptr,
672                                         sstrerror (errno, errbuf, sizeof (errbuf)));
673                         oconfig_free (root);
674                         return (NULL);
675                 }
676
677                 if (S_ISREG (statbuf.st_mode))
678                         temp = cf_read_file (path_ptr, depth);
679                 else if (S_ISDIR (statbuf.st_mode))
680                         temp = cf_read_dir (path_ptr, depth);
681                 else
682                 {
683                         ERROR ("configfile: %s is neither a file nor a "
684                                         "directory.", path);
685                         continue;
686                 }
687
688                 if (temp == NULL) {
689                         oconfig_free (root);
690                         return (NULL);
691                 }
692
693                 cf_ci_append_children (root, temp);
694                 sfree (temp->children);
695                 sfree (temp);
696         }
697
698         wordfree (&we);
699
700         return (root);
701 } /* oconfig_item_t *cf_read_generic */
702 /* #endif HAVE_WORDEXP_H */
703
704 #else /* if !HAVE_WORDEXP_H */
705 static oconfig_item_t *cf_read_generic (const char *path, int depth)
706 {
707         struct stat statbuf;
708         int status;
709
710         if (depth >= CF_MAX_DEPTH)
711         {
712                 ERROR ("configfile: Not including `%s' because the maximum "
713                                 "nesting depth has been reached.", path);
714                 return (NULL);
715         }
716
717         status = stat (path, &statbuf);
718         if (status != 0)
719         {
720                 char errbuf[1024];
721                 ERROR ("configfile: stat (%s) failed: %s",
722                                 path,
723                                 sstrerror (errno, errbuf, sizeof (errbuf)));
724                 return (NULL);
725         }
726
727         if (S_ISREG (statbuf.st_mode))
728                 return (cf_read_file (path, depth));
729         else if (S_ISDIR (statbuf.st_mode))
730                 return (cf_read_dir (path, depth));
731
732         ERROR ("configfile: %s is neither a file nor a directory.", path);
733         return (NULL);
734 } /* oconfig_item_t *cf_read_generic */
735 #endif /* !HAVE_WORDEXP_H */
736
737 /* 
738  * Public functions
739  */
740 int global_option_set (const char *option, const char *value)
741 {
742         int i;
743
744         DEBUG ("option = %s; value = %s;", option, value);
745
746         for (i = 0; i < cf_global_options_num; i++)
747                 if (strcasecmp (cf_global_options[i].key, option) == 0)
748                         break;
749
750         if (i >= cf_global_options_num)
751                 return (-1);
752
753         sfree (cf_global_options[i].value);
754
755         if (value != NULL)
756                 cf_global_options[i].value = strdup (value);
757         else
758                 cf_global_options[i].value = NULL;
759
760         return (0);
761 }
762
763 const char *global_option_get (const char *option)
764 {
765         int i;
766
767         for (i = 0; i < cf_global_options_num; i++)
768                 if (strcasecmp (cf_global_options[i].key, option) == 0)
769                         break;
770
771         if (i >= cf_global_options_num)
772                 return (NULL);
773         
774         return ((cf_global_options[i].value != NULL)
775                         ? cf_global_options[i].value
776                         : cf_global_options[i].def);
777 } /* char *global_option_get */
778
779 void cf_unregister (const char *type)
780 {
781         cf_callback_t *this, *prev;
782
783         for (prev = NULL, this = first_callback;
784                         this != NULL;
785                         prev = this, this = this->next)
786                 if (strcasecmp (this->type, type) == 0)
787                 {
788                         if (prev == NULL)
789                                 first_callback = this->next;
790                         else
791                                 prev->next = this->next;
792
793                         free (this);
794                         break;
795                 }
796 } /* void cf_unregister */
797
798 void cf_unregister_complex (const char *type)
799 {
800         cf_complex_callback_t *this, *prev;
801
802         for (prev = NULL, this = complex_callback_head;
803                         this != NULL;
804                         prev = this, this = this->next)
805                 if (strcasecmp (this->type, type) == 0)
806                 {
807                         if (prev == NULL)
808                                 complex_callback_head = this->next;
809                         else
810                                 prev->next = this->next;
811
812                         sfree (this->type);
813                         sfree (this);
814                         break;
815                 }
816 } /* void cf_unregister */
817
818 void cf_register (const char *type,
819                 int (*callback) (const char *, const char *),
820                 const char **keys, int keys_num)
821 {
822         cf_callback_t *cf_cb;
823
824         /* Remove this module from the list, if it already exists */
825         cf_unregister (type);
826
827         /* This pointer will be free'd in `cf_unregister' */
828         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
829                 return;
830
831         cf_cb->type     = type;
832         cf_cb->callback = callback;
833         cf_cb->keys     = keys;
834         cf_cb->keys_num = keys_num;
835
836         cf_cb->next = first_callback;
837         first_callback = cf_cb;
838 } /* void cf_register */
839
840 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
841 {
842         cf_complex_callback_t *new;
843
844         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
845         if (new == NULL)
846                 return (-1);
847
848         new->type = strdup (type);
849         if (new->type == NULL)
850         {
851                 sfree (new);
852                 return (-1);
853         }
854
855         new->callback = callback;
856         new->next = NULL;
857
858         if (complex_callback_head == NULL)
859         {
860                 complex_callback_head = new;
861         }
862         else
863         {
864                 cf_complex_callback_t *last = complex_callback_head;
865                 while (last->next != NULL)
866                         last = last->next;
867                 last->next = new;
868         }
869
870         return (0);
871 } /* int cf_register_complex */
872
873 int cf_read (char *filename)
874 {
875         oconfig_item_t *conf;
876         int i;
877
878         conf = cf_read_generic (filename, 0 /* depth */);
879         if (conf == NULL)
880         {
881                 ERROR ("Unable to read config file %s.", filename);
882                 return (-1);
883         }
884
885         for (i = 0; i < conf->children_num; i++)
886         {
887                 if (conf->children[i].children == NULL)
888                         dispatch_value (conf->children + i);
889                 else
890                         dispatch_block (conf->children + i);
891         }
892
893         oconfig_free (conf);
894
895         /* Read the default types.db if no `TypesDB' option was given. */
896         if (cf_default_typesdb)
897                 read_types_list (PKGDATADIR"/types.db");
898
899         return (0);
900 } /* int cf_read */