bmsd/rdconfig.c

changeset 0
033898178630
child 194
d202777ebae5
equal deleted inserted replaced
-1:000000000000 0:033898178630
1 /*****************************************************************************
2 * Copyright (C) 2017-2018
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the bms (Brewery Management System)
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 * bms 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 ThermFerm; 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 "bms.h"
24 #include "xutil.h"
25 #include "futil.h"
26 #include "rdconfig.h"
27
28
29 static char *mypath;
30 static char *k, *v;
31 static int linecnt = 0;
32
33 extern int debug;
34 extern char *Private_Path;
35
36
37 sys_config Config;
38
39
40 static int getstr(char **);
41 static int getint(char **);
42
43
44 key_list keytab[] = {
45 {(char *)"bms_name", getstr, (char **)&Config.bms_name},
46 {(char *)"bms_uuid", getstr, (char **)&Config.bms_uuid},
47 {(char *)"mysql_host", getstr, (char **)&Config.mysql_host},
48 {(char *)"mysql_port", getint, (char **)&Config.mysql_port},
49 {(char *)"mysql_user", getstr, (char **)&Config.mysql_user},
50 {(char *)"mysql_pass", getstr, (char **)&Config.mysql_pass},
51 {(char *)"mysql_database", getstr, (char **)&Config.mysql_database},
52 {(char *)"mqtt_host", getstr, (char **)&Config.mqtt_host},
53 {(char *)"mqtt_port", getint, (char **)&Config.mqtt_port},
54 {(char *)"mqtt_user", getstr, (char **)&Config.mqtt_user},
55 {(char *)"mqtt_pass", getstr, (char **)&Config.mqtt_pass},
56 {NULL, NULL, NULL}
57 };
58
59
60
61 /*
62 * Kill configuration data
63 */
64 void killconfig(void)
65 {
66 if (Config.bms_name)
67 free(Config.bms_name);
68 Config.bms_name = NULL;
69 if (Config.bms_uuid)
70 free(Config.bms_uuid);
71 Config.bms_uuid = NULL;
72
73 if (Config.mysql_host)
74 free(Config.mysql_host);
75 Config.mysql_host = NULL;
76 Config.mysql_port = 3306;
77 if (Config.mysql_user)
78 free(Config.mysql_user);
79 Config.mysql_user = NULL;
80 if (Config.mysql_pass)
81 free(Config.mysql_pass);
82 Config.mysql_pass = NULL;
83 if (Config.mysql_database)
84 free(Config.mysql_database);
85 Config.mysql_database = NULL;
86
87 if (Config.mqtt_host)
88 free(Config.mqtt_host);
89 Config.mqtt_host = NULL;
90 Config.mqtt_port = 1883;
91 if (Config.mqtt_user)
92 free(Config.mqtt_user);
93 Config.mqtt_user = NULL;
94 if (Config.mqtt_pass)
95 free(Config.mqtt_pass);
96 Config.mqtt_pass = NULL;
97 }
98
99
100
101 int wrconfig(void)
102 {
103 FILE *fp;
104
105 if (mypath)
106 free(mypath);
107 mypath = xstrcpy(Private_Path);
108 mypath = xstrcat(mypath, (char *)"/bms.config");
109 if (mkdirs(mypath, 0755) == FALSE)
110 return 1;
111
112 if ((fp = fopen(mypath, "w")) == NULL) {
113 syslog(LOG_NOTICE, "[rdconfig] could not rewrite %s", mypath);
114 return 1;
115 }
116
117 fprintf(fp, "# Configuration file for BMS %s\n", VERSION);
118 fprintf(fp, "#\n");
119 if (Config.bms_name)
120 fprintf(fp, "bms_name %s\n", Config.bms_name);
121 if (Config.bms_uuid)
122 fprintf(fp, "bms_uuid %s\n", Config.bms_uuid);
123 fprintf(fp, "#\n");
124
125 if (Config.mysql_host)
126 fprintf(fp, "mysql_host %s\n", Config.mysql_host);
127 fprintf(fp, "mysql_port %d\n", Config.mysql_port);
128 if (Config.mysql_user)
129 fprintf(fp, "mysql_user %s\n", Config.mysql_user);
130 else
131 fprintf(fp, "#mysql_user <username>\n");
132 if (Config.mysql_pass)
133 fprintf(fp, "mysql_pass %s\n", Config.mysql_pass);
134 else
135 fprintf(fp, "#mysql_pass <passwd>\n");
136 if (Config.mysql_database)
137 fprintf(fp, "mysql_database %s\n", Config.mysql_database);
138 else
139 fprintf(fp, "#mysql_database <dbname>\n");
140 fprintf(fp, "#\n");
141
142 if (Config.mqtt_host)
143 fprintf(fp, "mqtt_host %s\n", Config.mqtt_host);
144 fprintf(fp, "mqtt_port %d\n", Config.mqtt_port);
145 if (Config.mqtt_user)
146 fprintf(fp, "mqtt_user %s\n", Config.mqtt_user);
147 else
148 fprintf(fp, "#mqtt_user <username>\n");
149 if (Config.mqtt_pass)
150 fprintf(fp, "mqtt_pass %s\n", Config.mqtt_pass);
151 else
152 fprintf(fp, "#mqtt_pwd <passwd>\n");
153
154 fprintf(fp, "#\n");
155 fprintf(fp, "# End of generated configuration\n");
156 fclose(fp);
157 free(mypath);
158 mypath = NULL;
159 return 0;
160 }
161
162
163
164 int rdconfig(void)
165 {
166 FILE *fp;
167 uuid_t uu;
168 char buf[256], *p;
169 int i, rc = 0;
170
171 if (mypath)
172 free(mypath);
173 mypath = xstrcpy(Private_Path);
174 mypath = xstrcat(mypath, (char *)"/bms.config");
175
176 killconfig();
177
178 if (debug)
179 fprintf(stdout, "[rdconfig] reading %s\n", mypath);
180
181 if ((fp = fopen(mypath, "r")) == NULL) {
182 syslog(LOG_NOTICE, "[rdconfig] cannot open %s", mypath);
183 Config.bms_name = xstrcpy((char *)"My Brewery");
184 Config.bms_uuid = malloc(37);
185 uuid_generate(uu);
186 uuid_unparse(uu, Config.bms_uuid);
187 Config.mysql_port = 3306;
188 Config.mysql_host = xstrcpy((char *)"localhost");
189 Config.mqtt_port = 1883;
190 Config.mqtt_host = xstrcpy((char *)"localhost");
191 return wrconfig();
192 }
193
194 linecnt = 0;
195 while (fgets(buf, sizeof(buf) -1, fp)) {
196 linecnt++;
197 if (*(p = buf + strlen(buf) -1) != '\n') {
198 syslog(LOG_NOTICE, "[rdconfig] %s(%d): \"%s\" - line too long", mypath, linecnt, buf);
199 rc = 1;
200 break;
201 }
202 *p-- = '\0';
203 while ((p >= buf) && isspace(*p))
204 *p-- = '\0';
205 k = buf;
206 while (*k && isspace(*k))
207 k++;
208 p = k;
209 while (*p && !isspace(*p))
210 p++;
211 *p++='\0';
212 v = p;
213 while (*v && isspace(*v))
214 v++;
215
216 if ((*k == '\0') || (*k == '#')) {
217 continue;
218 }
219
220 for (i = 0; keytab[i].key; i++)
221 if (strcasecmp(k,keytab[i].key) == 0)
222 break;
223
224 if (keytab[i].key == NULL) {
225 syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - unknown keyword", mypath, linecnt, S(k), S(v));
226 rc = 1;
227 break;
228 } else if ((keytab[i].prc(keytab[i].dest))) {
229 rc = 1;
230 break;
231 }
232 }
233 fclose(fp);
234
235 free(mypath);
236 mypath = NULL;
237
238 return rc;
239 }
240
241
242
243 static int getstr(char **dest)
244 {
245 if (debug)
246 fprintf(stdout, "[rdconfig] getstr: %s(%d): %s %s\n", mypath, linecnt, S(k), S(v));
247
248 *dest = xstrcpy(v);
249 return 0;
250 }
251
252
253
254 static int getint(char **dest)
255 {
256 if (debug)
257 fprintf(stdout, "[rdconfig] getint: %s(%d): %s %s\n", mypath, linecnt, k, v);
258
259 if (strspn(v,"0123456789") != strlen(v))
260 syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v));
261 else
262 *((int*)dest)=atoi(v);
263 return 0;
264 }
265
266
267

mercurial