1 /***************************************************************************** |
|
2 * Copyright (C) 2014 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
7 * |
|
8 * This is free software; you can redistribute it and/or modify it |
|
9 * under the terms of the GNU General Public License as published by the |
|
10 * Free Software Foundation; either version 2, or (at your option) any |
|
11 * later version. |
|
12 * |
|
13 * mbsePi-apps is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with EC-65K; see the file COPYING. If not, write to the Free |
|
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
21 *****************************************************************************/ |
|
22 |
|
23 #include "mash.h" |
|
24 #include "xutil.h" |
|
25 |
|
26 |
|
27 int debug = FALSE; |
|
28 static char *mypath; |
|
29 static char *k, *v; |
|
30 static int linecnt = 0; |
|
31 sys_config Config; /* System configuration */ |
|
32 |
|
33 |
|
34 |
|
35 static int getint(char **); |
|
36 static int getw1(char **); |
|
37 |
|
38 |
|
39 #define XSTR(x) #x |
|
40 #define STR(x) XSTR(x) |
|
41 |
|
42 /* |
|
43 * System configuration table |
|
44 */ |
|
45 key_list keytab[] = { |
|
46 {(char *)"w1therm", getw1, (char **)&Config.w1therms}, |
|
47 {(char *)"lcd_cols", getint, (char **)&Config.lcd_cols}, |
|
48 {(char *)"lcd_rows", getint, (char **)&Config.lcd_rows}, |
|
49 {NULL, NULL, NULL} |
|
50 }; |
|
51 |
|
52 |
|
53 |
|
54 void killconfig(void) |
|
55 { |
|
56 w1_therm *tmp1, *old1; |
|
57 |
|
58 if (Config.name) |
|
59 free(Config.name); |
|
60 Config.name = NULL; |
|
61 |
|
62 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
|
63 old1 = tmp1->next; |
|
64 if (tmp1->master) |
|
65 free(tmp1->master); |
|
66 if (tmp1->name) |
|
67 free(tmp1->name); |
|
68 if (tmp1->alias) |
|
69 free(tmp1->alias); |
|
70 free(tmp1); |
|
71 } |
|
72 Config.w1therms = NULL; |
|
73 Config.my_port = 6554; |
|
74 |
|
75 Config.lcd_cols = 16; |
|
76 Config.lcd_rows = 2; |
|
77 } |
|
78 |
|
79 |
|
80 |
|
81 int wrconfig(char *config) |
|
82 { |
|
83 int rc = 0; |
|
84 FILE *fp; |
|
85 w1_therm *tmp1; |
|
86 |
|
87 if (getenv((char *)"USER") == NULL) { |
|
88 mypath = xstrcpy((char *)"/root"); |
|
89 } else { |
|
90 mypath = xstrcpy(getenv((char *)"HOME")); |
|
91 } |
|
92 mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); |
|
93 mypath = xstrcat(mypath, config); |
|
94 |
|
95 if (debug) |
|
96 fprintf(stdout, "Writing %s\n", mypath); |
|
97 |
|
98 if ((fp = fopen(mypath, "w")) == NULL) { |
|
99 syslog(LOG_NOTICE, "could not rewrite %s", mypath); |
|
100 return 1; |
|
101 } |
|
102 |
|
103 fprintf(fp, "# Configuration file for thermferm %s\n", VERSION); |
|
104 fprintf(fp, "\n"); |
|
105 |
|
106 fprintf(fp, "# LCD display\n"); |
|
107 fprintf(fp, "#\n"); |
|
108 fprintf(fp, "lcd_cols %d\n", Config.lcd_cols); |
|
109 fprintf(fp, "lcd_rows %d\n", Config.lcd_rows); |
|
110 fprintf(fp, "\n"); |
|
111 |
|
112 fprintf(fp, "# DS18B20 temperature sensors on the 1-wire bus.\n"); |
|
113 fprintf(fp, "#\n"); |
|
114 fprintf(fp, "# kwd master bus name alias\n"); |
|
115 for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) { |
|
116 fprintf(fp, "w1therm %s %d %s %s\n", tmp1->master, tmp1->bus, tmp1->name, tmp1->alias); |
|
117 } |
|
118 fprintf(fp, "\n"); |
|
119 |
|
120 fprintf(fp, "# End of generated configuration\n"); |
|
121 fclose(fp); |
|
122 syslog(LOG_NOTICE, "Written %s rc=%d", mypath, rc); |
|
123 free(mypath); |
|
124 mypath = NULL; |
|
125 |
|
126 return rc; |
|
127 } |
|
128 |
|
129 |
|
130 |
|
131 int rdconfig(char *config) |
|
132 { |
|
133 char buf[256], *p; |
|
134 FILE *fp; |
|
135 int i, rc = 0; |
|
136 |
|
137 killconfig(); |
|
138 |
|
139 syslog(LOG_NOTICE, "HOME='%s' USER='%s' LOGNAME='%s'", MBSE_SS(getenv((char *)"HOME")), MBSE_SS(getenv((char *)"USER")), MBSE_SS(getenv((char *)"LOGNAME"))); |
|
140 |
|
141 /* |
|
142 * Search config file |
|
143 */ |
|
144 if (getenv((char *)"USER") == NULL) { |
|
145 mypath = xstrcpy((char *)"/root"); |
|
146 } else { |
|
147 mypath = xstrcpy(getenv((char *)"HOME")); |
|
148 } |
|
149 mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); |
|
150 mypath = xstrcat(mypath, config); |
|
151 if ((fp = fopen(mypath, "r")) == NULL) { |
|
152 /* |
|
153 * Not in the users home directory |
|
154 */ |
|
155 free(mypath); |
|
156 mypath = xstrcpy((char *)"/etc/mbsepi-apps/"); |
|
157 mypath = xstrcat(mypath, config); |
|
158 if ((fp = fopen(mypath, "r")) == NULL) { |
|
159 /* |
|
160 * Try /usr/local/etc |
|
161 */ |
|
162 free(mypath); |
|
163 mypath = xstrcpy((char *)"/usr/local/etc/mbsepi-apps/"); |
|
164 mypath = xstrcat(mypath, config); |
|
165 if ((fp = fopen(mypath, "r")) == NULL) { |
|
166 syslog(LOG_NOTICE, "rdconfig: could find %s", config); |
|
167 return 1; |
|
168 } |
|
169 } |
|
170 } |
|
171 syslog(LOG_NOTICE, "rdconfig: using %s", mypath); |
|
172 |
|
173 linecnt = 0; |
|
174 while (fgets(buf, sizeof(buf) -1, fp)) { |
|
175 linecnt++; |
|
176 if (*(p = buf + strlen(buf) -1) != '\n') { |
|
177 syslog(LOG_NOTICE, "rdconfig: %s(%d): \"%s\" - line too long", mypath, linecnt, buf); |
|
178 rc = 1; |
|
179 break; |
|
180 } |
|
181 *p-- = '\0'; |
|
182 while ((p >= buf) && isspace(*p)) |
|
183 *p-- = '\0'; |
|
184 k = buf; |
|
185 while (*k && isspace(*k)) |
|
186 k++; |
|
187 p = k; |
|
188 while (*p && !isspace(*p)) |
|
189 p++; |
|
190 *p++='\0'; |
|
191 v = p; |
|
192 while (*v && isspace(*v)) |
|
193 v++; |
|
194 |
|
195 if ((*k == '\0') || (*k == '#')) { |
|
196 continue; |
|
197 } |
|
198 |
|
199 for (i = 0; keytab[i].key; i++) |
|
200 if (strcasecmp(k,keytab[i].key) == 0) |
|
201 break; |
|
202 |
|
203 if (keytab[i].key == NULL) { |
|
204 syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - unknown keyword", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
|
205 rc = 1; |
|
206 break; |
|
207 } else if ((keytab[i].prc(keytab[i].dest))) { |
|
208 rc = 1; |
|
209 break; |
|
210 } |
|
211 |
|
212 } |
|
213 fclose(fp); |
|
214 |
|
215 free(mypath); |
|
216 mypath = NULL; |
|
217 |
|
218 return rc; |
|
219 } |
|
220 |
|
221 |
|
222 |
|
223 static int getint(char **dest) |
|
224 { |
|
225 if (debug) |
|
226 syslog(LOG_NOTICE, "rdconfig: getint: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
|
227 |
|
228 if (strspn(v,"0123456789") != strlen(v)) |
|
229 syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad numeric", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
|
230 else |
|
231 *((int*)dest)=atoi(v); |
|
232 return 0; |
|
233 } |
|
234 |
|
235 |
|
236 |
|
237 static int getw1(char **dest) |
|
238 { |
|
239 char *p, *q = NULL, *r = NULL; |
|
240 w1_therm **tmpm; |
|
241 int rc = 0, tmpp; |
|
242 |
|
243 for (p = v; *p && !isspace(*p); p++); |
|
244 if (*p) |
|
245 *p++ = '\0'; |
|
246 while (*p && isspace(*p)) |
|
247 p++; |
|
248 if (*p == '\0') { |
|
249 syslog(LOG_NOTICE, "rdconfig: %s(%d): less then two tokens", mypath, linecnt); |
|
250 return 1; |
|
251 } |
|
252 |
|
253 for (q = p; *q && !isspace(*q); q++); |
|
254 if (*q && isspace(*q)) { |
|
255 if (*q) |
|
256 *q++ = '\0'; |
|
257 while (*q && isspace(*q)) |
|
258 q++; |
|
259 |
|
260 for (r = q; *r && !isspace(*r); r++); |
|
261 if (*r) |
|
262 *r++ = '\0'; |
|
263 rc = sscanf(p, "%d", &tmpp); |
|
264 if (rc != 1) { |
|
265 syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s is not a integer value", mypath, linecnt, p); |
|
266 return 1; |
|
267 } |
|
268 if (debug) |
|
269 syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s %d %s %s", mypath, linecnt, v, tmpp, q, r); |
|
270 } |
|
271 |
|
272 for (tmpm = (w1_therm**)dest; *tmpm; tmpm=&((*tmpm)->next)); |
|
273 (*tmpm) = (w1_therm *) xmalloc(sizeof(w1_therm)); |
|
274 (*tmpm)->next = NULL; |
|
275 (*tmpm)->master = xstrcpy(v); |
|
276 (*tmpm)->bus = tmpp; |
|
277 (*tmpm)->name = xstrcpy(q); |
|
278 (*tmpm)->alias = xstrcpy(r); |
|
279 (*tmpm)->present = 0; |
|
280 (*tmpm)->lastval = 0; |
|
281 (*tmpm)->update = 0; |
|
282 |
|
283 return 0; |
|
284 } |
|
285 |
|
286 |
|