X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftable.c;h=c6b5badfff43c7757686ec25cb7820a4bf2c42a9;hb=b24fe01f603e1422de29c2cf090eabb043df1965;hp=2911bf026a6405371f89a7719864e8a3c711fee0;hpb=04b395325b152a5ddf424d1a750f455a2f8229fb;p=collectd.git diff --git a/src/table.c b/src/table.c index 2911bf02..aebf0bbf 100644 --- a/src/table.c +++ b/src/table.c @@ -1,19 +1,24 @@ /** * collectd - src/table.c - * Copyright (C) 2009 Sebastian Harl + * Copyright (C) 2009 Sebastian Harl * - * 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 - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: * Sebastian Harl @@ -37,12 +42,12 @@ */ typedef struct { - char *type; - char *instance_prefix; - int *instances; - size_t instances_num; - int *values; - size_t values_num; + char *type; + char *instance_prefix; + size_t *instances; + size_t instances_num; + size_t *values; + size_t values_num; const data_set_t *ds; } tbl_result_t; @@ -134,11 +139,11 @@ static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci) return 0; } /* tbl_config_set_separator */ -static int tbl_config_append_array_i (char *name, int **var, size_t *len, +static int tbl_config_append_array_i (char *name, size_t **var, size_t *len, oconfig_item_t *ci) { - int *tmp; - + size_t *tmp; + size_t num; size_t i; if (1 > ci->values_num) { @@ -146,26 +151,28 @@ static int tbl_config_append_array_i (char *name, int **var, size_t *len, return 1; } - for (i = 0; i < ci->values_num; ++i) { + num = (size_t) ci->values_num; + for (i = 0; i < num; ++i) { if (OCONFIG_TYPE_NUMBER != ci->values[i].type) { log_err ("\"%s\" expects numerical arguments only.", name); return 1; } } - *len += ci->values_num; - tmp = (int *)realloc (*var, *len * sizeof (**var)); + tmp = realloc (*var, ((*len) + num) * sizeof (**var)); if (NULL == tmp) { char errbuf[1024]; log_err ("realloc failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return -1; } - *var = tmp; - for (i = *len - ci->values_num; i < *len; ++i) - (*var)[i] = (int)ci->values[i].value.number; + for (i = 0; i < num; ++i) { + (*var)[*len] = (size_t) ci->values[i].value.number; + (*len)++; + } + return 0; } /* tbl_config_append_array_s */ @@ -174,7 +181,7 @@ static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci) tbl_result_t *res; int status = 0; - size_t i; + int i; if (0 != ci->values_num) { log_err (" does not expect any arguments."); @@ -183,7 +190,7 @@ static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci) res = (tbl_result_t *)realloc (tbl->results, (tbl->results_num + 1) * sizeof (*tbl->results)); - if (NULL == tbl) { + if (res == NULL) { char errbuf[1024]; log_err ("realloc failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); @@ -261,7 +268,7 @@ static int tbl_config_table (oconfig_item_t *ci) tbl = tables + tables_num - 1; tbl_setup (tbl, ci->values[0].value.string); - for (i = 0; i < ci->children_num; ++i) { + for (i = 0; i < ((size_t) ci->children_num); ++i) { oconfig_item_t *c = ci->children + i; if (0 == strcasecmp (c->key, "Separator")) @@ -278,8 +285,9 @@ static int tbl_config_table (oconfig_item_t *ci) if (NULL == tbl->sep) { log_err ("Table \"%s\" does not specify any separator.", tbl->file); status = 1; + } else { + strunescape (tbl->sep, strlen (tbl->sep) + 1); } - strunescape (tbl->sep, strlen (tbl->sep) + 1); if (NULL == tbl->instance) { tbl->instance = sstrdup (tbl->file); @@ -315,7 +323,7 @@ static int tbl_config_table (oconfig_item_t *ci) static int tbl_config (oconfig_item_t *ci) { - size_t i; + int i; for (i = 0; i < ci->children_num; ++i) { oconfig_item_t *c = ci->children + i; @@ -346,9 +354,9 @@ static int tbl_prepare (tbl_t *tbl) return -1; } - if (res->values_num != (size_t)res->ds->ds_num) { + if (res->values_num != res->ds->ds_num) { log_err ("Invalid type \"%s\". Expected %zu data source%s, " - "got %i.", res->type, res->values_num, + "got %zu.", res->type, res->values_num, (1 == res->values_num) ? "" : "s", res->ds->ds_num); return -1; @@ -383,7 +391,7 @@ static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res, assert (res->values[i] < fields_num); value = fields[res->values[i]]; - if (0 != parse_value (value, &values[i], res->ds->ds[i])) + if (0 != parse_value (value, &values[i], res->ds->ds[i].type)) return -1; } @@ -450,7 +458,7 @@ static int tbl_parse_line (tbl_t *tbl, char *line, size_t len) } if (i <= tbl->max_colnum) { - log_err ("Not enough columns in line " + log_warn ("Not enough columns in line " "(expected at least %zu, got %zu).", tbl->max_colnum + 1, i); return -1; @@ -482,11 +490,11 @@ static int tbl_read_table (tbl_t *tbl) while (NULL != fgets (buf, sizeof (buf), fh)) { if ('\0' != buf[sizeof (buf) - 1]) { buf[sizeof (buf) - 1] = '\0'; - log_err ("Table %s: Truncated line: %s", tbl->file, buf); + log_warn ("Table %s: Truncated line: %s", tbl->file, buf); } if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) { - log_err ("Table %s: Failed to parse line: %s", tbl->file, buf); + log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf); continue; } }