Welcome to little lamb

Code » limb » commit 6cff7e2

buffer.h: Add PUTMSG_FLUSH to flush the buffer

author Olivier Brunel
2023-04-15 06:58:37 UTC
committer Olivier Brunel
2023-05-20 18:06:36 UTC
parent 338c9a128951401e786bd549a6e10ac4067404a7

buffer.h: Add PUTMSG_FLUSH to flush the buffer

Can be useful when needing to flush the buffer but without adding
anything (e.g. a newline), so PUTMSG_LFF doesn't apply.

src/doc/buffer.h.0.md +3 -0
src/doc/buffer.h/buffer_putmsg.3.md +4 -0
src/liblimb/buffer.h/buffer_putmsg.c +2 -0
src/liblimb/include/limb/buffer.h +6 -5

diff --git a/src/doc/buffer.h.0.md b/src/doc/buffer.h.0.md
index 7b94fdf..1b8e1b5 100644
--- a/src/doc/buffer.h.0.md
+++ b/src/doc/buffer.h.0.md
@@ -24,6 +24,9 @@ The following constants are defined :
 : *PUTMSG_ESC*, *ESC*
 :: Can be used as special string given to [buffer_putmsg](3) to toggle escaping
 
+: *PUTMSG_FLUSH*
+:: Can be used as special sting given to [buffer_putmsg](3) to flush the buffer.
+
 : *PUTMSG_HEX_NEXT*
 :: Can be used as special string given to [buffer_putmsg](3) to indicate that
 :: the next element is a pointer to a byte array (i.e. may include NULs) whose
diff --git a/src/doc/buffer.h/buffer_putmsg.3.md b/src/doc/buffer.h/buffer_putmsg.3.md
index ac924d4..ad0416e 100644
--- a/src/doc/buffer.h/buffer_putmsg.3.md
+++ b/src/doc/buffer.h/buffer_putmsg.3.md
@@ -32,6 +32,10 @@ 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.
 
+When *PUTMSG_FLUSH* was given as one of the string, the buffer is immediately
+flushed. Nothing is added to the buffer in its place, and processing continues
+on with remaining strings afterwards.
+
 When *PUTMSG_UINT_NEXT* was given as one of the strings, it acts as an indicator
 that the next element in `strings` should be treated as an unsigned integer
 (instead of a pointer to a string) whose value should be written in decimal form
diff --git a/src/liblimb/buffer.h/buffer_putmsg.c b/src/liblimb/buffer.h/buffer_putmsg.c
index 77e385f..24525b2 100644
--- a/src/liblimb/buffer.h/buffer_putmsg.c
+++ b/src/liblimb/buffer.h/buffer_putmsg.c
@@ -22,6 +22,8 @@ buffer_putmsg(buffer *b, const char * const *as, unsigned int n)
                 puts = buffer_puts;
         } else if (as[i] == PUTMSG_LFF) {
             buffer_putflush(b, "\n", 1);
+        } else if (as[i] == PUTMSG_FLUSH) {
+            buffer_flush(b);
         } else if ((as[i] == PUTMSG_UINT_NEXT || as[i] == PUTMSG_INT_NEXT) && i + 1 < n) {
             char buf_[U64_FMT], *buf = buf_;
             u64 u = (uintptr_t) as[++i];
diff --git a/src/liblimb/include/limb/buffer.h b/src/liblimb/include/limb/buffer.h
index eee3eb9..78da037 100644
--- a/src/liblimb/include/limb/buffer.h
+++ b/src/liblimb/include/limb/buffer.h
@@ -8,11 +8,12 @@
 
 /* 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 PUTMSG_UINT_NEXT    ((void *) 4)    /* next string is an uint */
-#define PUTMSG_INT_NEXT     ((void *) 5)    /* next string is an int */
-#define PUTMSG_HEX_NEXT     ((void *) 6)    /* next strings are data, dlen */
+#define PUTMSG_FLUSH        ((void *) 2)    /* flush buffer */
+#define PUTMSG_LFF          ((void *) 3)    /* add \n and flush buffer */
+#define PUTMSG_ESC          ((void *) 4)    /* toggle escaping */
+#define PUTMSG_UINT_NEXT    ((void *) 5)    /* next string is an uint */
+#define PUTMSG_INT_NEXT     ((void *) 6)    /* next string is an int */
+#define PUTMSG_HEX_NEXT     ((void *) 7)    /* next strings are data, dlen */
 
 #define PUTMSG_ASUINT(n)    ((const char *) (uintptr_t) (n))
 #define PUTMSG_ASINT(n)     ((const char *) ( intptr_t) (n))