src/configfile.[ch]: Implement "cf_util_get_flag".
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2010  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_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_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, "true"},
100         {"Interval",    NULL, "10"},
101         {"ReadThreads", NULL, "5"},
102         {"Timeout",     NULL, "2"},
103         {"PreCacheChain",  NULL, "PreCache"},
104         {"PostCacheChain", NULL, "PostCache"}
105 };
106 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
107
108 static int cf_default_typesdb = 1;
109
110 /*
111  * Functions to handle register/unregister, search, and other plugin related
112  * stuff
113  */
114 static cf_callback_t *cf_search (const char *type)
115 {
116         cf_callback_t *cf_cb;
117
118         if (type == NULL)
119                 return (NULL);
120
121         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
122                 if (strcasecmp (cf_cb->type, type) == 0)
123                         break;
124
125         return (cf_cb);
126 }
127
128 static int cf_dispatch (const char *type, const char *orig_key,
129                 const char *orig_value)
130 {
131         cf_callback_t *cf_cb;
132         char *key;
133         char *value;
134         int ret;
135         int i;
136
137         DEBUG ("type = %s, key = %s, value = %s",
138                         ESCAPE_NULL(type),
139                         ESCAPE_NULL(orig_key),
140                         ESCAPE_NULL(orig_value));
141
142         if ((cf_cb = cf_search (type)) == NULL)
143         {
144                 WARNING ("Found a configuration for the `%s' plugin, but "
145                                 "the plugin isn't loaded or didn't register "
146                                 "a configuration callback.", type);
147                 return (-1);
148         }
149
150         if ((key = strdup (orig_key)) == NULL)
151                 return (1);
152         if ((value = strdup (orig_value)) == NULL)
153         {
154                 free (key);
155                 return (2);
156         }
157
158         ret = -1;
159
160         for (i = 0; i < cf_cb->keys_num; i++)
161         {
162                 if ((cf_cb->keys[i] != NULL)
163                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
164                 {
165                         ret = (*cf_cb->callback) (key, value);
166                         break;
167                 }
168         }
169
170         if (i >= cf_cb->keys_num)
171                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
172
173         free (key);
174         free (value);
175
176         DEBUG ("cf_dispatch: return (%i)", ret);
177
178         return (ret);
179 } /* int cf_dispatch */
180
181 static int dispatch_global_option (const oconfig_item_t *ci)
182 {
183         if (ci->values_num != 1)
184                 return (-1);
185         if (ci->values[0].type == OCONFIG_TYPE_STRING)
186                 return (global_option_set (ci->key, ci->values[0].value.string));
187         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
188         {
189                 char tmp[128];
190                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
191                 return (global_option_set (ci->key, tmp));
192         }
193         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
194         {
195                 if (ci->values[0].value.boolean)
196                         return (global_option_set (ci->key, "true"));
197                 else
198                         return (global_option_set (ci->key, "false"));
199         }
200
201         return (-1);
202 } /* int dispatch_global_option */
203
204 static int dispatch_value_typesdb (const oconfig_item_t *ci)
205 {
206         int i = 0;
207
208         assert (strcasecmp (ci->key, "TypesDB") == 0);
209
210         cf_default_typesdb = 0;
211
212         if (ci->values_num < 1) {
213                 ERROR ("configfile: `TypesDB' needs at least one argument.");
214                 return (-1);
215         }
216
217         for (i = 0; i < ci->values_num; ++i)
218         {
219                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
220                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
221                                         "is not a string.", i + 1);
222                         continue;
223                 }
224
225                 read_types_list (ci->values[i].value.string);
226         }
227         return (0);
228 } /* int dispatch_value_typesdb */
229
230 static int dispatch_value_plugindir (const oconfig_item_t *ci)
231 {
232         assert (strcasecmp (ci->key, "PluginDir") == 0);
233         
234         if (ci->values_num != 1)
235                 return (-1);
236         if (ci->values[0].type != OCONFIG_TYPE_STRING)
237                 return (-1);
238
239         plugin_set_dir (ci->values[0].value.string);
240         return (0);
241 }
242
243 static int dispatch_loadplugin (const oconfig_item_t *ci)
244 {
245         int i;
246         uint32_t flags = 0;
247         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
248
249         if (ci->values_num != 1)
250                 return (-1);
251         if (ci->values[0].type != OCONFIG_TYPE_STRING)
252                 return (-1);
253
254         for (i = 0; i < ci->children_num; ++i) {
255                 if (ci->children[i].values_num != 1 ||
256                                 ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) {
257                         WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
258                         continue;
259                 }
260                 if (strcasecmp(ci->children[i].key, "globals") == 0) {
261                         flags |= PLUGIN_FLAGS_GLOBAL;
262                 } else {
263                         WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
264                 }
265         }
266         return (plugin_load (ci->values[0].value.string, flags));
267 } /* int dispatch_value_loadplugin */
268
269 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
270 {
271         char  buffer[4096];
272         char *buffer_ptr;
273         int   buffer_free;
274         int i;
275
276         buffer_ptr = buffer;
277         buffer_free = sizeof (buffer);
278
279         for (i = 0; i < ci->values_num; i++)
280         {
281                 int status = -1;
282
283                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
284                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
285                                         ci->values[i].value.string);
286                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
287                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
288                                         ci->values[i].value.number);
289                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
290                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
291                                         ci->values[i].value.boolean
292                                         ? "true" : "false");
293
294                 if ((status < 0) || (status >= buffer_free))
295                         return (-1);
296                 buffer_free -= status;
297                 buffer_ptr  += status;
298         }
299         /* skip the initial space */
300         buffer_ptr = buffer + 1;
301
302         return (cf_dispatch (plugin, ci->key, buffer_ptr));
303 } /* int dispatch_value_plugin */
304
305 static int dispatch_value (const oconfig_item_t *ci)
306 {
307         int ret = -2;
308         int i;
309
310         for (i = 0; i < cf_value_map_num; i++)
311                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
312                 {
313                         ret = cf_value_map[i].func (ci);
314                         break;
315                 }
316
317         for (i = 0; i < cf_global_options_num; i++)
318                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
319                 {
320                         ret = dispatch_global_option (ci);
321                         break;
322                 }
323
324         return (ret);
325 } /* int dispatch_value */
326
327 static int dispatch_block_plugin (oconfig_item_t *ci)
328 {
329         int i;
330         char *name;
331
332         cf_complex_callback_t *cb;
333
334         if (strcasecmp (ci->key, "Plugin") != 0)
335                 return (-1);
336         if (ci->values_num < 1)
337                 return (-1);
338         if (ci->values[0].type != OCONFIG_TYPE_STRING)
339                 return (-1);
340
341         name = ci->values[0].value.string;
342
343         /* Check for a complex callback first */
344         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
345                 if (strcasecmp (name, cb->type) == 0)
346                         return (cb->callback (ci));
347
348         /* Hm, no complex plugin found. Dispatch the values one by one */
349         for (i = 0; i < ci->children_num; i++)
350         {
351                 if (ci->children[i].children == NULL)
352                         dispatch_value_plugin (name, ci->children + i);
353                 else
354                 {
355                         WARNING ("There is a `%s' block within the "
356                                         "configuration for the %s plugin. "
357                                         "The plugin either only expects "
358                                         "\"simple\" configuration statements "
359                                         "or wasn't loaded using `LoadPlugin'."
360                                         " Please check your configuration.",
361                                         ci->children[i].key, name);
362                 }
363         }
364
365         return (0);
366 }
367
368
369 static int dispatch_block (oconfig_item_t *ci)
370 {
371         if (strcasecmp (ci->key, "LoadPlugin") == 0)
372                 return (dispatch_loadplugin (ci));
373         else if (strcasecmp (ci->key, "Plugin") == 0)
374                 return (dispatch_block_plugin (ci));
375         else if (strcasecmp (ci->key, "Threshold") == 0)
376                 return (ut_config (ci));
377         else if (strcasecmp (ci->key, "Chain") == 0)
378                 return (fc_configure (ci));
379
380         return (0);
381 }
382
383 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
384                 int offset)
385 {
386         oconfig_item_t *temp;
387         int i;
388
389         assert (offset >= 0);
390         assert (dst->children_num > offset);
391
392         /* Free the memory used by the replaced child. Usually that's the
393          * `Include "blah"' statement. */
394         temp = dst->children + offset;
395         for (i = 0; i < temp->values_num; i++)
396         {
397                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
398                 {
399                         sfree (temp->values[i].value.string);
400                 }
401         }
402         sfree (temp->values);
403         temp = NULL;
404
405         /* If (src->children_num == 0) the array size is decreased. If offset
406          * is _not_ the last element, (offset < (dst->children_num - 1)), then
407          * we need to move the trailing elements before resizing the array. */
408         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
409         {
410                 int nmemb = dst->children_num - (offset + 1);
411                 memmove (dst->children + offset, dst->children + offset + 1,
412                                 sizeof (oconfig_item_t) * nmemb);
413         }
414
415         /* Resize the memory containing the children to be big enough to hold
416          * all children. */
417         temp = (oconfig_item_t *) realloc (dst->children,
418                         sizeof (oconfig_item_t)
419                         * (dst->children_num + src->children_num - 1));
420         if (temp == NULL)
421         {
422                 ERROR ("configfile: realloc failed.");
423                 return (-1);
424         }
425         dst->children = temp;
426
427         /* If there are children behind the include statement, and they have
428          * not yet been moved because (src->children_num == 0), then move them
429          * to the end of the list, so that the new children have room before
430          * them. */
431         if ((src->children_num > 0)
432                         && ((dst->children_num - (offset + 1)) > 0))
433         {
434                 int nmemb = dst->children_num - (offset + 1);
435                 int old_offset = offset + 1;
436                 int new_offset = offset + src->children_num;
437
438                 memmove (dst->children + new_offset,
439                                 dst->children + old_offset,
440                                 sizeof (oconfig_item_t) * nmemb);
441         }
442
443         /* Last but not least: If there are new children, copy them to the
444          * memory reserved for them. */
445         if (src->children_num > 0)
446         {
447                 memcpy (dst->children + offset,
448                                 src->children,
449                                 sizeof (oconfig_item_t) * src->children_num);
450         }
451
452         /* Update the number of children. */
453         dst->children_num += (src->children_num - 1);
454
455         return (0);
456 } /* int cf_ci_replace_child */
457
458 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
459 {
460         oconfig_item_t *temp;
461
462         if ((src == NULL) || (src->children_num == 0))
463                 return (0);
464
465         temp = (oconfig_item_t *) realloc (dst->children,
466                         sizeof (oconfig_item_t)
467                         * (dst->children_num + src->children_num));
468         if (temp == NULL)
469         {
470                 ERROR ("configfile: realloc failed.");
471                 return (-1);
472         }
473         dst->children = temp;
474
475         memcpy (dst->children + dst->children_num,
476                         src->children,
477                         sizeof (oconfig_item_t)
478                         * src->children_num);
479         dst->children_num += src->children_num;
480
481         return (0);
482 } /* int cf_ci_append_children */
483
484 #define CF_MAX_DEPTH 8
485 static oconfig_item_t *cf_read_generic (const char *path, int depth);
486
487 static int cf_include_all (oconfig_item_t *root, int depth)
488 {
489         int i;
490
491         for (i = 0; i < root->children_num; i++)
492         {
493                 oconfig_item_t *new;
494                 oconfig_item_t *old;
495
496                 /* Ignore all blocks, including `Include' blocks. */
497                 if (root->children[i].children_num != 0)
498                         continue;
499
500                 if (strcasecmp (root->children[i].key, "Include") != 0)
501                         continue;
502
503                 old = root->children + i;
504
505                 if ((old->values_num != 1)
506                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
507                 {
508                         ERROR ("configfile: `Include' needs exactly one string argument.");
509                         continue;
510                 }
511
512                 new = cf_read_generic (old->values[0].value.string, depth + 1);
513                 if (new == NULL)
514                         continue;
515
516                 /* Now replace the i'th child in `root' with `new'. */
517                 cf_ci_replace_child (root, new, i);
518
519                 /* ... and go back to the new i'th child. */
520                 --i;
521
522                 sfree (new->values);
523                 sfree (new);
524         } /* for (i = 0; i < root->children_num; i++) */
525
526         return (0);
527 } /* int cf_include_all */
528
529 static oconfig_item_t *cf_read_file (const char *file, int depth)
530 {
531         oconfig_item_t *root;
532
533         assert (depth < CF_MAX_DEPTH);
534
535         root = oconfig_parse_file (file);
536         if (root == NULL)
537         {
538                 ERROR ("configfile: Cannot read file `%s'.", file);
539                 return (NULL);
540         }
541
542         cf_include_all (root, depth);
543
544         return (root);
545 } /* oconfig_item_t *cf_read_file */
546
547 static int cf_compare_string (const void *p1, const void *p2)
548 {
549         return strcmp (*(const char **) p1, *(const char **) p2);
550 }
551
552 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
553 {
554         oconfig_item_t *root = NULL;
555         DIR *dh;
556         struct dirent *de;
557         char **filenames = NULL;
558         int filenames_num = 0;
559         int status;
560         int i;
561
562         assert (depth < CF_MAX_DEPTH);
563
564         dh = opendir (dir);
565         if (dh == NULL)
566         {
567                 char errbuf[1024];
568                 ERROR ("configfile: opendir failed: %s",
569                                 sstrerror (errno, errbuf, sizeof (errbuf)));
570                 return (NULL);
571         }
572
573         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
574         if (root == NULL)
575         {
576                 ERROR ("configfile: malloc failed.");
577                 return (NULL);
578         }
579         memset (root, 0, sizeof (oconfig_item_t));
580
581         while ((de = readdir (dh)) != NULL)
582         {
583                 char   name[1024];
584                 char **tmp;
585
586                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
587                         continue;
588
589                 status = ssnprintf (name, sizeof (name), "%s/%s",
590                                 dir, de->d_name);
591                 if ((status < 0) || ((size_t) status >= sizeof (name)))
592                 {
593                         ERROR ("configfile: Not including `%s/%s' because its"
594                                         " name is too long.",
595                                         dir, de->d_name);
596                         for (i = 0; i < filenames_num; ++i)
597                                 free (filenames[i]);
598                         free (filenames);
599                         free (root);
600                         return (NULL);
601                 }
602
603                 ++filenames_num;
604                 tmp = (char **) realloc (filenames,
605                                 filenames_num * sizeof (*filenames));
606                 if (tmp == NULL) {
607                         ERROR ("configfile: realloc failed.");
608                         for (i = 0; i < filenames_num - 1; ++i)
609                                 free (filenames[i]);
610                         free (filenames);
611                         free (root);
612                         return (NULL);
613                 }
614                 filenames = tmp;
615
616                 filenames[filenames_num - 1] = sstrdup (name);
617         }
618
619         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
620                         cf_compare_string);
621
622         for (i = 0; i < filenames_num; ++i)
623         {
624                 oconfig_item_t *temp;
625                 char *name = filenames[i];
626
627                 temp = cf_read_generic (name, depth);
628                 if (temp == NULL)
629                 {
630                         /* An error should already have been reported. */
631                         sfree (name);
632                         continue;
633                 }
634
635                 cf_ci_append_children (root, temp);
636                 sfree (temp->children);
637                 sfree (temp);
638
639                 free (name);
640         }
641
642         free(filenames);
643         return (root);
644 } /* oconfig_item_t *cf_read_dir */
645
646 /* 
647  * cf_read_generic
648  *
649  * Path is stat'ed and either cf_read_file or cf_read_dir is called
650  * accordingly.
651  *
652  * There are two versions of this function: If `wordexp' exists shell wildcards
653  * will be expanded and the function will include all matches found. If
654  * `wordexp' (or, more precisely, it's header file) is not available the
655  * simpler function is used which does not do any such expansion.
656  */
657 #if HAVE_WORDEXP_H
658 static oconfig_item_t *cf_read_generic (const char *path, int depth)
659 {
660         oconfig_item_t *root = NULL;
661         int status;
662         const char *path_ptr;
663         wordexp_t we;
664         size_t i;
665
666         if (depth >= CF_MAX_DEPTH)
667         {
668                 ERROR ("configfile: Not including `%s' because the maximum "
669                                 "nesting depth has been reached.", path);
670                 return (NULL);
671         }
672
673         status = wordexp (path, &we, WRDE_NOCMD);
674         if (status != 0)
675         {
676                 ERROR ("configfile: wordexp (%s) failed.", path);
677                 return (NULL);
678         }
679
680         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
681         if (root == NULL)
682         {
683                 ERROR ("configfile: malloc failed.");
684                 return (NULL);
685         }
686         memset (root, '\0', sizeof (oconfig_item_t));
687
688         /* wordexp() might return a sorted list already. That's not
689          * documented though, so let's make sure we get what we want. */
690         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
691                         cf_compare_string);
692
693         for (i = 0; i < we.we_wordc; i++)
694         {
695                 oconfig_item_t *temp;
696                 struct stat statbuf;
697
698                 path_ptr = we.we_wordv[i];
699
700                 status = stat (path_ptr, &statbuf);
701                 if (status != 0)
702                 {
703                         char errbuf[1024];
704                         ERROR ("configfile: stat (%s) failed: %s",
705                                         path_ptr,
706                                         sstrerror (errno, errbuf, sizeof (errbuf)));
707                         oconfig_free (root);
708                         return (NULL);
709                 }
710
711                 if (S_ISREG (statbuf.st_mode))
712                         temp = cf_read_file (path_ptr, depth);
713                 else if (S_ISDIR (statbuf.st_mode))
714                         temp = cf_read_dir (path_ptr, depth);
715                 else
716                 {
717                         ERROR ("configfile: %s is neither a file nor a "
718                                         "directory.", path);
719                         continue;
720                 }
721
722                 if (temp == NULL) {
723                         oconfig_free (root);
724                         return (NULL);
725                 }
726
727                 cf_ci_append_children (root, temp);
728                 sfree (temp->children);
729                 sfree (temp);
730         }
731
732         wordfree (&we);
733
734         return (root);
735 } /* oconfig_item_t *cf_read_generic */
736 /* #endif HAVE_WORDEXP_H */
737
738 #else /* if !HAVE_WORDEXP_H */
739 static oconfig_item_t *cf_read_generic (const char *path, int depth)
740 {
741         struct stat statbuf;
742         int status;
743
744         if (depth >= CF_MAX_DEPTH)
745         {
746                 ERROR ("configfile: Not including `%s' because the maximum "
747                                 "nesting depth has been reached.", path);
748                 return (NULL);
749         }
750
751         status = stat (path, &statbuf);
752         if (status != 0)
753         {
754                 char errbuf[1024];
755                 ERROR ("configfile: stat (%s) failed: %s",
756                                 path,
757                                 sstrerror (errno, errbuf, sizeof (errbuf)));
758                 return (NULL);
759         }
760
761         if (S_ISREG (statbuf.st_mode))
762                 return (cf_read_file (path, depth));
763         else if (S_ISDIR (statbuf.st_mode))
764                 return (cf_read_dir (path, depth));
765
766         ERROR ("configfile: %s is neither a file nor a directory.", path);
767         return (NULL);
768 } /* oconfig_item_t *cf_read_generic */
769 #endif /* !HAVE_WORDEXP_H */
770
771 /* 
772  * Public functions
773  */
774 int global_option_set (const char *option, const char *value)
775 {
776         int i;
777
778         DEBUG ("option = %s; value = %s;", option, value);
779
780         for (i = 0; i < cf_global_options_num; i++)
781                 if (strcasecmp (cf_global_options[i].key, option) == 0)
782                         break;
783
784         if (i >= cf_global_options_num)
785                 return (-1);
786
787         sfree (cf_global_options[i].value);
788
789         if (value != NULL)
790                 cf_global_options[i].value = strdup (value);
791         else
792                 cf_global_options[i].value = NULL;
793
794         return (0);
795 }
796
797 const char *global_option_get (const char *option)
798 {
799         int i;
800
801         for (i = 0; i < cf_global_options_num; i++)
802                 if (strcasecmp (cf_global_options[i].key, option) == 0)
803                         break;
804
805         if (i >= cf_global_options_num)
806                 return (NULL);
807         
808         return ((cf_global_options[i].value != NULL)
809                         ? cf_global_options[i].value
810                         : cf_global_options[i].def);
811 } /* char *global_option_get */
812
813 void cf_unregister (const char *type)
814 {
815         cf_callback_t *this, *prev;
816
817         for (prev = NULL, this = first_callback;
818                         this != NULL;
819                         prev = this, this = this->next)
820                 if (strcasecmp (this->type, type) == 0)
821                 {
822                         if (prev == NULL)
823                                 first_callback = this->next;
824                         else
825                                 prev->next = this->next;
826
827                         free (this);
828                         break;
829                 }
830 } /* void cf_unregister */
831
832 void cf_unregister_complex (const char *type)
833 {
834         cf_complex_callback_t *this, *prev;
835
836         for (prev = NULL, this = complex_callback_head;
837                         this != NULL;
838                         prev = this, this = this->next)
839                 if (strcasecmp (this->type, type) == 0)
840                 {
841                         if (prev == NULL)
842                                 complex_callback_head = this->next;
843                         else
844                                 prev->next = this->next;
845
846                         sfree (this->type);
847                         sfree (this);
848                         break;
849                 }
850 } /* void cf_unregister */
851
852 void cf_register (const char *type,
853                 int (*callback) (const char *, const char *),
854                 const char **keys, int keys_num)
855 {
856         cf_callback_t *cf_cb;
857
858         /* Remove this module from the list, if it already exists */
859         cf_unregister (type);
860
861         /* This pointer will be free'd in `cf_unregister' */
862         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
863                 return;
864
865         cf_cb->type     = type;
866         cf_cb->callback = callback;
867         cf_cb->keys     = keys;
868         cf_cb->keys_num = keys_num;
869
870         cf_cb->next = first_callback;
871         first_callback = cf_cb;
872 } /* void cf_register */
873
874 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
875 {
876         cf_complex_callback_t *new;
877
878         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
879         if (new == NULL)
880                 return (-1);
881
882         new->type = strdup (type);
883         if (new->type == NULL)
884         {
885                 sfree (new);
886                 return (-1);
887         }
888
889         new->callback = callback;
890         new->next = NULL;
891
892         if (complex_callback_head == NULL)
893         {
894                 complex_callback_head = new;
895         }
896         else
897         {
898                 cf_complex_callback_t *last = complex_callback_head;
899                 while (last->next != NULL)
900                         last = last->next;
901                 last->next = new;
902         }
903
904         return (0);
905 } /* int cf_register_complex */
906
907 int cf_read (char *filename)
908 {
909         oconfig_item_t *conf;
910         int i;
911
912         conf = cf_read_generic (filename, 0 /* depth */);
913         if (conf == NULL)
914         {
915                 ERROR ("Unable to read config file %s.", filename);
916                 return (-1);
917         }
918
919         for (i = 0; i < conf->children_num; i++)
920         {
921                 if (conf->children[i].children == NULL)
922                         dispatch_value (conf->children + i);
923                 else
924                         dispatch_block (conf->children + i);
925         }
926
927         oconfig_free (conf);
928
929         /* Read the default types.db if no `TypesDB' option was given. */
930         if (cf_default_typesdb)
931                 read_types_list (PKGDATADIR"/types.db");
932
933         return (0);
934 } /* int cf_read */
935
936 /* Assures the config option is a string, duplicates it and returns the copy in
937  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
938  * success. */
939 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
940 {
941         char *string;
942
943         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
944         {
945                 ERROR ("cf_util_get_string: The %s option requires "
946                                 "exactly one string argument.", ci->key);
947                 return (-1);
948         }
949
950         string = strdup (ci->values[0].value.string);
951         if (string == NULL)
952                 return (-1);
953
954         if (*ret_string != NULL)
955                 sfree (*ret_string);
956         *ret_string = string;
957
958         return (0);
959 } /* }}} int cf_util_get_string */
960
961 /* Assures the config option is a string and copies it to the provided buffer.
962  * Assures null-termination. */
963 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
964                 size_t buffer_size)
965 {
966         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
967                 return (EINVAL);
968
969         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
970         {
971                 ERROR ("cf_util_get_string_buffer: The %s option requires "
972                                 "exactly one string argument.", ci->key);
973                 return (-1);
974         }
975
976         strncpy (buffer, ci->values[0].value.string, buffer_size);
977         buffer[buffer_size - 1] = 0;
978
979         return (0);
980 } /* }}} int cf_util_get_string_buffer */
981
982 /* Assures the config option is a number and returns it as an int. */
983 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
984 {
985         if ((ci == NULL) || (ret_value == NULL))
986                 return (EINVAL);
987
988         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
989         {
990                 ERROR ("cf_util_get_int: The %s option requires "
991                                 "exactly one numeric argument.", ci->key);
992                 return (-1);
993         }
994
995         *ret_value = (int) ci->values[0].value.number;
996
997         return (0);
998 } /* }}} int cf_util_get_int */
999
1000 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1001 {
1002         if ((ci == NULL) || (ret_bool == NULL))
1003                 return (EINVAL);
1004
1005         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1006         {
1007                 ERROR ("cf_util_get_boolean: The %s option requires "
1008                                 "exactly one boolean argument.", ci->key);
1009                 return (-1);
1010         }
1011
1012         *ret_bool = ci->values[0].value.boolean ? 1 : 0;
1013
1014         return (0);
1015 } /* }}} int cf_util_get_boolean */
1016
1017 int cf_util_get_flag (const oconfig_item_t *ci, /* {{{ */
1018                 unsigned int *ret_value, unsigned int flag)
1019 {
1020         int status;
1021         _Bool b;
1022
1023         if (ret_value == NULL)
1024                 return (EINVAL);
1025
1026         b = 0;
1027         status = cf_util_get_boolean (ci, &b);
1028         if (status != 0)
1029                 return (status);
1030
1031         if (b)
1032         {
1033                 *ret_value |= flag;
1034         }
1035         else
1036         {
1037                 *ret_value &= ~flag;
1038         }
1039
1040         return (0);
1041 } /* }}} int cf_util_get_flag */
1042
1043 /* Assures that the config option is a string. The string is then converted to
1044  * a port number using `service_name_to_port_number' and returned. Returns the
1045  * port number in the range [1-65535] or less than zero upon failure. */
1046 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1047 {
1048         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1049         {
1050                 ERROR ("cf_util_get_port_number: The %s option requires "
1051                                 "exactly one string argument.", ci->key);
1052                 return (-1);
1053         }
1054
1055         return (service_name_to_port_number (ci->values[0].value.string));
1056 } /* }}} int cf_util_get_port_number */