From 9539f293d5103f489ced43efc7c3427c0831f74c Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Thu, 11 Aug 2022 20:36:59 -0400 Subject: lib/printbuf: prt_str_indented() This adds a new helper, prt_str_indented(), which handles embedded control characters by calling prt_newline(), prt_tab(), and prt_tab_rjust() as needed. Signed-off-by: Kent Overstreet --- include/linux/printbuf.h | 1 + lib/printbuf.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h index 861c5d75f852..d8a9027b8c4d 100644 --- a/include/linux/printbuf.h +++ b/include/linux/printbuf.h @@ -99,6 +99,7 @@ void printbuf_indent_add(struct printbuf *, unsigned); void printbuf_indent_sub(struct printbuf *, unsigned); void prt_tab(struct printbuf *); void prt_tab_rjust(struct printbuf *); +void prt_str_indented(struct printbuf *, const char *); void prt_human_readable_u64(struct printbuf *, u64); void prt_human_readable_s64(struct printbuf *, s64); void prt_units_u64(struct printbuf *, u64); diff --git a/lib/printbuf.c b/lib/printbuf.c index 047470025748..e4905b758406 100644 --- a/lib/printbuf.c +++ b/lib/printbuf.c @@ -201,6 +201,45 @@ void prt_tab_rjust(struct printbuf *buf) } EXPORT_SYMBOL(prt_tab_rjust); +/** + * prt_str_indented - Print a string, handling embedded control characters + * + * @out: printbuf to output to + * @str: string to print + * + * The following contol characters are handled as so: + * \n: prt_newline + * \t: prt_tab + * \r: prt_tab_rjust + */ +void prt_str_indented(struct printbuf *out, const char *str) +{ + while (1) { + size_t n = strcspn(str, "\n\t\r"); + + prt_bytes(out, str, n); + str += n; + + if (!*str) + break; + + switch (*str) { + case '\n': + prt_newline(out); + break; + case '\t': + prt_tab(out); + break; + case '\r': + prt_tab_rjust(out); + break; + } + + str++; + } +} +EXPORT_SYMBOL(prt_str_indented); + /** * prt_human_readable_u64 - Print out a u64 in human readable units * -- cgit v1.2.3