]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/printbuf.c
Update bcachefs sources to 33a60d9b05 bcachefs: Assorted fixes for clang
[bcachefs-tools-debian] / libbcachefs / printbuf.c
1 // SPDX-License-Identifier: LGPL-2.1+
2 /* Copyright (C) 2022 Kent Overstreet */
3
4 #include <linux/err.h>
5 #include <linux/export.h>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/string_helpers.h>
9
10 #include "printbuf.h"
11
12 static inline unsigned printbuf_linelen(struct printbuf *buf)
13 {
14         return buf->pos - buf->last_newline;
15 }
16
17 int bch2_printbuf_make_room(struct printbuf *out, unsigned extra)
18 {
19         unsigned new_size;
20         char *buf;
21
22         if (!out->heap_allocated)
23                 return 0;
24
25         /* Reserved space for terminating nul: */
26         extra += 1;
27
28         if (out->pos + extra < out->size)
29                 return 0;
30
31         new_size = roundup_pow_of_two(out->size + extra);
32
33         /*
34          * Note: output buffer must be freeable with kfree(), it's not required
35          * that the user use printbuf_exit().
36          */
37         buf = krealloc(out->buf, new_size, !out->atomic ? GFP_KERNEL : GFP_NOWAIT);
38
39         if (!buf) {
40                 out->allocation_failure = true;
41                 return -ENOMEM;
42         }
43
44         out->buf        = buf;
45         out->size       = new_size;
46         return 0;
47 }
48
49 void bch2_prt_vprintf(struct printbuf *out, const char *fmt, va_list args)
50 {
51         int len;
52
53         do {
54                 va_list args2;
55
56                 va_copy(args2, 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));
60
61         len = min_t(size_t, len,
62                   printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
63         out->pos += len;
64 }
65
66 void bch2_prt_printf(struct printbuf *out, const char *fmt, ...)
67 {
68         va_list args;
69         int len;
70
71         do {
72                 va_start(args, fmt);
73                 len = vsnprintf(out->buf + out->pos, printbuf_remaining(out), fmt, args);
74                 va_end(args);
75         } while (len + 1 >= printbuf_remaining(out) &&
76                  !bch2_printbuf_make_room(out, len + 1));
77
78         len = min_t(size_t, len,
79                   printbuf_remaining(out) ? printbuf_remaining(out) - 1 : 0);
80         out->pos += len;
81 }
82
83 /**
84  * printbuf_str - returns printbuf's buf as a C string, guaranteed to be null
85  * terminated
86  */
87 const char *bch2_printbuf_str(const struct printbuf *buf)
88 {
89         /*
90          * If we've written to a printbuf then it's guaranteed to be a null
91          * terminated string - but if we haven't, then we might not have
92          * allocated a buffer at all:
93          */
94         return buf->pos
95                 ? buf->buf
96                 : "";
97 }
98
99 /**
100  * printbuf_exit - exit a printbuf, freeing memory it owns and poisoning it
101  * against accidental use.
102  */
103 void bch2_printbuf_exit(struct printbuf *buf)
104 {
105         if (buf->heap_allocated) {
106                 kfree(buf->buf);
107                 buf->buf = ERR_PTR(-EINTR); /* poison value */
108         }
109 }
110
111 void bch2_printbuf_tabstops_reset(struct printbuf *buf)
112 {
113         buf->nr_tabstops = 0;
114 }
115
116 void bch2_printbuf_tabstop_pop(struct printbuf *buf)
117 {
118         if (buf->nr_tabstops)
119                 --buf->nr_tabstops;
120 }
121
122 /*
123  * printbuf_tabstop_set - add a tabstop, n spaces from the previous tabstop
124  *
125  * @buf: printbuf to control
126  * @spaces: number of spaces from previous tabpstop
127  *
128  * In the future this function may allocate memory if setting more than
129  * PRINTBUF_INLINE_TABSTOPS or setting tabstops more than 255 spaces from start
130  * of line.
131  */
132 int bch2_printbuf_tabstop_push(struct printbuf *buf, unsigned spaces)
133 {
134         unsigned prev_tabstop = buf->nr_tabstops
135                 ? buf->_tabstops[buf->nr_tabstops - 1]
136                 : 0;
137
138         if (WARN_ON(buf->nr_tabstops >= ARRAY_SIZE(buf->_tabstops)))
139                 return -EINVAL;
140
141         buf->_tabstops[buf->nr_tabstops++] = prev_tabstop + spaces;
142         buf->has_indent_or_tabstops = true;
143         return 0;
144 }
145
146 /**
147  * printbuf_indent_add - add to the current indent level
148  *
149  * @buf: printbuf to control
150  * @spaces: number of spaces to add to the current indent level
151  *
152  * Subsequent lines, and the current line if the output position is at the start
153  * of the current line, will be indented by @spaces more spaces.
154  */
155 void bch2_printbuf_indent_add(struct printbuf *buf, unsigned spaces)
156 {
157         if (WARN_ON_ONCE(buf->indent + spaces < buf->indent))
158                 spaces = 0;
159
160         buf->indent += spaces;
161         prt_chars(buf, ' ', spaces);
162
163         buf->has_indent_or_tabstops = true;
164 }
165
166 /**
167  * printbuf_indent_sub - subtract from the current indent level
168  *
169  * @buf: printbuf to control
170  * @spaces: number of spaces to subtract from the current indent level
171  *
172  * Subsequent lines, and the current line if the output position is at the start
173  * of the current line, will be indented by @spaces less spaces.
174  */
175 void bch2_printbuf_indent_sub(struct printbuf *buf, unsigned spaces)
176 {
177         if (WARN_ON_ONCE(spaces > buf->indent))
178                 spaces = buf->indent;
179
180         if (buf->last_newline + buf->indent == buf->pos) {
181                 buf->pos -= spaces;
182                 printbuf_nul_terminate(buf);
183         }
184         buf->indent -= spaces;
185
186         if (!buf->indent && !buf->nr_tabstops)
187                 buf->has_indent_or_tabstops = false;
188 }
189
190 void bch2_prt_newline(struct printbuf *buf)
191 {
192         unsigned i;
193
194         bch2_printbuf_make_room(buf, 1 + buf->indent);
195
196         __prt_char(buf, '\n');
197
198         buf->last_newline       = buf->pos;
199
200         for (i = 0; i < buf->indent; i++)
201                 __prt_char(buf, ' ');
202
203         printbuf_nul_terminate(buf);
204
205         buf->last_field         = buf->pos;
206         buf->cur_tabstop        = 0;
207 }
208
209 /*
210  * Returns spaces from start of line, if set, or 0 if unset:
211  */
212 static inline unsigned cur_tabstop(struct printbuf *buf)
213 {
214         return buf->cur_tabstop < buf->nr_tabstops
215                 ? buf->_tabstops[buf->cur_tabstop]
216                 : 0;
217 }
218
219 static void __prt_tab(struct printbuf *out)
220 {
221         int spaces = max_t(int, 0, cur_tabstop(out) - printbuf_linelen(out));
222
223         prt_chars(out, ' ', spaces);
224
225         out->last_field = out->pos;
226         out->cur_tabstop++;
227 }
228
229 /**
230  * prt_tab - Advance printbuf to the next tabstop
231  *
232  * @buf: printbuf to control
233  *
234  * Advance output to the next tabstop by printing spaces.
235  */
236 void bch2_prt_tab(struct printbuf *out)
237 {
238         if (WARN_ON(!cur_tabstop(out)))
239                 return;
240
241         __prt_tab(out);
242 }
243
244 static void __prt_tab_rjust(struct printbuf *buf)
245 {
246         unsigned move = buf->pos - buf->last_field;
247         int pad = (int) cur_tabstop(buf) - (int) printbuf_linelen(buf);
248
249         if (pad > 0) {
250                 bch2_printbuf_make_room(buf, pad);
251
252                 if (buf->last_field + pad < buf->size)
253                         memmove(buf->buf + buf->last_field + pad,
254                                 buf->buf + buf->last_field,
255                                 min(move, buf->size - 1 - buf->last_field - pad));
256
257                 if (buf->last_field < buf->size)
258                         memset(buf->buf + buf->last_field, ' ',
259                                min((unsigned) pad, buf->size - buf->last_field));
260
261                 buf->pos += pad;
262                 printbuf_nul_terminate(buf);
263         }
264
265         buf->last_field = buf->pos;
266         buf->cur_tabstop++;
267 }
268
269 /**
270  * prt_tab_rjust - Advance printbuf to the next tabstop, right justifying
271  * previous output
272  *
273  * @buf: printbuf to control
274  *
275  * Advance output to the next tabstop by inserting spaces immediately after the
276  * previous tabstop, right justifying previously outputted text.
277  */
278 void bch2_prt_tab_rjust(struct printbuf *buf)
279 {
280         if (WARN_ON(!cur_tabstop(buf)))
281                 return;
282
283         __prt_tab_rjust(buf);
284 }
285
286 /**
287  * prt_bytes_indented - Print an array of chars, handling embedded control characters
288  *
289  * @out: printbuf to output to
290  * @str: string to print
291  * @count: number of bytes to print
292  *
293  * The following contol characters are handled as so:
294  *   \n: prt_newline    newline that obeys current indent level
295  *   \t: prt_tab        advance to next tabstop
296  *   \r: prt_tab_rjust  advance to next tabstop, with right justification
297  */
298 void bch2_prt_bytes_indented(struct printbuf *out, const char *str, unsigned count)
299 {
300         const char *unprinted_start = str;
301         const char *end = str + count;
302
303         if (!out->has_indent_or_tabstops || out->suppress_indent_tabstop_handling) {
304                 prt_bytes(out, str, count);
305                 return;
306         }
307
308         while (str != end) {
309                 switch (*str) {
310                 case '\n':
311                         prt_bytes(out, unprinted_start, str - unprinted_start);
312                         unprinted_start = str + 1;
313                         bch2_prt_newline(out);
314                         break;
315                 case '\t':
316                         if (likely(cur_tabstop(out))) {
317                                 prt_bytes(out, unprinted_start, str - unprinted_start);
318                                 unprinted_start = str + 1;
319                                 __prt_tab(out);
320                         }
321                         break;
322                 case '\r':
323                         if (likely(cur_tabstop(out))) {
324                                 prt_bytes(out, unprinted_start, str - unprinted_start);
325                                 unprinted_start = str + 1;
326                                 __prt_tab_rjust(out);
327                         }
328                         break;
329                 }
330
331                 str++;
332         }
333
334         prt_bytes(out, unprinted_start, str - unprinted_start);
335 }
336
337 /**
338  * prt_human_readable_u64 - Print out a u64 in human readable units
339  *
340  * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units
341  */
342 void bch2_prt_human_readable_u64(struct printbuf *buf, u64 v)
343 {
344         bch2_printbuf_make_room(buf, 10);
345         buf->pos += string_get_size(v, 1, !buf->si_units,
346                                     buf->buf + buf->pos,
347                                     printbuf_remaining_size(buf));
348 }
349
350 /**
351  * prt_human_readable_s64 - Print out a s64 in human readable units
352  *
353  * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units
354  */
355 void bch2_prt_human_readable_s64(struct printbuf *buf, s64 v)
356 {
357         if (v < 0)
358                 prt_char(buf, '-');
359         bch2_prt_human_readable_u64(buf, abs(v));
360 }
361
362 /**
363  * prt_units_u64 - Print out a u64 according to printbuf unit options
364  *
365  * Units are either raw (default), or human reabable units (controlled via
366  * @buf->human_readable_units)
367  */
368 void bch2_prt_units_u64(struct printbuf *out, u64 v)
369 {
370         if (out->human_readable_units)
371                 bch2_prt_human_readable_u64(out, v);
372         else
373                 bch2_prt_printf(out, "%llu", v);
374 }
375
376 /**
377  * prt_units_s64 - Print out a s64 according to printbuf unit options
378  *
379  * Units are either raw (default), or human reabable units (controlled via
380  * @buf->human_readable_units)
381  */
382 void bch2_prt_units_s64(struct printbuf *out, s64 v)
383 {
384         if (v < 0)
385                 prt_char(out, '-');
386         bch2_prt_units_u64(out, abs(v));
387 }
388
389 void bch2_prt_string_option(struct printbuf *out,
390                             const char * const list[],
391                             size_t selected)
392 {
393         size_t i;
394
395         for (i = 0; list[i]; i++)
396                 bch2_prt_printf(out, i == selected ? "[%s] " : "%s ", list[i]);
397 }
398
399 void bch2_prt_bitflags(struct printbuf *out,
400                        const char * const list[], u64 flags)
401 {
402         unsigned bit, nr = 0;
403         bool first = true;
404
405         while (list[nr])
406                 nr++;
407
408         while (flags && (bit = __ffs(flags)) < nr) {
409                 if (!first)
410                         bch2_prt_printf(out, ",");
411                 first = false;
412                 bch2_prt_printf(out, "%s", list[bit]);
413                 flags ^= 1 << bit;
414         }
415 }