fex2bin.c 6.65 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
#include <string.h>
25
26
27
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
28

29
30
#include "script_bin.h"

31
#define MAX_LINE	255
32

33
34
35
#define pr_info(...)	fprintf(stderr, "fex2bin: " __VA_ARGS__)
#define pr_err(...)	pr_info("E: " __VA_ARGS__)

36
37
38
39
40
41
42
43
44
45
/** 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)
46
{
47
48
49
50
51
52
	if (p>s) {
		while (p!=s && isblank(*--p))
			;
		*++p='\0';
	}
	return p;
53
54
55
56
}

/**
 */
57
static int parse_fex(FILE *in, const char *filename, struct script *script)
58
59
60
{
	char buffer[MAX_LINE+1];
	int ok = 1;
61
	struct script_section *last_section = NULL;
62

63
	/* TODO: deal with longer lines correctly (specially in comments) */
64
	for(size_t line = 1; ok && fgets(buffer, sizeof(buffer), in); line++) {
65
66
		char *s = skip_blank(buffer); /* beginning */
		char *pe = s; /* \0... to be found */
67

68
		if (*pe) while (*++pe)
69
			;
70
71
72
73
74
75
76
77
78
79
80
81

		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 == '#')
82
			continue; /* empty */
83
84
85
86
87
88
89
90
		else if (*s == '[') {
			/* section */
			char *p = ++s;
			while (isalnum(*p) || *p == '_')
				p++;

			if (*p == ']' && *(p+1) == '\0') {
				*p = '\0';
91
92
93
94
				if ((last_section = script_section_new(script, s)))
					continue;

				perror("malloc");
95
96
97
			} else if (*p) {
				errf("E: %s:%zu: invalid character at %zu.\n",
				     filename, line, p-buffer+1);
98
99
100
			} else {
				errf("E: %s:%zu: incomplete section declaration.\n",
				     filename, line);
101
			}
102
			ok = 0;
103
104
		} else {
			/* key = value */
105
106
107
			const char *key = s;
			char *mark, *p = s;

108
109
110
			if (!last_section) {
				errf("E: %s:%zu: data must follow a section.\n",
				     filename, line);
111
				goto parse_error;
112
113
			};

114
115
116
117
			while (isalnum(*p) || *p == '_')
				p++;
			mark = p;
			p = skip_blank(p);
118
119
			if (*p != '=')
				goto invalid_char_at_p;
120
121
122
123
124
			*mark = '\0'; /* truncate key */
			p = skip_blank(p+1);

			if (*p == '\0') {
				/* NULL */
125
126
127
				if (script_null_entry_new(last_section, key))
					continue;
				perror("malloc");
128
129
130
			} else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
				/* string */
				p++; *--pe = '\0';
131
				if (script_string_entry_new(last_section, key, pe-p, p)) {
132
#ifdef VERBOSE
133
134
135
					errf("%s.%s = \"%.*s\"\n",
					     last_section->name, key,
					     (int)(pe-p), p);
136
#endif
137
					continue;
138
				}
139
				perror("malloc");
140
141
142
			} else if (memcmp("port:P", p, 6) == 0) {
				/* GPIO */
				p += 6;
Alejandro Mery's avatar
Alejandro Mery committed
143
144
145
146
				if (*p < 'A' || *p > 'Z')
					;
				else {
					char *end;
147
					int port = *p++ - 'A' + 1;
148
					long v = strtol(p, &end, 10);
Alejandro Mery's avatar
Alejandro Mery committed
149
					if (end == p)
150
151
152
153
						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
154
					} else {
155
156
						int data[] = {-1,-1,-1,-1};
						int port_num = v;
Alejandro Mery's avatar
Alejandro Mery committed
157
						p = end;
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
						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)) {
184
#ifdef VERBOSE
185
186
187
188
							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]);
189
#endif
190
191
192
							continue;
						}
						perror("malloc");
Alejandro Mery's avatar
Alejandro Mery committed
193
194
					}
				}
195
196
197
198
			} else if (isdigit(*p)) {
				long long v = 0;
				char *end;
				v = strtoll(p, &end, 0);
199
200
				p = end;
				if (p != pe) {
201
					goto invalid_char_at_p;
202
203
204
				} else if (v > UINT32_MAX) {
					errf("E: %s:%zu: value out of range %lld.\n",
					     filename, line, v);
205
				} else if (script_single_entry_new(last_section, key, v)) {
206
#ifdef VERBOSE
207
208
					errf("%s.%s = %lld\n",
					     last_section->name, key, v);
209
#endif
210
					continue;
211
				}
212
			}
213
			goto parse_error;
214
215
216
217
invalid_char_at_p:
			errf("E: %s:%zu: invalid character at %zu.\n",
			     filename, line, p-buffer+1);
parse_error:
218
			ok = 0;
219
		}
220
221
	};

222
	if (ferror(in))
223
224
225
226
		ok = 0;
	return ok;
}

227
228
229
/**
 */
int main(int argc, char *argv[])
Alejandro Mery's avatar
Alejandro Mery committed
230
{
231
	int ret = 1;
232
233
	FILE *in = stdin;
	int out = 1;
234
	const char *fn[] = {"stdin", "stdout"};
235
	struct script *script;
236
237
238
239
240
241
242
243
244
245
246
247
248

	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];

249
			if ((out = open(fn[1], O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
250
251
252
253
254
				errf("%s: %s\n", fn[1], strerror(errno));
				goto usage;
			}
		}
	}
255
256
257
258
259
260

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

261
262
	if (parse_fex(in, fn[0], script)) {
		size_t sections, entries, bin_size;
263
		void *bin;
264
265

		bin_size = calculate_bin_size(script, &sections, &entries);
266
267
268
269
270
271
272
		bin = calloc(1, bin_size);
		if (!bin)
			perror("malloc");
		else if (generate_bin(bin, bin_size, script, sections, entries)) {

			while(bin_size) {
				ssize_t wc = write(out, bin, bin_size);
273

274
275
276
277
278
279
280
281
282
283
284
285
				if (wc>0) {
					bin += wc;
					bin_size -= wc;
				} else if (wc < 0 && errno != EINTR) {
					pr_err("%s: write: %s\n", fn[2],
					       strerror(errno));
					break;
				}
			}
			if (bin_size == 0)
				ret = 0;
		}
286
287
	}

288
	script_delete(script);
289
290
	goto done;
usage:
Alejandro Mery's avatar
Alejandro Mery committed
291
	errf("Usage: %s [<script.fex> [<script.bin>]]\n", argv[0]);
292
293

done:
294
	if (in && in != stdin) fclose(in);
295
	if (out > 2) close(out);
296
	return ret;
Alejandro Mery's avatar
Alejandro Mery committed
297
}