aboutsummaryrefslogtreecommitdiffhomepage
path: root/config.c
blob: f03d98a9bc6075ebbbb3adf1f7f4d72db75fa8a5 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <string.h>
#include <ctype.h>
#include "midimonster.h"
#include "config.h"
#include "backend.h"

static enum {
	none,
	backend_cfg,
	instance_cfg,
	map
} parser_state = none;

typedef enum {
	map_ltr,
	map_rtl,
	map_bidir
} map_type;

static backend* current_backend = NULL;
static instance* current_instance = NULL;

static char* config_trim_line(char* in){
	ssize_t u;
	//trim front
	for(; *in && !isgraph(*in); in++){
	}

	//trim back
	for(u = strlen(in); u >= 0 && !isgraph(in[u]); u--){
		in[u] = 0;
	}

	return in;
}

static int config_map(char* to_raw, char* from_raw){
	//create a copy because the original pointer may be used multiple times
	char* to = strdup(to_raw), *from = strdup(from_raw);
	char* chanspec_to = to, *chanspec_from = from;
	instance* instance_to = NULL, *instance_from = NULL;
	channel* channel_from = NULL, *channel_to = NULL;
	int rv = 1;

	if(!from || !to){
		fprintf(stderr, "Failed to allocate memory\n");
		return 1;
	}

	//separate channel spec from instance
	for(; *chanspec_to && *chanspec_to != '.'; chanspec_to++){
	}

	for(; *chanspec_from && *chanspec_from != '.'; chanspec_from++){
	}

	if(!*chanspec_to || !*chanspec_from){
		fprintf(stderr, "Mapping does not contain a proper instance specification\n");
		goto done;
	}

	//terminate
	*chanspec_to = *chanspec_from = 0;
	chanspec_to++;
	chanspec_from++;

	//find matching instances
	instance_to = instance_match(to);
	instance_from = instance_match(from);

	if(!instance_to || !instance_from){
		fprintf(stderr, "No such instance %s\n", instance_from ? to : from);
		goto done;
	}

	channel_from = instance_from->backend->channel(instance_from, chanspec_from);
	channel_to = instance_to->backend->channel(instance_to, chanspec_to);

	if(!channel_from || !channel_to){
		fprintf(stderr, "Failed to parse channel specifications\n");
		goto done;
	}

	rv = mm_map_channel(channel_from, channel_to);
done:
	free(from);
	free(to);
	return rv;
}

int config_read(char* cfg_file){
	int rv = 1;
	size_t line_alloc = 0;
	ssize_t status;
	map_type mapping_type = map_rtl;
	char* line_raw = NULL, *line, *separator;
	FILE* source = fopen(cfg_file, "r");
	if(!source){
		fprintf(stderr, "Failed to open configuration file for reading\n");
		return 1;
	}

	for(status = getline(&line_raw, &line_alloc, source); status >= 0; status = getline(&line_raw, &line_alloc, source)){
		line = config_trim_line(line_raw);
		if(*line == ';' || strlen(line) == 0){
			//skip comments
			continue;
		}
		if(*line == '[' && line[strlen(line) - 1] == ']'){
			if(!strncmp(line, "[backend ", 9)){
				//backend configuration
				parser_state = backend_cfg;
				line[strlen(line) - 1] = 0;
				current_backend = backend_match(line + 9);

				if(!current_backend){
					fprintf(stderr, "Cannot configure unknown backend %s\n", line + 9);
					goto bail;
				}
			}
			else if(!strcmp(line, "[map]")){
				//mapping configuration
				parser_state = map;
			}
			else{
				//backend instance configuration
				parser_state = instance_cfg;
				
				//trim braces
				line[strlen(line) - 1] = 0;
				line++;

				//find separating space and terminate
				for(separator = line; *separator && *separator != ' '; separator++){
				}
				if(!*separator){
					fprintf(stderr, "No instance name specified for backend %s\n", line);
					goto bail;
				}
				*separator = 0;
				separator++;

				current_backend = backend_match(line);
				if(!current_backend){
					fprintf(stderr, "No such backend %s\n", line);
					goto bail;
				}

				if(instance_match(separator)){
					fprintf(stderr, "Duplicate instance name %s\n", separator);
					goto bail;
				}

				//validate instance name
				if(strchr(separator, ' ') || strchr(separator, '.')){
					fprintf(stderr, "Invalid instance name %s\n", separator);
					goto bail;
				}

				current_instance = current_backend->create();
				if(!current_instance){
					fprintf(stderr, "Failed to instantiate backend %s\n", line);
					goto bail;
				}

				current_instance->name = strdup(separator);
				current_instance->backend = current_backend;
				fprintf(stderr, "Created %s instance %s\n", line, separator);
			}
		}
		else if(parser_state == map){
			mapping_type = map_rtl;
			//find separator
			for(separator = line; *separator && *separator != '<' && *separator != '>'; separator++){
			}

			switch(*separator){
				case '>':
					mapping_type = map_ltr;
					//fall through
				case '<': //default
					*separator = 0;
					separator++;
					break;
				case 0:
				default:
					fprintf(stderr, "Not a channel mapping: %s\n", line);
					goto bail;
			}

			if((mapping_type == map_ltr && *separator == '<')
					|| (mapping_type == map_rtl && *separator == '>')){
				mapping_type = map_bidir;
				separator++;
			}

			line = config_trim_line(line);
			separator = config_trim_line(separator);

			if(mapping_type == map_ltr || mapping_type == map_bidir){
				if(config_map(separator, line)){
					fprintf(stderr, "Failed to map channel %s to %s\n", line, separator);
					goto bail;
				}
			}
			if(mapping_type == map_rtl || mapping_type == map_bidir){
				if(config_map(line, separator)){
					fprintf(stderr, "Failed to map channel %s to %s\n", separator, line);
					goto bail;
				}
			}
		}
		else{
			//pass to parser
			//find separator
			separator = strchr(line, '=');
			if(!separator){
				fprintf(stderr, "Not an assignment: %s\n", line);
				goto bail;
			}

			*separator = 0;
			separator++;
			line = config_trim_line(line);
			separator = config_trim_line(separator);

			if(parser_state == backend_cfg && current_backend->conf(line, separator)){
				fprintf(stderr, "Failed to configure backend %s\n", current_backend->name);
				goto bail;
			}
			else if(parser_state == instance_cfg && current_backend->conf_instance(current_instance, line, separator)){
				fprintf(stderr, "Failed to configure instance %s\n", current_instance->name);
				goto bail;
			}
		}
	}

	rv = 0;
bail:
	fclose(source);
	free(line_raw);
	return rv;
}