]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/pretty-printers.c
Update bcachefs sources to 24c6361e20 bcachefs: Fix a trans path overflow in bch2_btr...
[bcachefs-tools-debian] / linux / pretty-printers.c
1 // SPDX-License-Identifier: LGPL-2.1+
2 /* Copyright (C) 2022 Kent Overstreet */
3
4 #include <linux/bitops.h>
5 #include <linux/kernel.h>
6 #include <linux/printbuf.h>
7 #include <linux/pretty-printers.h>
8
9 /**
10  * prt_string_option - Given a list of strings, print out the list and indicate
11  * which option is selected, with square brackets (sysfs style)
12  *
13  * @out: The printbuf to output to
14  * @list: List of strings to choose from
15  * @selected: The option to highlight, with square brackets
16  */
17 void prt_string_option(struct printbuf *out,
18                        const char * const list[],
19                        size_t selected)
20 {
21         size_t i;
22
23         for (i = 0; list[i]; i++) {
24                 if (i)
25                         prt_char(out, ' ');
26                 if (i == selected)
27                         prt_char(out, '[');
28                 prt_str(out, list[i]);
29                 if (i == selected)
30                         prt_char(out, ']');
31         }
32 }
33 EXPORT_SYMBOL(prt_string_option);
34
35 /**
36  * prt_bitflags: Given a bitmap and a list of names for each bit, print out which
37  * bits are on, comma separated
38  *
39  * @out: The printbuf to output to
40  * @list: List of names for each bit
41  * @flags: Bits to print
42  */
43 void prt_bitflags(struct printbuf *out,
44                   const char * const list[], u64 flags)
45 {
46         unsigned bit, nr = 0;
47         bool first = true;
48
49         while (list[nr])
50                 nr++;
51
52         while (flags && (bit = __ffs(flags)) < nr) {
53                 if (!first)
54                         prt_char(out, ',');
55                 first = false;
56                 prt_str(out, list[bit]);
57                 flags ^= 1 << bit;
58         }
59 }
60 EXPORT_SYMBOL(prt_bitflags);