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/slab.h>
8 #include <linux/string_helpers.h>
12 static inline unsigned printbuf_linelen(struct printbuf *buf)
14 return buf->pos - buf->last_newline;
17 int bch2_printbuf_make_room(struct printbuf *out, unsigned extra)
22 if (!out->heap_allocated)
25 /* Reserved space for terminating nul: */
28 if (out->pos + extra < out->size)
31 new_size = roundup_pow_of_two(out->size + extra);
34 * Note: output buffer must be freeable with kfree(), it's not required
35 * that the user use printbuf_exit().
37 buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWAIT);
40 out->allocation_failure = true;
49 void bch2_prt_vprintf(struct printbuf *out, const char *fmt, va_list args)
57 len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args2);
58 } while (len + 1 >= printbuf_remaining(out) &&
59 !bch2_printbuf_make_room(out, len + 1));
61 len = min_t(size_t, len,
62 printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
66 void bch2_prt_printf(struct printbuf *out, const char *fmt, ...)
73 len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args);
75 } while (len + 1 >= printbuf_remaining(out) &&
76 !bch2_printbuf_make_room(out, len + 1));
78 len = min_t(size_t, len,
79 printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
84 * bch2_printbuf_str() - returns printbuf's buf as a C string, guaranteed to be
86 * @buf: printbuf to terminate
87 * Returns: Printbuf contents, as a nul terminated C string
89 const char *bch2_printbuf_str(const struct printbuf *buf)
92 * If we've written to a printbuf then it's guaranteed to be a null
93 * terminated string - but if we haven't, then we might not have
94 * allocated a buffer at all:
102 * bch2_printbuf_exit() - exit a printbuf, freeing memory it owns and poisoning it
103 * against accidental use.
104 * @buf: printbuf to exit
106 void bch2_printbuf_exit(struct printbuf *buf)
108 if (buf->heap_allocated) {
110 buf->buf = ERR_PTR(-EINTR); /* poison value */
114 void bch2_printbuf_tabstops_reset(struct printbuf *buf)
116 buf->nr_tabstops = 0;
119 void bch2_printbuf_tabstop_pop(struct printbuf *buf)
121 if (buf->nr_tabstops)
126 * bch2_printbuf_tabstop_set() - add a tabstop, n spaces from the previous tabstop
128 * @buf: printbuf to control
129 * @spaces: number of spaces from previous tabpstop
131 * In the future this function may allocate memory if setting more than
132 * PRINTBUF_INLINE_TABSTOPS or setting tabstops more than 255 spaces from start
135 int bch2_printbuf_tabstop_push(struct printbuf *buf, unsigned spaces)
137 unsigned prev_tabstop = buf->nr_tabstops
138 ? buf->_tabstops[buf->nr_tabstops - 1]
141 if (WARN_ON(buf->nr_tabstops >= ARRAY_SIZE(buf->_tabstops)))
144 buf->_tabstops[buf->nr_tabstops++] = prev_tabstop + spaces;
145 buf->has_indent_or_tabstops = true;
150 * bch2_printbuf_indent_add() - add to the current indent level
152 * @buf: printbuf to control
153 * @spaces: number of spaces to add to the current indent level
155 * Subsequent lines, and the current line if the output position is at the start
156 * of the current line, will be indented by @spaces more spaces.
158 void bch2_printbuf_indent_add(struct printbuf *buf, unsigned spaces)
160 if (WARN_ON_ONCE(buf->indent + spaces < buf->indent))
163 buf->indent += spaces;
164 prt_chars(buf, ' ', spaces);
166 buf->has_indent_or_tabstops = true;
170 * bch2_printbuf_indent_sub() - subtract from the current indent level
172 * @buf: printbuf to control
173 * @spaces: number of spaces to subtract from the current indent level
175 * Subsequent lines, and the current line if the output position is at the start
176 * of the current line, will be indented by @spaces less spaces.
178 void bch2_printbuf_indent_sub(struct printbuf *buf, unsigned spaces)
180 if (WARN_ON_ONCE(spaces > buf->indent))
181 spaces = buf->indent;
183 if (buf->last_newline + buf->indent == buf->pos) {
185 printbuf_nul_terminate(buf);
187 buf->indent -= spaces;
189 if (!buf->indent && !buf->nr_tabstops)
190 buf->has_indent_or_tabstops = false;
193 void bch2_prt_newline(struct printbuf *buf)
197 bch2_printbuf_make_room(buf, 1 + buf->indent);
199 __prt_char(buf, '\n');
201 buf->last_newline = buf->pos;
203 for (i = 0; i < buf->indent; i++)
204 __prt_char(buf, ' ');
206 printbuf_nul_terminate(buf);
208 buf->last_field = buf->pos;
209 buf->cur_tabstop = 0;
213 * Returns spaces from start of line, if set, or 0 if unset:
215 static inline unsigned cur_tabstop(struct printbuf *buf)
217 return buf->cur_tabstop < buf->nr_tabstops
218 ? buf->_tabstops[buf->cur_tabstop]
222 static void __prt_tab(struct printbuf *out)
224 int spaces = max_t(int, 0, cur_tabstop(out) - printbuf_linelen(out));
226 prt_chars(out, ' ', spaces);
228 out->last_field = out->pos;
233 * bch2_prt_tab() - Advance printbuf to the next tabstop
234 * @out: printbuf to control
236 * Advance output to the next tabstop by printing spaces.
238 void bch2_prt_tab(struct printbuf *out)
240 if (WARN_ON(!cur_tabstop(out)))
246 static void __prt_tab_rjust(struct printbuf *buf)
248 unsigned move = buf->pos - buf->last_field;
249 int pad = (int) cur_tabstop(buf) - (int) printbuf_linelen(buf);
252 bch2_printbuf_make_room(buf, pad);
254 if (buf->last_field + pad < buf->size)
255 memmove(buf->buf + buf->last_field + pad,
256 buf->buf + buf->last_field,
257 min(move, buf->size - 1 - buf->last_field - pad));
259 if (buf->last_field < buf->size)
260 memset(buf->buf + buf->last_field, ' ',
261 min((unsigned) pad, buf->size - buf->last_field));
264 printbuf_nul_terminate(buf);
267 buf->last_field = buf->pos;
272 * bch2_prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
275 * @buf: printbuf to control
277 * Advance output to the next tabstop by inserting spaces immediately after the
278 * previous tabstop, right justifying previously outputted text.
280 void bch2_prt_tab_rjust(struct printbuf *buf)
282 if (WARN_ON(!cur_tabstop(buf)))
285 __prt_tab_rjust(buf);
289 * bch2_prt_bytes_indented() - Print an array of chars, handling embedded control characters
291 * @out: output printbuf
292 * @str: string to print
293 * @count: number of bytes to print
295 * The following contol characters are handled as so:
296 * \n: prt_newline newline that obeys current indent level
297 * \t: prt_tab advance to next tabstop
298 * \r: prt_tab_rjust advance to next tabstop, with right justification
300 void bch2_prt_bytes_indented(struct printbuf *out, const char *str, unsigned count)
302 const char *unprinted_start = str;
303 const char *end = str + count;
305 if (!out->has_indent_or_tabstops || out->suppress_indent_tabstop_handling) {
306 prt_bytes(out, str, count);
313 prt_bytes(out, unprinted_start, str - unprinted_start);
314 unprinted_start = str + 1;
315 bch2_prt_newline(out);
318 if (likely(cur_tabstop(out))) {
319 prt_bytes(out, unprinted_start, str - unprinted_start);
320 unprinted_start = str + 1;
325 if (likely(cur_tabstop(out))) {
326 prt_bytes(out, unprinted_start, str - unprinted_start);
327 unprinted_start = str + 1;
328 __prt_tab_rjust(out);
336 prt_bytes(out, unprinted_start, str - unprinted_start);
340 * bch2_prt_human_readable_u64() - Print out a u64 in human readable units
341 * @out: output printbuf
342 * @v: integer to print
344 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
346 void bch2_prt_human_readable_u64(struct printbuf *out, u64 v)
348 bch2_printbuf_make_room(out, 10);
349 out->pos += string_get_size(v, 1, !out->si_units,
351 printbuf_remaining_size(out));
355 * bch2_prt_human_readable_s64() - Print out a s64 in human readable units
356 * @out: output printbuf
357 * @v: integer to print
359 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
361 void bch2_prt_human_readable_s64(struct printbuf *out, s64 v)
365 bch2_prt_human_readable_u64(out, abs(v));
369 * bch2_prt_units_u64() - Print out a u64 according to printbuf unit options
370 * @out: output printbuf
371 * @v: integer to print
373 * Units are either raw (default), or human reabable units (controlled via
374 * @buf->human_readable_units)
376 void bch2_prt_units_u64(struct printbuf *out, u64 v)
378 if (out->human_readable_units)
379 bch2_prt_human_readable_u64(out, v);
381 bch2_prt_printf(out, "%llu", v);
385 * bch2_prt_units_s64() - Print out a s64 according to printbuf unit options
386 * @out: output printbuf
387 * @v: integer to print
389 * Units are either raw (default), or human reabable units (controlled via
390 * @buf->human_readable_units)
392 void bch2_prt_units_s64(struct printbuf *out, s64 v)
396 bch2_prt_units_u64(out, abs(v));
399 void bch2_prt_string_option(struct printbuf *out,
400 const char * const list[],
405 for (i = 0; list[i]; i++)
406 bch2_prt_printf(out, i == selected ? "[%s] " : "%s ", list[i]);
409 void bch2_prt_bitflags(struct printbuf *out,
410 const char * const list[], u64 flags)
412 unsigned bit, nr = 0;
418 while (flags && (bit = __ffs64(flags)) < nr) {
420 bch2_prt_printf(out, ",");
422 bch2_prt_printf(out, "%s", list[bit]);
423 flags ^= BIT_ULL(bit);