| 1 | /* |
| 2 | * config.c - Fan control daemon for MacBook |
| 3 | * |
| 4 | * Copyright (C) 2010 Mikael Strom <[email protected]> |
| 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 3 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 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
| 21 | #include "config.h"cxx . . $ `<file>/config.h`/ |
| 22 | |
| 23 | //----------------------------------------------------------------------------- |
| 24 | |
| 25 | static FILEcxx . . $ FILE# *fpcxx . . $ fp.; |
| 26 | |
| 27 | float temp_avg_floorcxx . . $ temp_avg_floor. = 40; // default values if no config file is found |
| 28 | float temp_avg_ceilingcxx . . $ temp_avg_ceiling. = 50; |
| 29 | |
| 30 | float temp_TC0P_floorcxx . . $ temp_TC0P_floor. = 40; |
| 31 | float temp_TC0P_ceilingcxx . . $ temp_TC0P_ceiling. = 50; |
| 32 | |
| 33 | float temp_TG0P_floorcxx . . $ temp_TG0P_floor. = 40; |
| 34 | float temp_TG0P_ceilingcxx . . $ temp_TG0P_ceiling. = 50; |
| 35 | |
| 36 | float fan_mincxx . . $ fan_min. = 0; |
| 37 | float fan_maxcxx . . $ fan_max. = 6200; // fixed max value |
| 38 | |
| 39 | int log_levelcxx . . $ log_level. = 0; |
| 40 | |
| 41 | int excludecxx . . $ exclude.[MAX_EXCLUDEcxx . . $ `config.h:36:9`!]; // array of sensors to exclude |
| 42 | |
| 43 | //----------------------------------------------------------------------------- |
| 44 | |
| 45 | int matchcxx . . $ match(e99f026884c896d5).(char* name, char* buf) |
| 46 | { |
| 47 | char* start = buf; |
| 48 | char* end = buf; |
| 49 | |
| 50 | if(strlencxx . . $ strlen(751346f8406fd082).(buf) < 1) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | // skip preceeding ws |
| 56 | while(*start && isblankcxx . . $ `<external>/17904084882347303253/ctype.h:201:12`!(*start)) |
| 57 | { |
| 58 | ++start; |
| 59 | } |
| 60 | |
| 61 | // skip to end |
| 62 | while(*end) |
| 63 | { |
| 64 | ++end; |
| 65 | } |
| 66 | |
| 67 | // delete ws backwards from end |
| 68 | while(end > start && isblankcxx . . $ `<external>/17904084882347303253/ctype.h:201:12`!(*end)) |
| 69 | { |
| 70 | *end = 0; |
| 71 | --end; |
| 72 | } |
| 73 | |
| 74 | // compare strings |
| 75 | |
| 76 | return strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, start) == 0; |
| 77 | } |
| 78 | |
| 79 | //----------------------------------------------------------------------------- |
| 80 | |
| 81 | int get_valcxx . . $ get_val(1ef8e6ec083aabb9).(char *buf) |
| 82 | { |
| 83 | while(isblankcxx . . $ `<external>/17904084882347303253/ctype.h:201:12`!(*buf)) |
| 84 | { |
| 85 | ++buf; |
| 86 | } |
| 87 | |
| 88 | if(! *buf) |
| 89 | { |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | return atoicxx . . $ atoi(5c4306d8553b3696).(buf); |
| 94 | } |
| 95 | |
| 96 | //----------------------------------------------------------------------------- |
| 97 | // format is: name : integer |
| 98 | |
| 99 | int read_paramcxx . . $ read_param(cd0c04d3624ba1ca).(char* name, int min_val, int max_val, int def) |
| 100 | { |
| 101 | fseekcxx . . $ fseek(543fae30d249f6b1).(fpcxx . . $ fp., 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 102 | |
| 103 | while(1) |
| 104 | { |
| 105 | char buf[64]; |
| 106 | char *s = fgetscxx . . $ fgets(efb0aaba7ac866ae).(buf, sizeof(buf), fpcxx . . $ fp.); |
| 107 | |
| 108 | if(s == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 109 | { |
| 110 | break; // exit when no more to read |
| 111 | } |
| 112 | |
| 113 | if(buf[0] == '#' || buf[0] == '\n') |
| 114 | { |
| 115 | continue; // skip comments |
| 116 | } |
| 117 | |
| 118 | char *colon = strchrcxx . . $ strchr(2050848944e00d54).(buf, ':'); // find colon |
| 119 | if(colon == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 120 | { |
| 121 | printfcxx . . $ printf(581fddd72a8db8fc).("Ill formed line in config file: %s\n", buf); |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | *colon = 0; // terminate string at colon |
| 126 | |
| 127 | if(matchcxx . . $ match(e99f026884c896d5).(name, buf)) |
| 128 | { |
| 129 | int val = get_valcxx . . $ get_val(1ef8e6ec083aabb9).(colon + 1); // get value |
| 130 | |
| 131 | if(val < 0) |
| 132 | { |
| 133 | printfcxx . . $ printf(581fddd72a8db8fc).("Ill formed line in config file: %s\n", buf); |
| 134 | continue; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | val = mincxx . . $ `config.h:40:9`!(max_val, val);// clamp |
| 139 | val = maxcxx . . $ `config.h:39:9`!(min_val, val); |
| 140 | return val; // success |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return def; |
| 146 | } |
| 147 | |
| 148 | //----------------------------------------------------------------------------- |
| 149 | // format is: exclude : integer {integer} |
| 150 | |
| 151 | void read_exclude_listcxx . . $ read_exclude_list(d878ad36e69247c7).() |
| 152 | { |
| 153 | fseekcxx . . $ fseek(543fae30d249f6b1).(fpcxx . . $ fp., 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 154 | |
| 155 | while(1) |
| 156 | { |
| 157 | char buf[256]; |
| 158 | char *s = fgetscxx . . $ fgets(efb0aaba7ac866ae).(buf, sizeof(buf), fpcxx . . $ fp.); |
| 159 | |
| 160 | if(s == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 161 | { |
| 162 | break; // exit when no more to read |
| 163 | } |
| 164 | |
| 165 | if(buf[0] == '#' || buf[0] == '\n') |
| 166 | { |
| 167 | continue; // skip comments |
| 168 | } |
| 169 | |
| 170 | char *colon = strchrcxx . . $ strchr(2050848944e00d54).(buf, ':'); // find colon |
| 171 | if(colon == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 172 | { |
| 173 | printfcxx . . $ printf(581fddd72a8db8fc).("Ill formed line in config file: %s\n", buf); |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | *colon = 0; // terminate string at colon |
| 178 | |
| 179 | if(matchcxx . . $ match(e99f026884c896d5).("exclude", buf)) |
| 180 | { |
| 181 | int i; |
| 182 | char* values = colon + 1; |
| 183 | |
| 184 | // get values |
| 185 | |
| 186 | for(i = 0; i < MAX_EXCLUDEcxx . . $ `config.h:36:9`!; ++i) |
| 187 | { |
| 188 | while(isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*values) || *values == ',') |
| 189 | { |
| 190 | ++values; |
| 191 | } |
| 192 | |
| 193 | if(isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(*values)) |
| 194 | { |
| 195 | int val = get_valcxx . . $ get_val(1ef8e6ec083aabb9).(values); |
| 196 | |
| 197 | if(val < 0) |
| 198 | { |
| 199 | printfcxx . . $ printf(581fddd72a8db8fc).("Ill formed line in config file: %s\n", buf); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | excludecxx . . $ exclude.[i] = val; |
| 204 | |
| 205 | while(isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(*values)) |
| 206 | { |
| 207 | ++values; |
| 208 | } |
| 209 | } |
| 210 | else if(*values == 0) |
| 211 | { |
| 212 | return; // done |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | printfcxx . . $ printf(581fddd72a8db8fc).("Ill formed line in config file: %s\n", buf); |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | //----------------------------------------------------------------------------- |
| 225 | |
| 226 | void read_cfgcxx . . $ read_cfg(fda57c8427c60377).(char* name) |
| 227 | { |
| 228 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(excludecxx . . $ exclude., 0, sizeof(excludecxx . . $ exclude.)); |
| 229 | |
| 230 | fpcxx . . $ fp. = fopencxx . . $ fopen(82ec0787cd37e0a3).(name, "r"); |
| 231 | |
| 232 | if(fpcxx . . $ fp. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 233 | { |
| 234 | temp_avg_ceilingcxx . . $ temp_avg_ceiling. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("temp_avg_ceiling", 0, 90, 50); |
| 235 | temp_avg_floorcxx . . $ temp_avg_floor. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("temp_avg_floor", 0, temp_avg_ceilingcxx . . $ temp_avg_ceiling. - 1, 40); |
| 236 | |
| 237 | temp_TC0P_ceilingcxx . . $ temp_TC0P_ceiling. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("temp_TC0P_ceiling", 0, 90, 65); |
| 238 | temp_TC0P_floorcxx . . $ temp_TC0P_floor. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("temp_TC0P_floor", 0, temp_TC0P_ceilingcxx . . $ temp_TC0P_ceiling. - 1, 50); |
| 239 | |
| 240 | temp_TG0P_ceilingcxx . . $ temp_TG0P_ceiling. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("temp_TG0P_ceiling", 0, 90, 80); |
| 241 | temp_TG0P_floorcxx . . $ temp_TG0P_floor. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("temp_TG0P_floor", 0, temp_TG0P_ceilingcxx . . $ temp_TG0P_ceiling. - 1, 65); |
| 242 | |
| 243 | fan_mincxx . . $ fan_min. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("fan_min", 0, 6200, 0); |
| 244 | |
| 245 | log_levelcxx . . $ log_level. = read_paramcxx . . $ read_param(cd0c04d3624ba1ca).("log_level", 0, 2, 0); |
| 246 | |
| 247 | read_exclude_listcxx . . $ read_exclude_list(d878ad36e69247c7).(); |
| 248 | |
| 249 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(fpcxx . . $ fp.); |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | printfcxx . . $ printf(581fddd72a8db8fc).("Could not open config file %s\n", name); |
| 254 | } |
| 255 | |
| 256 | printfcxx . . $ printf(581fddd72a8db8fc).("Using parameters:\n"); |
| 257 | |
| 258 | printfcxx . . $ printf(581fddd72a8db8fc).("\ttemp_avg_floor: %.0f\n", temp_avg_floorcxx . . $ temp_avg_floor.); |
| 259 | printfcxx . . $ printf(581fddd72a8db8fc).("\ttemp_avg_ceiling: %.0f\n", temp_avg_ceilingcxx . . $ temp_avg_ceiling.); |
| 260 | |
| 261 | printfcxx . . $ printf(581fddd72a8db8fc).("\ttemp_TC0P_floor: %.0f\n", temp_TC0P_floorcxx . . $ temp_TC0P_floor.); |
| 262 | printfcxx . . $ printf(581fddd72a8db8fc).("\ttemp_TC0P_ceiling: %.0f\n", temp_TC0P_ceilingcxx . . $ temp_TC0P_ceiling.); |
| 263 | |
| 264 | printfcxx . . $ printf(581fddd72a8db8fc).("\ttemp_TG0P_floor: %.0f\n", temp_TG0P_floorcxx . . $ temp_TG0P_floor.); |
| 265 | printfcxx . . $ printf(581fddd72a8db8fc).("\ttemp_TG0P_ceiling: %.0f\n", temp_TG0P_ceilingcxx . . $ temp_TG0P_ceiling.); |
| 266 | |
| 267 | printfcxx . . $ printf(581fddd72a8db8fc).("\tfan_min: %.0f\n", fan_mincxx . . $ fan_min.); |
| 268 | |
| 269 | if(excludecxx . . $ exclude.[0] != 0) |
| 270 | { |
| 271 | int i; |
| 272 | |
| 273 | printfcxx . . $ printf(581fddd72a8db8fc).("\texclude: "); |
| 274 | |
| 275 | for(i = 0; i < MAX_EXCLUDEcxx . . $ `config.h:36:9`! && excludecxx . . $ exclude.[i] != 0; ++i) |
| 276 | { |
| 277 | printfcxx . . $ printf(581fddd72a8db8fc).("temp%d_input ", excludecxx . . $ exclude.[i]); |
| 278 | } |
| 279 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 280 | } |
| 281 | |
| 282 | printfcxx . . $ printf(581fddd72a8db8fc).("\tlog_level: %d\n", log_levelcxx . . $ log_level.); |
| 283 | } |
| 284 | |
| 285 | //----------------------------------------------------------------------------- |