diff options
-rw-r--r-- | include/linux/printbuf.h | 1 | ||||
-rw-r--r-- | lib/printbuf.c | 39 |
2 files changed, 40 insertions, 0 deletions
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 @@ -202,6 +202,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 * * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units |