controller_load.c 6.25 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 "../../common/include/dynarg.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
29
30
31
32
#include <shell/shell.h>

static char *controller_load_filename;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
33
34

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

static int controller_load_ret;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
38
void yyerror(yyscan_t scanner, char const *s)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
40
41
42
43
	fprintf(stderr, "%s:%d: %s\n", 
	    controller_load_filename,
	    yyget_lineno(scanner),
	    s);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	controller_load_ret = -1;
}


bool controller_load_va_list_start = false;
va_list controller_load_va_list;

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

static struct controller_load_va_entry *va_entries = NULL;

void controller_load_var_add_str(char *string)
{
	struct controller_load_va_entry **entryp;
	
	if (!controller_load_va_list_start) {
		va_new(controller_load_va_list);
		controller_load_va_list_start = true;
	}
	
	va_add(controller_load_va_list, char *, string);
	
	for (entryp = &va_entries; *entryp; entryp = &(*entryp)->next);
	
	*entryp = malloc(sizeof(struct controller_load_va_entry));
	(*entryp)->next = NULL;
	(*entryp)->ptr = string;
}

void controller_load_var_add_dbl(double dbl)
{
	if (!controller_load_va_list_start) {
		va_new(controller_load_va_list);
		controller_load_va_list_start = true;
	}
	
	va_add(controller_load_va_list, double, dbl);
}

struct controller_load_array {
	void *array;
	size_t size;
};

static struct controller_load_array *controller_load_array = NULL;

void controller_load_var_add_flt(float flt)
{
	if (controller_load_array) {
		controller_load_array->array = realloc(
		    controller_load_array->array,
		    controller_load_array->size + sizeof(float));
		*(float*)(controller_load_array->array + controller_load_array->size) = flt;
		controller_load_array->size += sizeof(float);
	} else {
		/* float is promoted to double in a variadic function */
		controller_load_var_add_dbl(flt);
	}
}

void controller_load_var_add_float_array_start(void)
{
	controller_load_array = malloc(sizeof(struct controller_load_array));
	controller_load_array->array = NULL;
	controller_load_array->size = 0;
}

void controller_load_var_add_float_array_end(void)
{
	struct controller_load_va_entry **entryp;
	
	if (!controller_load_va_list_start) {
		va_new(controller_load_va_list);
		controller_load_va_list_start = true;
	}
	
	va_add(controller_load_va_list, float *, controller_load_array->array);
	
	for (entryp = &va_entries; *entryp; entryp = &(*entryp)->next);
	
	*entryp = malloc(sizeof(struct controller_load_va_entry));
	(*entryp)->next = NULL;
	(*entryp)->ptr = controller_load_array->array;
	
	free(controller_load_array);
	controller_load_array = NULL;
}

void controller_load_var_add_int(int i)
{
	if (!controller_load_va_list_start) {
		va_new(controller_load_va_list);
		controller_load_va_list_start = true;
	}
	
	va_add(controller_load_va_list, int, i);
}

void controller_load_var_add_ul(unsigned long ul)
{
	if (!controller_load_va_list_start) {
		va_new(controller_load_va_list);
		controller_load_va_list_start = true;
	}
	
	va_add(controller_load_va_list, unsigned long, ul);
}

void controller_load_var_clear(void)
{
	struct controller_load_va_entry *entry, *next;
	
	for (entry = va_entries; entry; entry = next) {
		if (entry->ptr)
			free(entry->ptr);
		next = entry->next;
		free(entry);
	}
	va_entries = NULL;
	
	if (controller_load_va_list_start) {
		va_free(controller_load_va_list);
		controller_load_va_list_start = false;
	}
}

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

static bool controller_load_use_string = false;
static char *controller_load_string = NULL;
static size_t controller_load_stringlen = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
181
static FILE *controller_load_input_file;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
182
183
184
185

int controller_load_yy_input(char *buf, int *readbytes, int sizebytes)
{
	if (!controller_load_use_string) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
186
187
		*readbytes = fread(buf, 1, sizebytes, 
		    controller_load_input_file);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
	} else {
		int copybytes;
		
		copybytes = sizebytes;
		if (copybytes > controller_load_stringlen)
			copybytes = controller_load_stringlen;
		*readbytes = copybytes;
		memcpy(buf, controller_load_string, copybytes);
		controller_load_string += copybytes;
		controller_load_stringlen -= copybytes;
	}
	
	return 0;
}


static int controller_load_shell(char *args, char *out, int *outlen)
{
	int ret;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
207
	yyscan_t scanner;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
208

Jeroen Vreeken's avatar
Jeroen Vreeken committed
209
210
211
212
213
214
	if (!args) {
		*outlen = sprintf(out,
		     "no arguments\n");
		return *outlen;
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
215
216
217
218
219
220
	controller_load_string = args;
	controller_load_stringlen = strlen(args);
	controller_load_use_string = true;

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

Jeroen Vreeken's avatar
Jeroen Vreeken committed
221
222
223
	yylex_init(&scanner);

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

Jeroen Vreeken's avatar
Jeroen Vreeken committed
225
226
	yylex_destroy(scanner);
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
	if (ret) {
		*outlen = sprintf(out,
		     "failed to parse arguments\n");
	} else {
		*outlen = sprintf(out,
		     "parsed arguments\n");
	}
	
	return *outlen;
}

static struct shell_cmd controller_cmd = {
	    "controller", 
	    "parse arguments as controller commands",
	    controller_load_shell
};

int controller_load_shell_add(void)
{
	int ret;
	
	ret = shell_cmd_add(&controller_cmd);

	return ret;
}

int controller_load(char *filename)
{
	controller_load_filename = filename;
	controller_load_ret = 0;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
258
259
260
261
	yyscan_t scanner;

	controller_load_input_file = fopen(filename, "r");
	if (!controller_load_input_file)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
262
263
264
		return -1;
	controller_load_use_string = false;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
265
266
267
	yylex_init(&scanner);

	controller_load_ret = yyparse(scanner, controller_load_input_file);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
268
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
269
270
	yylex_destroy(scanner);
	fclose(controller_load_input_file);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
271
272
273
	
	return controller_load_ret;
}