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, unsigned pos)
15 return pos - buf->last_newline;
18 static inline unsigned printbuf_linelen(struct printbuf *buf)
20 return __printbuf_linelen(buf, buf->pos);
24 * Returns spaces from start of line, if set, or 0 if unset:
26 static inline unsigned cur_tabstop(struct printbuf *buf)
28 return buf->cur_tabstop < buf->nr_tabstops
29 ? buf->_tabstops[buf->cur_tabstop]
33 int bch2_printbuf_make_room(struct printbuf *out, unsigned extra)
35 /* Reserved space for terminating nul: */
38 if (out->pos + extra <= out->size)
41 if (!out->heap_allocated) {
46 unsigned new_size = roundup_pow_of_two(out->size + extra);
49 * Note: output buffer must be freeable with kfree(), it's not required
50 * that the user use printbuf_exit().
52 char *buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWAIT);
55 out->allocation_failure = true;
65 static void printbuf_advance_pos(struct printbuf *out, unsigned len)
67 out->pos += min(len, printbuf_remaining(out));
70 static void printbuf_insert_spaces(struct printbuf *out, unsigned pos, unsigned nr)
72 unsigned move = out->pos - pos;
74 bch2_printbuf_make_room(out, nr);
76 if (pos + nr < out->size)
77 memmove(out->buf + pos + nr,
79 min(move, out->size - 1 - pos - nr));
82 memset(out->buf + pos, ' ', min(nr, out->size - pos));
84 printbuf_advance_pos(out, nr);
85 printbuf_nul_terminate_reserved(out);
88 static void __printbuf_do_indent(struct printbuf *out, unsigned pos)
92 unsigned len = out->pos - pos;
93 char *p = out->buf + pos;
94 char *n = memscan(p, '\n', len);
95 if (cur_tabstop(out)) {
96 n = min(n, (char *) memscan(p, '\r', len));
97 n = min(n, (char *) memscan(p, '\t', len));
107 out->last_newline = pos;
109 printbuf_insert_spaces(out, pos, out->indent);
111 pos = min(pos + out->indent, out->pos);
112 out->last_field = pos;
113 out->cur_tabstop = 0;
116 memmove(n, n + 1, out->pos - pos);
118 pad = (int) cur_tabstop(out) - (int) __printbuf_linelen(out, pos);
120 printbuf_insert_spaces(out, out->last_field, pad);
124 out->last_field = pos;
128 pad = (int) cur_tabstop(out) - (int) __printbuf_linelen(out, pos) - 1;
131 printbuf_insert_spaces(out, pos, pad - 1);
134 memmove(n, n + 1, out->pos - pos);
138 out->last_field = pos;
145 static inline void printbuf_do_indent(struct printbuf *out, unsigned pos)
147 if (out->has_indent_or_tabstops && !out->suppress_indent_tabstop_handling)
148 __printbuf_do_indent(out, pos);
151 void bch2_prt_vprintf(struct printbuf *out, const char *fmt, va_list args)
158 va_copy(args2, args);
159 len = vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), fmt, args2);
161 } while (len > printbuf_remaining(out) &&
162 !bch2_printbuf_make_room(out, len));
164 unsigned indent_pos = out->pos;
165 printbuf_advance_pos(out, len);
166 printbuf_do_indent(out, indent_pos);
169 void bch2_prt_printf(struct printbuf *out, const char *fmt, ...)
176 len = vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), fmt, args);
178 } while (len > printbuf_remaining(out) &&
179 !bch2_printbuf_make_room(out, len));
181 unsigned indent_pos = out->pos;
182 printbuf_advance_pos(out, len);
183 printbuf_do_indent(out, indent_pos);
187 * bch2_printbuf_str() - returns printbuf's buf as a C string, guaranteed to be
189 * @buf: printbuf to terminate
190 * Returns: Printbuf contents, as a nul terminated C string
192 const char *bch2_printbuf_str(const struct printbuf *buf)
195 * If we've written to a printbuf then it's guaranteed to be a null
196 * terminated string - but if we haven't, then we might not have
197 * allocated a buffer at all:
205 * bch2_printbuf_exit() - exit a printbuf, freeing memory it owns and poisoning it
206 * against accidental use.
207 * @buf: printbuf to exit
209 void bch2_printbuf_exit(struct printbuf *buf)
211 if (buf->heap_allocated) {
213 buf->buf = ERR_PTR(-EINTR); /* poison value */
217 void bch2_printbuf_tabstops_reset(struct printbuf *buf)
219 buf->nr_tabstops = 0;
222 void bch2_printbuf_tabstop_pop(struct printbuf *buf)
224 if (buf->nr_tabstops)
229 * bch2_printbuf_tabstop_set() - add a tabstop, n spaces from the previous tabstop
231 * @buf: printbuf to control
232 * @spaces: number of spaces from previous tabpstop
234 * In the future this function may allocate memory if setting more than
235 * PRINTBUF_INLINE_TABSTOPS or setting tabstops more than 255 spaces from start
238 int bch2_printbuf_tabstop_push(struct printbuf *buf, unsigned spaces)
240 unsigned prev_tabstop = buf->nr_tabstops
241 ? buf->_tabstops[buf->nr_tabstops - 1]
244 if (WARN_ON(buf->nr_tabstops >= ARRAY_SIZE(buf->_tabstops)))
247 buf->_tabstops[buf->nr_tabstops++] = prev_tabstop + spaces;
248 buf->has_indent_or_tabstops = true;
253 * bch2_printbuf_indent_add() - add to the current indent level
255 * @buf: printbuf to control
256 * @spaces: number of spaces to add to the current indent level
258 * Subsequent lines, and the current line if the output position is at the start
259 * of the current line, will be indented by @spaces more spaces.
261 void bch2_printbuf_indent_add(struct printbuf *buf, unsigned spaces)
263 if (WARN_ON_ONCE(buf->indent + spaces < buf->indent))
266 buf->indent += spaces;
267 prt_chars(buf, ' ', spaces);
269 buf->has_indent_or_tabstops = true;
273 * bch2_printbuf_indent_sub() - subtract from the current indent level
275 * @buf: printbuf to control
276 * @spaces: number of spaces to subtract from the current indent level
278 * Subsequent lines, and the current line if the output position is at the start
279 * of the current line, will be indented by @spaces less spaces.
281 void bch2_printbuf_indent_sub(struct printbuf *buf, unsigned spaces)
283 if (WARN_ON_ONCE(spaces > buf->indent))
284 spaces = buf->indent;
286 if (buf->last_newline + buf->indent == buf->pos) {
288 printbuf_nul_terminate(buf);
290 buf->indent -= spaces;
292 if (!buf->indent && !buf->nr_tabstops)
293 buf->has_indent_or_tabstops = false;
296 void bch2_prt_newline(struct printbuf *buf)
298 bch2_printbuf_make_room(buf, 1 + buf->indent);
300 __prt_char_reserved(buf, '\n');
302 buf->last_newline = buf->pos;
304 __prt_chars_reserved(buf, ' ', buf->indent);
306 printbuf_nul_terminate_reserved(buf);
308 buf->last_field = buf->pos;
309 buf->cur_tabstop = 0;
312 static void __prt_tab(struct printbuf *out)
314 int spaces = max_t(int, 0, cur_tabstop(out) - printbuf_linelen(out));
316 prt_chars(out, ' ', spaces);
318 out->last_field = out->pos;
323 * bch2_prt_tab() - Advance printbuf to the next tabstop
324 * @out: printbuf to control
326 * Advance output to the next tabstop by printing spaces.
328 void bch2_prt_tab(struct printbuf *out)
330 if (WARN_ON(!cur_tabstop(out)))
336 static void __prt_tab_rjust(struct printbuf *buf)
338 int pad = (int) cur_tabstop(buf) - (int) printbuf_linelen(buf);
340 printbuf_insert_spaces(buf, buf->last_field, pad);
342 buf->last_field = buf->pos;
347 * bch2_prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
350 * @buf: printbuf to control
352 * Advance output to the next tabstop by inserting spaces immediately after the
353 * previous tabstop, right justifying previously outputted text.
355 void bch2_prt_tab_rjust(struct printbuf *buf)
357 if (WARN_ON(!cur_tabstop(buf)))
360 __prt_tab_rjust(buf);
364 * bch2_prt_bytes_indented() - Print an array of chars, handling embedded control characters
366 * @out: output printbuf
367 * @str: string to print
368 * @count: number of bytes to print
370 * The following contol characters are handled as so:
371 * \n: prt_newline newline that obeys current indent level
372 * \t: prt_tab advance to next tabstop
373 * \r: prt_tab_rjust advance to next tabstop, with right justification
375 void bch2_prt_bytes_indented(struct printbuf *out, const char *str, unsigned count)
377 unsigned indent_pos = out->pos;
378 prt_bytes(out, str, count);
379 printbuf_do_indent(out, indent_pos);
383 * bch2_prt_human_readable_u64() - Print out a u64 in human readable units
384 * @out: output printbuf
385 * @v: integer to print
387 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
389 void bch2_prt_human_readable_u64(struct printbuf *out, u64 v)
391 bch2_printbuf_make_room(out, 10);
392 unsigned len = string_get_size(v, 1, !out->si_units,
394 printbuf_remaining_size(out));
395 printbuf_advance_pos(out, len);
399 * bch2_prt_human_readable_s64() - Print out a s64 in human readable units
400 * @out: output printbuf
401 * @v: integer to print
403 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
405 void bch2_prt_human_readable_s64(struct printbuf *out, s64 v)
409 bch2_prt_human_readable_u64(out, abs(v));
413 * bch2_prt_units_u64() - Print out a u64 according to printbuf unit options
414 * @out: output printbuf
415 * @v: integer to print
417 * Units are either raw (default), or human reabable units (controlled via
418 * @buf->human_readable_units)
420 void bch2_prt_units_u64(struct printbuf *out, u64 v)
422 if (out->human_readable_units)
423 bch2_prt_human_readable_u64(out, v);
425 bch2_prt_printf(out, "%llu", v);
429 * bch2_prt_units_s64() - Print out a s64 according to printbuf unit options
430 * @out: output printbuf
431 * @v: integer to print
433 * Units are either raw (default), or human reabable units (controlled via
434 * @buf->human_readable_units)
436 void bch2_prt_units_s64(struct printbuf *out, s64 v)
440 bch2_prt_units_u64(out, abs(v));
443 void bch2_prt_string_option(struct printbuf *out,
444 const char * const list[],
447 for (size_t i = 0; list[i]; i++)
448 bch2_prt_printf(out, i == selected ? "[%s] " : "%s ", list[i]);
451 void bch2_prt_bitflags(struct printbuf *out,
452 const char * const list[], u64 flags)
454 unsigned bit, nr = 0;
460 while (flags && (bit = __ffs64(flags)) < nr) {
462 bch2_prt_printf(out, ",");
464 bch2_prt_printf(out, "%s", list[bit]);
465 flags ^= BIT_ULL(bit);
469 void bch2_prt_bitflags_vector(struct printbuf *out,
470 const char * const list[],
471 unsigned long *v, unsigned nr)
476 for (i = 0; i < nr; i++)
482 for_each_set_bit(i, v, nr) {
484 bch2_prt_printf(out, ",");
486 bch2_prt_printf(out, "%s", list[i]);