aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-07-05 17:35:14 +0000
committerDavid Robillard <d@drobilla.net>2012-07-05 17:35:14 +0000
commit02a41f39b216ca43056fc2e8183351c077b5dd9c (patch)
tree1224e6ba637bc4879213786228a1b4d1e7516ea2 /src
parent1c7fb2461a581a501f62a7c72a444147da39787e (diff)
downloadserd-02a41f39b216ca43056fc2e8183351c077b5dd9c.tar.gz
serd-02a41f39b216ca43056fc2e8183351c077b5dd9c.tar.bz2
serd-02a41f39b216ca43056fc2e8183351c077b5dd9c.zip
Add error callback to reader and writer for custom error reporting.
Add -q option to serdi to suppress all non-data output, e.g. errors. Resolves #815. git-svn-id: http://svn.drobilla.net/serd/trunk@354 490d8e77-9747-427b-9fa3-0b8f29cee8a0
Diffstat (limited to 'src')
-rw-r--r--src/env.c2
-rw-r--r--src/reader.c62
-rw-r--r--src/serd_internal.h13
-rw-r--r--src/serdi.c15
-rw-r--r--src/string.c5
-rw-r--r--src/writer.c102
6 files changed, 139 insertions, 60 deletions
diff --git a/src/env.c b/src/env.c
index ecefb005..605be6e6 100644
--- a/src/env.c
+++ b/src/env.c
@@ -217,7 +217,7 @@ serd_env_expand(const SerdEnv* env,
const uint8_t* const colon = (const uint8_t*)memchr(
qname->buf, ':', qname->n_bytes + 1);
if (!colon) {
- return SERD_ERR_BAD_ARG; // Illegal qname
+ return SERD_ERR_BAD_ARG; // Invalid qname
}
const size_t name_len = colon - qname->buf;
diff --git a/src/reader.c b/src/reader.c
index 5c623039..d66dbb09 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -64,6 +64,8 @@ struct SerdReaderImpl {
SerdPrefixSink prefix_sink;
SerdStatementSink statement_sink;
SerdEndSink end_sink;
+ SerdErrorSink error_sink;
+ void* error_handle;
Ref rdf_first;
Ref rdf_rest;
Ref rdf_nil;
@@ -90,13 +92,14 @@ struct SerdReaderImpl {
};
static int
-error(SerdReader* reader, const char* fmt, ...)
+error(SerdReader* reader, SerdStatus st, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
- fprintf(stderr, "error: %s:%u:%u: ",
- reader->cur.filename, reader->cur.line, reader->cur.col);
- vfprintf(stderr, fmt, args);
+ const SerdError e = {
+ st, reader->cur.filename, reader->cur.line, reader->cur.col, fmt, &args
+ };
+ serd_error(reader->error_sink, reader->error_handle, &e);
va_end(args);
return 0;
}
@@ -149,7 +152,8 @@ eat_byte_check(SerdReader* reader, const uint8_t byte)
{
const uint8_t c = peek_byte(reader);
if (c != byte) {
- return error(reader, "expected `%c', not `%c'\n", byte, c);
+ return error(reader, SERD_ERR_BAD_SYNTAX,
+ "expected `%c', not `%c'\n", byte, c);
}
return eat_byte_safe(reader, byte);
}
@@ -271,7 +275,8 @@ read_hex(SerdReader* reader)
if (in_range(c, 0x30, 0x39) || in_range(c, 0x41, 0x46)) {
return eat_byte_safe(reader, c);
} else {
- return error(reader, "illegal hexadecimal digit `%c'\n", c);
+ return error(reader, SERD_ERR_BAD_SYNTAX,
+ "invalid hexadecimal digit `%c'\n", c);
}
}
@@ -298,7 +303,8 @@ read_hex_escape(SerdReader* reader, unsigned length, Ref dest)
} else if (c < 0x00110000) {
size = 4;
} else {
- error(reader, "unicode character 0x%X out of range\n", c);
+ error(reader, SERD_ERR_BAD_SYNTAX,
+ "unicode character 0x%X out of range\n", c);
push_replacement(reader, dest);
return true;
}
@@ -398,7 +404,7 @@ read_ucharacter_escape(SerdReader* reader, Ref dest)
static inline SerdStatus
bad_char(SerdReader* reader, Ref dest, const char* fmt, uint8_t c)
{
- error(reader, fmt, c);
+ error(reader, SERD_ERR_BAD_SYNTAX, fmt, c);
push_replacement(reader, dest);
// Skip bytes until the next start byte
@@ -454,7 +460,7 @@ read_character(SerdReader* reader, Ref dest)
const uint8_t c = peek_byte(reader);
assert(c != '\\'); // Only called from methods that handle escapes first
if (c == '\0') {
- error(reader, "unexpected end of file\n", c);
+ error(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of input\n", c);
return SERD_ERR_BAD_SYNTAX;
} else if (c < 0x20) {
return bad_char(reader, dest,
@@ -493,7 +499,8 @@ read_lcharacter(SerdReader* reader, Ref dest, SerdNodeFlags* flags)
if (read_scharacter_escape(reader, dest, flags)) {
return SERD_SUCCESS;
} else {
- error(reader, "illegal escape `\\%c'\n", peek_byte(reader));
+ error(reader, SERD_ERR_BAD_SYNTAX,
+ "invalid escape `\\%c'\n", peek_byte(reader));
return SERD_ERR_BAD_SYNTAX;
}
case 0xA: case 0xD:
@@ -517,7 +524,8 @@ read_scharacter(SerdReader* reader, Ref dest, SerdNodeFlags* flags)
if (read_scharacter_escape(reader, dest, flags)) {
return SERD_SUCCESS;
} else {
- error(reader, "illegal escape `\\%c'\n", peek_byte(reader));
+ error(reader, SERD_ERR_BAD_SYNTAX,
+ "invalid escape `\\%c'\n", peek_byte(reader));
return SERD_ERR_BAD_SYNTAX;
}
case '\"':
@@ -539,7 +547,8 @@ read_ucharacter(SerdReader* reader, Ref dest)
if (read_ucharacter_escape(reader, dest)) {
return SERD_SUCCESS;
} else {
- error(reader, "illegal escape `\\%c'\n", peek_byte(reader));
+ error(reader, SERD_ERR_BAD_SYNTAX,
+ "invalid escape `\\%c'\n", peek_byte(reader));
return SERD_FAILURE;
}
case '>':
@@ -707,7 +716,7 @@ read_prefixName(SerdReader* reader, Ref dest)
{
uint8_t c = peek_byte(reader);
if (c == '_') {
- error(reader, "unexpected `_'\n");
+ error(reader, SERD_ERR_BAD_SYNTAX, "unexpected `_'\n");
return pop_node(reader, dest);
}
TRY_RET(c = read_nameStartChar(reader));
@@ -741,7 +750,7 @@ read_language(SerdReader* reader)
{
uint8_t c = peek_byte(reader);
if (!in_range(c, 'a', 'z')) {
- return error(reader, "unexpected `%c'\n", c);
+ return error(reader, SERD_ERR_BAD_SYNTAX, "unexpected `%c'\n", c);
}
Ref ref = push_node(reader, SERD_LITERAL, "", 0);
push_byte(reader, ref, eat_byte_safe(reader, c));
@@ -795,7 +804,7 @@ read_0_9(SerdReader* reader, Ref str, bool at_least_one)
uint8_t c;
if (at_least_one) {
if (!is_digit((c = peek_byte(reader)))) {
- return error(reader, "expected digit\n");
+ return error(reader, SERD_ERR_BAD_SYNTAX, "expected digit\n");
}
push_byte(reader, str, eat_byte_safe(reader, c));
}
@@ -957,7 +966,8 @@ read_nodeID(SerdReader* reader)
reader->bprefix ? (char*)reader->bprefix : "",
reader->bprefix_len);
if (!read_name(reader, ref)) {
- return error(reader, "illegal character at start of name\n");
+ return error(reader, SERD_ERR_BAD_SYNTAX,
+ "invalid character at start of name\n");
}
if (reader->syntax == SERD_TURTLE) {
const char* const buf = (const char*)deref(reader, ref)->buf;
@@ -965,8 +975,8 @@ read_nodeID(SerdReader* reader)
memcpy((char*)buf, "docid", 5); // Prevent clash
reader->seen_genid = true;
} else if (reader->seen_genid && !strncmp(buf, "docid", 5)) {
- error(reader, "found both `genid' and `docid' blank IDs\n");
- error(reader, "resolve this with a blank ID prefix\n");
+ error(reader, SERD_ERR_ID_CLASH,
+ "found both `genid' and `docid' IDs, prefix required\n");
return pop_node(reader, ref);
}
}
@@ -1039,7 +1049,7 @@ read_blank(SerdReader* reader, ReadContext ctx, bool subject, Ref* dest)
case '(':
return read_collection(reader, ctx, dest);
default:
- return error(reader, "illegal blank node\n");
+ return error(reader, SERD_ERR_BAD_SYNTAX, "invalid blank node\n");
}
}
@@ -1311,7 +1321,7 @@ read_directive(SerdReader* reader)
switch (peek_byte(reader)) {
case 'b': return read_base(reader);
case 'p': return read_prefixID(reader);
- default: return error(reader, "illegal directive\n");
+ default: return error(reader, SERD_ERR_BAD_SYNTAX, "invalid directive\n");
}
}
@@ -1366,6 +1376,8 @@ serd_reader_new(SerdSyntax syntax,
me->prefix_sink = prefix_sink;
me->statement_sink = statement_sink;
me->end_sink = end_sink;
+ me->error_sink = NULL;
+ me->error_handle = NULL;
me->default_graph = SERD_NODE_NULL;
me->fd = 0;
me->stack = serd_stack_new(SERD_PAGE_SIZE);
@@ -1392,6 +1404,16 @@ serd_reader_new(SerdSyntax syntax,
SERD_API
void
+serd_reader_set_error_sink(SerdReader* reader,
+ SerdErrorSink error_sink,
+ void* error_handle)
+{
+ reader->error_sink = error_sink;
+ reader->error_handle = error_handle;
+}
+
+SERD_API
+void
serd_reader_free(SerdReader* reader)
{
pop_node(reader, reader->rdf_nil);
diff --git a/src/serd_internal.h b/src/serd_internal.h
index ee55de27..9a875daa 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -290,4 +290,17 @@ uri_is_under(const SerdURI* uri, const SerdURI* root)
return true;
}
+/* Error reporting */
+
+static inline void
+serd_error(SerdErrorSink error_sink, void* handle, const SerdError* e)
+{
+ if (error_sink) {
+ error_sink(handle, e);
+ } else {
+ fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col);
+ vfprintf(stderr, e->fmt, *e->args);
+ }
+}
+
#endif // SERD_INTERNAL_H
diff --git a/src/serdi.c b/src/serdi.c
index 4c482884..b90c00e5 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -52,6 +52,7 @@ print_usage(const char* name, bool error)
fprintf(os, " -i SYNTAX Input syntax (`turtle' or `ntriples').\n");
fprintf(os, " -o SYNTAX Output syntax (`turtle' or `ntriples').\n");
fprintf(os, " -p PREFIX Add PREFIX to blank node IDs.\n");
+ fprintf(os, " -q Suppress all output except data.\n");
fprintf(os, " -r ROOT_URI Keep relative URIs within ROOT_URI.\n");
fprintf(os, " -s INPUT Parse INPUT as string (terminates options).\n");
fprintf(os, " -v Display version information and exit.\n");
@@ -79,6 +80,12 @@ bad_arg(const char* name, char opt)
return 1;
}
+static SerdStatus
+quiet_error_sink(void* handle, const SerdError* e)
+{
+ return SERD_SUCCESS;
+}
+
int
main(int argc, char** argv)
{
@@ -93,6 +100,7 @@ main(int argc, char** argv)
bool bulk_read = true;
bool bulk_write = false;
bool full_uris = false;
+ bool quiet = false;
const uint8_t* in_name = NULL;
const uint8_t* add_prefix = NULL;
const uint8_t* chop_prefix = NULL;
@@ -111,6 +119,8 @@ main(int argc, char** argv)
full_uris = true;
} else if (argv[a][1] == 'h') {
return print_usage(argv[0], false);
+ } else if (argv[a][1] == 'q') {
+ quiet = true;
} else if (argv[a][1] == 'v') {
return print_version();
} else if (argv[a][1] == 's') {
@@ -205,6 +215,11 @@ main(int argc, char** argv)
(SerdStatementSink)serd_writer_write_statement,
(SerdEndSink)serd_writer_end_anon);
+ if (quiet) {
+ serd_reader_set_error_sink(reader, quiet_error_sink, NULL);
+ serd_writer_set_error_sink(writer, quiet_error_sink, NULL);
+ }
+
SerdNode root = serd_node_from_string(SERD_URI, root_uri);
serd_writer_set_root_uri(writer, &root);
serd_writer_chop_blank_prefix(writer, chop_prefix);
diff --git a/src/string.c b/src/string.c
index 24ab9792..a1a1dff1 100644
--- a/src/string.c
+++ b/src/string.c
@@ -29,8 +29,11 @@ serd_strerror(SerdStatus st)
case SERD_ERR_BAD_SYNTAX: return (const uint8_t*)"Invalid syntax";
case SERD_ERR_BAD_ARG: return (const uint8_t*)"Invalid argument";
case SERD_ERR_NOT_FOUND: return (const uint8_t*)"Not found";
+ case SERD_ERR_ID_CLASH: return (const uint8_t*)"Blank node ID clash";
+ case SERD_ERR_BAD_CURIE: return (const uint8_t*)"Invalid CURIE";
+ case SERD_ERR_INTERNAL: return (const uint8_t*)"Internal error";
}
- return (const uint8_t*)"Unknown error code"; // never reached
+ return (const uint8_t*)"Unknown error"; // never reached
}
SERD_API
diff --git a/src/writer.c b/src/writer.c
index 02c362af..5c85073a 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -74,24 +74,26 @@ static const SepRule rules[] = {
};
struct SerdWriterImpl {
- SerdSyntax syntax;
- SerdStyle style;
- SerdEnv* env;
- SerdNode root_node;
- SerdURI root_uri;
- SerdURI base_uri;
- SerdStack anon_stack;
- SerdBulkSink bulk_sink;
- SerdSink sink;
- void* stream;
- WriteContext context;
- SerdNode list_subj;
- unsigned list_depth;
- uint8_t* bprefix;
- size_t bprefix_len;
- unsigned indent;
- Sep last_sep;
- bool empty;
+ SerdSyntax syntax;
+ SerdStyle style;
+ SerdEnv* env;
+ SerdNode root_node;
+ SerdURI root_uri;
+ SerdURI base_uri;
+ SerdStack anon_stack;
+ SerdBulkSink bulk_sink;
+ SerdSink sink;
+ void* stream;
+ SerdErrorSink error_sink;
+ void* error_handle;
+ WriteContext context;
+ SerdNode list_subj;
+ unsigned list_depth;
+ uint8_t* bprefix;
+ size_t bprefix_len;
+ unsigned indent;
+ Sep last_sep;
+ bool empty;
};
typedef enum {
@@ -100,6 +102,16 @@ typedef enum {
WRITE_LONG_STRING
} TextContext;
+static void
+error(SerdWriter* writer, SerdStatus st, const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ const SerdError e = { st, NULL, 0, 0, fmt, &args };
+ serd_error(writer->error_sink, writer->error_handle, &e);
+ va_end(args);
+}
+
static inline WriteContext*
anon_stack_top(SerdWriter* writer)
{
@@ -207,7 +219,7 @@ write_text(SerdWriter* writer, TextContext ctx,
size = 4;
c = in & 0x07;
} else {
- fprintf(stderr, "Invalid UTF-8: %X\n", in);
+ error(writer, SERD_ERR_BAD_ARG, "invalid UTF-8: %X\n", in);
const uint8_t replacement_char[] = { 0xEF, 0xBF, 0xBD };
len += sink(replacement_char, sizeof(replacement_char), writer);
return len;
@@ -352,7 +364,8 @@ write_node(SerdWriter* writer,
switch (writer->syntax) {
case SERD_NTRIPLES:
if (serd_env_expand(writer->env, node, &uri_prefix, &uri_suffix)) {
- fprintf(stderr, "Undefined namespace prefix `%s'\n", node->buf);
+ error(writer, SERD_ERR_BAD_CURIE,
+ "undefined namespace prefix `%s'\n", node->buf);
return false;
}
sink("<", 1, writer);
@@ -590,7 +603,8 @@ serd_writer_end_anon(SerdWriter* writer,
return SERD_SUCCESS;
}
if (serd_stack_is_empty(&writer->anon_stack)) {
- fprintf(stderr, "Unexpected end of anonymous node\n");
+ error(writer, SERD_ERR_UNKNOWN,
+ "unexpected end of anonymous node\n");
return SERD_ERR_UNKNOWN;
}
assert(writer->indent > 0);
@@ -632,23 +646,25 @@ serd_writer_new(SerdSyntax syntax,
{
const WriteContext context = WRITE_CONTEXT_NULL;
SerdWriter* writer = (SerdWriter*)malloc(sizeof(SerdWriter));
- writer->syntax = syntax;
- writer->style = style;
- writer->env = env;
- writer->root_node = SERD_NODE_NULL;
- writer->root_uri = SERD_URI_NULL;
- writer->base_uri = base_uri ? *base_uri : SERD_URI_NULL;
- writer->anon_stack = serd_stack_new(sizeof(WriteContext));
- writer->sink = sink;
- writer->stream = stream;
- writer->context = context;
- writer->list_subj = SERD_NODE_NULL;
- writer->list_depth = 0;
- writer->bprefix = NULL;
- writer->bprefix_len = 0;
- writer->indent = 0;
- writer->last_sep = SEP_NONE;
- writer->empty = true;
+ writer->syntax = syntax;
+ writer->style = style;
+ writer->env = env;
+ writer->root_node = SERD_NODE_NULL;
+ writer->root_uri = SERD_URI_NULL;
+ writer->base_uri = base_uri ? *base_uri : SERD_URI_NULL;
+ writer->anon_stack = serd_stack_new(sizeof(WriteContext));
+ writer->sink = sink;
+ writer->stream = stream;
+ writer->error_sink = NULL;
+ writer->error_handle = NULL;
+ writer->context = context;
+ writer->list_subj = SERD_NODE_NULL;
+ writer->list_depth = 0;
+ writer->bprefix = NULL;
+ writer->bprefix_len = 0;
+ writer->indent = 0;
+ writer->last_sep = SEP_NONE;
+ writer->empty = true;
if (style & SERD_STYLE_BULK) {
writer->bulk_sink = serd_bulk_sink_new(sink, stream, SERD_PAGE_SIZE);
}
@@ -657,6 +673,16 @@ serd_writer_new(SerdSyntax syntax,
SERD_API
void
+serd_writer_set_error_sink(SerdWriter* writer,
+ SerdErrorSink error_sink,
+ void* error_handle)
+{
+ writer->error_sink = error_sink;
+ writer->error_handle = error_handle;
+}
+
+SERD_API
+void
serd_writer_chop_blank_prefix(SerdWriter* writer,
const uint8_t* prefix)
{