From 35a61709925727b49e68a5f084e74c47e3402d32 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 28 May 2007 21:43:31 +0200 Subject: [PATCH] src/configfile.c: Pass the `oconfig_item_t' types to the plugins if they request it. --- src/configfile.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/configfile.h | 9 +++++-- src/plugin.c | 12 ++++++++++ src/plugin.h | 10 ++++++-- 4 files changed, 98 insertions(+), 6 deletions(-) diff --git a/src/configfile.c b/src/configfile.c index 4b6803ed..f4e9c604 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -42,6 +42,13 @@ typedef struct cf_callback struct cf_callback *next; } cf_callback_t; +typedef struct cf_complex_callback_s +{ + char *type; + int (*callback) (oconfig_item_t *); + struct cf_complex_callback_s *next; +} cf_complex_callback_t; + typedef struct cf_value_map_s { char *key; @@ -65,6 +72,7 @@ static int dispatch_value_loadplugin (const oconfig_item_t *ci); * Private variables */ static cf_callback_t *first_callback = NULL; +static cf_complex_callback_t *complex_callback_head = NULL; static cf_value_map_t cf_value_map[] = { @@ -257,21 +265,29 @@ static int dispatch_block_plugin (oconfig_item_t *ci) int i; char *name; + cf_complex_callback_t *cb; + if (strcasecmp (ci->key, "Plugin") != 0) return (-1); - if (ci->values_num != 1) + if (ci->values_num < 1) return (-1); if (ci->values[0].type != OCONFIG_TYPE_STRING) return (-1); name = ci->values[0].value.string; + /* Check for a complex callback first */ + for (cb = complex_callback_head; cb != NULL; cb = cb->next) + if (strcasecmp (name, cb->type) == 0) + return (cb->callback (ci)); + + /* Hm, no complex plugin found. Dispatch the values one by one */ for (i = 0; i < ci->children_num; i++) { if (ci->children[i].children == NULL) dispatch_value_plugin (name, ci->children + i); else - {DEBUG ("No nested config blocks allow for plugins. Yet.");} + {DEBUG ("No nested config blocks allow for this plugin.");} } return (0); @@ -347,6 +363,26 @@ void cf_unregister (const char *type) } } /* void cf_unregister */ +void cf_unregister_complex (const char *type) +{ + cf_complex_callback_t *this, *prev; + + for (prev = NULL, this = complex_callback_head; + this != NULL; + prev = this, this = this->next) + if (strcasecmp (this->type, type) == 0) + { + if (prev == NULL) + complex_callback_head = this->next; + else + prev->next = this->next; + + sfree (this->type); + sfree (this); + break; + } +} /* void cf_unregister */ + void cf_register (const char *type, int (*callback) (const char *, const char *), const char **keys, int keys_num) @@ -369,6 +405,39 @@ void cf_register (const char *type, first_callback = cf_cb; } /* void cf_register */ +int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *)) +{ + cf_complex_callback_t *new; + + new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t)); + if (new == NULL) + return (-1); + + new->type = strdup (type); + if (new->type == NULL) + { + sfree (new); + return (-1); + } + + new->callback = callback; + new->next = NULL; + + if (complex_callback_head == NULL) + { + complex_callback_head = new; + } + else + { + cf_complex_callback_t *last = complex_callback_head; + while (last->next != NULL) + last = last->next; + last->next = new; + } + + return (0); +} /* int cf_register_complex */ + int cf_read (char *filename) { oconfig_item_t *conf; diff --git a/src/configfile.h b/src/configfile.h index 0ee8f33c..3952c180 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -1,3 +1,5 @@ +#ifndef CONFIGFILE_H +#define CONFIGFILE_H /** * collectd - src/configfile.h * Copyright (C) 2005,2006 Florian octo Forster @@ -20,8 +22,8 @@ * Florian octo Forster **/ -#ifndef CONFIGFILE_H -#define CONFIGFILE_H +#include "collectd.h" +#include "liboconfig/oconfig.h" /* * DESCRIPTION @@ -32,6 +34,7 @@ * `plugin_register' */ void cf_unregister (const char *type); +void cf_unregister_complex (const char *type); /* * DESCRIPTION @@ -61,6 +64,8 @@ void cf_register (const char *type, int (*callback) (const char *, const char *), const char **keys, int keys_num); +int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *)); + /* * DESCRIPTION * `cf_read' reads the config file `filename' and dispatches the read diff --git a/src/plugin.c b/src/plugin.c index da662040..6272581f 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -374,6 +374,12 @@ int plugin_register_config (const char *name, return (0); } /* int plugin_register_config */ +int plugin_register_complex_config (const char *type, + int (*callback) (oconfig_item_t *)) +{ + return (cf_register_complex (type, callback)); +} /* int plugin_register_complex_config */ + int plugin_register_init (const char *name, int (*callback) (void)) { @@ -458,6 +464,12 @@ int plugin_unregister_config (const char *name) return (0); } /* int plugin_unregister_config */ +int plugin_unregister_complex_config (const char *name) +{ + cf_unregister_complex (name); + return (0); +} /* int plugin_unregister_complex_config */ + int plugin_unregister_init (const char *name) { return (plugin_unregister (list_init, name)); diff --git a/src/plugin.h b/src/plugin.h index 83c21094..72d8adb6 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -1,9 +1,8 @@ #ifndef PLUGIN_H #define PLUGIN_H - /** * collectd - src/plugin.h - * Copyright (C) 2005,2006 Florian octo Forster + * Copyright (C) 2005-2007 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -22,6 +21,9 @@ * Florian octo Forster **/ +#include "collectd.h" +#include "configfile.h" + #define DATA_MAX_NAME_LEN 64 #define DS_TYPE_COUNTER 0 @@ -143,6 +145,8 @@ void plugin_shutdown_all (void); int plugin_register_config (const char *name, int (*callback) (const char *key, const char *val), const char **keys, int keys_num); +int plugin_register_complex_config (const char *type, + int (*callback) (oconfig_item_t *)); int plugin_register_init (const char *name, int (*callback) (void)); int plugin_register_read (const char *name, @@ -156,6 +160,7 @@ int plugin_register_log (char *name, void (*callback) (int, const char *)); int plugin_unregister_config (const char *name); +int plugin_unregister_complex_config (const char *name); int plugin_unregister_init (const char *name); int plugin_unregister_read (const char *name); int plugin_unregister_write (const char *name); @@ -163,6 +168,7 @@ int plugin_unregister_shutdown (const char *name); int plugin_unregister_data_set (const char *name); int plugin_unregister_log (const char *name); + /* * NAME * plugin_dispatch_values -- 2.11.0