Lines Matching +full:out +full:-
1 // SPDX-License-Identifier: LGPL-2.1+
15 return pos - buf->last_newline; in __printbuf_linelen()
20 return __printbuf_linelen(buf, buf->pos); in printbuf_linelen()
28 return buf->cur_tabstop < buf->nr_tabstops in cur_tabstop()
29 ? buf->_tabstops[buf->cur_tabstop] in cur_tabstop()
33 int bch2_printbuf_make_room(struct printbuf *out, unsigned extra) in bch2_printbuf_make_room() argument
38 if (out->pos + extra <= out->size) in bch2_printbuf_make_room()
41 if (!out->heap_allocated) { in bch2_printbuf_make_room()
42 out->overflow = true; in bch2_printbuf_make_room()
46 unsigned new_size = roundup_pow_of_two(out->size + extra); in bch2_printbuf_make_room()
50 out->allocation_failure = true; in bch2_printbuf_make_room()
51 out->overflow = true; in bch2_printbuf_make_room()
52 return -ENOMEM; in bch2_printbuf_make_room()
59 char *buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWAIT); in bch2_printbuf_make_room()
62 out->allocation_failure = true; in bch2_printbuf_make_room()
63 out->overflow = true; in bch2_printbuf_make_room()
64 return -ENOMEM; in bch2_printbuf_make_room()
67 out->buf = buf; in bch2_printbuf_make_room()
68 out->size = new_size; in bch2_printbuf_make_room()
72 static void printbuf_advance_pos(struct printbuf *out, unsigned len) in printbuf_advance_pos() argument
74 out->pos += min(len, printbuf_remaining(out)); in printbuf_advance_pos()
77 static void printbuf_insert_spaces(struct printbuf *out, unsigned pos, unsigned nr) in printbuf_insert_spaces() argument
79 unsigned move = out->pos - pos; in printbuf_insert_spaces()
81 bch2_printbuf_make_room(out, nr); in printbuf_insert_spaces()
83 if (pos + nr < out->size) in printbuf_insert_spaces()
84 memmove(out->buf + pos + nr, in printbuf_insert_spaces()
85 out->buf + pos, in printbuf_insert_spaces()
86 min(move, out->size - 1 - pos - nr)); in printbuf_insert_spaces()
88 if (pos < out->size) in printbuf_insert_spaces()
89 memset(out->buf + pos, ' ', min(nr, out->size - pos)); in printbuf_insert_spaces()
91 printbuf_advance_pos(out, nr); in printbuf_insert_spaces()
92 printbuf_nul_terminate_reserved(out); in printbuf_insert_spaces()
95 static void __printbuf_do_indent(struct printbuf *out, unsigned pos) in __printbuf_do_indent() argument
99 unsigned len = out->pos - pos; in __printbuf_do_indent()
100 char *p = out->buf + pos; in __printbuf_do_indent()
102 if (cur_tabstop(out)) { in __printbuf_do_indent()
107 pos = n - out->buf; in __printbuf_do_indent()
108 if (pos == out->pos) in __printbuf_do_indent()
114 out->last_newline = pos; in __printbuf_do_indent()
116 printbuf_insert_spaces(out, pos, out->indent); in __printbuf_do_indent()
118 pos = min(pos + out->indent, out->pos); in __printbuf_do_indent()
119 out->last_field = pos; in __printbuf_do_indent()
120 out->cur_tabstop = 0; in __printbuf_do_indent()
123 memmove(n, n + 1, out->pos - pos); in __printbuf_do_indent()
124 --out->pos; in __printbuf_do_indent()
125 pad = (int) cur_tabstop(out) - (int) __printbuf_linelen(out, pos); in __printbuf_do_indent()
127 printbuf_insert_spaces(out, out->last_field, pad); in __printbuf_do_indent()
131 out->last_field = pos; in __printbuf_do_indent()
132 out->cur_tabstop++; in __printbuf_do_indent()
135 pad = (int) cur_tabstop(out) - (int) __printbuf_linelen(out, pos) - 1; in __printbuf_do_indent()
138 printbuf_insert_spaces(out, pos, pad - 1); in __printbuf_do_indent()
141 memmove(n, n + 1, out->pos - pos); in __printbuf_do_indent()
142 --out->pos; in __printbuf_do_indent()
145 out->last_field = pos; in __printbuf_do_indent()
146 out->cur_tabstop++; in __printbuf_do_indent()
152 static inline void printbuf_do_indent(struct printbuf *out, unsigned pos) in printbuf_do_indent() argument
154 if (out->has_indent_or_tabstops && !out->suppress_indent_tabstop_handling) in printbuf_do_indent()
155 __printbuf_do_indent(out, pos); in printbuf_do_indent()
158 void bch2_prt_vprintf(struct printbuf *out, const char *fmt, va_list args) in bch2_prt_vprintf() argument
166 len = vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), fmt, args2); in bch2_prt_vprintf()
168 } while (len > printbuf_remaining(out) && in bch2_prt_vprintf()
169 !bch2_printbuf_make_room(out, len)); in bch2_prt_vprintf()
171 unsigned indent_pos = out->pos; in bch2_prt_vprintf()
172 printbuf_advance_pos(out, len); in bch2_prt_vprintf()
173 printbuf_do_indent(out, indent_pos); in bch2_prt_vprintf()
176 void bch2_prt_printf(struct printbuf *out, const char *fmt, ...) in bch2_prt_printf() argument
183 len = vsnprintf(out->buf + out->pos, printbuf_remaining_size(out), fmt, args); in bch2_prt_printf()
185 } while (len > printbuf_remaining(out) && in bch2_prt_printf()
186 !bch2_printbuf_make_room(out, len)); in bch2_prt_printf()
188 unsigned indent_pos = out->pos; in bch2_prt_printf()
189 printbuf_advance_pos(out, len); in bch2_prt_printf()
190 printbuf_do_indent(out, indent_pos); in bch2_prt_printf()
194 * bch2_printbuf_str() - returns printbuf's buf as a C string, guaranteed to be
203 * terminated string - but if we haven't, then we might not have in bch2_printbuf_str()
206 return buf->pos in bch2_printbuf_str()
207 ? buf->buf in bch2_printbuf_str()
212 * bch2_printbuf_exit() - exit a printbuf, freeing memory it owns and poisoning it
218 if (buf->heap_allocated) { in bch2_printbuf_exit()
219 kfree(buf->buf); in bch2_printbuf_exit()
220 buf->buf = ERR_PTR(-EINTR); /* poison value */ in bch2_printbuf_exit()
226 buf->nr_tabstops = 0; in bch2_printbuf_tabstops_reset()
231 if (buf->nr_tabstops) in bch2_printbuf_tabstop_pop()
232 --buf->nr_tabstops; in bch2_printbuf_tabstop_pop()
236 * bch2_printbuf_tabstop_set() - add a tabstop, n spaces from the previous tabstop
247 unsigned prev_tabstop = buf->nr_tabstops in bch2_printbuf_tabstop_push()
248 ? buf->_tabstops[buf->nr_tabstops - 1] in bch2_printbuf_tabstop_push()
251 if (WARN_ON(buf->nr_tabstops >= ARRAY_SIZE(buf->_tabstops))) in bch2_printbuf_tabstop_push()
252 return -EINVAL; in bch2_printbuf_tabstop_push()
254 buf->_tabstops[buf->nr_tabstops++] = prev_tabstop + spaces; in bch2_printbuf_tabstop_push()
255 buf->has_indent_or_tabstops = true; in bch2_printbuf_tabstop_push()
260 * bch2_printbuf_indent_add() - add to the current indent level
270 if (WARN_ON_ONCE(buf->indent + spaces < buf->indent)) in bch2_printbuf_indent_add()
273 buf->indent += spaces; in bch2_printbuf_indent_add()
276 buf->has_indent_or_tabstops = true; in bch2_printbuf_indent_add()
280 * bch2_printbuf_indent_sub() - subtract from the current indent level
290 if (WARN_ON_ONCE(spaces > buf->indent)) in bch2_printbuf_indent_sub()
291 spaces = buf->indent; in bch2_printbuf_indent_sub()
293 if (buf->last_newline + buf->indent == buf->pos) { in bch2_printbuf_indent_sub()
294 buf->pos -= spaces; in bch2_printbuf_indent_sub()
297 buf->indent -= spaces; in bch2_printbuf_indent_sub()
299 if (!buf->indent && !buf->nr_tabstops) in bch2_printbuf_indent_sub()
300 buf->has_indent_or_tabstops = false; in bch2_printbuf_indent_sub()
305 bch2_printbuf_make_room(buf, 1 + buf->indent); in bch2_prt_newline()
309 buf->last_newline = buf->pos; in bch2_prt_newline()
311 __prt_chars_reserved(buf, ' ', buf->indent); in bch2_prt_newline()
315 buf->last_field = buf->pos; in bch2_prt_newline()
316 buf->cur_tabstop = 0; in bch2_prt_newline()
319 void bch2_printbuf_strip_trailing_newline(struct printbuf *out) in bch2_printbuf_strip_trailing_newline() argument
321 for (int p = out->pos - 1; p >= 0; --p) { in bch2_printbuf_strip_trailing_newline()
322 if (out->buf[p] == '\n') { in bch2_printbuf_strip_trailing_newline()
323 out->pos = p; in bch2_printbuf_strip_trailing_newline()
326 if (out->buf[p] != ' ') in bch2_printbuf_strip_trailing_newline()
330 printbuf_nul_terminate_reserved(out); in bch2_printbuf_strip_trailing_newline()
333 static void __prt_tab(struct printbuf *out) in __prt_tab() argument
335 int spaces = max_t(int, 0, cur_tabstop(out) - printbuf_linelen(out)); in __prt_tab()
337 prt_chars(out, ' ', spaces); in __prt_tab()
339 out->last_field = out->pos; in __prt_tab()
340 out->cur_tabstop++; in __prt_tab()
344 * bch2_prt_tab() - Advance printbuf to the next tabstop
345 * @out: printbuf to control
349 void bch2_prt_tab(struct printbuf *out) in bch2_prt_tab() argument
351 if (WARN_ON(!cur_tabstop(out))) in bch2_prt_tab()
354 __prt_tab(out); in bch2_prt_tab()
359 int pad = (int) cur_tabstop(buf) - (int) printbuf_linelen(buf); in __prt_tab_rjust()
361 printbuf_insert_spaces(buf, buf->last_field, pad); in __prt_tab_rjust()
363 buf->last_field = buf->pos; in __prt_tab_rjust()
364 buf->cur_tabstop++; in __prt_tab_rjust()
368 * bch2_prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
385 * bch2_prt_bytes_indented() - Print an array of chars, handling embedded control characters
387 * @out: output printbuf
396 void bch2_prt_bytes_indented(struct printbuf *out, const char *str, unsigned count) in bch2_prt_bytes_indented() argument
398 unsigned indent_pos = out->pos; in bch2_prt_bytes_indented()
399 prt_bytes(out, str, count); in bch2_prt_bytes_indented()
400 printbuf_do_indent(out, indent_pos); in bch2_prt_bytes_indented()
404 * bch2_prt_human_readable_u64() - Print out a u64 in human readable units
405 * @out: output printbuf
408 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
410 void bch2_prt_human_readable_u64(struct printbuf *out, u64 v) in bch2_prt_human_readable_u64() argument
412 bch2_printbuf_make_room(out, 10); in bch2_prt_human_readable_u64()
413 unsigned len = string_get_size(v, 1, !out->si_units, in bch2_prt_human_readable_u64()
414 out->buf + out->pos, in bch2_prt_human_readable_u64()
415 printbuf_remaining_size(out)); in bch2_prt_human_readable_u64()
416 printbuf_advance_pos(out, len); in bch2_prt_human_readable_u64()
420 * bch2_prt_human_readable_s64() - Print out a s64 in human readable units
421 * @out: output printbuf
424 * Units of 2^10 (default) or 10^3 are controlled via @out->si_units
426 void bch2_prt_human_readable_s64(struct printbuf *out, s64 v) in bch2_prt_human_readable_s64() argument
429 prt_char(out, '-'); in bch2_prt_human_readable_s64()
430 bch2_prt_human_readable_u64(out, abs(v)); in bch2_prt_human_readable_s64()
434 * bch2_prt_units_u64() - Print out a u64 according to printbuf unit options
435 * @out: output printbuf
439 * @buf->human_readable_units)
441 void bch2_prt_units_u64(struct printbuf *out, u64 v) in bch2_prt_units_u64() argument
443 if (out->human_readable_units) in bch2_prt_units_u64()
444 bch2_prt_human_readable_u64(out, v); in bch2_prt_units_u64()
446 bch2_prt_printf(out, "%llu", v); in bch2_prt_units_u64()
450 * bch2_prt_units_s64() - Print out a s64 according to printbuf unit options
451 * @out: output printbuf
455 * @buf->human_readable_units)
457 void bch2_prt_units_s64(struct printbuf *out, s64 v) in bch2_prt_units_s64() argument
460 prt_char(out, '-'); in bch2_prt_units_s64()
461 bch2_prt_units_u64(out, abs(v)); in bch2_prt_units_s64()
464 void bch2_prt_string_option(struct printbuf *out, in bch2_prt_string_option() argument
469 bch2_prt_printf(out, i == selected ? "[%s] " : "%s ", list[i]); in bch2_prt_string_option()
472 void bch2_prt_bitflags(struct printbuf *out, in bch2_prt_bitflags() argument
483 bch2_prt_printf(out, ","); in bch2_prt_bitflags()
485 bch2_prt_printf(out, "%s", list[bit]); in bch2_prt_bitflags()
490 void bch2_prt_bitflags_vector(struct printbuf *out, in bch2_prt_bitflags_vector() argument
499 nr = i - 1; in bch2_prt_bitflags_vector()
505 bch2_prt_printf(out, ","); in bch2_prt_bitflags_vector()
507 bch2_prt_printf(out, "%s", list[i]); in bch2_prt_bitflags_vector()