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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/*
* Trivial smb.conf parsing code
* iniparser compatibility layer.
*
* Copyright Jeremy Allison <jra@samba.org> 2014
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, and the entire permission notice in its entirety,
* including the disclaimer of warranties.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* ALTERNATIVELY, this product may be distributed under the terms of
* the GNU Public License Version 3 or later, in which case the provisions
* of the GPL are required INSTEAD OF the above restrictions. (This clause is
* necessary due to a potential bad interaction between the GPL and
* the restrictions contained in a BSD-style copyright.)
*
* THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>
#include "tini.h"
#include "tiniparser.h"
struct tiniparser_entry {
struct tiniparser_entry *next_entry;
char *key;
char *value;
};
struct tiniparser_section {
struct tiniparser_section *next_section;
struct tiniparser_entry *entry_list;
char section_name[];
};
struct tiniparser_dictionary {
struct tiniparser_section *section_list;
};
/*
* Find a section from a given key.
* Also return start of subkey.
* Return NULL if section name can't be found,
* if no section name given, or no subkey given.
*/
static struct tiniparser_section *find_section(struct tiniparser_dictionary *d,
const char *key,
const char **subkey)
{
struct tiniparser_section *curr_section;
const char *p;
size_t section_len;
if (key == NULL) {
return NULL;
}
p = strchr(key, ':');
if (p == NULL) {
/* No section. */
return NULL;
}
section_len = p - key;
/* Ensure we have at least one character of section name. */
if (section_len == 0) {
return NULL;
}
/* Ensure we have at least one character of subkey. */
if (p[1] == '\0') {
return NULL;
}
for (curr_section = d->section_list;
curr_section;
curr_section = curr_section->next_section) {
/*
* Check if the key section matches the
* section name *exactly* (with terminating
* null after section_len characters.
*/
if ((strncasecmp(key, curr_section->section_name, section_len) == 0) &&
(curr_section->section_name[section_len] == '\0')) {
*subkey = p+1;
return curr_section;
}
}
return NULL;
}
static struct tiniparser_entry *find_entry(struct tiniparser_section *section,
const char *key)
{
struct tiniparser_entry *curr_entry;
for (curr_entry = section->entry_list;
curr_entry;
curr_entry = curr_entry->next_entry) {
if (strcasecmp(key,
curr_entry->key) == 0) {
return curr_entry;
}
}
return NULL;
}
const char *tiniparser_getstring(struct tiniparser_dictionary *d,
const char *key,
const char *default_value)
{
struct tiniparser_section *section;
struct tiniparser_entry *entry;
const char *subkey;
section = find_section(d, key, &subkey);
if (section == NULL) {
return default_value;
}
entry = find_entry(section, subkey);
if (entry == NULL) {
return default_value;
}
return entry->value;
}
bool tiniparser_getboolean(struct tiniparser_dictionary *d,
const char *key,
bool default_value)
{
const char *value = tiniparser_getstring(d, key, NULL);
if (value == NULL) {
return default_value;
}
switch(value[0]) {
case '1':
case 'T':
case 't':
case 'y':
case 'Y':
return true;
case '0':
case 'F':
case 'f':
case 'n':
case 'N':
return false;
default:
break;
}
return default_value;
}
int tiniparser_getint(struct tiniparser_dictionary *d,
const char *key,
int default_value)
{
const char *value = tiniparser_getstring(d, key, NULL);
if (value == NULL) {
return default_value;
}
return (int)strtol(value, NULL, 0);
}
static bool value_parser(const char *key,
const char *value,
void *private_data)
{
struct tiniparser_dictionary *d =
(struct tiniparser_dictionary *)private_data;
struct tiniparser_section *section = d->section_list;
struct tiniparser_entry *entry = NULL;
size_t val_len;
size_t key_len;
if (section == NULL) {
return false;
}
if (key == NULL) {
return false;
}
if (value == NULL) {
return false;
}
key_len = strlen(key) + 1;
val_len = strlen(value) + 1;
entry = find_entry(section, key);
if (entry) {
/* Replace current value. */
char *new_val = malloc(val_len);
if (new_val == NULL) {
return false;
}
memcpy(new_val, value, val_len);
free(entry->value);
entry->value = new_val;
return true;
}
/* Create a new entry. */
entry = malloc(sizeof(struct tiniparser_entry));
if (entry == NULL) {
return false;
}
entry->key = malloc(key_len);
if (entry->key == NULL) {
free(entry);
return false;
}
memcpy(entry->key, key, key_len);
entry->value = malloc(val_len);
if (entry->value == NULL) {
free(entry->key);
free(entry);
return false;
}
memcpy(entry->value, value, val_len);
entry->next_entry = section->entry_list;
section->entry_list = entry;
return true;
}
static bool section_parser(const char *section_name,
void *private_data)
{
struct tiniparser_section **pp_section;
struct tiniparser_section *new_section;
struct tiniparser_dictionary *d =
(struct tiniparser_dictionary *)private_data;
size_t section_name_len;
if (section_name == NULL) {
return false;
}
/* Section names can't contain ':' */
if (strchr(section_name, ':') != NULL) {
return false;
}
/* Do we already have this section ? */
for (pp_section = &d->section_list;
*pp_section;
pp_section = &(*pp_section)->next_section) {
if (strcasecmp(section_name,
(*pp_section)->section_name) == 0) {
/*
* Move to the front of the list for
* value_parser() to find it.
*/
/* First save current entry. */
struct tiniparser_section *curr_section = *pp_section;
/* Now unlink current entry from list. */
*pp_section = curr_section->next_section;
/* Make current entry next point to whole list. */
curr_section->next_section = d->section_list;
/* And replace list with current entry at start. */
d->section_list = curr_section;
return true;
}
}
section_name_len = strlen(section_name) + 1;
/* Create new section. */
new_section = malloc(
offsetof(struct tiniparser_section, section_name) +
section_name_len);
if (new_section == NULL) {
return false;
}
memcpy(new_section->section_name, section_name, section_name_len);
new_section->entry_list = NULL;
/* Add it to the head of the singly linked list. */
new_section->next_section = d->section_list;
d->section_list = new_section;
return true;
}
struct tiniparser_dictionary *tiniparser_load_stream(FILE *fp)
{
bool ret;
struct tiniparser_dictionary *d = NULL;
d = malloc(sizeof(struct tiniparser_dictionary));
if (d == NULL) {
return NULL;
}
d->section_list = NULL;
ret = tini_parse(fp,
false,
section_parser,
value_parser,
d);
if (ret == false) {
tiniparser_freedict(d);
d = NULL;
}
return d;
}
struct tiniparser_dictionary *tiniparser_load(const char *filename)
{
struct tiniparser_dictionary *d;
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
return NULL;
}
d = tiniparser_load_stream(fp);
fclose(fp);
return d;
}
void tiniparser_freedict(struct tiniparser_dictionary *d)
{
struct tiniparser_section *curr_section, *next_section;
if (d == NULL) {
return;
}
for (curr_section = d->section_list;
curr_section;
curr_section = next_section) {
struct tiniparser_entry *curr_entry, *next_entry;
next_section = curr_section->next_section;
for (curr_entry = curr_section->entry_list;
curr_entry;
curr_entry = next_entry) {
next_entry = curr_entry->next_entry;
free(curr_entry->key);
free(curr_entry->value);
free(curr_entry);
}
free(curr_section);
}
free(d);
}
|