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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
|
#include "pom.h"
#include <stdio.h> // still needed for sprintf, even if POM_NO_STDIO is defined.
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <inttypes.h>
#if __GNUC__ >= 6
#define ATTRIBUTE_PRINTF(fmt, args) __attribute__ ((format(printf, fmt, args)))
#else
#define ATTRIBUTE_PRINTF(fmt, args)
#endif
#if _MSC_VER >= 1600
#define PRINTF_FORMAT_STRING _Printf_format_string_
#else
#define PRINTF_FORMAT_STRING
#endif
struct pom_error {
const pom_error *next;
const char *file;
uint64_t line;
const char *message;
};
struct main_conf;
struct to_free {
struct to_free *next;
// fool's max_align_t
union {
void *ptr;
double d;
uint64_t u64;
} data[];
};
struct pom_conf {
struct main_conf *main;
// this is checked vs main->version_number
// to detect when a section is used after a merge
uint64_t version_number;
size_t prefix_len;
const struct conf_item *items;
size_t items_count;
};
struct pom_item_iter {
const pom_conf *conf;
const struct conf_item *conf_item;
pom_item item;
};
struct conf_item {
const char *key, *value, *file;
uint64_t line;
const struct pom_conf *section;
};
struct main_conf {
struct conf_item *items;
struct to_free *to_free_head, *to_free_tail;
size_t items_count;
// increases when merged
uint64_t version_number;
};
// temporary error that is eventually converted to a pom_error
struct parser_error {
uint64_t line;
// index into parser->error_messages.array
uint32_t message;
};
// type for parser::utf8_state
enum utf8_state {
UTF8_STATE_DEFAULT = 0,
// want 1 continuation byte
UTF8_STATE_1CONT = 1,
// want 2 continuation bytes
UTF8_STATE_2CONT = 2,
// want 3 continuation bytes
UTF8_STATE_3CONT = 3,
// want 2 continuation bytes, first one must be >=0xA0 (otherwise encoding is overlong)
UTF8_STATE_2CONT_GTEQ_A0 = 4,
// want 2 continuation bytes, first one must be <0xA0 (otherwise encodes a UTF-16 surrogate)
UTF8_STATE_2CONT_LT_A0 = 5,
// want 3 continuation bytes, first one must be >=0x90 (otherwise encodoing is overlong)
UTF8_STATE_3CONT_GTEQ_90 = 6,
// want 3 continuation bytes, first one must be <0x90 (otherwise encoding produces oversized code point)
UTF8_STATE_3CONT_LT_90 = 7,
};
// temporary item that is eventually turned into a conf_item
struct parser_item {
size_t key;
size_t value;
uint64_t line;
};
struct parser {
const char *filename;
uint64_t line_number;
size_t (*read_func)(void *, char *, size_t);
void *userdata;
pom_error *out_of_memory_error;
struct {
char *array;
size_t capacity;
} line;
struct {
struct parser_error *array;
size_t count, capacity;
} errors;
struct {
char *array;
size_t count, capacity;
} error_messages;
struct {
char *array;
size_t count, capacity;
} string_data;
struct {
char *array;
size_t capacity;
size_t len;
} current_section;
struct {
struct parser_item *array;
size_t capacity;
size_t count;
} items;
bool short_read, eof, out_of_memory, leftover_cr;
// see enum utf8_state -- starting state for future calls to read_func
uint8_t utf8_state;
uint16_t buf_pos;
uint16_t buf_count;
char buf[4096];
};
#ifdef POM_NO_STDIO
#define fatal_error(...) abort()
#else
// fatal_error should only be called when the API is misused
// (e.g. `NULL` argument that shouldn't be `NULL`).
static void fatal_error(PRINTF_FORMAT_STRING const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
static void
fatal_error(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
abort();
}
#endif
// Make an error with no next-error.
static pom_error *make_error(PRINTF_FORMAT_STRING const char *file, uint64_t line, const char *fmt, ...) ATTRIBUTE_PRINTF(3, 4);
static pom_error *
make_error(const char *file, uint64_t line, const char *fmt, ...) {
va_list args, args_copy;
va_start(args, fmt);
va_copy(args_copy, args);
bool bad_fmt = false;
int len = vsnprintf(NULL, 0, fmt, args);
if (len < 0 || len > INT_MAX - sizeof(pom_error) - 1) {
// Could technically happen if %s gets a really long string.
// Just use fmt as the error in this case.
bad_fmt = true;
len = strlen(fmt);
}
pom_error *err = malloc(sizeof(pom_error) + len + 1);
if (err) {
char *message = (char *)(err + 1);
if (bad_fmt) {
strcpy(message, fmt);
} else {
vsnprintf(message, len + 1, fmt, args_copy);
}
err->file = file;
err->line = line;
err->message = message;
err->next = NULL;
}
va_end(args_copy);
return err;
}
const pom_error *
pom_error_next(const pom_error *error) {
if (!error) return NULL;
return error->next;
}
const char *
pom_error_file(const pom_error *error) {
if (!error)
fatal_error("%s called with NULL argument", __func__);
return error->file;
}
uint64_t
pom_error_line(const pom_error *error) {
if (!error)
fatal_error("%s called with NULL argument", __func__);
return error->line;
}
const char *
pom_error_message(const pom_error *error) {
if (!error)
fatal_error("%s called with NULL argument", __func__);
return error->message;
}
#ifndef POM_NO_STDIO
void
pom_error_print(const pom_error *error) {
if (!error) {
fprintf(stderr, "No error.\n");
return;
}
fprintf(stderr, "Error:\n");
for (; error; error = pom_error_next(error)) {
fprintf(stderr, "%s:%" PRIu64 ": %s\n", error->file, error->line, error->message);
}
}
#endif
static void
parser_out_of_memory(struct parser *parser) {
parser->out_of_memory = true;
}
static POM__MUST_USE_L bool parser_realloc_(struct parser *parser, void *ptr, size_t elem_size, size_t *pcapacity, size_t new_capacity) POM__MUST_USE_R;
static bool
parser_realloc_(struct parser *parser, void *ptr, size_t elem_size, size_t *pcapacity, size_t new_capacity) {
size_t capacity = *pcapacity;
if (new_capacity > capacity) {
// this check is overly strict to avoid arithmetic overflow.
if (new_capacity >= SIZE_MAX / 4 / elem_size) {
parser_out_of_memory(parser);
return false;
}
// this is bad if not all pointer types have the same representation.
// I really hope we don't have to worry about that in 2025.
void **parray = ptr;
void *array = *parray;
new_capacity = new_capacity * 3 / 2 + 2;
array = realloc(array, new_capacity * elem_size);
if (!array) {
parser_out_of_memory(parser);
return false;
}
*parray = array;
*pcapacity = new_capacity;
}
return true;
}
// Strange resizing-array macro.
static void *
parser_append_(struct parser *parser, void *ptr, size_t elem_size, size_t *pcount, size_t *pcapacity,
size_t need) {
size_t old_count = *pcount;
// ensure addition below doesn't overflow
if (need >= SIZE_MAX / 8 - old_count) {
parser_out_of_memory(parser);
return NULL;
}
if (parser_realloc_(parser, ptr, elem_size, pcapacity, old_count + need)) {
*pcount += need;
return *(char **)ptr + elem_size * old_count;
} else {
return NULL;
}
}
#if __STDC_VERSION__ >= 202311
#define SAFETY_CAST_TYPEOF(t) (typeof(t))
#elif __GNUC__ >= 4
#define SAFETY_CAST_TYPEOF(t) (__typeof__(t))
#else
#define SAFETY_CAST_TYPEOF(t)
#endif
#define parser_realloc(parser, field, new_capacity) \
parser_realloc_(parser, &parser->field.array, sizeof parser->field.array[0], &parser->field.capacity, new_capacity)
// Adds room for `need` elements to the array `parser.field`,
// and returns a pointer to the first one.
#define parser_append(parser, field, need) \
SAFETY_CAST_TYPEOF(parser->field.array) parser_append_(parser, &parser->field.array, sizeof parser->field.array[0], &parser->field.count, &parser->field.capacity, need)
#define parser_append_one(parser, field) \
parser_append(parser, field, 1)
static void parser_error(struct parser *parser, PRINTF_FORMAT_STRING const char *fmt, ...) ATTRIBUTE_PRINTF(2, 3);
static void
parser_error(struct parser *parser, const char *fmt, ...) {
if (parser->out_of_memory) return;
if (parser->errors.count >= 1000) return; // don't bother at this point.
va_list args, args_copy;
va_start(args, fmt);
va_copy(args_copy, args);
bool bad_fmt = false;
int error_len = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if (error_len < 0) {
// could happen with a >INT_MAX-sized string, for example
bad_fmt = true;
error_len = strlen(fmt);
va_end(args_copy);
}
if (error_len > 1000)
error_len = 1000; // truncate very long errors
char *message = parser_append(parser, error_messages, error_len + 1);
if (!message) {
if (!bad_fmt) va_end(args_copy);
return;
}
uint32_t message_idx = message - parser->error_messages.array;
if (bad_fmt) {
// use fmt as error message
strcpy(message, fmt);
} else {
vsnprintf(message, error_len + 1, fmt, args_copy);
}
struct parser_error *error = parser_append_one(parser, errors);
if (!error) return;
error->line = parser->line_number;
error->message = message_idx;
}
// read more data into parser->buf. returns false on EOF.
static bool
parser_read_to_buf(struct parser *parser, bool skip_bom) {
if (parser->eof) return false;
uint8_t utf8_state = parser->utf8_state;
if (parser->short_read) { // last read was short, so we're at EOF
// EOF reached.
eof:
if (utf8_state) {
parser_error(parser, "Invalid UTF-8 (want continuation byte, got EOF).");
}
parser->eof = true;
return false;
}
char *buf = parser->buf;
size_t read_count = parser->read_func(parser->userdata, buf, sizeof parser->buf - 1);
parser->buf_pos = 0;
if (read_count == 0)
goto eof;
if (read_count < sizeof parser->buf - 1)
parser->short_read = true;
if (parser->leftover_cr && buf[0] != '\n')
parser_error(parser, "Carriage return with no newline after it.");
size_t in = 0, out = 0;
uint64_t original_line_number = parser->line_number;
if (skip_bom && read_count >= 3
&& (uint8_t)parser->buf[0] == 0xEF
&& (uint8_t)parser->buf[1] == 0xBB
&& (uint8_t)parser->buf[2] == 0xBF) {
// skip byte-order mark
in = 3;
}
for (; in < read_count; in++) {
uint8_t byte = buf[in];
if (utf8_state == 0) {
if (byte < 0x80) {
// ASCII
if (byte == '\r') {
if (in == read_count - 1) {
parser->leftover_cr = true;
} else if (buf[in + 1] != '\n') {
parser_error(parser, "Carriage return with no newline after it.");
}
continue;
} else if (byte == '\n') {
parser->line_number++;
} else if (byte >= 0 && byte < 32 && byte != '\t') {
parser_error(parser, "Illegal control character (ASCII code %d)", byte);
continue;
}
} else if (byte < 0xC2) {
utf8_invalid_start_byte:
parser_error(parser, "Invalid UTF-8 (invalid start byte 0x%02X)", byte);
continue;
} else if (byte < 0xE0) {
// 2-byte sequence
utf8_state = UTF8_STATE_1CONT;
} else if (byte == 0xE0) {
// 3-byte sequence; must check for overlongness
utf8_state = UTF8_STATE_2CONT_GTEQ_A0;
} else if (byte == 0xED) {
// 3-byte sequence; must check for UTF-16 surrogate
utf8_state = UTF8_STATE_2CONT_LT_A0;
} else if (byte < 0xF0) {
// 3-byte sequence
utf8_state = UTF8_STATE_2CONT;
} else if (byte == 0xF0) {
// 4-byte sequence; must check for overlongness
utf8_state = UTF8_STATE_3CONT_GTEQ_90;
} else if (byte < 0xF4) {
// 4-byte sequence
utf8_state = UTF8_STATE_3CONT;
} else if (byte == 0xF4) {
// 4-byte sequence; must check for too-big code points
utf8_state = UTF8_STATE_3CONT_LT_90;
} else {
goto utf8_invalid_start_byte;
}
} else if (utf8_state == UTF8_STATE_1CONT || utf8_state == UTF8_STATE_2CONT || utf8_state == UTF8_STATE_3CONT) {
utf8_state -= 1;
if ((byte & 0xC0) != 0x80) {
parser_error(parser, "Invalid UTF-8 (want continuation byte, got 0x%02X)", byte);
continue;
}
} else if (utf8_state == UTF8_STATE_2CONT_GTEQ_A0) {
utf8_state = UTF8_STATE_1CONT;
if (byte < 0xA0 || (byte & 0xC0) != 0x80) {
parser_error(parser, "Invalid UTF-8 (want continuation byte >= 0xA0, got 0x%02X)", byte);
continue;
}
} else if (utf8_state == UTF8_STATE_2CONT_LT_A0) {
utf8_state = UTF8_STATE_1CONT;
if (byte >= 0xA0 || (byte & 0xC0) != 0x80) {
parser_error(parser, "Invalid UTF-8 (want continuation byte < 0xA0, got 0x%02X)", byte);
continue;
}
} else if (utf8_state == UTF8_STATE_3CONT_GTEQ_90) {
utf8_state = UTF8_STATE_2CONT;
if (byte < 0x90 || (byte & 0xC0) != 0x80) {
parser_error(parser, "Invalid UTF-8 (want continuation byte >= 0x90, got 0x%02X)", byte);
continue;
}
} else if (utf8_state == UTF8_STATE_3CONT_LT_90) {
utf8_state = UTF8_STATE_2CONT;
if (byte >= 0x90 || (byte & 0xC0) != 0x80) {
parser_error(parser, "Invalid UTF-8 (want continuation byte < 0x90, got 0x%02X)", byte);
continue;
}
} else {
abort(); // should be unreachable.
}
buf[out++] = byte;
}
parser->utf8_state = utf8_state;
parser->line_number = original_line_number;
parser->buf_count = out;
return true;
}
// Reads into parser->line_buf.
static void
parser_read_line(struct parser *parser) {
if (parser->eof) {
parser->line.array[0] = 0;
return;
}
parser->line_number += 1;
size_t line_count = 0;
while (true) {
// NB: addition will not realistically overflow.
if (!parser_realloc(parser, line, line_count + sizeof parser->buf + 1))
return;
char *line = parser->line.array;
while (parser->buf_pos < parser->buf_count) {
char c = parser->buf[parser->buf_pos++];
if (c == '\n') {
line[line_count] = 0;
return;
}
line[line_count++] = c;
}
if (!parser_read_to_buf(parser, false)) {
// reached EOF
line[line_count] = 0;
return;
}
}
}
static void
strip_leading_accepted_spaces(char *s) {
size_t i;
for (i = 0; s[i] == '\t' || s[i] == ' '; i++);
memmove(s, s + i, strlen(s) + 1 - i);
}
static void
strip_trailing_accepted_spaces(char *s) {
size_t i = strlen(s);
while (i > 0) {
i--;
if (!(s[i] == '\t' || s[i] == ' ')) break;
s[i] = 0;
}
}
static void
check_valid_key(struct parser *parser, const char *key) {
uint8_t c;
if (key[0] == '.')
parser_error(parser, "Key shouldn't begin with .: %s", key);
for (size_t i = 0; (c = key[i]); i++) {
bool bad = false;
if (c < 64) {
if (c == '.') {
if (key[i+1] == 0) {
parser_error(parser, "Key shouldn't end with .: %s", key);
} else if (key[i+1] == '.') {
parser_error(parser, "Key shouldn't contain ..: %s", key);
}
}
// bitmask of disallowed ASCII characters 0-63
bad = (0xfc001bffffffffffU >> c) & 1;
} else if (c < 128) {
// bitmask of disallowed ASCII characters 64-127
bad = (0xfc0000017c000001U >> c) & 1;
}
if (bad) {
parser_error(parser, "Invalid character in key: '%c' (ASCII %d)", c, c);
}
}
}
static void
parse_quoted_value(struct parser *parser, const char *first_line) {
// TODO
abort();
}
static void
parse_line(struct parser *parser) {
parser_read_line(parser);
char *line = parser->line.array;
if (!line) return; // OOM
strip_leading_accepted_spaces(line);
if (line[0] == 0 || line[0] == '#') {
// blank line/comment
return;
}
if (line[0] == '[') {
strip_trailing_accepted_spaces(line);
size_t len = strlen(line);
if (line[len-1] != ']') {
parser_error(parser, "Missing ] to match [");
return;
}
line += 1;
len -= 2;
if (!parser_realloc(parser, current_section, len + 1))
return;
char *current_section = parser->current_section.array;
memcpy(current_section, line, len);
current_section[len] = 0;
parser->current_section.len = len;
if (len)
check_valid_key(parser, current_section);
return;
}
printf("%s|%s\n",parser->current_section.array,line);
size_t equals_idx;
for (size_t i = 0; ; i++) {
if (line[i] == '=') {
equals_idx = i;
break;
}
if (line[i] == 0) {
parser_error(parser, "Line should start with [ or contain an =");
return;
}
}
if (equals_idx == 0) {
parser_error(parser, "Expected key name before =");
return;
}
size_t key_idx = parser->string_data.count;
{
// Parse key
char *key = parser_append(parser, string_data, parser->current_section.len + 1 + equals_idx + 1);
char *p = key;
if (parser->current_section.len) {
memcpy(p, parser->current_section.array, parser->current_section.len);
p += parser->current_section.len;
*p++ = '.';
}
memcpy(p, line, equals_idx);
p[equals_idx] = 0;
strip_trailing_accepted_spaces(p);
check_valid_key(parser, key);
}
size_t value_start_idx = equals_idx + 1;
while (line[value_start_idx] == ' ' || line[value_start_idx] == '\t')
value_start_idx++;
size_t value_idx = parser->string_data.count;
if (line[value_start_idx] == '"' || line[value_start_idx] == '`') {
parse_quoted_value(parser, &line[value_start_idx]);
} else {
char *value = &line[value_start_idx];
strip_trailing_accepted_spaces(value);
size_t value_sz = strlen(value) + 1;
memcpy(parser_append(parser, string_data, value_sz),
value, value_sz);
}
struct parser_item *item = parser_append_one(parser, items);
item->key = key_idx;
item->value = value_idx;
item->line = parser->line_number;
}
static void
set_error(pom_error **error, pom_error *e) {
if (error) {
*error = e;
} else {
free(e);
}
}
static void conf_free_list_append(struct main_conf *conf, struct to_free *mem) {
mem->next = NULL;
if (conf->to_free_tail) {
conf->to_free_tail->next = mem;
conf->to_free_tail = mem;
} else {
conf->to_free_head = conf->to_free_tail = mem;
}
}
static void *conf_calloc(struct main_conf *conf, size_t nmemb, size_t sz) {
if (nmemb > SIZE_MAX / (2*sz)) return NULL;
struct to_free *mem = calloc(1, sizeof(struct to_free) + nmemb * sz);
if (!mem) return NULL;
conf_free_list_append(conf, mem);
return &mem->data[0];
}
static void conf_free(struct main_conf *conf) {
if (!conf) return;
for (struct to_free *next, *f = conf->to_free_head; f; f = next) {
next = f->next;
free(f);
}
free(conf);
}
pom_conf *
parser_finish(struct parser *parser) {
if (parser->out_of_memory || parser->errors.count) {
return NULL;
}
struct main_conf *conf = calloc(1, sizeof *conf);
if (!conf) {
parser_out_of_memory(parser);
return NULL;
}
conf->items_count = parser->items.count;
conf->items = conf_calloc(conf, conf->items_count, sizeof(struct conf_item));
char *filename = conf_calloc(conf, strlen(parser->filename) + 1, 1);
if (!conf->items || !filename) {
parser_out_of_memory(parser);
return NULL;
}
// we made room for the to_free header in pom_load.
struct to_free *string_data = (struct to_free *)parser->string_data.array;
// stop this from getting freed
parser->string_data.array = NULL;
conf_free_list_append(conf, string_data);
for (size_t i = 0; i < conf->items_count; i++) {
const struct parser_item *parser_item = &parser->items.array[i];
struct conf_item *conf_item = &conf->items[i];
conf_item->file = filename;
conf_item->line = parser_item->line;
conf_item->key = ((char*)string_data->data) + (parser_item->key - sizeof(struct to_free));
conf_item->value = ((char*)string_data->data) + (parser_item->value - sizeof(struct to_free));
conf_item->section = NULL; // TODO
}
pom_conf *root = conf_calloc(conf, 1, sizeof *root);
root->items = conf->items;
root->items_count = conf->items_count;
root->main = conf;
root->prefix_len = 0;
return root;
}
pom_conf *
pom_load(const char *filename,
size_t (*read_func)(void *userdata, char *buf, size_t len),
void *userdata, pom_error **error) {
if (!filename)
fatal_error("%s called with NULL file name", __func__);
if (!read_func)
fatal_error("%s called with NULL read function", __func__);
if (error) *error = NULL;
// Start by allocating out-of-memory error, so we can just return
// it if we run out of memory.
pom_error *out_of_memory = make_error(filename, 1, "Out of memory.");
if (!out_of_memory) return NULL;
char *current_section = calloc(1, 1);
if (!current_section) {
set_error(error, out_of_memory);
return NULL;
}
struct parser *parser = calloc(1, sizeof *parser);
if (!parser) {
free(current_section);
set_error(error, out_of_memory);
return NULL;
}
parser->filename = filename;
parser->out_of_memory_error = out_of_memory;
parser->read_func = read_func;
parser->userdata = userdata;
parser->current_section.array = current_section;
// make room for to_free header
parser_append(parser, string_data, sizeof(struct to_free));
// read into parser->buf, and skip initial BOM if present.
parser_read_to_buf(parser, true);
while (!(parser->eof || parser->out_of_memory))
parse_line(parser);
pom_conf *conf = parser_finish(parser);
if (parser->out_of_memory) {
set_error(error, out_of_memory);
} else if (parser->errors.count) {
if (error) {
// shouldn't overflow
size_t len = parser->errors.count * sizeof(pom_error) + parser->error_messages.count + strlen(filename) + 1;
// convert parser_errors to pom_error.
pom_error *errors = malloc(len);
if (errors) {
char *messages = (char *)(errors + parser->errors.count);
memcpy(messages,
parser->error_messages.array,
parser->error_messages.count);
char *filename = (char *)messages + parser->error_messages.count;
strcpy(filename, parser->filename);
for (size_t i = 0; i < parser->errors.count; i++) {
const struct parser_error *parser_error = &parser->errors.array[i];
errors[i].file = filename;
errors[i].line = parser_error->line;
errors[i].message = messages + parser_error->message;
errors[i].next = i == parser->errors.count - 1 ? NULL : &errors[i+1];
}
*error = errors;
} else {
*error = parser->out_of_memory_error;
}
}
free(parser->errors.array);
free(parser->error_messages.array);
}
if (!error || *error != out_of_memory) {
free(out_of_memory);
}
free(parser->line.array);
free(parser->current_section.array);
free(parser->string_data.array);
free(parser->items.array);
free(parser);
return conf;
}
static size_t
read_string(void *vpstring, char *buf, size_t len) {
const char **pstring = vpstring;
const char *string = *pstring;
size_t i;
for (i = 0; i < len; i++, string++) {
if (*string == 0) break;
buf[i] = *string;
}
*pstring = string;
return i;
}
pom_conf *
pom_load_string(const char *filename, const char *string, pom_error **error) {
return pom_load(filename, read_string, &string, error);
}
#ifndef POM_NO_STDIO
static size_t
read_file(void *file, char *buf, size_t len) {
return fread(buf, 1, len, file);
}
pom_conf *
pom_load_file(const char *filename, FILE *file, pom_error **error) {
if (!filename)
fatal_error("%s called with NULL file name", __func__);
if (!file)
fatal_error("%s called with NULL file", __func__);
return pom_load(filename, read_file, file, error);
}
pom_conf *
pom_load_path(const char *path, pom_error **error) {
if (!path)
fatal_error("%s called with NULL file name", __func__);
FILE *fp = fopen(path, "rb");
if (!fp) {
if (error) {
const char *message = strerror(errno);
*error = make_error(path, 1, "Couldn't open file: %s", message);
}
return NULL;
}
pom_conf *conf = pom_load_file(path, fp, error);
fclose(fp);
return conf;
}
#endif
void
pom_conf_free(pom_conf *conf) {
conf_free(conf->main);
}
static void
check_conf(const pom_conf *conf, const char *func) {
if (!conf)
fatal_error("NULL configuration passed to %s", func);
if (conf->version_number != conf->main->version_number)
fatal_error("Section obtained via pom_conf_section is being used\n"
"(in %s) after pom_conf_merge was called.\n"
"This is not allowed.", func);
}
#define check_conf(conf) check_conf(conf, __func__)
const pom_item *
pom_conf_next_item(const pom_conf *conf, pom_item_iter **p_iter) {
check_conf(conf);
if (!p_iter) fatal_error("NULL iter passed to %s", __func__);
if (!*p_iter) {
*p_iter = calloc(1, sizeof **p_iter);
if (!*p_iter) return NULL;
(*p_iter)->conf = conf;
(*p_iter)->conf_item = conf->items;
}
pom_item_iter *iter = *p_iter;
if (iter->conf != conf)
fatal_error("%s being called with inconsistent configurations for a single iterator", __func__);
if (iter->conf_item >= conf->items + conf->items_count) {
free(iter);
*p_iter = NULL;
return NULL;
}
iter->item.file = iter->conf_item->file;
iter->item.line = iter->conf_item->line;
iter->item.key = iter->conf_item->key + conf->prefix_len;
iter->item.value = iter->conf_item->value;
iter->conf_item++;
return &iter->item;
}
|