diff options
author | Mattias Andrée <maandree@kth.se> | 2018-11-06 20:32:42 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-11-06 20:32:42 +0100 |
commit | 0335a7cf215f1c5cba9ae8a49b3dbd1b980a20b1 (patch) | |
tree | e87a3444037b27f7a2743ea84e7af1d9ef50c1bf | |
parent | Add tests for mem[p]setelem and add man pages for libsimple_default_failure_exit (diff) | |
download | libsimple-0335a7cf215f1c5cba9ae8a49b3dbd1b980a20b1.tar.gz libsimple-0335a7cf215f1c5cba9ae8a49b3dbd1b980a20b1.tar.bz2 libsimple-0335a7cf215f1c5cba9ae8a49b3dbd1b980a20b1.tar.xz |
Add libsimple_eprintf_preprint and libsimple_eprintf_postprint
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | libsimple.c | 3 | ||||
-rw-r--r-- | libsimple.h | 8 | ||||
-rw-r--r-- | libsimple/printf.h | 34 | ||||
l--------- | man/libsimple_eprintf_postprint.3 | 1 | ||||
l--------- | man/libsimple_eprintf_preprint.3 | 1 | ||||
-rw-r--r-- | man/libsimple_vweprintf.3 | 43 | ||||
-rw-r--r-- | test.c | 7 | ||||
-rw-r--r-- | test.h | 12 | ||||
-rw-r--r-- | vweprintf.c | 35 |
9 files changed, 129 insertions, 15 deletions
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 @@ -2,6 +2,40 @@ /** + * 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 <libsimple.h> 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 @@ -294,6 +294,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; int r; @@ -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)); |