From: Sebastian Harl Date: Mon, 24 Mar 2008 11:06:49 +0000 (+0100) Subject: liboconfig: Include the filename in error messages. X-Git-Tag: collectd-4.2.6~6 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=7aee817a090f1f825e68c3a0288a373c52e16e03;p=collectd.git liboconfig: Include the filename in error messages. 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 "" is used instead, where X is replaced by the file descriptor. Signed-off-by: Sebastian Harl Signed-off-by: Florian Forster --- diff --git a/src/liboconfig/oconfig.c b/src/liboconfig/oconfig.c index db9285b3..8cc3c8ab 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), "", fileno (fh)); + + if ((status < 0) || (status >= sizeof (file))) { + c_file = ""; + } + 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 ea6ed0a0..48b9bf3d 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 @@ -186,7 +187,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 = ""; + 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 */