diff options
author | Jean Delvare <khali@linux-fr.org> | 2009-02-15 17:26:57 +0000 |
---|---|---|
committer | Jean Delvare <khali@linux-fr.org> | 2009-02-15 17:26:57 +0000 |
commit | 0efe346b80e69cc0fefad03c42d6e99d9f8fe48c (patch) | |
tree | 4a5360823418e61c42c024b7f1afc33e8cb5cdf4 | |
parent | 6a7286464d6c58417ded560413b546c36af37509 (diff) | |
download | lm-sensors-git-0efe346b80e69cc0fefad03c42d6e99d9f8fe48c.tar.gz |
Include the configuration file name when reporting parse errors.
git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@5649 7894878c-1315-0410-8ee3-d5d059ff63e0
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | doc/libsensors-API.txt | 4 | ||||
-rw-r--r-- | lib/access.c | 11 | ||||
-rw-r--r-- | lib/conf-parse.y | 4 | ||||
-rw-r--r-- | lib/conf.h | 1 | ||||
-rw-r--r-- | lib/data.c | 11 | ||||
-rw-r--r-- | lib/error.c | 23 | ||||
-rw-r--r-- | lib/error.h | 21 | ||||
-rw-r--r-- | lib/init.c | 6 |
9 files changed, 65 insertions, 19 deletions
@@ -12,7 +12,8 @@ SVN-HEAD Report configuration file read errors Exit the configuration file parser sooner Free bus statements from the configuration file sooner - Read extra configuration files from /etc/sensors.d + Read extra configuration files from /etc/sensors.d (#2174) + Report the configuration file name on parse errors lm_sensors.init: Support new format of /etc/sysconfig/lm_sensors (#2246) Drop support for kernels 2.4 and earlier lm_sensors.init.suse: Delete (actual SuSE script is much different) diff --git a/doc/libsensors-API.txt b/doc/libsensors-API.txt index 329d5461..9e1fe9a7 100644 --- a/doc/libsensors-API.txt +++ b/doc/libsensors-API.txt @@ -24,6 +24,10 @@ given new feature. enum sensors_subfeature_type SENSORS_SUBFEATURE_CURR_BEEP * Added error value for excessive recursion depth #define SENSORS_ERR_RECURSION 11 +* Added parse error reporting function including the configuration file + name + extern void (*sensors_parse_error_wfn) (const char *err, + const char *filename, int lineno); 0x401 lm-sensors 3.0.2 to 3.0.3 * Added bus type "virtual": diff --git a/lib/access.c b/lib/access.c index bb55c954..af296abc 100644 --- a/lib/access.c +++ b/lib/access.c @@ -1,7 +1,7 @@ /* access.c - Part of libsensors, a Linux library for reading sensor data. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> - Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org> + Copyright (C) 2007-2009 Jean Delvare <khali@linux-fr.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -511,7 +511,8 @@ static int sensors_do_this_chip_sets(const sensors_chip_name *name) subfeature = sensors_lookup_subfeature_name(chip_features, chip->sets[i].name); if (!subfeature) { - sensors_parse_error("Unknown feature name", + sensors_parse_error_wfn("Unknown feature name", + chip->sets[i].line.filename, chip->sets[i].line.lineno); err = -SENSORS_ERR_NO_ENTRY; continue; @@ -521,14 +522,16 @@ static int sensors_do_this_chip_sets(const sensors_chip_name *name) chip->sets[i].value, 0, 0, &value); if (res) { - sensors_parse_error("Error parsing expression", + sensors_parse_error_wfn("Error parsing expression", + chip->sets[i].line.filename, chip->sets[i].line.lineno); err = res; continue; } if ((res = sensors_set_value(name, subfeature->number, value))) { - sensors_parse_error("Failed to set value", + sensors_parse_error_wfn("Failed to set value", + chip->sets[i].line.filename, chip->sets[i].line.lineno); err = res; continue; diff --git a/lib/conf-parse.y b/lib/conf-parse.y index 0ee7de8a..6641253a 100644 --- a/lib/conf-parse.y +++ b/lib/conf-parse.y @@ -332,10 +332,10 @@ chip_name: NAME void sensors_yyerror(const char *err) { if (sensors_lex_error[0]) { - sensors_parse_error(sensors_lex_error,sensors_yylineno); + sensors_parse_error_wfn(sensors_lex_error, sensors_yyfilename, sensors_yylineno); sensors_lex_error[0] = '\0'; } else - sensors_parse_error(err,sensors_yylineno); + sensors_parse_error_wfn(err, sensors_yyfilename, sensors_yylineno); } sensors_expr *malloc_expr(void) @@ -24,6 +24,7 @@ /* This is defined in conf-lex.l */ int sensors_yylex(void); extern char sensors_lex_error[]; +extern const char *sensors_yyfilename; extern int sensors_yylineno; extern FILE *sensors_yyin; @@ -202,7 +202,8 @@ int sensors_parse_bus_id(const char *name, sensors_bus_id *bus) return 0; } -static int sensors_substitute_chip(sensors_chip_name *name, int lineno) +static int sensors_substitute_chip(sensors_chip_name *name, + const char *filename, int lineno) { int i, j; for (i = 0; i < sensors_config_busses_count; i++) @@ -211,7 +212,8 @@ static int sensors_substitute_chip(sensors_chip_name *name, int lineno) break; if (i == sensors_config_busses_count) { - sensors_parse_error("Undeclared bus id referenced", lineno); + sensors_parse_error_wfn("Undeclared bus id referenced", + filename, lineno); name->bus.nr = SENSORS_BUS_NR_IGNORE; return -SENSORS_ERR_BUS_NAME; } @@ -238,10 +240,12 @@ int sensors_substitute_busses(void) { int err, i, j, lineno; sensors_chip_name_list *chips; + const char *filename; int res = 0; for (i = sensors_config_chips_subst; i < sensors_config_chips_count; i++) { + filename = sensors_config_chips[i].line.filename; lineno = sensors_config_chips[i].line.lineno; chips = &sensors_config_chips[i].chips; for (j = 0; j < chips->fits_count; j++) { @@ -250,7 +254,8 @@ int sensors_substitute_busses(void) if (chips->fits[j].bus.nr == SENSORS_BUS_NR_ANY) continue; - err = sensors_substitute_chip(&chips->fits[j], lineno); + err = sensors_substitute_chip(&chips->fits[j], + filename, lineno); if (err) res = err; } diff --git a/lib/error.c b/lib/error.c index d626e87e..6df22420 100644 --- a/lib/error.c +++ b/lib/error.c @@ -1,7 +1,7 @@ /* error.c - Part of libsensors, a Linux library for reading sensor data. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> - Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org> + Copyright (C) 2007-2009 Jean Delvare <khali@linux-fr.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -25,10 +25,14 @@ #include "general.h" static void sensors_default_parse_error(const char *err, int lineno); +static void sensors_default_parse_error_wfn(const char *err, + const char *filename, int lineno); static void sensors_default_fatal_error(const char *proc, const char *err); void (*sensors_parse_error) (const char *err, int lineno) = sensors_default_parse_error; +void (*sensors_parse_error_wfn) (const char *err, const char *filename, + int lineno) = sensors_default_parse_error_wfn; void (*sensors_fatal_error) (const char *proc, const char *err) = sensors_default_fatal_error; @@ -64,6 +68,23 @@ void sensors_default_parse_error(const char *err, int lineno) fprintf(stderr, "Error: %s\n", err); } +void sensors_default_parse_error_wfn(const char *err, + const char *filename, int lineno) +{ + /* If application provided a custom parse error reporting function + but not the variant with the filename, fall back to the original + variant without the filename, for backwards compatibility. */ + if (sensors_parse_error != sensors_default_parse_error || + !filename) + return sensors_parse_error(err, lineno); + + if (lineno) + fprintf(stderr, "Error: File %s, line %d: %s\n", filename, + lineno, err); + else + fprintf(stderr, "Error: File %s: %s\n", filename, err); +} + void sensors_default_fatal_error(const char *proc, const char *err) { fprintf(stderr, "Fatal error in `%s': %s\n", proc, err); diff --git a/lib/error.h b/lib/error.h index 74a71732..2b3ba560 100644 --- a/lib/error.h +++ b/lib/error.h @@ -1,7 +1,7 @@ /* error.h - Part of libsensors, a Linux library for reading sensor data. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> - Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org> + Copyright (C) 2007-2009 Jean Delvare <khali@linux-fr.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -44,11 +44,22 @@ extern "C" { You may not modify the result! */ const char *sensors_strerror(int errnum); -/* This function is called when a parse error is detected. Give it a new - value, and your own function is called instead of the default (which - prints to stderr). This function may terminate the program, but it - usually outputs an error and returns. */ +/* These functions are called when a parse error is detected. Give them new + values, and your own functions are called instead of the default (which + print to stderr). These functions may terminate the program, but they + usually output an error and return. The first function is the original + one, the second one was added later when support for multiple + configuration files was added. + The library code now only calls the second function. However, for + backwards compatibility, if an application provides a custom handling + function for the first function but not the second, then all parse + errors will be reported using the first function (that is, the filename + is never reported.) + Note that filename can be NULL (if filename isn't known) and lineno + can be 0 (if the error occurs before the actual parsing starts.) */ extern void (*sensors_parse_error) (const char *err, int lineno); +extern void (*sensors_parse_error_wfn) (const char *err, + const char *filename, int lineno); /* This function is called when an immediately fatal error (like no memory left) is detected. Give it a new value, and your own function @@ -125,7 +125,7 @@ static int add_config_from_dir(const char *dir) count = scandir(dir, &namelist, config_file_filter, alphasort); if (count < 0) { - sensors_parse_error(strerror(errno), 0); + sensors_parse_error_wfn(strerror(errno), NULL, 0); return -SENSORS_ERR_PARSE; } @@ -147,7 +147,7 @@ static int add_config_from_dir(const char *dir) fclose(input); } else { res = -SENSORS_ERR_PARSE; - sensors_parse_error(strerror(errno), 0); + sensors_parse_error_wfn(strerror(errno), path, 0); } } @@ -187,7 +187,7 @@ int sensors_init(FILE *input) goto exit_cleanup; } else if (errno != ENOENT) { - sensors_parse_error(strerror(errno), 0); + sensors_parse_error_wfn(strerror(errno), name, 0); res = -SENSORS_ERR_PARSE; goto exit_cleanup; } |