openldap: copyright update + minor cleanup
[collectd.git] / src / utils_ignorelist.c
index 532eb4b..a8ca7db 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/utils_ignorelist.c
  * Copyright (C) 2006 Lubos Stanek <lubek at users.sourceforge.net>
+ * Copyright (C) 2008 Florian Forster <octo at collectd.org>
  *
  * This program is free software; you can redistribute it and/
  * or modify it under the terms of the GNU General Public Li-
@@ -19,6 +20,7 @@
  *
  * Authors:
  *   Lubos Stanek <lubek at users.sourceforge.net>
+ *   Florian Forster <octo at collectd.org>
  **/
 /**
  * ignorelist handles plugin's list of configured collectable
  *     return;
  **/
 
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "common.h"
-#include "utils_debug.h"
+#include "plugin.h"
 #include "utils_ignorelist.h"
 
 /*
@@ -83,67 +89,45 @@ static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
 }
 
 #if HAVE_REGEX_H
-static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
+static int ignorelist_append_regex(ignorelist_t *il, const char *re_str)
 {
-       int rcompile;
-       regex_t *regtemp;
-       int errsize;
-       char *regerr = NULL;
-       ignorelist_item_t *new;
+       regex_t *re;
+       ignorelist_item_t *entry;
+       int status;
 
-       /* create buffer */
-       if ((regtemp = malloc(sizeof(regex_t))) == NULL)
+       re = malloc (sizeof (*re));
+       if (re == NULL)
        {
-               syslog (LOG_ERR, "cannot allocate new config entry");
-               return (1);
+               ERROR ("utils_ignorelist: malloc failed");
+               return (ENOMEM);
        }
-       memset (regtemp, '\0', sizeof(regex_t));
+       memset (re, 0, sizeof (*re));
 
-       /* compile regex */
-       if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
+       status = regcomp (re, re_str, REG_EXTENDED);
+       if (status != 0)
        {
-               /* prepare message buffer */
-               errsize = regerror(rcompile, regtemp, NULL, 0);
-               if (errsize)
-                       regerr = smalloc(errsize);
-               /* get error message */
-               if (regerror (rcompile, regtemp, regerr, errsize))
-               {
-                       fprintf (stderr, "Cannot compile regex %s: %i/%s",
-                                       entry, rcompile, regerr);
-                       syslog (LOG_ERR, "Cannot compile regex %s: %i/%s",
-                                       entry, rcompile, regerr);
-               }
-               else
-               {
-                       fprintf (stderr, "Cannot compile regex %s: %i",
-                                       entry, rcompile);
-                       syslog (LOG_ERR, "Cannot compile regex %s: %i",
-                                       entry, rcompile);
-               }
-
-               if (errsize)
-                       sfree (regerr);
-               regfree (regtemp);
-               return (1);
+               char errbuf[1024] = "";
+               regerror (status, re, errbuf, sizeof (errbuf));
+               ERROR ("utils_ignorelist: regcomp failed: %s", errbuf);
+               regfree (re);
+               sfree (re);
+               return (status);
        }
-       DBG("regex compiled: %s - %i", entry, rcompile);
 
-       /* create new entry */
-       if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
+       entry = malloc (sizeof (*entry));
+       if (entry == NULL)
        {
-               syslog (LOG_ERR, "cannot allocate new config entry");
-               regfree (regtemp);
-               return (1);
+               ERROR ("utils_ignorelist: malloc failed");
+               regfree (re);
+               sfree (re);
+               return (ENOMEM);
        }
-       memset (new, '\0', sizeof(ignorelist_item_t));
-       new->rmatch = regtemp;
-
-       /* append new entry */
-       ignorelist_append (il, new);
+       memset (entry, 0, sizeof (*entry));
+       entry->rmatch = re;
 
+       ignorelist_append (il, entry);
        return (0);
-} /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
+} /* int ignorelist_append_regex */
 #endif
 
 static int ignorelist_append_string(ignorelist_t *il, const char *entry)
@@ -153,7 +137,7 @@ static int ignorelist_append_string(ignorelist_t *il, const char *entry)
        /* create new entry */
        if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
        {
-               syslog (LOG_ERR, "cannot allocate new entry");
+               ERROR ("cannot allocate new entry");
                return (1);
        }
        memset (new, '\0', sizeof(ignorelist_item_t));
@@ -213,10 +197,6 @@ ignorelist_t *ignorelist_create (int invert)
 
        /* smalloc exits if it failes */
        il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
-       DBG("Ignorelist created 0x%p, default is %s",
-                       (void *) il,
-                       invert ? "collect" : "ignore");
-
        memset (il, '\0', sizeof (ignorelist_t));
 
        /*
@@ -236,8 +216,6 @@ void ignorelist_free (ignorelist_t *il)
        ignorelist_item_t *this;
        ignorelist_item_t *next;
 
-       DBG ("(il = 0x%p)", (void *) il);
-
        if (il == NULL)
                return;
 
@@ -248,6 +226,7 @@ void ignorelist_free (ignorelist_t *il)
                if (this->rmatch != NULL)
                {
                        regfree (this->rmatch);
+                       sfree (this->rmatch);
                        this->rmatch = NULL;
                }
 #endif
@@ -270,7 +249,7 @@ void ignorelist_set_invert (ignorelist_t *il, int invert)
 {
        if (il == NULL)
        {
-               DBG("ignore call with ignorelist_t == NULL");
+               DEBUG("ignore call with ignorelist_t == NULL");
                return;
        }
 
@@ -279,16 +258,15 @@ void ignorelist_set_invert (ignorelist_t *il, int invert)
 
 /*
  * append entry into ignorelist_t
- * return 1 for success
+ * return 0 for success
  */
 int ignorelist_add (ignorelist_t *il, const char *entry)
 {
-       int ret;
        size_t entry_len;
 
        if (il == NULL)
        {
-               DBG ("add called with ignorelist_t == NULL");
+               DEBUG ("add called with ignorelist_t == NULL");
                return (1);
        }
 
@@ -297,7 +275,7 @@ int ignorelist_add (ignorelist_t *il, const char *entry)
        /* append nothing */
        if (entry_len == 0)
        {
-               DBG("not appending: empty entry");
+               DEBUG("not appending: empty entry");
                return (1);
        }
 
@@ -306,24 +284,21 @@ int ignorelist_add (ignorelist_t *il, const char *entry)
        if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
        {
                char *entry_copy;
+               size_t entry_copy_size;
+               int status;
 
                /* We need to copy `entry' since it's const */
-               entry_copy = smalloc (entry_len);
-               memset (entry_copy, '\0', entry_len);
-               strncpy (entry_copy, entry + 1, entry_len - 2);
+               entry_copy_size = entry_len - 1;
+               entry_copy = smalloc (entry_copy_size);
+               sstrncpy (entry_copy, entry + 1, entry_copy_size);
 
-               DBG("I'm about to add regex entry: %s", entry_copy);
-               ret = ignorelist_append_regex(il, entry_copy);
+               status = ignorelist_append_regex(il, entry_copy);
                sfree (entry_copy);
+               return status;
        }
-       else
 #endif
-       {
-               DBG("to add entry: %s", entry);
-               ret = ignorelist_append_string(il, entry);
-       }
 
-       return (ret);
+       return ignorelist_append_string(il, entry);
 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
 
 /*
@@ -334,10 +309,8 @@ int ignorelist_match (ignorelist_t *il, const char *entry)
 {
        ignorelist_item_t *traverse;
 
-       assert (il != NULL);
-
        /* if no entries, collect all */
-       if (il->head == NULL)
+       if ((il == NULL) || (il->head == NULL))
                return (0);
 
        if ((entry == NULL) || (strlen (entry) == 0))