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