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