Commit 6eabb9cc authored by Alejandro Mery's avatar Alejandro Mery
Browse files

fex2bin: constructing the script tree... except GPIOs

parent 4d6b096c
...@@ -46,11 +46,11 @@ static inline char *rtrim(const char *s, char *p) ...@@ -46,11 +46,11 @@ static inline char *rtrim(const char *s, char *p)
/** /**
*/ */
static int parse_fex(FILE *in, const char *filename, static int parse_fex(FILE *in, const char *filename, struct script *script)
struct script *UNUSED(script))
{ {
char buffer[MAX_LINE+1]; char buffer[MAX_LINE+1];
int ok = 1; int ok = 1;
struct script_section *last_section = NULL;
/* TODO: deal with longer lines correctly (specially in comments) */ /* TODO: deal with longer lines correctly (specially in comments) */
for(size_t line = 1; fgets(buffer, sizeof(buffer), in); line++) { for(size_t line = 1; fgets(buffer, sizeof(buffer), in); line++) {
...@@ -71,7 +71,7 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -71,7 +71,7 @@ static int parse_fex(FILE *in, const char *filename,
pe = rtrim(s, pe); pe = rtrim(s, pe);
if (pe == s || *s == ';' || *s == '#') if (pe == s || *s == ';' || *s == '#')
; /* empty */ continue; /* empty */
else if (*s == '[') { else if (*s == '[') {
/* section */ /* section */
char *p = ++s; char *p = ++s;
...@@ -80,17 +80,29 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -80,17 +80,29 @@ static int parse_fex(FILE *in, const char *filename,
if (*p == ']' && *(p+1) == '\0') { if (*p == ']' && *(p+1) == '\0') {
*p = '\0'; *p = '\0';
fprintf(stdout, "[%s]\n", s); if ((last_section = script_section_new(script, s)))
continue;
perror("malloc");
} else if (*p) { } else if (*p) {
errf("E: %s:%zu: invalid character at %zu.\n", errf("E: %s:%zu: invalid character at %zu.\n",
filename, line, p-buffer+1); filename, line, p-buffer+1);
goto parse_error; } else {
errf("E: %s:%zu: incomplete section declaration.\n",
filename, line);
} }
} else { } else {
/* key = value */ /* key = value */
const char *key = s; const char *key = s;
char *mark, *p = s; char *mark, *p = s;
if (!last_section) {
errf("E: %s:%zu: data must follow a section.\n",
filename, line);
ok = 0;
break;
};
while (isalnum(*p) || *p == '_') while (isalnum(*p) || *p == '_')
p++; p++;
mark = p; mark = p;
...@@ -98,23 +110,30 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -98,23 +110,30 @@ static int parse_fex(FILE *in, const char *filename,
if (*p != '=') { if (*p != '=') {
errf("E: %s:%zu: invalid character at %zu.\n", errf("E: %s:%zu: invalid character at %zu.\n",
filename, line, p-buffer+1); filename, line, p-buffer+1);
goto parse_error; ok = 0;
break;
} }
*mark = '\0'; /* truncate key */ *mark = '\0'; /* truncate key */
p = skip_blank(p+1); p = skip_blank(p+1);
if (*p == '\0') { if (*p == '\0') {
/* NULL */ /* NULL */
fprintf(stdout, "%s = NULL\n", key); if (script_null_entry_new(last_section, key))
continue;
perror("malloc");
} else if (pe > p+1 && *p == '"' && pe[-1] == '"') { } else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
/* string */ /* string */
p++; *--pe = '\0'; p++; *--pe = '\0';
fprintf(stdout, "%s = \"%s\"\n", key, p); if (script_string_entry_new(last_section, key, pe-p, p))
continue;
perror("malloc");
} else if (memcmp("port:P", p, 6) == 0) { } else if (memcmp("port:P", p, 6) == 0) {
/* GPIO */ /* GPIO */
p += 6; p += 6;
errf("I: key:%s GPIO:%s (%zu)\n", errf("I: key:%s GPIO:%s (%zu)\n",
key, p, pe-p); key, p, pe-p);
continue;
} else if (isdigit(*p)) { } else if (isdigit(*p)) {
long long v = 0; long long v = 0;
char *end; char *end;
...@@ -122,12 +141,12 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -122,12 +141,12 @@ static int parse_fex(FILE *in, const char *filename,
if (end != pe) { if (end != pe) {
errf("E: %s:%zu: invalid character at %zu.\n", errf("E: %s:%zu: invalid character at %zu.\n",
filename, line, end-buffer+1); filename, line, end-buffer+1);
goto parse_error;
} else if (v > UINT32_MAX) { } else if (v > UINT32_MAX) {
errf("E: %s:%zu: value out of range %lld.\n", errf("E: %s:%zu: value out of range %lld.\n",
filename, line, v); filename, line, v);
} else if (script_single_entry_new(last_section, key, v)) {
continue;
} }
fprintf(stdout, "%s = %llu\n", key, v);
} else { } else {
errf("E: %s:%zu: invalid character at %zu.\n", errf("E: %s:%zu: invalid character at %zu.\n",
filename, line, p-buffer+1); filename, line, p-buffer+1);
...@@ -136,10 +155,8 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -136,10 +155,8 @@ static int parse_fex(FILE *in, const char *filename,
} }
}; };
if (ferror(in)) { if (ferror(in))
parse_error:
ok = 0; ok = 0;
}
return ok; return ok;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment