From 7004fc9c03aa95adc214eb15ca2400487d122d58 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 12 Jul 2010 09:40:31 +0200 Subject: [PATCH] src/utils_search.[ch]: Add a module for parsing search strings. --- src/Makefile.am | 3 +- src/utils_search.c | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils_search.h | 35 ++++++++++ 3 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 src/utils_search.c create mode 100644 src/utils_search.h diff --git a/src/Makefile.am b/src/Makefile.am index f2ba322..6ef1f82 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -34,4 +34,5 @@ collection_fcgi_SOURCES = main.c \ graph_list.c graph_list.h \ rrd_args.c rrd_args.h \ utils_array.c utils_array.h \ - utils_cgi.c utils_cgi.h + utils_cgi.c utils_cgi.h \ + utils_search.c utils_search.h diff --git a/src/utils_search.c b/src/utils_search.c new file mode 100644 index 0000000..cb01a41 --- /dev/null +++ b/src/utils_search.c @@ -0,0 +1,197 @@ +/** + * collection4 - utils_search.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 + **/ + +#include +#include +#include +#include + +#include "utils_search.h" +#include "utils_array.h" + +#include +#include + +struct search_info_s +{ + char *host; + char *plugin; + char *plugin_instance; + char *type; + char *type_instance; + + str_array_t *terms; +}; + +static char *read_quoted_string (const char **buffer) /* {{{ */ +{ + const char *ptr = *buffer; + char *ret; + size_t ret_len; + + if (ptr[0] != '"') + return (NULL); + ptr++; + + ret_len = 0; + while ((*ptr != '"') && (*ptr != 0)) + { + ret_len++; + + if (*ptr == '\\') + ptr += 2; + else + ptr++; + } + + if ((ret_len < 1) || (*ptr != '"')) + return (NULL); + + ret = malloc (ret_len + 1); + if (ret == NULL) + return (NULL); + + ptr = *buffer + 1; + ret_len = 0; + while ((*ptr != '"') && (*ptr != 0)) + { + if (*ptr == '"') + break; + + if (*ptr == '\\') + ptr++; + + ret[ret_len] = *ptr; + + ptr++; + ret_len++; + } + + /* terminate string */ + ret[ret_len] = 0; + + /* "ptr" points to the '"' sign, so advance one more */ + ptr++; + *buffer = ptr; + + return (ret); +} /* }}} char *read_quoted_string */ + +static char *read_unquoted_word (const char **buffer) /* {{{ */ +{ + const char *ptr = *buffer; + char *ret; + size_t ret_len; + + ret_len = 0; + while (!isspace ((int) ptr[ret_len]) && (ptr[ret_len] != 0)) + ret_len++; + + if (ret_len < 1) + return (NULL); + + ret = malloc (ret_len + 1); + if (ret == NULL) + return (NULL); + + memcpy (ret, ptr, ret_len); + ret[ret_len] = 0; + + ptr += ret_len; + *buffer = ptr; + + return (ret); +} /* }}} char *read_unquoted_word */ + +static char *next_token (const char **buffer) /* {{{ */ +{ + const char *ptr = *buffer; + char *ret; + + while (isspace ((int) (*ptr))) + ptr++; + + if (ptr[0] == 0) + return (NULL); + else if (ptr[0] == '"') + { + ret = read_quoted_string (&ptr); + if (ret != NULL) + { + *buffer = ptr; + return (ret); + } + } + + ret = read_unquoted_word (&ptr); + if (ret != NULL) + *buffer = ptr; + + return (ret); +} /* }}} char *next_token */ + +search_info_t *search_parse (const char *search) /* {{{ */ +{ + const char *ptr; + char *token; + search_info_t *si; + + si = malloc (sizeof (*si)); + if (si == NULL) + return (NULL); + memset (si, 0, sizeof (*si)); + + si->terms = array_create (); + if (si->terms == NULL) + { + free (si); + return (NULL); + } + + ptr = search; + + while ((token = next_token (&ptr)) != NULL) + { + array_append (si->terms, token); + + free (token); + } + + return (si); +} /* }}} search_info_t *search_parse */ + +void search_destroy (search_info_t *si) /* {{{ */ +{ + if (si == NULL) + return; + + free (si->host); + free (si->plugin); + free (si->plugin_instance); + free (si->type); + free (si->type_instance); + + array_destroy (si->terms); +} /* }}} void search_destroy */ + +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_search.h b/src/utils_search.h new file mode 100644 index 0000000..a1fa643 --- /dev/null +++ b/src/utils_search.h @@ -0,0 +1,35 @@ +/** + * collection4 - utils_search.h + * 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 + **/ + +#ifndef UTILS_SEARCH_H +#define UTILS_SEARCH_H 1 + +struct search_info_s; +typedef struct search_info_s search_info_t; + +search_info_t *search_parse (const char *search); +void search_destroy (search_info_t *si); + +#endif /* UTILS_SEARCH_H */ +/* vim: set sw=2 sts=2 et fdm=marker : */ + -- 2.11.0