summaryrefslogtreecommitdiff
path: root/lib/data.c
blob: 8cd97348dc1cb7ff39c4144e55fd294def1c2e98 (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
/*
    data.c - Part of libsensors, a Linux library for reading sensor data.
    Copyright (c) 1998  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 "error.h"
#include "data.h"
#include "sensors.h"

sensors_chip *sensors_config_chips = NULL;
int sensors_config_chips_count = 0;
int sensors_config_chips_max = 0;

sensors_bus *sensors_config_busses = NULL;
int sensors_config_busses_count = 0;
int sensors_config_busses_max = 0;

static int sensors_substitute_chip(sensors_chip_name *name,int lineno);

/* Wow, this must be one of the ugliest functions I have ever written.
   The idea is that it parses a chip name. These are valid names:

     lm78-i2c-10-5e		*-i2c-10-5e
     lm78-i2c-10-*		*-i2c-10-*
     lm78-i2c-*-5e		*-i2c-*-5e
     lm78-i2c-*-*		*-i2c-*-*
     lm78-isa-10dd		*-isa-10dd
     lm78-isa-*			*-isa-*
     lm78-*			*-*
     				*
   Here 'lm78' can be any prefix. To complicate matters, such a prefix
   can also contain dashes (like lm78-j, for example!). 'i2c' and 'isa' are
   literal strings, just like all dashes '-' and wildcards '*'. '10' can
   be any decimal i2c bus number. '5e' can be any hexadecimal i2c device
   address, and '10dd' any hexadecimal isa address.

   If '*' is used in prefixes, together with dashes, ambigious parses are
   introduced. In that case, the prefix is kept as small as possible.

   The 'prefix' part in the result is freshly allocated. All old contents
   of res is overwritten. res itself is not allocated. In case of an error
   return (ie. != 0), res is undefined, but all allocations are undone.

   Don't tell me there are bugs in here, because I'll start screaming :-)
*/

int sensors_parse_chip_name(const char *orig_name, sensors_chip_name *res)
{
  char *part2, *part3, *part4;
  char *name = strdup(orig_name);
  int i;

  if (! name)
    sensors_fatal_error("sensors_parse_chip_name","Allocating new name");
  /* First split name in upto four pieces. */
  if ((part4 = strrchr(name,'-')))
    *part4++ = '\0';
  if ((part3 = strrchr(name,'-')))
    *part3++ = '\0';
  if ((part2 = strrchr(name,'-'))) 
    *part2++ = '\0';

  /* No dashes found? */
  if (! part4) {
    if (!strcmp(name,"*")) {
      res->prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
      res->bus = SENSORS_CHIP_NAME_BUS_ANY;
      res->addr = SENSORS_CHIP_NAME_ADDR_ANY;
      goto SUCCES;
    } else 
      goto ERROR;
  }

  /* At least one dash found. Now part4 is either '*', or an address */
  if (!strcmp(part4,"*"))
    res->addr = SENSORS_CHIP_NAME_ADDR_ANY;
  else {
    if ((strlen(part4) > 4) || (strlen(part4) == 0))
      goto ERROR;
    res->addr = 0;
    for (i = 0; ; i++) { 
      switch (part4[i]) {
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        res->addr = res->addr * 16 + part4[i] - '0';
        break;
      case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        res->addr = res->addr * 16 + part4[i] - 'a' + 10;
        break;
      case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        res->addr = res->addr * 16 + part4[i] - 'A' + 10;
        break;
      case 0:
        goto DONE;
      default:
        goto ERROR;
      }
    }
DONE:
  }

  /* OK. So let's look at part3. It must either be the number of the
     i2c bus (and then part2 *must* be "i2c"), or it must be "isa",
     or, if part4 was "*", it belongs to 'prefix'. Or no second dash
     was found at all, of course. */
  if (! part3) {
    if (res->addr == SENSORS_CHIP_NAME_ADDR_ANY) {
      res->bus = SENSORS_CHIP_NAME_BUS_ANY;
    } else
      goto ERROR;
  } else if (!strcmp(part3,"isa")) {
    res->bus = SENSORS_CHIP_NAME_BUS_ISA;
    if (part2)
      *(part2-1) = '-';
  } else if (part2 && !strcmp(part2,"i2c") && !strcmp(part3,"*"))
    res->bus = SENSORS_CHIP_NAME_BUS_ANY_I2C;
  else if (part2 && !strcmp(part2,"i2c")) {
    if ((strlen(part3) > 3) || (strlen(part3) == 0))
      goto ERROR;
    res->bus = 0;
    for (i = 0; ; i++) { 
      switch (part3[i]) {
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        res->bus = res->bus * 10 + part3[i] - '0';
        break;
      case 0:
        goto DONE2;
      default:
        goto ERROR;
      }
    }
DONE2:
  } else if (res->addr == SENSORS_CHIP_NAME_ADDR_ANY) {
    res->bus = SENSORS_CHIP_NAME_BUS_ANY;
    if (part2)
      *(part2-1) = '-';
    *(part3-1) = '-';
  } else
    goto ERROR;
    
  if (!strcmp(name,"*"))
    res->prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
  else if (! (res->prefix = strdup(name)))
    sensors_fatal_error("sensors_parse_chip_name","Allocating new name");
  goto SUCCES;

SUCCES:
  free(name);
  return 0;

ERROR:
  free(name);
  return -SENSORS_ERR_CHIP_NAME;
}

int sensors_parse_i2cbus_name(const char *name, int *res)
{
  int i;

  if (! strcmp(name,"isa")) {
    *res = SENSORS_CHIP_NAME_BUS_ISA;
    return 0;
  }
  if (strncmp(name,"i2c-",4)) 
    return -1;
  name += 4;
  if ((strlen(name) > 3) || (strlen(name) == 0))
    return -SENSORS_ERR_BUS_NAME;
  *res = 0;
  for (i = 0; ; i++) { 
    switch (name[i]) {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      *res = *res * 10 + name[i] - '0';
      break;
    case 0:
      return 0;
    default:
      return -SENSORS_ERR_BUS_NAME;
    }
  }
}


int sensors_eval_expr(sensors_expr *expr, double val, double *result)
{
  double res1,res2;
  int res;

  if (expr->kind == sensors_kind_val) {
    *result = expr->data.val;
    return 0;
  }
  if (expr->kind == sensors_kind_var) {
    /* We should check the kind of variable here! */
    *result = val;
    return 0;
  }
  if ((res = sensors_eval_expr(expr->data.subexpr.sub1,val,&res1)))
    return res;
  if (expr->data.subexpr.sub2 && 
      (res = sensors_eval_expr(expr->data.subexpr.sub2,val,&res2)))
    return res;
  switch(expr->data.subexpr.op) {
  case sensors_add:
    *result = res1 + res2;
    return 0;
  case sensors_sub:
    *result = res1 - res2;
    return 0;
  case sensors_multiply:
    *result = res1 * res2;
    return 0;
  case sensors_divide:
    if (res2 == 0.0)
      return -SENSORS_ERR_DIV_ZERO;
    *result = res1 / res2;
    return 0;
  case sensors_negate:
    *result = -res1;
    return 0;
  }
  return 0;
}

int sensors_substitute_chip(sensors_chip_name *name,int lineno)
{
  int i,j;
  for (i = 0; i < sensors_config_busses_count; i++)
    if (sensors_config_busses[i].number == name->bus)
      break;

  if (i == sensors_config_busses_count) {
    sensors_parse_error("Undeclared i2c bus referenced",lineno);
    name->bus = sensors_proc_bus_count;
    return -SENSORS_ERR_BUS_NAME;
  }

  for (j = 0; j < sensors_proc_bus_count; j++) {
    if (!strcmp(sensors_config_busses[i].adapter,
                sensors_proc_bus[j].adapter) &&
        !strcmp(sensors_config_busses[i].algorithm,
                sensors_proc_bus[j].algorithm)) 
      break;
  }

  /* Well, if we did not find anything, j - sensors_proc_bus_count; so if
     we set this chip's bus number to j, it will never be matched. Good. */
  name->bus = j;
  return 0;
}

      
int sensors_substitute_busses(void)
{
  int err,i,j,lineno;
  sensors_chip_name_list *chips;
  int res=0;
  
  for(i = 0; i < sensors_config_chips_count; i++) {
    lineno = sensors_config_chips[i].lineno;
    chips = &sensors_config_chips[i].chips;
    for(j = 0; j < chips->fits_count; j++)
      if ((chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ISA) &&
          (chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY) &&
          (chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY_I2C))
        if ((err = sensors_substitute_chip(chips->fits+j, lineno)))
          res = err;
  }
  return res;
}