2 * collection4 - utils_search.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
30 #include "utils_search.h"
31 #include "graph_instance.h"
32 #include "utils_array.h"
35 #include <fcgi_stdio.h>
41 char *plugin_instance;
51 static char *read_quoted_string (const char **buffer) /* {{{ */
53 const char *ptr = *buffer;
62 while ((*ptr != '"') && (*ptr != 0))
72 if ((ret_len < 1) || (*ptr != '"'))
75 ret = malloc (ret_len + 1);
81 while ((*ptr != '"') && (*ptr != 0))
95 /* terminate string */
98 /* "ptr" points to the '"' sign, so advance one more */
103 } /* }}} char *read_quoted_string */
105 static char *read_unquoted_word (const char **buffer) /* {{{ */
107 const char *ptr = *buffer;
112 while (!isspace ((int) ptr[ret_len]) && (ptr[ret_len] != 0))
118 ret = malloc (ret_len + 1);
122 memcpy (ret, ptr, ret_len);
129 } /* }}} char *read_unquoted_word */
131 static char *next_token (const char **buffer) /* {{{ */
133 const char *ptr = *buffer;
136 while (isspace ((int) (*ptr)))
141 else if (ptr[0] == '"')
143 ret = read_quoted_string (&ptr);
151 ret = read_unquoted_word (&ptr);
156 } /* }}} char *next_token */
158 static int store_token_field (char **field, const char *token) /* {{{ */
162 if ((field == NULL) || (token == NULL))
165 copy = strdup (token);
173 } /* }}} int store_token_field */
175 static int store_token (search_info_t *si, const char *token) /* {{{ */
177 if (strncmp ("host:", token, strlen ("host:")) == 0)
178 return (store_token_field (&si->host, token + strlen ("host:")));
179 else if (strncmp ("plugin:", token, strlen ("plugin:")) == 0)
180 return (store_token_field (&si->plugin, token + strlen ("plugin:")));
181 else if (strncmp ("plugin_instance:", token, strlen ("plugin_instance:")) == 0)
182 return (store_token_field (&si->plugin_instance, token + strlen ("plugin_instance:")));
183 else if (strncmp ("type:", token, strlen ("type:")) == 0)
184 return (store_token_field (&si->type, token + strlen ("type:")));
185 else if (strncmp ("type_instance:", token, strlen ("type_instance:")) == 0)
186 return (store_token_field (&si->type_instance, token + strlen ("type_instance:")));
188 return (array_append (si->terms, token));
189 } /* }}} int store_token */
194 search_info_t *search_parse (const char *search) /* {{{ */
200 si = malloc (sizeof (*si));
203 memset (si, 0, sizeof (*si));
205 si->terms = array_create ();
206 if (si->terms == NULL)
214 while ((token = next_token (&ptr)) != NULL)
216 store_token (si, token);
221 } /* }}} search_info_t *search_parse */
223 void search_destroy (search_info_t *si) /* {{{ */
230 free (si->plugin_instance);
232 free (si->type_instance);
234 array_destroy (si->terms);
235 } /* }}} void search_destroy */
237 graph_ident_t *search_to_ident (search_info_t *si) /* {{{ */
242 return (ident_create ((si->host == NULL) ? ANY_TOKEN : si->host,
243 (si->plugin == NULL) ? ANY_TOKEN : si->plugin,
244 (si->plugin_instance == NULL) ? ANY_TOKEN : si->plugin_instance,
245 (si->type == NULL) ? ANY_TOKEN : si->type,
246 (si->type_instance == NULL) ? ANY_TOKEN : si->type_instance));
247 } /* }}} graph_ident_t *search_to_ident */
249 _Bool search_graph_title_matches (search_info_t *si, /* {{{ */
256 if ((si == NULL) || (title == NULL))
259 if (si->terms == NULL)
262 argc = array_argc (si->terms);
263 argv = array_argv (si->terms);
264 for (i = 0; i < argc; i++)
265 if (strstr (title, argv[i]) == NULL)
269 } /* }}} _Bool search_graph_title_matches */
271 _Bool search_graph_inst_matches (search_info_t *si, /* {{{ */
272 graph_config_t *cfg, graph_instance_t *inst,
279 if ((si == NULL) || (cfg == NULL) || (inst == NULL))
282 if ((si->host != NULL)
283 && !inst_matches_field (inst, GIF_HOST, si->host))
285 else if ((si->plugin != NULL)
286 && !inst_matches_field (inst, GIF_PLUGIN, si->plugin))
288 else if ((si->plugin_instance != NULL)
289 && !inst_matches_field (inst, GIF_PLUGIN_INSTANCE, si->plugin_instance))
291 else if ((si->type != NULL)
292 && !inst_matches_field (inst, GIF_TYPE, si->type))
294 else if ((si->type_instance != NULL)
295 && !inst_matches_field (inst, GIF_TYPE_INSTANCE, si->type_instance))
298 if (si->terms == NULL)
301 argc = array_argc (si->terms);
302 argv = array_argv (si->terms);
303 for (i = 0; i < argc; i++)
305 if (inst_matches_string (cfg, inst, argv[i]))
308 if ((title != NULL) && (strstr (title, argv[i]) != NULL))
315 } /* }}} _Bool search_graph_inst_matches */
317 /* vim: set sw=2 sts=2 et fdm=marker : */