root/lm-sensors/branches/scanner-opt-branch/lib/conf-lex.l @ 4122

Revision 4122, 6.2 KB (checked in by mmh, 7 years ago)

Cleanup style and formatting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1%{
2/*
3    conf-lex.l - Part of libsensors, a Linux library for reading sensor data.
4    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <stdlib.h>
22#include <string.h>
23
24#include "general.h"
25#include "data.h"
26#include "conf-parse.h"
27#include "error.h"
28#include "scanner.h"
29
30static int buffer_count;
31static int buffer_max;
32static char *buffer;
33
34char sensors_lex_error[100];
35
36#define buffer_malloc() sensors_malloc_array(&buffer,&buffer_count,\
37                                             &buffer_max,1)
38#define buffer_free() sensors_free_array(&buffer,&buffer_count,\
39                                         &buffer_max)
40#define buffer_add_char(c) sensors_add_array_el(c,&buffer,\
41                                                &buffer_count,\
42                                                &buffer_max,1)
43#define buffer_add_string(s) sensors_add_array_els(s,strlen(s),\
44                                                   &buffer, \
45                                                   &buffer_count,&buffer_max,1)
46
47%}
48
49 /* Scanner for configuration files */
50
51%option nodefault
52%option noyywrap
53%option yylineno
54%option nounput
55
56 /*
57  * States. 'Normal' states STRING and MIDDLE share some rules;
58  * other states have only their own rules
59  */
60
61%s MIDDLE
62%x STRING
63%x ERR
64
65 /* Any whitespace-like character */
66
67BLANK           [[:space:]]
68
69IDCHAR          [[:alnum:]_]
70
71 /* Note: `10', `10.4' and `.4' are valid, `10.' is not */
72
73FLOAT   [[:digit:]]*\.?[[:digit:]]+
74
75 /* Only positive whole numbers are recognized here */
76
77NUM     0|([1-9][[:digit:]]*)
78
79 /* Only number between 1 and 255, octally represented. */
80
81OCTESC          (1[0-7]{0,2})|([2-7][0-7]?)|(0[1-7][0-7]?)|(00[1-7])
82
83
84%%
85
86
87 /* End of line: It may be the end of this line. Same for End of file. */
88
89<MIDDLE>\n      |
90<MIDDLE><<EOF>> {
91                  BEGIN(INITIAL);
92                  return EOL;
93                }
94
95 /*
96  * We want to match any blank, except End of line; that is why we have to
97  * match whitespace one by one!
98  */
99
100{BLANK}         /* Eat up a blank */
101
102 /* Escaped End of line: eat and be happy */
103
104<MIDDLE>\\\n    /* Eat this! */
105
106 /*
107  * Remove a comment; we do not change the state,
108  * this is done when the \n is eaten
109  */
110
111#[^\n]*         /* Eat this! */
112
113 /* Some keywords at the beginning of lines */
114
115<INITIAL>"label" {
116                  sensors_yylval.line = sensors_yylineno;
117                  BEGIN(MIDDLE);
118                  return LABEL;
119                }
120
121<INITIAL>"set"  {
122                  sensors_yylval.line = sensors_yylineno;
123                  BEGIN(MIDDLE);
124                  return SET;
125                }
126
127<INITIAL>"compute" {
128                  sensors_yylval.line = sensors_yylineno;
129                  BEGIN(MIDDLE);
130                  return COMPUTE;
131                }
132
133<INITIAL>"bus"  {
134                  sensors_yylval.line = sensors_yylineno;
135                  BEGIN(MIDDLE);
136                  return BUS;
137                }
138
139<INITIAL>"chip" {
140                  sensors_yylval.line = sensors_yylineno;
141                  BEGIN(MIDDLE);
142                  return CHIP;
143                }
144<INITIAL>"ignore" {
145                  sensors_yylval.line = sensors_yylineno;
146                  BEGIN(MIDDLE);
147                  return IGNORE;
148                }
149
150 /* Anything else at the beginning of a line is an error */
151
152<INITIAL>.      {
153                  yymore();
154                  BEGIN(ERR);
155                }
156
157<ERR>[^\n]*\n   {
158                  BEGIN(INITIAL);
159                  strcpy(sensors_lex_error,"Invalid keyword");
160                  return ERROR;
161                }
162
163 /* A number */
164
165<MIDDLE>{FLOAT} {
166                  sensors_yylval.value = atof(sensors_yytext);
167                  return FLOAT;
168                }
169
170 /* Some operators */
171
172<MIDDLE>"+"     return '+';
173<MIDDLE>"-"     return '-';
174<MIDDLE>"*"     return '*';
175<MIDDLE>"/"     return '/';
176<MIDDLE>"("     return '(';
177<MIDDLE>")"     return ')';
178<MIDDLE>","     return ',';
179<MIDDLE>"@"     return '@';
180<MIDDLE>"^"     return '^';
181<MIDDLE>"`"     return '`';
182
183 /* Quoted string */
184
185<MIDDLE>\"      {
186                  buffer_malloc();
187                  BEGIN(STRING);
188                }
189
190 /* Oops, newline while in a string is not good */
191
192<STRING>\n      |
193<STRING>\\\n    {
194                  buffer_add_char("\0");
195                  strcpy(sensors_lex_error,"No matching double quote");
196                  buffer_free();
197                  BEGIN(INITIAL);
198                  return ERROR;
199                }
200
201 /* At the end */
202
203<STRING>\"      {
204                  buffer_add_char("\0");
205                  sensors_yylval.name = strdup(buffer);
206                  if (! sensors_yylval.name)
207                    sensors_fatal_error("conf-lex.l",
208                                        "Allocating a new string");
209                  buffer_free();
210                  BEGIN(MIDDLE);
211                  return NAME;
212                }
213
214<STRING>\\a     {
215                  buffer_add_char("\a");
216                }
217
218<STRING>\\b     {
219                  buffer_add_char("\b");
220                }
221
222<STRING>\\f     {
223                  buffer_add_char("\f");
224                }
225
226<STRING>\\n     {
227                  buffer_add_char("\n");
228                }
229
230<STRING>\\r     {
231                  buffer_add_char("\r");
232                }
233
234<STRING>\\t     {
235                  buffer_add_char("\t");
236                }
237
238<STRING>\\v     {
239                  buffer_add_char("\v");
240                }
241
242 /* We can't support \0, this would cause havoc! */
243
244<STRING>\\{OCTESC} {
245                  int res;
246                  sscanf(sensors_yytext+1,"%o",&res);
247                  buffer_add_char(&res);
248                }
249
250 /* Other escapes: just copy the character behind the slash */
251
252<STRING>\\.     {
253                  buffer_add_char(&sensors_yytext[1]);
254                }
255
256 /* Anything else */
257
258<STRING>[^\\\n\"]+ {
259                  buffer_add_string(sensors_yytext);
260                }
261
262 /* A normal, unquoted identifier */
263
264<MIDDLE>{IDCHAR}+ {
265                  sensors_yylval.name = strdup(sensors_yytext);
266                  if (! sensors_yylval.name)
267                    sensors_fatal_error("conf-lex.l",
268                                        "Allocating a new string");
269                 
270                  return NAME;
271                }
272
273%%
274
275/*
276        Do the buffer handling manually.  This allows us to scan as many
277        config files as we need to, while cleaning up properly after each
278        one.  The "BEGIN(0)" line ensures that we start in the default state,
279        even if e.g. the previous config file was syntactically broken.
280
281        Returns 0 if successful, !0 otherwise.
282*/
283
284static YY_BUFFER_STATE scan_buf = (YY_BUFFER_STATE)0;
285
286int sensors_scanner_init(FILE *input)
287{
288        BEGIN(0);
289        if (!(scan_buf = sensors_yy_create_buffer(input, YY_BUF_SIZE)))
290                return -1;
291
292        sensors_yy_switch_to_buffer(scan_buf);
293        return 0;
294}
295
296void sensors_scanner_exit(void)
297{
298        sensors_yy_delete_buffer(scan_buf);
299        scan_buf = (YY_BUFFER_STATE)0;
300}
301
Note: See TracBrowser for help on using the browser.