Code

Added first version of ACPI support to the battery plugin
[collectd.git] / src / configfile.c
index 76e542178ab5e4b7a43e1bb6c59049294f8c5852..988f7312b1fca159cffac54dfbc99ddbdc302fc9 100644 (file)
@@ -20,6 +20,9 @@
  *   Florian octo Forster <octo at verplant.org>
  **/
 
+/* TODO
+ * make internal-only functions `static' */
+
 #include "collectd.h"
 
 #include "libconfig/libconfig.h"
@@ -59,14 +62,18 @@ typedef struct cf_mode_item
        int   mode;
 } cf_mode_item_t;
 
+/* TODO
+ * - LogFile
+ */
 static cf_mode_item_t cf_mode_list[] =
 {
        {"Server",      NULL, MODE_CLIENT                           },
        {"Port",        NULL, MODE_CLIENT | MODE_SERVER             },
        {"PIDFile",     NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL},
-       {"DataDir",     NULL, MODE_SERVER |               MODE_LOCAL}
+       {"DataDir",     NULL, MODE_SERVER | MODE_SERVER | MODE_LOCAL},
+       {"LogFile",     NULL, MODE_SERVER | MODE_SERVER | MODE_LOCAL}
 };
-static int cf_mode_num = 4;
+static int cf_mode_num = 5;
 
 static int nesting_depth = 0;
 static char *current_module = NULL;
@@ -101,6 +108,8 @@ int cf_dispatch (char *type, const char *orig_key, const char *orig_value)
        int ret;
        int i;
 
+       DBG ("type = %s, key = %s, value = %s", type, orig_key, orig_value);
+
        if ((cf_cb = cf_search (type)) == NULL)
        {
                syslog (LOG_WARNING, "Plugin `%s' did not register a callback.\n", type);
@@ -198,7 +207,7 @@ void cf_register (char *type,
 /*
  * Other query functions
  */
-char *cf_get_mode_option (const char *key)
+char *cf_get_option (const char *key, char *def)
 {
        int i;
 
@@ -207,8 +216,12 @@ char *cf_get_mode_option (const char *key)
                if ((cf_mode_list[i].mode & operating_mode) == 0)
                        continue;
 
-               if (strcasecmp (cf_mode_list[i].key, key) == 0)
+               if (strcasecmp (cf_mode_list[i].key, key) != 0)
+                       continue;
+
+               if (cf_mode_list[i].value != NULL)
                        return (cf_mode_list[i].value);
+               return (def);
        }
 
        return (NULL);
@@ -220,11 +233,9 @@ char *cf_get_mode_option (const char *key)
 
 /*
  * `cf_callback_mode'
- *   Start/end the `mode' section
+ *   Chose the `operating_mode'
  *
- * <Mode `arguments'>
- *   ...
- * </Mode>
+ * Mode `value'
  */
 int cf_callback_mode (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
@@ -233,48 +244,19 @@ int cf_callback_mode (const char *shortvar, const char *var,
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if (flags == LC_FLAGS_SECTIONSTART)
-       {
-               if (nesting_depth != 0)
-               {
-                       fprintf (stderr, ERR_NOT_NESTED);
-                       return (LC_CBRET_ERROR);
-               }
-
-               if (arguments == NULL)
-               {
-                       fprintf (stderr, ERR_NEEDS_ARG, shortvar);
-                       return (LC_CBRET_ERROR);
-               }
-
-               nesting_depth++;
-
-               if (((operating_mode == MODE_CLIENT)
-                                       && (strcasecmp (arguments, "Client") == 0))
-                               || ((operating_mode == MODE_SERVER)
-                                       && (strcasecmp (arguments, "Server") == 0))
-                               || ((operating_mode == MODE_LOCAL)
-                                       && (strcasecmp (arguments, "Local") == 0)))
-               {
-                       return (LC_CBRET_OKAY);
-               }
-               else
-               {
-                       return (LC_CBRET_IGNORESECTION);
-               }
-       }
-       else if (flags == LC_FLAGS_SECTIONEND)
-       {
-               nesting_depth--;
-
-               return (LC_CBRET_OKAY);
-       }
+       if (strcasecmp (value, "Client") == 0)
+               operating_mode = MODE_CLIENT;
+       else if (strcasecmp (value, "Server") == 0)
+               operating_mode = MODE_SERVER;
+       else if (strcasecmp (value, "Local") == 0)
+               operating_mode = MODE_LOCAL;
        else
        {
-               fprintf (stderr, ERR_SECTION_ONLY, shortvar);
+               syslog (LOG_ERR, "Invalid value for config option `Mode': `%s'", value);
                return (LC_CBRET_ERROR);
        }
 
+       return (LC_CBRET_OKAY);
 }
 
 /*
@@ -356,12 +338,6 @@ int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if (nesting_depth == 0)
-       {
-               fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
-               return (LC_CBRET_ERROR);
-       }
-
        if (plugin_load (value))
                syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
 
@@ -370,7 +346,14 @@ int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
        return (LC_CBRET_OKAY);
 }
 
-/* XXX think about how to do the command line stuff */
+/*
+ * `cf_callback_mode_switch'
+ *   Change the contents of the global variable `operating_mode'
+ *
+ *   This should be command line options. One *can* do this in the config
+ *   files, but I will not document this. Don't whine abount it not working as
+ *   you expect if you do it anyways.
+ */
 int cf_callback_mode_switch (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
@@ -482,50 +465,54 @@ int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
        return (LC_CBRET_OKAY);
 }
 
-int cf_read (char *filename)
+void cf_init (void)
 {
+       static int run_once = 0;
        int i;
 
-       if (filename == NULL)
-               filename = CONFIGFILE;
-
-       lc_register_callback ("Client", 'c', LC_VAR_NONE,
-                       cf_callback_mode_switch, NULL);
-       lc_register_callback ("Local", 'l', LC_VAR_NONE,
-                       cf_callback_mode_switch, NULL);
-       lc_register_callback ("Server", 's', LC_VAR_NONE,
-                       cf_callback_mode_switch, NULL);
+       if (run_once != 0)
+               return;
+       run_once = 1;
 
-       lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
+       lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_STRING,
                        cf_callback_mode, NULL);
        lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
                        cf_callback_plugin, NULL);
 
-       lc_register_callback ("Mode.PluginDir", 'P',
+       lc_register_callback ("PluginDir", SHORTOPT_NONE,
                        LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
-       lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
+       lc_register_callback ("LoadPlugin", SHORTOPT_NONE,
                        LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
 
        for (i = 0; i < cf_mode_num; i++)
        {
-               char            longvar[256];
                cf_mode_item_t *item;
 
                item = &cf_mode_list[i];
 
-               if (snprintf (longvar, 256, "Mode.%s", item->key) >= 256)
-                       continue;
-
-               lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
+               lc_register_callback (item->key, SHORTOPT_NONE, LC_VAR_STRING,
                                cf_callback_mode_option, (void *) item);
        }
+}
 
+int cf_read (char *filename)
+{
+       cf_init ();
+
+       if (filename == NULL)
+               filename = CONFIGFILE;
+
+       DBG ("Starting to parse file `%s'", filename);
+
+       /* int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type); */
        if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
        {
                syslog (LOG_ERR, "lc_process_file (%s): %s", filename, lc_geterrstr ());
                return (-1);
        }
 
+       DBG ("Done parsing file `%s'", filename);
+
        /* free memory and stuff */
        lc_cleanup ();