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