2 * collectd - src/configfile.c
3 * Copyright (C) 2005,2006 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
25 #include "liboconfig/oconfig.h"
29 #include "configfile.h"
31 #include "utils_debug.h"
33 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
38 typedef struct cf_callback
41 int (*callback) (const char *, const char *);
44 struct cf_callback *next;
47 typedef struct cf_value_map_s
50 int (*func) (const oconfig_item_t *);
53 typedef struct cf_global_option_s
61 * Prototypes of callback functions
63 static int dispatch_value_plugindir (const oconfig_item_t *ci);
64 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
69 static cf_callback_t *first_callback = NULL;
71 static cf_value_map_t cf_value_map[] =
73 {"PluginDir", dispatch_value_plugindir},
74 {"LoadPlugin", dispatch_value_loadplugin}
76 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
78 static cf_global_option_t cf_global_options[] =
80 {"BaseDir", NULL, PKGLOCALSTATEDIR},
81 {"LogFile", NULL, LOGFILE},
82 {"PIDFile", NULL, PIDFILE}
84 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
87 * Functions to handle register/unregister, search, and other plugin related
90 static cf_callback_t *cf_search (const char *type)
97 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
98 if (strcasecmp (cf_cb->type, type) == 0)
104 static int cf_dispatch (const char *type, const char *orig_key,
105 const char *orig_value)
107 cf_callback_t *cf_cb;
113 DBG ("type = %s, key = %s, value = %s",
115 ESCAPE_NULL(orig_key),
116 ESCAPE_NULL(orig_value));
118 if ((cf_cb = cf_search (type)) == NULL)
120 syslog (LOG_WARNING, "Plugin `%s' did not register a callback.", type);
124 if ((key = strdup (orig_key)) == NULL)
126 if ((value = strdup (orig_value)) == NULL)
134 for (i = 0; i < cf_cb->keys_num; i++)
136 if (strcasecmp (cf_cb->keys[i], key) == 0)
138 ret = (*cf_cb->callback) (key, value);
143 if (i >= cf_cb->keys_num)
144 syslog (LOG_WARNING, "Plugin `%s' did not register for value `%s'.", type, key);
149 DBG ("return (%i)", ret);
152 } /* int cf_dispatch */
154 static int dispatch_global_option (const oconfig_item_t *ci)
156 if (ci->values_num != 1)
158 if (ci->values[0].type != OCONFIG_TYPE_STRING)
161 return (global_option_set (ci->key, ci->values[0].value.string));
164 static int dispatch_value_plugindir (const oconfig_item_t *ci)
166 assert (strcasecmp (ci->key, "PluginDir") == 0);
168 if (ci->values_num != 1)
170 if (ci->values[0].type != OCONFIG_TYPE_STRING)
173 plugin_set_dir (ci->values[0].value.string);
177 static int dispatch_value_loadplugin (const oconfig_item_t *ci)
179 assert (strcasecmp (ci->key, "LoadPlugin") == 0);
181 if (ci->values_num != 1)
183 if (ci->values[0].type != OCONFIG_TYPE_STRING)
186 return (plugin_load (ci->values[0].value.string));
187 } /* int dispatch_value_loadplugin */
189 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
197 buffer_free = sizeof (buffer);
199 for (i = 0; i < ci->values_num; i++)
203 if (ci->values[i].type == OCONFIG_TYPE_STRING)
204 status = snprintf (buffer_ptr, buffer_free, " %s",
205 ci->values[i].value.string);
206 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
207 status = snprintf (buffer_ptr, buffer_free, " %lf",
208 ci->values[i].value.number);
209 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
210 status = snprintf (buffer_ptr, buffer_free, " %s",
211 ci->values[i].value.boolean
214 if ((status < 0) || (status >= buffer_free))
216 buffer_free -= status;
217 buffer_ptr += status;
219 /* skip the initial space */
220 buffer_ptr = buffer + 1;
222 return (cf_dispatch (plugin, ci->key, buffer_ptr));
223 } /* int plugin_conf_dispatch */
225 static int dispatch_value (const oconfig_item_t *ci)
230 for (i = 0; i < cf_value_map_num; i++)
231 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
233 ret = cf_value_map[i].func (ci);
237 for (i = 0; i < cf_global_options_num; i++)
238 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
240 ret = dispatch_global_option (ci);
245 } /* int dispatch_value */
247 static int dispatch_block_plugin (oconfig_item_t *ci)
252 if (strcasecmp (ci->key, "Plugin") != 0)
254 if (ci->values_num != 1)
256 if (ci->values[0].type != OCONFIG_TYPE_STRING)
259 name = ci->values[0].value.string;
261 for (i = 0; i < ci->children_num; i++)
263 if (ci->children[i].children == NULL)
264 dispatch_value_plugin (name, ci->children + i);
266 {DBG ("No nested config blocks allow for plugins. Yet.");}
273 static int dispatch_block (oconfig_item_t *ci)
275 if (strcasecmp (ci->key, "Plugin") == 0)
276 return (dispatch_block_plugin (ci));
284 int global_option_set (const char *option, const char *value)
288 for (i = 0; i < cf_global_options_num; i++)
289 if (strcasecmp (cf_global_options[i].key, option) == 0)
292 if (i >= cf_global_options_num)
295 if (cf_global_options[i].value != NULL)
296 free (cf_global_options[i].value);
299 cf_global_options[i].value = strdup (value);
301 cf_global_options[i].value = NULL;
306 const char *global_option_get (const char *option)
310 for (i = 0; i < cf_global_options_num; i++)
311 if (strcasecmp (cf_global_options[i].key, option) == 0)
314 if (i >= cf_global_options_num)
317 return ((cf_global_options[i].value != NULL)
318 ? cf_global_options[i].value
319 : cf_global_options[i].def);
320 } /* char *global_option_get */
322 void cf_unregister (const char *type)
324 cf_callback_t *this, *prev;
326 for (prev = NULL, this = first_callback;
328 prev = this, this = this->next)
329 if (strcasecmp (this->type, type) == 0)
332 first_callback = this->next;
334 prev->next = this->next;
341 void cf_register (const char *type,
342 int (*callback) (const char *, const char *),
343 const char **keys, int keys_num)
345 cf_callback_t *cf_cb;
347 /* Remove this module from the list, if it already exists */
348 cf_unregister (type);
350 /* This pointer will be free'd in `cf_unregister' */
351 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
355 cf_cb->callback = callback;
357 cf_cb->keys_num = keys_num;
359 cf_cb->next = first_callback;
360 first_callback = cf_cb;
361 } /* void cf_register */
363 int cf_read (char *filename)
365 oconfig_item_t *conf;
368 conf = oconfig_parse_file (filename);
371 syslog (LOG_ERR, "Unable to read config file %s.", filename);
375 for (i = 0; i < conf->children_num; i++)
377 if (conf->children[i].children == NULL)
378 dispatch_value (conf->children + i);
380 dispatch_block (conf->children + i);