root/lm-sensors/tags/V3-0-1/lib/conf-parse.y

Revision 4793, 9.2 kB (checked in by khali, 1 year ago)

Complete the previous memory leak fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 %{
2 /*
3     conf-parse.y - 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 #define YYERROR_VERBOSE
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "data.h"
28 #include "general.h"
29 #include "error.h"
30 #include "conf.h"
31 #include "access.h"
32 #include "init.h"
33
34 static void sensors_yyerror(const char *err);
35 static sensors_expr *malloc_expr(void);
36
37 static sensors_chip *current_chip = NULL;
38
39 #define bus_add_el(el) sensors_add_array_el(el,\
40                                       &sensors_config_busses,\
41                                       &sensors_config_busses_count,\
42                                       &sensors_config_busses_max,\
43                                       sizeof(sensors_bus))
44 #define label_add_el(el) sensors_add_array_el(el,\
45                                         &current_chip->labels,\
46                                         &current_chip->labels_count,\
47                                         &current_chip->labels_max,\
48                                         sizeof(sensors_label));
49 #define set_add_el(el) sensors_add_array_el(el,\
50                                       &current_chip->sets,\
51                                       &current_chip->sets_count,\
52                                       &current_chip->sets_max,\
53                                       sizeof(sensors_set));
54 #define compute_add_el(el) sensors_add_array_el(el,\
55                                           &current_chip->computes,\
56                                           &current_chip->computes_count,\
57                                           &current_chip->computes_max,\
58                                           sizeof(sensors_compute));
59 #define ignore_add_el(el) sensors_add_array_el(el,\
60                                           &current_chip->ignores,\
61                                           &current_chip->ignores_count,\
62                                           &current_chip->ignores_max,\
63                                           sizeof(sensors_ignore));
64 #define chip_add_el(el) sensors_add_array_el(el,\
65                                        &sensors_config_chips,\
66                                        &sensors_config_chips_count,\
67                                        &sensors_config_chips_max,\
68                                        sizeof(sensors_chip));
69
70 #define fits_add_el(el,list) sensors_add_array_el(el,\
71                                                   &(list).fits,\
72                                                   &(list).fits_count,\
73                                                   &(list).fits_max, \
74                                                   sizeof(sensors_chip_name));
75
76 %}
77
78 %union {
79   double value;
80   char *name;
81   void *nothing;
82   sensors_chip_name_list chips;
83   sensors_expr *expr;
84   sensors_bus_id bus;
85   sensors_chip_name chip;
86   int line;
87
88
89 %left <nothing> '-' '+'
90 %left <nothing> '*' '/'
91 %left <nothing> NEG
92 %right <nothing> '^' '`'
93
94 %token <nothing> ','
95 %token <nothing> EOL
96 %token <line> BUS
97 %token <line> LABEL
98 %token <line> SET
99 %token <line> CHIP
100 %token <line> COMPUTE
101 %token <line> IGNORE
102 %token <value> FLOAT
103 %token <name> NAME
104 %token <nothing> ERROR
105
106 %type <chips> chip_name_list
107 %type <expr> expression
108 %type <bus> bus_id
109 %type <name> adapter_name
110 %type <name> function_name
111 %type <name> string
112 %type <chip> chip_name
113
114 %start input
115
116 %%
117
118 input:    /* empty */
119         | input line
120 ;
121
122 line:     bus_statement EOL
123         | label_statement EOL
124         | set_statement EOL
125         | chip_statement EOL
126         | compute_statement EOL
127         | ignore_statement EOL
128         | error EOL
129 ;
130
131 bus_statement:    BUS bus_id adapter_name
132                   { sensors_bus new_el;
133                     new_el.lineno = $1;
134                     new_el.bus = $2;
135                     new_el.adapter = $3;
136                     bus_add_el(&new_el);
137                   }
138 ;
139
140 label_statement:          LABEL function_name string
141                           { sensors_label new_el;
142                             if (!current_chip) {
143                               sensors_yyerror("Label statement before first chip statement");
144                               free($2);
145                               free($3);
146                               YYERROR;
147                             }
148                             new_el.lineno = $1;
149                             new_el.name = $2;
150                             new_el.value = $3;
151                             label_add_el(&new_el);
152                           }
153 ;
154
155 set_statement:    SET function_name expression
156                   { sensors_set new_el;
157                     if (!current_chip) {
158                       sensors_yyerror("Set statement before first chip statement");
159                       free($2);
160                       free_expr($3);
161                       YYERROR;
162                     }
163                     new_el.lineno = $1;
164                     new_el.name = $2;
165                     new_el.value = $3;
166                     set_add_el(&new_el);
167                   }
168 ;
169
170 compute_statement:        COMPUTE function_name expression ',' expression
171                           { sensors_compute new_el;
172                             if (!current_chip) {
173                               sensors_yyerror("Compute statement before first chip statement");
174                               free($2);
175                               free_expr($3);
176                               free_expr($5);
177                               YYERROR;
178                             }
179                             new_el.lineno = $1;
180                             new_el.name = $2;
181                             new_el.from_proc = $3;
182                             new_el.to_proc = $5;
183                             compute_add_el(&new_el);
184                           }
185 ;
186
187 ignore_statement:       IGNORE function_name
188                         { sensors_ignore new_el;
189                           if (!current_chip) {
190                             sensors_yyerror("Ignore statement before first chip statement");
191                             free($2);
192                             YYERROR;
193                           }
194                           new_el.lineno = $1;
195                           new_el.name = $2;
196                           ignore_add_el(&new_el);
197                         }
198 ;
199
200 chip_statement:   CHIP chip_name_list
201                   { sensors_chip new_el;
202                     new_el.lineno = $1;
203                     new_el.labels = NULL;
204                     new_el.sets = NULL;
205                     new_el.computes = NULL;
206                     new_el.ignores = NULL;
207                     new_el.labels_count = new_el.labels_max = 0;
208                     new_el.sets_count = new_el.sets_max = 0;
209                     new_el.computes_count = new_el.computes_max = 0;
210                     new_el.ignores_count = new_el.ignores_max = 0;
211                     new_el.chips = $2;
212                     chip_add_el(&new_el);
213                     current_chip = sensors_config_chips +
214                                    sensors_config_chips_count - 1;
215                   }
216 ;
217
218 chip_name_list:   chip_name
219                   {
220                     $$.fits = NULL;
221                     $$.fits_count = $$.fits_max = 0;
222                     fits_add_el(&$1,$$);
223                   }
224                 | chip_name_list chip_name
225                   { $$ = $1;
226                     fits_add_el(&$2,$$);
227                   }
228 ;
229        
230 expression:       FLOAT
231                   { $$ = malloc_expr();
232                     $$->data.val = $1;
233                     $$->kind = sensors_kind_val;
234                   }
235                 | NAME
236                   { $$ = malloc_expr();
237                     $$->data.var = $1;
238                     $$->kind = sensors_kind_var;
239                   }
240                 | '@'
241                   { $$ = malloc_expr();
242                     $$->kind = sensors_kind_source;
243                   }
244                 | expression '+' expression
245                   { $$ = malloc_expr();
246                     $$->kind = sensors_kind_sub;
247                     $$->data.subexpr.op = sensors_add;
248                     $$->data.subexpr.sub1 = $1;
249                     $$->data.subexpr.sub2 = $3;
250                   }
251                 | expression '-' expression
252                   { $$ = malloc_expr();
253                     $$->kind = sensors_kind_sub;
254                     $$->data.subexpr.op = sensors_sub;
255                     $$->data.subexpr.sub1 = $1;
256                     $$->data.subexpr.sub2 = $3;
257                   }
258                 | expression '*' expression
259                   { $$ = malloc_expr();
260                     $$->kind = sensors_kind_sub;
261                     $$->data.subexpr.op = sensors_multiply;
262                     $$->data.subexpr.sub1 = $1;
263                     $$->data.subexpr.sub2 = $3;
264                   }
265                 | expression '/' expression
266                   { $$ = malloc_expr();
267                     $$->kind = sensors_kind_sub;
268                     $$->data.subexpr.op = sensors_divide;
269                     $$->data.subexpr.sub1 = $1;
270                     $$->data.subexpr.sub2 = $3;
271                   }
272                 | '-' expression  %prec NEG
273                   { $$ = malloc_expr();
274                     $$->kind = sensors_kind_sub;
275                     $$->data.subexpr.op = sensors_negate;
276                     $$->data.subexpr.sub1 = $2;
277                     $$->data.subexpr.sub2 = NULL;
278                   }
279                 | '(' expression ')'
280                   { $$ = $2; }
281                 | '^' expression
282                   { $$ = malloc_expr();
283                     $$->kind = sensors_kind_sub;
284                     $$->data.subexpr.op = sensors_exp;
285                     $$->data.subexpr.sub1 = $2;
286                     $$->data.subexpr.sub2 = NULL;
287                   }
288                 | '`' expression
289                   { $$ = malloc_expr();
290                     $$->kind = sensors_kind_sub;
291                     $$->data.subexpr.op = sensors_log;
292                     $$->data.subexpr.sub1 = $2;
293                     $$->data.subexpr.sub2 = NULL;
294                   }
295 ;
296
297 bus_id:           NAME
298                   { int res = sensors_parse_bus_id($1,&$$);
299                     free($1);
300                     if (res) {
301                       sensors_yyerror("Parse error in bus id");
302                       YYERROR;
303                     }
304                   }
305 ;
306
307 adapter_name:     NAME
308                   { $$ = $1; }
309 ;
310
311 function_name:    NAME
312                   { $$ = $1; }
313 ;
314
315 string:   NAME
316           { $$ = $1; }
317 ;
318
319 chip_name:        NAME
320                   { int res = sensors_parse_chip_name($1,&$$);
321                     free($1);
322                     if (res) {
323                       sensors_yyerror("Parse error in chip name");
324                       YYERROR;
325                     }
326                   }
327 ;
328
329 %%
330
331 void sensors_yyerror(const char *err)
332 {
333   if (sensors_lex_error[0]) {
334     sensors_parse_error(sensors_lex_error,sensors_yylineno);
335     sensors_lex_error[0] = '\0';
336   } else
337     sensors_parse_error(err,sensors_yylineno);
338 }
339
340 sensors_expr *malloc_expr(void)
341 {
342   sensors_expr *res = malloc(sizeof(sensors_expr));
343   if (! res)
344     sensors_fatal_error("malloc_expr","Allocating a new expression");
345   return res;
346 }
347  
Note: See TracBrowser for help on using the browser.