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"
33 #include "configfile.h"
34 #include "utils_debug.h"
36 #define SHORTOPT_NONE 0
38 #define ERR_NOT_NESTED "Sections cannot be nested.\n"
39 #define ERR_SECTION_ONLY "`%s' can only be used as section.\n"
40 #define ERR_NEEDS_ARG "Section `%s' needs an argument.\n"
41 #define ERR_NEEDS_SECTION "`%s' can only be used within a section.\n"
43 typedef struct cf_callback
46 int (*callback) (char *, char *);
49 struct cf_callback *next;
52 static cf_callback_t *first_callback = NULL;
54 static int nesting_depth = 0;
55 static char *current_module = NULL;
57 /* cf_register needs this prototype */
58 int cf_callback_general (const char *, const char *, const char *,
59 const char *, lc_flags_t, void *);
62 * Functions to handle register/unregister, search, ...
64 cf_callback_t *cf_search (char *type)
71 for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
72 if (strcasecmp (cf_cb->type, type) == 0)
78 int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
86 if ((cf_cb = cf_search (type)) == NULL)
88 fprintf (stderr, "Plugin `%s' did not register a callback.\n", type);
92 if ((key = strdup (orig_key)) == NULL)
94 if ((value = strdup (orig_value)) == NULL)
102 for (i = 0; i < cf_cb->keys_num; i++)
104 if (strcasecmp (cf_cb->keys[i], key) == 0)
105 ret = (*cf_cb->callback) (key, value);
108 if (i >= cf_cb->keys_num)
109 fprintf (stderr, "Plugin `%s' did not register for value `%s'.\n", type, key);
117 void cf_unregister (char *type)
119 cf_callback_t *this, *prev;
121 for (prev = NULL, this = first_callback;
123 prev = this, this = this->next)
124 if (strcasecmp (this->type, type) == 0)
127 first_callback = this->next;
129 prev->next = this->next;
136 void cf_register (char *type,
137 int (*callback) (char *, char *),
138 char **keys, int keys_num)
140 cf_callback_t *cf_cb;
144 /* Remove this module from the list, if it already exists */
145 cf_unregister (type);
147 /* This pointer will be free'd in `cf_unregister' */
148 if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
152 cf_cb->callback = callback;
154 cf_cb->keys_num = keys_num;
156 cf_cb->next = first_callback;
157 first_callback = cf_cb;
159 for (i = 0; i < keys_num; i++)
161 if (snprintf (buf, 64, "Module.%s", keys[i]) < 64)
163 /* This may be called multiple times for the same
164 * `key', but apparently `lc_register_*' can handle
166 lc_register_callback (buf, SHORTOPT_NONE,
167 LC_VAR_STRING, cf_callback_general,
172 DBG ("Key was truncated: `%s'", keys[i]);
178 * Functions for the actual parsing
180 int cf_callback_general (const char *shortvar, const char *var,
181 const char *arguments, const char *value, lc_flags_t flags,
184 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
185 shortvar, var, arguments, value);
187 if ((nesting_depth == 0) || (current_module == NULL))
189 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
190 return (LC_CBRET_ERROR);
193 /* Send the data to the plugin */
194 if (cf_dispatch (current_module, shortvar, value) < 0)
195 return (LC_CBRET_ERROR);
197 return (LC_CBRET_OKAY);
200 int cf_callback_section_mode (const char *shortvar, const char *var,
201 const char *arguments, const char *value, lc_flags_t flags,
204 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
205 shortvar, var, arguments, value);
207 if (flags == LC_FLAGS_SECTIONSTART)
209 if (nesting_depth != 0)
211 fprintf (stderr, ERR_NOT_NESTED);
212 return (LC_CBRET_ERROR);
215 if (arguments == NULL)
217 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
218 return (LC_CBRET_ERROR);
223 if (((operating_mode == MODE_CLIENT)
224 && (strcasecmp (arguments, "Client") == 0))
225 || ((operating_mode == MODE_SERVER)
226 && (strcasecmp (arguments, "Server") == 0))
227 || ((operating_mode == MODE_LOCAL)
228 && (strcasecmp (arguments, "Local") == 0)))
230 return (LC_CBRET_OKAY);
234 return (LC_CBRET_IGNORESECTION);
237 else if (flags == LC_FLAGS_SECTIONEND)
241 return (LC_CBRET_OKAY);
245 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
246 return (LC_CBRET_ERROR);
251 int cf_callback_section_module (const char *shortvar, const char *var,
252 const char *arguments, const char *value, lc_flags_t flags,
255 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
256 shortvar, var, arguments, value);
258 if (flags == LC_FLAGS_SECTIONSTART)
260 if (nesting_depth != 0)
262 fprintf (stderr, ERR_NOT_NESTED);
263 return (LC_CBRET_ERROR);
266 if (arguments == NULL)
268 fprintf (stderr, ERR_NEEDS_ARG, shortvar);
269 return (LC_CBRET_ERROR);
272 if ((current_module = strdup (arguments)) == NULL)
275 return (LC_CBRET_ERROR);
280 if (cf_search (current_module) != NULL)
281 return (LC_CBRET_OKAY);
283 return (LC_CBRET_IGNORESECTION);
285 else if (flags == LC_FLAGS_SECTIONEND)
287 if (current_module != NULL)
289 free (current_module);
290 current_module == NULL;
295 return (LC_CBRET_OKAY);
299 fprintf (stderr, ERR_SECTION_ONLY, shortvar);
300 return (LC_CBRET_ERROR);
304 int cf_callback_loadmodule (const char *shortvar, const char *var,
305 const char *arguments, const char *value, lc_flags_t flags,
308 DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
309 shortvar, var, arguments, value);
311 if (nesting_depth == 0)
313 fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
314 return (LC_CBRET_ERROR);
319 * - Write wrapper around `plugin_load' to resolve path/filename
320 * - Call this new, public function here
322 DBG ("Implement me, idiot!");
324 return (LC_CBRET_OKAY);
327 int cf_read (char *filename)
329 if (filename == NULL)
330 filename = CONFIGFILE;
332 lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
333 cf_callback_section_mode, NULL);
334 lc_register_callback ("Module", SHORTOPT_NONE, LC_VAR_SECTION,
335 cf_callback_section_module, NULL);
339 * - Add more directives, such as `DefaultMode', `DataDir', `PIDFile', ...
342 lc_register_callback ("Mode.LoadModule", SHORTOPT_NONE,
343 LC_VAR_STRING, cf_callback_loadmodule,
346 if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
348 /* FIXME: Use syslog here */
349 fprintf (stderr, "Error loading config file `%s': %s\n",
350 filename, lc_geterrstr ());
354 /* free memory and stuff */