xref: /freebsd/sys/contrib/openzfs/include/libnvpair.h (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
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
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
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
273ff01b23SMartin 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*2a58b312SMartin Matuska _LIBNVPAIR_H int nvpair_value_match(nvpair_t *, int, const char *,
46*2a58b312SMartin Matuska     const char **);
47*2a58b312SMartin Matuska _LIBNVPAIR_H int nvpair_value_match_regex(nvpair_t *, int, const char *,
48*2a58b312SMartin Matuska     regex_t *, const char **);
49eda14cbcSMatt Macy 
503ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_print(FILE *, nvlist_t *);
513ff01b23SMartin Matuska _LIBNVPAIR_H int nvlist_print_json(FILE *, nvlist_t *);
523ff01b23SMartin Matuska _LIBNVPAIR_H void dump_nvlist(nvlist_t *, int);
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy /*
55eda14cbcSMatt Macy  * Private nvlist printing interface that allows the caller some control
56eda14cbcSMatt Macy  * over output rendering (as opposed to nvlist_print and dump_nvlist).
57eda14cbcSMatt Macy  *
58eda14cbcSMatt Macy  * Obtain an opaque nvlist_prtctl_t cookie using nvlist_prtctl_alloc
59eda14cbcSMatt Macy  * (NULL on failure);  on return the cookie is set up for default formatting
60eda14cbcSMatt Macy  * and rendering.  Quote the cookie in subsequent customisation functions and
61eda14cbcSMatt Macy  * then pass the cookie to nvlist_prt to render the nvlist.  Finally,
62eda14cbcSMatt Macy  * use nvlist_prtctl_free to release the cookie.
63eda14cbcSMatt Macy  *
64eda14cbcSMatt Macy  * For all nvlist_lookup_xxx and nvlist_lookup_xxx_array functions
65eda14cbcSMatt Macy  * we have a corresponding brace of functions that appoint replacement
66eda14cbcSMatt Macy  * rendering functions:
67eda14cbcSMatt Macy  *
68eda14cbcSMatt Macy  *	extern void nvlist_prtctl_xxx(nvlist_prtctl_t,
69eda14cbcSMatt Macy  *	    void (*)(nvlist_prtctl_t ctl, void *private, const char *name,
70eda14cbcSMatt Macy  *	    xxxtype value))
71eda14cbcSMatt Macy  *
72eda14cbcSMatt Macy  *	and
73eda14cbcSMatt Macy  *
74eda14cbcSMatt Macy  *	extern void nvlist_prtctl_xxx_array(nvlist_prtctl_t,
75eda14cbcSMatt Macy  *	    void (*)(nvlist_prtctl_t ctl, void *private, const char *name,
76eda14cbcSMatt Macy  *	    xxxtype value, uint_t count))
77eda14cbcSMatt Macy  *
78eda14cbcSMatt Macy  * where xxxtype is the C datatype corresponding to xxx, eg int8_t for "int8"
79eda14cbcSMatt Macy  * and char * for "string".  The function that is appointed to render the
80eda14cbcSMatt Macy  * specified datatype receives as arguments the cookie, the nvlist
81eda14cbcSMatt Macy  * member name, the value of that member (or a pointer for array function),
82eda14cbcSMatt Macy  * and (for array rendering functions) a count of the number of elements.
83eda14cbcSMatt Macy  */
84eda14cbcSMatt Macy 
85eda14cbcSMatt Macy typedef struct nvlist_prtctl *nvlist_prtctl_t;	/* opaque */
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy enum nvlist_indent_mode {
88eda14cbcSMatt Macy 	NVLIST_INDENT_ABS,	/* Absolute indentation */
89eda14cbcSMatt Macy 	NVLIST_INDENT_TABBED	/* Indent with tabstops */
90eda14cbcSMatt Macy };
91eda14cbcSMatt Macy 
923ff01b23SMartin Matuska _LIBNVPAIR_H nvlist_prtctl_t nvlist_prtctl_alloc(void);
933ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_prtctl_free(nvlist_prtctl_t);
943ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_prt(nvlist_t *, nvlist_prtctl_t);
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy /* Output stream */
973ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_prtctl_setdest(nvlist_prtctl_t, FILE *);
983ff01b23SMartin Matuska _LIBNVPAIR_H FILE *nvlist_prtctl_getdest(nvlist_prtctl_t);
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy /* Indentation mode, start indent, indent increment; default tabbed/0/1 */
1013ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_prtctl_setindent(nvlist_prtctl_t,
1023ff01b23SMartin Matuska     enum nvlist_indent_mode, int, int);
1033ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_prtctl_doindent(nvlist_prtctl_t, int);
104eda14cbcSMatt Macy 
105eda14cbcSMatt Macy enum nvlist_prtctl_fmt {
106eda14cbcSMatt Macy 	NVLIST_FMT_MEMBER_NAME,		/* name fmt; default "%s = " */
107eda14cbcSMatt Macy 	NVLIST_FMT_MEMBER_POSTAMBLE,	/* after nvlist member; default "\n" */
108eda14cbcSMatt Macy 	NVLIST_FMT_BTWN_ARRAY		/* between array members; default " " */
109eda14cbcSMatt Macy };
110eda14cbcSMatt Macy 
1113ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_prtctl_setfmt(nvlist_prtctl_t, enum nvlist_prtctl_fmt,
112eda14cbcSMatt Macy     const char *);
1133ff01b23SMartin Matuska _LIBNVPAIR_H void nvlist_prtctl_dofmt(nvlist_prtctl_t, enum nvlist_prtctl_fmt,
1143ff01b23SMartin Matuska     ...);
115eda14cbcSMatt Macy 
116eda14cbcSMatt Macy /*
117eda14cbcSMatt Macy  * Function prototypes for interfaces that appoint a new rendering function
118eda14cbcSMatt Macy  * for single-valued nvlist members.
119eda14cbcSMatt Macy  *
120eda14cbcSMatt Macy  * A replacement function receives arguments as follows:
121eda14cbcSMatt Macy  *
122eda14cbcSMatt Macy  *	nvlist_prtctl_t	Print control structure; do not change preferences
123eda14cbcSMatt Macy  *			for this object from a print callback function.
124eda14cbcSMatt Macy  *
125eda14cbcSMatt Macy  *	void *		The function-private cookie argument registered
126eda14cbcSMatt Macy  *			when the replacement function was appointed.
127eda14cbcSMatt Macy  *
128eda14cbcSMatt Macy  *	nvlist_t *	The full nvlist that is being processed.  The
129eda14cbcSMatt Macy  *			rendering function is called to render a single
130eda14cbcSMatt Macy  *			member (name and value passed as below) but it may
131eda14cbcSMatt Macy  *			want to reference or incorporate other aspects of
132eda14cbcSMatt Macy  *			the full nvlist.
133eda14cbcSMatt Macy  *
134eda14cbcSMatt Macy  *	const char *	Member name to render
135eda14cbcSMatt Macy  *
136eda14cbcSMatt Macy  *	valtype		Value of the member to render
137eda14cbcSMatt Macy  *
138eda14cbcSMatt Macy  * The function must return non-zero if it has rendered output for this
139eda14cbcSMatt Macy  * member, or 0 if it wants to default to standard rendering for this
140eda14cbcSMatt Macy  * one member.
141eda14cbcSMatt Macy  */
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy #define	NVLIST_PRINTCTL_SVDECL(funcname, valtype) \
1443ff01b23SMartin Matuska     _LIBNVPAIR_H void funcname(nvlist_prtctl_t, \
145eda14cbcSMatt Macy     int (*)(nvlist_prtctl_t, void *, nvlist_t *, const char *, valtype), \
146eda14cbcSMatt Macy     void *)
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_boolean, int);
149eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_boolean_value, boolean_t);
150eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_byte, uchar_t);
151eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int8, int8_t);
152eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint8, uint8_t);
153eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int16, int16_t);
154eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint16, uint16_t);
155eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int32, int32_t);
156eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint32, uint32_t);
157eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int64, int64_t);
158eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint64, uint64_t);
159eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_double, double);
160*2a58b312SMartin Matuska NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_string, const char *);
161eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_hrtime, hrtime_t);
162eda14cbcSMatt Macy NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_nvlist, nvlist_t *);
163eda14cbcSMatt Macy 
164eda14cbcSMatt Macy #undef	NVLIST_PRINTCTL_SVDECL	/* was just for "clarity" above */
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy /*
167eda14cbcSMatt Macy  * Function prototypes for interfaces that appoint a new rendering function
168eda14cbcSMatt Macy  * for array-valued nvlist members.
169eda14cbcSMatt Macy  *
170eda14cbcSMatt Macy  * One additional argument is taken: uint_t for the number of array elements
171eda14cbcSMatt Macy  *
172eda14cbcSMatt Macy  * Return values as above.
173eda14cbcSMatt Macy  */
174eda14cbcSMatt Macy #define	NVLIST_PRINTCTL_AVDECL(funcname, vtype) \
1753ff01b23SMartin Matuska     _LIBNVPAIR_H void funcname(nvlist_prtctl_t, \
176eda14cbcSMatt Macy     int (*)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, uint_t), \
177eda14cbcSMatt Macy     void *)
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_boolean_array, boolean_t *);
180eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_byte_array, uchar_t *);
181eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int8_array, int8_t *);
182eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint8_array, uint8_t *);
183eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int16_array, int16_t *);
184eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint16_array, uint16_t *);
185eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int32_array, int32_t *);
186eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint32_array, uint32_t *);
187eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int64_array, int64_t *);
188eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint64_array, uint64_t *);
189*2a58b312SMartin Matuska NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_string_array, const char **);
190eda14cbcSMatt Macy NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_nvlist_array, nvlist_t **);
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy #undef	NVLIST_PRINTCTL_AVDECL	/* was just for "clarity" above */
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy #ifdef	__cplusplus
195eda14cbcSMatt Macy }
196eda14cbcSMatt Macy #endif
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy #endif	/* _LIBNVPAIR_H */
199