Changed `plugin_set_dir' to take a `const' argument.
authorocto <octo>
Tue, 20 Dec 2005 08:03:34 +0000 (08:03 +0000)
committerocto <octo>
Tue, 20 Dec 2005 08:03:34 +0000 (08:03 +0000)
Reorganized the `configfile.c' file. Changed callback names, their order and added comments.
Added `cf_callback_mode_plugindir' to allow the config file to change the plugin directory.

src/configfile.c
src/plugin.c
src/plugin.h

index a061004..76e5421 100644 (file)
@@ -64,16 +64,15 @@ 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},
-       {"PluginDir",   NULL, MODE_CLIENT | MODE_SERVER | MODE_LOCAL}
+       {"DataDir",     NULL, MODE_SERVER |               MODE_LOCAL}
 };
-static int cf_mode_num = 5;
+static int cf_mode_num = 4;
 
 static int nesting_depth = 0;
 static char *current_module = NULL;
 
-/* cf_register needs this prototype */
-int cf_callback_dispatch (const char *, const char *, const char *,
+/* `cf_register' needs this prototype */
+int cf_callback_plugin_dispatch (const char *, const char *, const char *,
                const char *, lc_flags_t, void *);
 
 /*
@@ -186,7 +185,7 @@ void cf_register (char *type,
                         * `key', but apparently `lc_register_*' can handle
                         * it.. */
                        lc_register_callback (buf, SHORTOPT_NONE,
-                                       LC_VAR_STRING, cf_callback_dispatch,
+                                       LC_VAR_STRING, cf_callback_plugin_dispatch,
                                        NULL);
                }
                else
@@ -215,35 +214,98 @@ char *cf_get_mode_option (const char *key)
        return (NULL);
 }
 
-/* 
- * Functions for the actual parsing
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * Functions for the actual parsing                                    *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+/*
+ * `cf_callback_mode'
+ *   Start/end the `mode' section
+ *
+ * <Mode `arguments'>
+ *   ...
+ * </Mode>
  */
-int cf_callback_dispatch (const char *shortvar, const char *var,
+int cf_callback_mode (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if ((nesting_depth == 0) || (current_module == NULL))
+       if (flags == LC_FLAGS_SECTIONSTART)
        {
-               fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
-               return (LC_CBRET_ERROR);
+               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--;
 
-       /* Send the data to the plugin */
-       if (cf_dispatch (current_module, shortvar, value) < 0)
+               return (LC_CBRET_OKAY);
+       }
+       else
+       {
+               fprintf (stderr, ERR_SECTION_ONLY, shortvar);
                return (LC_CBRET_ERROR);
+       }
+
+}
+
+/*
+ * `cf_callback_mode_plugindir'
+ *   Change the plugin directory
+ *
+ * <Mode xxx>
+ *   PluginDir `value'
+ * </Mode>
+ */
+int cf_callback_mode_plugindir (const char *shortvar, const char *var,
+               const char *arguments, const char *value, lc_flags_t flags,
+               void *extra)
+{
+       DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
+                       shortvar, var, arguments, value);
+
+       plugin_set_dir (value);
 
        return (LC_CBRET_OKAY);
 }
 
-int cf_callback_options_mode (const char *shortvar, const char *var,
+int cf_callback_mode_option (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        cf_mode_item_t *item;
 
+       DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
+                       shortvar, var, arguments, value);
+
        if (extra == NULL)
        {
                fprintf (stderr, "No extra..?\n");
@@ -279,58 +341,67 @@ int cf_callback_options_mode (const char *shortvar, const char *var,
        return (LC_CBRET_OKAY);
 }
 
-int cf_callback_section_mode (const char *shortvar, const char *var,
+/*
+ * `cf_callback_mode_loadmodule':
+ *   Load a plugin.
+ *
+ * <Mode xxx>
+ *   LoadPlugin `value'
+ * </Mode>
+ */
+int cf_callback_mode_loadmodule (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if (flags == LC_FLAGS_SECTIONSTART)
+       if (nesting_depth == 0)
        {
-               if (nesting_depth != 0)
-               {
-                       fprintf (stderr, ERR_NOT_NESTED);
-                       return (LC_CBRET_ERROR);
-               }
+               fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
+               return (LC_CBRET_ERROR);
+       }
 
-               if (arguments == NULL)
-               {
-                       fprintf (stderr, ERR_NEEDS_ARG, shortvar);
-                       return (LC_CBRET_ERROR);
-               }
+       if (plugin_load (value))
+               syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", value);
 
-               nesting_depth++;
+       /* Return `okay' even if there was an error, because it's not a syntax
+        * problem.. */
+       return (LC_CBRET_OKAY);
+}
 
-               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--;
+/* XXX think about how to do the command line stuff */
+int cf_callback_mode_switch (const char *shortvar, const char *var,
+               const char *arguments, const char *value, lc_flags_t flags,
+               void *extra)
+{
+       DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
+                       shortvar, var, arguments, value);
 
-               return (LC_CBRET_OKAY);
-       }
+       if (strcasecmp (shortvar, "Client") == 0)
+               operating_mode = MODE_CLIENT;
+       else if (strcasecmp (shortvar, "Local") == 0)
+               operating_mode = MODE_LOCAL;
+       else if (strcasecmp (shortvar, "Server") == 0)
+               operating_mode = MODE_SERVER;
        else
        {
-               fprintf (stderr, ERR_SECTION_ONLY, shortvar);
+               fprintf (stderr, "cf_callback_mode_switch: Wrong mode!\n");
                return (LC_CBRET_ERROR);
        }
 
+       return (LC_CBRET_OKAY);
 }
 
-int cf_callback_section_module (const char *shortvar, const char *var,
+/*
+ * `cf_callback_plugin'
+ *   Start/end section `plugin'
+ *
+ * <Plugin `arguments'>
+ *   ...
+ * </Plugin>
+ */
+int cf_callback_plugin (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
@@ -383,24 +454,31 @@ int cf_callback_section_module (const char *shortvar, const char *var,
        }
 }
 
-int cf_callback_loadmodule (const char *shortvar, const char *var,
+/*
+ * `cf_callback_plugin_dispatch'
+ *   Send options within `plugin' sections to the plugin that requests it.
+ *
+ * <Plugin `current_module'>
+ *   `var' `value'
+ * </Plugin>
+ */
+int cf_callback_plugin_dispatch (const char *shortvar, const char *var,
                const char *arguments, const char *value, lc_flags_t flags,
                void *extra)
 {
        DBG ("shortvar = %s, var = %s, arguments = %s, value = %s, ...",
                        shortvar, var, arguments, value);
 
-       if (nesting_depth == 0)
+       if ((nesting_depth == 0) || (current_module == NULL))
        {
                fprintf (stderr, ERR_NEEDS_SECTION, shortvar);
                return (LC_CBRET_ERROR);
        }
 
-       if (plugin_load (value))
-               syslog (LOG_ERR, "plugin_load (%s): failed to load plugin", shortvar);
+       /* Send the data to the plugin */
+       if (cf_dispatch (current_module, shortvar, value) < 0)
+               return (LC_CBRET_ERROR);
 
-       /* Return `okay' even if there was an error, because it's not a syntax
-        * problem.. */
        return (LC_CBRET_OKAY);
 }
 
@@ -411,14 +489,22 @@ int cf_read (char *filename)
        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);
+
        lc_register_callback ("Mode", SHORTOPT_NONE, LC_VAR_SECTION,
-                       cf_callback_section_mode, NULL);
+                       cf_callback_mode, NULL);
        lc_register_callback ("Plugin", SHORTOPT_NONE, LC_VAR_SECTION,
-                       cf_callback_section_module, NULL);
+                       cf_callback_plugin, NULL);
 
+       lc_register_callback ("Mode.PluginDir", 'P',
+                       LC_VAR_STRING, cf_callback_mode_plugindir, NULL);
        lc_register_callback ("Mode.LoadPlugin", SHORTOPT_NONE,
-                       LC_VAR_STRING, cf_callback_loadmodule,
-                       NULL);
+                       LC_VAR_STRING, cf_callback_mode_loadmodule, NULL);
 
        for (i = 0; i < cf_mode_num; i++)
        {
@@ -431,7 +517,7 @@ int cf_read (char *filename)
                        continue;
 
                lc_register_callback (longvar, SHORTOPT_NONE, LC_VAR_STRING,
-                               cf_callback_options_mode, (void *) item);
+                               cf_callback_mode_option, (void *) item);
        }
 
        if (lc_process_file ("collectd", filename, LC_CONF_APACHE))
index ce4c943..abc08e5 100644 (file)
@@ -52,7 +52,7 @@ char *plugin_get_dir (void)
                return (plugindir);
 }
 
-void plugin_set_dir (char *dir)
+void plugin_set_dir (const char *dir)
 {
        if (plugindir != NULL)
                free (plugindir);
index 8749327..a07c205 100644 (file)
@@ -36,7 +36,7 @@
  * NOTES
  *  If `dir' is NULL the compiled in default `PLUGINDIR' is used.
  */
-void plugin_set_dir (char *dir);
+void plugin_set_dir (const char *dir);
 
 /*
  * NAME