2 * collectd - src/configfile.c
3 * Copyright (C) 2005 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 * - remove all (I mean *ALL*) calls to `fprintf': `stderr' will have been
31 #include "libconfig/libconfig.h"
34 #include "configfile.h"
35 #include "utils_debug.h"
37 #define SHORTOPT_NONE 0
39 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
40 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
41 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
42 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
45 extern int operating_mode;
47 static int operating_mode = MODE_LOCAL;
50 typedef struct cf_callback
53 int (*callback) (char *, char *);
56 struct cf_callback *next;
59 static cf_callback_t *first_callback = NULL;
61 static int nesting_depth = 0;
62 static char *current_module = NULL;
64 /* cf_register needs this prototype */
65 int cf_callback_general (const char *, const char *, const char *,
66 const char *, lc_flags_t, void *);
69 * Functions to handle register/unregister, search, ...
71 cf_callback_t *cf_search (char *type)
78 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
79 if (strcasecmp (cf_cb->type, type) == 0)
85 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
93 if ((cf_cb = cf_search (type)) == NULL)
95 fprintf (stderr, "Plugin `%s' did not register a callback.\n", type);
99 if ((key = strdup (orig_key)) == NULL)
101 if ((value = strdup (orig_value)) == NULL)
109 for (i = 0; i < cf_cb->keys_num; i++)
111 if (strcasecmp (cf_cb->keys[i], key) == 0)
112 ret = (*cf_cb->callback) (key, value);
115 if (i >= cf_cb->keys_num)
116 fprintf (stderr, "Plugin `%s' did not register for value `%s'.\n", type, key);
124 void cf_unregister (char *type)
126 cf_callback_t *this, *prev;
128 for (prev = NULL, this = first_callback;
130 prev = this, this = this->next)
131 if (strcasecmp (this->type, type) == 0)
134 first_callback = this->next;
136 prev->next = this->next;
143 void cf_register (char *type,
144 int (*callback) (char *, char *),
145 char **keys, int keys_num)
147 cf_callback_t *cf_cb;
151 /* Remove this module from the list, if it already exists */
152 cf_unregister (type);
154 /* This pointer will be free'd in `cf_unregister' */
155 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
159 cf_cb->callback = callback;
161 cf_cb->keys_num = keys_num;
163 cf_cb->next = first_callback;
164 first_callback = cf_cb;
166 for (i = 0; i < keys_num; i++)
168 if (snprintf (buf, 64, "Module.%s", keys[i]) < 64)
170 /* This may be called multiple times for the same
171 * `key', but apparently `lc_register_*' can handle
173 lc_register_callback (buf, SHORTOPT_NONE,
174 LC_VAR_STRING, cf_callback_general,
179 DBG ("Key was truncated: `%s'", keys[i]);
185 * Functions for the actual parsing
187 int cf_callback_general (const char *shortvar, const char *var,
188 const char *arguments, const char *value, lc_flags_t flags,
191 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
192 shortvar, var, arguments, value);
194 if ((nesting_depth == 0) || (current_module == NULL))
196 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
197 return (LC_CBRET_ERROR);
200 /* Send the data to the plugin */
201 if (cf_dispatch (current_module, shortvar, value) < 0)
202 return (LC_CBRET_ERROR);
204 return (LC_CBRET_OKAY);
207 int cf_callback_section_mode (const char *shortvar, const char *var,
208 const char *arguments, const char *value, lc_flags_t flags,
211 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
212 shortvar, var, arguments, value);
214 if (flags == LC_FLAGS_SECTIONSTART)
216 if (nesting_depth != 0)
218 fprintf (stderr, ERR_NOT_NESTED);
219 return (LC_CBRET_ERROR);
222 if (arguments == NULL)
224 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
225 return (LC_CBRET_ERROR);
230 if (((operating_mode == MODE_CLIENT)
231 && (strcasecmp (arguments, "Client") == 0))
232 || ((operating_mode == MODE_SERVER)
233 && (strcasecmp (arguments, "Server") == 0))
234 || ((operating_mode == MODE_LOCAL)
235 && (strcasecmp (arguments, "Local") == 0)))
237 return (LC_CBRET_OKAY);
241 return (LC_CBRET_IGNORESECTION);
244 else if (flags == LC_FLAGS_SECTIONEND)
248 return (LC_CBRET_OKAY);
252 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
253 return (LC_CBRET_ERROR);
258 int cf_callback_section_module (const char *shortvar, const char *var,
259 const char *arguments, const char *value, lc_flags_t flags,
262 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
263 shortvar, var, arguments, value);
265 if (flags == LC_FLAGS_SECTIONSTART)
267 if (nesting_depth != 0)
269 fprintf (stderr, ERR_NOT_NESTED);
270 return (LC_CBRET_ERROR);
273 if (arguments == NULL)
275 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
276 return (LC_CBRET_ERROR);
279 if ((current_module = strdup (arguments)) == NULL)
282 return (LC_CBRET_ERROR);
287 if (cf_search (current_module) != NULL)
288 return (LC_CBRET_OKAY);
290 return (LC_CBRET_IGNORESECTION);
292 else if (flags == LC_FLAGS_SECTIONEND)
294 if (current_module != NULL)
296 free (current_module);
297 current_module == NULL;
302 return (LC_CBRET_OKAY);
306 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
307 return (LC_CBRET_ERROR);
311 int cf_callback_loadmodule (const char *shortvar, const char *var,
312 const char *arguments, const char *value, lc_flags_t flags,
315 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
316 shortvar, var, arguments, value);
318 if (nesting_depth == 0)
320 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
321 return (LC_CBRET_ERROR);
324 if (plugin_load (value))
325 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", shortvar);
327 /* Return `okay' even if there was an error, because it's not a syntax
329 return (LC_CBRET_OKAY);
332 int cf_read (char *filename)
334 if (filename == NULL)
335 filename = CONFIGFILE;
337 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
338 cf_callback_section_mode, NULL);
339 lc_register_callback ("Module", SHORTOPT_NONE, LC_VAR_SECTION,
340 cf_callback_section_module, NULL);
344 * - Add more directives, such as `DefaultMode', `DataDir', `PIDFile', ...
347 lc_register_callback ("Mode.LoadModule", SHORTOPT_NONE,
348 LC_VAR_STRING, cf_callback_loadmodule,
351 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
353 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
357 /* free memory and stuff */