X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Faction_search.c;h=27b6a0efb2e700d18c5c83431bc3d18dbc9abc0c;hb=ddfdfa88eb2e3db84ab47d65bc49bcc565f4bdd7;hp=7782d778fe79de05db643f5dacf75abca9d18ad5;hpb=748436c71114d396ded0d2187355aced2275f6c4;p=collection4.git diff --git a/src/action_search.c b/src/action_search.c index 7782d77..27b6a0e 100644 --- a/src/action_search.c +++ b/src/action_search.c @@ -21,10 +21,13 @@ * Florian octo Forster **/ +#include "config.h" + #include #include #include #include +#include #include "action_search.h" #include "common.h" @@ -33,6 +36,7 @@ #include "graph_instance.h" #include "graph_list.h" #include "utils_cgi.h" +#include "utils_search.h" #include #include @@ -52,6 +56,17 @@ struct callback_data_s }; typedef struct callback_data_s callback_data_t; +static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */ +{ + printf ("\n\n", + script_name (), script_name ()); + + return (0); +} /* }}} int left_menu */ + static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */ graph_instance_t *inst, void *user_data) @@ -129,33 +144,10 @@ static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */ return (0); } /* }}} int print_graph_inst_html */ -static int print_graph_html (graph_config_t *cfg, /* {{{ */ - __attribute__((unused)) void *user_data) -{ - char params[1024]; - char title[1024]; - - if (graph_num_instances (cfg) < 1) - return (0); - - memset (title, 0, sizeof (title)); - graph_get_title (cfg, title, sizeof (title)); - html_escape_buffer (title, sizeof (title)); - - memset (params, 0, sizeof (params)); - graph_get_params (cfg, params, sizeof (params)); - html_escape_buffer (params, sizeof (params)); - - printf ("
  • " - "%s
  • \n", - script_name (), params, title); - - return (0); -} /* }}} int print_graph_html */ - struct page_data_s { - const char *search_term; + char *search_term; + search_info_t *search_info; }; typedef struct page_data_s page_data_t; @@ -166,45 +158,20 @@ static int print_search_result (void *user_data) /* {{{ */ /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0, /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0, /* search_term = */ pg_data->search_term }; + char *search_term_html; - if (pg_data->search_term != NULL) - { - char *search_term_html = html_escape (pg_data->search_term); - printf ("

    Search results for "%s"

    \n", - search_term_html); - free (search_term_html); - } + assert (pg_data->search_term != NULL); + + search_term_html = html_escape (pg_data->search_term); + printf ("

    Search results for "%s"

    \n", + search_term_html); + free (search_term_html); printf ("
      \n"); - if (pg_data->search_term == NULL) - { - gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data); - } - else - { - char *term_lc = strtolower_copy (pg_data->search_term); - - if (strncmp ("host:", term_lc, strlen ("host:")) == 0) - gl_search_field (GIF_HOST, term_lc + strlen ("host:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0) - gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0) - gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("type:", term_lc, strlen ("type:")) == 0) - gl_search_field (GIF_TYPE, term_lc + strlen ("type:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0) - gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"), - print_graph_inst_html, /* user_data = */ &cb_data); - else - gl_search (term_lc, - print_graph_inst_html, /* user_data = */ &cb_data); - - free (term_lc); - } + + gl_search (pg_data->search_info, print_graph_inst_html, + /* user_data = */ &cb_data); + if (cb_data.cfg != NULL) printf ("
    \n"); @@ -219,73 +186,101 @@ static int print_search_result (void *user_data) /* {{{ */ return (0); } /* }}} int print_search_result */ -static int print_host_list_callback (const char *host, void *user_data) /* {{{ */ +static int print_search_form (void *user_data) /* {{{ */ { - char *host_html; - - /* Make compiler happy */ - user_data = NULL; - - if (host == NULL) - return (EINVAL); - - host_html = html_escape (host); - if (host_html == NULL) - return (ENOMEM); - - printf ("
  • " - "%s
  • \n", - script_name (), host_html, host_html); + page_data_t *pg_data = user_data; + char search_term_html[1024]; - return (0); -} /* }}} int print_host_list_callback */ + if (pg_data->search_term != NULL) + { + html_escape_copy (search_term_html, pg_data->search_term, + sizeof (search_term_html)); + } + else + { + search_term_html[0] = 0; + } -static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */ -{ - return (0); - printf ("

    List of hosts

    \n" - "
      \n"); - gl_foreach_host (print_host_list_callback, /* user data = */ NULL); - printf ("
    \n"); + printf ("
    \n" + " \n" + "
    \n" + " Advanced search\n" +#if 0 + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" +#endif + "
    Search for
    \n" + " " + "
    \n" + "
    \n", + script_name (), search_term_html); return (0); -} /* }}} int print_host_list */ +} /* }}} int print_search_form */ -static int search_html (const char *term) /* {{{ */ +static int search_html (page_data_t *pg_data) /* {{{ */ { - page_data_t pg_data; page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT; char title[512]; - if (term != NULL) + if (pg_data->search_term != NULL) snprintf (title, sizeof (title), "Graphs matching \"%s\"", - term); + pg_data->search_term); else - strncpy (title, "List of all graphs", sizeof (title)); + strncpy (title, "Search", sizeof (title)); title[sizeof (title) - 1] = 0; - memset (&pg_data, 0, sizeof (pg_data)); - pg_data.search_term = term; - pg_callbacks.top_right = html_print_search_box; - pg_callbacks.middle_left = print_host_list; - pg_callbacks.middle_center = print_search_result; + pg_callbacks.middle_left = left_menu; + if (pg_data->search_term != NULL) + pg_callbacks.middle_center = print_search_result; + else + pg_callbacks.middle_center = print_search_form; - html_print_page (title, &pg_callbacks, &pg_data); + html_print_page (title, &pg_callbacks, pg_data); return (0); } /* }}} int search_html */ int action_search (void) /* {{{ */ { - char *search; + page_data_t pg_data; int status; - gl_update (); + pg_data.search_term = strtolower_copy (param ("q")); + if ((pg_data.search_term != NULL) && (pg_data.search_term[0] == 0)) + { + free (pg_data.search_term); + pg_data.search_term = NULL; + pg_data.search_info = NULL; + } + else + { + pg_data.search_info = search_parse (pg_data.search_term); + } + + status = search_html (&pg_data); - search = strtolower_copy (param ("q")); - status = search_html (search); - free (search); + free (pg_data.search_term); + search_destroy (pg_data.search_info); return (status); } /* }}} int action_search */