X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftarget_replace.c;h=382fc3575de6b0aca4c7b3926366dea0cbae832c;hb=79963d13c1884d1d92667cc502ad20758b084a12;hp=40a6fec992687466720d11abea95778fb5984aa2;hpb=ed985cb9d3d89aec3490b17e0fda3ee97908f935;p=collectd.git diff --git a/src/target_replace.c b/src/target_replace.c index 40a6fec9..382fc357 100644 --- a/src/target_replace.c +++ b/src/target_replace.c @@ -34,8 +34,7 @@ struct tr_action_s; typedef struct tr_action_s tr_action_t; -struct tr_action_s -{ +struct tr_action_s { regex_t re; char *replacement; int may_be_empty; @@ -43,8 +42,7 @@ struct tr_action_s tr_action_t *next; }; -struct tr_data_s -{ +struct tr_data_s { tr_action_t *host; tr_action_t *plugin; tr_action_t *plugin_instance; @@ -53,7 +51,7 @@ struct tr_data_s }; typedef struct tr_data_s tr_data_t; -static char *tr_strdup (const char *orig) /* {{{ */ +static char *tr_strdup(const char *orig) /* {{{ */ { size_t sz; char *dest; @@ -61,86 +59,80 @@ static char *tr_strdup (const char *orig) /* {{{ */ if (orig == NULL) return (NULL); - sz = strlen (orig) + 1; - dest = malloc (sz); + sz = strlen(orig) + 1; + dest = malloc(sz); if (dest == NULL) return (NULL); - memcpy (dest, orig, sz); + memcpy(dest, orig, sz); return (dest); } /* }}} char *tr_strdup */ -static void tr_action_destroy (tr_action_t *act) /* {{{ */ +static void tr_action_destroy(tr_action_t *act) /* {{{ */ { if (act == NULL) return; - regfree (&act->re); - sfree (act->replacement); + regfree(&act->re); + sfree(act->replacement); if (act->next != NULL) - tr_action_destroy (act->next); + tr_action_destroy(act->next); - sfree (act); + sfree(act); } /* }}} void tr_action_destroy */ -static int tr_config_add_action (tr_action_t **dest, /* {{{ */ - const oconfig_item_t *ci, int may_be_empty) -{ +static int tr_config_add_action(tr_action_t **dest, /* {{{ */ + const oconfig_item_t *ci, int may_be_empty) { tr_action_t *act; int status; if (dest == NULL) return (-EINVAL); - if ((ci->values_num != 2) - || (ci->values[0].type != OCONFIG_TYPE_STRING) - || (ci->values[1].type != OCONFIG_TYPE_STRING)) - { - ERROR ("Target `replace': The `%s' option requires exactly two string " - "arguments.", ci->key); + if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) || + (ci->values[1].type != OCONFIG_TYPE_STRING)) { + ERROR("Target `replace': The `%s' option requires exactly two string " + "arguments.", + ci->key); return (-1); } - act = calloc (1, sizeof (*act)); - if (act == NULL) - { - ERROR ("tr_config_add_action: calloc failed."); + act = calloc(1, sizeof(*act)); + if (act == NULL) { + ERROR("tr_config_add_action: calloc failed."); return (-ENOMEM); } act->replacement = NULL; act->may_be_empty = may_be_empty; - status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED); - if (status != 0) - { + status = regcomp(&act->re, ci->values[0].value.string, REG_EXTENDED); + if (status != 0) { char errbuf[1024] = ""; /* regerror assures null termination. */ - regerror (status, &act->re, errbuf, sizeof (errbuf)); - ERROR ("Target `replace': Compiling the regular expression `%s' " - "failed: %s.", - ci->values[0].value.string, errbuf); - sfree (act); + regerror(status, &act->re, errbuf, sizeof(errbuf)); + ERROR("Target `replace': Compiling the regular expression `%s' " + "failed: %s.", + ci->values[0].value.string, errbuf); + sfree(act); return (-EINVAL); } - act->replacement = tr_strdup (ci->values[1].value.string); - if (act->replacement == NULL) - { - ERROR ("tr_config_add_action: tr_strdup failed."); - regfree (&act->re); - sfree (act); + act->replacement = tr_strdup(ci->values[1].value.string); + if (act->replacement == NULL) { + ERROR("tr_config_add_action: tr_strdup failed."); + regfree(&act->re); + sfree(act); return (-ENOMEM); } /* Insert action at end of list. */ if (*dest == NULL) *dest = act; - else - { + else { tr_action_t *prev; prev = *dest; @@ -153,69 +145,64 @@ static int tr_config_add_action (tr_action_t **dest, /* {{{ */ return (0); } /* }}} int tr_config_add_action */ -static int tr_action_invoke (tr_action_t *act_head, /* {{{ */ - char *buffer_in, size_t buffer_in_size, int may_be_empty) -{ +static int tr_action_invoke(tr_action_t *act_head, /* {{{ */ + char *buffer_in, size_t buffer_in_size, + int may_be_empty) { int status; char buffer[DATA_MAX_NAME_LEN]; - regmatch_t matches[8] = { [0] = { 0 } }; + regmatch_t matches[8] = {[0] = {0}}; if (act_head == NULL) return (-EINVAL); - sstrncpy (buffer, buffer_in, sizeof (buffer)); + sstrncpy(buffer, buffer_in, sizeof(buffer)); - DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer); + DEBUG("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer); - for (tr_action_t *act = act_head; act != NULL; act = act->next) - { + for (tr_action_t *act = act_head; act != NULL; act = act->next) { char temp[DATA_MAX_NAME_LEN]; char *subst_status; - status = regexec (&act->re, buffer, - STATIC_ARRAY_SIZE (matches), matches, - /* flags = */ 0); + status = regexec(&act->re, buffer, STATIC_ARRAY_SIZE(matches), matches, + /* flags = */ 0); if (status == REG_NOMATCH) continue; - else if (status != 0) - { + else if (status != 0) { char errbuf[1024] = ""; - regerror (status, &act->re, errbuf, sizeof (errbuf)); - ERROR ("Target `replace': Executing a regular expression failed: %s.", - errbuf); + regerror(status, &act->re, errbuf, sizeof(errbuf)); + ERROR("Target `replace': Executing a regular expression failed: %s.", + errbuf); continue; } - subst_status = subst (temp, sizeof (temp), buffer, - (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement); - if (subst_status == NULL) - { - ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, " - "replacement = %s) failed.", - buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, - act->replacement); + subst_status = subst(temp, sizeof(temp), buffer, (size_t)matches[0].rm_so, + (size_t)matches[0].rm_eo, act->replacement); + if (subst_status == NULL) { + ERROR("Target `replace': subst (buffer = %s, start = %zu, end = %zu, " + "replacement = %s) failed.", + buffer, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo, + act->replacement); continue; } - sstrncpy (buffer, temp, sizeof (buffer)); + sstrncpy(buffer, temp, sizeof(buffer)); - DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer); + DEBUG("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer); } /* for (act = act_head; act != NULL; act = act->next) */ - if ((may_be_empty == 0) && (buffer[0] == 0)) - { - WARNING ("Target `replace': Replacement resulted in an empty string, " - "which is not allowed for this buffer (`host' or `plugin')."); + if ((may_be_empty == 0) && (buffer[0] == 0)) { + WARNING("Target `replace': Replacement resulted in an empty string, " + "which is not allowed for this buffer (`host' or `plugin')."); return (0); } - DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer); - sstrncpy (buffer_in, buffer, buffer_in_size); + DEBUG("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer); + sstrncpy(buffer_in, buffer, buffer_in_size); return (0); } /* }}} int tr_action_invoke */ -static int tr_destroy (void **user_data) /* {{{ */ +static int tr_destroy(void **user_data) /* {{{ */ { tr_data_t *data; @@ -226,25 +213,24 @@ static int tr_destroy (void **user_data) /* {{{ */ if (data == NULL) return (0); - tr_action_destroy (data->host); - tr_action_destroy (data->plugin); - tr_action_destroy (data->plugin_instance); + tr_action_destroy(data->host); + tr_action_destroy(data->plugin); + tr_action_destroy(data->plugin_instance); /* tr_action_destroy (data->type); */ - tr_action_destroy (data->type_instance); - sfree (data); + tr_action_destroy(data->type_instance); + sfree(data); return (0); } /* }}} int tr_destroy */ -static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ +static int tr_create(const oconfig_item_t *ci, void **user_data) /* {{{ */ { tr_data_t *data; int status; - data = calloc (1, sizeof (*data)); - if (data == NULL) - { - ERROR ("tr_create: calloc failed."); + data = calloc(1, sizeof(*data)); + if (data == NULL) { + ERROR("tr_create: calloc failed."); return (-ENOMEM); } @@ -255,32 +241,31 @@ static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ data->type_instance = NULL; status = 0; - for (int i = 0; i < ci->children_num; i++) - { + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; - if ((strcasecmp ("Host", child->key) == 0) - || (strcasecmp ("Hostname", child->key) == 0)) - status = tr_config_add_action (&data->host, child, - /* may be empty = */ 0); - else if (strcasecmp ("Plugin", child->key) == 0) - status = tr_config_add_action (&data->plugin, child, - /* may be empty = */ 0); - else if (strcasecmp ("PluginInstance", child->key) == 0) - status = tr_config_add_action (&data->plugin_instance, child, - /* may be empty = */ 1); + if ((strcasecmp("Host", child->key) == 0) || + (strcasecmp("Hostname", child->key) == 0)) + status = tr_config_add_action(&data->host, child, + /* may be empty = */ 0); + else if (strcasecmp("Plugin", child->key) == 0) + status = tr_config_add_action(&data->plugin, child, + /* may be empty = */ 0); + else if (strcasecmp("PluginInstance", child->key) == 0) + status = tr_config_add_action(&data->plugin_instance, child, + /* may be empty = */ 1); #if 0 else if (strcasecmp ("Type", child->key) == 0) status = tr_config_add_action (&data->type, child, /* may be empty = */ 0); #endif - else if (strcasecmp ("TypeInstance", child->key) == 0) - status = tr_config_add_action (&data->type_instance, child, - /* may be empty = */ 1); - else - { - ERROR ("Target `replace': The `%s' configuration option is not understood " - "and will be ignored.", child->key); + else if (strcasecmp("TypeInstance", child->key) == 0) + status = tr_config_add_action(&data->type_instance, child, + /* may be empty = */ 1); + else { + ERROR("Target `replace': The `%s' configuration option is not understood " + "and will be ignored.", + child->key); status = 0; } @@ -289,25 +274,21 @@ static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ } /* Additional sanity-checking */ - while (status == 0) - { - if ((data->host == NULL) - && (data->plugin == NULL) - && (data->plugin_instance == NULL) + while (status == 0) { + if ((data->host == NULL) && (data->plugin == NULL) && + (data->plugin_instance == NULL) /* && (data->type == NULL) */ - && (data->type_instance == NULL)) - { - ERROR ("Target `replace': You need to set at least one of `Host', " - "`Plugin', `PluginInstance' or `TypeInstance'."); + && (data->type_instance == NULL)) { + ERROR("Target `replace': You need to set at least one of `Host', " + "`Plugin', `PluginInstance' or `TypeInstance'."); status = -1; } break; } - if (status != 0) - { - tr_destroy ((void *) &data); + if (status != 0) { + tr_destroy((void *)&data); return (status); } @@ -315,42 +296,39 @@ static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ return (0); } /* }}} int tr_create */ -static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */ - notification_meta_t __attribute__((unused)) **meta, void **user_data) -{ +static int tr_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */ + notification_meta_t __attribute__((unused)) * *meta, + void **user_data) { tr_data_t *data; if ((ds == NULL) || (vl == NULL) || (user_data == NULL)) return (-EINVAL); data = *user_data; - if (data == NULL) - { - ERROR ("Target `replace': Invoke: `data' is NULL."); + if (data == NULL) { + ERROR("Target `replace': Invoke: `data' is NULL."); return (-EINVAL); } -#define HANDLE_FIELD(f,e) \ - if (data->f != NULL) \ - tr_action_invoke (data->f, vl->f, sizeof (vl->f), e) - HANDLE_FIELD (host, 0); - HANDLE_FIELD (plugin, 0); - HANDLE_FIELD (plugin_instance, 1); +#define HANDLE_FIELD(f, e) \ + if (data->f != NULL) \ + tr_action_invoke(data->f, vl->f, sizeof(vl->f), e) + HANDLE_FIELD(host, 0); + HANDLE_FIELD(plugin, 0); + HANDLE_FIELD(plugin_instance, 1); /* HANDLE_FIELD (type); */ - HANDLE_FIELD (type_instance, 1); + HANDLE_FIELD(type_instance, 1); return (FC_TARGET_CONTINUE); } /* }}} int tr_invoke */ -void module_register (void) -{ - target_proc_t tproc = { 0 }; +void module_register(void) { + target_proc_t tproc = {0}; - tproc.create = tr_create; - tproc.destroy = tr_destroy; - tproc.invoke = tr_invoke; - fc_register_target ("replace", tproc); + tproc.create = tr_create; + tproc.destroy = tr_destroy; + tproc.invoke = tr_invoke; + fc_register_target("replace", tproc); } /* module_register */ /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */ -