[collectd] [PATCH] liboconfig: Include the filename in error messages.

Sebastian Harl sh at tokkee.org
Mon Mar 24 12:06:49 CET 2008


As collectd now supports more than one config file, this is more
convenient.

A module-global variable is used for that purpose. If no filename is
available (e.g. if the user uses oconfig_parse_fh() directly), a string
like "<fd#X>" is used instead, where X is replaced by the file descriptor.

Signed-off-by: Sebastian Harl <sh at tokkee.org>
---
 src/liboconfig/oconfig.c |   23 +++++++++++++++++++++++
 src/liboconfig/parser.y  |   11 ++++++++++-
 2 files changed, 33 insertions(+), 1 deletions(-)

diff --git a/src/liboconfig/oconfig.c b/src/liboconfig/oconfig.c
index db9285b..8cc3c8a 100644
--- a/src/liboconfig/oconfig.c
+++ b/src/liboconfig/oconfig.c
@@ -27,6 +27,7 @@
 extern FILE *yyin;
 
 oconfig_item_t *ci_root;
+char           *c_file;
 
 static void yyset_in  (FILE *fd)
 {
@@ -38,8 +39,24 @@ oconfig_item_t *oconfig_parse_fh (FILE *fh)
   int status;
   oconfig_item_t *ret;
 
+  char file[10];
+
   yyset_in (fh);
 
+  if (NULL == c_file) {
+    int status;
+
+    status = snprintf (file, sizeof (file), "<fd#%d>", fileno (fh));
+
+    if ((status < 0) || (status >= sizeof (file))) {
+      c_file = "<unknown>";
+    }
+    else {
+      file[sizeof (file) - 1] = '\0';
+      c_file = file;
+    }
+  }
+
   status = yyparse ();
   if (status != 0)
   {
@@ -47,6 +64,8 @@ oconfig_item_t *oconfig_parse_fh (FILE *fh)
     return (NULL);
   }
 
+  c_file = NULL;
+
   ret = ci_root;
   ci_root = NULL;
   yyset_in ((FILE *) 0);
@@ -59,6 +78,8 @@ oconfig_item_t *oconfig_parse_file (const char *file)
   FILE *fh;
   oconfig_item_t *ret;
 
+  c_file = file;
+
   fh = fopen (file, "r");
   if (fh == NULL)
   {
@@ -69,6 +90,8 @@ oconfig_item_t *oconfig_parse_file (const char *file)
   ret = oconfig_parse_fh (fh);
   fclose (fh);
 
+  c_file = NULL;
+
   return (ret);
 } /* oconfig_item_t *oconfig_parse_file */
 
diff --git a/src/liboconfig/parser.y b/src/liboconfig/parser.y
index 8df2c6e..91f20e9 100644
--- a/src/liboconfig/parser.y
+++ b/src/liboconfig/parser.y
@@ -30,6 +30,7 @@ extern int yylineno;
 extern char *yytext;
 
 extern oconfig_item_t *ci_root;
+extern char           *c_file;
 %}
 
 %start entire_file
@@ -192,7 +193,15 @@ entire_file:
 %%
 static int yyerror (const char *s)
 {
-	fprintf (stderr, "Error in line %i near `%s': %s\n", yylineno, yytext, s);
+	char *text;
+
+	if (*yytext == '\n')
+		text = "<newline>";
+	else
+		text = yytext;
+
+	fprintf (stderr, "Parse error in file `%s', line %i near `%s': %s\n",
+		c_file, yylineno, text, s);
 	return (-1);
 } /* int yyerror */
 
-- 
1.5.4.3.325.g6d216

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20080324/7e24e9de/attachment.pgp 


More information about the collectd mailing list