fex2bin.c 4.22 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
static int parse_fex(FILE *in, const char *filename,
50
		     struct script *UNUSED(script))
51
52
53
54
{
	char buffer[MAX_LINE+1];
	int ok = 1;

55
	/* TODO: deal with longer lines correctly (specially in comments) */
56
	for(size_t line = 1; fgets(buffer, sizeof(buffer), in); line++) {
57
58
		char *s = skip_blank(buffer); /* beginning */
		char *pe = s; /* \0... to be found */
59

60
		if (*pe) while (*++pe)
61
			;
62
63
64
65
66
67
68
69
70
71
72
73
74

		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 == '#')
			; /* empty */
75
76
77
78
79
80
81
82
		else if (*s == '[') {
			/* section */
			char *p = ++s;
			while (isalnum(*p) || *p == '_')
				p++;

			if (*p == ']' && *(p+1) == '\0') {
				*p = '\0';
83
				fprintf(stdout, "[%s]\n", s);
84
85
86
87
88
89
90
			} else if (*p) {
				errf("E: %s:%zu: invalid character at %zu.\n",
				     filename, line, p-buffer+1);
				goto parse_error;
			}
		} else {
			/* key = value */
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
			const char *key = s;
			char *mark, *p = s;

			while (isalnum(*p) || *p == '_')
				p++;
			mark = p;
			p = skip_blank(p);
			if (*p != '=') {
				errf("E: %s:%zu: invalid character at %zu.\n",
				     filename, line, p-buffer+1);
				goto parse_error;
			}
			*mark = '\0'; /* truncate key */
			p = skip_blank(p+1);

			if (*p == '\0') {
				/* NULL */
				fprintf(stdout, "%s = NULL\n", key);
			} else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
				/* string */
				p++; *--pe = '\0';
				fprintf(stdout, "%s = \"%s\"\n", key, p);
113
114
115
116
			} else if (memcmp("port:P", p, 6) == 0) {
				/* GPIO */
				p += 6;
				errf("I: key:%s GPIO:%s (%zu)\n",
117
				     key, p, pe-p);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
			} else if (isdigit(*p)) {
				long long v = 0;
				char *end;
				v = strtoll(p, &end, 0);
				if (end != pe) {
					errf("E: %s:%zu: invalid character at %zu.\n",
					     filename, line, end-buffer+1);
					goto parse_error;
				} else if (v > UINT32_MAX) {
					errf("E: %s:%zu: value out of range %lld.\n",
					     filename, line, v);
				}
				fprintf(stdout, "%s = %llu\n", key, v);
			} else {
				errf("E: %s:%zu: invalid character at %zu.\n",
				     filename, line, p-buffer+1);
134
135
			}

136
		}
137
138
	};

139
140
	if (ferror(in)) {
parse_error:
141
		ok = 0;
142
	}
143
144
145
	return ok;
}

146
147
148
/**
 */
int main(int argc, char *argv[])
Alejandro Mery's avatar
Alejandro Mery committed
149
{
150
151
152
	int ret = -1;
	FILE *in = stdin, *out = stdout;
	const char *fn[] = {"stdin", "stdout"};
153
	struct script *script;
154

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

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
	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;
			}
		}
	}
175
176
177
178
179
180

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

181
182
183
	if (parse_fex(in, fn[0], script)) {
		ret = 0;
	}
184
	script_delete(script);
185
186
	goto done;
usage:
Alejandro Mery's avatar
Alejandro Mery committed
187
	errf("Usage: %s [<script.fex> [<script.bin>]]\n", argv[0]);
188
189

done:
190
191
	if (in && in != stdin) fclose(in);
	if (out && out != stdout) fclose(out);
192
	return ret;
Alejandro Mery's avatar
Alejandro Mery committed
193
}