Welcome to little lamb

Code » limb » commit b12f470

put: Take a buffer as destination; Add PUT_LFF..

author Olivier Brunel
2023-02-22 07:43:06 UTC
committer Olivier Brunel
2023-02-22 07:50:34 UTC
parent 2be693b11b4902672bd28ec30441d5d325879b68

put: Take a buffer as destination; Add PUT_LFF..

..and macros add() and adde() to simply add string to stdout and stderr
respectively.

Instead of an option to specify buffer_1 or buffer_2 as destination,
simply take a buffer. Much better, and will allow to write to any
buffer.

Also need PUT_LFF to indicate to add '\n' and flush the buffer.

doc/out.3.md +16 -6
doc/put.3.md +10 -12
include/limb/output.h +14 -11
src/put.c +3 -4

diff --git a/doc/out.3.md b/doc/out.3.md
index ba7b22c..dd2530b 100644
--- a/doc/out.3.md
+++ b/doc/out.3.md
@@ -5,7 +5,8 @@
 
 # NAME
 
-out, err, sys, outdie, errdie, sysdie - write text to standard output/error (and terminate process)
+out, err, sys, add, adde, outdie, errdie, sysdie - write text to standard
+output/error (and terminate process)
 
 # SYNOPSIS
 
@@ -16,6 +17,9 @@ void out(...)
 void err(...)
 void sys(...)
 
+void add(...)
+void adde(...)
+
 void outdie(<em>e</em>, ...)
 void errdie(<em>e</em>, ...)
 void sysdie(<em>e</em>, ...)
@@ -27,13 +31,19 @@ All of those are implemented as macros (to [put](3)) and will write to
 either standard outpout or standard error, then maybe terminate the calling
 process or return.
 
-The macro `out`() writes given strings to /stdout/.
+The macro `out`() writes given strings to /stdout/, adding a newline and
+flushing out the buffer.
+
+The macro `err`() writes given strings to /stderr/, adding a newline and
+flushing out the buffer.
+
+The macro `add`() writes given strings to /stdout/.
 
-The macro `err`() writes given strings to /stderr/.
+The macro `adde`() writes given strings to /stderr/.
 
-The macro `sys`() is similar to `err`() put will add a colon, a space and the
-error description from [strerror](3) on `errno`.
+The macro `sys`() is similar to `err`() but will add a colon, a space and the
+error description from [strerror](3) on `errno` after the given strings.
 
 Macros `outdie`(), `errdie`() and `sysdie`() are similar to `out`(), `err`() and
-`sys`() respectively but will terminate the calling process (via [_exit](3)
+`sys`() respectively but will terminate the calling process (via [\_exit](3)
 using `e`).
diff --git a/doc/put.3.md b/doc/put.3.md
index de872c5..af5a4ce 100644
--- a/doc/put.3.md
+++ b/doc/put.3.md
@@ -12,35 +12,33 @@ put - write text to standard output/error
     #include <limb/output.h>
 
 ```pre hl
-void put(int <em>ret</em>, unsigned int <em>opts</em>, const char * const *<em>as</em>, unsigned int <em>n</em>)
+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
-/stdout/ (by default) or /stderr/ if `PUT_ERR` was given in `opts`.
+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`.
 
-A new line is always added at the end.
+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`.
+If `PUT_DIE` was specified, the program then ends - calling [\_exit](3) with
+the value of `ret`. Note that `PUT_DIE` implies `PUT_LFF`.
 
 # FLAGS
 
 Values for `opts` are constructed by a bitwise-inclusive OR of flags from the
 following list :
 
-: `PUT_OUT`
-:: Write to standard output. This is the default, unless `PUT_ERR` is specified.
-
-: `PUT_ERR`
-:: Write to standard error.
-
 : `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 & 0377` as exit status 
+:: Terminate the calling process, returning `ret & 0xff` as exit status 
diff --git a/include/limb/output.h b/include/limb/output.h
index 45d5094..335d74e 100644
--- a/include/limb/output.h
+++ b/include/limb/output.h
@@ -1,26 +1,29 @@
 #ifndef LIMB_OUTPUT_H
 #define LIMB_OUTPUT_H
 
+#include <skalibs/buffer.h>
+
 enum {
-    PUT_OUT     =  0, /* implied default unless PUT_ERR is set */
-    PUT_ERR     = (1 << 0),
-    PUT_SYS     = (1 << 1),
-    PUT_DIE     = (1 << 2),
+    PUT_SYS     = (1 << 0),
+    PUT_DIE     = (1 << 1),
+    PUT_LFF     = (1 << 2),
 };
 
-extern void put(int e, unsigned int opts, const char * const *as, unsigned int n);
+extern void put(buffer *b, int e, unsigned int opts, const char * const *as, unsigned int n);
 
 extern const char *PROG;
 
 #define outarray(...)       ((char const * const []) {__VA_ARGS__})
 #define outalen(...)        (sizeof(outarray(__VA_ARGS__)) / sizeof(char const *))
 
-#define out(...)            put(0, PUT_OUT, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
-#define err(...)            put(0, PUT_ERR, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
-#define sys(...)            put(0, PUT_ERR | PUT_SYS, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
-#define outdie(e, ...)      put(e, PUT_OUT | PUT_DIE, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
-#define errdie(e, ...)      put(e, PUT_ERR | PUT_DIE, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
-#define sysdie(e, ...)      put(e, PUT_ERR | PUT_SYS | PUT_DIE, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define out(...)            put(buffer_1, 0, PUT_LFF, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define err(...)            put(buffer_2, 0, PUT_LFF, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define sys(...)            put(buffer_2, 0, PUT_SYS | PUT_LFF, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define add(...)            put(buffer_1, 0, 0, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define adde(...)           put(buffer_2, 0, 0, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define outdie(e, ...)      put(buffer_1, e, PUT_LFF | PUT_DIE, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define errdie(e, ...)      put(buffer_2, e, PUT_LFF | PUT_DIE, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
+#define sysdie(e, ...)      put(buffer_2, e, PUT_SYS | PUT_LFF | PUT_DIE, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
 
 #define warn(...)           err(PROG, ": warning: ", __VA_ARGS__)
 #define warnu(...)          warn("unable to ", __VA_ARGS__)
diff --git a/src/put.c b/src/put.c
index a0402c9..5e66fc8 100644
--- a/src/put.c
+++ b/src/put.c
@@ -1,13 +1,11 @@
 #include <errno.h>
 #include <unistd.h> /* _exit() */
-#include <skalibs/buffer.h>
 #include "limb/output.h"
 
 void
-put(int r, unsigned int opts, const char * const *as, unsigned int n)
+put(buffer *b, int r, unsigned int opts, const char * const *as, unsigned int n)
 {
     int e = errno;
-    buffer *b = (opts & PUT_ERR) ? buffer_2 : buffer_1;
 
     unsigned int i;
     for (i = 0; i < n; ++i)
@@ -18,7 +16,8 @@ put(int r, unsigned int opts, const char * const *as, unsigned int n)
         buffer_puts(b, strerror(e));
     }
 
-    buffer_putflush(b, "\n", 1);
+    if (opts & (PUT_LFF | PUT_DIE))
+        buffer_putflush(b, "\n", 1);
 
     errno = e;
     if (opts & PUT_DIE)