From 4feae9674e99027828a41634060c167679e86063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 4 Feb 2019 21:21:50 +0100 Subject: Improve ERR. --- err.h | 6 +++++- parse.c | 18 +++++++++--------- parse.h | 2 -- strbuf.c | 3 +-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/err.h b/err.h index d3db805c..5689721b 100644 --- a/err.h +++ b/err.h @@ -2,6 +2,10 @@ #define ERR_H #include -#define ERR(s) fprintf(stderr, "ERR (%s:%i): %s\n", __FILE__, __LINE__, s) +#define RED "\x1B[0;31m" +#define RESET "\x1b[m" +#define ERR(msg) fprintf(stderr, RED "ERR" RESET " (%s:%i) %s\n", __FILE__, __LINE__, #msg) +#define ERR_F(fmt, ...) fprintf(stderr, RED "ERR" RESET " (%s:%i) " fmt "\n", \ + __FILE__, __LINE__, ##__VA_ARGS__) #endif /* ERR_H */ diff --git a/parse.c b/parse.c index 68cd4b60..a5f3206d 100644 --- a/parse.c +++ b/parse.c @@ -6,6 +6,8 @@ #include "macro.h" #include "vcal.h" +#include "err.h" + int parse_file(char* fname, FILE* f, vcalendar* cal) { int segments = 1; SNEW(strbuf, str, segments * SEGSIZE); @@ -42,7 +44,7 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) { s[0] = fgetc(f); s[1] = fgetc(f); - if (s[0] != '\n') { ERR("expected newline after CR", line); } + if (s[0] != '\n') { ERR_F("%s, %i", "expected newline after CR", line); } else if (s[1] == ' ' || s[1] == '\t') { /* Folded line, increase size of key and continue. */ @@ -51,14 +53,14 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) { * encountered. */ if (strbuf_realloc(&str, ++segments * SEGSIZE) != 0) { - ERR("Failed to realloc strbuf", line); + ERR_F("%s, %i", "Failed to realloc strbuf", line); exit (1); } continue; } else { /* Actuall end of line, handle values. */ if (ungetc(s[1], f) != s[1]) { - ERR("Failed to put character back on FILE", line); + ERR_F("%s, %i", "Failed to put character back on FILE", line); exit (2); } @@ -106,14 +108,12 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) { } if (! feof(f)) { - ERR("Error parsing", errno); + ERR("Error parsing"); } else { /* * The standard (3.4, l. 2675) says that each icalobject must * end with CRLF. My files however does not, so we also parse * the end here. - * TODO -- this doesn't do anything with its read value - * TODO This might crash if we have the CRLF */ if (str.ptr + 1 > vallen) { vallen = str.ptr + 1; @@ -152,7 +152,7 @@ int handle_kv( case s_none: if (! (strbuf_c(&cline->key, "BEGIN") && strbuf_c(cline->vals.cur->value, "VCALENDAR"))) { - ERR("Invalid start of calendar", line); + ERR_F("%s, %i\n%s", "Invalid start of calendar", line, ev->filename); return -1; } ctx->scope = s_calendar; @@ -179,7 +179,7 @@ int handle_kv( case s_event: if (ev == NULL) { - ERR("Something has gone terribly wrong", line); + ERR_F("%s, %i", "Something has gone terribly wrong", line); return -5; } if (strbuf_c(&cline->key, "END")) { @@ -188,7 +188,7 @@ int handle_kv( ctx->scope = s_calendar; return ctx->scope; } else { - ERR("Trying to end something, expected VEVENT", line); + ERR_F("%s, %i", "Trying to end something, expected VEVENT", line); return -3; } } else { diff --git a/parse.h b/parse.h index 667ccd35..2752256a 100644 --- a/parse.h +++ b/parse.h @@ -16,8 +16,6 @@ */ #define SEGSIZE 75 -#define ERR(x, line) fprintf(stderr, "ERR %i: %s (cal %i)\n", __LINE__, (x), (line)); - #define LINE(nr, key, value) fprintf(stderr, "(%i) %i: [%s] := [%s]\n", __LINE__, nr, key, value); typedef enum { diff --git a/strbuf.c b/strbuf.c index 874aea4f..81c9e1f5 100644 --- a/strbuf.c +++ b/strbuf.c @@ -3,8 +3,7 @@ #include #ifdef SAFE_STR -#include -#define ERR(s) fprintf(stderr, "\x1B[0;31mERR\x1b[m (strbuf %3i): %s\n", __LINE__, s) +#include "err.h" #endif INIT_F(strbuf) { -- cgit v1.2.3