Welcome to little lamb

Code » limb » commit 3aa5cac

buffer.h: Remove PUTMSG_LFF & add PUTMSG_TOGGLE_ESC

author Olivier Brunel
2023-04-17 12:49:23 UTC
committer Olivier Brunel
2023-05-20 18:06:37 UTC
parent 89724c6fd78745533d752a495efeac3027ee422c

buffer.h: Remove PUTMSG_LFF & add PUTMSG_TOGGLE_ESC

One can simply use "\n" and PUTMSG_FLUSH for the former. The later will
allow to use escaping "silently", i.e. without the enclosing quotes.

src/doc/buffer.h.0.md +4 -5
src/doc/buffer.h/buffer_putmsg.3.md +13 -10
src/liblimb/buffer.h/buffer_putmsg.c +3 -4
src/liblimb/include/limb/buffer.h +2 -2
src/liblimb/include/limb/output.h +16 -16
src/liblimb/output.h/list_matches_full.c +1 -1

diff --git a/src/doc/buffer.h.0.md b/src/doc/buffer.h.0.md
index 1b8e1b5..378e856 100644
--- a/src/doc/buffer.h.0.md
+++ b/src/doc/buffer.h.0.md
@@ -22,7 +22,7 @@ This header defines required functions to performed buffered I/O operations.
 The following constants are defined :
 
 : *PUTMSG_ESC*, *ESC*
-:: Can be used as special string given to [buffer_putmsg](3) to toggle escaping
+:: Same as *PUTMSG_TOGGLE_ESC* but also add a double-quote.
 
 : *PUTMSG_FLUSH*
 :: Can be used as special sting given to [buffer_putmsg](3) to flush the buffer.
@@ -34,14 +34,13 @@ The following constants are defined :
 :: integer. The byte array's content should be written into the buffer as
 :: hexadecimal.
 
-: *PUTMSG_LFF*
-:: Can be used as special string given to [buffer_putmsg](3) to add a new line
-:: and flush the buffer
-
 : *PUTMSG_SYS*
 :: Can be used as special string given to [buffer_putmsg](3) to add error
 :: description of `errno`
 
+: *PUTMSG_TOGGLE_ESC*
+:: Can be used as special string given to [buffer_putmsg](3) to toggle escaping.
+
 : *PUTMSG_UINT_NEXT*
 :: Can be used as special string given to [buffer_putmsg](3) to indicate that
 :: the next element should be treated as an unsigned integer, whose decimal
diff --git a/src/doc/buffer.h/buffer_putmsg.3.md b/src/doc/buffer.h/buffer_putmsg.3.md
index ad0416e..e817112 100644
--- a/src/doc/buffer.h/buffer_putmsg.3.md
+++ b/src/doc/buffer.h/buffer_putmsg.3.md
@@ -23,15 +23,15 @@ 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
+When *PUTMSG_TOGGLE_ESC* was given as one of the strings, escaping mode is
 toggled. See [[ESCAPING]] below for more.
 
+When *PUTMSG_ESC* (or *ESC*) was given as one of the strings, escaping mode is
+toggled (See [[ESCAPING]] below for more) and a double-quote is added.
+
 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.
-
 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.
@@ -70,11 +70,14 @@ dump, as described on [buffer_puthex](3).
 
 # 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 is possible to pass the special constant *PUTMSG_TOGGLE_ESC* or *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.
+*PUTMSG_TOGGLE_ESC* only has for effect to toggle the escaping, while
+*PUTMSG_ESC* and *ESC* will also have a double quote (`"`) will be put in place
+of each constant.
 
 This escaping can be enabled as many times as needed.
 
@@ -85,7 +88,7 @@ following :
 
 ```c
 int r = 42;
-const char *strings[] = { "The answer is ", PUTMSG_UINT_NEXT, PUTMSG_ASUINT(r), PUTMSG_LFF };
+const char *strings[] = { "The answer is ", PUTMSG_UINT_NEXT, PUTMSG_ASUINT(r), "\n", PUTMSG_FLUSH };
 buffer_putmsg(buffer_1, strings, sizeof(strings) / sizeof(*strings));
 ```
 
@@ -93,6 +96,6 @@ Which is identical to the following :
 
 ```c
 int r = 42;
-const char *strings[] = { "The answer is ", PUTMSG_UINT(r), PUTMSG_LFF };
-buffer_putmsg(buffer_1, strings, 4);
+const char *strings[] = { "The answer is ", PUTMSG_UINT(r), "\n", PUTMSG_FLUSH };
+buffer_putmsg(buffer_1, strings, 5);
 ```
diff --git a/src/liblimb/buffer.h/buffer_putmsg.c b/src/liblimb/buffer.h/buffer_putmsg.c
index 24525b2..435a272 100644
--- a/src/liblimb/buffer.h/buffer_putmsg.c
+++ b/src/liblimb/buffer.h/buffer_putmsg.c
@@ -14,14 +14,13 @@ buffer_putmsg(buffer *b, const char * const *as, unsigned int n)
     for (unsigned i = 0; i < n; ++i) {
         if (as[i] == PUTMSG_SYS) {
             puts(b, strerror(e));
-        } else if (as[i] == PUTMSG_ESC) {
-            buffer_put(b, "\"", 1);
+        } else if (as[i] == PUTMSG_ESC || as[i] == PUTMSG_TOGGLE_ESC) {
+            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] == PUTMSG_FLUSH) {
             buffer_flush(b);
         } else if ((as[i] == PUTMSG_UINT_NEXT || as[i] == PUTMSG_INT_NEXT) && i + 1 < n) {
diff --git a/src/liblimb/include/limb/buffer.h b/src/liblimb/include/limb/buffer.h
index 78da037..b7b4b06 100644
--- a/src/liblimb/include/limb/buffer.h
+++ b/src/liblimb/include/limb/buffer.h
@@ -9,8 +9,8 @@
 /* special values usable as strings for buffer_putmsg() */
 #define PUTMSG_SYS          ((void *) 1)    /* add strerror(errno) */
 #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_TOGGLE_ESC   ((void *) 3)    /* toggle escaping */
+#define PUTMSG_ESC          ((void *) 4)    /* toggle escaping & add quote */
 #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 */
diff --git a/src/liblimb/include/limb/output.h b/src/liblimb/include/limb/output.h
index 6f011d7..6339370 100644
--- a/src/liblimb/include/limb/output.h
+++ b/src/liblimb/include/limb/output.h
@@ -28,27 +28,27 @@ extern const char *PROG;
 #define outmsgdie(e,l,...)  out_putmsgdie(e, l, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
 #define errmsgdie(e,l,...)  err_putmsgdie(e, l, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
 
-#define quiet(...)          outmsg(OLVL_QUIET, __VA_ARGS__, PUTMSG_LFF)
-#define out(...)            outmsg(OLVL_NORMAL, __VA_ARGS__, PUTMSG_LFF)
-#define verb(...)           outmsg(OLVL_VERBOSE, __VA_ARGS__, PUTMSG_LFF)
-#define dbg(...)            dbgmsg(OLVL_DEBUG, __VA_ARGS__, PUTMSG_LFF)
-#define dbgverb(...)        dbgmsg(OLVL_MAXIMUM, __VA_ARGS__, PUTMSG_LFF)
+#define quiet(...)          outmsg(OLVL_QUIET, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define out(...)            outmsg(OLVL_NORMAL, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define verb(...)           outmsg(OLVL_VERBOSE, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define dbg(...)            dbgmsg(OLVL_DEBUG, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define dbgverb(...)        dbgmsg(OLVL_MAXIMUM, __VA_ARGS__, "\n", PUTMSG_FLUSH)
 
-#define err(...)            errmsg(OLVL_NORMAL, __VA_ARGS__, PUTMSG_LFF)
-#define errverb(...)        errmsg(OLVL_VERBOSE, __VA_ARGS__, PUTMSG_LFF)
-#define sys(...)            errmsg(OLVL_NORMAL, __VA_ARGS__, ": ", PUTMSG_SYS, PUTMSG_LFF)
-#define sysverb(...)        errmsg(OLVL_VERBOSE, __VA_ARGS__, ": ", PUTMSG_SYS, PUTMSG_LFF)
+#define err(...)            errmsg(OLVL_NORMAL, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define errverb(...)        errmsg(OLVL_VERBOSE, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define sys(...)            errmsg(OLVL_NORMAL, __VA_ARGS__, ": ", PUTMSG_SYS, "\n", PUTMSG_FLUSH)
+#define sysverb(...)        errmsg(OLVL_VERBOSE, __VA_ARGS__, ": ", PUTMSG_SYS, "\n", PUTMSG_FLUSH)
 
 #define add(...)            outmsg(OLVL_NORMAL, __VA_ARGS__)
 #define adde(...)           errmsg(OLVL_NORMAL, __VA_ARGS__)
 
-#define quietdie(e,...)     outmsgdie(e, OLVL_QUIET, __VA_ARGS__, PUTMSG_LFF)
-#define outdie(e,...)       outmsgdie(e, OLVL_NORMAL, __VA_ARGS__, PUTMSG_LFF)
-#define verbdie(e,...)      outmsgdie(e, OLVL_VERBOSE, __VA_ARGS__, PUTMSG_LFF)
-#define errdie(e,...)       errmsgdie(e, OLVL_NORMAL, __VA_ARGS__, PUTMSG_LFF)
-#define errverbdie(e,...)   errmsgdie(e, OLVL_VERBOSE, __VA_ARGS__, PUTMSG_LFF)
-#define sysdie(e,...)       errmsgdie(e, OLVL_NORMAL, __VA_ARGS__, ": ", PUTMSG_SYS, PUTMSG_LFF)
-#define sysverbdie(e,...)   errmsgdie(e, OLVL_VERBOSE, __VA_ARGS__, ": ", PUTMSG_SYS, PUTMSG_LFF)
+#define quietdie(e,...)     outmsgdie(e, OLVL_QUIET, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define outdie(e,...)       outmsgdie(e, OLVL_NORMAL, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define verbdie(e,...)      outmsgdie(e, OLVL_VERBOSE, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define errdie(e,...)       errmsgdie(e, OLVL_NORMAL, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define errverbdie(e,...)   errmsgdie(e, OLVL_VERBOSE, __VA_ARGS__, "\n", PUTMSG_FLUSH)
+#define sysdie(e,...)       errmsgdie(e, OLVL_NORMAL, __VA_ARGS__, ": ", PUTMSG_SYS, "\n", PUTMSG_FLUSH)
+#define sysverbdie(e,...)   errmsgdie(e, OLVL_VERBOSE, __VA_ARGS__, ": ", PUTMSG_SYS, "\n", PUTMSG_FLUSH)
 
 #define warn(...)           err(PROG, ": warning: ", __VA_ARGS__)
 #define warnu(...)          warn("unable to ", __VA_ARGS__)
diff --git a/src/liblimb/output.h/list_matches_full.c b/src/liblimb/output.h/list_matches_full.c
index 2d8434f..6957617 100644
--- a/src/liblimb/output.h/list_matches_full.c
+++ b/src/liblimb/output.h/list_matches_full.c
@@ -16,5 +16,5 @@ list_matches_full(obuffer_putmsgfn putmsg, u8 level, const char *intro,
     for (list += llen; (el = * (const char **) (list + offset)); list += llen)
         if (!strncmp(str, el, slen))
             putmsg(level, outarray(sep, prefix, el), 3);
-    putmsg(level, outarray(outro, PUTMSG_LFF), 2);
+    putmsg(level, outarray(outro, "\n", PUTMSG_FLUSH), 2);
 }