From 8a3753760230b270c8f4615891cba2586a661d21 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 2 Jul 2010 19:27:44 +0200 Subject: [PATCH] Various modules: Use the new "rrd_args_t" structure. --- src/action_graph.c | 40 ++++++++++++++++++++++++++-------------- src/graph.c | 14 +++++++------- src/graph.h | 3 ++- src/graph_def.c | 29 +++++++++++++++-------------- src/graph_def.h | 3 ++- src/graph_instance.c | 6 +++--- src/graph_instance.h | 3 ++- src/rrd_args.c | 10 +++++++++- src/rrd_args.h | 2 ++ 9 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/action_graph.c b/src/action_graph.c index d496a24..8789730 100644 --- a/src/action_graph.c +++ b/src/action_graph.c @@ -23,7 +23,7 @@ struct graph_data_s { - str_array_t *args; + rrd_args_t *args; rrd_info_t *info; time_t mtime; time_t expires; @@ -99,10 +99,10 @@ static int get_time_args (graph_data_t *data) /* {{{ */ data->begin = begin; data->end = end; - array_append (data->args, "-s"); - array_append_format (data->args, "%li", begin); - array_append (data->args, "-e"); - array_append_format (data->args, "%li", end); + array_append (data->args->options, "-s"); + array_append_format (data->args->options, "%li", begin); + array_append (data->args->options, "-e"); + array_append_format (data->args->options, "%li", end); return (0); } /* }}} int get_time_args */ @@ -206,6 +206,9 @@ int action_graph (void) /* {{{ */ graph_instance_t *inst; int status; + int argc; + char **argv; + cfg = gl_graph_get_selected (); if (cfg == NULL) OUTPUT_ERROR ("gl_graph_get_selected () failed.\n"); @@ -214,31 +217,39 @@ int action_graph (void) /* {{{ */ if (inst == NULL) OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg); - data.args = array_create (); + data.args = ra_create (); if (data.args == NULL) return (ENOMEM); - array_append (data.args, "graph"); - array_append (data.args, "-"); - array_append (data.args, "--imgformat"); - array_append (data.args, "PNG"); + array_append (data.args->options, "graph"); + array_append (data.args->options, "-"); + array_append (data.args->options, "--imgformat"); + array_append (data.args->options, "PNG"); get_time_args (&data); status = inst_get_rrdargs (cfg, inst, data.args); if (status != 0) { - array_destroy (data.args); + ra_destroy (data.args); OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status); } + argc = ra_argc (data.args); + argv = ra_argv (data.args); + if ((argc < 0) || (argv == NULL)) + { + ra_destroy (data.args); + return (-1); + } + rrd_clear_error (); - data.info = rrd_graph_v (array_argc (data.args), array_argv (data.args)); + data.info = rrd_graph_v (argc, argv); if ((data.info == NULL) || rrd_test_error ()) { printf ("Content-Type: text/plain\n\n"); printf ("rrd_graph_v failed: %s\n", rrd_get_error ()); - emulate_graph (array_argc (data.args), array_argv (data.args)); + emulate_graph (argc, argv); } else { @@ -264,7 +275,8 @@ int action_graph (void) /* {{{ */ if (data.info != NULL) rrd_info_free (data.info); - array_destroy (data.args); + ra_argv_free (argv); + ra_destroy (data.args); data.args = NULL; return (0); diff --git a/src/graph.c b/src/graph.c index 01f0791..8d12a25 100644 --- a/src/graph.c +++ b/src/graph.c @@ -470,27 +470,27 @@ int graph_clear_instances (graph_config_t *cfg) /* {{{ */ } /* }}} int graph_clear_instances */ int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */ - str_array_t *args) + rrd_args_t *args) { if ((cfg == NULL) || (inst == NULL) || (args == NULL)) return (EINVAL); if (cfg->title != NULL) { - array_append (args, "-t"); - array_append (args, cfg->title); + array_append (args->options, "-t"); + array_append (args->options, cfg->title); } if (cfg->vertical_label != NULL) { - array_append (args, "-v"); - array_append (args, cfg->vertical_label); + array_append (args->options, "-v"); + array_append (args->options, cfg->vertical_label); } if (cfg->show_zero) { - array_append (args, "-l"); - array_append (args, "0"); + array_append (args->options, "-l"); + array_append (args->options, "0"); } return (0); diff --git a/src/graph.h b/src/graph.h index 0cb45d7..ddfbda6 100644 --- a/src/graph.h +++ b/src/graph.h @@ -4,6 +4,7 @@ #include "graph_types.h" #include "graph_ident.h" #include "oconfig.h" +#include "rrd_args.h" #include "utils_array.h" /* @@ -64,7 +65,7 @@ int graph_compare (graph_config_t *cfg, const graph_ident_t *ident); int graph_clear_instances (graph_config_t *cfg); int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, - str_array_t *args); + rrd_args_t *args); #endif /* GRAPH_H */ /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/graph_def.c b/src/graph_def.c index cecc8f0..197c6b5 100644 --- a/src/graph_def.c +++ b/src/graph_def.c @@ -287,7 +287,7 @@ int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */ } /* }}} int def_foreach */ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */ - str_array_t *args) + rrd_args_t *args) { char *file; int index; @@ -304,38 +304,39 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */ DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file); - index = array_argc (args); + index = args->index; + args->index++; /* CDEFs */ - array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN", + array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN", index, file, def->ds_name); - array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE", + array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE", index, file, def->ds_name); - array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX", + array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX", index, file, def->ds_name); /* VDEFs */ - array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM", + array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM", index, index); - array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE", + array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE", index, index); - array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM", + array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM", index, index); - array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST", + array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST", index, index); /* Graph part */ - array_append_format (args, "%s:def_%04i_avg#%06"PRIx32":%s%s", + array_append_format (args->draw, "%s:def_%04i_avg#%06"PRIx32":%s%s", def->area ? "AREA" : "LINE1", index, def->color, (def->legend != NULL) ? def->legend : def->ds_name, def->stack ? ":STACK" : ""); - array_append_format (args, "GPRINT:vdef_%04i_min:%s min,", + array_append_format (args->draw, "GPRINT:vdef_%04i_min:%s min,", index, (def->format != NULL) ? def->format : "%6.2lf"); - array_append_format (args, "GPRINT:vdef_%04i_avg:%s avg,", + array_append_format (args->draw, "GPRINT:vdef_%04i_avg:%s avg,", index, (def->format != NULL) ? def->format : "%6.2lf"); - array_append_format (args, "GPRINT:vdef_%04i_max:%s max,", + array_append_format (args->draw, "GPRINT:vdef_%04i_max:%s max,", index, (def->format != NULL) ? def->format : "%6.2lf"); - array_append_format (args, "GPRINT:vdef_%04i_lst:%s last\\l", + array_append_format (args->draw, "GPRINT:vdef_%04i_lst:%s last\\l", index, (def->format != NULL) ? def->format : "%6.2lf"); free (file); diff --git a/src/graph_def.h b/src/graph_def.h index a4bb7fd..456602b 100644 --- a/src/graph_def.h +++ b/src/graph_def.h @@ -4,6 +4,7 @@ #include "graph_types.h" #include "utils_array.h" #include "oconfig.h" +#include "rrd_args.h" graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, const char *ds_name); @@ -22,7 +23,7 @@ _Bool def_matches (graph_def_t *def, graph_ident_t *ident); int def_foreach (graph_def_t *def, def_callback_t callback, void *user_data); int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, - str_array_t *args); + rrd_args_t *args); /* vim: set sw=2 sts=2 et fdm=marker : */ #endif diff --git a/src/graph_instance.c b/src/graph_instance.c index 5431589..317c381 100644 --- a/src/graph_instance.c +++ b/src/graph_instance.c @@ -26,7 +26,7 @@ struct graph_instance_s /* {{{ */ struct def_callback_data_s { graph_instance_t *inst; - str_array_t *args; + rrd_args_t *args; }; typedef struct def_callback_data_s def_callback_data_t; @@ -124,7 +124,7 @@ static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ { def_callback_data_t *data = user_data; graph_instance_t *inst = data->inst; - str_array_t *args = data->args; + rrd_args_t *args = data->args; size_t i; @@ -311,7 +311,7 @@ int inst_get_all_selected (graph_config_t *cfg, /* {{{ */ int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */ graph_instance_t *inst, - str_array_t *args) + rrd_args_t *args) { def_callback_data_t data = { inst, args }; graph_def_t *defs; diff --git a/src/graph_instance.h b/src/graph_instance.h index fa08dcd..61d5ea4 100644 --- a/src/graph_instance.h +++ b/src/graph_instance.h @@ -5,6 +5,7 @@ #include "graph_types.h" #include "graph_ident.h" +#include "rrd_args.h" #include "utils_array.h" /* @@ -26,7 +27,7 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, char *buffer, size_t buffer_size); int inst_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, - str_array_t *args); + rrd_args_t *args); graph_ident_t *inst_get_selector (graph_instance_t *inst); diff --git a/src/rrd_args.c b/src/rrd_args.c index f5358c8..f913733 100644 --- a/src/rrd_args.c +++ b/src/rrd_args.c @@ -75,6 +75,7 @@ char **ra_argv (rrd_args_t *ra) /* {{{ */ return (NULL); pos = 0; + argv[0] = NULL; #define APPEND_FIELD(field) do \ { \ @@ -89,7 +90,6 @@ char **ra_argv (rrd_args_t *ra) /* {{{ */ pos += ary_argc; \ argv[pos] = NULL; \ } \ - free (ary_argv); \ } while (0) APPEND_FIELD (options); @@ -102,4 +102,12 @@ char **ra_argv (rrd_args_t *ra) /* {{{ */ return (argv); } /* }}} char **ra_argv */ +void ra_argv_free (char **argv) /* {{{ */ +{ + /* The pointers contained in the "argv" come from "array_argv". We don't need + * to free them. We only need to free what we actually alloced directly in + * "ra_argv". */ + free (argv); +} /* }}} void ra_argv_free */ + /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/rrd_args.h b/src/rrd_args.h index 4bfeea7..b3ef34a 100644 --- a/src/rrd_args.h +++ b/src/rrd_args.h @@ -10,6 +10,7 @@ struct rrd_args_s str_array_t *calc; str_array_t *draw; + int index; char last_stack_cdef[64]; }; typedef struct rrd_args_s rrd_args_t; @@ -19,6 +20,7 @@ void ra_destroy (rrd_args_t *ra); int ra_argc (rrd_args_t *ra); char **ra_argv (rrd_args_t *ra); +void ra_argv_free (char **argv); #endif /* RRD_ARGS_H */ /* vim: set sw=2 sts=2 et fdm=marker : */ -- 2.11.0