1 | /* $Id: config.c 6 2024-09-13 10:28:20Z nishi $ */
|
---|
2 |
|
---|
3 | #include "tw_config.h"
|
---|
4 |
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <stdlib.h>
|
---|
7 | #include <string.h>
|
---|
8 |
|
---|
9 | #include <cm_string.h>
|
---|
10 | #include <cm_log.h>
|
---|
11 |
|
---|
12 | struct tw_config config;
|
---|
13 |
|
---|
14 | void tw_config_init(void) {}
|
---|
15 |
|
---|
16 | int tw_config_read(const char* path) {
|
---|
17 | cm_log("Config", "Reading %s", path);
|
---|
18 | char cbuf[2];
|
---|
19 | cbuf[1] = 0;
|
---|
20 | int ln = 0;
|
---|
21 | FILE* f = fopen(path, "r");
|
---|
22 | if(f != NULL) {
|
---|
23 | char* line = malloc(1);
|
---|
24 | line[0] = 0;
|
---|
25 | int stop = 0;
|
---|
26 | char* vhost = NULL;
|
---|
27 | while(stop == 0) {
|
---|
28 | int c = fread(cbuf, 1, 1, f);
|
---|
29 | if(cbuf[0] == '\n' || c <= 0) {
|
---|
30 | ln++;
|
---|
31 | char* l = cm_trim(line);
|
---|
32 | if(strlen(l) > 0 && l[0] != '#') {
|
---|
33 | char** r = cm_split(l, " \t");
|
---|
34 | int i;
|
---|
35 | if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
|
---|
36 | for(i = 1; r[i] != NULL; i++) {
|
---|
37 | if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
|
---|
38 | stop = 1;
|
---|
39 | break;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
|
---|
43 | if(vhost != NULL) {
|
---|
44 | cm_log("Config", "Already in virtual host section");
|
---|
45 | stop = 1;
|
---|
46 | } else {
|
---|
47 | if(r[1] == NULL) {
|
---|
48 | cm_log("Config", "Missing virtual host");
|
---|
49 | stop = 1;
|
---|
50 | } else {
|
---|
51 | vhost = cm_strdup(r[1]);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
|
---|
55 | if(vhost == NULL) {
|
---|
56 | cm_log("Config", "Not in virtual host section");
|
---|
57 | stop = 1;
|
---|
58 | } else {
|
---|
59 | free(vhost);
|
---|
60 | vhost = NULL;
|
---|
61 | }
|
---|
62 | } else {
|
---|
63 | if(r[0] != NULL) {
|
---|
64 | cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
|
---|
65 | }
|
---|
66 | stop = 1;
|
---|
67 | }
|
---|
68 | for(i = 0; r[i] != NULL; i++) free(r[i]);
|
---|
69 | free(r);
|
---|
70 | }
|
---|
71 | free(l);
|
---|
72 | free(line);
|
---|
73 | line = malloc(1);
|
---|
74 | line[0] = 0;
|
---|
75 | if(c <= 0) break;
|
---|
76 | } else if(cbuf[0] != '\r') {
|
---|
77 | char* tmp = line;
|
---|
78 | line = cm_strcat(tmp, cbuf);
|
---|
79 | free(tmp);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | free(line);
|
---|
83 | fclose(f);
|
---|
84 | return stop;
|
---|
85 | } else {
|
---|
86 | cm_log("Config", "Could not open the file");
|
---|
87 | return 1;
|
---|
88 | }
|
---|
89 | }
|
---|