Error codes

This commit is contained in:
Wilson Lin 2018-07-05 20:56:24 +12:00
parent 9bdae51b01
commit ae806c9455
7 changed files with 36 additions and 14 deletions

View File

@ -5,10 +5,30 @@
#include <stdio.h>
#include <stdarg.h>
void hbe_fatal(char *fmt, ...) {
typedef enum hbe_errcode {
HBE_IO_FOPEN_FAIL = 129,
HBE_IO_FCLOSE_FAIL,
HBE_IO_FREAD_FAIL,
HBE_IO_FWRITE_FAIL,
HBE_PARSE_INVALID_ENTITY = 257,
HBE_PARSE_NONSTANDARD_TAG,
HBE_PARSE_UCASE_TAG,
HBE_PARSE_UCASE_ATTR,
HBE_PARSE_UNQUOTED_ATTR,
HBE_PARSE_ILLEGAL_CHILD,
HBE_PARSE_UNCLOSED_TAG,
HBE_PARSE_UNEXPECTED_END = 513,
HBE_PARSE_EXPECTED_NOT_FOUND,
HBE_INTERR_PEEK_OFFSET_GEQ_ZERO = 8193,
} hbe_errcode_t;
void hbe_fatal(hbe_errcode_t errcode, char *fmt, ...) {
va_list args;
va_start(args, fmt);
fprintf(stderr, "[FATAL] hyperbuild encountered an error:\n");
fprintf(stderr, "[FATAL] hyperbuild encountered error %d:\n", errcode);
vfprintf(stderr, fmt, args);
printf("\n");
va_end(args);

View File

@ -1,4 +1,5 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
@ -47,11 +48,11 @@ int main(int argc, char **argv) {
}
if (input_path == NULL) {
hbe_fatal("No input file provided");
hbe_fatal(1, "No input file provided");
}
if (output_path == NULL) {
hbe_fatal("No output file provided");
hbe_fatal(1, "No output file provided");
}
printf("Input: %s\n", input_path);
@ -65,4 +66,6 @@ int main(int argc, char **argv) {
hbu_pipe_blank_set_output_fstreamout(pipe, output);
hbs_content(pipe);
printf("All done!\n");
exit(EXIT_SUCCESS);
}

View File

@ -42,8 +42,7 @@ void hbs_tag(hbu_pipe_t pipe) {
hbu_pipe_require(pipe, '>');
if (!hbu_buffer_equal(opening_name, closing_name)) {
// TODO
hbe_fatal("EUNCTAG");
hbe_fatal(HBE_PARSE_UNCLOSED_TAG, "Tag not closed");
}
}

View File

@ -18,7 +18,7 @@
FILE *fd = fopen(path, mode); \
\
if (fd == NULL) { \
hbe_fatal("Failed to open file %s for " verb, fstream->name); \
hbe_fatal(HBE_IO_FOPEN_FAIL, "Failed to open file %s for " verb " with error %d", fstream->name, errno); \
} \
\
fstream->fd = fd; \
@ -28,7 +28,7 @@
\
void hbu_fstream##type##_delete(hbu_fstream##type##_t fstream) { \
if (fclose(fstream->fd) == EOF) { \
hbe_fatal("Failed to close " noun " stream for file %s", fstream->name); \
hbe_fatal(HBE_IO_FCLOSE_FAIL, "Failed to close " noun " stream for file %s with error %d", fstream->name, errno); \
} \
\
free(fstream); \

View File

@ -14,7 +14,7 @@ hb_eod_char_t hbu_fstreamin_read(hbu_fstreamin_t fstreamin) {
if (fread(&c, SIZEOF_CHAR, 1, fstreamin->fd) != SIZEOF_CHAR) {
if (ferror(fstreamin->fd)) {
hbe_fatal("Failed to read input file %s", fstreamin->name);
hbe_fatal(HBE_IO_FREAD_FAIL, "Failed to read input file %s", fstreamin->name);
}
// Must be EOF

View File

@ -10,7 +10,7 @@ HBU_FSTREAM_BUILD_INFRA(out, "w", "write", "writing")
void hbu_fstreamout_write(hbu_fstreamout_t fstreamout, hb_char_t c) {
if (fwrite(&c, SIZEOF_CHAR, 1, fstreamout->fd) != SIZEOF_CHAR) {
hbe_fatal("Failed to write to output file %s", fstreamout->name);
hbe_fatal(HBE_IO_FWRITE_FAIL, "Failed to write to output file %s", fstreamout->name);
}
}

View File

@ -42,7 +42,7 @@ static hb_eod_char_t _hbu_pipe_read_from_input(hbu_pipe_t pipe) {
static void _hbu_pipe_assert_valid_peek_offset(size_t offset) {
if (offset <= 0) {
hbe_fatal("INTERR $offset is less than or equal to zero");
hbe_fatal(HBE_INTERR_PEEK_OFFSET_GEQ_ZERO, "INTERR $offset is less than or equal to zero");
}
}
@ -111,7 +111,7 @@ static char *_hbu_pipe_generate_pos_msg(hbu_pipe_t pipe) {
static void _hbu_pipe_assert_not_eoi(hbu_pipe_t pipe, hb_eod_char_t c) {
if (c == HB_EOD) {
hbe_fatal("Unexpected end of input at %s", _hbu_pipe_generate_pos_msg(pipe));
hbe_fatal(HBE_PARSE_UNEXPECTED_END, "Unexpected end of input at %s", _hbu_pipe_generate_pos_msg(pipe));
}
}
@ -277,7 +277,7 @@ void hbu_pipe_require(hbu_pipe_t pipe, hb_char_t c) {
hb_char_t n = hbu_pipe_accept(pipe);
if (c != n) {
hbe_fatal("Expected `%c` (0x%x), got `%c` (0x%x)", c, c, n, n);
hbe_fatal(HBE_PARSE_EXPECTED_NOT_FOUND, "Expected `%c` (0x%x), got `%c` (0x%x)", c, c, n, n);
}
}
@ -285,7 +285,7 @@ hb_char_t hbu_pipe_require_predicate(hbu_pipe_t pipe, hbu_pipe_predicate_t pred,
hb_char_t n = hbu_pipe_accept(pipe);
if (!(*pred)(n)) {
hbe_fatal("Expected %s, got `%c` (0x%x)", name, n, n);
hbe_fatal(HBE_PARSE_EXPECTED_NOT_FOUND, "Expected %s, got `%c` (0x%x)", name, n, n);
}
return n;