aboutsummaryrefslogtreecommitdiff
path: root/config.c
blob: 0b57714f961597156a5f2c8ff7b25378c0fb7e5b (plain)
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
#include <string.h>
#include <stdio.h>
#include <ctype.h>

#include "websocksy.h"
#include "config.h"
#include "plugin.h"

static enum /*_config_file_section*/ {
	cfg_main,
	cfg_backend
} config_section = cfg_main;

static int config_file_line(ws_config* config, char* key, char* value, size_t line_no){
	if(!strcmp(key, "port")){
		free(config->port);
		config->port = strdup(value);
	}
	else if(!strcmp(key, "listen")){
		free(config->host);
		config->host = strdup(value);
	}
	else if(!strcmp(key, "backend")){
		//clean up the previously registered backend
		if(config->backend.cleanup){
			config->backend.cleanup();
		}
		//load the backend plugin
		if(plugin_backend_load(PLUGINS, value, &(config->backend))){
			return 1;
		}
		if(config->backend.init() != WEBSOCKSY_API_VERSION){
			fprintf(stderr, "Loaded backend %s was built for a different API version\n", value);
			return 1;
		}
	}
	return 0;
}

int config_parse_file(ws_config* config, char* filename){
	ssize_t line_current;
	size_t line_alloc = 0, line_no = 1, key_len;
	char* line = NULL, *key, *value;
	FILE* input = fopen(filename, "r");
	if(!input){
		fprintf(stderr, "Failed to open %s as configuration file\n", filename);
		return 1;
	}

	for(line_current = getline(&line, &line_alloc, input); line_current >= 0; line_current = getline(&line, &line_alloc, input)){
		if(!strncmp(line, "[core]", 6)){
			config_section = cfg_main;
		}
		else if(!strncmp(line, "[backend]", 9)){
			config_section = cfg_backend;
		}
		else if(line_current > 0){
			//right-trim newlines & spaces from value
			for(line_current--; line_current && isspace(line[line_current]); line_current--){
				line[line_current] = 0;
			}

			if(line_current > 0){
				//left-trim key
				for(key = line; *key && isspace(key[0]); key++){
				}

				value = strchr(key, '=');
				if(value){
					*value = 0;
					//left-trim value
					for(value++; *value && isspace(value[0]); value++){
					}

					//right-trim key
					for(key_len = strlen(key) - 1; *key && key_len && isspace(key[key_len]); key_len--){
						key[key_len] = 0;
					}

					switch(config_section){
						case cfg_main:
							if(config_file_line(config, key, value, line_no)){
								free(line);
								fclose(input);
								return 1;
							}
							break;
						case cfg_backend:
							if(config->backend.config(key, value)){
								fprintf(stderr, "Backend configuration failed in %s line %lu\n", filename, line_no);
								free(line);
								fclose(input);
								return 1;
							}
							break;
					}
				}
			}
		}
		line_no++;
	}

	free(line);
	fclose(input);
	return 0;
}

int config_parse_arguments(ws_config* config, int argc, char** argv){
	size_t u;
	char* option = NULL, *value = NULL;

	//if exactly one argument, treat it as config file
	if(argc == 1){
		return config_parse_file(config, argv[0]);
	}

	if(argc % 2){
		return 1;
	}

	for(u = 0; u < argc; u += 2){
		if(argv[u][0] != '-'){
			return 1;
		}
		switch(argv[u][1]){
			case 'p':
				config_file_line(config, "port", argv[u + 1], 0);
				break;
			case 'l':
				config_file_line(config, "listen", argv[u + 1], 0);
				break;
			case 'b':
				config_file_line(config, "backend", argv[u + 1], 0);
				break;
			case 'c':
				if(!strchr(argv[u + 1], '=')){
					return 1;
				}
				if(!config->backend.config){
					continue;
				}
				option = strdup(argv[u + 1]);
				value = strchr(option, '=');
				*value = 0;
				value++;
				config->backend.config(option, value);
				free(option);
				option = NULL;
				value = NULL;
				break;
		}
	}
	return 0;
}