summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2022-08-11 20:36:59 -0400
committerKent Overstreet <kent.overstreet@gmail.com>2022-08-11 20:39:50 -0400
commit9539f293d5103f489ced43efc7c3427c0831f74c (patch)
tree484705fc0b95077d1ecff6ca123490400928e267
parentee785ee9c0ce7250030f0d7b22fd4e6e621596ab (diff)
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 <kent.overstreet@gmail.com>
-rw-r--r--include/linux/printbuf.h1
-rw-r--r--lib/printbuf.c39
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