source: mainline/uspace/lib/conf/src/ini.c@ 241f1985

Last change on this file since 241f1985 was 241f1985, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Correcting failure from previous merge

The commits from Michal Koutný from the branch system-daemon
where built on a old version of Helenos. Because of this
many types and API functions have changed. This commit
upgrades the merge code

  • Property mode set to 100644
File size: 12.5 KB
Line 
1/*
2 * Copyright (c) 2015 Michal Koutny
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <adt/hash.h>
30#include <adt/hash_table.h>
31#include <assert.h>
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <str.h>
36
37#include "conf/ini.h"
38
39#define LINE_BUFFER 256
40
41/** Representation of key-value pair from INI file
42 *
43 * @note Structure is owner of its string data.
44 */
45typedef struct {
46 ht_link_t ht_link;
47
48 /** First line from where the item was extracted. */
49 size_t lineno;
50
51 char *key;
52 char *value;
53} ini_item_t;
54
55/** Line reader for generic parsing */
56typedef char *(*line_reader_t)(char *, int, void *);
57
58/* Necessary forward declarations */
59static void ini_section_destroy(ini_section_t **);
60static void ini_item_destroy(ini_item_t **);
61static ini_section_t *ini_section_create(void);
62static ini_item_t *ini_item_create(void);
63
64/* Hash table functions */
65static size_t ini_section_ht_hash(const ht_link_t *item)
66{
67 ini_section_t *section =
68 hash_table_get_inst(item, ini_section_t, ht_link);
69 /* Nameless default section */
70 if (section->name == NULL) {
71 return 0;
72 }
73
74 return hash_string(section->name);
75}
76
77static size_t ini_section_ht_key_hash(const void *key)
78{
79 /* Nameless default section */
80 if (key == NULL) {
81 return 0;
82 }
83 return hash_string((const char *)key);
84}
85
86static bool ini_section_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
87{
88 return str_cmp(
89 hash_table_get_inst(item1, ini_section_t, ht_link)->name,
90 hash_table_get_inst(item2, ini_section_t, ht_link)->name) == 0;
91}
92
93static bool ini_section_ht_key_equal(const void *key, const ht_link_t *item)
94{
95 const char *name = key;
96 ini_section_t *section =
97 hash_table_get_inst(item, ini_section_t, ht_link);
98
99 if (key == NULL || section->name == NULL) {
100 return section->name == key;
101 }
102
103 return str_cmp(name, section->name) == 0;
104}
105
106static void ini_section_ht_remove(ht_link_t *item)
107{
108 ini_section_t *section = hash_table_get_inst(item, ini_section_t, ht_link);
109 ini_section_destroy(&section);
110}
111
112
113static size_t ini_item_ht_hash(const ht_link_t *item)
114{
115 ini_item_t *ini_item =
116 hash_table_get_inst(item, ini_item_t, ht_link);
117 assert(ini_item->key);
118 return hash_string(ini_item->key);
119}
120
121static size_t ini_item_ht_key_hash(const void *key)
122{
123 return hash_string((const char *)key);
124}
125
126static bool ini_item_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
127{
128 return str_cmp(
129 hash_table_get_inst(item1, ini_item_t, ht_link)->key,
130 hash_table_get_inst(item2, ini_item_t, ht_link)->key) == 0;
131}
132
133static bool ini_item_ht_key_equal(const void *key, const ht_link_t *item)
134{
135 return str_cmp((const char *)key,
136 hash_table_get_inst(item, ini_item_t, ht_link)->key) == 0;
137}
138
139static void ini_item_ht_remove(ht_link_t *item)
140{
141 ini_item_t *ini_item = hash_table_get_inst(item, ini_item_t, ht_link);
142 ini_item_destroy(&ini_item);
143}
144
145
146static hash_table_ops_t configuration_ht_ops = {
147 .hash = &ini_section_ht_hash,
148 .key_hash = &ini_section_ht_key_hash,
149 .equal = &ini_section_ht_equal,
150 .key_equal = &ini_section_ht_key_equal,
151 .remove_callback = &ini_section_ht_remove
152};
153
154static hash_table_ops_t section_ht_ops = {
155 .hash = &ini_item_ht_hash,
156 .key_hash = &ini_item_ht_key_hash,
157 .equal = &ini_item_ht_equal,
158 .key_equal = &ini_item_ht_key_equal,
159 .remove_callback = &ini_item_ht_remove
160};
161
162/*
163 * Static functions
164 */
165static char *read_file(char *buffer, int size, void *data)
166{
167 return fgets(buffer, size, (FILE *)data);
168}
169
170static char *read_string(char *buffer, int size, void *data)
171{
172 char **string_ptr = (char **)data;
173 char *string = *string_ptr;
174
175 int i = 0;
176 while (i < size - 1) {
177 char c = string[i];
178 if (c == '\0') {
179 break;
180 }
181
182 buffer[i++] = c;
183
184 if (c == '\n') {
185 break;
186 }
187 }
188
189 if (i == 0) {
190 return NULL;
191 }
192
193 buffer[i] = '\0';
194 *string_ptr = string + i;
195 return buffer;
196}
197
198static int ini_parse_generic(line_reader_t line_reader, void *reader_data,
199 ini_configuration_t *conf, text_parse_t *parse)
200{
201 int rc = EOK;
202 char *line_buffer = NULL;
203
204 line_buffer = malloc(LINE_BUFFER);
205 if (line_buffer == NULL) {
206 rc = ENOMEM;
207 goto finish;
208 }
209
210 char *line = NULL;
211 ini_section_t *cur_section = NULL;
212 size_t lineno = 0;
213
214 while ((line = line_reader(line_buffer, LINE_BUFFER, reader_data))) {
215 ++lineno;
216 size_t line_len = str_size(line);
217 if (line[line_len - 1] != '\n') {
218 text_parse_raise_error(parse, lineno, INI_ETOO_LONG);
219 rc = EINVAL;
220 /* Cannot recover terminate parsing */
221 goto finish;
222 }
223 /* Ingore leading/trailing whitespace */
224 str_rtrim(line, '\n');
225 str_ltrim(line, ' ');
226 str_rtrim(line, ' ');
227
228 /* Empty line */
229 if (str_size(line) == 0) {
230 continue;
231 }
232
233 /* Comment line */
234 if (line[0] == ';' || line[0] == '#') {
235 continue;
236 }
237
238 /* Start new section */
239 if (line[0] == '[') {
240 cur_section = ini_section_create();
241 if (cur_section == NULL) {
242 rc = ENOMEM;
243 goto finish;
244 }
245
246 char *close_bracket = str_chr(line, ']');
247 if (close_bracket == NULL) {
248 ini_section_destroy(&cur_section);
249 text_parse_raise_error(parse, lineno,
250 INI_EBRACKET_EXPECTED);
251 rc = EINVAL;
252 goto finish;
253 }
254
255 cur_section->lineno = lineno;
256 *close_bracket = '\0';
257 cur_section->name = str_dup(line + 1);
258
259 if (!hash_table_insert_unique(&conf->sections,
260 &cur_section->ht_link)) {
261 ini_section_destroy(&cur_section);
262 text_parse_raise_error(parse, lineno,
263 INI_EDUP_SECTION);
264 rc = EINVAL;
265 goto finish;
266 }
267
268 continue;
269 }
270
271 /* Create a default section if none was specified */
272 if (cur_section == NULL) {
273 cur_section = ini_section_create();
274 if (cur_section == NULL) {
275 rc = ENOMEM;
276 goto finish;
277 }
278 cur_section->lineno = lineno;
279
280 bool inserted = hash_table_insert_unique(&conf->sections,
281 &cur_section->ht_link);
282 assert(inserted);
283 }
284
285 /* Parse key-value pairs */
286 ini_item_t *item = ini_item_create();
287 if (item == NULL) {
288 rc = ENOMEM;
289 goto finish;
290 }
291 item->lineno = lineno;
292
293 char *assign_char = str_chr(line, '=');
294 if (assign_char == NULL) {
295 rc = EINVAL;
296 text_parse_raise_error(parse, lineno,
297 INI_EASSIGN_EXPECTED);
298 goto finish;
299 }
300
301 *assign_char = '\0';
302 char *key = line;
303 str_ltrim(key, ' ');
304 str_rtrim(key, ' ');
305 item->key = str_dup(key);
306 if (item->key == NULL) {
307 ini_item_destroy(&item);
308 rc = ENOMEM;
309 goto finish;
310 }
311
312 char *value = assign_char + 1;
313 str_ltrim(value, ' ');
314 str_rtrim(value, ' ');
315 item->value = str_dup(value);
316 if (item->value == NULL) {
317 ini_item_destroy(&item);
318 rc = ENOMEM;
319 goto finish;
320 }
321
322 hash_table_insert(&cur_section->items, &item->ht_link);
323 }
324
325finish:
326 free(line_buffer);
327
328 return rc;
329}
330
331
332
333/*
334 * Actual INI functions
335 */
336
337void ini_configuration_init(ini_configuration_t *conf)
338{
339 hash_table_create(&conf->sections, 0, 0, &configuration_ht_ops);
340}
341
342/** INI configuration destructor
343 *
344 * Release all resources of INI structure but the structure itself.
345 */
346void ini_configuration_deinit(ini_configuration_t *conf)
347{
348 hash_table_destroy(&conf->sections);
349}
350
351static void ini_section_init(ini_section_t *section)
352{
353 hash_table_create(&section->items, 0, 0, &section_ht_ops);
354 section->name = NULL;
355}
356
357static ini_section_t* ini_section_create(void)
358{
359 ini_section_t *section = malloc(sizeof(ini_section_t));
360 if (section != NULL) {
361 ini_section_init(section);
362 }
363 return section;
364}
365
366static void ini_section_destroy(ini_section_t **section_ptr)
367{
368 ini_section_t *section = *section_ptr;
369 if (section == NULL) {
370 return;
371 }
372 hash_table_destroy(&section->items);
373 free(section->name);
374 free(section);
375 *section_ptr = NULL;
376}
377
378static void ini_item_init(ini_item_t *item)
379{
380 item->key = NULL;
381 item->value = NULL;
382}
383
384static ini_item_t *ini_item_create(void)
385{
386 ini_item_t *item = malloc(sizeof(ini_item_t));
387 if (item != NULL) {
388 ini_item_init(item);
389 }
390 return item;
391}
392
393static void ini_item_destroy(ini_item_t **item_ptr)
394{
395 ini_item_t *item = *item_ptr;
396 if (item == NULL) {
397 return;
398 }
399 free(item->key);
400 free(item->value);
401 free(item);
402 *item_ptr = NULL;
403}
404
405
406/** Parse file contents to INI structure
407 *
408 * @param[in] filename
409 * @param[out] conf initialized structure for configuration
410 * @param[out] parse initialized structure to keep parsing errors
411 *
412 * @return EOK on success
413 * @return EIO when file cannot be opened
414 * @return ENOMEM
415 * @return EINVAL on parse error (details in parse structure)
416 */
417int ini_parse_file(const char *filename, ini_configuration_t *conf,
418 text_parse_t *parse)
419{
420 FILE *f = NULL;
421 f = fopen(filename, "r");
422 if (f == NULL) {
423 return EIO;
424 }
425
426 int rc = ini_parse_generic(&read_file, f, conf, parse);
427 fclose(f);
428 return rc;
429}
430
431/** Parse string to INI structure
432 *
433 * @param[in] string
434 * @param[out] conf initialized structure for configuration
435 * @param[out] parse initialized structure to keep parsing errors
436 *
437 * @return EOK on success
438 * @return ENOMEM
439 * @return EINVAL on parse error (details in parse structure)
440 */
441int ini_parse_string(const char *string, ini_configuration_t *conf,
442 text_parse_t *parse)
443{
444 const char *string_ptr = string;
445
446 return ini_parse_generic(&read_string, &string_ptr, conf, parse);
447}
448
449
450/** Get a section from configuration
451 *
452 * @param[in] ini_configuration
453 * @param[in] section_name name of section or NULL for default section
454 *
455 * @return Section with given name
456 * @return NULL when no such section exits
457 */
458ini_section_t *ini_get_section(ini_configuration_t *ini_conf,
459 const char *section_name)
460{
461 ht_link_t *item = hash_table_find(&ini_conf->sections,
462 (void *)section_name);
463 if (item == NULL) {
464 return NULL;
465 }
466
467 return hash_table_get_inst(item, ini_section_t, ht_link);
468}
469
470/** Get item iterator to items with given key in the section
471 *
472 * @param[in] section
473 * @param[in] key
474 *
475 * @return Always return iterator (even when there's no item with given key)
476 */
477ini_item_iterator_t ini_section_get_iterator(ini_section_t *section,
478 const char *key)
479{
480 ini_item_iterator_t result;
481 result.first_item = hash_table_find(&section->items, (void *)key);
482 result.cur_item = result.first_item;
483 result.table = &section->items;
484 result.incremented = false;
485
486 return result;
487}
488
489bool ini_item_iterator_valid(ini_item_iterator_t *iterator)
490{
491 bool empty = (iterator->cur_item == NULL);
492 bool maybe_looped = (iterator->cur_item == iterator->first_item);
493 return !(empty || (maybe_looped && iterator->incremented));
494}
495
496/** Move iterator to next item (of the same key)
497 *
498 * @param[in] iterator valid iterator
499 */
500void ini_item_iterator_inc(ini_item_iterator_t *iterator)
501{
502 iterator->cur_item =
503 hash_table_find_next(iterator->table, iterator->first_item, iterator->cur_item);
504 iterator->incremented = true;
505}
506
507/** Get item value for current iterator
508 *
509 * @param[in] iterator valid iterator
510 */
511const char *ini_item_iterator_value(ini_item_iterator_t *iterator)
512{
513 ini_item_t *ini_item =
514 hash_table_get_inst(iterator->cur_item, ini_item_t, ht_link);
515 return ini_item->value;
516}
517
518/** Get item line number for current iterator
519 *
520 * @param[in] iterator valid iterator
521 *
522 * @return Line number of input where item was originally defined.
523 */
524size_t ini_item_iterator_lineno(ini_item_iterator_t *iterator)
525{
526 ini_item_t *ini_item =
527 hash_table_get_inst(iterator->cur_item, ini_item_t, ht_link);
528 return ini_item->lineno;
529}
Note: See TracBrowser for help on using the repository browser.