Code

Merge remote-tracking branch 'github/pr/387'
[collectd.git] / src / types_list.c
index 847d0988dd608773416b7e37845867b9219a84d6..10cb4f28b81e674c7af566b9bf288851b84d9442 100644 (file)
@@ -33,7 +33,10 @@ static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
   int   fields_num;
 
   if (buf_len < 11)
+  {
+    ERROR ("parse_ds: (buf_len = %zu) < 11", buf_len);
     return (-1);
+  }
 
   if (buf[buf_len - 1] == ',')
   {
@@ -54,17 +57,26 @@ static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
   }
 
   if (fields_num != 4)
+  {
+    ERROR ("parse_ds: (fields_num = %i) != 4", fields_num);
     return (-1);
+  }
 
-  strncpy (dsrc->name, fields[0], sizeof (dsrc->name));
-  dsrc->name[sizeof (dsrc->name) - 1] = '\0';
+  sstrncpy (dsrc->name, fields[0], sizeof (dsrc->name));
 
   if (strcasecmp (fields[1], "GAUGE") == 0)
     dsrc->type = DS_TYPE_GAUGE;
   else if (strcasecmp (fields[1], "COUNTER") == 0)
     dsrc->type = DS_TYPE_COUNTER;
+  else if (strcasecmp (fields[1], "DERIVE") == 0)
+    dsrc->type = DS_TYPE_DERIVE;
+  else if (strcasecmp (fields[1], "ABSOLUTE") == 0)
+    dsrc->type = DS_TYPE_ABSOLUTE;
   else
+  {
+    ERROR ("(fields[1] = %s) != (GAUGE || COUNTER || DERIVE || ABSOLUTE)", fields[1]);
     return (-1);
+  }
 
   if (strcasecmp (fields[2], "U") == 0)
     dsrc->min = NAN;
@@ -76,45 +88,50 @@ static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
   else
     dsrc->max = atof (fields[3]);
 
-  DEBUG ("parse_ds: dsrc = {%s, %i, %lf, %lf};",
-      dsrc->name, dsrc->type, dsrc->min, dsrc->max);
-
   return (0);
 } /* int parse_ds */
 
-static void parse_line (char *buf, size_t buf_len)
+static void parse_line (char *buf)
 {
   char  *fields[64];
   size_t fields_num;
-  data_set_t ds;
+  data_set_t *ds;
   int i;
 
   fields_num = strsplit (buf, fields, 64);
   if (fields_num < 2)
     return;
 
-  memset (&ds, '\0', sizeof (ds));
+  /* Ignore lines which begin with a hash sign. */
+  if (fields[0][0] == '#')
+    return;
 
-  strncpy (ds.type, fields[0], sizeof (ds.type));
-  ds.type[sizeof (ds.type) - 1] = '\0';
+  ds = (data_set_t *) malloc (sizeof (data_set_t));
+  if (ds == NULL)
+    return;
+
+  memset (ds, '\0', sizeof (data_set_t));
 
-  ds.ds_num = fields_num - 1;
-  ds.ds = (data_source_t *) calloc (ds.ds_num, sizeof (data_source_t));
-  if (ds.ds == NULL)
+  sstrncpy (ds->type, fields[0], sizeof (ds->type));
+
+  ds->ds_num = fields_num - 1;
+  ds->ds = (data_source_t *) calloc (ds->ds_num, sizeof (data_source_t));
+  if (ds->ds == NULL)
     return;
 
-  for (i = 0; i < ds.ds_num; i++)
-    if (parse_ds (ds.ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
+  for (i = 0; i < ds->ds_num; i++)
+    if (parse_ds (ds->ds + i, fields[i + 1], strlen (fields[i + 1])) != 0)
     {
-      sfree (ds.ds);
+      sfree (ds->ds);
+      ERROR ("types_list: parse_line: Cannot parse data source #%i "
+         "of data set %s", i, ds->type);
       return;
     }
 
-  DEBUG ("parse_line: ds = {%s, %i, %p};",
-      ds.type, ds.ds_num, (void *) ds.ds);
+  plugin_register_data_set (ds);
 
-  plugin_register_data_set (&ds);
-  sfree (ds.ds);
+  sfree (ds->ds);
+  sfree (ds);
 } /* void parse_line */
 
 static void parse_file (FILE *fh)
@@ -141,16 +158,21 @@ static void parse_file (FILE *fh)
     if ((buf_len == 0) || (buf[0] == '#'))
       continue;
 
-    parse_line (buf, buf_len);
+    while ((buf_len > 0) && ((buf[buf_len - 1] == '\n')
+         || (buf[buf_len - 1] == '\n')))
+      buf[--buf_len] = '\0';
+
+    if (buf_len == 0)
+      continue;
+
+    parse_line (buf);
   } /* while (fgets) */
 } /* void parse_file */
 
-int read_types_list (void)
+int read_types_list (const char *file)
 {
-  const char *file;
   FILE *fh;
 
-  file = global_option_get ("TypesDB");
   if (file == NULL)
     return (-1);
 
@@ -158,7 +180,9 @@ int read_types_list (void)
   if (fh == NULL)
   {
     char errbuf[1024];
-    ERROR ("open (%s) failed: %s", 
+    fprintf (stderr, "Failed to open types database `%s': %s.\n",
+       file, sstrerror (errno, errbuf, sizeof (errbuf)));
+    ERROR ("Failed to open types database `%s': %s",
        file, sstrerror (errno, errbuf, sizeof (errbuf)));
     return (-1);
   }