2012-05-02 09:35:52 +02:00
|
|
|
/*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
2012-05-04 00:47:29 +02:00
|
|
|
#include "fex2bin.h"
|
2012-05-02 09:35:52 +02:00
|
|
|
|
2012-05-05 02:13:37 +02:00
|
|
|
#include <ctype.h>
|
2012-05-04 10:37:14 +02:00
|
|
|
#include <errno.h>
|
2012-05-05 20:06:23 +02:00
|
|
|
#include <limits.h>
|
2012-05-04 10:37:14 +02:00
|
|
|
#include <stdio.h>
|
2012-05-05 20:06:23 +02:00
|
|
|
#include <stdlib.h>
|
2012-05-04 10:37:14 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2012-05-05 02:47:45 +02:00
|
|
|
#define MAX_LINE 255
|
2012-05-04 14:02:10 +02:00
|
|
|
|
2012-05-05 18:55:09 +02:00
|
|
|
/** 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)
|
2012-05-04 14:02:10 +02:00
|
|
|
{
|
2012-05-05 18:55:09 +02:00
|
|
|
if (p>s) {
|
|
|
|
|
while (p!=s && isblank(*--p))
|
|
|
|
|
;
|
|
|
|
|
*++p='\0';
|
|
|
|
|
}
|
|
|
|
|
return p;
|
2012-05-04 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
2012-05-06 16:24:26 +02:00
|
|
|
static int parse_fex(FILE *in, const char *filename, struct script *script)
|
2012-05-04 14:02:10 +02:00
|
|
|
{
|
|
|
|
|
char buffer[MAX_LINE+1];
|
|
|
|
|
int ok = 1;
|
2012-05-06 16:24:26 +02:00
|
|
|
struct script_section *last_section = NULL;
|
2012-05-04 14:02:10 +02:00
|
|
|
|
2012-05-05 02:47:45 +02:00
|
|
|
/* TODO: deal with longer lines correctly (specially in comments) */
|
2012-05-06 20:29:36 +02:00
|
|
|
for(size_t line = 1; ok && fgets(buffer, sizeof(buffer), in); line++) {
|
2012-05-05 18:55:09 +02:00
|
|
|
char *s = skip_blank(buffer); /* beginning */
|
|
|
|
|
char *pe = s; /* \0... to be found */
|
2012-05-05 02:47:45 +02:00
|
|
|
|
2012-05-05 18:55:09 +02:00
|
|
|
if (*pe) while (*++pe)
|
2012-05-05 02:47:45 +02:00
|
|
|
;
|
2012-05-05 18:55:09 +02:00
|
|
|
|
|
|
|
|
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 == '#')
|
2012-05-06 16:24:26 +02:00
|
|
|
continue; /* empty */
|
2012-05-05 02:47:45 +02:00
|
|
|
else if (*s == '[') {
|
|
|
|
|
/* section */
|
|
|
|
|
char *p = ++s;
|
|
|
|
|
while (isalnum(*p) || *p == '_')
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
if (*p == ']' && *(p+1) == '\0') {
|
|
|
|
|
*p = '\0';
|
2012-05-06 16:24:26 +02:00
|
|
|
if ((last_section = script_section_new(script, s)))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
perror("malloc");
|
2012-05-05 02:47:45 +02:00
|
|
|
} else if (*p) {
|
|
|
|
|
errf("E: %s:%zu: invalid character at %zu.\n",
|
|
|
|
|
filename, line, p-buffer+1);
|
2012-05-06 16:24:26 +02:00
|
|
|
} else {
|
|
|
|
|
errf("E: %s:%zu: incomplete section declaration.\n",
|
|
|
|
|
filename, line);
|
2012-05-05 02:47:45 +02:00
|
|
|
}
|
2012-05-06 20:29:36 +02:00
|
|
|
ok = 0;
|
2012-05-05 02:47:45 +02:00
|
|
|
} else {
|
|
|
|
|
/* key = value */
|
2012-05-05 18:55:09 +02:00
|
|
|
const char *key = s;
|
|
|
|
|
char *mark, *p = s;
|
|
|
|
|
|
2012-05-06 16:24:26 +02:00
|
|
|
if (!last_section) {
|
|
|
|
|
errf("E: %s:%zu: data must follow a section.\n",
|
|
|
|
|
filename, line);
|
2012-05-06 20:37:53 +02:00
|
|
|
goto parse_error;
|
2012-05-06 16:24:26 +02:00
|
|
|
};
|
|
|
|
|
|
2012-05-05 18:55:09 +02:00
|
|
|
while (isalnum(*p) || *p == '_')
|
|
|
|
|
p++;
|
|
|
|
|
mark = p;
|
|
|
|
|
p = skip_blank(p);
|
2012-05-06 20:37:53 +02:00
|
|
|
if (*p != '=')
|
|
|
|
|
goto invalid_char_at_p;
|
2012-05-05 18:55:09 +02:00
|
|
|
*mark = '\0'; /* truncate key */
|
|
|
|
|
p = skip_blank(p+1);
|
|
|
|
|
|
|
|
|
|
if (*p == '\0') {
|
|
|
|
|
/* NULL */
|
2012-05-06 16:24:26 +02:00
|
|
|
if (script_null_entry_new(last_section, key))
|
|
|
|
|
continue;
|
|
|
|
|
perror("malloc");
|
2012-05-05 18:55:09 +02:00
|
|
|
} else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
|
|
|
|
|
/* string */
|
|
|
|
|
p++; *--pe = '\0';
|
2012-05-06 18:51:13 +02:00
|
|
|
if (script_string_entry_new(last_section, key, pe-p, p)) {
|
|
|
|
|
errf("%s.%s = \"%.*s\"\n",
|
|
|
|
|
last_section->name, key,
|
|
|
|
|
(int)(pe-p), p);
|
2012-05-06 16:24:26 +02:00
|
|
|
continue;
|
2012-05-06 18:51:13 +02:00
|
|
|
}
|
2012-05-06 16:24:26 +02:00
|
|
|
|
|
|
|
|
perror("malloc");
|
2012-05-05 20:06:23 +02:00
|
|
|
} else if (memcmp("port:P", p, 6) == 0) {
|
|
|
|
|
/* GPIO */
|
|
|
|
|
p += 6;
|
2012-05-06 16:51:39 +02:00
|
|
|
if (*p < 'A' || *p > 'Z')
|
|
|
|
|
;
|
|
|
|
|
else {
|
|
|
|
|
char *end;
|
|
|
|
|
int port = *p++ - 'A';
|
|
|
|
|
long port_num = strtol(p, &end, 10);
|
|
|
|
|
if (end == p)
|
|
|
|
|
;
|
|
|
|
|
else if (port_num<0 || port_num>255) {
|
|
|
|
|
errf("E: %s:%zu: port out of range at %zu.\n",
|
|
|
|
|
filename, line, p-buffer+1);
|
2012-05-06 20:37:53 +02:00
|
|
|
goto parse_error;
|
2012-05-06 16:51:39 +02:00
|
|
|
} else {
|
|
|
|
|
p = end;
|
|
|
|
|
errf("%s.%s = GPIO %d.%ld (%s)\n",
|
|
|
|
|
last_section->name, key,
|
|
|
|
|
port, port_num, p);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-05 20:06:23 +02:00
|
|
|
} else if (isdigit(*p)) {
|
|
|
|
|
long long v = 0;
|
|
|
|
|
char *end;
|
|
|
|
|
v = strtoll(p, &end, 0);
|
2012-05-06 20:37:53 +02:00
|
|
|
p = end;
|
|
|
|
|
if (p != pe) {
|
|
|
|
|
;
|
2012-05-05 20:06:23 +02:00
|
|
|
} else if (v > UINT32_MAX) {
|
|
|
|
|
errf("E: %s:%zu: value out of range %lld.\n",
|
|
|
|
|
filename, line, v);
|
2012-05-06 20:37:53 +02:00
|
|
|
goto parse_error;
|
2012-05-06 16:24:26 +02:00
|
|
|
} else if (script_single_entry_new(last_section, key, v)) {
|
2012-05-06 18:51:13 +02:00
|
|
|
errf("%s.%s = %lld\n",
|
|
|
|
|
last_section->name, key, v);
|
2012-05-06 16:24:26 +02:00
|
|
|
continue;
|
2012-05-05 20:06:23 +02:00
|
|
|
}
|
2012-05-05 18:55:09 +02:00
|
|
|
}
|
2012-05-06 20:37:53 +02:00
|
|
|
invalid_char_at_p:
|
|
|
|
|
errf("E: %s:%zu: invalid character at %zu.\n",
|
|
|
|
|
filename, line, p-buffer+1);
|
|
|
|
|
parse_error:
|
2012-05-06 20:29:36 +02:00
|
|
|
ok = 0;
|
2012-05-05 02:47:45 +02:00
|
|
|
}
|
2012-05-04 14:02:10 +02:00
|
|
|
};
|
|
|
|
|
|
2012-05-06 16:24:26 +02:00
|
|
|
if (ferror(in))
|
2012-05-04 14:02:10 +02:00
|
|
|
ok = 0;
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-04 10:37:14 +02:00
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
int main(int argc, char *argv[])
|
2012-05-02 09:35:52 +02:00
|
|
|
{
|
2012-05-04 10:37:14 +02:00
|
|
|
int ret = -1;
|
|
|
|
|
FILE *in = stdin, *out = stdout;
|
|
|
|
|
const char *fn[] = {"stdin", "stdout"};
|
2012-05-04 11:32:40 +02:00
|
|
|
struct script *script;
|
2012-05-04 10:37:14 +02:00
|
|
|
|
2012-05-06 12:50:00 +02:00
|
|
|
errf("WARNING: this tool is still not functional, sorry\n");
|
|
|
|
|
|
2012-05-04 10:37:14 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-04 11:32:40 +02:00
|
|
|
|
|
|
|
|
if ((script = script_new()) == NULL) {
|
|
|
|
|
errf("malloc: %s\n", strerror(errno));
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-04 14:02:10 +02:00
|
|
|
if (parse_fex(in, fn[0], script)) {
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
2012-05-04 11:32:40 +02:00
|
|
|
script_delete(script);
|
2012-05-04 10:37:14 +02:00
|
|
|
goto done;
|
|
|
|
|
usage:
|
2012-05-04 23:24:31 +02:00
|
|
|
errf("Usage: %s [<script.fex> [<script.bin>]]\n", argv[0]);
|
2012-05-04 10:37:14 +02:00
|
|
|
|
|
|
|
|
done:
|
2012-05-04 14:02:10 +02:00
|
|
|
if (in && in != stdin) fclose(in);
|
|
|
|
|
if (out && out != stdout) fclose(out);
|
2012-05-04 10:37:14 +02:00
|
|
|
return ret;
|
2012-05-02 09:35:52 +02:00
|
|
|
}
|