xref: /titanic_44/usr/src/lib/libnvpair/libnvpair.c (revision f6e214c7418f43af38bd8c3a557e3d0a1d311cfa)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5602ca9eaScth  * Common Development and Distribution License (the "License").
6602ca9eaScth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f6e214c7SGavin Maltby  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <unistd.h>
267c478bd9Sstevel@tonic-gate #include <strings.h>
2788ecc943SGeorge Wilson #include <libintl.h>
28602ca9eaScth #include <sys/types.h>
29602ca9eaScth #include <sys/inttypes.h>
30*f6e214c7SGavin Maltby #include <stdarg.h>
31*f6e214c7SGavin Maltby #include <note.h>
327c478bd9Sstevel@tonic-gate #include "libnvpair.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * libnvpair - A tools library for manipulating <name, value> pairs.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *	This library provides routines packing an unpacking nv pairs
387c478bd9Sstevel@tonic-gate  *	for transporting data across process boundaries, transporting
397c478bd9Sstevel@tonic-gate  *	between kernel and userland, and possibly saving onto disk files.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
42*f6e214c7SGavin Maltby /*
43*f6e214c7SGavin Maltby  * Print control structure.
44*f6e214c7SGavin Maltby  */
45*f6e214c7SGavin Maltby 
46*f6e214c7SGavin Maltby #define	DEFINEOP(opname, vtype) \
47*f6e214c7SGavin Maltby 	struct { \
48*f6e214c7SGavin Maltby 		int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
49*f6e214c7SGavin Maltby 		    const char *, vtype); \
50*f6e214c7SGavin Maltby 		void *arg; \
51*f6e214c7SGavin Maltby 	} opname
52*f6e214c7SGavin Maltby 
53*f6e214c7SGavin Maltby #define	DEFINEARROP(opname, vtype) \
54*f6e214c7SGavin Maltby 	struct { \
55*f6e214c7SGavin Maltby 		int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
56*f6e214c7SGavin Maltby 		    const char *, vtype, uint_t); \
57*f6e214c7SGavin Maltby 		void *arg; \
58*f6e214c7SGavin Maltby 	} opname
59*f6e214c7SGavin Maltby 
60*f6e214c7SGavin Maltby struct nvlist_printops {
61*f6e214c7SGavin Maltby 	DEFINEOP(print_boolean, int);
62*f6e214c7SGavin Maltby 	DEFINEOP(print_boolean_value, boolean_t);
63*f6e214c7SGavin Maltby 	DEFINEOP(print_byte, uchar_t);
64*f6e214c7SGavin Maltby 	DEFINEOP(print_int8, int8_t);
65*f6e214c7SGavin Maltby 	DEFINEOP(print_uint8, uint8_t);
66*f6e214c7SGavin Maltby 	DEFINEOP(print_int16, int16_t);
67*f6e214c7SGavin Maltby 	DEFINEOP(print_uint16, uint16_t);
68*f6e214c7SGavin Maltby 	DEFINEOP(print_int32, int32_t);
69*f6e214c7SGavin Maltby 	DEFINEOP(print_uint32, uint32_t);
70*f6e214c7SGavin Maltby 	DEFINEOP(print_int64, int64_t);
71*f6e214c7SGavin Maltby 	DEFINEOP(print_uint64, uint64_t);
72*f6e214c7SGavin Maltby 	DEFINEOP(print_double, double);
73*f6e214c7SGavin Maltby 	DEFINEOP(print_string, char *);
74*f6e214c7SGavin Maltby 	DEFINEOP(print_hrtime, hrtime_t);
75*f6e214c7SGavin Maltby 	DEFINEOP(print_nvlist, nvlist_t *);
76*f6e214c7SGavin Maltby 	DEFINEARROP(print_boolean_array, boolean_t *);
77*f6e214c7SGavin Maltby 	DEFINEARROP(print_byte_array, uchar_t *);
78*f6e214c7SGavin Maltby 	DEFINEARROP(print_int8_array, int8_t *);
79*f6e214c7SGavin Maltby 	DEFINEARROP(print_uint8_array, uint8_t *);
80*f6e214c7SGavin Maltby 	DEFINEARROP(print_int16_array, int16_t *);
81*f6e214c7SGavin Maltby 	DEFINEARROP(print_uint16_array, uint16_t *);
82*f6e214c7SGavin Maltby 	DEFINEARROP(print_int32_array, int32_t *);
83*f6e214c7SGavin Maltby 	DEFINEARROP(print_uint32_array, uint32_t *);
84*f6e214c7SGavin Maltby 	DEFINEARROP(print_int64_array, int64_t *);
85*f6e214c7SGavin Maltby 	DEFINEARROP(print_uint64_array, uint64_t *);
86*f6e214c7SGavin Maltby 	DEFINEARROP(print_string_array, char **);
87*f6e214c7SGavin Maltby 	DEFINEARROP(print_nvlist_array, nvlist_t **);
88*f6e214c7SGavin Maltby };
89*f6e214c7SGavin Maltby 
90*f6e214c7SGavin Maltby struct nvlist_prtctl {
91*f6e214c7SGavin Maltby 	FILE *nvprt_fp;			/* output destination */
92*f6e214c7SGavin Maltby 	enum nvlist_indent_mode nvprt_indent_mode; /* see above */
93*f6e214c7SGavin Maltby 	int nvprt_indent;		/* absolute indent, or tab depth */
94*f6e214c7SGavin Maltby 	int nvprt_indentinc;		/* indent or tab increment */
95*f6e214c7SGavin Maltby 	const char *nvprt_nmfmt;	/* member name format, max one %s */
96*f6e214c7SGavin Maltby 	const char *nvprt_eomfmt;	/* after member format, e.g. "\n" */
97*f6e214c7SGavin Maltby 	const char *nvprt_btwnarrfmt;	/* between array members */
98*f6e214c7SGavin Maltby 	int nvprt_btwnarrfmt_nl;	/* nvprt_eoamfmt includes newline? */
99*f6e214c7SGavin Maltby 	struct nvlist_printops *nvprt_dfltops;
100*f6e214c7SGavin Maltby 	struct nvlist_printops *nvprt_custops;
101*f6e214c7SGavin Maltby };
102*f6e214c7SGavin Maltby 
103*f6e214c7SGavin Maltby #define	DFLTPRTOP(pctl, type) \
104*f6e214c7SGavin Maltby 	((pctl)->nvprt_dfltops->print_##type.op)
105*f6e214c7SGavin Maltby 
106*f6e214c7SGavin Maltby #define	DFLTPRTOPARG(pctl, type) \
107*f6e214c7SGavin Maltby 	((pctl)->nvprt_dfltops->print_##type.arg)
108*f6e214c7SGavin Maltby 
109*f6e214c7SGavin Maltby #define	CUSTPRTOP(pctl, type) \
110*f6e214c7SGavin Maltby 	((pctl)->nvprt_custops->print_##type.op)
111*f6e214c7SGavin Maltby 
112*f6e214c7SGavin Maltby #define	CUSTPRTOPARG(pctl, type) \
113*f6e214c7SGavin Maltby 	((pctl)->nvprt_custops->print_##type.arg)
114*f6e214c7SGavin Maltby 
115*f6e214c7SGavin Maltby #define	RENDER(pctl, type, nvl, name, val) \
116*f6e214c7SGavin Maltby 	{ \
117*f6e214c7SGavin Maltby 		int done = 0; \
118*f6e214c7SGavin Maltby 		if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
119*f6e214c7SGavin Maltby 			done = CUSTPRTOP(pctl, type)(pctl, \
120*f6e214c7SGavin Maltby 			    CUSTPRTOPARG(pctl, type), nvl, name, val); \
121*f6e214c7SGavin Maltby 		} \
122*f6e214c7SGavin Maltby 		if (!done) { \
123*f6e214c7SGavin Maltby 			(void) DFLTPRTOP(pctl, type)(pctl, \
124*f6e214c7SGavin Maltby 			    DFLTPRTOPARG(pctl, type), nvl, name, val); \
125*f6e214c7SGavin Maltby 		} \
126*f6e214c7SGavin Maltby 		(void) fprintf(pctl->nvprt_fp, pctl->nvprt_eomfmt); \
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
129*f6e214c7SGavin Maltby #define	ARENDER(pctl, type, nvl, name, arrp, count) \
130*f6e214c7SGavin Maltby 	{ \
131*f6e214c7SGavin Maltby 		int done = 0; \
132*f6e214c7SGavin Maltby 		if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
133*f6e214c7SGavin Maltby 			done = CUSTPRTOP(pctl, type)(pctl, \
134*f6e214c7SGavin Maltby 			    CUSTPRTOPARG(pctl, type), nvl, name, arrp, count); \
135*f6e214c7SGavin Maltby 		} \
136*f6e214c7SGavin Maltby 		if (!done) { \
137*f6e214c7SGavin Maltby 			(void) DFLTPRTOP(pctl, type)(pctl, \
138*f6e214c7SGavin Maltby 			    DFLTPRTOPARG(pctl, type), nvl, name, arrp, count); \
139*f6e214c7SGavin Maltby 		} \
140*f6e214c7SGavin Maltby 		(void) fprintf(pctl->nvprt_fp, pctl->nvprt_eomfmt); \
141*f6e214c7SGavin Maltby 	}
142*f6e214c7SGavin Maltby 
143*f6e214c7SGavin Maltby static void nvlist_print_with_indent(nvlist_t *, nvlist_prtctl_t);
144*f6e214c7SGavin Maltby 
145*f6e214c7SGavin Maltby /*
146*f6e214c7SGavin Maltby  * ======================================================================
147*f6e214c7SGavin Maltby  * |									|
148*f6e214c7SGavin Maltby  * | Indentation							|
149*f6e214c7SGavin Maltby  * |									|
150*f6e214c7SGavin Maltby  * ======================================================================
151*f6e214c7SGavin Maltby  */
152*f6e214c7SGavin Maltby 
153*f6e214c7SGavin Maltby static void
154*f6e214c7SGavin Maltby indent(nvlist_prtctl_t pctl, int onemore)
155*f6e214c7SGavin Maltby {
156*f6e214c7SGavin Maltby 	int depth;
157*f6e214c7SGavin Maltby 
158*f6e214c7SGavin Maltby 	switch (pctl->nvprt_indent_mode) {
159*f6e214c7SGavin Maltby 	case NVLIST_INDENT_ABS:
160*f6e214c7SGavin Maltby 		(void) fprintf(pctl->nvprt_fp, "%*s",
161*f6e214c7SGavin Maltby 		    pctl->nvprt_indent + onemore * pctl->nvprt_indentinc, "");
162*f6e214c7SGavin Maltby 		break;
163*f6e214c7SGavin Maltby 
164*f6e214c7SGavin Maltby 	case NVLIST_INDENT_TABBED:
165*f6e214c7SGavin Maltby 		depth = pctl->nvprt_indent + onemore;
166*f6e214c7SGavin Maltby 		while (depth-- > 0)
167*f6e214c7SGavin Maltby 			(void) fprintf(pctl->nvprt_fp, "\t");
168*f6e214c7SGavin Maltby 	}
169*f6e214c7SGavin Maltby }
170*f6e214c7SGavin Maltby 
171*f6e214c7SGavin Maltby /*
172*f6e214c7SGavin Maltby  * ======================================================================
173*f6e214c7SGavin Maltby  * |									|
174*f6e214c7SGavin Maltby  * | Default nvlist member rendering functions.				|
175*f6e214c7SGavin Maltby  * |									|
176*f6e214c7SGavin Maltby  * ======================================================================
177*f6e214c7SGavin Maltby  */
178*f6e214c7SGavin Maltby 
179*f6e214c7SGavin Maltby /*
180*f6e214c7SGavin Maltby  * Generate functions to print single-valued nvlist members.
181*f6e214c7SGavin Maltby  *
182*f6e214c7SGavin Maltby  * type_and_variant - suffix to form function name
183*f6e214c7SGavin Maltby  * vtype - C type for the member value
184*f6e214c7SGavin Maltby  * ptype - C type to cast value to for printing
185*f6e214c7SGavin Maltby  * vfmt - format string for pair value, e.g "%d" or "0x%llx"
186*f6e214c7SGavin Maltby  */
187*f6e214c7SGavin Maltby 
188*f6e214c7SGavin Maltby #define	NVLIST_PRTFUNC(type_and_variant, vtype, ptype, vfmt) \
189*f6e214c7SGavin Maltby static int \
190*f6e214c7SGavin Maltby nvprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
191*f6e214c7SGavin Maltby     nvlist_t *nvl, const char *name, vtype value) \
192*f6e214c7SGavin Maltby { \
193*f6e214c7SGavin Maltby 	FILE *fp = pctl->nvprt_fp; \
194*f6e214c7SGavin Maltby 	NOTE(ARGUNUSED(private)) \
195*f6e214c7SGavin Maltby 	NOTE(ARGUNUSED(nvl)) \
196*f6e214c7SGavin Maltby 	indent(pctl, 1); \
197*f6e214c7SGavin Maltby 	(void) fprintf(fp, pctl->nvprt_nmfmt, name); \
198*f6e214c7SGavin Maltby 	(void) fprintf(fp, vfmt, (ptype)value); \
199*f6e214c7SGavin Maltby 	return (1); \
200*f6e214c7SGavin Maltby }
201*f6e214c7SGavin Maltby 
202*f6e214c7SGavin Maltby NVLIST_PRTFUNC(boolean, int, int, "%d")
203*f6e214c7SGavin Maltby NVLIST_PRTFUNC(boolean_value, boolean_t, int, "%d")
204*f6e214c7SGavin Maltby NVLIST_PRTFUNC(byte, uchar_t, uchar_t, "0x%2.2x")
205*f6e214c7SGavin Maltby NVLIST_PRTFUNC(int8, int8_t, int, "%d")
206*f6e214c7SGavin Maltby NVLIST_PRTFUNC(uint8, uint8_t, uint8_t, "0x%x")
207*f6e214c7SGavin Maltby NVLIST_PRTFUNC(int16, int16_t, int16_t, "%d")
208*f6e214c7SGavin Maltby NVLIST_PRTFUNC(uint16, uint16_t, uint16_t, "0x%x")
209*f6e214c7SGavin Maltby NVLIST_PRTFUNC(int32, int32_t, int32_t, "%d")
210*f6e214c7SGavin Maltby NVLIST_PRTFUNC(uint32, uint32_t, uint32_t, "0x%x")
211*f6e214c7SGavin Maltby NVLIST_PRTFUNC(int64, int64_t, longlong_t, "%lld")
212*f6e214c7SGavin Maltby NVLIST_PRTFUNC(uint64, uint64_t, u_longlong_t, "0x%llx")
213*f6e214c7SGavin Maltby NVLIST_PRTFUNC(double, double, double, "0x%llf")
214*f6e214c7SGavin Maltby NVLIST_PRTFUNC(string, char *, char *, "%s")
215*f6e214c7SGavin Maltby NVLIST_PRTFUNC(hrtime, hrtime_t, hrtime_t, "0x%llx")
216*f6e214c7SGavin Maltby 
217*f6e214c7SGavin Maltby /*
218*f6e214c7SGavin Maltby  * Generate functions to print array-valued nvlist members.
219*f6e214c7SGavin Maltby  */
220*f6e214c7SGavin Maltby 
221*f6e214c7SGavin Maltby #define	NVLIST_ARRPRTFUNC(type_and_variant, vtype, ptype, vfmt) \
222*f6e214c7SGavin Maltby static int \
223*f6e214c7SGavin Maltby nvaprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
224*f6e214c7SGavin Maltby     nvlist_t *nvl, const char *name, vtype *valuep, uint_t count) \
225*f6e214c7SGavin Maltby { \
226*f6e214c7SGavin Maltby 	FILE *fp = pctl->nvprt_fp; \
227*f6e214c7SGavin Maltby 	uint_t i; \
228*f6e214c7SGavin Maltby 	NOTE(ARGUNUSED(private)) \
229*f6e214c7SGavin Maltby 	NOTE(ARGUNUSED(nvl)) \
230*f6e214c7SGavin Maltby 	for (i = 0; i < count; i++) { \
231*f6e214c7SGavin Maltby 		if (i == 0 || pctl->nvprt_btwnarrfmt_nl) { \
232*f6e214c7SGavin Maltby 			indent(pctl, 1); \
233*f6e214c7SGavin Maltby 			(void) fprintf(fp, pctl->nvprt_nmfmt, name); \
234*f6e214c7SGavin Maltby 			if (pctl->nvprt_btwnarrfmt_nl) \
235*f6e214c7SGavin Maltby 				(void) fprintf(fp, "[%d]: ", i); \
236*f6e214c7SGavin Maltby 		} \
237*f6e214c7SGavin Maltby 		if (i != 0) \
238*f6e214c7SGavin Maltby 			(void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
239*f6e214c7SGavin Maltby 		(void) fprintf(fp, vfmt, (ptype)valuep[i]); \
240*f6e214c7SGavin Maltby 	} \
241*f6e214c7SGavin Maltby 	return (1); \
242*f6e214c7SGavin Maltby }
243*f6e214c7SGavin Maltby 
244*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(boolean_array, boolean_t, boolean_t, "%d")
245*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(byte_array, uchar_t, uchar_t, "0x%2.2x")
246*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(int8_array, int8_t, int8_t, "%d")
247*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(uint8_array, uint8_t, uint8_t, "0x%x")
248*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(int16_array, int16_t, int16_t, "%d")
249*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(uint16_array, uint16_t, uint16_t, "0x%x")
250*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(int32_array, int32_t, int32_t, "%d")
251*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(uint32_array, uint32_t, uint32_t, "0x%x")
252*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(int64_array, int64_t, longlong_t, "%lld")
253*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(uint64_array, uint64_t, u_longlong_t, "0x%llx")
254*f6e214c7SGavin Maltby NVLIST_ARRPRTFUNC(string_array, char *, char *, "%s")
255*f6e214c7SGavin Maltby 
256*f6e214c7SGavin Maltby /*ARGSUSED*/
257*f6e214c7SGavin Maltby static int
258*f6e214c7SGavin Maltby nvprint_nvlist(nvlist_prtctl_t pctl, void *private,
259*f6e214c7SGavin Maltby     nvlist_t *nvl, const char *name, nvlist_t *value)
260*f6e214c7SGavin Maltby {
261*f6e214c7SGavin Maltby 	FILE *fp = pctl->nvprt_fp;
262*f6e214c7SGavin Maltby 
263*f6e214c7SGavin Maltby 	indent(pctl, 1);
264*f6e214c7SGavin Maltby 	(void) fprintf(fp, "%s = (embedded nvlist)\n", name);
265*f6e214c7SGavin Maltby 
266*f6e214c7SGavin Maltby 	pctl->nvprt_indent += pctl->nvprt_indentinc;
267*f6e214c7SGavin Maltby 	nvlist_print_with_indent(value, pctl);
268*f6e214c7SGavin Maltby 	pctl->nvprt_indent -= pctl->nvprt_indentinc;
269*f6e214c7SGavin Maltby 
270*f6e214c7SGavin Maltby 	indent(pctl, 1);
271*f6e214c7SGavin Maltby 	(void) fprintf(fp, "(end %s)\n", name);
272*f6e214c7SGavin Maltby 
273*f6e214c7SGavin Maltby 	return (1);
274*f6e214c7SGavin Maltby }
275*f6e214c7SGavin Maltby 
276*f6e214c7SGavin Maltby /*ARGSUSED*/
277*f6e214c7SGavin Maltby static int
278*f6e214c7SGavin Maltby nvaprint_nvlist_array(nvlist_prtctl_t pctl, void *private,
279*f6e214c7SGavin Maltby     nvlist_t *nvl, const char *name, nvlist_t **valuep, uint_t count)
280*f6e214c7SGavin Maltby {
281*f6e214c7SGavin Maltby 	FILE *fp = pctl->nvprt_fp;
282*f6e214c7SGavin Maltby 	uint_t i;
283*f6e214c7SGavin Maltby 
284*f6e214c7SGavin Maltby 	indent(pctl, 1);
285*f6e214c7SGavin Maltby 	(void) fprintf(fp, "%s = (array of embedded nvlists)\n", name);
286*f6e214c7SGavin Maltby 
287*f6e214c7SGavin Maltby 	for (i = 0; i < count; i++) {
288*f6e214c7SGavin Maltby 		indent(pctl, 1);
289*f6e214c7SGavin Maltby 		(void) fprintf(fp, "(start %s[%d])\n", name, i);
290*f6e214c7SGavin Maltby 
291*f6e214c7SGavin Maltby 		pctl->nvprt_indent += pctl->nvprt_indentinc;
292*f6e214c7SGavin Maltby 		nvlist_print_with_indent(valuep[i], pctl);
293*f6e214c7SGavin Maltby 		pctl->nvprt_indent -= pctl->nvprt_indentinc;
294*f6e214c7SGavin Maltby 
295*f6e214c7SGavin Maltby 		indent(pctl, 1);
296*f6e214c7SGavin Maltby 		(void) fprintf(fp, "(end %s[%d])\n", name, i);
297*f6e214c7SGavin Maltby 	}
298*f6e214c7SGavin Maltby 
299*f6e214c7SGavin Maltby 	return (1);
300*f6e214c7SGavin Maltby }
301*f6e214c7SGavin Maltby 
302*f6e214c7SGavin Maltby /*
303*f6e214c7SGavin Maltby  * ======================================================================
304*f6e214c7SGavin Maltby  * |									|
305*f6e214c7SGavin Maltby  * | Interfaces that allow control over formatting.			|
306*f6e214c7SGavin Maltby  * |									|
307*f6e214c7SGavin Maltby  * ======================================================================
308*f6e214c7SGavin Maltby  */
309*f6e214c7SGavin Maltby 
310*f6e214c7SGavin Maltby void
311*f6e214c7SGavin Maltby nvlist_prtctl_setdest(nvlist_prtctl_t pctl, FILE *fp)
312*f6e214c7SGavin Maltby {
313*f6e214c7SGavin Maltby 	pctl->nvprt_fp = fp;
314*f6e214c7SGavin Maltby }
315*f6e214c7SGavin Maltby 
316*f6e214c7SGavin Maltby FILE *
317*f6e214c7SGavin Maltby nvlist_prtctl_getdest(nvlist_prtctl_t pctl)
318*f6e214c7SGavin Maltby {
319*f6e214c7SGavin Maltby 	return (pctl->nvprt_fp);
320*f6e214c7SGavin Maltby }
321*f6e214c7SGavin Maltby 
322*f6e214c7SGavin Maltby 
323*f6e214c7SGavin Maltby void
324*f6e214c7SGavin Maltby nvlist_prtctl_setindent(nvlist_prtctl_t pctl, enum nvlist_indent_mode mode,
325*f6e214c7SGavin Maltby     int start, int inc)
326*f6e214c7SGavin Maltby {
327*f6e214c7SGavin Maltby 	if (mode < NVLIST_INDENT_ABS || mode > NVLIST_INDENT_TABBED)
328*f6e214c7SGavin Maltby 		mode = NVLIST_INDENT_TABBED;
329*f6e214c7SGavin Maltby 
330*f6e214c7SGavin Maltby 	if (start < 0)
331*f6e214c7SGavin Maltby 		start = 0;
332*f6e214c7SGavin Maltby 
333*f6e214c7SGavin Maltby 	if (inc < 0)
334*f6e214c7SGavin Maltby 		inc = 1;
335*f6e214c7SGavin Maltby 
336*f6e214c7SGavin Maltby 	pctl->nvprt_indent_mode = mode;
337*f6e214c7SGavin Maltby 	pctl->nvprt_indent = start;
338*f6e214c7SGavin Maltby 	pctl->nvprt_indentinc = inc;
339*f6e214c7SGavin Maltby }
340*f6e214c7SGavin Maltby 
341*f6e214c7SGavin Maltby void
342*f6e214c7SGavin Maltby nvlist_prtctl_doindent(nvlist_prtctl_t pctl, int onemore)
343*f6e214c7SGavin Maltby {
344*f6e214c7SGavin Maltby 	indent(pctl, onemore);
345*f6e214c7SGavin Maltby }
346*f6e214c7SGavin Maltby 
347*f6e214c7SGavin Maltby 
348*f6e214c7SGavin Maltby void
349*f6e214c7SGavin Maltby nvlist_prtctl_setfmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which,
350*f6e214c7SGavin Maltby     const char *fmt)
351*f6e214c7SGavin Maltby {
352*f6e214c7SGavin Maltby 	switch (which) {
353*f6e214c7SGavin Maltby 	case NVLIST_FMT_MEMBER_NAME:
354*f6e214c7SGavin Maltby 		if (fmt == NULL)
355*f6e214c7SGavin Maltby 			fmt = "%s = ";
356*f6e214c7SGavin Maltby 		pctl->nvprt_nmfmt = fmt;
357*f6e214c7SGavin Maltby 		break;
358*f6e214c7SGavin Maltby 
359*f6e214c7SGavin Maltby 	case NVLIST_FMT_MEMBER_POSTAMBLE:
360*f6e214c7SGavin Maltby 		if (fmt == NULL)
361*f6e214c7SGavin Maltby 			fmt = "\n";
362*f6e214c7SGavin Maltby 		pctl->nvprt_eomfmt = fmt;
363*f6e214c7SGavin Maltby 		break;
364*f6e214c7SGavin Maltby 
365*f6e214c7SGavin Maltby 	case NVLIST_FMT_BTWN_ARRAY:
366*f6e214c7SGavin Maltby 		if (fmt == NULL) {
367*f6e214c7SGavin Maltby 			pctl->nvprt_btwnarrfmt = " ";
368*f6e214c7SGavin Maltby 			pctl->nvprt_btwnarrfmt_nl = 0;
369*f6e214c7SGavin Maltby 		} else {
370*f6e214c7SGavin Maltby 			pctl->nvprt_btwnarrfmt = fmt;
371*f6e214c7SGavin Maltby 			pctl->nvprt_btwnarrfmt_nl = (strstr(fmt, "\n") != NULL);
372*f6e214c7SGavin Maltby 		}
373*f6e214c7SGavin Maltby 		break;
374*f6e214c7SGavin Maltby 
375*f6e214c7SGavin Maltby 	default:
376*f6e214c7SGavin Maltby 		break;
377*f6e214c7SGavin Maltby 	}
378*f6e214c7SGavin Maltby }
379*f6e214c7SGavin Maltby 
380*f6e214c7SGavin Maltby 
381*f6e214c7SGavin Maltby void
382*f6e214c7SGavin Maltby nvlist_prtctl_dofmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which, ...)
383*f6e214c7SGavin Maltby {
384*f6e214c7SGavin Maltby 	FILE *fp = pctl->nvprt_fp;
385*f6e214c7SGavin Maltby 	va_list ap;
386*f6e214c7SGavin Maltby 	char *name;
387*f6e214c7SGavin Maltby 
388*f6e214c7SGavin Maltby 	va_start(ap, which);
389*f6e214c7SGavin Maltby 
390*f6e214c7SGavin Maltby 	switch (which) {
391*f6e214c7SGavin Maltby 	case NVLIST_FMT_MEMBER_NAME:
392*f6e214c7SGavin Maltby 		name = va_arg(ap, char *);
393*f6e214c7SGavin Maltby 		(void) fprintf(fp, pctl->nvprt_nmfmt, name);
394*f6e214c7SGavin Maltby 		break;
395*f6e214c7SGavin Maltby 
396*f6e214c7SGavin Maltby 	case NVLIST_FMT_MEMBER_POSTAMBLE:
397*f6e214c7SGavin Maltby 		(void) fprintf(fp, pctl->nvprt_eomfmt);
398*f6e214c7SGavin Maltby 		break;
399*f6e214c7SGavin Maltby 
400*f6e214c7SGavin Maltby 	case NVLIST_FMT_BTWN_ARRAY:
401*f6e214c7SGavin Maltby 		(void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
402*f6e214c7SGavin Maltby 		break;
403*f6e214c7SGavin Maltby 
404*f6e214c7SGavin Maltby 	default:
405*f6e214c7SGavin Maltby 		break;
406*f6e214c7SGavin Maltby 	}
407*f6e214c7SGavin Maltby 
408*f6e214c7SGavin Maltby 	va_end(ap);
409*f6e214c7SGavin Maltby }
410*f6e214c7SGavin Maltby 
411*f6e214c7SGavin Maltby /*
412*f6e214c7SGavin Maltby  * ======================================================================
413*f6e214c7SGavin Maltby  * |									|
414*f6e214c7SGavin Maltby  * | Interfaces to allow appointment of replacement rendering functions.|
415*f6e214c7SGavin Maltby  * |									|
416*f6e214c7SGavin Maltby  * ======================================================================
417*f6e214c7SGavin Maltby  */
418*f6e214c7SGavin Maltby 
419*f6e214c7SGavin Maltby #define	NVLIST_PRINTCTL_REPLACE(type, vtype) \
420*f6e214c7SGavin Maltby void \
421*f6e214c7SGavin Maltby nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
422*f6e214c7SGavin Maltby     int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype), \
423*f6e214c7SGavin Maltby     void *private) \
424*f6e214c7SGavin Maltby { \
425*f6e214c7SGavin Maltby 	CUSTPRTOP(pctl, type) = func; \
426*f6e214c7SGavin Maltby 	CUSTPRTOPARG(pctl, type) = private; \
427*f6e214c7SGavin Maltby }
428*f6e214c7SGavin Maltby 
429*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(boolean, int)
430*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(boolean_value, boolean_t)
431*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(byte, uchar_t)
432*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(int8, int8_t)
433*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(uint8, uint8_t)
434*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(int16, int16_t)
435*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(uint16, uint16_t)
436*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(int32, int32_t)
437*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(uint32, uint32_t)
438*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(int64, int64_t)
439*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(uint64, uint64_t)
440*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(double, double)
441*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(string, char *)
442*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(hrtime, hrtime_t)
443*f6e214c7SGavin Maltby NVLIST_PRINTCTL_REPLACE(nvlist, nvlist_t *)
444*f6e214c7SGavin Maltby 
445*f6e214c7SGavin Maltby #define	NVLIST_PRINTCTL_AREPLACE(type, vtype) \
446*f6e214c7SGavin Maltby void \
447*f6e214c7SGavin Maltby nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
448*f6e214c7SGavin Maltby     int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, \
449*f6e214c7SGavin Maltby     uint_t), void *private) \
450*f6e214c7SGavin Maltby { \
451*f6e214c7SGavin Maltby 	CUSTPRTOP(pctl, type) = func; \
452*f6e214c7SGavin Maltby 	CUSTPRTOPARG(pctl, type) = private; \
453*f6e214c7SGavin Maltby }
454*f6e214c7SGavin Maltby 
455*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(boolean_array, boolean_t *)
456*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(byte_array, uchar_t *)
457*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(int8_array, int8_t *)
458*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(uint8_array, uint8_t *)
459*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(int16_array, int16_t *)
460*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(uint16_array, uint16_t *)
461*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(int32_array, int32_t *)
462*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(uint32_array, uint32_t *)
463*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(int64_array, int64_t *)
464*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(uint64_array, uint64_t *)
465*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(string_array, char **)
466*f6e214c7SGavin Maltby NVLIST_PRINTCTL_AREPLACE(nvlist_array, nvlist_t **)
467*f6e214c7SGavin Maltby 
468*f6e214c7SGavin Maltby /*
469*f6e214c7SGavin Maltby  * ======================================================================
470*f6e214c7SGavin Maltby  * |									|
471*f6e214c7SGavin Maltby  * | Interfaces to manage nvlist_prtctl_t cookies.			|
472*f6e214c7SGavin Maltby  * |									|
473*f6e214c7SGavin Maltby  * ======================================================================
474*f6e214c7SGavin Maltby  */
475*f6e214c7SGavin Maltby 
476*f6e214c7SGavin Maltby 
477*f6e214c7SGavin Maltby static const struct nvlist_printops defprtops = {
478*f6e214c7SGavin Maltby 	{ nvprint_boolean, NULL },
479*f6e214c7SGavin Maltby 	{ nvprint_boolean_value, NULL },
480*f6e214c7SGavin Maltby 	{ nvprint_byte, NULL },
481*f6e214c7SGavin Maltby 	{ nvprint_int8, NULL },
482*f6e214c7SGavin Maltby 	{ nvprint_uint8, NULL },
483*f6e214c7SGavin Maltby 	{ nvprint_int16, NULL },
484*f6e214c7SGavin Maltby 	{ nvprint_uint16, NULL },
485*f6e214c7SGavin Maltby 	{ nvprint_int32, NULL },
486*f6e214c7SGavin Maltby 	{ nvprint_uint32, NULL },
487*f6e214c7SGavin Maltby 	{ nvprint_int64, NULL },
488*f6e214c7SGavin Maltby 	{ nvprint_uint64, NULL },
489*f6e214c7SGavin Maltby 	{ nvprint_double, NULL },
490*f6e214c7SGavin Maltby 	{ nvprint_string, NULL },
491*f6e214c7SGavin Maltby 	{ nvprint_hrtime, NULL },
492*f6e214c7SGavin Maltby 	{ nvprint_nvlist, NULL },
493*f6e214c7SGavin Maltby 	{ nvaprint_boolean_array, NULL },
494*f6e214c7SGavin Maltby 	{ nvaprint_byte_array, NULL },
495*f6e214c7SGavin Maltby 	{ nvaprint_int8_array, NULL },
496*f6e214c7SGavin Maltby 	{ nvaprint_uint8_array, NULL },
497*f6e214c7SGavin Maltby 	{ nvaprint_int16_array, NULL },
498*f6e214c7SGavin Maltby 	{ nvaprint_uint16_array, NULL },
499*f6e214c7SGavin Maltby 	{ nvaprint_int32_array, NULL },
500*f6e214c7SGavin Maltby 	{ nvaprint_uint32_array, NULL },
501*f6e214c7SGavin Maltby 	{ nvaprint_int64_array, NULL },
502*f6e214c7SGavin Maltby 	{ nvaprint_uint64_array, NULL },
503*f6e214c7SGavin Maltby 	{ nvaprint_string_array, NULL },
504*f6e214c7SGavin Maltby 	{ nvaprint_nvlist_array, NULL },
505*f6e214c7SGavin Maltby };
506*f6e214c7SGavin Maltby 
507*f6e214c7SGavin Maltby static void
508*f6e214c7SGavin Maltby prtctl_defaults(FILE *fp, struct nvlist_prtctl *pctl,
509*f6e214c7SGavin Maltby     struct nvlist_printops *ops)
510*f6e214c7SGavin Maltby {
511*f6e214c7SGavin Maltby 	pctl->nvprt_fp = fp;
512*f6e214c7SGavin Maltby 	pctl->nvprt_indent_mode = NVLIST_INDENT_TABBED;
513*f6e214c7SGavin Maltby 	pctl->nvprt_indent = 0;
514*f6e214c7SGavin Maltby 	pctl->nvprt_indentinc = 1;
515*f6e214c7SGavin Maltby 	pctl->nvprt_nmfmt = "%s = ";
516*f6e214c7SGavin Maltby 	pctl->nvprt_eomfmt = "\n";
517*f6e214c7SGavin Maltby 	pctl->nvprt_btwnarrfmt = " ";
518*f6e214c7SGavin Maltby 	pctl->nvprt_btwnarrfmt_nl = 0;
519*f6e214c7SGavin Maltby 
520*f6e214c7SGavin Maltby 	pctl->nvprt_dfltops = (struct nvlist_printops *)&defprtops;
521*f6e214c7SGavin Maltby 	pctl->nvprt_custops = ops;
522*f6e214c7SGavin Maltby }
523*f6e214c7SGavin Maltby 
524*f6e214c7SGavin Maltby nvlist_prtctl_t
525*f6e214c7SGavin Maltby nvlist_prtctl_alloc(void)
526*f6e214c7SGavin Maltby {
527*f6e214c7SGavin Maltby 	struct nvlist_prtctl *pctl;
528*f6e214c7SGavin Maltby 	struct nvlist_printops *ops;
529*f6e214c7SGavin Maltby 
530*f6e214c7SGavin Maltby 	if ((pctl = malloc(sizeof (*pctl))) == NULL)
531*f6e214c7SGavin Maltby 		return (NULL);
532*f6e214c7SGavin Maltby 
533*f6e214c7SGavin Maltby 	if ((ops = calloc(1, sizeof (*ops))) == NULL) {
534*f6e214c7SGavin Maltby 		free(pctl);
535*f6e214c7SGavin Maltby 		return (NULL);
536*f6e214c7SGavin Maltby 	}
537*f6e214c7SGavin Maltby 
538*f6e214c7SGavin Maltby 	prtctl_defaults(stdout, pctl, ops);
539*f6e214c7SGavin Maltby 
540*f6e214c7SGavin Maltby 	return (pctl);
541*f6e214c7SGavin Maltby }
542*f6e214c7SGavin Maltby 
543*f6e214c7SGavin Maltby void
544*f6e214c7SGavin Maltby nvlist_prtctl_free(nvlist_prtctl_t pctl)
545*f6e214c7SGavin Maltby {
546*f6e214c7SGavin Maltby 	if (pctl != NULL) {
547*f6e214c7SGavin Maltby 		free(pctl->nvprt_custops);
548*f6e214c7SGavin Maltby 		free(pctl);
549*f6e214c7SGavin Maltby 	}
550*f6e214c7SGavin Maltby }
551*f6e214c7SGavin Maltby 
552*f6e214c7SGavin Maltby /*
553*f6e214c7SGavin Maltby  * ======================================================================
554*f6e214c7SGavin Maltby  * |									|
555*f6e214c7SGavin Maltby  * | Top-level print request interfaces.				|
556*f6e214c7SGavin Maltby  * |									|
557*f6e214c7SGavin Maltby  * ======================================================================
558*f6e214c7SGavin Maltby  */
559*f6e214c7SGavin Maltby 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * nvlist_print - Prints elements in an event buffer
5627c478bd9Sstevel@tonic-gate  */
563*f6e214c7SGavin Maltby static void
564*f6e214c7SGavin Maltby nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl)
5657c478bd9Sstevel@tonic-gate {
566*f6e214c7SGavin Maltby 	FILE *fp = pctl->nvprt_fp;
5677c478bd9Sstevel@tonic-gate 	char *name;
5687c478bd9Sstevel@tonic-gate 	uint_t nelem;
5697c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if (nvl == NULL)
5727c478bd9Sstevel@tonic-gate 		return;
5737c478bd9Sstevel@tonic-gate 
574*f6e214c7SGavin Maltby 	indent(pctl, 0);
5757c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl));
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	nvp = nvlist_next_nvpair(nvl, NULL);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	while (nvp) {
5807c478bd9Sstevel@tonic-gate 		data_type_t type = nvpair_type(nvp);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
5837c478bd9Sstevel@tonic-gate 		nelem = 0;
584*f6e214c7SGavin Maltby 
5857c478bd9Sstevel@tonic-gate 		switch (type) {
5867c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN: {
587*f6e214c7SGavin Maltby 			RENDER(pctl, boolean, nvl, name, 1);
5887c478bd9Sstevel@tonic-gate 			break;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE: {
5917c478bd9Sstevel@tonic-gate 			boolean_t val;
5927c478bd9Sstevel@tonic-gate 			(void) nvpair_value_boolean_value(nvp, &val);
593*f6e214c7SGavin Maltby 			RENDER(pctl, boolean_value, nvl, name, val);
5947c478bd9Sstevel@tonic-gate 			break;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE: {
5977c478bd9Sstevel@tonic-gate 			uchar_t val;
5987c478bd9Sstevel@tonic-gate 			(void) nvpair_value_byte(nvp, &val);
599*f6e214c7SGavin Maltby 			RENDER(pctl, byte, nvl, name, val);
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8: {
6037c478bd9Sstevel@tonic-gate 			int8_t val;
6047c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int8(nvp, &val);
605*f6e214c7SGavin Maltby 			RENDER(pctl, int8, nvl, name, val);
6067c478bd9Sstevel@tonic-gate 			break;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8: {
6097c478bd9Sstevel@tonic-gate 			uint8_t val;
6107c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint8(nvp, &val);
611*f6e214c7SGavin Maltby 			RENDER(pctl, uint8, nvl, name, val);
6127c478bd9Sstevel@tonic-gate 			break;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16: {
6157c478bd9Sstevel@tonic-gate 			int16_t val;
6167c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int16(nvp, &val);
617*f6e214c7SGavin Maltby 			RENDER(pctl, int16, nvl, name, val);
6187c478bd9Sstevel@tonic-gate 			break;
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16: {
6217c478bd9Sstevel@tonic-gate 			uint16_t val;
6227c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint16(nvp, &val);
623*f6e214c7SGavin Maltby 			RENDER(pctl, uint16, nvl, name, val);
6247c478bd9Sstevel@tonic-gate 			break;
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32: {
6277c478bd9Sstevel@tonic-gate 			int32_t val;
6287c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, &val);
629*f6e214c7SGavin Maltby 			RENDER(pctl, int32, nvl, name, val);
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32: {
6337c478bd9Sstevel@tonic-gate 			uint32_t val;
6347c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint32(nvp, &val);
635*f6e214c7SGavin Maltby 			RENDER(pctl, uint32, nvl, name, val);
6367c478bd9Sstevel@tonic-gate 			break;
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64: {
6397c478bd9Sstevel@tonic-gate 			int64_t val;
6407c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int64(nvp, &val);
641*f6e214c7SGavin Maltby 			RENDER(pctl, int64, nvl, name, val);
6427c478bd9Sstevel@tonic-gate 			break;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64: {
6457c478bd9Sstevel@tonic-gate 			uint64_t val;
6467c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint64(nvp, &val);
647*f6e214c7SGavin Maltby 			RENDER(pctl, uint64, nvl, name, val);
6487c478bd9Sstevel@tonic-gate 			break;
6497c478bd9Sstevel@tonic-gate 		}
650825ba0f2Srobj 		case DATA_TYPE_DOUBLE: {
651825ba0f2Srobj 			double val;
652825ba0f2Srobj 			(void) nvpair_value_double(nvp, &val);
653*f6e214c7SGavin Maltby 			RENDER(pctl, double, nvl, name, val);
654825ba0f2Srobj 			break;
655825ba0f2Srobj 		}
6567c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING: {
6577c478bd9Sstevel@tonic-gate 			char *val;
6587c478bd9Sstevel@tonic-gate 			(void) nvpair_value_string(nvp, &val);
659*f6e214c7SGavin Maltby 			RENDER(pctl, string, nvl, name, val);
6607c478bd9Sstevel@tonic-gate 			break;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY: {
6637c478bd9Sstevel@tonic-gate 			boolean_t *val;
6647c478bd9Sstevel@tonic-gate 			(void) nvpair_value_boolean_array(nvp, &val, &nelem);
665*f6e214c7SGavin Maltby 			ARENDER(pctl, boolean_array, nvl, name, val, nelem);
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY: {
6697c478bd9Sstevel@tonic-gate 			uchar_t *val;
6707c478bd9Sstevel@tonic-gate 			(void) nvpair_value_byte_array(nvp, &val, &nelem);
671*f6e214c7SGavin Maltby 			ARENDER(pctl, byte_array, nvl, name, val, nelem);
6727c478bd9Sstevel@tonic-gate 			break;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY: {
6757c478bd9Sstevel@tonic-gate 			int8_t *val;
6767c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int8_array(nvp, &val, &nelem);
677*f6e214c7SGavin Maltby 			ARENDER(pctl, int8_array, nvl, name, val, nelem);
6787c478bd9Sstevel@tonic-gate 			break;
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY: {
6817c478bd9Sstevel@tonic-gate 			uint8_t *val;
6827c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint8_array(nvp, &val, &nelem);
683*f6e214c7SGavin Maltby 			ARENDER(pctl, uint8_array, nvl, name, val, nelem);
6847c478bd9Sstevel@tonic-gate 			break;
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY: {
6877c478bd9Sstevel@tonic-gate 			int16_t *val;
6887c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int16_array(nvp, &val, &nelem);
689*f6e214c7SGavin Maltby 			ARENDER(pctl, int16_array, nvl, name, val, nelem);
6907c478bd9Sstevel@tonic-gate 			break;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY: {
6937c478bd9Sstevel@tonic-gate 			uint16_t *val;
6947c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint16_array(nvp, &val, &nelem);
695*f6e214c7SGavin Maltby 			ARENDER(pctl, uint16_array, nvl, name, val, nelem);
6967c478bd9Sstevel@tonic-gate 			break;
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY: {
6997c478bd9Sstevel@tonic-gate 			int32_t *val;
7007c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32_array(nvp, &val, &nelem);
701*f6e214c7SGavin Maltby 			ARENDER(pctl, int32_array, nvl, name, val, nelem);
7027c478bd9Sstevel@tonic-gate 			break;
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY: {
7057c478bd9Sstevel@tonic-gate 			uint32_t *val;
7067c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint32_array(nvp, &val, &nelem);
707*f6e214c7SGavin Maltby 			ARENDER(pctl, uint32_array, nvl, name, val, nelem);
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY: {
7117c478bd9Sstevel@tonic-gate 			int64_t *val;
7127c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int64_array(nvp, &val, &nelem);
713*f6e214c7SGavin Maltby 			ARENDER(pctl, int64_array, nvl, name, val, nelem);
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY: {
7177c478bd9Sstevel@tonic-gate 			uint64_t *val;
7187c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint64_array(nvp, &val, &nelem);
719*f6e214c7SGavin Maltby 			ARENDER(pctl, uint64_array, nvl, name, val, nelem);
7207c478bd9Sstevel@tonic-gate 			break;
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY: {
7237c478bd9Sstevel@tonic-gate 			char **val;
7247c478bd9Sstevel@tonic-gate 			(void) nvpair_value_string_array(nvp, &val, &nelem);
725*f6e214c7SGavin Maltby 			ARENDER(pctl, string_array, nvl, name, val, nelem);
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		case DATA_TYPE_HRTIME: {
7297c478bd9Sstevel@tonic-gate 			hrtime_t val;
7307c478bd9Sstevel@tonic-gate 			(void) nvpair_value_hrtime(nvp, &val);
731*f6e214c7SGavin Maltby 			RENDER(pctl, hrtime, nvl, name, val);
7327c478bd9Sstevel@tonic-gate 			break;
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST: {
7357c478bd9Sstevel@tonic-gate 			nvlist_t *val;
7367c478bd9Sstevel@tonic-gate 			(void) nvpair_value_nvlist(nvp, &val);
737*f6e214c7SGavin Maltby 			RENDER(pctl, nvlist, nvl, name, val);
7387c478bd9Sstevel@tonic-gate 			break;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY: {
7417c478bd9Sstevel@tonic-gate 			nvlist_t **val;
7427c478bd9Sstevel@tonic-gate 			(void) nvpair_value_nvlist_array(nvp, &val, &nelem);
743*f6e214c7SGavin Maltby 			ARENDER(pctl, nvlist_array, nvl, name, val, nelem);
7447c478bd9Sstevel@tonic-gate 			break;
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 		default:
7477c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, " unknown data type (%d)", type);
7487c478bd9Sstevel@tonic-gate 			break;
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate void
7557c478bd9Sstevel@tonic-gate nvlist_print(FILE *fp, nvlist_t *nvl)
7567c478bd9Sstevel@tonic-gate {
757*f6e214c7SGavin Maltby 	struct nvlist_prtctl pc;
758*f6e214c7SGavin Maltby 
759*f6e214c7SGavin Maltby 	prtctl_defaults(fp, &pc, NULL);
760*f6e214c7SGavin Maltby 	nvlist_print_with_indent(nvl, &pc);
7617c478bd9Sstevel@tonic-gate }
762602ca9eaScth 
763*f6e214c7SGavin Maltby void
764*f6e214c7SGavin Maltby nvlist_prt(nvlist_t *nvl, nvlist_prtctl_t pctl)
765*f6e214c7SGavin Maltby {
766*f6e214c7SGavin Maltby 	nvlist_print_with_indent(nvl, pctl);
767*f6e214c7SGavin Maltby }
76888ecc943SGeorge Wilson 
76988ecc943SGeorge Wilson #define	NVP(elem, type, vtype, ptype, format) { \
77088ecc943SGeorge Wilson 	vtype	value; \
77188ecc943SGeorge Wilson \
77288ecc943SGeorge Wilson 	(void) nvpair_value_##type(elem, &value); \
77388ecc943SGeorge Wilson 	(void) printf("%*s%s: " format "\n", indent, "", \
77488ecc943SGeorge Wilson 	    nvpair_name(elem), (ptype)value); \
77588ecc943SGeorge Wilson }
77688ecc943SGeorge Wilson 
77788ecc943SGeorge Wilson #define	NVPA(elem, type, vtype, ptype, format) { \
77888ecc943SGeorge Wilson 	uint_t	i, count; \
77988ecc943SGeorge Wilson 	vtype	*value;  \
78088ecc943SGeorge Wilson \
78188ecc943SGeorge Wilson 	(void) nvpair_value_##type(elem, &value, &count); \
78288ecc943SGeorge Wilson 	for (i = 0; i < count; i++) { \
78388ecc943SGeorge Wilson 		(void) printf("%*s%s[%d]: " format "\n", indent, "", \
78488ecc943SGeorge Wilson 		    nvpair_name(elem), i, (ptype)value[i]); \
78588ecc943SGeorge Wilson 	} \
78688ecc943SGeorge Wilson }
78788ecc943SGeorge Wilson 
78888ecc943SGeorge Wilson /*
78988ecc943SGeorge Wilson  * Similar to nvlist_print() but handles arrays slightly differently.
79088ecc943SGeorge Wilson  */
79188ecc943SGeorge Wilson void
79288ecc943SGeorge Wilson dump_nvlist(nvlist_t *list, int indent)
79388ecc943SGeorge Wilson {
79488ecc943SGeorge Wilson 	nvpair_t	*elem = NULL;
79588ecc943SGeorge Wilson 	boolean_t	bool_value;
79688ecc943SGeorge Wilson 	nvlist_t	*nvlist_value;
79788ecc943SGeorge Wilson 	nvlist_t	**nvlist_array_value;
79888ecc943SGeorge Wilson 	uint_t		i, count;
79988ecc943SGeorge Wilson 
80088ecc943SGeorge Wilson 	if (list == NULL) {
80188ecc943SGeorge Wilson 		return;
80288ecc943SGeorge Wilson 	}
80388ecc943SGeorge Wilson 
80488ecc943SGeorge Wilson 	while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
80588ecc943SGeorge Wilson 		switch (nvpair_type(elem)) {
80688ecc943SGeorge Wilson 		case DATA_TYPE_BOOLEAN_VALUE:
80788ecc943SGeorge Wilson 			(void) nvpair_value_boolean_value(elem, &bool_value);
80888ecc943SGeorge Wilson 			(void) printf("%*s%s: %s\n", indent, "",
80988ecc943SGeorge Wilson 			    nvpair_name(elem), bool_value ? "true" : "false");
81088ecc943SGeorge Wilson 			break;
81188ecc943SGeorge Wilson 
81288ecc943SGeorge Wilson 		case DATA_TYPE_BYTE:
81388ecc943SGeorge Wilson 			NVP(elem, byte, uchar_t, int, "%u");
81488ecc943SGeorge Wilson 			break;
81588ecc943SGeorge Wilson 
81688ecc943SGeorge Wilson 		case DATA_TYPE_INT8:
81788ecc943SGeorge Wilson 			NVP(elem, int8, int8_t, int, "%d");
81888ecc943SGeorge Wilson 			break;
81988ecc943SGeorge Wilson 
82088ecc943SGeorge Wilson 		case DATA_TYPE_UINT8:
82188ecc943SGeorge Wilson 			NVP(elem, uint8, uint8_t, int, "%u");
82288ecc943SGeorge Wilson 			break;
82388ecc943SGeorge Wilson 
82488ecc943SGeorge Wilson 		case DATA_TYPE_INT16:
82588ecc943SGeorge Wilson 			NVP(elem, int16, int16_t, int, "%d");
82688ecc943SGeorge Wilson 			break;
82788ecc943SGeorge Wilson 
82888ecc943SGeorge Wilson 		case DATA_TYPE_UINT16:
82988ecc943SGeorge Wilson 			NVP(elem, uint16, uint16_t, int, "%u");
83088ecc943SGeorge Wilson 			break;
83188ecc943SGeorge Wilson 
83288ecc943SGeorge Wilson 		case DATA_TYPE_INT32:
83388ecc943SGeorge Wilson 			NVP(elem, int32, int32_t, long, "%ld");
83488ecc943SGeorge Wilson 			break;
83588ecc943SGeorge Wilson 
83688ecc943SGeorge Wilson 		case DATA_TYPE_UINT32:
83788ecc943SGeorge Wilson 			NVP(elem, uint32, uint32_t, ulong_t, "%lu");
83888ecc943SGeorge Wilson 			break;
83988ecc943SGeorge Wilson 
84088ecc943SGeorge Wilson 		case DATA_TYPE_INT64:
84188ecc943SGeorge Wilson 			NVP(elem, int64, int64_t, longlong_t, "%lld");
84288ecc943SGeorge Wilson 			break;
84388ecc943SGeorge Wilson 
84488ecc943SGeorge Wilson 		case DATA_TYPE_UINT64:
84588ecc943SGeorge Wilson 			NVP(elem, uint64, uint64_t, u_longlong_t, "%llu");
84688ecc943SGeorge Wilson 			break;
84788ecc943SGeorge Wilson 
84888ecc943SGeorge Wilson 		case DATA_TYPE_STRING:
84988ecc943SGeorge Wilson 			NVP(elem, string, char *, char *, "'%s'");
85088ecc943SGeorge Wilson 			break;
85188ecc943SGeorge Wilson 
85288ecc943SGeorge Wilson 		case DATA_TYPE_BYTE_ARRAY:
85388ecc943SGeorge Wilson 			NVPA(elem, byte_array, uchar_t, int, "%u");
85488ecc943SGeorge Wilson 			break;
85588ecc943SGeorge Wilson 
85688ecc943SGeorge Wilson 		case DATA_TYPE_INT8_ARRAY:
85788ecc943SGeorge Wilson 			NVPA(elem, int8_array, int8_t, int, "%d");
85888ecc943SGeorge Wilson 			break;
85988ecc943SGeorge Wilson 
86088ecc943SGeorge Wilson 		case DATA_TYPE_UINT8_ARRAY:
86188ecc943SGeorge Wilson 			NVPA(elem, uint8_array, uint8_t, int, "%u");
86288ecc943SGeorge Wilson 			break;
86388ecc943SGeorge Wilson 
86488ecc943SGeorge Wilson 		case DATA_TYPE_INT16_ARRAY:
86588ecc943SGeorge Wilson 			NVPA(elem, int16_array, int16_t, int, "%d");
86688ecc943SGeorge Wilson 			break;
86788ecc943SGeorge Wilson 
86888ecc943SGeorge Wilson 		case DATA_TYPE_UINT16_ARRAY:
86988ecc943SGeorge Wilson 			NVPA(elem, uint16_array, uint16_t, int, "%u");
87088ecc943SGeorge Wilson 			break;
87188ecc943SGeorge Wilson 
87288ecc943SGeorge Wilson 		case DATA_TYPE_INT32_ARRAY:
87388ecc943SGeorge Wilson 			NVPA(elem, int32_array, int32_t, long, "%ld");
87488ecc943SGeorge Wilson 			break;
87588ecc943SGeorge Wilson 
87688ecc943SGeorge Wilson 		case DATA_TYPE_UINT32_ARRAY:
87788ecc943SGeorge Wilson 			NVPA(elem, uint32_array, uint32_t, ulong_t, "%lu");
87888ecc943SGeorge Wilson 			break;
87988ecc943SGeorge Wilson 
88088ecc943SGeorge Wilson 		case DATA_TYPE_INT64_ARRAY:
88188ecc943SGeorge Wilson 			NVPA(elem, int64_array, int64_t, longlong_t, "%lld");
88288ecc943SGeorge Wilson 			break;
88388ecc943SGeorge Wilson 
88488ecc943SGeorge Wilson 		case DATA_TYPE_UINT64_ARRAY:
88588ecc943SGeorge Wilson 			NVPA(elem, uint64_array, uint64_t, u_longlong_t,
88688ecc943SGeorge Wilson 			    "%llu");
88788ecc943SGeorge Wilson 			break;
88888ecc943SGeorge Wilson 
88988ecc943SGeorge Wilson 		case DATA_TYPE_STRING_ARRAY:
89088ecc943SGeorge Wilson 			NVPA(elem, string_array, char *, char *, "'%s'");
89188ecc943SGeorge Wilson 			break;
89288ecc943SGeorge Wilson 
89388ecc943SGeorge Wilson 		case DATA_TYPE_NVLIST:
89488ecc943SGeorge Wilson 			(void) nvpair_value_nvlist(elem, &nvlist_value);
89588ecc943SGeorge Wilson 			(void) printf("%*s%s:\n", indent, "",
89688ecc943SGeorge Wilson 			    nvpair_name(elem));
89788ecc943SGeorge Wilson 			dump_nvlist(nvlist_value, indent + 4);
89888ecc943SGeorge Wilson 			break;
89988ecc943SGeorge Wilson 
90088ecc943SGeorge Wilson 		case DATA_TYPE_NVLIST_ARRAY:
90188ecc943SGeorge Wilson 			(void) nvpair_value_nvlist_array(elem,
90288ecc943SGeorge Wilson 			    &nvlist_array_value, &count);
90388ecc943SGeorge Wilson 			for (i = 0; i < count; i++) {
90488ecc943SGeorge Wilson 				(void) printf("%*s%s[%u]:\n", indent, "",
90588ecc943SGeorge Wilson 				    nvpair_name(elem), i);
90688ecc943SGeorge Wilson 				dump_nvlist(nvlist_array_value[i], indent + 4);
90788ecc943SGeorge Wilson 			}
90888ecc943SGeorge Wilson 			break;
90988ecc943SGeorge Wilson 
91088ecc943SGeorge Wilson 		default:
91188ecc943SGeorge Wilson 			(void) printf(dgettext(TEXT_DOMAIN, "bad config type "
91288ecc943SGeorge Wilson 			    "%d for %s\n"), nvpair_type(elem),
91388ecc943SGeorge Wilson 			    nvpair_name(elem));
91488ecc943SGeorge Wilson 		}
91588ecc943SGeorge Wilson 	}
91688ecc943SGeorge Wilson }
91788ecc943SGeorge Wilson 
918602ca9eaScth /*
919*f6e214c7SGavin Maltby  * ======================================================================
920*f6e214c7SGavin Maltby  * |									|
921*f6e214c7SGavin Maltby  * | Misc private interface.						|
922*f6e214c7SGavin Maltby  * |									|
923*f6e214c7SGavin Maltby  * ======================================================================
924*f6e214c7SGavin Maltby  */
925*f6e214c7SGavin Maltby 
926*f6e214c7SGavin Maltby /*
927602ca9eaScth  * Determine if string 'value' matches 'nvp' value.  The 'value' string is
928602ca9eaScth  * converted, depending on the type of 'nvp', prior to match.  For numeric
929602ca9eaScth  * types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
930602ca9eaScth  * is an array type, 'ai' is the index into the array against which we are
931602ca9eaScth  * checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
932602ca9eaScth  * in a regex_t compilation of value in 'value_regex' to trigger regular
933602ca9eaScth  * expression string match instead of simple strcmp().
934602ca9eaScth  *
935602ca9eaScth  * Return 1 on match, 0 on no-match, and -1 on error.  If the error is
936602ca9eaScth  * related to value syntax error and 'ep' is non-NULL, *ep will point into
937602ca9eaScth  * the 'value' string at the location where the error exists.
938602ca9eaScth  *
939602ca9eaScth  * NOTE: It may be possible to move the non-regex_t version of this into
940602ca9eaScth  * common code used by library/kernel/boot.
941602ca9eaScth  */
942602ca9eaScth int
943602ca9eaScth nvpair_value_match_regex(nvpair_t *nvp, int ai,
944602ca9eaScth     char *value, regex_t *value_regex, char **ep)
945602ca9eaScth {
946602ca9eaScth 	char	*evalue;
947602ca9eaScth 	uint_t	a_len;
948602ca9eaScth 	int	sr;
949602ca9eaScth 
950602ca9eaScth 	if (ep)
951602ca9eaScth 		*ep = NULL;
952602ca9eaScth 
953602ca9eaScth 	if ((nvp == NULL) || (value == NULL))
954602ca9eaScth 		return (-1);		/* error fail match - invalid args */
955602ca9eaScth 
956602ca9eaScth 	/* make sure array and index combination make sense */
957602ca9eaScth 	if ((nvpair_type_is_array(nvp) && (ai < 0)) ||
958602ca9eaScth 	    (!nvpair_type_is_array(nvp) && (ai >= 0)))
959602ca9eaScth 		return (-1);		/* error fail match - bad index */
960602ca9eaScth 
961602ca9eaScth 	/* non-string values should be single 'chunk' */
962602ca9eaScth 	if ((nvpair_type(nvp) != DATA_TYPE_STRING) &&
963602ca9eaScth 	    (nvpair_type(nvp) != DATA_TYPE_STRING_ARRAY)) {
964602ca9eaScth 		value += strspn(value, " \t");
965602ca9eaScth 		evalue = value + strcspn(value, " \t");
966602ca9eaScth 		if (*evalue) {
967602ca9eaScth 			if (ep)
968602ca9eaScth 				*ep = evalue;
969602ca9eaScth 			return (-1);	/* error fail match - syntax */
970602ca9eaScth 		}
971602ca9eaScth 	}
972602ca9eaScth 
973602ca9eaScth 	sr = EOF;
974602ca9eaScth 	switch (nvpair_type(nvp)) {
975602ca9eaScth 	case DATA_TYPE_STRING: {
976602ca9eaScth 		char	*val;
977602ca9eaScth 
978602ca9eaScth 		/* check string value for match */
979602ca9eaScth 		if (nvpair_value_string(nvp, &val) == 0) {
980602ca9eaScth 			if (value_regex) {
981602ca9eaScth 				if (regexec(value_regex, val,
982602ca9eaScth 				    (size_t)0, NULL, 0) == 0)
983602ca9eaScth 					return (1);	/* match */
984602ca9eaScth 			} else {
985602ca9eaScth 				if (strcmp(value, val) == 0)
986602ca9eaScth 					return (1);	/* match */
987602ca9eaScth 			}
988602ca9eaScth 		}
989602ca9eaScth 		break;
990602ca9eaScth 	}
991602ca9eaScth 	case DATA_TYPE_STRING_ARRAY: {
992602ca9eaScth 		char **val_array;
993602ca9eaScth 
994602ca9eaScth 		/* check indexed string value of array for match */
995602ca9eaScth 		if ((nvpair_value_string_array(nvp, &val_array, &a_len) == 0) &&
996602ca9eaScth 		    (ai < a_len)) {
997602ca9eaScth 			if (value_regex) {
998602ca9eaScth 				if (regexec(value_regex, val_array[ai],
999602ca9eaScth 				    (size_t)0, NULL, 0) == 0)
1000602ca9eaScth 					return (1);
1001602ca9eaScth 			} else {
1002602ca9eaScth 				if (strcmp(value, val_array[ai]) == 0)
1003602ca9eaScth 					return (1);
1004602ca9eaScth 			}
1005602ca9eaScth 		}
1006602ca9eaScth 		break;
1007602ca9eaScth 	}
1008602ca9eaScth 	case DATA_TYPE_BYTE: {
1009602ca9eaScth 		uchar_t val, val_arg;
1010602ca9eaScth 
1011602ca9eaScth 		/* scanf uchar_t from value and check for match */
1012602ca9eaScth 		sr = sscanf(value, "%c", &val_arg);
1013602ca9eaScth 		if ((sr == 1) && (nvpair_value_byte(nvp, &val) == 0) &&
1014602ca9eaScth 		    (val == val_arg))
1015602ca9eaScth 			return (1);
1016602ca9eaScth 		break;
1017602ca9eaScth 	}
1018602ca9eaScth 	case DATA_TYPE_BYTE_ARRAY: {
1019602ca9eaScth 		uchar_t *val_array, val_arg;
1020602ca9eaScth 
1021602ca9eaScth 
1022602ca9eaScth 		/* check indexed value of array for match */
1023602ca9eaScth 		sr = sscanf(value, "%c", &val_arg);
1024602ca9eaScth 		if ((sr == 1) &&
1025602ca9eaScth 		    (nvpair_value_byte_array(nvp, &val_array, &a_len) == 0) &&
1026602ca9eaScth 		    (ai < a_len) &&
1027602ca9eaScth 		    (val_array[ai] == val_arg))
1028602ca9eaScth 			return (1);
1029602ca9eaScth 		break;
1030602ca9eaScth 	}
1031602ca9eaScth 	case DATA_TYPE_INT8: {
1032602ca9eaScth 		int8_t val, val_arg;
1033602ca9eaScth 
1034602ca9eaScth 		/* scanf int8_t from value and check for match */
1035602ca9eaScth 		sr = sscanf(value, "%"SCNi8, &val_arg);
1036602ca9eaScth 		if ((sr == 1) &&
1037602ca9eaScth 		    (nvpair_value_int8(nvp, &val) == 0) &&
1038602ca9eaScth 		    (val == val_arg))
1039602ca9eaScth 			return (1);
1040602ca9eaScth 		break;
1041602ca9eaScth 	}
1042602ca9eaScth 	case DATA_TYPE_INT8_ARRAY: {
1043602ca9eaScth 		int8_t *val_array, val_arg;
1044602ca9eaScth 
1045602ca9eaScth 		/* check indexed value of array for match */
1046602ca9eaScth 		sr = sscanf(value, "%"SCNi8, &val_arg);
1047602ca9eaScth 		if ((sr == 1) &&
1048602ca9eaScth 		    (nvpair_value_int8_array(nvp, &val_array, &a_len) == 0) &&
1049602ca9eaScth 		    (ai < a_len) &&
1050602ca9eaScth 		    (val_array[ai] == val_arg))
1051602ca9eaScth 			return (1);
1052602ca9eaScth 		break;
1053602ca9eaScth 	}
1054602ca9eaScth 	case DATA_TYPE_UINT8: {
1055602ca9eaScth 		uint8_t val, val_arg;
1056602ca9eaScth 
1057602ca9eaScth 		/* scanf uint8_t from value and check for match */
1058602ca9eaScth 		sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1059602ca9eaScth 		if ((sr == 1) &&
1060602ca9eaScth 		    (nvpair_value_uint8(nvp, &val) == 0) &&
1061602ca9eaScth 		    (val == val_arg))
1062602ca9eaScth 			return (1);
1063602ca9eaScth 		break;
1064602ca9eaScth 	}
1065602ca9eaScth 	case DATA_TYPE_UINT8_ARRAY: {
1066602ca9eaScth 		uint8_t *val_array, val_arg;
1067602ca9eaScth 
1068602ca9eaScth 		/* check indexed value of array for match */
1069602ca9eaScth 		sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1070602ca9eaScth 		if ((sr == 1) &&
1071602ca9eaScth 		    (nvpair_value_uint8_array(nvp, &val_array, &a_len) == 0) &&
1072602ca9eaScth 		    (ai < a_len) &&
1073602ca9eaScth 		    (val_array[ai] == val_arg))
1074602ca9eaScth 			return (1);
1075602ca9eaScth 		break;
1076602ca9eaScth 	}
1077602ca9eaScth 	case DATA_TYPE_INT16: {
1078602ca9eaScth 		int16_t val, val_arg;
1079602ca9eaScth 
1080602ca9eaScth 		/* scanf int16_t from value and check for match */
1081602ca9eaScth 		sr = sscanf(value, "%"SCNi16, &val_arg);
1082602ca9eaScth 		if ((sr == 1) &&
1083602ca9eaScth 		    (nvpair_value_int16(nvp, &val) == 0) &&
1084602ca9eaScth 		    (val == val_arg))
1085602ca9eaScth 			return (1);
1086602ca9eaScth 		break;
1087602ca9eaScth 	}
1088602ca9eaScth 	case DATA_TYPE_INT16_ARRAY: {
1089602ca9eaScth 		int16_t *val_array, val_arg;
1090602ca9eaScth 
1091602ca9eaScth 		/* check indexed value of array for match */
1092602ca9eaScth 		sr = sscanf(value, "%"SCNi16, &val_arg);
1093602ca9eaScth 		if ((sr == 1) &&
1094602ca9eaScth 		    (nvpair_value_int16_array(nvp, &val_array, &a_len) == 0) &&
1095602ca9eaScth 		    (ai < a_len) &&
1096602ca9eaScth 		    (val_array[ai] == val_arg))
1097602ca9eaScth 			return (1);
1098602ca9eaScth 		break;
1099602ca9eaScth 	}
1100602ca9eaScth 	case DATA_TYPE_UINT16: {
1101602ca9eaScth 		uint16_t val, val_arg;
1102602ca9eaScth 
1103602ca9eaScth 		/* scanf uint16_t from value and check for match */
1104602ca9eaScth 		sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1105602ca9eaScth 		if ((sr == 1) &&
1106602ca9eaScth 		    (nvpair_value_uint16(nvp, &val) == 0) &&
1107602ca9eaScth 		    (val == val_arg))
1108602ca9eaScth 			return (1);
1109602ca9eaScth 		break;
1110602ca9eaScth 	}
1111602ca9eaScth 	case DATA_TYPE_UINT16_ARRAY: {
1112602ca9eaScth 		uint16_t *val_array, val_arg;
1113602ca9eaScth 
1114602ca9eaScth 		/* check indexed value of array for match */
1115602ca9eaScth 		sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1116602ca9eaScth 		if ((sr == 1) &&
1117602ca9eaScth 		    (nvpair_value_uint16_array(nvp, &val_array, &a_len) == 0) &&
1118602ca9eaScth 		    (ai < a_len) &&
1119602ca9eaScth 		    (val_array[ai] == val_arg))
1120602ca9eaScth 			return (1);
1121602ca9eaScth 		break;
1122602ca9eaScth 	}
1123602ca9eaScth 	case DATA_TYPE_INT32: {
1124602ca9eaScth 		int32_t val, val_arg;
1125602ca9eaScth 
1126602ca9eaScth 		/* scanf int32_t from value and check for match */
1127602ca9eaScth 		sr = sscanf(value, "%"SCNi32, &val_arg);
1128602ca9eaScth 		if ((sr == 1) &&
1129602ca9eaScth 		    (nvpair_value_int32(nvp, &val) == 0) &&
1130602ca9eaScth 		    (val == val_arg))
1131602ca9eaScth 			return (1);
1132602ca9eaScth 		break;
1133602ca9eaScth 	}
1134602ca9eaScth 	case DATA_TYPE_INT32_ARRAY: {
1135602ca9eaScth 		int32_t *val_array, val_arg;
1136602ca9eaScth 
1137602ca9eaScth 		/* check indexed value of array for match */
1138602ca9eaScth 		sr = sscanf(value, "%"SCNi32, &val_arg);
1139602ca9eaScth 		if ((sr == 1) &&
1140602ca9eaScth 		    (nvpair_value_int32_array(nvp, &val_array, &a_len) == 0) &&
1141602ca9eaScth 		    (ai < a_len) &&
1142602ca9eaScth 		    (val_array[ai] == val_arg))
1143602ca9eaScth 			return (1);
1144602ca9eaScth 		break;
1145602ca9eaScth 	}
1146602ca9eaScth 	case DATA_TYPE_UINT32: {
1147602ca9eaScth 		uint32_t val, val_arg;
1148602ca9eaScth 
1149602ca9eaScth 		/* scanf uint32_t from value and check for match */
1150602ca9eaScth 		sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1151602ca9eaScth 		if ((sr == 1) &&
1152602ca9eaScth 		    (nvpair_value_uint32(nvp, &val) == 0) &&
1153602ca9eaScth 		    (val == val_arg))
1154602ca9eaScth 			return (1);
1155602ca9eaScth 		break;
1156602ca9eaScth 	}
1157602ca9eaScth 	case DATA_TYPE_UINT32_ARRAY: {
1158602ca9eaScth 		uint32_t *val_array, val_arg;
1159602ca9eaScth 
1160602ca9eaScth 		/* check indexed value of array for match */
1161602ca9eaScth 		sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1162602ca9eaScth 		if ((sr == 1) &&
1163602ca9eaScth 		    (nvpair_value_uint32_array(nvp, &val_array, &a_len) == 0) &&
1164602ca9eaScth 		    (ai < a_len) &&
1165602ca9eaScth 		    (val_array[ai] == val_arg))
1166602ca9eaScth 			return (1);
1167602ca9eaScth 		break;
1168602ca9eaScth 	}
1169602ca9eaScth 	case DATA_TYPE_INT64: {
1170602ca9eaScth 		int64_t val, val_arg;
1171602ca9eaScth 
1172602ca9eaScth 		/* scanf int64_t from value and check for match */
1173602ca9eaScth 		sr = sscanf(value, "%"SCNi64, &val_arg);
1174602ca9eaScth 		if ((sr == 1) &&
1175602ca9eaScth 		    (nvpair_value_int64(nvp, &val) == 0) &&
1176602ca9eaScth 		    (val == val_arg))
1177602ca9eaScth 			return (1);
1178602ca9eaScth 		break;
1179602ca9eaScth 	}
1180602ca9eaScth 	case DATA_TYPE_INT64_ARRAY: {
1181602ca9eaScth 		int64_t *val_array, val_arg;
1182602ca9eaScth 
1183602ca9eaScth 		/* check indexed value of array for match */
1184602ca9eaScth 		sr = sscanf(value, "%"SCNi64, &val_arg);
1185602ca9eaScth 		if ((sr == 1) &&
1186602ca9eaScth 		    (nvpair_value_int64_array(nvp, &val_array, &a_len) == 0) &&
1187602ca9eaScth 		    (ai < a_len) &&
1188602ca9eaScth 		    (val_array[ai] == val_arg))
1189602ca9eaScth 				return (1);
1190602ca9eaScth 		break;
1191602ca9eaScth 	}
1192602ca9eaScth 	case DATA_TYPE_UINT64: {
1193602ca9eaScth 		uint64_t val_arg, val;
1194602ca9eaScth 
1195602ca9eaScth 		/* scanf uint64_t from value and check for match */
1196602ca9eaScth 		sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1197602ca9eaScth 		if ((sr == 1) &&
1198602ca9eaScth 		    (nvpair_value_uint64(nvp, &val) == 0) &&
1199602ca9eaScth 		    (val == val_arg))
1200602ca9eaScth 			return (1);
1201602ca9eaScth 		break;
1202602ca9eaScth 	}
1203602ca9eaScth 	case DATA_TYPE_UINT64_ARRAY: {
1204602ca9eaScth 		uint64_t *val_array, val_arg;
1205602ca9eaScth 
1206602ca9eaScth 		/* check indexed value of array for match */
1207602ca9eaScth 		sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1208602ca9eaScth 		if ((sr == 1) &&
1209602ca9eaScth 		    (nvpair_value_uint64_array(nvp, &val_array, &a_len) == 0) &&
1210602ca9eaScth 		    (ai < a_len) &&
1211602ca9eaScth 		    (val_array[ai] == val_arg))
1212602ca9eaScth 			return (1);
1213602ca9eaScth 		break;
1214602ca9eaScth 	}
1215602ca9eaScth 	case DATA_TYPE_BOOLEAN_VALUE: {
1216602ca9eaScth 		boolean_t val, val_arg;
1217602ca9eaScth 
1218602ca9eaScth 		/* scanf boolean_t from value and check for match */
1219602ca9eaScth 		sr = sscanf(value, "%"SCNi32, &val_arg);
1220602ca9eaScth 		if ((sr == 1) &&
1221602ca9eaScth 		    (nvpair_value_boolean_value(nvp, &val) == 0) &&
1222602ca9eaScth 		    (val == val_arg))
1223602ca9eaScth 			return (1);
1224602ca9eaScth 		break;
1225602ca9eaScth 	}
1226602ca9eaScth 	case DATA_TYPE_BOOLEAN_ARRAY: {
1227602ca9eaScth 		boolean_t *val_array, val_arg;
1228602ca9eaScth 
1229602ca9eaScth 		/* check indexed value of array for match */
1230602ca9eaScth 		sr = sscanf(value, "%"SCNi32, &val_arg);
1231602ca9eaScth 		if ((sr == 1) &&
1232602ca9eaScth 		    (nvpair_value_boolean_array(nvp,
1233602ca9eaScth 		    &val_array, &a_len) == 0) &&
1234602ca9eaScth 		    (ai < a_len) &&
1235602ca9eaScth 		    (val_array[ai] == val_arg))
1236602ca9eaScth 			return (1);
1237602ca9eaScth 		break;
1238602ca9eaScth 	}
1239602ca9eaScth 	case DATA_TYPE_HRTIME:
1240602ca9eaScth 	case DATA_TYPE_NVLIST:
1241602ca9eaScth 	case DATA_TYPE_NVLIST_ARRAY:
1242602ca9eaScth 	case DATA_TYPE_BOOLEAN:
1243825ba0f2Srobj 	case DATA_TYPE_DOUBLE:
1244602ca9eaScth 	case DATA_TYPE_UNKNOWN:
1245602ca9eaScth 	default:
1246602ca9eaScth 		/*
1247602ca9eaScth 		 * unknown/unsupported data type
1248602ca9eaScth 		 */
1249602ca9eaScth 		return (-1);		/* error fail match */
1250602ca9eaScth 	}
1251602ca9eaScth 
1252602ca9eaScth 	/*
1253602ca9eaScth 	 * check to see if sscanf failed conversion, return approximate
1254602ca9eaScth 	 * pointer to problem
1255602ca9eaScth 	 */
1256602ca9eaScth 	if (sr != 1) {
1257602ca9eaScth 		if (ep)
1258602ca9eaScth 			*ep = value;
1259602ca9eaScth 		return (-1);		/* error fail match  - syntax */
1260602ca9eaScth 	}
1261602ca9eaScth 
1262602ca9eaScth 	return (0);			/* fail match */
1263602ca9eaScth }
1264602ca9eaScth 
1265602ca9eaScth int
1266602ca9eaScth nvpair_value_match(nvpair_t *nvp, int ai, char *value, char **ep)
1267602ca9eaScth {
1268602ca9eaScth 	return (nvpair_value_match_regex(nvp, ai, value, NULL, ep));
1269602ca9eaScth }
1270