1 // SPDX-License-Identifier: LGPL-2.1+
2 /* Copyright (C) 2022 Kent Overstreet */
5 #include <linux/export.h>
6 #include <linux/kernel.h>
7 #include <linux/printbuf.h>
8 #include <linux/slab.h>
9 #include <linux/string_helpers.h>
11 static inline unsigned printbuf_linelen(struct printbuf *buf)
13 return buf->pos - buf->last_newline;
16 int printbuf_make_room(struct printbuf *out, unsigned extra)
21 if (!out->heap_allocated)
24 /* Reserved space for terminating nul: */
27 if (out->pos + extra < out->size)
30 new_size = roundup_pow_of_two(out->size + extra);
33 * Note: output buffer must be freeable with kfree(), it's not required
34 * that the user use printbuf_exit().
36 buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWAIT);
39 out->allocation_failure = true;
47 EXPORT_SYMBOL(printbuf_make_room);
50 * printbuf_str - returns printbuf's buf as a C string, guaranteed to be null
53 const char *printbuf_str(const struct printbuf *buf)
56 * If we've written to a printbuf then it's guaranteed to be a null
57 * terminated string - but if we haven't, then we might not have
58 * allocated a buffer at all:
64 EXPORT_SYMBOL(printbuf_str);
67 * printbuf_exit - exit a printbuf, freeing memory it owns and poisoning it
68 * against accidental use.
70 void printbuf_exit(struct printbuf *buf)
72 if (buf->heap_allocated) {
74 buf->buf = ERR_PTR(-EINTR); /* poison value */
77 EXPORT_SYMBOL(printbuf_exit);
79 void printbuf_tabstops_reset(struct printbuf *buf)
83 EXPORT_SYMBOL(printbuf_tabstops_reset);
85 void printbuf_tabstop_pop(struct printbuf *buf)
90 EXPORT_SYMBOL(printbuf_tabstop_pop);
93 * printbuf_tabstop_set - add a tabstop, n spaces from the previous tabstop
95 * @buf: printbuf to control
96 * @spaces: number of spaces from previous tabpstop
98 * In the future this function may allocate memory if setting more than
99 * PRINTBUF_INLINE_TABSTOPS or setting tabstops more than 255 spaces from start
102 int printbuf_tabstop_push(struct printbuf *buf, unsigned spaces)
104 unsigned prev_tabstop = buf->nr_tabstops
105 ? buf->_tabstops[buf->nr_tabstops - 1]
108 if (WARN_ON(buf->nr_tabstops >= ARRAY_SIZE(buf->_tabstops)))
111 buf->_tabstops[buf->nr_tabstops++] = prev_tabstop + spaces;
112 buf->has_indent_or_tabstops = true;
115 EXPORT_SYMBOL(printbuf_tabstop_push);
118 * printbuf_indent_add - add to the current indent level
120 * @buf: printbuf to control
121 * @spaces: number of spaces to add to the current indent level
123 * Subsequent lines, and the current line if the output position is at the start
124 * of the current line, will be indented by @spaces more spaces.
126 void printbuf_indent_add(struct printbuf *buf, unsigned spaces)
128 if (WARN_ON_ONCE(buf->indent + spaces < buf->indent))
131 buf->indent += spaces;
132 prt_chars(buf, ' ', spaces);
134 buf->has_indent_or_tabstops = true;
136 EXPORT_SYMBOL(printbuf_indent_add);
139 * printbuf_indent_sub - subtract from the current indent level
141 * @buf: printbuf to control
142 * @spaces: number of spaces to subtract from the current indent level
144 * Subsequent lines, and the current line if the output position is at the start
145 * of the current line, will be indented by @spaces less spaces.
147 void printbuf_indent_sub(struct printbuf *buf, unsigned spaces)
149 if (WARN_ON_ONCE(spaces > buf->indent))
150 spaces = buf->indent;
152 if (buf->last_newline + buf->indent == buf->pos) {
154 printbuf_nul_terminate(buf);
156 buf->indent -= spaces;
158 if (!buf->indent && !buf->nr_tabstops)
159 buf->has_indent_or_tabstops = false;
161 EXPORT_SYMBOL(printbuf_indent_sub);
163 void prt_newline(struct printbuf *buf)
167 printbuf_make_room(buf, 1 + buf->indent);
169 __prt_char(buf, '\n');
171 buf->last_newline = buf->pos;
173 for (i = 0; i < buf->indent; i++)
174 __prt_char(buf, ' ');
176 printbuf_nul_terminate(buf);
178 buf->last_field = buf->pos;
179 buf->cur_tabstop = 0;
181 EXPORT_SYMBOL(prt_newline);
184 * Returns spaces from start of line, if set, or 0 if unset:
186 static inline unsigned cur_tabstop(struct printbuf *buf)
188 return buf->cur_tabstop < buf->nr_tabstops
189 ? buf->_tabstops[buf->cur_tabstop]
193 static void __prt_tab(struct printbuf *out)
195 int spaces = max_t(int, 0, cur_tabstop(out) - printbuf_linelen(out));
197 prt_chars(out, ' ', spaces);
199 out->last_field = out->pos;
204 * prt_tab - Advance printbuf to the next tabstop
206 * @buf: printbuf to control
208 * Advance output to the next tabstop by printing spaces.
210 void prt_tab(struct printbuf *out)
212 if (WARN_ON(!cur_tabstop(out)))
217 EXPORT_SYMBOL(prt_tab);
219 static void __prt_tab_rjust(struct printbuf *buf)
221 unsigned move = buf->pos - buf->last_field;
222 int pad = (int) cur_tabstop(buf) - (int) printbuf_linelen(buf);
225 printbuf_make_room(buf, pad);
227 if (buf->last_field + pad < buf->size)
228 memmove(buf->buf + buf->last_field + pad,
229 buf->buf + buf->last_field,
230 min(move, buf->size - 1 - buf->last_field - pad));
232 if (buf->last_field < buf->size)
233 memset(buf->buf + buf->last_field, ' ',
234 min((unsigned) pad, buf->size - buf->last_field));
237 printbuf_nul_terminate(buf);
240 buf->last_field = buf->pos;
245 * prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
248 * @buf: printbuf to control
250 * Advance output to the next tabstop by inserting spaces immediately after the
251 * previous tabstop, right justifying previously outputted text.
253 void prt_tab_rjust(struct printbuf *buf)
255 if (WARN_ON(!cur_tabstop(buf)))
258 __prt_tab_rjust(buf);
260 EXPORT_SYMBOL(prt_tab_rjust);
263 * prt_bytes_indented - Print an array of chars, handling embedded control characters
265 * @out: printbuf to output to
266 * @str: string to print
267 * @count: number of bytes to print
269 * The following contol characters are handled as so:
270 * \n: prt_newline newline that obeys current indent level
271 * \t: prt_tab advance to next tabstop
272 * \r: prt_tab_rjust advance to next tabstop, with right justification
274 void prt_bytes_indented(struct printbuf *out, const char *str, unsigned count)
276 const char *unprinted_start = str;
277 const char *end = str + count;
279 if (!out->has_indent_or_tabstops || out->suppress_indent_tabstop_handling) {
280 prt_bytes(out, str, count);
287 prt_bytes(out, unprinted_start, str - unprinted_start);
288 unprinted_start = str + 1;
292 if (likely(cur_tabstop(out))) {
293 prt_bytes(out, unprinted_start, str - unprinted_start);
294 unprinted_start = str + 1;
299 if (likely(cur_tabstop(out))) {
300 prt_bytes(out, unprinted_start, str - unprinted_start);
301 unprinted_start = str + 1;
302 __prt_tab_rjust(out);
310 prt_bytes(out, unprinted_start, str - unprinted_start);
312 EXPORT_SYMBOL(prt_bytes_indented);
315 * prt_human_readable_u64 - Print out a u64 in human readable units
317 * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units
319 void prt_human_readable_u64(struct printbuf *buf, u64 v)
321 printbuf_make_room(buf, 10);
322 buf->pos += string_get_size(v, 1, !buf->si_units,
324 printbuf_remaining_size(buf));
326 EXPORT_SYMBOL(prt_human_readable_u64);
329 * prt_human_readable_s64 - Print out a s64 in human readable units
331 * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units
333 void prt_human_readable_s64(struct printbuf *buf, s64 v)
337 prt_human_readable_u64(buf, abs(v));
339 EXPORT_SYMBOL(prt_human_readable_s64);
342 * prt_units_u64 - Print out a u64 according to printbuf unit options
344 * Units are either raw (default), or human reabable units (controlled via
345 * @buf->human_readable_units)
347 void prt_units_u64(struct printbuf *out, u64 v)
349 if (out->human_readable_units)
350 prt_human_readable_u64(out, v);
352 prt_printf(out, "%llu", v);
354 EXPORT_SYMBOL(prt_units_u64);
357 * prt_units_s64 - Print out a s64 according to printbuf unit options
359 * Units are either raw (default), or human reabable units (controlled via
360 * @buf->human_readable_units)
362 void prt_units_s64(struct printbuf *out, s64 v)
366 prt_units_u64(out, abs(v));
368 EXPORT_SYMBOL(prt_units_s64);