From 0335a7cf215f1c5cba9ae8a49b3dbd1b980a20b1 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 6 Nov 2018 20:32:42 +0100 Subject: Add libsimple_eprintf_preprint and libsimple_eprintf_postprint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libsimple.c | 3 --- libsimple.h | 8 -------- libsimple/printf.h | 34 +++++++++++++++++++++++++++++++ man/libsimple_eprintf_postprint.3 | 1 + man/libsimple_eprintf_preprint.3 | 1 + man/libsimple_vweprintf.3 | 43 ++++++++++++++++++++++++++++++++++++++- test.c | 7 +++++++ test.h | 12 +++++++++++ vweprintf.c | 35 ++++++++++++++++++++++++++++--- 9 files changed, 129 insertions(+), 15 deletions(-) create mode 120000 man/libsimple_eprintf_postprint.3 create mode 120000 man/libsimple_eprintf_preprint.3 diff --git a/libsimple.c b/libsimple.c index 93f7357..3026160 100644 --- a/libsimple.c +++ b/libsimple.c @@ -3,9 +3,6 @@ #ifndef TEST -int libsimple_default_failure_exit = 1; - - #else #include "test.h" diff --git a/libsimple.h b/libsimple.h index 2c39098..4663eb4 100644 --- a/libsimple.h +++ b/libsimple.h @@ -47,14 +47,6 @@ #endif -/** - * Exit value for `libsimple_eprintf` - * - * Default value is 1 - */ -extern int libsimple_default_failure_exit; - - #include "libsimple/printf.h" #include "libsimple/definitions.h" #include "libsimple/memalloc.h" diff --git a/libsimple/printf.h b/libsimple/printf.h index 9b6253c..cf7192e 100644 --- a/libsimple/printf.h +++ b/libsimple/printf.h @@ -1,6 +1,40 @@ /* See LICENSE file for copyright and license details. */ +/** + * Exit value for `libsimple_eprintf` + * + * Default value is 1 + */ +extern int libsimple_default_failure_exit; + + +/** + * Unless `NULL`, called from `libsimple_vweprintf` + * before the `eprintf`-family of functions print + * the message. + * + * `errno` is not necessarily the same value as + * it was when `libsimple_vweprintf` was called. + * + * Default value is `NULL` + */ +extern void (*libsimple_eprintf_preprint)(void); + + +/** + * Unless `NULL`, called from `libsimple_vweprintf` + * after the `eprintf`-family of functions print + * the message. + * + * `errno` is not necessarily the same value as + * it was when `libsimple_vweprintf` was called. + * + * Default value is `NULL` + */ +extern void (*libsimple_eprintf_postprint)(void); + + /** * Version of `printf` that allocates, on the heap, a * sufficiently large string and writes the output to it diff --git a/man/libsimple_eprintf_postprint.3 b/man/libsimple_eprintf_postprint.3 new file mode 120000 index 0000000..39ff456 --- /dev/null +++ b/man/libsimple_eprintf_postprint.3 @@ -0,0 +1 @@ +libsimple_vweprintf.3 \ No newline at end of file diff --git a/man/libsimple_eprintf_preprint.3 b/man/libsimple_eprintf_preprint.3 new file mode 120000 index 0000000..39ff456 --- /dev/null +++ b/man/libsimple_eprintf_preprint.3 @@ -0,0 +1 @@ +libsimple_vweprintf.3 \ No newline at end of file diff --git a/man/libsimple_vweprintf.3 b/man/libsimple_vweprintf.3 index ca241b1..06d339d 100644 --- a/man/libsimple_vweprintf.3 +++ b/man/libsimple_vweprintf.3 @@ -6,6 +6,8 @@ libsimple_vweprintf \- print an error message #include extern int libsimple_default_failure_exit; +extern void (*libsimple_eprint_preprint)(void); +extern void (*libsimple_eprint_postprint)(void); void libsimple_vweprintf(const char *\fIfmt\fP, va_list \fIap\fP); static inline void libsimple_weprintf(const char *\fIfmt\fP, ...); @@ -74,7 +76,7 @@ does not start with the output is preffixed with .nf - \fB\(dq%s: \(dq, \fPIargv0\fP + \fB\(dq%s: \(dq, \fP\fIargv0\fP .fi .PP The @@ -110,6 +112,45 @@ functions that terminate the process by calling the exit value of the process will be .IR libsimple_default_failure_exit (3), which is 1 by default. +.PP +The +.BR libsimple_vweprintf (), +.BR libsimple_weprintf (), +.BR libsimple_venprintf (), +.BR libsimple_enprintf (), +.BR libsimple_veprintf (), +and +.BR libsimple_eprintf () +functions call +.I libsimple_eprint_preprint +unless it is +.B NULL +before they print the message, after printing +the message they call +.I libsimple_eprint_postprint +unless it is +.BR NULL . +The +.I libsimple_eprint_preprint +and +.I libsimple_eprint_postprint +variables are +.B NULL +byte default. +.I errno +may have a different value from when the +.BR libsimple_vweprintf (), +.BR libsimple_weprintf (), +.BR libsimple_venprintf (), +.BR libsimple_enprintf (), +.BR libsimple_veprintf (), +or +.BR libsimple_eprintf () +function called when +.I libsimple_eprint_preprint +or +.I libsimple_eprint_postprint +is called. .SH RETURN VALUE None. .SH ERRORS diff --git a/test.c b/test.c index 80b7ad7..f785483 100644 --- a/test.c +++ b/test.c @@ -293,6 +293,13 @@ fprintf(FILE *restrict stream, const char *restrict format, ...) int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap) +{ + return test_vfprintf(stream, format, ap); +} + + +int +test_vfprintf(FILE *restrict stream, const char *restrict format, va_list ap) { size_t old_alloc_fail_in = alloc_fail_in; va_list ap2; diff --git a/test.h b/test.h index 3aba488..29e6f13 100644 --- a/test.h +++ b/test.h @@ -83,3 +83,15 @@ size_t get_pagesize(void); size_t round_up(size_t); int have_custom_malloc(void); /* return 0 if run under valgrind(1) */ struct allocinfo *get_allocinfo(void *); + + +int test_vfprintf(FILE *restrict stream, const char *restrict format, va_list ap); + +static inline int +test_fprintf(FILE *restrict stream, const char *restrict format, ...) +{ + va_list ap; + va_start(ap, format); + return test_vfprintf(stream, format, ap); + va_end(ap); +} diff --git a/vweprintf.c b/vweprintf.c index 96a82a7..9263d52 100644 --- a/vweprintf.c +++ b/vweprintf.c @@ -6,6 +6,11 @@ extern char *argv0; +int libsimple_default_failure_exit = 1; +void (*libsimple_eprintf_preprint)(void) = NULL; +void (*libsimple_eprintf_postprint)(void) = NULL; + + void libsimple_vweprintf(const char *fmt, va_list ap) { @@ -44,6 +49,8 @@ libsimple_vweprintf(const char *fmt, va_list ap) suffix1 = "\n"; } + if (libsimple_eprintf_preprint) + libsimple_eprintf_preprint(); if (message) { /* This is to avoid mangling when multiple processes are writting. */ fprintf(stderr, "%s%s%s%s%s%s", prefix1, prefix2, message, suffix1, suffix2, suffix3); @@ -52,6 +59,8 @@ libsimple_vweprintf(const char *fmt, va_list ap) vfprintf(stderr, fmt, ap); fprintf(stderr, "%s%s%s", suffix1, suffix2, suffix3); } + if (libsimple_eprintf_postprint) + libsimple_eprintf_postprint(); errno = saved_errno; } @@ -64,6 +73,18 @@ libsimple_vweprintf(const char *fmt, va_list ap) # pragma GCC diagnostic ignored "-Wformat-zero-length" #endif +static void +preprint(void) +{ + test_fprintf(stderr, "pre\n"); +} + +static void +postprint(void) +{ + test_fprintf(stderr, "post\n"); +} + int main(void) { @@ -92,15 +113,23 @@ main(void) for (j = 0; j < 3; j++) { T("%s%s%s\n", "abc", "123", "xyz"); assert_stderr("%s%s\n", prefix, "abc123xyz"); + libsimple_eprintf_preprint = preprint; T("%s%s%s\n", "abc", "123", "\n"); - assert_stderr("%s%s\n", prefix, "abc123\n"); + assert_stderr("pre\n%s%s\n", prefix, "abc123\n"); + libsimple_eprintf_preprint = NULL; + libsimple_eprintf_postprint = postprint; T("%s%s%s", "abc", "123", "xyz"); - assert_stderr("%s%s\n", prefix, "abc123xyz"); + assert_stderr("%s%s\npost\n", prefix, "abc123xyz"); + libsimple_eprintf_postprint = NULL; T("%s%s%s", "abc", "123", "\n"); assert_stderr("%s%s\n", prefix, "abc123\n"); errno = EDOM; + libsimple_eprintf_preprint = preprint; + libsimple_eprintf_postprint = postprint; T("%s%s%s:", "abc", "123", "\n"); - assert_stderr("%s%s: %s\n", prefix, "abc123\n", strerror(EDOM)); + assert_stderr("pre\n%s%s: %s\npost\n", prefix, "abc123\n", strerror(EDOM)); + libsimple_eprintf_preprint = NULL; + libsimple_eprintf_postprint = NULL; errno = ERANGE; T("%s%s%s:", "abc", "123", "\n"); assert_stderr("%s%s: %s\n", prefix, "abc123\n", strerror(ERANGE)); -- cgit v1.2.3-70-g09d2