script_fex.c 8.05 KB
Newer Older
Alejandro Mery's avatar
Alejandro Mery committed
1
2
3
4
5
/*
 * 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
Alejandro Mery's avatar
Alejandro Mery committed
6
 * the Free Software Foundation, either version 2 of the License, or
Alejandro Mery's avatar
Alejandro Mery committed
7
8
9
10
11
12
13
14
15
16
 * (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/>.
 */
17
#include "common.h"
Alejandro Mery's avatar
Alejandro Mery committed
18

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

25
26
#include "script.h"
#include "script_fex.h"
27

28
#define MAX_LINE	255
29

30
31
32
33
34
35
36
37
#define pr_info(...)	errf("fexc-fex: " __VA_ARGS__)
#define pr_err(...)	errf("E: fexc-fex: " __VA_ARGS__)

#ifdef DEBUG
#define pr_debug(...)	errf("D: fexc-fex: " __VA_ARGS__)
#else
#define pr_debug(...)
#endif
38

39
40
41
42
43
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
/*
 * generator
 */
static inline size_t strlen2(const char *s)
{
	size_t l = strlen(s);
	const char *p = &s[l-1];
	while (l && *p >= '0' && *p <= '9') {
		l--;
		p--;
	}
	return l;
}

static int find_full_match(const char *s, size_t l, const char **list)
{
	while (*list) {
		if (memcmp(s, *list, l) == 0)
			return 1;
		list++;
	}

	return 0;
}

/**
 */
static int decompile_single_mode(const char *name)
{
	static const char *hexa_entries[] = {
		"dram_baseaddr", "dram_zq", "dram_tpr", "dram_emr",
		"g2d_size",
		"rtp_press_threshold", "rtp_sensitive_level",
		"ctp_twi_addr", "csi_twi_addr", "csi_twi_addr_b", "tkey_twi_addr",
		"lcd_gamma_tbl_",
		"gsensor_twi_addr",
		NULL };
	size_t l = strlen2(name);

	if (find_full_match(name, l, hexa_entries))
		return 0;
	else
		return -1;
}

int script_generate_fex(FILE *out, const char *UNUSED(filename),
			struct script *script)
{
	struct list_entry *ls, *le;
	struct script_section *section;
	struct script_entry *entry;

	for (ls = list_first(&script->sections); ls;
	     ls = list_next(&script->sections, ls)) {
		section = container_of(ls, struct script_section, sections);

		fprintf(out, "[%s]\n", section->name);
		for (le = list_first(&section->entries); le;
		     le = list_next(&section->entries, le)) {
			entry = container_of(le, struct script_entry, entries);

			switch(entry->type) {
			case SCRIPT_VALUE_TYPE_SINGLE_WORD: {
				int mode = decompile_single_mode(entry->name);
				struct script_single_entry *single;
				single = container_of(entry, struct script_single_entry, entry);

106
				fprintf(out, "%s = ", entry->name);
107
108
109
110
111
112
113
114
115
116
117
				if (mode < 0)
					fprintf(out, "%d", single->value);
				else if (mode > 0)
					fprintf(out, "0x%0*x", mode, single->value);
				else
					fprintf(out, "0x%x", single->value);
				fputc('\n', out);
				}; break;
			case SCRIPT_VALUE_TYPE_STRING: {
				struct script_string_entry *string;
				string = container_of(entry, struct script_string_entry, entry);
118
				fprintf(out, "%s = \"%.*s\"\n", entry->name,
119
120
121
122
123
124
125
126
127
					(int)string->l, string->string);
				}; break;
			case SCRIPT_VALUE_TYPE_MULTI_WORD:
				abort();
			case SCRIPT_VALUE_TYPE_GPIO: {
				char port = 'A'-1;
				struct script_gpio_entry *gpio;
				gpio = container_of(entry, struct script_gpio_entry, entry);

Alejandro Mery's avatar
Alejandro Mery committed
128
129
130
131
132
133
134
135
				if (gpio->port == 0xffff) {
					fprintf(out, "%s = port:power%u", entry->name,
						gpio->port_num);
				} else {
					port += gpio->port;
					fprintf(out, "%s = port:P%c%02u", entry->name,
						port, gpio->port_num);
				}
136
137
138
139
140
141
142
143
144
				for (const int *p = gpio->data, *pe = p+4; p != pe; p++) {
					if (*p == -1)
						fputs("<default>", out);
					else
						fprintf(out, "<%d>", *p);
				}
				fputc('\n', out);
				}; break;
			case SCRIPT_VALUE_TYPE_NULL:
145
				fprintf(out, "%s =\n", entry->name);
146
147
148
149
150
				break;
			}
		}
		fputc('\n', out);
	}
151
	return 1;
152
153
154
155
156
157
}

/*
 * parser
 */

158
159
160
161
162
163
164
165
166
167
/** 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)
168
{
169
170
171
172
173
174
	if (p>s) {
		while (p!=s && isblank(*--p))
			;
		*++p='\0';
	}
	return p;
175
176
177
178
}

/**
 */
179
int script_parse_fex(FILE *in, const char *filename, struct script *script)
180
181
182
{
	char buffer[MAX_LINE+1];
	int ok = 1;
183
	struct script_section *last_section = NULL;
184

185
	/* TODO: deal with longer lines correctly (specially in comments) */
186
	for(size_t line = 1; ok && fgets(buffer, sizeof(buffer), in); line++) {
187
188
		char *s = skip_blank(buffer); /* beginning */
		char *pe = s; /* \0... to be found */
189

190
		if (*pe) while (*++pe)
191
			;
192
193
194
195
196
197
198
199
200
201
202
203

		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 == '#')
204
			continue; /* empty */
205
206
207
208
209
210
211
212
		else if (*s == '[') {
			/* section */
			char *p = ++s;
			while (isalnum(*p) || *p == '_')
				p++;

			if (*p == ']' && *(p+1) == '\0') {
				*p = '\0';
213
214
215
216
				if ((last_section = script_section_new(script, s)))
					continue;

				perror("malloc");
217
218
219
			} else if (*p) {
				errf("E: %s:%zu: invalid character at %zu.\n",
				     filename, line, p-buffer+1);
220
221
222
			} else {
				errf("E: %s:%zu: incomplete section declaration.\n",
				     filename, line);
223
			}
224
			ok = 0;
225
226
		} else {
			/* key = value */
227
228
229
			const char *key = s;
			char *mark, *p = s;

230
231
232
			if (!last_section) {
				errf("E: %s:%zu: data must follow a section.\n",
				     filename, line);
233
				goto parse_error;
234
235
			};

236
237
238
239
			while (isalnum(*p) || *p == '_')
				p++;
			mark = p;
			p = skip_blank(p);
240
241
			if (*p != '=')
				goto invalid_char_at_p;
242
243
244
245
246
			*mark = '\0'; /* truncate key */
			p = skip_blank(p+1);

			if (*p == '\0') {
				/* NULL */
247
248
249
				if (script_null_entry_new(last_section, key))
					continue;
				perror("malloc");
250
251
252
			} else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
				/* string */
				p++; *--pe = '\0';
253
				if (script_string_entry_new(last_section, key, pe-p, p)) {
254
255
256
					pr_debug("%s.%s = \"%.*s\"\n",
						 last_section->name, key,
						 (int)(pe-p), p);
257
					continue;
258
				}
259
				perror("malloc");
260
261
262
			} else if (memcmp("port:P", p, 6) == 0) {
				/* GPIO */
				p += 6;
Alejandro Mery's avatar
Alejandro Mery committed
263
264
265
266
				if (*p < 'A' || *p > 'Z')
					;
				else {
					char *end;
267
					int port = *p++ - 'A' + 1;
268
					long v = strtol(p, &end, 10);
Alejandro Mery's avatar
Alejandro Mery committed
269
					if (end == p)
270
271
272
273
						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
274
					} else {
275
276
						int data[] = {-1,-1,-1,-1};
						int port_num = v;
Alejandro Mery's avatar
Alejandro Mery committed
277
						p = end;
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
						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)) {
304
305
306
307
							pr_debug("%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]);
308
309
310
							continue;
						}
						perror("malloc");
Alejandro Mery's avatar
Alejandro Mery committed
311
312
					}
				}
313
314
315
316
			} else if (isdigit(*p)) {
				long long v = 0;
				char *end;
				v = strtoll(p, &end, 0);
317
318
				p = end;
				if (p != pe) {
319
					goto invalid_char_at_p;
320
321
322
				} else if (v > UINT32_MAX) {
					errf("E: %s:%zu: value out of range %lld.\n",
					     filename, line, v);
323
				} else if (script_single_entry_new(last_section, key, v)) {
324
325
					pr_debug("%s.%s = %lld\n",
						 last_section->name, key, v);
326
					continue;
327
				}
328
329
			} else {
				goto invalid_char_at_p;
330
			}
331
			goto parse_error;
332
333
334
335
invalid_char_at_p:
			errf("E: %s:%zu: invalid character at %zu.\n",
			     filename, line, p-buffer+1);
parse_error:
336
			ok = 0;
337
		}
338
339
	};

340
	if (ferror(in))
341
342
343
		ok = 0;
	return ok;
}