fex2bin.c 7.49 KB
Newer Older
Alejandro Mery's avatar
Alejandro Mery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright (C) 2012  Alejandro Mery <amery@geeks.cl>
 *
 * 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/>.
 */
Alejandro Mery's avatar
Alejandro Mery committed
17
#include "fex2bin.h"
Alejandro Mery's avatar
Alejandro Mery committed
18

19
#include <ctype.h>
20
#include <errno.h>
21
#include <limits.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
25
#include <string.h>

26
#define MAX_LINE	255
27

28
29
30
31
32
33
34
35
36
37
/** find first not blank char */
static inline char *skip_blank(char *p)
{
	while(isblank(*p))
		p++;
	return p;
}

/** trim out blank chars at the end of a string */
static inline char *rtrim(const char *s, char *p)
38
{
39
40
41
42
43
44
	if (p>s) {
		while (p!=s && isblank(*--p))
			;
		*++p='\0';
	}
	return p;
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
#define WORDS(S)	(((S)+(sizeof(uint32_t)-1))/(sizeof(uint32_t)))
/**
 */
static int generate_bin(FILE *UNUSED(out), const char *UNUSED(filename),
			struct script *script)
{
	size_t sections = 0, entries = 0, words = 0;
	struct list_entry *ls, *le;
	struct script_section *section;
	struct script_entry *entry;
	struct script_string_entry *string;

	/* count */
	for (ls = list_first(&script->sections); ls;
	     ls = list_next(&script->sections, ls)) {
		section = container_of(ls, struct script_section, sections);
		size_t c = 0;

		for (le = list_first(&section->entries); le;
		     le = list_next(&section->entries, le)) {
			size_t size = 0;
			entry = container_of(le, struct script_entry, entries);
			c++;

			switch(entry->type) {
			case SCRIPT_VALUE_TYPE_NULL:
			case SCRIPT_VALUE_TYPE_SINGLE_WORD:
				size = sizeof(uint32_t);
				break;
			case SCRIPT_VALUE_TYPE_STRING:
				string = container_of(entry, struct script_string_entry,
						      entry);
				size = string->l;
				break;
			case SCRIPT_VALUE_TYPE_GPIO:
				size = sizeof(struct script_bin_gpio_value);
				break;
			default:
				abort();
			}
			words += WORDS(size);
		}
		if (c>0) {
			sections++;
			entries += c;
		}
	}

	errf("sections:%zu entries:%zu data:%zu/%zu -> %zu\n", sections, entries,
	     words, words*sizeof(uint32_t),
	     sizeof(struct script_bin_head) +
	     sections*sizeof(struct script_bin_section) +
	     entries*sizeof(struct script_bin_entry) +
	     words*sizeof(uint32_t));
	return 0;
}

104
105
/**
 */
106
static int parse_fex(FILE *in, const char *filename, struct script *script)
107
108
109
{
	char buffer[MAX_LINE+1];
	int ok = 1;
110
	struct script_section *last_section = NULL;
111

112
	/* TODO: deal with longer lines correctly (specially in comments) */
113
	for(size_t line = 1; ok && fgets(buffer, sizeof(buffer), in); line++) {
114
115
		char *s = skip_blank(buffer); /* beginning */
		char *pe = s; /* \0... to be found */
116

117
		if (*pe) while (*++pe)
118
			;
119
120
121
122
123
124
125
126
127
128
129
130

		if (pe>s && pe[-1] == '\n') {
			if (pe>s+1 && pe[-2] == '\r')
				pe -= 2;
			else
				pe -= 1;
			*pe = '\0';
		}

		pe = rtrim(s, pe);

		if (pe == s || *s == ';' || *s == '#')
131
			continue; /* empty */
132
133
134
135
136
137
138
139
		else if (*s == '[') {
			/* section */
			char *p = ++s;
			while (isalnum(*p) || *p == '_')
				p++;

			if (*p == ']' && *(p+1) == '\0') {
				*p = '\0';
140
141
142
143
				if ((last_section = script_section_new(script, s)))
					continue;

				perror("malloc");
144
145
146
			} else if (*p) {
				errf("E: %s:%zu: invalid character at %zu.\n",
				     filename, line, p-buffer+1);
147
148
149
			} else {
				errf("E: %s:%zu: incomplete section declaration.\n",
				     filename, line);
150
			}
151
			ok = 0;
152
153
		} else {
			/* key = value */
154
155
156
			const char *key = s;
			char *mark, *p = s;

157
158
159
			if (!last_section) {
				errf("E: %s:%zu: data must follow a section.\n",
				     filename, line);
160
				goto parse_error;
161
162
			};

163
164
165
166
			while (isalnum(*p) || *p == '_')
				p++;
			mark = p;
			p = skip_blank(p);
167
168
			if (*p != '=')
				goto invalid_char_at_p;
169
170
171
172
173
			*mark = '\0'; /* truncate key */
			p = skip_blank(p+1);

			if (*p == '\0') {
				/* NULL */
174
175
176
				if (script_null_entry_new(last_section, key))
					continue;
				perror("malloc");
177
178
179
			} else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
				/* string */
				p++; *--pe = '\0';
180
				if (script_string_entry_new(last_section, key, pe-p, p)) {
181
#ifdef VERBOSE
182
183
184
					errf("%s.%s = \"%.*s\"\n",
					     last_section->name, key,
					     (int)(pe-p), p);
185
#endif
186
					continue;
187
				}
188
				perror("malloc");
189
190
191
			} else if (memcmp("port:P", p, 6) == 0) {
				/* GPIO */
				p += 6;
Alejandro Mery's avatar
Alejandro Mery committed
192
193
194
195
196
				if (*p < 'A' || *p > 'Z')
					;
				else {
					char *end;
					int port = *p++ - 'A';
197
					long v = strtol(p, &end, 10);
Alejandro Mery's avatar
Alejandro Mery committed
198
					if (end == p)
199
200
201
202
						goto invalid_char_at_p;
					else if (v<0 || v>255) {
						errf("E: %s:%zu: port out of range at %zu (%ld).\n",
						     filename, line, p-buffer+1, v);
Alejandro Mery's avatar
Alejandro Mery committed
203
					} else {
204
205
						int data[] = {-1,-1,-1,-1};
						int port_num = v;
Alejandro Mery's avatar
Alejandro Mery committed
206
						p = end;
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
						for (int i=0; *p && i<4; i++) {
							if (memcmp(p, "<default>", 9) == 0) {
								p += 9;
								continue;
							} else if (*p == '<') {
								v = strtol(++p, &end, 10);
								if (end == p) {
									;
								} else if (v<0 || v>INT32_MAX) {
									errf("E: %s:%zu: value out of range at %zu (%ld).\n",
									     filename, line, p-buffer+1, v);
									goto parse_error;
								} else if (*end != '>') {
									p = end;
								} else {
									p = end+1;
									data[i] = v;
									continue;
								}
							}
							break;
						}
						if (*p)
							goto invalid_char_at_p;
						if (script_gpio_entry_new(last_section, key,
									  port, port_num, data)) {
233
#ifdef VERBOSE
234
235
236
237
							errf("%s.%s = GPIO %d.%d (%d,%d,%d,%d)\n",
							     last_section->name, key,
							     port, port_num,
							     data[0], data[1], data[2], data[3]);
238
#endif
239
240
241
							continue;
						}
						perror("malloc");
Alejandro Mery's avatar
Alejandro Mery committed
242
243
					}
				}
244
245
246
247
			} else if (isdigit(*p)) {
				long long v = 0;
				char *end;
				v = strtoll(p, &end, 0);
248
249
				p = end;
				if (p != pe) {
250
					goto invalid_char_at_p;
251
252
253
				} else if (v > UINT32_MAX) {
					errf("E: %s:%zu: value out of range %lld.\n",
					     filename, line, v);
254
				} else if (script_single_entry_new(last_section, key, v)) {
255
#ifdef VERBOSE
256
257
					errf("%s.%s = %lld\n",
					     last_section->name, key, v);
258
#endif
259
					continue;
260
				}
261
			}
262
			goto parse_error;
263
264
265
266
invalid_char_at_p:
			errf("E: %s:%zu: invalid character at %zu.\n",
			     filename, line, p-buffer+1);
parse_error:
267
			ok = 0;
268
		}
269
270
	};

271
	if (ferror(in))
272
273
274
275
		ok = 0;
	return ok;
}

276
277
278
/**
 */
int main(int argc, char *argv[])
Alejandro Mery's avatar
Alejandro Mery committed
279
{
280
281
282
	int ret = -1;
	FILE *in = stdin, *out = stdout;
	const char *fn[] = {"stdin", "stdout"};
283
	struct script *script;
284

285
286
	errf("WARNING: this tool is still not functional, sorry\n");

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	if (argc>1) {
		if (strcmp(argv[1],"-") == 0)
			; /* we are using stdin anyway */
		else if ((fn[0] = argv[1]) &&
			 (in = fopen(fn[0], "r")) == NULL) {
			errf("%s: %s\n", fn[0], strerror(errno));
			goto usage;
		}

		if (argc>2) {
			fn[1] = argv[2];

			if ((out = fopen(fn[1], "w")) == NULL) {
				errf("%s: %s\n", fn[1], strerror(errno));
				goto usage;
			}
		}
	}
305
306
307
308
309
310

	if ((script = script_new()) == NULL) {
		errf("malloc: %s\n", strerror(errno));
		goto done;
	}

311
312
	if (parse_fex(in, fn[0], script) &&
	    generate_bin(out, fn[1], script))
313
		ret = 0;
314

315
	script_delete(script);
316
317
	goto done;
usage:
Alejandro Mery's avatar
Alejandro Mery committed
318
	errf("Usage: %s [<script.fex> [<script.bin>]]\n", argv[0]);
319
320

done:
321
322
	if (in && in != stdin) fclose(in);
	if (out && out != stdout) fclose(out);
323
	return ret;
Alejandro Mery's avatar
Alejandro Mery committed
324
}