1 // SPDX-License-Identifier: LGPL-2.1+
2 /* Copyright (C) 2022 Kent Overstreet */
4 #include <linux/bitmap.h>
6 #include <linux/export.h>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/string_helpers.h>
13 static inline unsigned printbuf_linelen(struct printbuf *buf)
15 return buf->pos - buf->last_newline;
18 int bch2_printbuf_make_room(struct printbuf *out, unsigned extra)
23 if (!out->heap_allocated)
26 /* Reserved space for terminating nul: */
29 if (out->pos + extra < out->size)
32 new_size = roundup_pow_of_two(out->size + extra);
35 * Note: output buffer must be freeable with kfree(), it's not required
36 * that the user use printbuf_exit().
38 buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWAIT);
41 out->allocation_failure = true;
50 void bch2_prt_vprintf(struct printbuf *out, const char *fmt, va_list args)
58 len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args2);
59 } while (len + 1 >= printbuf_remaining(out) &&
60 !bch2_printbuf_make_room(out, len + 1));
62 len = min_t(size_t, len,
63 printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
67 void bch2_prt_printf(struct printbuf *out, const char *fmt, ...)
74 len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args);
76 } while (len + 1 >= printbuf_remaining(out) &&
77 !bch2_printbuf_make_room(out, len + 1));
79 len = min_t(size_t, len,
80 printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
85 * bch2_printbuf_str() - returns printbuf's buf as a C string, guaranteed to be
87 * @buf: printbuf to terminate
88 * Returns: Printbuf contents, as a nul terminated C string
90 const char *bch2_printbuf_str(const struct printbuf *buf)
93 * If we've written to a printbuf then it's guaranteed to be a null
94 * terminated string - but if we haven't, then we might not have
95 * allocated a buffer at all:
103 * bch2_printbuf_exit() - exit a printbuf, freeing memory it owns and poisoning it
104 * against accidental use.
105 * @buf: printbuf to exit
107 void bch2_printbuf_exit(struct printbuf *buf)
109 if (buf->heap_allocated) {
111 buf->buf = ERR_PTR(-EINTR); /* poison value */
115 void bch2_printbuf_tabstops_reset(struct printbuf *buf)
117 buf->nr_tabstops = 0;
120 void bch2_printbuf_tabstop_pop(struct printbuf *buf)
122 if (buf->nr_tabstops)
127 * bch2_printbuf_tabstop_set() - add a tabstop, n spaces from the previous tabstop
129 * @buf: printbuf to control
130 * @spaces: number of spaces from previous tabpstop
132 * In the future this function may allocate memory if setting more than
133 * PRINTBUF_INLINE_TABSTOPS or setting tabstops more than 255 spaces from start
136 int bch2_printbuf_tabstop_push(struct printbuf *buf, unsigned spaces)
138 unsigned prev_tabstop = buf->nr_tabstops
139 ? buf->_tabstops[buf->nr_tabstops - 1]
142 if (WARN_ON(buf->nr_tabstops >= ARRAY_SIZE(buf->_tabstops)))
145 buf->_tabstops[buf->nr_tabstops++] = prev_tabstop + spaces;
146 buf->has_indent_or_tabstops = true;
151 * bch2_printbuf_indent_add() - add to the current indent level
153 * @buf: printbuf to control
154 * @spaces: number of spaces to add to the current indent level
156 * Subsequent lines, and the current line if the output position is at the start
157 * of the current line, will be indented by @spaces more spaces.
159 void bch2_printbuf_indent_add(struct printbuf *buf, unsigned spaces)
161 if (WARN_ON_ONCE(buf->indent + spaces < buf->indent))
164 buf->indent += spaces;
165 prt_chars(buf, ' ', spaces);
167 buf->has_indent_or_tabstops = true;
171 * bch2_printbuf_indent_sub() - subtract from the current indent level
173 * @buf: printbuf to control
174 * @spaces: number of spaces to subtract from the current indent level
176 * Subsequent lines, and the current line if the output position is at the start
177 * of the current line, will be indented by @spaces less spaces.
179 void bch2_printbuf_indent_sub(struct printbuf *buf, unsigned spaces)
181 if (WARN_ON_ONCE(spaces > buf->indent))
182 spaces = buf->indent;
184 if (buf->last_newline + buf->indent == buf->pos) {
186 printbuf_nul_terminate(buf);
188 buf->indent -= spaces;
190 if (!buf->indent && !buf->nr_tabstops)
191 buf->has_indent_or_tabstops = false;
194 void bch2_prt_newline(struct printbuf *buf)
198 bch2_printbuf_make_room(buf, 1 + buf->indent);
200 __prt_char(buf, '\n');
202 buf->last_newline = buf->pos;
204 for (i = 0; i < buf->indent; i++)
205 __prt_char(buf, ' ');
207 printbuf_nul_terminate(buf);
209 buf->last_field = buf->pos;
210 buf->cur_tabstop = 0;
214 * Returns spaces from start of line, if set, or 0 if unset:
216 static inline unsigned cur_tabstop(struct printbuf *buf)
218 return buf->cur_tabstop < buf->nr_tabstops
219 ? buf->_tabstops[buf->cur_tabstop]
223 static void __prt_tab(struct printbuf *out)
225 int spaces = max_t(int, 0, cur_tabstop(out) - printbuf_linelen(out));
227 prt_chars(out, ' ', spaces);
229 out->last_field = out->pos;
234 * bch2_prt_tab() - Advance printbuf to the next tabstop
235 * @out: printbuf to control
237 * Advance output to the next tabstop by printing spaces.
239 void bch2_prt_tab(struct printbuf *out)
241 if (WARN_ON(!cur_tabstop(out)))
247 static void __prt_tab_rjust(struct printbuf *buf)
249 unsigned move = buf->pos - buf->last_field;
250 int pad = (int) cur_tabstop(buf) - (int) printbuf_linelen(buf);
253 bch2_printbuf_make_room(buf, pad);
255 if (buf->last_field + pad < buf->size)
256 memmove(buf->buf + buf->last_field + pad,
257 buf->buf + buf->last_field,
258 min(move, buf->size - 1 - buf->last_field - pad));
260 if (buf->last_field < buf->size)
261 memset(buf->buf + buf->last_field, ' ',
262 min((unsigned) pad, buf->size - buf->last_field));
265 printbuf_nul_terminate(buf);
268 buf->last_field = buf->pos;
273 * bch2_prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
276 * @buf: printbuf to control
278 * Advance output to the next tabstop by inserting spaces immediately after the
279 * previous tabstop, right justifying previously outputted text.
281 void bch2_prt_tab_rjust(struct printbuf *buf)
283 if (WARN_ON(!cur_tabstop(buf)))
286 __prt_tab_rjust(buf);
290 * bch2_prt_bytes_indented() - Print an array of chars, handling embedded control characters
292 * @out: output printbuf
293 * @str: string to print
294 * @count: number of bytes to print
296 * The following contol characters are handled as so:
297 * \n: prt_newline newline that obeys current indent level
298 * \t: prt_tab advance to next tabstop
299 * \r: prt_tab_rjust advance to next tabstop, with right justification
301 void bch2_prt_bytes_indented(struct printbuf *out, const char *str, unsigned count)
303 const char *unprinted_start = str;
304 const char *end = str + count;
306 if (!out->has_indent_or_tabstops || out->suppress_indent_tabstop_handling) {
307 prt_bytes(out, str, count);
314 prt_bytes(out, unprinted_start, str - unprinted_start);
315 unprinted_start = str + 1;
316 bch2_prt_newline(out);
319 if (likely(cur_tabstop(out))) {
320 prt_bytes(out, unprinted_start, str - unprinted_start);
321 unprinted_start = str + 1;
326 if (likely(cur_tabstop(out))) {
327 prt_bytes(out, unprinted_start, str - unprinted_start);
328 unprinted_start = str + 1;
329 __prt_tab_rjust(out);
337 prt_bytes(out, unprinted_start, str - unprinted_start);
341 * bch2_prt_human_readable_u64() - Print out a u64 in human readable units
342 * @out: output printbuf
343 * @v: integer to print
345 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
347 void bch2_prt_human_readable_u64(struct printbuf *out, u64 v)
349 bch2_printbuf_make_room(out, 10);
350 out->pos += string_get_size(v, 1, !out->si_units,
352 printbuf_remaining_size(out));
356 * bch2_prt_human_readable_s64() - Print out a s64 in human readable units
357 * @out: output printbuf
358 * @v: integer to print
360 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
362 void bch2_prt_human_readable_s64(struct printbuf *out, s64 v)
366 bch2_prt_human_readable_u64(out, abs(v));
370 * bch2_prt_units_u64() - Print out a u64 according to printbuf unit options
371 * @out: output printbuf
372 * @v: integer to print
374 * Units are either raw (default), or human reabable units (controlled via
375 * @buf->human_readable_units)
377 void bch2_prt_units_u64(struct printbuf *out, u64 v)
379 if (out->human_readable_units)
380 bch2_prt_human_readable_u64(out, v);
382 bch2_prt_printf(out, "%llu", v);
386 * bch2_prt_units_s64() - Print out a s64 according to printbuf unit options
387 * @out: output printbuf
388 * @v: integer to print
390 * Units are either raw (default), or human reabable units (controlled via
391 * @buf->human_readable_units)
393 void bch2_prt_units_s64(struct printbuf *out, s64 v)
397 bch2_prt_units_u64(out, abs(v));
400 void bch2_prt_string_option(struct printbuf *out,
401 const char * const list[],
406 for (i = 0; list[i]; i++)
407 bch2_prt_printf(out, i == selected ? "[%s] " : "%s ", list[i]);
410 void bch2_prt_bitflags(struct printbuf *out,
411 const char * const list[], u64 flags)
413 unsigned bit, nr = 0;
419 while (flags && (bit = __ffs64(flags)) < nr) {
421 bch2_prt_printf(out, ",");
423 bch2_prt_printf(out, "%s", list[bit]);
424 flags ^= BIT_ULL(bit);
428 void bch2_prt_bitflags_vector(struct printbuf *out,
429 const char * const list[],
430 unsigned long *v, unsigned nr)
435 for (i = 0; i < nr; i++)
441 for_each_set_bit(i, v, nr) {
443 bch2_prt_printf(out, ",");
445 bch2_prt_printf(out, "%s", list[i]);