src/graph_instance.c: Properly escape parameters.
[collection4.git] / src / graph_instance.c
index e32dcab..86cec84 100644 (file)
@@ -1,3 +1,26 @@
+/**
+ * collection4 - graph_instance.c
+ * Copyright (C) 2010  Florian octo Forster
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * 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
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Florian octo Forster <ff at octo.it>
+ **/
+
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
@@ -26,7 +49,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 +147,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;
 
@@ -151,6 +174,36 @@ static const char *get_part_from_param (const char *prim_key, /* {{{ */
   return (param (sec_key));
 } /* }}} const char *get_part_from_param */
 
+static graph_ident_t *inst_get_selector_from_params (void) /* {{{ */
+{
+  const char *host = get_part_from_param ("inst_host", "host");
+  const char *plugin = get_part_from_param ("inst_plugin", "plugin");
+  const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
+      "plugin_instance");
+  const char *type = get_part_from_param ("inst_type", "type");
+  const char *type_instance = get_part_from_param ("inst_type_instance",
+      "type_instance");
+
+  graph_ident_t *ident;
+
+  if ((host == NULL)
+      || (plugin == NULL) || (plugin_instance == NULL)
+      || (type == NULL) || (type_instance == NULL))
+  {
+    fprintf (stderr, "inst_get_selected: A parameter is NULL\n");
+    return (NULL);
+  }
+
+  ident = ident_create (host, plugin, plugin_instance, type, type_instance);
+  if (ident == NULL)
+  {
+    fprintf (stderr, "inst_get_selected: ident_create failed\n");
+    return (NULL);
+  }
+
+  return (ident);
+} /* }}} graph_ident_t *inst_get_selector_from_params */
+
 /*
  * Public functions
  */
@@ -231,14 +284,6 @@ int inst_add_file (graph_instance_t *inst, /* {{{ */
 
 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
 {
-  const char *host = get_part_from_param ("inst_host", "host");
-  const char *plugin = get_part_from_param ("inst_plugin", "plugin");
-  const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
-      "plugin_instance");
-  const char *type = get_part_from_param ("inst_type", "type");
-  const char *type_instance = get_part_from_param ("inst_type_instance",
-      "type_instance");
-
   graph_ident_t *ident;
   graph_instance_t *inst;
 
@@ -251,15 +296,7 @@ graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
     return (NULL);
   }
 
-  if ((host == NULL)
-      || (plugin == NULL) || (plugin_instance == NULL)
-      || (type == NULL) || (type_instance == NULL))
-  {
-    DEBUG ("inst_get_selected: A parameter is NULL.\n");
-    return (NULL);
-  }
-
-  ident = ident_create (host, plugin, plugin_instance, type, type_instance);
+  ident = inst_get_selector_from_params ();
   if (ident == NULL)
   {
     fprintf (stderr, "inst_get_selected: ident_create failed\n");
@@ -267,14 +304,37 @@ graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
   }
 
   inst = graph_inst_find_exact (cfg, ident);
-  ident_destroy (ident);
 
+  ident_destroy (ident);
   return (inst);
 } /* }}} graph_instance_t *inst_get_selected */
 
+int inst_get_all_selected (graph_config_t *cfg, /* {{{ */
+    graph_inst_callback_t callback, void *user_data)
+{
+  graph_ident_t *ident;
+  int status;
+
+  if ((cfg == NULL) || (callback == NULL))
+    return (EINVAL);
+
+  ident = inst_get_selector_from_params ();
+  if (ident == NULL)
+  {
+    fprintf (stderr, "inst_get_all_selected: "
+        "inst_get_selector_from_params failed\n");
+    return (EINVAL);
+  }
+
+  status = graph_inst_find_all_matching (cfg, ident, callback, user_data);
+
+  ident_destroy (ident);
+  return (status);
+} /* }}} int inst_get_all_selected */
+
 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;
@@ -333,6 +393,12 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
 
   buffer[0] = 0;
 
+#define COPY_ESCAPE(str) do {                                   \
+  char tmp[1024];                                               \
+  uri_escape_copy (tmp, (str), sizeof (tmp));                   \
+  strlcat (buffer, tmp, buffer_size);                           \
+} while (0)                                                     \
+
 #define COPY_FIELD(field) do {                                  \
   const char *cfg_f  = ident_get_##field (cfg_select);          \
   const char *inst_f = ident_get_##field (inst->select);        \
@@ -340,19 +406,19 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
   {                                                             \
     strlcat (buffer, #field, buffer_size);                      \
     strlcat (buffer, "=", buffer_size);                         \
-    strlcat (buffer, cfg_f, buffer_size);                       \
+    COPY_ESCAPE (cfg_f);                                        \
   }                                                             \
   else                                                          \
   {                                                             \
     strlcat (buffer, "graph_", buffer_size);                    \
     strlcat (buffer, #field, buffer_size);                      \
     strlcat (buffer, "=", buffer_size);                         \
-    strlcat (buffer, cfg_f, buffer_size);                       \
+    COPY_ESCAPE (cfg_f);                                        \
     strlcat (buffer, ";", buffer_size);                         \
     strlcat (buffer, "inst_", buffer_size);                     \
     strlcat (buffer, #field, buffer_size);                      \
     strlcat (buffer, "=", buffer_size);                         \
-    strlcat (buffer, inst_f, buffer_size);                      \
+    COPY_ESCAPE (inst_f);                                       \
   }                                                             \
 } while (0)
 
@@ -367,12 +433,19 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
   COPY_FIELD(type_instance);
 
 #undef COPY_FIELD
+#undef COPY_ESCAPE
 
   ident_destroy (cfg_select);
 
   return (0);
 } /* }}} int inst_get_params */
 
+int inst_compare (const graph_instance_t *i0, /* {{{ */
+    const graph_instance_t *i1)
+{
+  return (ident_compare (i0->select, i1->select));
+} /* }}} int inst_compare */
+
 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
     const graph_ident_t *ident)
 {
@@ -382,13 +455,24 @@ int inst_compare_ident (graph_instance_t *inst, /* {{{ */
   return (ident_compare (inst->select, ident));
 } /* }}} int inst_compare_ident */
 
-_Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
+_Bool inst_ident_matches (graph_instance_t *inst, /* {{{ */
     const graph_ident_t *ident)
 {
+#if C4_DEBUG
   if ((inst == NULL) || (ident == NULL))
     return (0);
+#endif
 
   return (ident_matches (inst->select, ident));
+} /* }}} _Bool inst_ident_matches */
+
+_Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
+    const graph_ident_t *ident)
+{
+  if ((inst == NULL) || (ident == NULL))
+    return (0);
+
+  return (ident_matches (ident, inst->select));
 } /* }}} _Bool inst_matches_ident */
 
 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
@@ -457,10 +541,37 @@ _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
   return (0);
 } /* }}} _Bool inst_matches_field */
 
+int inst_to_json (const graph_instance_t *inst, /* {{{ */
+    yajl_gen handler)
+{
+  size_t i;
+
+  if ((inst == NULL) || (handler == NULL))
+    return (EINVAL);
+
+  /* TODO: error handling */
+  yajl_gen_map_open (handler);
+  yajl_gen_string (handler,
+      (unsigned char *) "select",
+      (unsigned int) strlen ("select"));
+  ident_to_json (inst->select, handler);
+  yajl_gen_string (handler,
+      (unsigned char *) "files",
+      (unsigned int) strlen ("files"));
+  yajl_gen_array_open (handler);
+  for (i = 0; i < inst->files_num; i++)
+    ident_to_json (inst->files[i], handler);
+  yajl_gen_array_close (handler);
+  yajl_gen_map_close (handler);
+
+  return (0);
+} /* }}} int inst_to_json */
+
 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
     char *buffer, size_t buffer_size)
 {
   graph_ident_t *cfg_select;
+  int status;
 
   if ((cfg == NULL) || (inst == NULL)
       || (buffer == NULL) || (buffer_size < 2))
@@ -473,31 +584,12 @@ int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
     return (-1);
   }
 
-  buffer[0] = 0;
-
-#define CHECK_FIELD(field) do {                                              \
-  if (IS_ANY (ident_get_##field (cfg_select)))                               \
-  {                                                                          \
-    if (buffer[0] != 0)                                                      \
-      strlcat (buffer, "/", buffer_size);                                    \
-    strlcat (buffer, ident_get_##field (inst->select), buffer_size);         \
-  }                                                                          \
-} while (0)
-
-  CHECK_FIELD (host);
-  CHECK_FIELD (plugin);
-  CHECK_FIELD (plugin_instance);
-  CHECK_FIELD (type);
-  CHECK_FIELD (type_instance);
-
-#undef CHECK_FIELD
-
-  if (buffer[0] == 0)
-    strlcat (buffer, "default", buffer_size);
+  status = ident_describe (inst->select, cfg_select,
+      buffer, buffer_size);
 
   ident_destroy (cfg_select);
 
-  return (0);
+  return (status);
 } /* }}} int inst_describe */
 
 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */