-
Notifications
You must be signed in to change notification settings - Fork 0
/
houseconfig.c
230 lines (199 loc) · 6.76 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
/* 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.
*
* int houseconfig_open (void);
*
* Return a file descriptor for reading the current configuration.
* This is typically used when there is no primitive for building
* a JSON config text from the live system, for example because
* there is no automatic discovery to populate the configuration.
*
* int houseconfig_size (void);
*
* Return the size of the configuration JSON text currently used.
*
* 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_json.h>
#include "houselog.h"
#include "houseconfig.h"
#define CONFIGMAXSIZE 1024
static ParserToken ConfigParsed[CONFIGMAXSIZE];
static int ConfigTokenCount = 0;
static char *ConfigText;
static int ConfigTextSize = 0;
static int ConfigTextLength = 0;
#define HOUSECONFIG_PATH "/etc/house/"
#define HOUSECONFIG_EXT ".json"
static const char *ConfigFile = HOUSECONFIG_PATH "portal" HOUSECONFIG_EXT;
static const char *houseconfig_refresh (const char *file) {
houselog_event ("CONFIG", "DATA", "LOADING", "FROM %s", file);
if (ConfigText) echttp_parser_free (ConfigText);
ConfigText = echttp_parser_load (file);
if (!ConfigText) {
ConfigTextLength = 0;
ConfigTokenCount = 0;
return "no configuration";
}
ConfigTextLength = strlen(ConfigText);
ConfigTokenCount = CONFIGMAXSIZE;
return echttp_json_parse (ConfigText, ConfigParsed, &ConfigTokenCount);
}
void houseconfig_default (const char *arg) {
const char *name = 0;
if (strncmp ("--config=", arg, 9) == 0) {
name = arg + 9;
} else if (strncmp ("-config=", arg, 8) == 0) {
name = arg + 8;
} else {
return;
}
if (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);
}
}
const char *houseconfig_load (int argc, const char **argv) {
int i;
for (i = 1; i < argc; ++i) {
houseconfig_default (argv[i]);
}
return houseconfig_refresh (ConfigFile);
}
const char *houseconfig_update (const char *text) {
int fd;
if (ConfigText) echttp_parser_free (ConfigText);
ConfigText = echttp_parser_string (text);
if (!ConfigText) {
ConfigTextLength = 0;
ConfigTokenCount = 0;
return "no configuration";
}
ConfigTextLength = strlen(ConfigText);
ConfigTokenCount = CONFIGMAXSIZE;
const char *error =
echttp_json_parse (ConfigText, ConfigParsed, &ConfigTokenCount);
if (error) {
echttp_parser_free(ConfigText);
ConfigText = 0;
ConfigTextLength = 0;
ConfigTokenCount = 0;
houselog_trace (HOUSE_FAILURE, "CONFIG", "ERROR %s", error);
return error;
}
fd = open (ConfigFile, O_WRONLY|O_TRUNC|O_CREAT, 0777);
if (fd >= 0) {
write (fd, text, ConfigTextLength);
close (fd);
houselog_event ("CONFIG", "DATA", "SAVED", "TO %s", ConfigFile);
} else {
houselog_event ("CONFIG", "FILE", "ERROR", "CANNOT OPEN %s", ConfigFile);
}
return 0;
}
int houseconfig_open (void) {
return open(ConfigFile, O_RDONLY);
}
int houseconfig_size (void) {
return ConfigTextLength;
}
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);
}