From 930a75074eff7f1d3c91b3a244d47066247d7cd5 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 8 Jan 2023 23:04:40 +0100 Subject: Print warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- common.h | 13 +++++++++++++ libfonts.h | 22 ++++++++++++++++++++++ libfonts_get_default_font.c | 10 ++++++---- libfonts_get_default_rendering_settings.c | 15 ++++++++++----- libfonts_get_output_rendering_settings.c | 17 +++++++++++------ libfonts_parse_aa__.c | 2 +- libfonts_parse_order__.c | 2 +- 7 files changed, 64 insertions(+), 17 deletions(-) diff --git a/common.h b/common.h index fe0d5ad..1047e2f 100644 --- a/common.h +++ b/common.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -123,6 +124,18 @@ X(LIBFONTS_SUBPIXEL_ORDER_BALANCED_BR_BG, "balanced br:bg") +static inline void +warning(struct libfonts_context *ctx, int err, const char *function, const char *fmt, ...) +{ + va_list args; + if (ctx && ctx->warning) { + va_start(args, fmt); + ctx->warning(err, function, fmt, args, ctx->user); + va_end(args); + } +} + + static inline void transform(double *x_out, double *y_out, double x, double y, const struct libfonts_transformation *transformation) { diff --git a/libfonts.h b/libfonts.h index 2c7202a..f45c203 100644 --- a/libfonts.h +++ b/libfonts.h @@ -2,6 +2,7 @@ #ifndef LIBFONTS_H #define LIBFONTS_H +#include #include #include @@ -1622,6 +1623,27 @@ struct libfonts_context { * Only used if `.use_context_uid` is non-zero */ uid_t uid; + + /** + * Application data used in as the last argument + * in callback functions + */ + void *user; + + /** + * If non-`NULL` some function may call thing + * function to let the application print a + * warning + * + * @param err `errno` value for the warning, 0 if none + * @param function The name of the function the warning is emitted from (closest public function) + * @param fmt Formattable description of the warning, will start in lower case, + * and will end with a colon if and only if a description of `err` + * shall be appended to the message (with a preceding space) + * @param args Argument used into the description via formatting + * @param user `.user` as is + */ + void (*warning)(int err, const char *function, const char *fmt, va_list args, void *user); }; diff --git a/libfonts_get_default_font.c b/libfonts_get_default_font.c index 69d2729..e0ad9ab 100644 --- a/libfonts_get_default_font.c +++ b/libfonts_get_default_font.c @@ -25,6 +25,7 @@ getn(char **outp, const char *file_part1, size_t file_part1_len, const char *fil char *line, *buf = NULL; size_t size = 0, off = 0, avail = 0; char *value; + size_t lineno = 0; *outp = NULL; @@ -60,7 +61,7 @@ open_again: case ENODEV: case ENOTDIR: case ENXIO: - /* TODO print warning using `ctx` */ + warning(ctx, errno, "libfonts_get_default_font", "failed to open %s:", path); goto out; default: goto out; @@ -83,6 +84,7 @@ open_again: if (!len) break; line[len -= 1] = '\0'; + lineno++; while (isblank(*line)) { line++; @@ -96,13 +98,13 @@ open_again: value = libfonts_confsplit__(line); if (!value) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_default_font", "bad line in %s at line %zu", path, lineno); continue; } if (!strcmp(line, name)) { if (*outp) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_default_font", "duplicate definition in %s at line %zu", path, lineno); free(*outp); *outp = NULL; } @@ -110,7 +112,7 @@ open_again: if (!*outp) goto fail; } else if (strcmp(line, "sans-serif") && strcmp(line, "serif") && strcmp(line, "monospace")) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_default_font", "bad font class in %s at line %zu: %s", path, lineno, line); } } diff --git a/libfonts_get_default_rendering_settings.c b/libfonts_get_default_rendering_settings.c index ebdc51c..f40b1d1 100644 --- a/libfonts_get_default_rendering_settings.c +++ b/libfonts_get_default_rendering_settings.c @@ -15,6 +15,7 @@ getn(const char *file_part1, size_t file_part1_len, const char *file_part2, size_t size = 0, off = 0, avail = 0; char *value; unsigned int found_fields = 0; + size_t lineno = 0; if (file_part1_len > SIZE_MAX - file_part2_len - 1) goto enomem; @@ -48,7 +49,7 @@ open_again: case ENODEV: case ENOTDIR: case ENXIO: - /* TODO print warning using `ctx` */ + warning(ctx, errno, "libfonts_get_default_rendering_settings", "failed to open %s:", path); goto out; default: goto out; @@ -68,6 +69,7 @@ open_again: if (!len) break; line[len -= 1] = '\0'; + lineno++; while (isblank(*line)) line++; @@ -79,22 +81,25 @@ open_again: value = libfonts_confsplit__(line); if (!value) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_default_rendering_settings", "bad line in %s at line %zu", path, lineno); continue; } #define X(INDEX, CONFNAME, CNAME, DEFVAL, PARSER)\ if (!strcmp(line, CONFNAME)) {\ if (found_fields & (1U << INDEX)) {\ - /* TODO warning */\ + warning(ctx, 0, "libfonts_get_default_rendering_settings",\ + "duplicate definition in %s at line %zu", path, lineno); \ }\ found_fields |= (1U << INDEX);\ if (!PARSER(&settings->CNAME, value)) {\ - /* TODO warning */\ + warning(ctx, 0, "libfonts_get_default_rendering_settings",\ + "invalid value in %s at line %zu: %s", path, lineno, value);\ }\ } else LIST_RENDERING_SETTINGS(X,) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_default_rendering_settings",\ + "bad definition in %s at line %zu: %s", path, lineno, line);\ } #undef X } diff --git a/libfonts_get_output_rendering_settings.c b/libfonts_get_output_rendering_settings.c index 2cecf3c..b9c1efd 100644 --- a/libfonts_get_output_rendering_settings.c +++ b/libfonts_get_output_rendering_settings.c @@ -18,6 +18,7 @@ getn(const char *file_part1, size_t file_part1_len, const char *file_part2, int in_a_section = 0; char *value; unsigned int found_fields = 0; + size_t lineno = 0; if (file_part1_len > SIZE_MAX - file_part2_len - 1) goto enomem; @@ -51,7 +52,7 @@ open_again: case ENODEV: case ENOTDIR: case ENXIO: - /* TODO print warning using `ctx` */ + warning(ctx, errno, "libfonts_get_default_rendering_settings", "failed to open %s:", path); goto out; default: goto out; @@ -71,6 +72,7 @@ open_again: if (!len) break; line[len -= 1] = '\0'; + lineno++; while (isblank(*line)) { line++; @@ -93,7 +95,7 @@ open_again: } else if (!in_a_section) { value = libfonts_confsplit__(line); if (!value) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_output_rendering_settings", "bad line in %s at line %zu", path, lineno); continue; } /* TODO aliases should be declarable above the first "[%s]" */ @@ -101,21 +103,24 @@ open_again: } else if (in_the_section) { value = libfonts_confsplit__(line); if (!value) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_output_rendering_settings", "bad line in %s at line %zu", path, lineno); continue; } #define X(INDEX, CONFNAME, CNAME, DEFVAL, PARSER)\ if (!strcmp(line, CONFNAME)) {\ if (found_fields & (1U << INDEX)) {\ - /* TODO warning */\ + warning(ctx, 0, "libfonts_get_output_rendering_settings",\ + "duplicate definition in %s at line %zu", path, lineno); \ }\ found_fields |= (1U << INDEX);\ if (!PARSER(&settings->CNAME, value)) {\ - /* TODO warning */\ + warning(ctx, 0, "libfonts_get_output_rendering_settings",\ + "invalid value in %s at line %zu: %s", path, lineno, value);\ }\ } else LIST_RENDERING_SETTINGS(X,) { - /* TODO warning */ + warning(ctx, 0, "libfonts_get_output_rendering_settings",\ + "bad definition in %s at line %zu: %s", path, lineno, line);\ } #undef X } diff --git a/libfonts_parse_aa__.c b/libfonts_parse_aa__.c index 0ed9a3d..8394a56 100644 --- a/libfonts_parse_aa__.c +++ b/libfonts_parse_aa__.c @@ -36,7 +36,7 @@ main(void) ASSERT(res == C);\ strcpy(buf, S);\ for (i = 0; buf[i]; i++)\ - buf[i] = toupper(buf[i]);\ + buf[i] = (char)toupper(buf[i]);\ res = 999;\ ASSERT(libfonts_parse_aa__(&res, buf) == 1);\ ASSERT(res == C);\ diff --git a/libfonts_parse_order__.c b/libfonts_parse_order__.c index 349347e..906c4a9 100644 --- a/libfonts_parse_order__.c +++ b/libfonts_parse_order__.c @@ -37,7 +37,7 @@ main(void) ASSERT(res == C);\ strcpy(buf, S);\ for (i = 0; buf[i]; i++)\ - buf[i] = toupper(buf[i]);\ + buf[i] = (char)toupper(buf[i]);\ res = 999;\ ASSERT(libfonts_parse_order__(&res, buf) == 1);\ ASSERT(res == C);\ -- cgit v1.2.3-70-g09d2