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);
60 } while (len + 1 >= printbuf_remaining(out) &&
61 !bch2_printbuf_make_room(out, len + 1));
63 len = min_t(size_t, len,
64 printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
68 void bch2_prt_printf(struct printbuf *out, const char *fmt, ...)
75 len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args);
77 } while (len + 1 >= printbuf_remaining(out) &&
78 !bch2_printbuf_make_room(out, len + 1));
80 len = min_t(size_t, len,
81 printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
86 * bch2_printbuf_str() - returns printbuf's buf as a C string, guaranteed to be
88 * @buf: printbuf to terminate
89 * Returns: Printbuf contents, as a nul terminated C string
91 const char *bch2_printbuf_str(const struct printbuf *buf)
94 * If we've written to a printbuf then it's guaranteed to be a null
95 * terminated string - but if we haven't, then we might not have
96 * allocated a buffer at all:
104 * bch2_printbuf_exit() - exit a printbuf, freeing memory it owns and poisoning it
105 * against accidental use.
106 * @buf: printbuf to exit
108 void bch2_printbuf_exit(struct printbuf *buf)
110 if (buf->heap_allocated) {
112 buf->buf = ERR_PTR(-EINTR); /* poison value */
116 void bch2_printbuf_tabstops_reset(struct printbuf *buf)
118 buf->nr_tabstops = 0;
121 void bch2_printbuf_tabstop_pop(struct printbuf *buf)
123 if (buf->nr_tabstops)
128 * bch2_printbuf_tabstop_set() - add a tabstop, n spaces from the previous tabstop
130 * @buf: printbuf to control
131 * @spaces: number of spaces from previous tabpstop
133 * In the future this function may allocate memory if setting more than
134 * PRINTBUF_INLINE_TABSTOPS or setting tabstops more than 255 spaces from start
137 int bch2_printbuf_tabstop_push(struct printbuf *buf, unsigned spaces)
139 unsigned prev_tabstop = buf->nr_tabstops
140 ? buf->_tabstops[buf->nr_tabstops - 1]
143 if (WARN_ON(buf->nr_tabstops >= ARRAY_SIZE(buf->_tabstops)))
146 buf->_tabstops[buf->nr_tabstops++] = prev_tabstop + spaces;
147 buf->has_indent_or_tabstops = true;
152 * bch2_printbuf_indent_add() - add to the current indent level
154 * @buf: printbuf to control
155 * @spaces: number of spaces to add to the current indent level
157 * Subsequent lines, and the current line if the output position is at the start
158 * of the current line, will be indented by @spaces more spaces.
160 void bch2_printbuf_indent_add(struct printbuf *buf, unsigned spaces)
162 if (WARN_ON_ONCE(buf->indent + spaces < buf->indent))
165 buf->indent += spaces;
166 prt_chars(buf, ' ', spaces);
168 buf->has_indent_or_tabstops = true;
172 * bch2_printbuf_indent_sub() - subtract from the current indent level
174 * @buf: printbuf to control
175 * @spaces: number of spaces to subtract from the current indent level
177 * Subsequent lines, and the current line if the output position is at the start
178 * of the current line, will be indented by @spaces less spaces.
180 void bch2_printbuf_indent_sub(struct printbuf *buf, unsigned spaces)
182 if (WARN_ON_ONCE(spaces > buf->indent))
183 spaces = buf->indent;
185 if (buf->last_newline + buf->indent == buf->pos) {
187 printbuf_nul_terminate(buf);
189 buf->indent -= spaces;
191 if (!buf->indent && !buf->nr_tabstops)
192 buf->has_indent_or_tabstops = false;
195 void bch2_prt_newline(struct printbuf *buf)
199 bch2_printbuf_make_room(buf, 1 + buf->indent);
201 __prt_char(buf, '\n');
203 buf->last_newline = buf->pos;
205 for (i = 0; i < buf->indent; i++)
206 __prt_char(buf, ' ');
208 printbuf_nul_terminate(buf);
210 buf->last_field = buf->pos;
211 buf->cur_tabstop = 0;
215 * Returns spaces from start of line, if set, or 0 if unset:
217 static inline unsigned cur_tabstop(struct printbuf *buf)
219 return buf->cur_tabstop < buf->nr_tabstops
220 ? buf->_tabstops[buf->cur_tabstop]
224 static void __prt_tab(struct printbuf *out)
226 int spaces = max_t(int, 0, cur_tabstop(out) - printbuf_linelen(out));
228 prt_chars(out, ' ', spaces);
230 out->last_field = out->pos;
235 * bch2_prt_tab() - Advance printbuf to the next tabstop
236 * @out: printbuf to control
238 * Advance output to the next tabstop by printing spaces.
240 void bch2_prt_tab(struct printbuf *out)
242 if (WARN_ON(!cur_tabstop(out)))
248 static void __prt_tab_rjust(struct printbuf *buf)
250 unsigned move = buf->pos - buf->last_field;
251 int pad = (int) cur_tabstop(buf) - (int) printbuf_linelen(buf);
254 bch2_printbuf_make_room(buf, pad);
256 if (buf->last_field + pad < buf->size)
257 memmove(buf->buf + buf->last_field + pad,
258 buf->buf + buf->last_field,
259 min(move, buf->size - 1 - buf->last_field - pad));
261 if (buf->last_field < buf->size)
262 memset(buf->buf + buf->last_field, ' ',
263 min((unsigned) pad, buf->size - buf->last_field));
266 printbuf_nul_terminate(buf);
269 buf->last_field = buf->pos;
274 * bch2_prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
277 * @buf: printbuf to control
279 * Advance output to the next tabstop by inserting spaces immediately after the
280 * previous tabstop, right justifying previously outputted text.
282 void bch2_prt_tab_rjust(struct printbuf *buf)
284 if (WARN_ON(!cur_tabstop(buf)))
287 __prt_tab_rjust(buf);
291 * bch2_prt_bytes_indented() - Print an array of chars, handling embedded control characters
293 * @out: output printbuf
294 * @str: string to print
295 * @count: number of bytes to print
297 * The following contol characters are handled as so:
298 * \n: prt_newline newline that obeys current indent level
299 * \t: prt_tab advance to next tabstop
300 * \r: prt_tab_rjust advance to next tabstop, with right justification
302 void bch2_prt_bytes_indented(struct printbuf *out, const char *str, unsigned count)
304 const char *unprinted_start = str;
305 const char *end = str + count;
307 if (!out->has_indent_or_tabstops || out->suppress_indent_tabstop_handling) {
308 prt_bytes(out, str, count);
315 prt_bytes(out, unprinted_start, str - unprinted_start);
316 unprinted_start = str + 1;
317 bch2_prt_newline(out);
320 if (likely(cur_tabstop(out))) {
321 prt_bytes(out, unprinted_start, str - unprinted_start);
322 unprinted_start = str + 1;
327 if (likely(cur_tabstop(out))) {
328 prt_bytes(out, unprinted_start, str - unprinted_start);
329 unprinted_start = str + 1;
330 __prt_tab_rjust(out);
338 prt_bytes(out, unprinted_start, str - unprinted_start);
342 * bch2_prt_human_readable_u64() - Print out a u64 in human readable units
343 * @out: output printbuf
344 * @v: integer to print
346 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
348 void bch2_prt_human_readable_u64(struct printbuf *out, u64 v)
350 bch2_printbuf_make_room(out, 10);
351 out->pos += string_get_size(v, 1, !out->si_units,
353 printbuf_remaining_size(out));
357 * bch2_prt_human_readable_s64() - Print out a s64 in human readable units
358 * @out: output printbuf
359 * @v: integer to print
361 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
363 void bch2_prt_human_readable_s64(struct printbuf *out, s64 v)
367 bch2_prt_human_readable_u64(out, abs(v));
371 * bch2_prt_units_u64() - Print out a u64 according to printbuf unit options
372 * @out: output printbuf
373 * @v: integer to print
375 * Units are either raw (default), or human reabable units (controlled via
376 * @buf->human_readable_units)
378 void bch2_prt_units_u64(struct printbuf *out, u64 v)
380 if (out->human_readable_units)
381 bch2_prt_human_readable_u64(out, v);
383 bch2_prt_printf(out, "%llu", v);
387 * bch2_prt_units_s64() - Print out a s64 according to printbuf unit options
388 * @out: output printbuf
389 * @v: integer to print
391 * Units are either raw (default), or human reabable units (controlled via
392 * @buf->human_readable_units)
394 void bch2_prt_units_s64(struct printbuf *out, s64 v)
398 bch2_prt_units_u64(out, abs(v));
401 void bch2_prt_string_option(struct printbuf *out,
402 const char * const list[],
407 for (i = 0; list[i]; i++)
408 bch2_prt_printf(out, i == selected ? "[%s] " : "%s ", list[i]);
411 void bch2_prt_bitflags(struct printbuf *out,
412 const char * const list[], u64 flags)
414 unsigned bit, nr = 0;
420 while (flags && (bit = __ffs64(flags)) < nr) {
422 bch2_prt_printf(out, ",");
424 bch2_prt_printf(out, "%s", list[bit]);
425 flags ^= BIT_ULL(bit);
429 void bch2_prt_bitflags_vector(struct printbuf *out,
430 const char * const list[],
431 unsigned long *v, unsigned nr)
436 for (i = 0; i < nr; i++)
442 for_each_set_bit(i, v, nr) {
444 bch2_prt_printf(out, ",");
446 bch2_prt_printf(out, "%s", list[i]);