-
Notifications
You must be signed in to change notification settings - Fork 0
/
configparser.c
1532 lines (1437 loc) · 38.2 KB
/
configparser.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
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of "reprepro"
* Copyright (C) 2007 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "error.h"
#include "names.h"
#include "atoms.h"
#include "filecntl.h"
#include "configparser.h"
struct configiterator {
FILE *f;
unsigned int startline, line, column, markerline, markercolumn;
const char *filename;
const char *chunkname;
bool eol;
};
const char *config_filename(const struct configiterator *iter) {
return iter->filename;
}
unsigned int config_line(const struct configiterator *iter) {
return iter->line;
}
unsigned int config_column(const struct configiterator *iter) {
return iter->column;
}
unsigned int config_firstline(const struct configiterator *iter) {
return iter->startline;
}
unsigned int config_markerline(const struct configiterator *iter) {
return iter->markerline;
}
unsigned int config_markercolumn(const struct configiterator *iter) {
return iter->markercolumn;
}
void config_overline(struct configiterator *iter) {
int c;
while (!iter->eol) {
c = fgetc(iter->f);
if (c == '#') {
do {
c = fgetc(iter->f);
} while (c != EOF && c != '\n');
}
if (c == EOF || c == '\n')
iter->eol = true;
else
iter->column++;
}
}
bool config_nextline(struct configiterator *iter) {
int c;
assert (iter->eol);
c = fgetc(iter->f);
while (c == '#') {
do {
c = fgetc(iter->f);
} while (c != EOF && c != '\n');
iter->line++;
c = fgetc(iter->f);
}
if (c == EOF)
return false;
if (c == ' ' || c == '\t') {
iter->line++;
iter->column = 1;
iter->eol = false;
return true;
}
(void)ungetc(c, iter->f);
return false;
}
retvalue linkedlistfinish(UNUSED(void *privdata), void *this, void **last, UNUSED(bool complete), UNUSED(struct configiterator *dummy3)) {
*last = this;
return RET_NOTHING;
}
static inline retvalue finishchunk(configfinishfunction finishfunc, void *privdata, struct configiterator *iter, const struct configfield *fields, size_t fieldcount, bool *found, void **this, void **last, bool complete) {
size_t i;
retvalue r;
if (complete)
for (i = 0 ; i < fieldcount ; i++) {
if (!fields[i].required)
continue;
if (found[i])
continue;
fprintf(stderr,
"Error parsing config file %s, line %u:\n"
"Required field '%s' not found in\n"
"%s starting in line %u and ending in line %u.\n",
iter->filename, iter->line,
fields[i].name, iter->chunkname,
iter->startline, iter->line-1);
(void)finishfunc(privdata, *this, last, false, iter);
*this = NULL;
return RET_ERROR_MISSING;
}
r = finishfunc(privdata, *this, last, complete, iter);
*this = NULL;
return r;
}
char *configfile_expandname(const char *filename, char *fndup) {
const char *fromdir;
char *n;
assert (fndup == NULL || fndup == filename);
if (filename[0] == '/' || (filename[0] == '.' && filename[1] == '/'))
return fndup?fndup:strdup(filename);
if (filename[0] == '~' && filename[1] == '/') {
n = calc_dirconcat(getenv("HOME"), filename + 2);
free(fndup);
return n;
}
if (filename[0] != '+' || filename[1] == '\0' || filename[2] != '/') {
n = calc_dirconcat(global.confdir, filename);
free(fndup);
return n;
}
if (filename[1] == 'b') {
fromdir = global.basedir;
} else if (filename[1] == 'o') {
fromdir = global.outdir;
} else if (filename[1] == 'c') {
fromdir = global.confdir;
} else {
fprintf(stderr, "Warning: strange filename '%s'!\n",
filename);
return fndup?fndup:strdup(filename);
}
n = calc_dirconcat(fromdir, filename + 3);
free(fndup);
return n;
}
static retvalue configfile_parse_multi(/*@only@*/char *, bool, configinitfunction, configfinishfunction, const char *, const struct configfield *, size_t, void *, int, void **, struct strlist *);
static retvalue configfile_parse_single(/*@only@*/char *filename, bool ignoreunknown, configinitfunction initfunc, configfinishfunction finishfunc, const char *chunkname, const struct configfield *fields, size_t fieldcount, void *privdata, int depth, void **last_p, struct strlist *filenames) {
bool found[fieldcount];
void *this = NULL;
char key[100];
size_t keylen;
int c, ret;
size_t i;
struct configiterator iter;
retvalue result, r;
bool afterinclude = false;
if (strlist_in(filenames, filename)) {
if (verbose >= 0) {
fprintf(stderr,
"Ignoring subsequent inclusion of '%s'!\n", filename);
}
free(filename);
return RET_NOTHING;
}
iter.filename = filename;
r = strlist_add(filenames, filename);
if (RET_WAS_ERROR(r))
return r;
iter.chunkname = chunkname;
iter.line = 0;
iter.column = 0;
iter.f = fopen(iter.filename, "r");
if (iter.f == NULL) {
int e = errno;
fprintf(stderr, "Error opening config file '%s': %s(%d)\n",
iter.filename, strerror(e), e);
return RET_ERRNO(e);
}
result = RET_NOTHING;
do {
iter.line++;
iter.column = 1;
c = fgetc(iter.f);
while (c == '#') {
do {
c = fgetc(iter.f);
} while (c != EOF && c != '\n');
iter.line++;
c = fgetc(iter.f);
}
if (c == '\r') {
do {
c = fgetc(iter.f);
} while (c == '\r');
if (c != EOF && c != '\n') {
fprintf(stderr,
"%s:%u: error parsing configuration file: CR without following LF!\n",
iter.filename, iter.line);
result = RET_ERROR;
break;
}
}
if (c == EOF)
break;
if (c == '\n') {
afterinclude = false;
/* Ignore multiple emptye lines */
if (this == NULL)
continue;
/* finish this chunk, to get ready for the next: */
r = finishchunk(finishfunc, privdata, &iter,
fields, fieldcount, found,
&this, last_p, true);
if (RET_WAS_ERROR(r)) {
result = r;
break;
}
continue;
}
if (afterinclude) {
fprintf(stderr,
"Warning parsing %s, line %u: no empty line after '!include'-sequence"
" might cause ambiguity in the future!\n",
iter.filename, iter.line);
afterinclude = false;
}
if (c == '!') {
keylen = 0;
while ((c = fgetc(iter.f)) != EOF && c >= 'a' && c <= 'z') {
iter.column++;
key[keylen++] = c;
if (keylen >= 10)
break;
}
if (c != ':') {
fprintf(stderr,
"Error parsing %s, line %u: invalid !-sequence!\n",
iter.filename, iter.line);
result = RET_ERROR;
break;
}
iter.column++;
if (keylen == 7 && memcmp(key, "include", 7) == 0) {
char *filetoinclude;
if (this != NULL) {
fprintf(stderr,
"Error parsing %s, line %u: '!include' statement within unterminated %s!\n"
"(perhaps you forgot to put an empty line before this)\n",
iter.filename, iter.line,
chunkname);
result = RET_ERROR;
break;
}
if (depth > 20) {
fprintf(stderr,
"Error parsing %s, line %u: too many nested '!include' statements!\n",
iter.filename, iter.line);
result = RET_ERROR;
break;
}
r = config_getonlyword(&iter, "!include",
NULL, &filetoinclude);
if (RET_WAS_ERROR(r)) {
result = r;
break;
}
filetoinclude = configfile_expandname(
filetoinclude, filetoinclude);
r = configfile_parse_multi(filetoinclude,
ignoreunknown,
initfunc, finishfunc,
chunkname,
fields, fieldcount,
privdata, depth + 1,
last_p, filenames);
if (RET_WAS_ERROR(r)) {
result = r;
break;
}
afterinclude = true;
} else {
key[keylen] = '\0';
fprintf(stderr,
"Error parsing %s, line %u: unknown !-sequence '%s'!\n",
iter.filename, iter.line, key);
result = RET_ERROR;
break;
}
/* ignore all data left of this field */
do {
config_overline(&iter);
} while (config_nextline(&iter));
continue;
}
if (c == '\0') {
fprintf(stderr,
"Error parsing %s, line %u: \\000 character not allowed in config files!\n",
iter.filename, iter.line);
result = RET_ERROR;
break;
}
if (c == ' ' || c == '\t') {
fprintf(stderr,
"Error parsing %s, line %u: unexpected white space before keyword!\n",
iter.filename, iter.line);
result = RET_ERROR;
break;
}
key[0] = c;
keylen = 1;
while ((c = fgetc(iter.f)) != EOF && c != ':' && c != '\n'
&& c != '#' && c != '\0') {
iter.column++;
if (c == ' ') {
fprintf(stderr,
"Error parsing %s, line %u: Unexpected space in header name!\n",
iter.filename, iter.line);
result = RET_ERROR;
break;
}
if (c == '\t') {
fprintf(stderr,
"Error parsing %s, line %u: Unexpected tabulator character in header name!\n",
iter.filename, iter.line);
result = RET_ERROR;
break;
}
key[keylen++] = c;
if (keylen >= 100)
break;
}
if (c != ':') {
if (c != ' ' && c != '\t')
/* newline or end-of-file */
fprintf(stderr,
"Error parsing %s, line %u, column %u: Colon expected!\n",
iter.filename, iter.line, iter.column);
result = RET_ERROR;
break;
}
if (this == NULL) {
/* new chunk, initialize everything */
r = initfunc(privdata, *last_p, &this);
assert (r != RET_NOTHING);
if (RET_WAS_ERROR(r)) {
result = r;
break;
}
assert (this != NULL);
iter.startline = iter.line;
memset(found, 0, sizeof(found));
}
for (i = 0 ; i < fieldcount ; i++) {
if (keylen != fields[i].namelen)
continue;
if (strncasecmp(key, fields[i].name, keylen) != 0)
continue;
break;
}
if (i >= fieldcount) {
key[keylen] = '\0';
if (!ignoreunknown) {
fprintf(stderr,
"Error parsing %s, line %u: Unknown header '%s'!\n",
iter.filename, iter.line, key);
result = RET_ERROR_UNKNOWNFIELD;
break;
}
if (verbose >= 0)
fprintf(stderr,
"Warning parsing %s, line %u: Unknown header '%s'!\n",
iter.filename, iter.line, key);
} else if (found[i]) {
fprintf(stderr,
"Error parsing %s, line %u: Second appearance of '%s' in the same chunk!\n",
iter.filename, iter.line, fields[i].name);
result = RET_ERROR;
break;
} else
found[i] = true;
do {
c = fgetc(iter.f);
iter.column++;
} while (c == ' ' || c == '\t');
(void)ungetc(c, iter.f);
iter.eol = false;
if (i < fieldcount) {
r = fields[i].setfunc(privdata, fields[i].name, this, &iter);
RET_UPDATE(result, r);
if (RET_WAS_ERROR(r))
break;
}
/* ignore all data left of this field */
do {
config_overline(&iter);
} while (config_nextline(&iter));
} while (true);
if (this != NULL) {
r = finishchunk(finishfunc, privdata, &iter,
fields, fieldcount, found,
&this, last_p,
!RET_WAS_ERROR(result));
RET_UPDATE(result, r);
}
if (ferror(iter.f) != 0) {
int e = errno;
fprintf(stderr, "Error reading config file '%s': %s(%d)\n",
iter.filename, strerror(e), e);
r = RET_ERRNO(e);
RET_UPDATE(result, r);
}
ret = fclose(iter.f);
if (ret != 0) {
int e = errno;
fprintf(stderr, "Error closing config file '%s': %s(%d)\n",
iter.filename, strerror(e), e);
r = RET_ERRNO(e);
RET_UPDATE(result, r);
}
return result;
}
static retvalue configfile_parse_multi(/*@only@*/char *fullfilename, bool ignoreunknown, configinitfunction initfunc, configfinishfunction finishfunc, const char *chunkname, const struct configfield *fields, size_t fieldcount, void *privdata, int depth, void **last_p, struct strlist *filenames) {
retvalue result = RET_NOTHING, r;
if (isdirectory(fullfilename)) {
DIR *dir;
struct dirent *de;
int e;
char *subfilename;
dir = opendir(fullfilename);
if (dir == NULL) {
e = errno;
fprintf(stderr,
"Error %d opening directory '%s': %s\n",
e, fullfilename, strerror(e));
free(fullfilename);
return RET_ERRNO(e);
}
while ((errno = 0, de = readdir(dir)) != NULL) {
size_t l;
if (de->d_type != DT_REG && de->d_type != DT_LNK
&& de->d_type != DT_UNKNOWN)
continue;
if (de->d_name[0] == '.')
continue;
l = strlen(de->d_name);
if (l < 5 || strcmp(de->d_name + l - 5, ".conf") != 0)
continue;
subfilename = calc_dirconcat(fullfilename, de->d_name);
if (FAILEDTOALLOC(subfilename)) {
(void)closedir(dir);
free(fullfilename);
return RET_ERROR_OOM;
}
r = configfile_parse_single(subfilename, ignoreunknown,
initfunc, finishfunc,
chunkname, fields, fieldcount, privdata,
depth, last_p, filenames);
RET_UPDATE(result, r);
if (RET_WAS_ERROR(r)) {
(void)closedir(dir);
free(fullfilename);
return r;
}
}
e = errno;
if (e != 0) {
(void)closedir(dir);
fprintf(stderr,
"Error %d reading directory '%s': %s\n",
e, fullfilename, strerror(e));
free(fullfilename);
return RET_ERRNO(e);
}
if (closedir(dir) != 0) {
e = errno;
fprintf(stderr,
"Error %d closing directory '%s': %s\n",
e, fullfilename, strerror(e));
free(fullfilename);
return RET_ERRNO(e);
}
free(fullfilename);
} else {
r = configfile_parse_single(fullfilename, ignoreunknown,
initfunc, finishfunc,
chunkname, fields, fieldcount, privdata,
depth, last_p, filenames);
RET_UPDATE(result, r);
}
return result;
}
retvalue configfile_parse(const char *filename, bool ignoreunknown, configinitfunction initfunc, configfinishfunction finishfunc, const char *chunkname, const struct configfield *fields, size_t fieldcount, void *privdata) {
struct strlist filenames;
void *last = NULL;
retvalue r;
char *fullfilename;
fullfilename = configfile_expandname(filename, NULL);
if (fullfilename == NULL)
return RET_ERROR_OOM;
strlist_init(&filenames);
r = configfile_parse_multi(fullfilename, ignoreunknown,
initfunc, finishfunc,
chunkname, fields, fieldcount, privdata,
0, &last, &filenames);
/* only free filenames last, as they might still be
* referenced while running */
strlist_done(&filenames);
return r;
}
static inline int config_nextchar(struct configiterator *iter) {
int c;
unsigned int realcolumn;
c = fgetc(iter->f);
realcolumn = iter->column + 1;
if (c == '#') {
do {
c = fgetc(iter->f);
realcolumn++;
} while (c != '\n' && c != EOF && c != '\r');
}
if (c == '\r') {
while (c == '\r') {
realcolumn++;
c = fgetc(iter->f);
}
if (c != '\n' && c != EOF) {
fprintf(stderr,
"Warning parsing config file '%s', line '%u', column %u: CR not followed by LF!\n",
config_filename(iter),
config_line(iter),
realcolumn);
}
}
if (c == EOF) {
fprintf(stderr,
"Warning parsing config file '%s', line '%u': File ending without final LF!\n",
config_filename(iter),
config_line(iter));
/* fake a proper text file: */
c = '\n';
}
iter->column++;
if (c == '\n')
iter->eol = true;
return c;
}
static inline int config_nextnonspace(struct configiterator *iter) {
int c;
do {
iter->markerline = iter->line;
iter->markercolumn = iter->column;
if (iter->eol) {
if (!config_nextline(iter))
return EOF;
}
c = config_nextchar(iter);
} while (c == '\n' || c == ' ' || c == '\t');
return c;
}
int config_nextnonspaceinline(struct configiterator *iter) {
int c;
do {
iter->markerline = iter->line;
iter->markercolumn = iter->column;
if (iter->eol)
return EOF;
c = config_nextchar(iter);
if (c == '\n')
return EOF;
} while (c == '\r' || c == ' ' || c == '\t');
return c;
}
#define configparser_errorlast(iter, message, ...) \
fprintf(stderr, "Error parsing %s, line %u, column %u: " message "\n", \
iter->filename, iter->markerline, \
iter->markercolumn, ## __VA_ARGS__);
#define configparser_error(iter, message, ...) \
fprintf(stderr, "Error parsing %s, line %u, column %u: " message "\n", \
iter->filename, iter->line, \
iter->column, ## __VA_ARGS__);
retvalue config_completeword(struct configiterator *iter, char firstc, char **result_p) {
size_t size = 0, len = 0;
char *value = NULL, *nv;
int c = firstc;
iter->markerline = iter->line;
iter->markercolumn = iter->column;
do {
if (len + 2 >= size) {
nv = realloc(value, size+128);
if (FAILEDTOALLOC(nv)) {
free(value);
return RET_ERROR_OOM;
}
size += 128;
value = nv;
}
value[len] = c;
len++;
c = config_nextchar(iter);
if (c == '\n')
break;
} while (c != ' ' && c != '\t');
assert (len > 0);
assert (len < size);
value[len] = '\0';
nv = realloc(value, len+1);
if (nv == NULL)
*result_p = value;
else
*result_p = nv;
return RET_OK;
}
retvalue config_getwordinline(struct configiterator *iter, char **result_p) {
int c;
c = config_nextnonspaceinline(iter);
if (c == EOF)
return RET_NOTHING;
return config_completeword(iter, c, result_p);
}
retvalue config_getword(struct configiterator *iter, char **result_p) {
int c;
c = config_nextnonspace(iter);
if (c == EOF)
return RET_NOTHING;
return config_completeword(iter, c, result_p);
}
retvalue config_gettimespan(struct configiterator *iter, const char *header, unsigned long *time_p) {
long long currentnumber, currentsum = 0;
bool empty = true;
int c;
do {
c = config_nextnonspace(iter);
if (c == EOF) {
if (empty) {
configparser_errorlast(iter,
"Unexpected end of %s header (value expected).", header);
return RET_ERROR;
}
*time_p = currentsum;
return RET_OK;
}
iter->markerline = iter->line;
iter->markercolumn = iter->column;
currentnumber = 0;
if (c < '0' || c > '9') {
configparser_errorlast(iter,
"Unexpected character '%c' where a digit was expected in %s header.",
(char)c, header);
return RET_ERROR;
}
empty = false;
do {
if (currentnumber > 3660) {
configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
return RET_ERROR;
}
currentnumber *= 10;
currentnumber += (c - '0');
c = config_nextchar(iter);
} while (c >= '0' && c <= '9');
if (c == ' ' || c == '\t' || c == '\n')
c = config_nextnonspace(iter);
if (c == 'y') {
if (currentnumber > 100) {
configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
return RET_ERROR;
}
currentnumber *= 365*24*60*60;
} else if (c == 'm') {
if (currentnumber > 1200) {
configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
return RET_ERROR;
}
currentnumber *= 31*24*60*60;
} else if (c == 'd') {
if (currentnumber > 36600) {
configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
return RET_ERROR;
}
currentnumber *= 24*60*60;
} else {
if (currentnumber > 36600) {
configparser_errorlast(iter,
"Absurdly long time span (> 100 years) in %s header.", header);
return RET_ERROR;
}
currentnumber *= 24*60*60;
if (c != EOF) {
configparser_errorlast(iter,
"Unexpected character '%c' where a 'd','m' or 'y' was expected in %s header.",
(char)c, header);
return RET_ERROR;
}
}
currentsum += currentnumber;
} while (true);
}
retvalue config_getonlyword(struct configiterator *iter, const char *header, checkfunc check, char **result_p) {
char *value;
retvalue r;
r = config_getword(iter, &value);
if (r == RET_NOTHING) {
configparser_errorlast(iter,
"Unexpected end of %s header (value expected).", header);
return RET_ERROR;
}
if (RET_WAS_ERROR(r))
return r;
if (config_nextnonspace(iter) != EOF) {
configparser_error(iter,
"End of %s header expected (but trailing garbage).", header);
free(value);
return RET_ERROR;
}
if (check != NULL) {
const char *errormessage = check(value);
if (errormessage != NULL) {
configparser_errorlast(iter,
"Malformed %s content '%s': %s", header, value, errormessage);
free(value);
checkerror_free(errormessage);
return RET_ERROR;
}
}
*result_p = value;
return RET_OK;
}
retvalue config_getscript(struct configiterator *iter, const char *name, char **value_p) {
char *value;
retvalue r;
r = config_getonlyword(iter, name, NULL, &value);
if (RET_IS_OK(r)) {
assert (value != NULL && value[0] != '\0');
value = configfile_expandname(value, value);
if (FAILEDTOALLOC(value))
return RET_ERROR_OOM;
*value_p = value;
}
return r;
}
retvalue config_geturl(struct configiterator *iter, const char *header, char **result_p) {
char *value, *p;
retvalue r;
size_t l;
r = config_getword(iter, &value);
if (r == RET_NOTHING) {
configparser_errorlast(iter,
"Unexpected end of %s header (value expected).", header);
return RET_ERROR;
}
if (RET_WAS_ERROR(r))
return r;
// TODO: think about allowing (escaped) spaces...
if (config_nextnonspace(iter) != EOF) {
configparser_error(iter,
"End of %s header expected (but trailing garbage).", header);
free(value);
return RET_ERROR;
}
p = value;
while (*p != '\0' && (*p == '_' || *p == '-' || *p == '+' ||
(*p>='a' && *p<='z') || (*p>='A' && *p<='Z') ||
(*p>='0' && *p<='9'))) {
p++;
}
if (*p != ':') {
configparser_errorlast(iter,
"Malformed %s field: no colon (must be method:path).", header);
free(value);
return RET_ERROR;
}
if (p == value) {
configparser_errorlast(iter,
"Malformed %s field: transport method name expected (colon is not allowed to be the first character)!", header);
free(value);
return RET_ERROR;
}
p++;
l = strlen(p);
/* remove one leading slash, as we always add one and some apt-methods
* are confused with //. (end with // if you really want it) */
if (l > 0 && p[l - 1] == '/')
p[l - 1] = '\0';
*result_p = value;
return RET_OK;
}
retvalue config_getuniqwords(struct configiterator *iter, const char *header, checkfunc check, struct strlist *result_p) {
char *value;
retvalue r;
struct strlist data;
const char *errormessage;
strlist_init(&data);
while ((r = config_getword(iter, &value)) != RET_NOTHING) {
if (RET_WAS_ERROR(r)) {
strlist_done(&data);
return r;
}
if (strlist_in(&data, value)) {
configparser_errorlast(iter,
"Unexpected duplicate '%s' within %s header.", value, header);
free(value);
strlist_done(&data);
return RET_ERROR;
} else if (check != NULL && (errormessage = check(value)) != NULL) {
configparser_errorlast(iter,
"Malformed %s element '%s': %s", header, value, errormessage);
checkerror_free(errormessage);
free(value);
strlist_done(&data);
return RET_ERROR;
} else {
r = strlist_add(&data, value);
if (RET_WAS_ERROR(r)) {
strlist_done(&data);
return r;
}
}
}
strlist_move(result_p, &data);
return RET_OK;
}
retvalue config_getinternatomlist(struct configiterator *iter, const char *header, enum atom_type type, checkfunc check, struct atomlist *result_p) {
char *value;
retvalue r;
struct atomlist data;
const char *errormessage;
atom_t atom;
atomlist_init(&data);
while ((r = config_getword(iter, &value)) != RET_NOTHING) {
if (RET_WAS_ERROR(r)) {
atomlist_done(&data);
return r;
}
if (check != NULL && (errormessage = check(value)) != NULL) {
configparser_errorlast(iter,
"Malformed %s element '%s': %s", header, value, errormessage);
checkerror_free(errormessage);
free(value);
atomlist_done(&data);
return RET_ERROR;
}
r = atom_intern(type, value, &atom);
if (RET_WAS_ERROR(r))
return r;
r = atomlist_add_uniq(&data, atom);
if (r == RET_NOTHING) {
configparser_errorlast(iter,
"Unexpected duplicate '%s' within %s header.", value, header);
free(value);
atomlist_done(&data);
return RET_ERROR;
}
free(value);
if (RET_WAS_ERROR(r)) {
atomlist_done(&data);
return r;
}
}
atomlist_move(result_p, &data);
return RET_OK;
}
retvalue config_getatom(struct configiterator *iter, const char *header, enum atom_type type, atom_t *result_p) {
char *value;
retvalue r;
atom_t atom;
r = config_getword(iter, &value);
if (r == RET_NOTHING) {
configparser_errorlast(iter,
"Unexpected empty '%s' field.", header);
r = RET_ERROR_MISSING;
}
if (RET_WAS_ERROR(r))
return r;
atom = atom_find(type, value);
if (!atom_defined(atom)) {
configparser_errorlast(iter,
"Not previously seen %s '%s' within '%s' field.", atomtypes[type], value, header);
free(value);
return RET_ERROR;
}
*result_p = atom;
free(value);
return RET_OK;
}
retvalue config_getatomlist(struct configiterator *iter, const char *header, enum atom_type type, struct atomlist *result_p) {
char *value;
retvalue r;
struct atomlist data;
atom_t atom;
atomlist_init(&data);
while ((r = config_getword(iter, &value)) != RET_NOTHING) {
if (RET_WAS_ERROR(r)) {
atomlist_done(&data);
return r;
}
atom = atom_find(type, value);
if (!atom_defined(atom)) {
configparser_errorlast(iter,
"Not previously seen %s '%s' within '%s' header.", atomtypes[type], value, header);
free(value);
atomlist_done(&data);
return RET_ERROR;
}
r = atomlist_add_uniq(&data, atom);
if (r == RET_NOTHING) {
configparser_errorlast(iter,
"Unexpected duplicate '%s' within %s header.", value, header);
free(value);
atomlist_done(&data);
return RET_ERROR;
}
free(value);
if (RET_WAS_ERROR(r)) {
atomlist_done(&data);
return r;
}
}
atomlist_move(result_p, &data);
return RET_OK;
}
retvalue config_getsplitatoms(struct configiterator *iter, const char *header, enum atom_type type, struct atomlist *from_p, struct atomlist *into_p) {
char *value, *separator;
atom_t origin, destination;
retvalue r;