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)
113 ret = (*cf_cb->callback) (key, value);
118 if (i >= cf_cb->keys_num)
119 fprintf (stderr, "Plugin `%s' did not register for value `%s'.\n", type, key);
127 void cf_unregister (char *type)
129 cf_callback_t *this, *prev;
131 for (prev = NULL, this = first_callback;
133 prev = this, this = this->next)
134 if (strcasecmp (this->type, type) == 0)
137 first_callback = this->next;
139 prev->next = this->next;
146 void cf_register (char *type,
147 int (*callback) (char *, char *),
148 char **keys, int keys_num)
150 cf_callback_t *cf_cb;
154 /* Remove this module from the list, if it already exists */
155 cf_unregister (type);
157 /* This pointer will be free'd in `cf_unregister' */
158 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
162 cf_cb->callback = callback;
164 cf_cb->keys_num = keys_num;
166 cf_cb->next = first_callback;
167 first_callback = cf_cb;
169 for (i = 0; i < keys_num; i++)
171 if (snprintf (buf, 64, "Plugin.%s", keys[i]) < 64)
173 /* This may be called multiple times for the same
174 * `key', but apparently `lc_register_*' can handle
176 lc_register_callback (buf, SHORTOPT_NONE,
177 LC_VAR_STRING, cf_callback_general,
182 DBG ("Key was truncated: `%s'", keys[i]);
188 * Functions for the actual parsing
190 int cf_callback_general (const char *shortvar, const char *var,
191 const char *arguments, const char *value, lc_flags_t flags,
194 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
195 shortvar, var, arguments, value);
197 if ((nesting_depth == 0) || (current_module == NULL))
199 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
200 return (LC_CBRET_ERROR);
203 /* Send the data to the plugin */
204 if (cf_dispatch (current_module, shortvar, value) < 0)
205 return (LC_CBRET_ERROR);
207 return (LC_CBRET_OKAY);
210 int cf_callback_section_mode (const char *shortvar, const char *var,
211 const char *arguments, const char *value, lc_flags_t flags,
214 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
215 shortvar, var, arguments, value);
217 if (flags == LC_FLAGS_SECTIONSTART)
219 if (nesting_depth != 0)
221 fprintf (stderr, ERR_NOT_NESTED);
222 return (LC_CBRET_ERROR);
225 if (arguments == NULL)
227 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
228 return (LC_CBRET_ERROR);
233 if (((operating_mode == MODE_CLIENT)
234 && (strcasecmp (arguments, "Client") == 0))
235 || ((operating_mode == MODE_SERVER)
236 && (strcasecmp (arguments, "Server") == 0))
237 || ((operating_mode == MODE_LOCAL)
238 && (strcasecmp (arguments, "Local") == 0)))
240 return (LC_CBRET_OKAY);
244 return (LC_CBRET_IGNORESECTION);
247 else if (flags == LC_FLAGS_SECTIONEND)
251 return (LC_CBRET_OKAY);
255 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
256 return (LC_CBRET_ERROR);
261 int cf_callback_section_module (const char *shortvar, const char *var,
262 const char *arguments, const char *value, lc_flags_t flags,
265 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
266 shortvar, var, arguments, value);
268 if (flags == LC_FLAGS_SECTIONSTART)
270 if (nesting_depth != 0)
272 fprintf (stderr, ERR_NOT_NESTED);
273 return (LC_CBRET_ERROR);
276 if (arguments == NULL)
278 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
279 return (LC_CBRET_ERROR);
282 if ((current_module = strdup (arguments)) == NULL)
285 return (LC_CBRET_ERROR);
290 if (cf_search (current_module) != NULL)
291 return (LC_CBRET_OKAY);
293 return (LC_CBRET_IGNORESECTION);
295 else if (flags == LC_FLAGS_SECTIONEND)
297 if (current_module != NULL)
299 free (current_module);
300 current_module = NULL;
305 return (LC_CBRET_OKAY);
309 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
310 return (LC_CBRET_ERROR);
314 int cf_callback_loadmodule (const char *shortvar, const char *var,
315 const char *arguments, const char *value, lc_flags_t flags,
318 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
319 shortvar, var, arguments, value);
321 if (nesting_depth == 0)
323 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
324 return (LC_CBRET_ERROR);
327 if (plugin_load (value))
328 syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", shortvar);
330 /* Return `okay' even if there was an error, because it's not a syntax
332 return (LC_CBRET_OKAY);
335 int cf_read (char *filename)
337 if (filename == NULL)
338 filename = CONFIGFILE;
340 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
341 cf_callback_section_mode, NULL);
342 lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
343 cf_callback_section_module, NULL);
347 * - Add more directives, such as `DefaultMode', `DataDir', `PIDFile', ...
350 lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
351 LC_VAR_STRING, cf_callback_loadmodule,
354 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
356 syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
360 /* free memory and stuff */