controller_load.c 8.15 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2009

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdbool.h>
#include "controller_load_parser.tab.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
25
#include "controller_load_parser.yy.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
26
27
#include "controller_load.h"
#include "controller_sample.h"
28
#include "controller_block.h"
29
#include "../../common/include/dynarg.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
#include <shell/shell.h>

32
33
34
35
struct controller_load_array {
	void *array;
	size_t size;
};
Jeroen Vreeken's avatar
Jeroen Vreeken committed
36

Jeroen Vreeken's avatar
Jeroen Vreeken committed
37

38
39
40
41
42
43
44
45
46
47
48
49
struct controller_load_extra {
	char *filename;
	int ret;
	bool va_list_start;
	va_list va_list;
	struct controller_load_va_entry *va_entries;
	struct controller_load_array *array;
	bool use_string;
	char *string;
	size_t stringlen;
	FILE *input_file;
};
Jeroen Vreeken's avatar
Jeroen Vreeken committed
50

51
52

int yyparse(yyscan_t scanner, FILE *);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
53

Jeroen Vreeken's avatar
Jeroen Vreeken committed
54
void yyerror(yyscan_t scanner, char const *s)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
55
{
56
57
	struct controller_load_extra *extra = yyget_extra(scanner);
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
	fprintf(stderr, "%s:%d: %s\n", 
59
	    extra->filename,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
60
61
	    yyget_lineno(scanner),
	    s);
62
	extra->ret = -1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
63
64
65
66
67
68
69
70
71
}


struct controller_load_va_entry {
	struct controller_load_va_entry *next;
	void *ptr;
};


72
void controller_load_var_add_str(char *string, yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
73
{
74
75
	struct controller_load_extra *extra = yyget_extra(scanner);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
76
77
	struct controller_load_va_entry **entryp;
	
78
79
80
	if (!extra->va_list_start) {
		va_new(extra->va_list);
		extra->va_list_start = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
81
82
	}
	
83
	va_add(extra->va_list, char *, string);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
84
	
85
	for (entryp = &extra->va_entries; *entryp; entryp = &(*entryp)->next);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
86
87
88
89
90
91
	
	*entryp = malloc(sizeof(struct controller_load_va_entry));
	(*entryp)->next = NULL;
	(*entryp)->ptr = string;
}

92
void controller_load_var_add_dbl(double dbl, yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
93
{
94
95
96
97
98
	struct controller_load_extra *extra = yyget_extra(scanner);

	if (!extra->va_list_start) {
		va_new(extra->va_list);
		extra->va_list_start = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
99
100
	}
	
101
	va_add(extra->va_list, double, dbl);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
102
103
}

104
void controller_load_var_add_flt(float flt, yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
105
{
106
107
108
109
110
111
112
113
	struct controller_load_extra *extra = yyget_extra(scanner);

	if (extra->array) {
		extra->array->array = realloc(
		    extra->array->array,
		    extra->array->size + sizeof(float));
		*(float*)(extra->array->array + extra->array->size) = flt;
		extra->array->size += sizeof(float);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
114
115
	} else {
		/* float is promoted to double in a variadic function */
116
		controller_load_var_add_dbl(flt, scanner);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
117
118
119
	}
}

120
void controller_load_var_add_float_array_start(yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
121
{
122
123
124
125
126
	struct controller_load_extra *extra = yyget_extra(scanner);

	extra->array = malloc(sizeof(struct controller_load_array));
	extra->array->array = NULL;
	extra->array->size = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
127
128
}

129
void controller_load_var_add_float_array_end(yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
130
{
131
	struct controller_load_extra *extra = yyget_extra(scanner);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
132
133
	struct controller_load_va_entry **entryp;
	
134
135
136
	if (!extra->va_list_start) {
		va_new(extra->va_list);
		extra->va_list_start = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
137
138
	}
	
139
	va_add(extra->va_list, float *, extra->array->array);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
140
	
141
	for (entryp = &extra->va_entries; *entryp; entryp = &(*entryp)->next);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
143
144
	
	*entryp = malloc(sizeof(struct controller_load_va_entry));
	(*entryp)->next = NULL;
145
	(*entryp)->ptr = extra->array->array;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
146
	
147
148
	free(extra->array);
	extra->array = NULL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
149
150
}

151
void controller_load_var_add_int(int i, yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
152
{
153
154
155
156
	struct controller_load_extra *extra = yyget_extra(scanner);
	if (!extra->va_list_start) {
		va_new(extra->va_list);
		extra->va_list_start = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
157
158
	}
	
159
	va_add(extra->va_list, int, i);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
160
161
}

162
void controller_load_var_add_ul(unsigned long ul, yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
163
{
164
165
166
167
	struct controller_load_extra *extra = yyget_extra(scanner);
	if (!extra->va_list_start) {
		va_new(extra->va_list);
		extra->va_list_start = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
168
169
	}
	
170
	va_add(extra->va_list, unsigned long, ul);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
171
172
}

173
void controller_load_var_clear(yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
174
{
175
	struct controller_load_extra *extra = yyget_extra(scanner);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
176
177
	struct controller_load_va_entry *entry, *next;
	
178
	for (entry = extra->va_entries; entry; entry = next) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
179
180
181
182
183
		if (entry->ptr)
			free(entry->ptr);
		next = entry->next;
		free(entry);
	}
184
	extra->va_entries = NULL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
185
	
186
187
188
	if (extra->va_list_start) {
		va_free(extra->va_list);
		extra->va_list_start = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
189
190
191
192
193
194
195
196
	}
}

void controller_load_frequency(double frequency)
{
	controller_sample_frequency_set(frequency);
}

197
198
199
200
201
202
int controller_load_block_create(char *type, char *name, yyscan_t scanner)
{
	struct controller_load_extra *extra = yyget_extra(scanner);

	return controller_block_create(type, name, extra->va_list);
}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
203

204
int controller_load_block_param_set(char *block, char *param, yyscan_t scanner)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
205
{
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
	struct controller_load_extra *extra = yyget_extra(scanner);

	return controller_block_param_set(block, param, extra->va_list);
}

void controller_load_include(char *file_name)
{
	int r;

	printf("Include '%s'\n", file_name);
	r = controller_load(file_name);
	printf("End of include '%s': %d\n", file_name, r);
}


int controller_load_yy_input(char *buf, int *readbytes, int sizebytes, yyscan_t scanner)
{
	struct controller_load_extra *extra = yyget_extra(scanner);

	if (!extra->use_string) {
		*readbytes = fread(buf, 1, sizebytes, extra->input_file);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
227
228
229
230
	} else {
		int copybytes;
		
		copybytes = sizebytes;
231
232
		if (copybytes > extra->stringlen)
			copybytes = extra->stringlen;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
233
		*readbytes = copybytes;
234
235
236
		memcpy(buf, extra->string, copybytes);
		extra->string += copybytes;
		extra->stringlen -= copybytes;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
237
238
239
240
241
242
243
244
245
	}
	
	return 0;
}


static int controller_load_shell(char *args, char *out, int *outlen)
{
	int ret;
246
	struct controller_load_extra extra;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
247
	char *safe_context;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
248
	yyscan_t scanner;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
249

Jeroen Vreeken's avatar
Jeroen Vreeken committed
250
251
252
253
254
255
	if (!args) {
		*outlen = sprintf(out,
		     "no arguments\n");
		return *outlen;
	}

256
257
258
259
260
261
262
	extra.ret = 0;
	extra.va_entries = NULL;
	extra.va_list_start = false;
	extra.array = NULL;
	extra.use_string = true;
	extra.stringlen = strlen(args);
	extra.string = args;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
263
264
265

	printf("parsing: %s\n", args);

266
	yylex_init_extra(&extra, &scanner);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
267

Jeroen Vreeken's avatar
Jeroen Vreeken committed
268
269
270
	safe_context = controller_block_context_get();
	controller_block_context_set("shell");

Jeroen Vreeken's avatar
Jeroen Vreeken committed
271
	ret = yyparse(scanner, NULL);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
272

Jeroen Vreeken's avatar
Jeroen Vreeken committed
273
274
	controller_block_context_set(safe_context);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
275
276
	yylex_destroy(scanner);
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
277
278
279
280
281
282
283
284
285
286
287
	if (ret) {
		*outlen = sprintf(out,
		     "failed to parse arguments\n");
	} else {
		*outlen = sprintf(out,
		     "parsed arguments\n");
	}
	
	return *outlen;
}

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
static int controller_load_file_shell(char *args, char *out, int *outlen)
{
	int ret;

	if (!args) {
		*outlen = sprintf(out,
		     "no arguments\n");
		return *outlen;
	}

	printf("loading: %s\n", args);

	ret = controller_load(args);
	
	if (ret) {
		*outlen = sprintf(out,
		     "failed to load file\n");
	} else {
		*outlen = sprintf(out,
		     "parsed file\n");
	}
	
	return *outlen;
}

static struct shell_cmd controller_cmd[] = {
	{ "controller", 
	  "parse arguments as controller commands",
	  controller_load_shell
	},
	{ "controller_file", 
	  "load controller commands from file",
	  controller_load_file_shell
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
322
323
324
325
};

int controller_load_shell_add(void)
{
326
327
328
329
330
	int ret = 0, i;
	
	for (i = 0; i < sizeof(controller_cmd)/sizeof(struct shell_cmd); i++) {
		ret |= shell_cmd_add(&controller_cmd[i]);
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
331
332
333
334
335
336
	
	return ret;
}

int controller_load(char *filename)
{
337
	struct controller_load_extra extra;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
338
339
	char *safe_context;
	yyscan_t scanner;
340
341
342
343
344
345
346
347
	
	extra.filename = filename;
	extra.ret = 0;
	extra.va_entries = NULL;
	extra.va_list_start = false;
	extra.array = NULL;
	extra.use_string = false;
	extra.stringlen = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
348

Jeroen Vreeken's avatar
Jeroen Vreeken committed
349

350
	extra.input_file = fopen(filename, "r");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
351
352
353
354
	if (!extra.input_file) {
		extra.ret = -1;
		goto err_nofile;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
355
	
356
	yylex_init_extra(&extra, &scanner);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
357

Jeroen Vreeken's avatar
Jeroen Vreeken committed
358
359
360
	safe_context = controller_block_context_get();
	controller_block_context_set(filename);

361
	extra.ret = yyparse(scanner, extra.input_file);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
362
363

	controller_block_context_set(safe_context);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
364
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
365
	yylex_destroy(scanner);
366
	fclose(extra.input_file);
367
368

	controller_block_link();
Jeroen Vreeken's avatar
Jeroen Vreeken committed
369
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
370
err_nofile:
371
	return extra.ret;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
372
}