summaryrefslogtreecommitdiff
path: root/config.c
blob: 79e8b45d3f455e5ca6edbf1c7e157e94a4e06e3d (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "config.h"
#include "macros.h"
#include "misc.h"

char *read_file(const char *filename, long *size) {
	/* Open the file. */
	FILE *fp = fopen(filename, "r");
	/* Size of the file, in bytes. */
	long filesize = 0;
	/* Buffer of the file contents. */
	char *buf;

	/* Return NULL, if we couldn't open the file. */
	if (fp == NULL) {
		return NULL;
	}

	/* Return NULL, if we couldn't seek to the end of the file. */
	if (fseek(fp, 0L, SEEK_END)) {
		fclose(fp);
		return NULL;
	}

	/* Get the size of the file, in bytes. */
	filesize = ftell(fp);

	/* Return NULL, if the returned size is negative. */
	if (filesize < 0) {
		fclose(fp);
		return NULL;
	}

	/* Allocate enough space for the entire file, plus one. */
	buf = calloc(filesize+1, sizeof(char));

	/* Return NULL, if the buffer wasn't allocated. */
	if (buf == NULL) {
		fclose(fp);
		return NULL;
	}

	/* Seek back to the start of the file. */
	rewind(fp);
	/* Read the entire file contents into the buffer. */
	fread(buf, sizeof(char), filesize, fp);
	/* Close the file. */
	fclose(fp);

	/* Return the filesize, in bytes. */
	*size = filesize;
	/* Return the buffer. */
	return buf;
}

char *get_line(char **str) {
	char *s;
	size_t i;
	char *tmp = *str;

	for (i = 0; tmp[i] != '\n' && tmp[i] != '\0'; i++);

	s = calloc(i+1, sizeof(char));
	memcpy(s, *str, i);

	*str += (i+1);
	return s;
}

char *make_str(const char *str) {
	const size_t length = strlen(str);
	char *s = calloc(length+1, sizeof(char));
	memcpy(s, str, length+1);
	return s;
}

config_val parse_option_value(const config_opt *opt, char *value) {
	config_val val = {0};

	switch (opt->type) {
		case TYPE_INT	:
		case TYPE_BOOL	: val.i = strtol(value, NULL, 0); break;
		case TYPE_STRING: val.str = make_str(value); break;
		case TYPE_FLOAT	: val.f = strtof(value, NULL); break;
		default		: break;
	}

	return val;
}

void set_config_opt(config *conf, config_type type, size_t offset, config_val val) {
	char *cfg = (char *)conf;
	switch (type) {
		case TYPE_INT	:
		case TYPE_BOOL	: *(int *)(cfg+offset) = val.i; break;
		case TYPE_STRING: *(char **)(cfg+offset) = val.str; break;
		case TYPE_FLOAT	: *(float *)(cfg+offset) = val.f; break;
		default		: break;
	}
}

void cleanup_config(config *conf) {
	char *cfg = (char *)conf;
	for (const config_opt *opt = config_opts; opt->name != NULL; opt++) {
		/* Is this config option's type a string, and is the string of that config option in conf not NULL? */
		if (opt->type == TYPE_STRING && *(char **)(cfg+opt->offset) != NULL) {
			free(*(char **)(cfg+opt->offset));
		}
	}
	free(conf);
}

config *parse_config(const char *filename) {
	/* Size of the file, in bytes. */
	long filesize = 0;
	/* Config settings. */
	config *cfg;
	/* Buffer of the file contents. */
	char *buf = read_file(filename, &filesize);
	char *tmp = buf;

	/* Return NULL, if the buffer wasn't allocated */
	if (buf == NULL) {
		return NULL;
	}

	cfg = calloc(1, sizeof(config));

	/* Return NULL, if cfg wasn't allocated. */
	if (cfg == NULL) {
		free(buf);
		return NULL;
	}

	while (*tmp != '\0') {
		char *line = get_line(&tmp);
		char *value;
		char *name = strtok_r(line, "=", &value);
		const config_opt *opt;

		/* Check to see if the config option name we got is valid. */
		for (opt = config_opts; opt->name != NULL && strcmp(name, opt->name); opt++);

		/* Is the config option valid? */
		if (opt->name != NULL) {
			config_val val = parse_option_value(opt, value);
			if (opt->offset == offsetof(config, port)) {
				const int port = strtol(val.str, NULL, 0);
				if (port <= 0 || port > 65535) {
					log(LOG_ERR, "Invalid port %d. (Valid port must be between 1, and 65535.)", port);
					cleanup_config(cfg);
					free(line);
					free(buf);
					return NULL;
				}
			}
			set_config_opt(cfg, opt->type, opt->offset, val);
		}

		free(line);
	}

	free(buf);
	return cfg;
}