diff --git a/lib/conf-lex.l b/lib/conf-lex.l index dd369321..f3c97f67 100644 --- a/lib/conf-lex.l +++ b/lib/conf-lex.l @@ -25,6 +25,7 @@ #include "data.h" #include "conf-parse.h" #include "error.h" +#include "scanner.h" static int buffer_count; static int buffer_max; @@ -48,6 +49,7 @@ char sensors_lex_error[100]; /* Scanner for configuration files */ %option nodefault +%option noyywrap %option yylineno %option nounput @@ -269,9 +271,30 @@ OCTESC (1[0-7]{0,2})|([2-7][0-7]?)|(0[1-7][0-7]?)|(00[1-7]) %% -int sensors_yywrap(void) +/* + Do the buffer handling manually. This allows us to scan as many + config files as we need to, while cleaning up properly after each + one. The "BEGIN(0)" line ensures that we start in the default state, + even if e.g. the previous config file was syntactically broken. + + Returns 0 if successful, !0 otherwise. +*/ + +static YY_BUFFER_STATE scan_buf = (YY_BUFFER_STATE)0; + +int sensors_scanner_init(FILE *input) { - yy_delete_buffer(YY_CURRENT_BUFFER); - return 1; + BEGIN(0); + if (!(scan_buf = sensors_yy_create_buffer(input, YY_BUF_SIZE))) + return -1; + + sensors_yy_switch_to_buffer(scan_buf); + return 0; +} + +void sensors_scanner_exit(void) +{ + sensors_yy_delete_buffer(scan_buf); + scan_buf = (YY_BUFFER_STATE)0; } diff --git a/lib/init.c b/lib/init.c index a503a4a0..863c020e 100644 --- a/lib/init.c +++ b/lib/init.c @@ -26,6 +26,7 @@ #include "access.h" #include "conf.h" #include "sysfs.h" +#include "scanner.h" static void free_proc_chips_entry(sensors_proc_chips_entry entry); static void free_chip_name(sensors_chip_name name); @@ -48,7 +49,8 @@ int sensors_init(FILE *input) if ((res = sensors_read_proc_chips()) || (res = sensors_read_proc_bus())) return res; } - sensors_yyin = input; + if ((res = sensors_scanner_init(input))) + return -SENSORS_ERR_PARSE; if ((res = sensors_yyparse())) return -SENSORS_ERR_PARSE; if ((res = sensors_substitute_busses())); @@ -60,6 +62,8 @@ void sensors_cleanup(void) { int i; + sensors_scanner_exit(); + for (i = 0; i < sensors_proc_chips_count; i++) free_proc_chips_entry(sensors_proc_chips[i]); free(sensors_proc_chips); diff --git a/lib/scanner.h b/lib/scanner.h new file mode 100644 index 00000000..d8e9a413 --- /dev/null +++ b/lib/scanner.h @@ -0,0 +1,27 @@ +/* + scanner.h - Part of libsensors, a Linux library for reading sensor data. + Copyright (c) 2006 Mark M. Hoffman + + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef LIB_SENSORS_SCANNER_H +#define LIB_SENSORS_SCANNER_H + +extern int sensors_scanner_init(FILE *input); +extern void sensors_scanner_exit(void); + +#endif +