xref: /freebsd/sys/contrib/openzfs/include/libnvpair.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
15  */
16 
17 #ifndef	_LIBNVPAIR_H
18 #define	_LIBNVPAIR_H extern __attribute__((visibility("default")))
19 
20 #include <sys/nvpair.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <regex.h>
24 
25 #ifdef	__cplusplus
26 extern "C" {
27 #endif
28 
29 /*
30  * All interfaces described in this file are private to Solaris, and
31  * are subject to change at any time and without notice.  The public
32  * nvlist/nvpair interfaces, as documented in manpage sections 3NVPAIR,
33  * are all imported from <sys/nvpair.h> included above.
34  */
35 
36 _LIBNVPAIR_H int nvpair_value_match(nvpair_t *, int, const char *,
37     const char **);
38 _LIBNVPAIR_H int nvpair_value_match_regex(nvpair_t *, int, const char *,
39     regex_t *, const char **);
40 
41 _LIBNVPAIR_H void nvlist_print(FILE *, nvlist_t *);
42 _LIBNVPAIR_H int nvlist_print_json(FILE *, nvlist_t *);
43 _LIBNVPAIR_H void dump_nvlist(nvlist_t *, int);
44 
45 /*
46  * Private nvlist printing interface that allows the caller some control
47  * over output rendering (as opposed to nvlist_print and dump_nvlist).
48  *
49  * Obtain an opaque nvlist_prtctl_t cookie using nvlist_prtctl_alloc
50  * (NULL on failure);  on return the cookie is set up for default formatting
51  * and rendering.  Quote the cookie in subsequent customisation functions and
52  * then pass the cookie to nvlist_prt to render the nvlist.  Finally,
53  * use nvlist_prtctl_free to release the cookie.
54  *
55  * For all nvlist_lookup_xxx and nvlist_lookup_xxx_array functions
56  * we have a corresponding brace of functions that appoint replacement
57  * rendering functions:
58  *
59  *	extern void nvlist_prtctl_xxx(nvlist_prtctl_t,
60  *	    void (*)(nvlist_prtctl_t ctl, void *private, const char *name,
61  *	    xxxtype value))
62  *
63  *	and
64  *
65  *	extern void nvlist_prtctl_xxx_array(nvlist_prtctl_t,
66  *	    void (*)(nvlist_prtctl_t ctl, void *private, const char *name,
67  *	    xxxtype value, uint_t count))
68  *
69  * where xxxtype is the C datatype corresponding to xxx, eg int8_t for "int8"
70  * and char * for "string".  The function that is appointed to render the
71  * specified datatype receives as arguments the cookie, the nvlist
72  * member name, the value of that member (or a pointer for array function),
73  * and (for array rendering functions) a count of the number of elements.
74  */
75 
76 typedef struct nvlist_prtctl *nvlist_prtctl_t;	/* opaque */
77 
78 enum nvlist_indent_mode {
79 	NVLIST_INDENT_ABS,	/* Absolute indentation */
80 	NVLIST_INDENT_TABBED	/* Indent with tabstops */
81 };
82 
83 _LIBNVPAIR_H nvlist_prtctl_t nvlist_prtctl_alloc(void);
84 _LIBNVPAIR_H void nvlist_prtctl_free(nvlist_prtctl_t);
85 _LIBNVPAIR_H void nvlist_prt(nvlist_t *, nvlist_prtctl_t);
86 
87 /* Output stream */
88 _LIBNVPAIR_H void nvlist_prtctl_setdest(nvlist_prtctl_t, FILE *);
89 _LIBNVPAIR_H FILE *nvlist_prtctl_getdest(nvlist_prtctl_t);
90 
91 /* Indentation mode, start indent, indent increment; default tabbed/0/1 */
92 _LIBNVPAIR_H void nvlist_prtctl_setindent(nvlist_prtctl_t,
93     enum nvlist_indent_mode, int, int);
94 _LIBNVPAIR_H void nvlist_prtctl_doindent(nvlist_prtctl_t, int);
95 
96 enum nvlist_prtctl_fmt {
97 	NVLIST_FMT_MEMBER_NAME,		/* name fmt; default "%s = " */
98 	NVLIST_FMT_MEMBER_POSTAMBLE,	/* after nvlist member; default "\n" */
99 	NVLIST_FMT_BTWN_ARRAY		/* between array members; default " " */
100 };
101 
102 _LIBNVPAIR_H void nvlist_prtctl_setfmt(nvlist_prtctl_t, enum nvlist_prtctl_fmt,
103     const char *);
104 _LIBNVPAIR_H void nvlist_prtctl_dofmt(nvlist_prtctl_t, enum nvlist_prtctl_fmt,
105     ...);
106 
107 /*
108  * Function prototypes for interfaces that appoint a new rendering function
109  * for single-valued nvlist members.
110  *
111  * A replacement function receives arguments as follows:
112  *
113  *	nvlist_prtctl_t	Print control structure; do not change preferences
114  *			for this object from a print callback function.
115  *
116  *	void *		The function-private cookie argument registered
117  *			when the replacement function was appointed.
118  *
119  *	nvlist_t *	The full nvlist that is being processed.  The
120  *			rendering function is called to render a single
121  *			member (name and value passed as below) but it may
122  *			want to reference or incorporate other aspects of
123  *			the full nvlist.
124  *
125  *	const char *	Member name to render
126  *
127  *	valtype		Value of the member to render
128  *
129  * The function must return non-zero if it has rendered output for this
130  * member, or 0 if it wants to default to standard rendering for this
131  * one member.
132  */
133 
134 #define	NVLIST_PRINTCTL_SVDECL(funcname, valtype) \
135     _LIBNVPAIR_H void funcname(nvlist_prtctl_t, \
136     int (*)(nvlist_prtctl_t, void *, nvlist_t *, const char *, valtype), \
137     void *)
138 
139 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_boolean, int);
140 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_boolean_value, boolean_t);
141 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_byte, uchar_t);
142 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int8, int8_t);
143 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint8, uint8_t);
144 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int16, int16_t);
145 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint16, uint16_t);
146 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int32, int32_t);
147 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint32, uint32_t);
148 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int64, int64_t);
149 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint64, uint64_t);
150 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_double, double);
151 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_string, const char *);
152 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_hrtime, hrtime_t);
153 NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_nvlist, nvlist_t *);
154 
155 #undef	NVLIST_PRINTCTL_SVDECL	/* was just for "clarity" above */
156 
157 /*
158  * Function prototypes for interfaces that appoint a new rendering function
159  * for array-valued nvlist members.
160  *
161  * One additional argument is taken: uint_t for the number of array elements
162  *
163  * Return values as above.
164  */
165 #define	NVLIST_PRINTCTL_AVDECL(funcname, vtype) \
166     _LIBNVPAIR_H void funcname(nvlist_prtctl_t, \
167     int (*)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, uint_t), \
168     void *)
169 
170 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_boolean_array, boolean_t *);
171 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_byte_array, uchar_t *);
172 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int8_array, int8_t *);
173 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint8_array, uint8_t *);
174 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int16_array, int16_t *);
175 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint16_array, uint16_t *);
176 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int32_array, int32_t *);
177 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint32_array, uint32_t *);
178 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int64_array, int64_t *);
179 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint64_array, uint64_t *);
180 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_string_array, const char **);
181 NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_nvlist_array, nvlist_t **);
182 
183 #undef	NVLIST_PRINTCTL_AVDECL	/* was just for "clarity" above */
184 
185 #ifdef	__cplusplus
186 }
187 #endif
188 
189 #endif	/* _LIBNVPAIR_H */
190