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