diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/blogc/datetime-parser.c | 61 | ||||
-rw-r--r-- | src/blogc/datetime-parser.h | 5 | ||||
-rw-r--r-- | src/blogc/error.h | 34 | ||||
-rw-r--r-- | src/blogc/errors.c | 40 | ||||
-rw-r--r-- | src/blogc/errors.h | 24 | ||||
-rw-r--r-- | src/blogc/file.c | 9 | ||||
-rw-r--r-- | src/blogc/file.h | 5 | ||||
-rw-r--r-- | src/blogc/loader.c | 15 | ||||
-rw-r--r-- | src/blogc/loader.h | 9 | ||||
-rw-r--r-- | src/blogc/main.c | 7 | ||||
-rw-r--r-- | src/blogc/renderer.c | 7 | ||||
-rw-r--r-- | src/blogc/source-parser.c | 23 | ||||
-rw-r--r-- | src/blogc/source-parser.h | 5 | ||||
-rw-r--r-- | src/blogc/template-parser.c | 65 | ||||
-rw-r--r-- | src/blogc/template-parser.h | 5 | ||||
-rw-r--r-- | src/common/error.c (renamed from src/blogc/error.c) | 54 | ||||
-rw-r--r-- | src/common/error.h | 26 |
17 files changed, 217 insertions, 177 deletions
diff --git a/src/blogc/datetime-parser.c b/src/blogc/datetime-parser.c index a685c77..2fd3b4f 100644 --- a/src/blogc/datetime-parser.c +++ b/src/blogc/datetime-parser.c @@ -16,8 +16,9 @@ #include <string.h> -#include "error.h" #include "datetime-parser.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -47,14 +48,14 @@ typedef enum { char* blogc_convert_datetime(const char *orig, const char *format, - blogc_error_t **err) + bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; #ifndef HAVE_TIME_H - *err = blogc_error_new(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new(BLOGC_WARNING_DATETIME_PARSER, "Your operating system does not supports the datetime functionalities " "used by blogc. Sorry."); return NULL; @@ -80,7 +81,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_YEAR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -91,7 +92,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_THIRD_YEAR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -102,7 +103,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FOURTH_YEAR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid third digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -111,7 +112,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff - 1900; if (tmp < 0) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid year. Found %d, must be >= 1900.", tmp + 1900); break; @@ -120,7 +121,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_HYPHEN; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid fourth digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -131,7 +132,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_MONTH; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between year and month. " "Found '%c', must be '-'.", c); break; @@ -142,7 +143,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_MONTH; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of month. " "Found '%c', must be integer >= 0 and <= 1.", c); break; @@ -151,7 +152,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff - 1; if (tmp < 0 || tmp > 11) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid month. Found %d, must be >= 1 and <= 12.", tmp + 1); break; @@ -160,7 +161,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_HYPHEN; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of month. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -171,7 +172,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_DAY; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between month and day. " "Found '%c', must be '-'.", c); break; @@ -182,7 +183,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_DAY; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of day. " "Found '%c', must be integer >= 0 and <= 3.", c); break; @@ -191,7 +192,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff; if (tmp < 1 || tmp > 31) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid day. Found %d, must be >= 1 and <= 31.", tmp); break; @@ -200,7 +201,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SPACE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of day. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -211,7 +212,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_HOUR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between date and time. " "Found '%c', must be ' ' (empty space).", c); break; @@ -222,7 +223,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_HOUR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of hours. " "Found '%c', must be integer >= 0 and <= 2.", c); break; @@ -231,7 +232,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff; if (tmp < 0 || tmp > 23) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid hours. Found %d, must be >= 0 and <= 23.", tmp); break; @@ -240,7 +241,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_COLON; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of hours. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -251,7 +252,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_MINUTE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between hours and minutes. " "Found '%c', must be ':'.", c); break; @@ -262,7 +263,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_MINUTE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of minutes. " "Found '%c', must be integer >= 0 and <= 5.", c); break; @@ -274,7 +275,7 @@ blogc_convert_datetime(const char *orig, const char *format, // this won't happen because we are restricting the digits // to 00-59 already, but lets keep the code here for // reference. - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid minutes. Found %d, must be >= 0 and <= 59.", tmp); break; @@ -283,7 +284,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_COLON; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of minutes. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -294,7 +295,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_SECOND; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between minutes and seconds. " "Found '%c', must be ':'.", c); break; @@ -305,7 +306,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_SECOND; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of seconds. " "Found '%c', must be integer >= 0 and <= 6.", c); break; @@ -314,7 +315,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff; if (tmp < 0 || tmp > 60) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid seconds. Found %d, must be >= 0 and <= 60.", tmp); break; @@ -323,7 +324,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_DONE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of seconds. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -355,7 +356,7 @@ blogc_convert_datetime(const char *orig, const char *format, case DATETIME_SECOND_MINUTE: case DATETIME_FIRST_SECOND: case DATETIME_SECOND_SECOND: - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid datetime string. " "Found '%s', formats allowed are: 'yyyy-mm-dd hh:mm:ss', " "'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and 'yyyy-mm-dd'.", @@ -374,7 +375,7 @@ blogc_convert_datetime(const char *orig, const char *format, char buf[1024]; if (0 == strftime(buf, sizeof(buf), format, &t)) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Failed to format DATE variable, FORMAT is too long: %s", format); return NULL; diff --git a/src/blogc/datetime-parser.h b/src/blogc/datetime-parser.h index a5087b3..08ad3c9 100644 --- a/src/blogc/datetime-parser.h +++ b/src/blogc/datetime-parser.h @@ -9,9 +9,10 @@ #ifndef _DATETIME_H #define _DATETIME_H -#include "error.h" +#include "errors.h" +#include "../common/error.h" char* blogc_convert_datetime(const char *orig, const char *format, - blogc_error_t **err); + bc_error_t **err); #endif /* _DATETIME_H */ diff --git a/src/blogc/error.h b/src/blogc/error.h deleted file mode 100644 index 31fbaf2..0000000 --- a/src/blogc/error.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifndef _ERROR_H -#define _ERROR_H - -#include <stddef.h> - -typedef enum { - BLOGC_ERROR_SOURCE_PARSER = 1, - BLOGC_ERROR_TEMPLATE_PARSER, - BLOGC_ERROR_LOADER, - BLOGC_ERROR_FILE, - BLOGC_WARNING_DATETIME_PARSER, -} blogc_error_type_t; - -typedef struct { - char *msg; - blogc_error_type_t type; -} blogc_error_t; - -blogc_error_t* blogc_error_new(blogc_error_type_t type, const char *msg); -blogc_error_t* blogc_error_new_printf(blogc_error_type_t type, const char *format, ...); -blogc_error_t* blogc_error_parser(blogc_error_type_t type, const char *src, - size_t src_len, size_t current, const char *format, ...); -void blogc_error_print(blogc_error_t *err); -void blogc_error_free(blogc_error_t *err); - -#endif /* _ERROR_H */ diff --git a/src/blogc/errors.c b/src/blogc/errors.c new file mode 100644 index 0000000..6447a4a --- /dev/null +++ b/src/blogc/errors.c @@ -0,0 +1,40 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include "errors.h" +#include "../common/error.h" + + +void +blogc_error_print(bc_error_t *err) +{ + if (err == NULL) + return; + + switch((blogc_error_type_t) err->type) { + case BLOGC_ERROR_SOURCE_PARSER: + fprintf(stderr, "blogc: error: source: %s\n", err->msg); + break; + case BLOGC_ERROR_TEMPLATE_PARSER: + fprintf(stderr, "blogc: error: template: %s\n", err->msg); + break; + case BLOGC_ERROR_LOADER: + fprintf(stderr, "blogc: error: loader: %s\n", err->msg); + break; + case BLOGC_ERROR_FILE: + fprintf(stderr, "blogc: error: file: %s\n", err->msg); + break; + case BLOGC_WARNING_DATETIME_PARSER: + fprintf(stderr, "blogc: warning: datetime: %s\n", err->msg); + break; + default: + fprintf(stderr, "blogc: error: %s\n", err->msg); + } +} diff --git a/src/blogc/errors.h b/src/blogc/errors.h new file mode 100644 index 0000000..427d9e7 --- /dev/null +++ b/src/blogc/errors.h @@ -0,0 +1,24 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _ERRORS_H +#define _ERRORS_H + +#include "../common/error.h" + +typedef enum { + BLOGC_ERROR_SOURCE_PARSER = 1, + BLOGC_ERROR_TEMPLATE_PARSER, + BLOGC_ERROR_LOADER, + BLOGC_ERROR_FILE, + BLOGC_WARNING_DATETIME_PARSER, +} blogc_error_type_t; + +void blogc_error_print(bc_error_t *err); + +#endif /* _ERRORS_H */ diff --git a/src/blogc/file.c b/src/blogc/file.c index cdd6df4..94148fe 100644 --- a/src/blogc/file.c +++ b/src/blogc/file.c @@ -13,7 +13,8 @@ #include <stdio.h> #include <string.h> #include "file.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utf8.h" #include "../common/utils.h" @@ -22,7 +23,7 @@ char* -blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) +blogc_file_get_contents(const char *path, size_t *len, bc_error_t **err) { if (path == NULL || err == NULL || *err != NULL) return NULL; @@ -32,7 +33,7 @@ blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) if (fp == NULL) { int tmp_errno = errno; - *err = blogc_error_new_printf(BLOGC_ERROR_FILE, + *err = bc_error_new_printf(BLOGC_ERROR_FILE, "Failed to open file (%s): %s", path, strerror(tmp_errno)); return NULL; } @@ -60,7 +61,7 @@ blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) fclose(fp); if (!blogc_utf8_validate_str(str)) { - *err = blogc_error_new_printf(BLOGC_ERROR_FILE, + *err = bc_error_new_printf(BLOGC_ERROR_FILE, "File content is not valid UTF-8: %s", path); bc_string_free(str, true); return NULL; diff --git a/src/blogc/file.h b/src/blogc/file.h index d2c4390..c509874 100644 --- a/src/blogc/file.h +++ b/src/blogc/file.h @@ -11,11 +11,12 @@ #include <stddef.h> #include <stdio.h> -#include "error.h" +#include "errors.h" +#include "../common/error.h" #define BLOGC_FILE_CHUNK_SIZE 1024 -char* blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err); +char* blogc_file_get_contents(const char *path, size_t *len, bc_error_t **err); int blogc_fprintf(FILE *stream, const char *format, ...); #endif /* _FILE_H */ diff --git a/src/blogc/loader.c b/src/blogc/loader.c index 914f07f..93d9eef 100644 --- a/src/blogc/loader.c +++ b/src/blogc/loader.c @@ -16,7 +16,8 @@ #include "source-parser.h" #include "template-parser.h" #include "loader.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -58,7 +59,7 @@ blogc_get_filename(const char *f) bc_slist_t* -blogc_template_parse_from_file(const char *f, blogc_error_t **err) +blogc_template_parse_from_file(const char *f, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -73,7 +74,7 @@ blogc_template_parse_from_file(const char *f, blogc_error_t **err) bc_trie_t* -blogc_source_parse_from_file(const char *f, blogc_error_t **err) +blogc_source_parse_from_file(const char *f, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -96,9 +97,9 @@ blogc_source_parse_from_file(const char *f, blogc_error_t **err) bc_slist_t* -blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, blogc_error_t **err) +blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err) { - blogc_error_t *tmp_err = NULL; + bc_error_t *tmp_err = NULL; bc_slist_t *rv = NULL; unsigned int with_date = 0; @@ -123,10 +124,10 @@ blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, blogc_error_t **er char *f = tmp->data; bc_trie_t *s = blogc_source_parse_from_file(f, &tmp_err); if (s == NULL) { - *err = blogc_error_new_printf(BLOGC_ERROR_LOADER, + *err = bc_error_new_printf(BLOGC_ERROR_LOADER, "An error occurred while parsing source file: %s\n\n%s", f, tmp_err->msg); - blogc_error_free(tmp_err); + bc_error_free(tmp_err); tmp_err = NULL; bc_slist_free_full(rv, (bc_free_func_t) bc_trie_free); rv = NULL; diff --git a/src/blogc/loader.h b/src/blogc/loader.h index 97286a0..c8a7230 100644 --- a/src/blogc/loader.h +++ b/src/blogc/loader.h @@ -9,13 +9,14 @@ #ifndef _LOADER_H #define _LOADER_H -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" char* blogc_get_filename(const char *f); -bc_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err); -bc_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err); +bc_slist_t* blogc_template_parse_from_file(const char *f, bc_error_t **err); +bc_trie_t* blogc_source_parse_from_file(const char *f, bc_error_t **err); bc_slist_t* blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, - blogc_error_t **err); + bc_error_t **err); #endif /* _LOADER_H */ diff --git a/src/blogc/main.c b/src/blogc/main.c index 0f174d4..8563c68 100644 --- a/src/blogc/main.c +++ b/src/blogc/main.c @@ -26,7 +26,8 @@ #include "template-parser.h" #include "loader.h" #include "renderer.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utf8.h" #include "../common/utils.h" @@ -219,7 +220,7 @@ main(int argc, char **argv) goto cleanup; } - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = blogc_source_parse_from_files(config, sources, &err); if (err != NULL) { @@ -286,7 +287,7 @@ cleanup3: blogc_template_free_stmts(l); cleanup2: bc_slist_free_full(s, (bc_free_func_t) bc_trie_free); - blogc_error_free(err); + bc_error_free(err); cleanup: bc_trie_free(config); free(template); diff --git a/src/blogc/renderer.c b/src/blogc/renderer.c index 409c75e..7c29818 100644 --- a/src/blogc/renderer.c +++ b/src/blogc/renderer.c @@ -13,9 +13,10 @@ #include <stdlib.h> #include <string.h> #include "datetime-parser.h" -#include "error.h" +#include "errors.h" #include "template-parser.h" #include "renderer.h" +#include "../common/error.h" #include "../common/utils.h" @@ -43,11 +44,11 @@ blogc_format_date(const char *date, bc_trie_t *global, bc_trie_t *local) if (date_format == NULL) return bc_strdup(date); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *rv = blogc_convert_datetime(date, date_format, &err); if (err != NULL) { blogc_error_print(err); - blogc_error_free(err); + bc_error_free(err); return bc_strdup(date); } return rv; diff --git a/src/blogc/source-parser.c b/src/blogc/source-parser.c index dc39760..7cb929e 100644 --- a/src/blogc/source-parser.c +++ b/src/blogc/source-parser.c @@ -11,7 +11,8 @@ #include "content-parser.h" #include "source-parser.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -27,7 +28,7 @@ typedef enum { bc_trie_t* -blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) +blogc_source_parse(const char *src, size_t src_len, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -60,7 +61,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) state = SOURCE_SEPARATOR; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Can't find a configuration key or the content separator."); break; @@ -93,7 +94,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) ((current - start == 13) && (0 == strncmp("BLOGC_VERSION", src + start, 13)))) { - *err = blogc_error_new_printf(BLOGC_ERROR_SOURCE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_SOURCE_PARSER, "'%s' variable is forbidden in source files. It will " "be set for you by the compiler.", key); break; @@ -101,7 +102,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) state = SOURCE_CONFIG_VALUE_START; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Invalid configuration key."); break; @@ -111,7 +112,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Configuration value not provided for '%s'.", key); break; @@ -134,7 +135,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) state = SOURCE_CONTENT_START; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Invalid content separator. Must be more than one '-' characters."); break; @@ -183,21 +184,21 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) // output. :) switch (state) { case SOURCE_START: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Your source file is empty."); break; case SOURCE_CONFIG_KEY: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Your last configuration key is missing ':' and " "the value"); break; case SOURCE_CONFIG_VALUE_START: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Configuration value not provided for '%s'.", key); break; case SOURCE_CONFIG_VALUE: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "No line ending after the configuration value for " "'%s'.", key); break; diff --git a/src/blogc/source-parser.h b/src/blogc/source-parser.h index d135f23..d920775 100644 --- a/src/blogc/source-parser.h +++ b/src/blogc/source-parser.h @@ -10,10 +10,11 @@ #define _SOURCE_PARSER_H #include <stddef.h> -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" bc_trie_t* blogc_source_parse(const char *src, size_t src_len, - blogc_error_t **err); + bc_error_t **err); #endif /* _SOURCE_PARSER_H */ diff --git a/src/blogc/template-parser.c b/src/blogc/template-parser.c index 1cca885..acf8f7c 100644 --- a/src/blogc/template-parser.c +++ b/src/blogc/template-parser.c @@ -11,7 +11,8 @@ #include <string.h> #include "template-parser.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -42,7 +43,7 @@ typedef enum { bc_slist_t* -blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) +blogc_template_parse(const char *src, size_t src_len, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -169,13 +170,13 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) break; } if (c == '-') { - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Duplicated whitespace " "cleaner before statement."); break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must begin with lowercase letter."); break; @@ -195,7 +196,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) block_foreach_open = foreach_open; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Blocks can't be nested."); break; } @@ -204,14 +205,14 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) { if (block_open) { if (if_count != block_if_count) { - *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "%d open 'if', 'ifdef' and/or 'ifndef' statements " "were not closed inside a '%s' block!", if_count - block_if_count, block_type); break; } if (!block_foreach_open && foreach_open) { - *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "An open 'foreach' statement was not closed " "inside a '%s' block!", block_type); break; @@ -221,7 +222,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) block_open = false; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'endblock' statement without an open 'block' statement."); break; @@ -268,13 +269,13 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) else_open = true; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "More than one 'else' statement for an open 'if', " "'ifdef' or 'ifndef' statement."); break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'else' statement without an open 'if', 'ifdef' or " "'ifndef' statement."); @@ -292,7 +293,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) else_open = false; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'endif' statement without an open 'if', 'ifdef' or " "'ifndef' statement."); @@ -308,7 +309,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) foreach_open = true; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'foreach' statements can't " "be nested."); break; @@ -324,14 +325,14 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) foreach_open = false; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'endforeach' statement without an open 'foreach' " "statement."); break; } } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement type: Allowed types are: 'block', " "'endblock', 'if', 'ifdef', 'ifndef', 'else', 'endif', " @@ -346,7 +347,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid block syntax. Must begin with lowercase letter."); break; @@ -380,7 +381,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) break; } } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid block type. Allowed types are: 'entry', 'listing' " "and 'listing_once'."); @@ -394,7 +395,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must begin with uppercase letter."); break; @@ -410,7 +411,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must be uppercase letter, number " "or '_'."); @@ -446,7 +447,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) } op_start = 0; op_end = 0; - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid 'if' operand. Must be double-quoted static " "string or variable."); @@ -476,7 +477,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid foreach variable name. Must begin with uppercase " "letter."); @@ -490,7 +491,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid foreach variable name. Must be uppercase letter, " "number or '_'."); @@ -512,13 +513,13 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) break; } if (c == '-') { - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Duplicated whitespace " "cleaner after statement."); break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must end with '%%}'."); break; @@ -532,7 +533,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must begin with uppercase letter."); break; @@ -550,7 +551,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_CLOSE_BRACKET; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must be uppercase letter, number " "or '_'."); @@ -563,7 +564,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_CLOSE_BRACKET; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must end with '}}'."); break; @@ -589,7 +590,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) tmp_op = BLOGC_TEMPLATE_OP_NEQ; } if (tmp_op == 0) { - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, op_start, "Invalid 'if' operator. Must be '<', '>', " "'<=', '>=', '==' or '!='."); @@ -622,7 +623,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current + 1; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must end with '}'."); break; @@ -637,17 +638,17 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) if (*err == NULL) { if (state == TEMPLATE_BLOCK_IF_STRING_OPERAND) - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, start2, "Found an open double-quoted string."); else if (if_count != 0) - *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "%d open 'if', 'ifdef' and/or 'ifndef' statements were not closed!", if_count); else if (block_open) - *err = blogc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, "An open block was not closed!"); else if (foreach_open) - *err = blogc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, "An open 'foreach' statement was not closed!"); } diff --git a/src/blogc/template-parser.h b/src/blogc/template-parser.h index b22c2e7..3ed3b07 100644 --- a/src/blogc/template-parser.h +++ b/src/blogc/template-parser.h @@ -10,7 +10,8 @@ #define _TEMPLATE_PARSER_H #include <stddef.h> -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" /* @@ -47,7 +48,7 @@ typedef struct { } blogc_template_stmt_t; bc_slist_t* blogc_template_parse(const char *src, size_t src_len, - blogc_error_t **err); + bc_error_t **err); void blogc_template_free_stmts(bc_slist_t *stmts); #endif /* _TEMPLATE_PARSER_H */ diff --git a/src/blogc/error.c b/src/common/error.c index cca8c03..dbc1d81 100644 --- a/src/blogc/error.c +++ b/src/common/error.c @@ -10,34 +10,34 @@ #include <stdlib.h> #include <stdarg.h> #include "error.h" -#include "../common/utils.h" +#include "utils.h" -blogc_error_t* -blogc_error_new(blogc_error_type_t type, const char *msg) +bc_error_t* +bc_error_new(int type, const char *msg) { - blogc_error_t *err = bc_malloc(sizeof(blogc_error_t)); + bc_error_t *err = bc_malloc(sizeof(bc_error_t)); err->type = type; err->msg = bc_strdup(msg); return err; } -blogc_error_t* -blogc_error_new_printf(blogc_error_type_t type, const char *format, ...) +bc_error_t* +bc_error_new_printf(int type, const char *format, ...) { va_list ap; va_start(ap, format); char *tmp = bc_strdup_vprintf(format, ap); va_end(ap); - blogc_error_t *rv = blogc_error_new(type, tmp); + bc_error_t *rv = bc_error_new(type, tmp); free(tmp); return rv; } -blogc_error_t* -blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, +bc_error_t* +bc_error_parser(int type, const char *src, size_t src_len, size_t current, const char *format, ...) { va_list ap; @@ -85,12 +85,12 @@ blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, char *line = bc_strndup(src + linestart, lineend - linestart); - blogc_error_t *rv = NULL; + bc_error_t *rv = NULL; if (line[0] == '\0') // "near" message isn't useful if line is empty - rv = blogc_error_new(type, msg); + rv = bc_error_new(type, msg); else - rv = blogc_error_new_printf(type, + rv = bc_error_new_printf(type, "%s\nError occurred near line %d, position %d: %s", msg, lineno, pos, line); @@ -102,35 +102,7 @@ blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, void -blogc_error_print(blogc_error_t *err) -{ - if (err == NULL) - return; - - switch(err->type) { - case BLOGC_ERROR_SOURCE_PARSER: - fprintf(stderr, "blogc: error: source: %s\n", err->msg); - break; - case BLOGC_ERROR_TEMPLATE_PARSER: - fprintf(stderr, "blogc: error: template: %s\n", err->msg); - break; - case BLOGC_ERROR_LOADER: - fprintf(stderr, "blogc: error: loader: %s\n", err->msg); - break; - case BLOGC_ERROR_FILE: - fprintf(stderr, "blogc: error: file: %s\n", err->msg); - break; - case BLOGC_WARNING_DATETIME_PARSER: - fprintf(stderr, "blogc: warning: datetime: %s\n", err->msg); - break; - default: - fprintf(stderr, "blogc: error: %s\n", err->msg); - } -} - - -void -blogc_error_free(blogc_error_t *err) +bc_error_free(bc_error_t *err) { if (err == NULL) return; diff --git a/src/common/error.h b/src/common/error.h new file mode 100644 index 0000000..17843ed --- /dev/null +++ b/src/common/error.h @@ -0,0 +1,26 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _ERROR_H +#define _ERROR_H + +#include <stddef.h> + +typedef struct { + char *msg; + int type; +} bc_error_t; + +bc_error_t* bc_error_new(int type, const char *msg); +bc_error_t* bc_error_new_printf(int type, const char *format, ...); +bc_error_t* bc_error_parser(int type, const char *src, size_t src_len, + size_t current, const char *format, ...); +void bc_error_print(bc_error_t *err); +void bc_error_free(bc_error_t *err); + +#endif /* _ERROR_H */ |