| 1 | /* |
|---|
| 2 | init.c - Part of libsensors, a Linux library for reading sensor data. |
|---|
| 3 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <stdlib.h> |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include "sensors.h" |
|---|
| 23 | #include "data.h" |
|---|
| 24 | #include "error.h" |
|---|
| 25 | #include "access.h" |
|---|
| 26 | #include "conf.h" |
|---|
| 27 | #include "sysfs.h" |
|---|
| 28 | #include "scanner.h" |
|---|
| 29 | |
|---|
| 30 | static void free_chip_name(sensors_chip_name *name); |
|---|
| 31 | static void free_chip_features(sensors_chip_feature *features); |
|---|
| 32 | static void free_bus(sensors_bus *bus); |
|---|
| 33 | static void free_chip(sensors_chip *chip); |
|---|
| 34 | static void free_label(sensors_label *label); |
|---|
| 35 | static void free_set(sensors_set *set); |
|---|
| 36 | static void free_compute(sensors_compute *compute); |
|---|
| 37 | static void free_ignore(sensors_ignore *ignore); |
|---|
| 38 | static void free_expr(sensors_expr *expr); |
|---|
| 39 | |
|---|
| 40 | int sensors_init(FILE *input) |
|---|
| 41 | { |
|---|
| 42 | int res; |
|---|
| 43 | sensors_cleanup(); |
|---|
| 44 | if (!sensors_init_sysfs()) |
|---|
| 45 | return -SENSORS_ERR_PROC; |
|---|
| 46 | if ((res = sensors_read_sysfs_bus()) || (res = sensors_read_sysfs_chips())) |
|---|
| 47 | return res; |
|---|
| 48 | if ((res = sensors_scanner_init(input))) |
|---|
| 49 | return -SENSORS_ERR_PARSE; |
|---|
| 50 | if ((res = sensors_yyparse())) |
|---|
| 51 | return -SENSORS_ERR_PARSE; |
|---|
| 52 | if ((res = sensors_substitute_busses())) |
|---|
| 53 | return res; |
|---|
| 54 | return 0; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void sensors_cleanup(void) |
|---|
| 58 | { |
|---|
| 59 | int i; |
|---|
| 60 | |
|---|
| 61 | sensors_scanner_exit(); |
|---|
| 62 | |
|---|
| 63 | for (i = 0; i < sensors_proc_chips_count; i++) { |
|---|
| 64 | free_chip_name(&sensors_proc_chips[i].chip); |
|---|
| 65 | free_chip_features(sensors_proc_chips[i].feature); |
|---|
| 66 | } |
|---|
| 67 | free(sensors_proc_chips); |
|---|
| 68 | sensors_proc_chips = NULL; |
|---|
| 69 | sensors_proc_chips_count = sensors_proc_chips_max = 0; |
|---|
| 70 | |
|---|
| 71 | for (i = 0; i < sensors_config_busses_count; i++) |
|---|
| 72 | free_bus(&sensors_config_busses[i]); |
|---|
| 73 | free(sensors_config_busses); |
|---|
| 74 | sensors_config_busses = NULL; |
|---|
| 75 | sensors_config_busses_count = sensors_config_busses_max = 0; |
|---|
| 76 | |
|---|
| 77 | for (i = 0; i < sensors_config_chips_count; i++) |
|---|
| 78 | free_chip(&sensors_config_chips[i]); |
|---|
| 79 | free(sensors_config_chips); |
|---|
| 80 | sensors_config_chips = NULL; |
|---|
| 81 | sensors_config_chips_count = sensors_config_chips_max = 0; |
|---|
| 82 | |
|---|
| 83 | for (i = 0; i < sensors_proc_bus_count; i++) |
|---|
| 84 | free_bus(&sensors_proc_bus[i]); |
|---|
| 85 | free(sensors_proc_bus); |
|---|
| 86 | sensors_proc_bus = NULL; |
|---|
| 87 | sensors_proc_bus_count = sensors_proc_bus_max = 0; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | void free_chip_name(sensors_chip_name *name) |
|---|
| 91 | { |
|---|
| 92 | free(name->prefix); |
|---|
| 93 | free(name->path); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void free_chip_features(sensors_chip_feature *features) |
|---|
| 97 | { |
|---|
| 98 | int i; |
|---|
| 99 | |
|---|
| 100 | for (i = 0; features[i].data.name; i++) |
|---|
| 101 | free(features[i].data.name); |
|---|
| 102 | free(features); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | void free_bus(sensors_bus *bus) |
|---|
| 106 | { |
|---|
| 107 | free(bus->adapter); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void free_chip(sensors_chip *chip) |
|---|
| 111 | { |
|---|
| 112 | int i; |
|---|
| 113 | |
|---|
| 114 | for (i = 0; i < chip->chips.fits_count; i++) |
|---|
| 115 | free_chip_name(&chip->chips.fits[i]); |
|---|
| 116 | free(chip->chips.fits); |
|---|
| 117 | chip->chips.fits_count = chip->chips.fits_max = 0; |
|---|
| 118 | |
|---|
| 119 | for (i = 0; i < chip->labels_count; i++) |
|---|
| 120 | free_label(&chip->labels[i]); |
|---|
| 121 | free(chip->labels); |
|---|
| 122 | chip->labels_count = chip->labels_max = 0; |
|---|
| 123 | |
|---|
| 124 | for (i = 0; i < chip->sets_count; i++) |
|---|
| 125 | free_set(&chip->sets[i]); |
|---|
| 126 | free(chip->sets); |
|---|
| 127 | chip->sets_count = chip->sets_max = 0; |
|---|
| 128 | |
|---|
| 129 | for (i = 0; i < chip->computes_count; i++) |
|---|
| 130 | free_compute(&chip->computes[i]); |
|---|
| 131 | free(chip->computes); |
|---|
| 132 | chip->computes_count = chip->computes_max = 0; |
|---|
| 133 | |
|---|
| 134 | for (i = 0; i < chip->ignores_count; i++) |
|---|
| 135 | free_ignore(&chip->ignores[i]); |
|---|
| 136 | free(chip->ignores); |
|---|
| 137 | chip->ignores_count = chip->ignores_max = 0; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void free_label(sensors_label *label) |
|---|
| 141 | { |
|---|
| 142 | free(label->name); |
|---|
| 143 | free(label->value); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void free_set(sensors_set *set) |
|---|
| 147 | { |
|---|
| 148 | free(set->name); |
|---|
| 149 | free_expr(set->value); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | void free_compute(sensors_compute *compute) |
|---|
| 153 | { |
|---|
| 154 | free(compute->name); |
|---|
| 155 | free_expr(compute->from_proc); |
|---|
| 156 | free_expr(compute->to_proc); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | void free_ignore(sensors_ignore *ignore) |
|---|
| 160 | { |
|---|
| 161 | free(ignore->name); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | void free_expr(sensors_expr *expr) |
|---|
| 165 | { |
|---|
| 166 | if ((expr->kind) == sensors_kind_var) |
|---|
| 167 | free(expr->data.var); |
|---|
| 168 | else if ((expr->kind) == sensors_kind_sub) { |
|---|
| 169 | if (expr->data.subexpr.sub1) |
|---|
| 170 | free_expr(expr->data.subexpr.sub1); |
|---|
| 171 | if (expr->data.subexpr.sub2) |
|---|
| 172 | free_expr(expr->data.subexpr.sub2); |
|---|
| 173 | } |
|---|
| 174 | free(expr); |
|---|
| 175 | } |
|---|