First attempt at a search infrastructure.
authorFlorian Forster <ff@octo.it>
Tue, 15 Jun 2010 17:26:36 +0000 (19:26 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 15 Jun 2010 17:26:36 +0000 (19:26 +0200)
action_list_graphs.c
graph.c
graph.h
graph_def.c
graph_def.h
graph_ident.h
graph_instance.c
graph_instance.h
graph_list.c
graph_list.h
graph_types.h [new file with mode: 0644]

index 29e3d8b..08d0eb4 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "action_list_graphs.h"
 #include "graph.h"
 
 #include "action_list_graphs.h"
 #include "graph.h"
+#include "graph_ident.h"
 #include "graph_list.h"
 #include "utils_params.h"
 
 #include "graph_list.h"
 #include "utils_params.h"
 
@@ -62,13 +63,33 @@ static int list_graphs_json (void) /* {{{ */
   return (0);
 } /* }}} int list_graphs_json */
 
   return (0);
 } /* }}} int list_graphs_json */
 
+struct callback_data_s
+{
+  graph_config_t *cfg;
+};
+typedef struct callback_data_s callback_data_t;
+
 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
     graph_instance_t *inst,
 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
     graph_instance_t *inst,
-    __attribute__((unused)) void *user_data)
+    void *user_data)
 {
 {
+  callback_data_t *data = user_data;
   char params[1024];
   char desc[1024];
 
   char params[1024];
   char desc[1024];
 
+  if (data->cfg != cfg)
+  {
+    if (data->cfg != NULL)
+      printf ("  </ul></li>\n");
+
+    memset (desc, 0, sizeof (desc));
+    graph_get_title (cfg, desc, sizeof (desc));
+
+    printf ("  <li>%s\n  <ul>\n", desc);
+
+    data->cfg = cfg;
+  }
+
   memset (params, 0, sizeof (params));
   inst_get_params (cfg, inst, params, sizeof (params));
 
   memset (params, 0, sizeof (params));
   inst_get_params (cfg, inst, params, sizeof (params));
 
@@ -81,27 +102,20 @@ static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
   return (0);
 } /* }}} int print_graph_inst_html */
 
   return (0);
 } /* }}} int print_graph_inst_html */
 
-static int print_graph_html (graph_config_t *cfg, /* {{{ */
-    __attribute__((unused)) void *user_data)
-{
-  char buffer[1024];
-
-  memset (buffer, 0, sizeof (buffer));
-  graph_get_title (cfg, buffer, sizeof (buffer));
-
-  printf ("  <li>%s\n  <ul>\n", buffer);
-  gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
-  printf ("  </ul></li>\n");
-
-  return (0);
-} /* }}} int print_graph_html */
-
-static int list_graphs_html (void) /* {{{ */
+static int list_graphs_html (const char *term) /* {{{ */
 {
 {
+  callback_data_t data = { NULL };
   printf ("Content-Type: text/html\n\n");
 
   printf ("<ul>\n");
   printf ("Content-Type: text/html\n\n");
 
   printf ("<ul>\n");
-  gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
+  if (term == NULL)
+    gl_instance_get_all (print_graph_inst_html, /* user_data = */ &data);
+  else
+    gl_search (term, print_graph_inst_html, /* user_data = */ &data);
+
+  if (data.cfg != NULL)
+    printf ("  </ul></li>\n");
+
   printf ("</ul>\n");
 
   return (0);
   printf ("</ul>\n");
 
   return (0);
@@ -120,7 +134,7 @@ int action_list_graphs (void) /* {{{ */
   if (strcmp ("json", format) == 0)
     return (list_graphs_json ());
   else
   if (strcmp ("json", format) == 0)
     return (list_graphs_json ());
   else
-    return (list_graphs_html ());
+    return (list_graphs_html (param ("search")));
 } /* }}} int action_list_graphs */
 
 /* vim: set sw=2 sts=2 et fdm=marker : */
 } /* }}} int action_list_graphs */
 
 /* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/graph.c b/graph.c
index 1a8f327..2809ebb 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -250,6 +250,57 @@ _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
   return (ident_matches (cfg->select, ident));
 } /* }}} _Bool graph_matches */
 
   return (ident_matches (cfg->select, ident));
 } /* }}} _Bool graph_matches */
 
+struct graph_search_data_s
+{
+  graph_config_t *cfg;
+  graph_inst_callback_t callback;
+  void *user_data;
+};
+typedef struct graph_search_data_s graph_search_data_t;
+
+static int graph_search_submit (graph_instance_t *inst, /* {{{ */
+    void *user_data)
+{
+  graph_search_data_t *data = user_data;
+
+  if ((inst == NULL) || (data == NULL))
+    return (EINVAL);
+
+  return ((*data->callback) (data->cfg, inst, data->user_data));
+} /* }}} int graph_search_submit */
+
+int graph_search (graph_config_t *cfg, const char *term, /* {{{ */
+    graph_inst_callback_t callback,
+    void *user_data)
+{
+  graph_search_data_t data = { cfg, callback, user_data };
+  char buffer[1024];
+  int status;
+
+  status = graph_get_title (cfg, buffer, sizeof (buffer));
+  if (status != 0)
+  {
+    fprintf (stderr, "graph_search: graph_get_title failed\n");
+    return (status);
+  }
+
+  if (strstr (buffer, term) != NULL)
+  {
+    status = inst_foreach (cfg->instances, graph_search_submit, &data);
+    if (status != 0)
+      return (status);
+  }
+  else
+  {
+    status = inst_search (cfg, cfg->instances, term,
+        graph_search_submit, &data);
+    if (status != 0)
+      return (status);
+  }
+
+  return (0);
+} /* }}} int graph_search */
+
 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
 {
   if ((cfg == NULL) || (ident == NULL))
 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
 {
   if ((cfg == NULL) || (ident == NULL))
diff --git a/graph.h b/graph.h
index 6e9ae07..57a866f 100644 (file)
--- a/graph.h
+++ b/graph.h
@@ -1,15 +1,7 @@
 #ifndef GRAPH_H
 #define GRAPH_H 1
 
 #ifndef GRAPH_H
 #define GRAPH_H 1
 
-/*
- * Data types
- */
-struct graph_config_s;
-typedef struct graph_config_s graph_config_t;
-
-#include "graph_def.h"
-#include "graph_ident.h"
-#include "graph_instance.h"
+#include "graph_types.h"
 #include "oconfig.h"
 #include "utils_array.h"
 
 #include "oconfig.h"
 #include "utils_array.h"
 
@@ -37,6 +29,9 @@ int graph_add_def (graph_config_t *cfg, graph_def_t *def);
 
 _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident);
 
 
 _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident);
 
+int graph_search (graph_config_t *cfg, const char *term,
+    graph_inst_callback_t callback, void *user_data);
+
 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident);
 
 int graph_clear_instances (graph_config_t *cfg);
 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident);
 
 int graph_clear_instances (graph_config_t *cfg);
index 4ea0966..a9ae806 100644 (file)
@@ -5,6 +5,7 @@
 #include "graph_def.h"
 #include "graph.h"
 #include "graph_config.h"
 #include "graph_def.h"
 #include "graph.h"
 #include "graph_config.h"
+#include "graph_ident.h"
 #include "common.h"
 #include "oconfig.h"
 
 #include "common.h"
 #include "oconfig.h"
 
@@ -219,9 +220,11 @@ int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
       graph_config_get_bool (child, &def->area);
     else if (strcasecmp ("Format", child->key) == 0)
       graph_config_get_string (child, &def->format);
       graph_config_get_bool (child, &def->area);
     else if (strcasecmp ("Format", child->key) == 0)
       graph_config_get_string (child, &def->format);
+#if 0
     else
     else
-      fprintf (stderr, "def_config: Ignoring unknown config option \"%s\"",
+      fprintf (stderr, "def_config: Ignoring unknown config option \"%s\"\n",
           child->key);
           child->key);
+#endif
   }
 
   return (graph_add_def (cfg, def));
   }
 
   return (graph_add_def (cfg, def));
index b2cd228..a4bb7fd 100644 (file)
@@ -1,14 +1,7 @@
 #ifndef GRAPH_DEF_H
 #define GRAPH_DEF_H 1
 
 #ifndef GRAPH_DEF_H
 #define GRAPH_DEF_H 1
 
-struct graph_def_s;
-typedef struct graph_def_s graph_def_t;
-
-typedef int (*def_callback_t) (graph_def_t *def,
-    void *user_data);
-
-#include "graph.h"
-#include "graph_ident.h"
+#include "graph_types.h"
 #include "utils_array.h"
 #include "oconfig.h"
 
 #include "utils_array.h"
 #include "oconfig.h"
 
index 5107011..03a3c72 100644 (file)
@@ -1,15 +1,14 @@
 #ifndef GRAPH_IDENT_H
 #define GRAPH_IDENT_H 1
 
 #ifndef GRAPH_IDENT_H
 #define GRAPH_IDENT_H 1
 
+#include "graph_types.h"
+
 #define ANY_TOKEN "/any/"
 #define ALL_TOKEN "/all/"
 
 #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
 #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
 
 #define ANY_TOKEN "/any/"
 #define ALL_TOKEN "/all/"
 
 #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
 #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
 
-struct graph_ident_s;
-typedef struct graph_ident_s graph_ident_t;
-
 graph_ident_t *ident_create (const char *host,
     const char *plugin, const char *plugin_instance,
     const char *type, const char *type_instance);
 graph_ident_t *ident_create (const char *host,
     const char *plugin, const char *plugin_instance,
     const char *type, const char *type_instance);
@@ -40,7 +39,7 @@ int ident_compare (const graph_ident_t *i0,
     const graph_ident_t *i1);
 
 _Bool ident_matches (const graph_ident_t *selector,
     const graph_ident_t *i1);
 
 _Bool ident_matches (const graph_ident_t *selector,
-               const graph_ident_t *ident);
+    const graph_ident_t *ident);
 
 char *ident_to_string (const graph_ident_t *ident);
 char *ident_to_file (const graph_ident_t *ident);
 
 char *ident_to_string (const graph_ident_t *ident);
 char *ident_to_file (const graph_ident_t *ident);
index 103be23..e1e0a36 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 
 #include "graph_instance.h"
 #include <errno.h>
 
 #include "graph_instance.h"
+#include "graph_def.h"
 #include "graph_ident.h"
 #include "graph_list.h"
 #include "common.h"
 #include "graph_ident.h"
 #include "graph_list.h"
 #include "common.h"
@@ -414,6 +415,38 @@ int inst_foreach (graph_instance_t *inst, /* {{{ */
   return (0);
 } /* }}} int inst_foreach */
 
   return (0);
 } /* }}} int inst_foreach */
 
+int inst_search (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
+    const char *term, inst_callback_t cb, void *user_data)
+{
+  graph_instance_t *ptr;
+  char buffer[1024];
+  int status;
+
+  if ((inst == NULL) || (cb == NULL))
+    return (EINVAL);
+
+  for (ptr = inst; ptr != NULL; ptr = ptr->next)
+  {
+    status = inst_describe (cfg, ptr, buffer, sizeof (buffer));
+    if (status != 0)
+    {
+      fprintf (stderr, "inst_search: inst_describe failed\n");
+      return (status);
+    }
+
+    /* no match */
+    if (strstr (buffer, term) == NULL)
+      continue;
+
+    /* match */
+    status = (*cb) (ptr, user_data);
+    if (status != 0)
+      return (status);
+  }
+
+  return (0);
+} /* }}} int inst_search */
+
 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
     const graph_ident_t *ident)
 {
 graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
     const graph_ident_t *ident)
 {
index b90f984..f02901b 100644 (file)
@@ -4,12 +4,7 @@
 /*
  * Data types
  */
 /*
  * Data types
  */
-struct graph_instance_s;
-typedef struct graph_instance_s graph_instance_t;
-
-typedef int (*inst_callback_t) (graph_instance_t *inst, void *user_data);
-
-#include "graph.h"
+#include "graph_types.h"
 #include "utils_array.h"
 
 /*
 #include "utils_array.h"
 
 /*
@@ -40,6 +35,10 @@ int inst_append (graph_instance_t *head, graph_instance_t *inst);
 int inst_foreach (graph_instance_t *inst,
                inst_callback_t cb, void *user_data);
 
 int inst_foreach (graph_instance_t *inst,
                inst_callback_t cb, void *user_data);
 
+int inst_search (graph_config_t *cfg, graph_instance_t *inst,
+    const char *term, inst_callback_t cb,
+    void *user_data);
+
 graph_instance_t *inst_find_matching (graph_instance_t *inst,
     const graph_ident_t *ident);
 
 graph_instance_t *inst_find_matching (graph_instance_t *inst,
     const graph_ident_t *ident);
 
index 965fcba..3c8d375 100644 (file)
@@ -153,7 +153,7 @@ int gl_config_submit (void) /* {{{ */
   return (0);
 } /* }}} int graph_config_submit */
 
   return (0);
 } /* }}} int graph_config_submit */
 
-int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */
+int gl_graph_get_all (graph_callback_t callback, /* {{{ */
     void *user_data)
 {
   size_t i;
     void *user_data)
 {
   size_t i;
@@ -211,7 +211,7 @@ graph_config_t *gl_graph_get_selected (void) /* {{{ */
 struct gl_inst_callback_data /* {{{ */
 {
   graph_config_t *cfg;
 struct gl_inst_callback_data /* {{{ */
 {
   graph_config_t *cfg;
-  gl_inst_callback callback;
+  graph_inst_callback_t callback;
   void *user_data;
 }; /* }}} struct gl_inst_callback_data */
 
   void *user_data;
 }; /* }}} struct gl_inst_callback_data */
 
@@ -224,7 +224,7 @@ static int gl_inst_callback_handler (graph_instance_t *inst, /* {{{ */
 } /* }}} int gl_inst_callback_handler */
 
 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
 } /* }}} int gl_inst_callback_handler */
 
 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
-    gl_inst_callback callback, void *user_data)
+    graph_inst_callback_t callback, void *user_data)
 {
   struct gl_inst_callback_data data =
   {
 {
   struct gl_inst_callback_data data =
   {
@@ -240,7 +240,7 @@ int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
         gl_inst_callback_handler, &data));
 } /* }}} int gl_graph_instance_get_all */
 
         gl_inst_callback_handler, &data));
 } /* }}} int gl_graph_instance_get_all */
 
-int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
+int gl_instance_get_all (graph_inst_callback_t callback, /* {{{ */
     void *user_data)
 {
   size_t i;
     void *user_data)
 {
   size_t i;
@@ -260,6 +260,25 @@ int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
 } /* }}} int gl_instance_get_all */
 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
 
 } /* }}} int gl_instance_get_all */
 /* }}} gl_instance_get_all, gl_graph_instance_get_all */
 
+int gl_search (const char *term, graph_inst_callback_t callback, /* {{{ */
+    void *user_data)
+{
+  size_t i;
+
+  for (i = 0; i < gl_active_num; i++)
+  {
+    int status;
+
+    status = graph_search (gl_active[i], term,
+        /* callback  = */ callback,
+        /* user data = */ user_data);
+    if (status != 0)
+      return (status);
+  }
+
+  return (0);
+} /* }}} int gl_search */
+
 int gl_update (void) /* {{{ */
 {
   time_t now;
 int gl_update (void) /* {{{ */
 {
   time_t now;
index c17d6fc..e6b6888 100644 (file)
@@ -1,33 +1,27 @@
 #ifndef GRAPH_LIST_H
 #define GRAPH_LIST_H 1
 
 #ifndef GRAPH_LIST_H
 #define GRAPH_LIST_H 1
 
+#include "graph_types.h"
+#include "graph.h"
 #include "graph_instance.h"
 
 /*
 #include "graph_instance.h"
 
 /*
- * Callback types
- */
-typedef int (*gl_cfg_callback) (graph_config_t *cfg,
-    void *user_data);
-
-typedef int (*gl_inst_callback) (graph_config_t *cfg,
-    graph_instance_t *inst, void *user_data);
-
-/*
  * Functions
  */
 int gl_add_graph (graph_config_t *cfg);
 
 int gl_config_submit (void);
 
  * Functions
  */
 int gl_add_graph (graph_config_t *cfg);
 
 int gl_config_submit (void);
 
-int gl_graph_get_all (gl_cfg_callback callback,
-    void *user_data);
-
 graph_config_t *gl_graph_get_selected (void);
 
 graph_config_t *gl_graph_get_selected (void);
 
-int gl_graph_instance_get_all (graph_config_t *cfg,
-    gl_inst_callback callback, void *user_data);
+int gl_graph_get_all (graph_callback_t callback, void *user_data);
+
+int gl_graph_instance_get_all (graph_config_t *cfg, graph_inst_callback_t callback,
+    void *user_data);
+
+int gl_instance_get_all (graph_inst_callback_t callback, void *user_data);
 
 
-int gl_instance_get_all (gl_inst_callback callback,
+int gl_search (const char *search, graph_inst_callback_t callback,
     void *user_data);
 
 int gl_update (void);
     void *user_data);
 
 int gl_update (void);
diff --git a/graph_types.h b/graph_types.h
new file mode 100644 (file)
index 0000000..623e671
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef GRAPH_TYPES_H
+#define GRAPH_TYPES_H 1
+
+/*
+ * Opaque types
+ */
+struct graph_config_s;
+typedef struct graph_config_s graph_config_t;
+
+struct graph_def_s;
+typedef struct graph_def_s graph_def_t;
+
+struct graph_ident_s;
+typedef struct graph_ident_s graph_ident_t;
+
+struct graph_instance_s;
+typedef struct graph_instance_s graph_instance_t;
+
+/*
+ * Callback types
+ */
+typedef int (*graph_callback_t) (graph_config_t *cfg,
+    void *user_data);
+
+typedef int (*graph_inst_callback_t) (graph_config_t *cfg,
+    graph_instance_t *inst, void *user_data);
+
+typedef int (*def_callback_t) (graph_def_t *def,
+    void *user_data);
+
+typedef int (*inst_callback_t) (graph_instance_t *inst,
+               void *user_data);
+
+#endif /* GRAPH_TYPES_H */
+/* vim: set sw=2 sts=2 et fdm=marker : */