Subversion Repositories Tewi

Rev

Rev 5 | Rev 7 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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