Subversion Repositories Tewi

Rev

Rev 5 | Rev 7 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 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
 
6 nishi 12
struct tw_config config;
13
 
14
void tw_config_init(void) {}
15
 
16
int tw_config_read(const char* path) {
4 nishi 17
	cm_log("Config", "Reading %s", path);
18
	char cbuf[2];
19
	cbuf[1] = 0;
6 nishi 20
	int ln = 0;
4 nishi 21
	FILE* f = fopen(path, "r");
6 nishi 22
	if(f != NULL) {
4 nishi 23
		char* line = malloc(1);
24
		line[0] = 0;
6 nishi 25
		int stop = 0;
26
		char* vhost = NULL;
27
		while(stop == 0) {
4 nishi 28
			int c = fread(cbuf, 1, 1, f);
6 nishi 29
			if(cbuf[0] == '\n' || c <= 0) {
30
				ln++;
4 nishi 31
				char* l = cm_trim(line);
6 nishi 32
				if(strlen(l) > 0 && l[0] != '#') {
5 nishi 33
					char** r = cm_split(l, " \t");
34
					int i;
6 nishi 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;
5 nishi 40
							}
41
						}
6 nishi 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;
5 nishi 67
					}
68
					for(i = 0; r[i] != NULL; i++) free(r[i]);
69
					free(r);
4 nishi 70
				}
71
				free(l);
72
				free(line);
73
				line = malloc(1);
74
				line[0] = 0;
75
				if(c <= 0) break;
6 nishi 76
			} else if(cbuf[0] != '\r') {
4 nishi 77
				char* tmp = line;
78
				line = cm_strcat(tmp, cbuf);
79
				free(tmp);
80
			}
81
		}
82
		free(line);
83
		fclose(f);
6 nishi 84
		return stop;
85
	} else {
5 nishi 86
		cm_log("Config", "Could not open the file");
4 nishi 87
		return 1;
88
	}
89
}