1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-22 20:33:09 +02:00

tools/: add error reporting to text2edit

This commit is contained in:
Werner Almesberger 2012-07-01 21:29:44 -03:00
parent e4d8355a2d
commit ea3f2a9d6b
4 changed files with 50 additions and 39 deletions

View File

@ -57,10 +57,9 @@ int main(int argc, char **argv)
(*last)->type = edit_nl; (*last)->type = edit_nl;
last = &(*last)->next; last = &(*last)->next;
} }
*last = text2edit(argv[i]); *last = text2edit(argv[i], &err);
if (!*last) { if (!*last) {
fprintf(stderr, "\"%s\": compilation failed\n", fprintf(stderr, "\"%s\": %s\n", argv[i], err);
argv[i]);
return 1; return 1;
} }
} }

View File

@ -200,17 +200,17 @@ static void add_string(struct edit ***last, const char *start, size_t len)
} }
static int parse_coord(struct edit *e, const char *s, static const char *parse_coord(struct edit *e, const char *s,
enum edit_type off, enum edit_type pos) enum edit_type off, enum edit_type pos)
{ {
char *end; char *end;
if (!*s) if (!s[2])
return 0; return alloc_sprintf("incomplete %c tag", s[1]);
e->u.n = strtoul(s+1, &end, 0); e->u.n = strtoul(s+3, &end, 0);
if (*end != '>') if (*end != '>')
return 0; return alloc_sprintf("invalid number in %c tag", s[1]);
switch (*s) { switch (s[2]) {
case '-': case '-':
e->u.n = -e->u.n; e->u.n = -e->u.n;
/* fall through */ /* fall through */
@ -221,17 +221,17 @@ static int parse_coord(struct edit *e, const char *s,
e->type = pos; e->type = pos;
break; break;
default: default:
return 0; return
alloc_sprintf("unrecognized positioning %c%c", s[1], s[2]);
} }
return 1; return NULL;
} }
struct edit *text2edit(const char *s) static const char *parse_edit(struct edit **edits, const char *s)
{ {
struct edit *edits = NULL, *e; struct edit *e;
struct edit **last = &edits; const char *start, *err;
const char *start;
int have_text = 0; int have_text = 0;
char *end; char *end;
@ -240,7 +240,7 @@ struct edit *text2edit(const char *s)
if (*s != '<' && *s != '\n') if (*s != '<' && *s != '\n')
continue; continue;
if (s != start) { if (s != start) {
add_string(&last, start, s-start); add_string(&edits, start, s-start);
have_text = 1; have_text = 1;
} }
start = s+1; start = s+1;
@ -251,8 +251,8 @@ struct edit *text2edit(const char *s)
e = alloc_type(struct edit); e = alloc_type(struct edit);
e->type = edit_nl; /* pick something without data */ e->type = edit_nl; /* pick something without data */
e->next = NULL; e->next = NULL;
*last = e; *edits = e;
last = &e->next; edits = &e->next;
if (*s == '\n') { if (*s == '\n') {
have_text = 0; have_text = 0;
@ -261,49 +261,66 @@ struct edit *text2edit(const char *s)
end = strchr(s, '>'); end = strchr(s, '>');
if (!end) if (!end)
goto fail; return alloc_sprintf("< without >");
switch (s[1]) { switch (s[1]) {
case 'F': case 'F':
if (strncmp(s, "<FONT ", 6)) if (strncmp(s, "<FONT ", 6))
goto fail; goto fail_tag;
e->type = edit_font; e->type = edit_font;
e->u.s = alloc_string_n(s+6, end-s-6); e->u.s = alloc_string_n(s+6, end-s-6);
break; break;
case 'I': case 'I':
if (strncmp(s, "<IMG ", 5)) if (strncmp(s, "<IMG ", 5))
goto fail; goto fail_tag;
e->type = edit_img; e->type = edit_img;
e->u.s = alloc_string_n(s+5, end-s-5); e->u.s = alloc_string_n(s+5, end-s-5);
break; break;
case 'S': case 'S':
if (strncmp(s, "<SPC ", 5)) if (strncmp(s, "<SPC ", 5))
goto fail; goto fail_tag;
e->type = edit_spc; e->type = edit_spc;
e->u.n = strtoul(s+5, &end, 0); e->u.n = strtoul(s+5, &end, 0);
if (*end != '>') if (*end != '>')
goto fail; return
alloc_sprintf("invalid number in SPC tag");
break; break;
case 'X': case 'X':
if (!parse_coord(e, s+2, edit_xoff, edit_xpos)) err = parse_coord(e, s, edit_xoff, edit_xpos);
goto fail; if (err)
return err;
break; break;
case 'Y': case 'Y':
if (!parse_coord(e, s+2, edit_yoff, edit_ypos)) err = parse_coord(e, s, edit_yoff, edit_ypos);
goto fail; if (err)
return err;
break; break;
default: default:
goto fail; goto fail_tag;
} }
s = end; s = end;
start = s+1; start = s+1;
} }
if (s != start) if (s != start)
add_string(&last, start, s-start); add_string(&edits, start, s-start);
return edits; return NULL;
fail: fail_tag:
free_edit(e); return alloc_sprintf("unrecognized tag in %.*s", end-s+1, s);
}
struct edit *text2edit(const char *s, const char **error)
{
struct edit *edits = NULL;
const char *err;
err = parse_edit(&edits, s);
if (!err)
return edits;
if (error)
*error = err;
free_edit(edits);
return NULL; return NULL;
} }

View File

@ -68,7 +68,7 @@ int draw_char(void *canvas, int width, int height,
const struct font *font, char c, int x, int y); const struct font *font, char c, int x, int y);
int char_height(const struct font *font, char c); int char_height(const struct font *font, char c);
struct edit *text2edit(const char *s); struct edit *text2edit(const char *s, const char **error);
char *edit2text(const struct edit *e); char *edit2text(const struct edit *e);
void free_edit(struct edit *e); void free_edit(struct edit *e);

View File

@ -32,12 +32,7 @@ static inline void *alloc_size(size_t size)
#define alloc_type(t) ((t *) alloc_size(sizeof(t))) #define alloc_type(t) ((t *) alloc_size(sizeof(t)))
/* static const char *alloc_sprintf(const char *fmt, ...)
* @@@ __attribute__((used)) is an ugly wait to get rid of the "unused
* function" warning. (The "unused" attribute doesn't do the trick.)
*/
static const char * __attribute__((used)) alloc_sprintf(const char *fmt, ...)
{ {
va_list ap; va_list ap;
char *tmp, *res; char *tmp, *res;