-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouseconfig.c
251 lines (214 loc) · 7.53 KB
/
houseconfig.c
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
/* HouseConfig - A simpler API for reading a program JSON configuration.
*
* Copyright 2021, Pascal Martin
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* houseconfig.c - Simple API to access a JSON configuration.
*
* SYNOPSYS:
*
* void houseconfig_default (const char *arg);
*
* Set a hardcoded default for a command line option.
*
* const char *houseconfig_load (int argc, const char **argv);
*
* Load the configuration from the specified config option, or else
* from the default config file.
*
* This function consumes the following command line options:
* -config=STRING set the name of the configuration file.
* -no-local-storage Do not use a local configuration file.
*
* const char *houseconfig_name (void);
*
* Return the basename of the current configuration file.
*
* const char *houseconfig_current (void);
*
* Return the JSON data for the current configuration.
*
* int houseconfig_active (void);
*
* Return true if a configuration was successfully activated.
*
* const char *houseconfig_update (const char *text);
*
* Update both the live configuration and the configuration file with
* the provided text.
*
* const char *houseconfig_string (int parent, const char *path);
* int houseconfig_integer (int parent, const char *path);
* double houseconfig_boolean (int parent, const char *path);
*
* Access individual items starting from the specified parent
* (the config root is index 0).
*
* int houseconfig_array (int parent, const char *path);
* int houseconfig_array_length (int array);
*
* Retrieve an array.
*
* int houseconfig_object (int parent, const char *path);
* int houseconfig_array_object (int parent, int index);
*
* Retrieve an object (2nd form: as element of an array).
*
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <echttp.h>
#include <echttp_json.h>
#include "houselog.h"
#include "houseconfig.h"
static ParserToken *ConfigParsed = 0;
static int ConfigTokenAllocated = 0;
static int ConfigTokenCount = 0;
static char *ConfigText = 0;
static char *ConfigTextCurrent = 0;
#define HOUSECONFIG_PATH "/etc/house/"
#define HOUSECONFIG_EXT ".json"
static int ConfigFileEnabled = 1;
static const char *ConfigFile = HOUSECONFIG_PATH "portal" HOUSECONFIG_EXT;
static const char *ConfigName = 0; // Will point to base name in ConfigFile.
static const char *houseconfig_parse (void) {
if (!ConfigText) {
ConfigTokenCount = 0;
return "no configuration";
}
int count = echttp_json_estimate(ConfigText);
if (count > ConfigTokenAllocated) {
if (ConfigParsed) free (ConfigParsed);
ConfigTokenAllocated = count;
ConfigParsed = calloc (ConfigTokenAllocated, sizeof(ParserToken));
}
ConfigTokenCount = ConfigTokenAllocated;
char *proposedconfig = strdup (ConfigText);
const char *error =
echttp_json_parse (ConfigText, ConfigParsed, &ConfigTokenCount);
if (error) {
free (proposedconfig); // Don't touch the last valid config text.
ConfigTokenCount = 0;
houselog_trace (HOUSE_FAILURE, "CONFIG", "ERROR %s", error);
return error;
}
// The new proposed config is in service now.
if (ConfigTextCurrent) free (ConfigTextCurrent);
ConfigTextCurrent = proposedconfig;
return 0;
}
static void houseconfig_write (const char *text, int length) {
if (!ConfigFileEnabled) return;
int fd = open (ConfigFile, O_WRONLY|O_TRUNC|O_CREAT, 0777);
if (fd >= 0) {
write (fd, text, length);
close (fd);
houselog_event ("CONFIG", "DATA", "SAVED", "TO %s", ConfigFile);
} else {
houselog_event ("CONFIG", "FILE", "ERROR", "CANNOT WRITE TO %s", ConfigFile);
}
}
void houseconfig_default (const char *arg) {
const char *name = 0;
if (echttp_option_match ("-config=", arg, &name)) {
if ((name[0] == '/') || (name[0] == '.')) {
ConfigFile = strdup(name);
} else {
char buffer[1024];
const char *extension = HOUSECONFIG_EXT;
if (strchr (name, '.')) extension = "";
snprintf (buffer, sizeof(buffer),
HOUSECONFIG_PATH "%s%s", name, extension);
ConfigFile = strdup(buffer);
}
} else if (echttp_option_present ("-no-local-storage", arg)) {
ConfigFileEnabled = 0;
}
}
const char *houseconfig_load (int argc, const char **argv) {
int i;
for (i = 1; i < argc; ++i) {
houseconfig_default (argv[i]);
}
char *basename = strrchr (ConfigFile, '/');
if (basename) ConfigName = basename + 1;
else ConfigName = ConfigFile;
if (!ConfigFileEnabled) return 0; // No error.
houselog_event ("CONFIG", "DATA", "LOADING", "FROM %s", ConfigFile);
if (ConfigText) echttp_parser_free (ConfigText);
ConfigText = echttp_parser_load (ConfigFile);
return houseconfig_parse ();
}
const char *houseconfig_update (const char *text) {
if (ConfigText) echttp_parser_free (ConfigText);
ConfigText = echttp_parser_string (text);
const char *error = houseconfig_parse ();
if (error) return error;
houseconfig_write (text, strlen(text));
return 0;
}
const char *houseconfig_name (void) {
return ConfigName;
}
const char *houseconfig_current (void) {
return ConfigTextCurrent;
}
int houseconfig_active (void) {
return ConfigTokenCount > 0;
}
int houseconfig_find (int parent, const char *path, int type) {
int i;
if (parent < 0 || parent >= ConfigTokenCount) return -1;
i = echttp_json_search(ConfigParsed+parent, path);
if (i >= 0 && ConfigParsed[parent+i].type == type) return parent+i;
return -1;
}
const char *houseconfig_string (int parent, const char *path) {
int i = houseconfig_find(parent, path, PARSER_STRING);
return (i >= 0) ? ConfigParsed[i].value.string : 0;
}
int houseconfig_integer (int parent, const char *path) {
int i = houseconfig_find(parent, path, PARSER_INTEGER);
return (i >= 0) ? ConfigParsed[i].value.integer : 0;
}
int houseconfig_boolean (int parent, const char *path) {
int i = houseconfig_find(parent, path, PARSER_BOOL);
return (i >= 0) ? ConfigParsed[i].value.bool : 0;
}
int houseconfig_array (int parent, const char *path) {
return houseconfig_find(parent, path, PARSER_ARRAY);
}
int houseconfig_array_object (int parent, int index) {
char path[32];
snprintf (path, sizeof(path), "[%d]", index);
return houseconfig_find(parent, path, PARSER_OBJECT);
}
int houseconfig_array_length (int array) {
if (array < 0
|| array >= ConfigTokenCount
|| ConfigParsed[array].type != PARSER_ARRAY) return 0;
return ConfigParsed[array].length;
}
int houseconfig_object (int parent, const char *path) {
return houseconfig_find(parent, path, PARSER_OBJECT);
}