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@tokkee.org>
Signed-off-by: Florian Forster <octo@huhu.verplant.org>
extern FILE *yyin;
oconfig_item_t *ci_root;
+char *c_file;
static void yyset_in (FILE *fd)
{
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)
{
return (NULL);
}
+ c_file = NULL;
+
ret = ci_root;
ci_root = NULL;
yyset_in ((FILE *) 0);
FILE *fh;
oconfig_item_t *ret;
+ c_file = file;
+
fh = fopen (file, "r");
if (fh == NULL)
{
ret = oconfig_parse_fh (fh);
fclose (fh);
+ c_file = NULL;
+
return (ret);
} /* oconfig_item_t *oconfig_parse_file */
extern char *yytext;
extern oconfig_item_t *ci_root;
+extern char *c_file;
%}
%start 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 */