File: conf-lex.l

package info (click to toggle)
lm-sensors 1%3A2.10.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,104 kB
  • ctags: 10,474
  • sloc: ansic: 61,469; perl: 7,544; sh: 1,491; makefile: 400; lex: 300; yacc: 291
file content (300 lines) | stat: -rw-r--r-- 6,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
%{
/*
    conf-lex.l - Part of libsensors, a Linux library for reading sensor data.
    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> 

    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.
*/

#include <stdlib.h>
#include <string.h>

#include "general.h"
#include "data.h"
#include "conf-parse.h"
#include "error.h"
#include "scanner.h"

static int buffer_count;
static int buffer_max;
static char *buffer;

char sensors_lex_error[100];

#define buffer_malloc() sensors_malloc_array(&buffer,&buffer_count,\
                                             &buffer_max,1)
#define buffer_free() sensors_free_array(&buffer,&buffer_count,\
                                         &buffer_max)
#define buffer_add_char(c) sensors_add_array_el(c,&buffer,\
                                                &buffer_count,\
                                                &buffer_max,1)
#define buffer_add_string(s) sensors_add_array_els(s,strlen(s),\
                                                   &buffer, \
                                                   &buffer_count,&buffer_max,1)

%}

/* Scanner for configuration files */

%option nodefault
%option noyywrap
%option yylineno
%option nounput

/* States. 'Normal' states STRING and MIDDLE share some rules; other states
   have only their own rules */
%s MIDDLE
%x STRING
%x ERR

/* Any whitespace-like character */
BLANK		[[:space:]]

IDCHAR		[[:alnum:]_]

/* Note: `10', `10.4' and `.4' are valid, `10.' is not */
FLOAT   [[:digit:]]*\.?[[:digit:]]+

/* Only positive whole numbers are recognized here */
NUM	0|([1-9][[:digit:]]*)

/* Only number between 1 and 255, octally represented. */
OCTESC		(1[0-7]{0,2})|([2-7][0-7]?)|(0[1-7][0-7]?)|(00[1-7])


%%


 /* End of line: It may be the end of this line. Same for End of file. */
<MIDDLE>\n	|
<MIDDLE><<EOF>> {
		  BEGIN(INITIAL);
		  return EOL;
		}

 /* We want to match any blank, except End of line; that is why we have to
    match whitespace one by one! */
{BLANK}		/* Eat up a blank */

 /* Escaped End of line: eat and be happy */
<MIDDLE>\\\n	/* Eat this! */

 /* Remove a comment; we do not change the state, this is done when the \n is
    eaten */
#[^\n]*		/* Eat this! */

 /* Some keywords at the beginning of lines */
<INITIAL>"label" {
		  sensors_yylval.line = sensors_yylineno;
		  BEGIN(MIDDLE);
		  return LABEL;
		}

<INITIAL>"set"	{
		  sensors_yylval.line = sensors_yylineno;
		  BEGIN(MIDDLE);
		  return SET;
		}

<INITIAL>"compute" {
		  sensors_yylval.line = sensors_yylineno;
		  BEGIN(MIDDLE);
		  return COMPUTE;
		}

<INITIAL>"bus"	{
		  sensors_yylval.line = sensors_yylineno;
		  BEGIN(MIDDLE);
		  return BUS;
		}

<INITIAL>"chip"	{
		  sensors_yylval.line = sensors_yylineno;
		  BEGIN(MIDDLE);
		  return CHIP;
		}
<INITIAL>"ignore" {
		  sensors_yylval.line = sensors_yylineno;
		  BEGIN(MIDDLE);
		  return IGNORE;
		}

 /* Anything else at the beginning of a line is an error */
<INITIAL>.	{
		  yymore();
		  BEGIN(ERR);
		}

<ERR>[^\n]*\n	{
		  BEGIN(INITIAL);
		  strcpy(sensors_lex_error,"Invalid keyword");
		  return ERROR;
		}

 /* A number */
<MIDDLE>{FLOAT}	{
		  sensors_yylval.value = atof(sensors_yytext);
		  return FLOAT;
		}

 /* Some operators */
<MIDDLE>"+"	{
		  return '+';
		}

<MIDDLE>"-"	{
		  return '-';
		}

<MIDDLE>"*"	{
		  return '*';
		}

<MIDDLE>"/"	{
		  return '/';
		}

<MIDDLE>"("	{
		  return '(';
		}

<MIDDLE>")"	{
		  return ')';
		}
<MIDDLE>","	{
		  return ',';
		}
<MIDDLE>"@"	{
		  return '@';
		}
<MIDDLE>"^"	{
		  return '^';
		}
<MIDDLE>"`"	{
		  return '`';
		}

 /* Quoted string */
<MIDDLE>\"	{
		  buffer_malloc();
		  BEGIN(STRING);
		}

 /* Oops, newline while in a string is not good */
<STRING>\n	|
<STRING>\\\n	{
		  buffer_add_char("\0");
		  strcpy(sensors_lex_error,"No matching double quote");
		  buffer_free();
		  BEGIN(INITIAL);
		  return ERROR;
		}

 /* At the end */
<STRING>\"	{
		  buffer_add_char("\0");
		  sensors_yylval.name = strdup(buffer);
		  if (! sensors_yylval.name)
		    sensors_fatal_error("conf-lex.l",
                                        "Allocating a new string");
		  buffer_free();
		  BEGIN(MIDDLE);
		  return NAME;
		}

<STRING>\\a	{
		  buffer_add_char("\a");
		}

<STRING>\\b	{
		  buffer_add_char("\b");
		}

<STRING>\\f	{
		  buffer_add_char("\f");
		}

<STRING>\\n	{
		  buffer_add_char("\n");
		}

<STRING>\\r	{
		  buffer_add_char("\r");
		}

<STRING>\\t	{
		  buffer_add_char("\t");
		}

<STRING>\\v	{
		  buffer_add_char("\v");
		}

 /* We can't support \0, this would cause havoc! */
<STRING>\\{OCTESC} {
		  int res;
		  sscanf(sensors_yytext+1,"%o",&res);
		  buffer_add_char(&res);
		}

 /* Other escapes: just copy the character behind the slash */
<STRING>\\.	{
		  buffer_add_char(&sensors_yytext[1]);
		}

 /* Anything else */
<STRING>[^\\\n\"]+ {
		  buffer_add_string(sensors_yytext);
		}

 /* A normal, unquoted identifier */
<MIDDLE>{IDCHAR}+ {
		  sensors_yylval.name = strdup(sensors_yytext);
		  if (! sensors_yylval.name)
		    sensors_fatal_error("conf-lex.l",
                                        "Allocating a new string");
		  
		  return NAME;
		}

%%

/*
	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)
{
	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;
}