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"
32 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
37 typedef struct cf_callback
40 int (*callback) (const char *, const char *);
43 struct cf_callback *next;
46 typedef struct cf_value_map_s
49 int (*func) (const oconfig_item_t *);
52 typedef struct cf_global_option_s
60 * Prototypes of callback functions
62 static int dispatch_value_plugindir (const oconfig_item_t *ci);
63 static int dispatch_value_loadplugin (const oconfig_item_t *ci);
68 static cf_callback_t *first_callback = NULL;
70 static cf_value_map_t cf_value_map[] =
72 {"PluginDir", dispatch_value_plugindir},
73 {"LoadPlugin", dispatch_value_loadplugin}
75 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
77 static cf_global_option_t cf_global_options[] =
79 {"BaseDir", NULL, PKGLOCALSTATEDIR},
80 {"PIDFile", NULL, PIDFILE},
81 {"Hostname", NULL, NULL},
82 {"Interval", NULL, "10"}
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 DEBUG ("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 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 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
149 DEBUG ("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 {DEBUG ("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 DEBUG ("option = %s; value = %s;", option, value);
290 for (i = 0; i < cf_global_options_num; i++)
291 if (strcasecmp (cf_global_options[i].key, option) == 0)
294 if (i >= cf_global_options_num)
297 if (cf_global_options[i].value != NULL)
298 free (cf_global_options[i].value);
301 cf_global_options[i].value = strdup (value);
303 cf_global_options[i].value = NULL;
308 const char *global_option_get (const char *option)
312 for (i = 0; i < cf_global_options_num; i++)
313 if (strcasecmp (cf_global_options[i].key, option) == 0)
316 if (i >= cf_global_options_num)
319 return ((cf_global_options[i].value != NULL)
320 ? cf_global_options[i].value
321 : cf_global_options[i].def);
322 } /* char *global_option_get */
324 void cf_unregister (const char *type)
326 cf_callback_t *this, *prev;
328 for (prev = NULL, this = first_callback;
330 prev = this, this = this->next)
331 if (strcasecmp (this->type, type) == 0)
334 first_callback = this->next;
336 prev->next = this->next;
343 void cf_register (const char *type,
344 int (*callback) (const char *, const char *),
345 const char **keys, int keys_num)
347 cf_callback_t *cf_cb;
349 /* Remove this module from the list, if it already exists */
350 cf_unregister (type);
352 /* This pointer will be free'd in `cf_unregister' */
353 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
357 cf_cb->callback = callback;
359 cf_cb->keys_num = keys_num;
361 cf_cb->next = first_callback;
362 first_callback = cf_cb;
363 } /* void cf_register */
365 int cf_read (char *filename)
367 oconfig_item_t *conf;
370 conf = oconfig_parse_file (filename);
373 ERROR ("Unable to read config file %s.", filename);
377 for (i = 0; i < conf->children_num; i++)
379 if (conf->children[i].children == NULL)
380 dispatch_value (conf->children + i);
382 dispatch_block (conf->children + i);