From de9239ac3e16e12503eae5ddbfcdaae27f262081 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 15 Jun 2010 19:26:36 +0200 Subject: [PATCH] First attempt at a search infrastructure. --- action_list_graphs.c | 52 ++++++++++++++++++++++++++++---------------- graph.c | 51 +++++++++++++++++++++++++++++++++++++++++++ graph.h | 13 ++++------- graph_def.c | 5 ++++- graph_def.h | 9 +------- graph_ident.h | 7 +++--- graph_instance.c | 33 ++++++++++++++++++++++++++++ graph_instance.h | 11 +++++----- graph_list.c | 27 +++++++++++++++++++---- graph_list.h | 24 ++++++++------------ graph_types.h | 35 +++++++++++++++++++++++++++++ 11 files changed, 201 insertions(+), 66 deletions(-) create mode 100644 graph_types.h diff --git a/action_list_graphs.c b/action_list_graphs.c index 29e3d8b..08d0eb4 100644 --- a/action_list_graphs.c +++ b/action_list_graphs.c @@ -5,6 +5,7 @@ #include "action_list_graphs.h" #include "graph.h" +#include "graph_ident.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 */ +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, - __attribute__((unused)) void *user_data) + void *user_data) { + callback_data_t *data = user_data; char params[1024]; char desc[1024]; + if (data->cfg != cfg) + { + if (data->cfg != NULL) + printf (" \n"); + + memset (desc, 0, sizeof (desc)); + graph_get_title (cfg, desc, sizeof (desc)); + + printf ("
  • %s\n \n"); return (0); @@ -120,7 +134,7 @@ int action_list_graphs (void) /* {{{ */ 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 : */ diff --git a/graph.c b/graph.c index 1a8f327..2809ebb 100644 --- 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 */ +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)) diff --git a/graph.h b/graph.h index 6e9ae07..57a866f 100644 --- a/graph.h +++ b/graph.h @@ -1,15 +1,7 @@ #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" @@ -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); +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); diff --git a/graph_def.c b/graph_def.c index 4ea0966..a9ae806 100644 --- a/graph_def.c +++ b/graph_def.c @@ -5,6 +5,7 @@ #include "graph_def.h" #include "graph.h" #include "graph_config.h" +#include "graph_ident.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); +#if 0 else - fprintf (stderr, "def_config: Ignoring unknown config option \"%s\"", + fprintf (stderr, "def_config: Ignoring unknown config option \"%s\"\n", child->key); +#endif } return (graph_add_def (cfg, def)); diff --git a/graph_def.h b/graph_def.h index b2cd228..a4bb7fd 100644 --- a/graph_def.h +++ b/graph_def.h @@ -1,14 +1,7 @@ #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" diff --git a/graph_ident.h b/graph_ident.h index 5107011..03a3c72 100644 --- a/graph_ident.h +++ b/graph_ident.h @@ -1,15 +1,14 @@ #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)) -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); @@ -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 *ident); + const graph_ident_t *ident); char *ident_to_string (const graph_ident_t *ident); char *ident_to_file (const graph_ident_t *ident); diff --git a/graph_instance.c b/graph_instance.c index 103be23..e1e0a36 100644 --- a/graph_instance.c +++ b/graph_instance.c @@ -3,6 +3,7 @@ #include #include "graph_instance.h" +#include "graph_def.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 */ +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) { diff --git a/graph_instance.h b/graph_instance.h index b90f984..f02901b 100644 --- a/graph_instance.h +++ b/graph_instance.h @@ -4,12 +4,7 @@ /* * 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" /* @@ -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_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); diff --git a/graph_list.c b/graph_list.c index 965fcba..3c8d375 100644 --- a/graph_list.c +++ b/graph_list.c @@ -153,7 +153,7 @@ int gl_config_submit (void) /* {{{ */ 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; @@ -211,7 +211,7 @@ graph_config_t *gl_graph_get_selected (void) /* {{{ */ 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 */ @@ -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, /* {{{ */ - gl_inst_callback callback, void *user_data) + graph_inst_callback_t callback, void *user_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 */ -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; @@ -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_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; diff --git a/graph_list.h b/graph_list.h index c17d6fc..e6b6888 100644 --- a/graph_list.h +++ b/graph_list.h @@ -1,17 +1,10 @@ #ifndef GRAPH_LIST_H #define GRAPH_LIST_H 1 +#include "graph_types.h" +#include "graph.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 */ @@ -19,15 +12,16 @@ 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); -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); diff --git a/graph_types.h b/graph_types.h new file mode 100644 index 0000000..623e671 --- /dev/null +++ b/graph_types.h @@ -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 : */ -- 2.30.2