summaryrefslogtreecommitdiff
path: root/lib/conf-parse.y
blob: 1937f544dc66feeab25c6447efba4d8cde7825f6 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
%{
/*
    conf-parse.y - Part of libsensors, a Linux library for reading sensor data.
    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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 Lesser 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., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA.
*/

#define YYERROR_VERBOSE

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

#include "data.h"
#include "general.h"
#include "error.h"
#include "conf.h"
#include "access.h"
#include "init.h"

static void sensors_yyerror(const char *err);
static sensors_expr *malloc_expr(void);

static sensors_chip *current_chip = NULL;

#define bus_add_el(el) sensors_add_array_el(el,\
                                      &sensors_config_busses,\
                                      &sensors_config_busses_count,\
                                      &sensors_config_busses_max,\
                                      sizeof(sensors_bus))
#define label_add_el(el) sensors_add_array_el(el,\
                                        &current_chip->labels,\
                                        &current_chip->labels_count,\
                                        &current_chip->labels_max,\
                                        sizeof(sensors_label));
#define set_add_el(el) sensors_add_array_el(el,\
                                      &current_chip->sets,\
                                      &current_chip->sets_count,\
                                      &current_chip->sets_max,\
                                      sizeof(sensors_set));
#define compute_add_el(el) sensors_add_array_el(el,\
                                          &current_chip->computes,\
                                          &current_chip->computes_count,\
                                          &current_chip->computes_max,\
                                          sizeof(sensors_compute));
#define ignore_add_el(el) sensors_add_array_el(el,\
                                          &current_chip->ignores,\
                                          &current_chip->ignores_count,\
                                          &current_chip->ignores_max,\
                                          sizeof(sensors_ignore));
#define chip_add_el(el) sensors_add_array_el(el,\
                                       &sensors_config_chips,\
                                       &sensors_config_chips_count,\
                                       &sensors_config_chips_max,\
                                       sizeof(sensors_chip));

#define fits_add_el(el,list) sensors_add_array_el(el,\
                                                  &(list).fits,\
                                                  &(list).fits_count,\
                                                  &(list).fits_max, \
		                                  sizeof(sensors_chip_name));

%}

%union {
  double value;
  char *name;
  void *nothing;
  sensors_chip_name_list chips;
  sensors_expr *expr;
  sensors_bus_id bus;
  sensors_chip_name chip;
  sensors_config_line line;
}  

%left <nothing> '-' '+'
%left <nothing> '*' '/'
%left <nothing> NEG
%right <nothing> '^' '`'

%token <nothing> ','
%token <nothing> EOL
%token <line> BUS
%token <line> LABEL
%token <line> SET
%token <line> CHIP
%token <line> COMPUTE
%token <line> IGNORE
%token <value> FLOAT
%token <name> NAME
%token <nothing> ERROR

%type <chips> chip_name_list
%type <expr> expression
%type <bus> bus_id
%type <name> adapter_name
%type <name> function_name
%type <name> string
%type <chip> chip_name

%start input

%%

input:	  /* empty */
	| input line
;

line:	  bus_statement EOL
	| label_statement EOL
	| set_statement EOL
	| chip_statement EOL
	| compute_statement EOL
	| ignore_statement EOL
	| error	EOL
;

bus_statement:	  BUS bus_id adapter_name
		  { sensors_bus new_el;
		    new_el.line = $1;
		    new_el.bus = $2;
                    new_el.adapter = $3;
		    bus_add_el(&new_el);
		  }
;

label_statement:	  LABEL function_name string
			  { sensors_label new_el;
			    if (!current_chip) {
			      sensors_yyerror("Label statement before first chip statement");
			      free($2);
			      free($3);
			      YYERROR;
			    }
			    new_el.line = $1;
			    new_el.name = $2;
			    new_el.value = $3;
			    label_add_el(&new_el);
			  }
;

set_statement:	  SET function_name expression
		  { sensors_set new_el;
		    if (!current_chip) {
		      sensors_yyerror("Set statement before first chip statement");
		      free($2);
		      sensors_free_expr($3);
		      YYERROR;
		    }
		    new_el.line = $1;
		    new_el.name = $2;
		    new_el.value = $3;
		    set_add_el(&new_el);
		  }
;

compute_statement:	  COMPUTE function_name expression ',' expression
			  { sensors_compute new_el;
			    if (!current_chip) {
			      sensors_yyerror("Compute statement before first chip statement");
			      free($2);
			      sensors_free_expr($3);
			      sensors_free_expr($5);
			      YYERROR;
			    }
			    new_el.line = $1;
			    new_el.name = $2;
			    new_el.from_proc = $3;
			    new_el.to_proc = $5;
			    compute_add_el(&new_el);
			  }
;

ignore_statement:	IGNORE function_name
			{ sensors_ignore new_el;
			  if (!current_chip) {
			    sensors_yyerror("Ignore statement before first chip statement");
			    free($2);
			    YYERROR;
			  }
			  new_el.line = $1;
			  new_el.name = $2;
			  ignore_add_el(&new_el);
			}
;

chip_statement:	  CHIP chip_name_list
		  { sensors_chip new_el;
		    new_el.line = $1;
		    new_el.labels = NULL;
		    new_el.sets = NULL;
		    new_el.computes = NULL;
		    new_el.ignores = NULL;
		    new_el.labels_count = new_el.labels_max = 0;
		    new_el.sets_count = new_el.sets_max = 0;
		    new_el.computes_count = new_el.computes_max = 0;
		    new_el.ignores_count = new_el.ignores_max = 0;
		    new_el.chips = $2;
		    chip_add_el(&new_el);
		    current_chip = sensors_config_chips + 
		                   sensors_config_chips_count - 1;
		  }
;

chip_name_list:	  chip_name
		  { 
		    $$.fits = NULL;
		    $$.fits_count = $$.fits_max = 0;
		    fits_add_el(&$1,$$);
		  }
		| chip_name_list chip_name
		  { $$ = $1;
		    fits_add_el(&$2,$$);
		  }
;
	
expression:	  FLOAT	
		  { $$ = malloc_expr(); 
		    $$->data.val = $1; 
		    $$->kind = sensors_kind_val;
		  }
		| NAME
		  { $$ = malloc_expr(); 
		    $$->data.var = $1;
		    $$->kind = sensors_kind_var;
		  }
		| '@'
		  { $$ = malloc_expr();
		    $$->kind = sensors_kind_source;
		  }
		| expression '+' expression
		  { $$ = malloc_expr(); 
		    $$->kind = sensors_kind_sub;
		    $$->data.subexpr.op = sensors_add;
		    $$->data.subexpr.sub1 = $1;
		    $$->data.subexpr.sub2 = $3;
		  }
		| expression '-' expression
		  { $$ = malloc_expr(); 
		    $$->kind = sensors_kind_sub;
		    $$->data.subexpr.op = sensors_sub;
		    $$->data.subexpr.sub1 = $1;
		    $$->data.subexpr.sub2 = $3;
		  }
		| expression '*' expression
		  { $$ = malloc_expr(); 
		    $$->kind = sensors_kind_sub;
		    $$->data.subexpr.op = sensors_multiply;
		    $$->data.subexpr.sub1 = $1;
		    $$->data.subexpr.sub2 = $3;
		  }
		| expression '/' expression
		  { $$ = malloc_expr(); 
		    $$->kind = sensors_kind_sub;
		    $$->data.subexpr.op = sensors_divide;
		    $$->data.subexpr.sub1 = $1;
		    $$->data.subexpr.sub2 = $3;
		  }
		| '-' expression  %prec NEG
		  { $$ = malloc_expr(); 
		    $$->kind = sensors_kind_sub;
		    $$->data.subexpr.op = sensors_negate;
		    $$->data.subexpr.sub1 = $2;
		    $$->data.subexpr.sub2 = NULL;
		  }
		| '(' expression ')'
		  { $$ = $2; }
		| '^' expression
		  { $$ = malloc_expr(); 
		    $$->kind = sensors_kind_sub;
		    $$->data.subexpr.op = sensors_exp;
		    $$->data.subexpr.sub1 = $2;
		    $$->data.subexpr.sub2 = NULL;
		  }
		| '`' expression
		  { $$ = malloc_expr(); 
		    $$->kind = sensors_kind_sub;
		    $$->data.subexpr.op = sensors_log;
		    $$->data.subexpr.sub1 = $2;
		    $$->data.subexpr.sub2 = NULL;
		  }
;

bus_id:		  NAME
		  { int res = sensors_parse_bus_id($1,&$$);
		    free($1);
		    if (res) {
                      sensors_yyerror("Parse error in bus id");
		      YYERROR;
                    }
		  }
;

adapter_name:	  NAME
		  { $$ = $1; }
;

function_name:	  NAME
		  { $$ = $1; }
;

string:	  NAME
	  { $$ = $1; }
;

chip_name:	  NAME
		  { int res = sensors_parse_chip_name($1,&$$); 
		    free($1);
		    if (res) {
		      sensors_yyerror("Parse error in chip name");
		      YYERROR;
		    }
		  }
;

%%

void sensors_yyerror(const char *err)
{
  if (sensors_lex_error[0]) {
    sensors_parse_error_wfn(sensors_lex_error, sensors_yyfilename, sensors_yylineno);
    sensors_lex_error[0] = '\0';
  } else
    sensors_parse_error_wfn(err, sensors_yyfilename, sensors_yylineno);
}

sensors_expr *malloc_expr(void)
{
  sensors_expr *res = malloc(sizeof(sensors_expr));
  if (! res)
    sensors_fatal_error(__func__, "Allocating a new expression");
  return res;
}