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