author | Olivier Brunel
<jjk@jjacky.com> 2023-04-30 07:20:47 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-05-20 18:06:39 UTC |
parent | fd02055b524731a349911b46cac241d95804146c |
src/doc/buffer.h.0.md | +10 | -59 |
src/doc/buffer.h/buffer_putmsg.3.md | +40 | -23 |
src/liblimb/buffer.h/buffer_putmsg.c | +8 | -8 |
src/liblimb/include/limb/buffer.h | +19 | -15 |
src/liblimb/include/limb/output.h | +4 | -4 |
diff --git a/src/doc/buffer.h.0.md b/src/doc/buffer.h.0.md index 3163cac..4cc0ee2 100644 --- a/src/doc/buffer.h.0.md +++ b/src/doc/buffer.h.0.md @@ -21,70 +21,21 @@ This header defines required functions to performed buffered I/O operations. The following constants are defined : -: *PUTMSG_ESC*, *ESC* -:: 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. - -: *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 -:: length is specified as the following element, to be treated as an unsigned -:: integer. The byte array's content should be written into the buffer as -:: hexadecimal. - -: *PUTMSG_LEN_NEXT* -:: Can be used as special string given to [buffer_putmsg](3) to indicate that -:: the next element is a pointer to a string whose length to be printed is -:: specified as the following element, to be treated as an unsigned integer. - -: *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 -:: representation should be written into the buffer - -: *PUTMSG_INT_NEXT* -:: Same as *PUTMSG_UINT_NEXT* only for a signed integer +: *PUTMSG_FLUSH*, *PUTMSG_TOGGLE_ESC*, *PUTMSG_ESC*, *PUTMSG_UINT_NEXT*, +: *PUTMSG_INT_NEX*, *PUTMSG_ERR_NEXT*, *PUTMSG_LEN_NEXT*, *PUTMSG_HEX_NEXT*, +: *PUTMSG_DUMP_NEXT*, *ESC* +:: Can be used as a string given to [buffer_putmsg](3) with a special meaning. ## Macros The following macros are defined : -: *PUTMSG_ASUINT(`n`)* -:: Typecast `n` so it can be passed into an array of strings to -:: [buffer_putmsg](3), following *PUTMSG_UINT_NEXT*, in order to have its -:: decimal representation written into the buffer - -: *PUTMSG_ASINT(`n`)* -:: Same as *PUTMSG_ASUINT(`n`)* but with a signed integer (following -:: *PUTMSG_INT_NEXT*) - -: *PUTMSG_HEX(`data`,`dlen`)*, *HEX(`data`,`dlen`)* -:: Shorthand to easily add a blob/byte array whose content shall be written out -:: in hexadecimal form into a message. This is intended for use in a -:: coma-separated list, as it expands to: -:: `PUTMSG_HEX_NEXT, data, PUTMSG_ASUINT(dlen)` - -: *PUTMSG_LEN(`data`, `dlen`)*, *LEN(`data`, `dlen`)* -:: Shorthand to easily write (part of) a string that's not NUL terminated This -:: is intended for use in a coma-separated list, as it expands to: -:: `PUTMSG_LEN_NEXT, data, PUTMSG_ASUINT(dlen)` - -: *PUTMSG_UINT(`n`)* -:: Shorthand to easily add an unsigned number into a message. This is intended -:: for use in a coma-separated list, as it expands to: -:: `PUTMSG_UINT_NEXT, PUTMSG_ASUINT(n)` - -: *PUTMSG_INT(`n`)* -:: Same as *PUTMSG_UINT(`n`)* only for a signed integer +: *PUTMSG_UINT(`n`)*, *PUTMSG_INT(`n`)*, *PUTMSG_ERR(`n`)*, +: *PUTMSG_LEN(`data`,`dlen`)*, *PUTMSG_HEX(`data`,`dlen`)*, +: *PUTMSG_DUMP(`data`,`dlen`)* +: *PMUINT(`n`)*, *PMINT(`n`)*, *PMERR(`n`)*, *PMLEN(`data`,`dlen`)*, +: *PMHEX(`data`,`dlen`)*, *PMDMP(`data`,`dlen`)* +:: Can be used for special processing to [buffer_putmsg](3). ## Functions diff --git a/src/doc/buffer.h/buffer_putmsg.3.md b/src/doc/buffer.h/buffer_putmsg.3.md index 4c86f11..d9edb41 100644 --- a/src/doc/buffer.h/buffer_putmsg.3.md +++ b/src/doc/buffer.h/buffer_putmsg.3.md @@ -23,19 +23,21 @@ 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_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. +## Special "strings" -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). +A few constants are defined that can be given as one strings in the array +`strings`, to have special meaning. 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_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_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 @@ -44,6 +46,16 @@ into the buffer. When *PUTMSG_INT_NEXT* was given as one of the strings, it does the same as *PUTMSG_UINT_NEXT* only treating the next element as a signed integer. +When *PUTMSG_ERR_NEXT* was given as one of the strings, in its place is put the +error description of the error, as returned by [strerror](3), whose value is +to be obtained from the following element, treated as an unsigned integer +(instead of a pointer to a string). + +When *PUTMSG_LEN_NEXT* was given as one of the strings, it acts as an indicator +that the next element in `strings` is a string that may not be NUL-terminated, +and whose length to be written into `buf` is to be obtained for the following +element, treated as an unsigned integer (instead of a pointer to a string). + When *PUTMSG_HEX_NEXT* was given as one of the strings, it acts as an indicator that the next element in `strings` is a byte-array whose length in to be obtained from the following element, treated as an unsigned integer (instead of @@ -57,37 +69,42 @@ HEX_3BLOCK_LF | HEX_LF` given to [buffer_puthex](3), so a space is added after each byte, another space after every 8 bytes, a newline after every 24 bytes, and a trailing new line is added. -When *PUTMSG_LEN_NEXT* was given as one of the strings, it acts as an indicator -that the next element in `strings` is a string that may not be NUL-terminated, -and whose length to be written into `buf` is to be obtained for the following -element, treated as an unsigned integer (instead of a pointer to s string). - ! INFO: ! A few macros are available, in order to simply things: ! : *PUTMSG_ASUINT(`n`)*, *PUTMSG_ASINT(`n`)* ! :: To cast `n` as unsigned or signed integer, respectively, to be added into ! :: a string array. ! -! : *PUTMSG_UINT(`n`)*, *PUTMSG_INT(`n`)* -! :: Shorthand to easily add an unsigned or signed number, respectively, into a -! :: message. This is intended for use in a coma-separated list, as it expands -! :: to, e.g: `PUTMSG_UINT_NEXT, PUTMSG_ASUINT(n)` +! : *PUTMSG_UINT(`n`)*, *PMUINT(`n`)* +! :: Shorthand to easily add an unsigned number into a message. This is +! :: intended for use in a coma-separated list, as it expands to : +! :: `PUTMSG_UINT_NEXT, PUTMSG_ASUINT(n)` +! +! : *PUTMSG_INT(`n`)*, *PMINT(`n`)* +! :: Shorthand to easily add a signed number into a message. This is intended +! :: for use in a coma-separated list, as it expands to : +! :: `PUTMSG_INT_NEXT, PUTMSG_ASINT(n)` ! -! : *PUTMSG_HEX(`data`,`dlen`)*, *HEX(`data`,`dlen`)* +! :: *PUTMSG_ERR(`n`)*, *PMERR(`n`)* +! :: Shorthand to easily add an error description. This is intended for use in a +! :: coma-separated list, as it expends to : +! :: `PUTMSG_ERR_NEXT, PUTMSG_ASUINT(n)` +! +! : *PUTMSG_LEN(`data`, `dlen`)*, *PMLEN(`data`, `dlen`)* +! :: Shorthand to easily write (part of) a string that's not NUL terminated This +! :: is intended for use in a coma-separated list, as it expands to: +! :: `PUTMSG_LEN_NEXT, data, PUTMSG_ASUINT(dlen)` +! +! : *PUTMSG_HEX(`data`,`dlen`)*, *PMHEX(`data`,`dlen`)* ! :: Shorthand to easily add a blob/byte array whose content shall be written ! :: out in hexadecimal form into a message. This is intended for use in a ! :: coma-separated, as it expands to: ! :: `PUTMSG_HEX_NEXT, data, PUTMSG_ASUINT(dlen)` ! -! : *PUTMSG_DUMP(`data`, `dlen`)*, *DUMP(`data`, `dlen`)* +! : *PUTMSG_DUMP(`data`, `dlen`)*, *PMDUMP(`data`, `dlen`)* ! :: Shorthand to easily add an hexadecimal dump of a data blob/byte array. This ! :: is intended for use in a coma-separated list, as it expands to: ! :: `PUTMSG_DUMP_NEXT, data, PUTMSG_ASUINT(dlen)` -! -! : *PUTMSG_LEN(`data`, `dlen`)*, *LEN(`data`, `dlen`)* -! :: Shorthand to easily write (part of) a string that's not NUL terminated This -! :: is intended for use in a coma-separated list, as it expands to: -! :: `PUTMSG_LEN_NEXT, data, PUTMSG_ASUINT(dlen)` # ESCAPING diff --git a/src/liblimb/buffer.h/buffer_putmsg.c b/src/liblimb/buffer.h/buffer_putmsg.c index 7b1d147..fc4988b 100644 --- a/src/liblimb/buffer.h/buffer_putmsg.c +++ b/src/liblimb/buffer.h/buffer_putmsg.c @@ -12,8 +12,8 @@ buffer_putmsg(buffer *b, const char * const *as, unsigned int n) ssize_t (*puts) (buffer *b, const char *s) = buffer_puts; for (unsigned i = 0; i < n; ++i) { - if (as[i] == PUTMSG_SYS) { - puts(b, strerror(e)); + if (as[i] == PUTMSG_FLUSH) { + buffer_flush(b); } else if (as[i] == PUTMSG_ESC || as[i] == PUTMSG_TOGGLE_ESC) { if (as[i] == PUTMSG_ESC) buffer_put(b, "\"", 1); @@ -21,8 +21,6 @@ buffer_putmsg(buffer *b, const char * const *as, unsigned int n) puts = buffer_putescs; else puts = buffer_puts; - } 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]; @@ -33,16 +31,18 @@ buffer_putmsg(buffer *b, const char * const *as, unsigned int n) buf[u64_fmt(buf, u)] = 0; buffer_puts(b, buf_); - } else if ((as[i] == PUTMSG_HEX_NEXT || as[i] == PUTMSG_DUMP_NEXT) && i + 2 < n) { - buffer_puthex(b, as[i + 1], (uintptr_t) as[i + 2], - (as[i] == PUTMSG_DUMP_NEXT) ? HEX_SP | HEX_1BLOCK_SP | HEX_3BLOCK_LF | HEX_LF : 0); - i += 2; + } else if (as[i] == PUTMSG_ERR_NEXT && i + 1 < n) { + puts(b, strerror((uintptr_t) as[++i])); } else if (as[i] == PUTMSG_LEN_NEXT && i + 2 < n) { if (puts == buffer_puts) buffer_put(b, as[i + 1], (uintptr_t) as[i + 2]); else buffer_putesc(b, as[i + 1], (uintptr_t) as[i + 2]); i += 2; + } else if ((as[i] == PUTMSG_HEX_NEXT || as[i] == PUTMSG_DUMP_NEXT) && i + 2 < n) { + buffer_puthex(b, as[i + 1], (uintptr_t) as[i + 2], + (as[i] == PUTMSG_DUMP_NEXT) ? HEX_SP | HEX_1BLOCK_SP | HEX_3BLOCK_LF | HEX_LF : 0); + i += 2; } else if (as[i] && as[i][0]) { puts(b, as[i]); } diff --git a/src/liblimb/include/limb/buffer.h b/src/liblimb/include/limb/buffer.h index 89fddc4..957d0f0 100644 --- a/src/liblimb/include/limb/buffer.h +++ b/src/liblimb/include/limb/buffer.h @@ -8,29 +8,33 @@ #include <limb/hex.h> /* HEX_* */ /* 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_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 */ -#define PUTMSG_LEN_NEXT ((void *) 8) /* next strings are data, dlen */ +#define PUTMSG_FLUSH ((void *) 1) /* flush buffer */ +#define PUTMSG_TOGGLE_ESC ((void *) 2) /* toggle escaping */ +#define PUTMSG_ESC ((void *) 3) /* toggle escaping & add quote */ +#define PUTMSG_UINT_NEXT ((void *) 4) /* next string is an uint */ +#define PUTMSG_INT_NEXT ((void *) 5) /* next string is an int */ +#define PUTMSG_ERR_NEXT ((void *) 6) /* add strerror(next) */ +#define PUTMSG_LEN_NEXT ((void *) 7) /* next strings are data, dlen */ +#define PUTMSG_HEX_NEXT ((void *) 8) /* next strings are data, dlen */ #define PUTMSG_DUMP_NEXT ((void *) 9) /* next strings are data, dlen */ #define PUTMSG_ASUINT(n) ((const char *) (uintptr_t) (n)) #define PUTMSG_ASINT(n) ((const char *) ( intptr_t) (n)) -#define PUTMSG_UINT(n) PUTMSG_UINT_NEXT, PUTMSG_ASUINT(n) -#define PUTMSG_INT(n) PUTMSG_INT_NEXT, PUTMSG_ASINT(n) -#define PUTMSG_HEX(d,l) PUTMSG_HEX_NEXT, d, PUTMSG_ASUINT(l) -#define PUTMSG_LEN(d,l) PUTMSG_LEN_NEXT, d, PUTMSG_ASUINT(l) +#define PUTMSG_UINT(n) PUTMSG_UINT_NEXT, PUTMSG_ASUINT(n) +#define PUTMSG_INT(n) PUTMSG_INT_NEXT, PUTMSG_ASINT(n) +#define PUTMSG_ERR(n) PUTMSG_ERR_NEXT, PUTMSG_ASUINT(n) +#define PUTMSG_LEN(d,l) PUTMSG_LEN_NEXT, d, PUTMSG_ASUINT(l) +#define PUTMSG_HEX(d,l) PUTMSG_HEX_NEXT, d, PUTMSG_ASUINT(l) #define PUTMSG_DUMP(d,l) PUTMSG_DUMP_NEXT, d, PUTMSG_ASUINT(l) #define ESC PUTMSG_ESC -#define HEX(d,l) PUTMSG_HEX(d,l) -#define LEN(d,l) PUTMSG_LEN(d,l) -#define DUMP(d,l) PUTMSG_DUMP(d,l) +#define PMUINT(n) PUTMSG_UINT(n) +#define PMINT(n) PUTMSG_INT(n) +#define PMERR(n) PUTMSG_ERR(n) +#define PMLEN(d,l) PUTMSG_LEN(d,l) +#define PMHEX(d,l) PUTMSG_HEX(d,l) +#define PMDMP(d,l) PUTMSG_DUMP(d,l) /* Writing */ diff --git a/src/liblimb/include/limb/output.h b/src/liblimb/include/limb/output.h index 3154bff..76ba60d 100644 --- a/src/liblimb/include/limb/output.h +++ b/src/liblimb/include/limb/output.h @@ -39,8 +39,8 @@ extern const char *PROG; #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 sys(...) errmsg(OLVL_NORMAL, __VA_ARGS__, ": ", PUTMSG_ERR(errno), "\n", PUTMSG_FLUSH) +#define sysverb(...) errmsg(OLVL_VERBOSE, __VA_ARGS__, ": ", PUTMSG_ERR(errno), "\n", PUTMSG_FLUSH) #define add(...) outmsg(OLVL_NORMAL, __VA_ARGS__) #define adde(...) errmsg(OLVL_NORMAL, __VA_ARGS__) @@ -50,8 +50,8 @@ extern const char *PROG; #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 sysdie(e,...) errmsgdie(e, OLVL_NORMAL, __VA_ARGS__, ": ", PUTMSG_ERR(errno), "\n", PUTMSG_FLUSH) +#define sysverbdie(e,...) errmsgdie(e, OLVL_VERBOSE, __VA_ARGS__, ": ", PUTMSG_ERR(errno), "\n", PUTMSG_FLUSH) #define warn(...) err(PROG, ": warning: ", __VA_ARGS__) #define warnu(...) warn("unable to ", __VA_ARGS__)