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