author | Olivier Brunel
<jjk@jjacky.com> 2023-03-21 18:14:24 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-03-21 18:21:06 UTC |
parent | 46507200eaeef2fa58ca856f9d55c09f63b2278c |
doc/buffer.h.0.md | +18 | -0 |
doc/buffer_putmsg.3.md | +43 | -0 |
doc/put.3.md | +0 | -54 |
include/limb/buffer.h | +8 | -0 |
meta/libs/limb | +1 | -2 |
src/{put.c => buffer_putmsg.c} | +7 | -15 |
diff --git a/doc/buffer.h.0.md b/doc/buffer.h.0.md index d2074f0..d579ac3 100644 --- a/doc/buffer.h.0.md +++ b/doc/buffer.h.0.md @@ -17,6 +17,21 @@ This header defines required functions to performed buffered I/O operations. ! This header is a complement to skalibs' own [skalibs/buffer.h](0) and thusly ! includes said header. +## Constants + +The following constants are defined : + +: *PUTMSG_ESC*, *ESC* +:: Can be used as special string given to [buffer_putmsg](3) to toggle escaping + +: *PUTMSG_SYS* +:: Can be used as special string given to [buffer_putmsg](3) to add error +:: description of `errno` + +: *PUTMSG_LFF* +:: Can be used as special string given to [buffer_putmsg](3) to add a new line +:: and flush the buffer + ## Functions The following functions are defined : @@ -30,3 +45,6 @@ The following functions are defined : : [buffer_putescs](3) :: Similar but for a NUL-terminated string. + +: [buffer_putmsg](3) +:: To write a message, given as an array of NUL-terminated strings diff --git a/doc/buffer_putmsg.3.md b/doc/buffer_putmsg.3.md new file mode 100644 index 0000000..3c0271d --- /dev/null +++ b/doc/buffer_putmsg.3.md @@ -0,0 +1,43 @@ +% limb manual +% buffer_putmsg(3) +% limb 0.0.1 +% 2023.01.16 + +# NAME + +buffer\_putmsg - write message given as array of strings to a buffer + +# SYNOPSIS + + #include <limb/buffer.h> + +```pre hl +void buffer_putmsg(buffer *<em>buf</em>, const char * const *<em>strings</em>, unsigned int <em>nstr</em>) +``` + +# DESCRIPTION + +The `buffer_putmsg`() function will write the message, composed of `nstr` +NUL-terminated strings from array `strings`, into buffer `buf`. + +Note that it is allowed to give NULL as a string, in which case it will be +treated as an empty string (i.e. nothing is added to the buffer). + +When *PUTMSG_ESC* (or *ESC*) was given as one of the strings, escaping mode is +toggled. See [[ESCAPING]] below for more. + +When *PUTMSG_SYS* was given as one of the strings, in its place is put the error +description of `errno`, as returned by [strerror](3). + +When *PUTMSG_LFF* was given as one of the strings, in its place is added a new +line and the buffer is flushed. + +# ESCAPING + +It is possible to pass the special constant *PUTMSG_ESC* (or *ESC*) as one of +the strings, so that following strings will be escaped using [buffer_putesc](3). +It can be specified again to stop escaping and return to "normal" processing. + +Additionally, a double quote (`"`) will be put in place of each constant. + +This escaping can be enabled as many times as needed. diff --git a/doc/put.3.md b/doc/put.3.md deleted file mode 100644 index 34cb41d..0000000 --- a/doc/put.3.md +++ /dev/null @@ -1,54 +0,0 @@ -% limb manual -% put(3) -% limb 0.0.1 -% 2023.01.16 - -# NAME - -put - write text to standard output/error - -# SYNOPSIS - - #include <limb/output.h> - -```pre hl -void put(buffer *<em>b</em>, int <em>ret</em>, unsigned int <em>opts</em>, const char * const *<em>as</em>, unsigned int <em>n</em>) -``` - -# DESCRIPTION - -The `put`() function will write `n` strings, given in the array `as`, either to -the given buffer `b`. - -If `PUT_SYS` was specified, a suffix composed of a colon and a space is added, -followed by the text return by [strerror](3) for `errno`. - -If `PUT_LFF` was specified a new line is always added at the end and the buffer -is flushed. - -If `PUT_DIE` was specified, the program then ends - calling [\_exit](3) with -the value of `ret`. Note that `PUT_DIE` implies `PUT_LFF`. - -# ESCAPING - -It is possible to pass the special constant `ESC` as one of the strings, so that -following strings will be escaped using [buffer_putesc](3). It can be specified -again to stop escaping and return to "normal" processing. - -Additionally, a double quote (`"`) will be put in place of each `ESC` constant. - -This escaping can be enabled as many times as needed. - -# FLAGS - -Values for `opts` are constructed by a bitwise-inclusive OR of flags from the -following list : - -: `PUT_SYS` -:: Append a colon, a space, and the error string computed from `errno` - -: `PUT_LFF` -:: Append a newline (`\n`) then flush the buffer - -: `PUT_DIE` -:: Terminate the calling process, returning `ret & 0xff` as exit status diff --git a/include/limb/buffer.h b/include/limb/buffer.h index 1b7c4e5..ea8372f 100644 --- a/include/limb/buffer.h +++ b/include/limb/buffer.h @@ -6,6 +6,14 @@ #include <skalibs/buffer.h> +/* special values usable as strings for buffer_putmsg() */ +#define PUTMSG_SYS ((void *) 1) /* add strerror(errno) */ +#define PUTMSG_LFF ((void *) 2) /* add \n and flush buffer */ +#define PUTMSG_ESC ((void *) 3) /* toggle escaping */ + +#define ESC PUTMSG_ESC + +extern void buffer_putmsg(buffer *b, const char * const *as, unsigned int n); extern size_t buffer_putescall(buffer *b, const char *s, size_t len, size_t *pos); extern ssize_t buffer_putesc(buffer *b, const char *s, size_t len); extern ssize_t buffer_putescs(buffer *b, const char *s); diff --git a/meta/libs/limb b/meta/libs/limb index f3c86aa..7e81f6f 100644 --- a/meta/libs/limb +++ b/meta/libs/limb @@ -1,5 +1,3 @@ -# ouput.h -obj/put.o # unix-transactional.H obj/openc_createat.o obj/openc_exclat.o @@ -26,6 +24,7 @@ obj/sacolptr.o obj/buffer_putescall.o obj/buffer_putesc.o obj/buffer_putescs.o +obj/buffer_putmsg.o # find msb obj/msb64.o # {,un}pack u64 diff --git a/src/put.c b/src/buffer_putmsg.c similarity index 62% rename from src/put.c rename to src/buffer_putmsg.c index ba5f8c0..3409038 100644 --- a/src/put.c +++ b/src/buffer_putmsg.c @@ -2,37 +2,29 @@ * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ /* SPDX-License-Identifier: GPL-2.0-only */ #include <errno.h> -#include <unistd.h> /* _exit() */ #include "limb/buffer.h" -#include "limb/output.h" void -put(buffer *b, int r, unsigned int opts, const char * const *as, unsigned int n) +buffer_putmsg(buffer *b, const char * const *as, unsigned int n) { int e = errno; - ssize_t (*puts) (buffer *b, const char *s) = buffer_puts; + for (unsigned i = 0; i < n; ++i) { - if (as[i] == ESC) { + if (as[i] == PUTMSG_SYS) { + puts(b, strerror(e)); + } else if (as[i] == PUTMSG_ESC) { buffer_put(b, "\"", 1); if (puts == buffer_puts) puts = buffer_putescs; else puts = buffer_puts; + } else if (as[i] == PUTMSG_LFF) { + buffer_putflush(b, "\n", 1); } else if (as[i] && as[i][0]) { puts(b, as[i]); } } - if (opts & PUT_SYS) { - buffer_put(b, ": ", 2); - buffer_puts(b, strerror(e)); - } - - if (opts & (PUT_LFF | PUT_DIE)) - buffer_putflush(b, "\n", 1); - errno = e; - if (opts & PUT_DIE) - _exit(r); }