4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
29 #include <sys/types.h>
30 #include <sys/inttypes.h>
32 #include "libnvpair.h"
35 * libnvpair - A tools library for manipulating <name, value> pairs.
37 * This library provides routines packing an unpacking nv pairs
38 * for transporting data across process boundaries, transporting
39 * between kernel and userland, and possibly saving onto disk files.
43 * Print control structure.
46 #define DEFINEOP(opname, vtype) \
48 int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
49 const char *, vtype); \
53 #define DEFINEARROP(opname, vtype) \
55 int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
56 const char *, vtype, uint_t); \
60 struct nvlist_printops
{
61 DEFINEOP(print_boolean
, int);
62 DEFINEOP(print_boolean_value
, boolean_t
);
63 DEFINEOP(print_byte
, uchar_t
);
64 DEFINEOP(print_int8
, int8_t);
65 DEFINEOP(print_uint8
, uint8_t);
66 DEFINEOP(print_int16
, int16_t);
67 DEFINEOP(print_uint16
, uint16_t);
68 DEFINEOP(print_int32
, int32_t);
69 DEFINEOP(print_uint32
, uint32_t);
70 DEFINEOP(print_int64
, int64_t);
71 DEFINEOP(print_uint64
, uint64_t);
72 DEFINEOP(print_double
, double);
73 DEFINEOP(print_string
, const char *);
74 DEFINEOP(print_hrtime
, hrtime_t
);
75 DEFINEOP(print_nvlist
, nvlist_t
*);
76 DEFINEARROP(print_boolean_array
, boolean_t
*);
77 DEFINEARROP(print_byte_array
, uchar_t
*);
78 DEFINEARROP(print_int8_array
, int8_t *);
79 DEFINEARROP(print_uint8_array
, uint8_t *);
80 DEFINEARROP(print_int16_array
, int16_t *);
81 DEFINEARROP(print_uint16_array
, uint16_t *);
82 DEFINEARROP(print_int32_array
, int32_t *);
83 DEFINEARROP(print_uint32_array
, uint32_t *);
84 DEFINEARROP(print_int64_array
, int64_t *);
85 DEFINEARROP(print_uint64_array
, uint64_t *);
86 DEFINEARROP(print_string_array
, const char **);
87 DEFINEARROP(print_nvlist_array
, nvlist_t
**);
90 struct nvlist_prtctl
{
91 FILE *nvprt_fp
; /* output destination */
92 enum nvlist_indent_mode nvprt_indent_mode
; /* see above */
93 int nvprt_indent
; /* absolute indent, or tab depth */
94 int nvprt_indentinc
; /* indent or tab increment */
95 const char *nvprt_nmfmt
; /* member name format, max one %s */
96 const char *nvprt_eomfmt
; /* after member format, e.g. "\n" */
97 const char *nvprt_btwnarrfmt
; /* between array members */
98 int nvprt_btwnarrfmt_nl
; /* nvprt_eoamfmt includes newline? */
99 struct nvlist_printops
*nvprt_dfltops
;
100 struct nvlist_printops
*nvprt_custops
;
103 #define DFLTPRTOP(pctl, type) \
104 ((pctl)->nvprt_dfltops->print_##type.op)
106 #define DFLTPRTOPARG(pctl, type) \
107 ((pctl)->nvprt_dfltops->print_##type.arg)
109 #define CUSTPRTOP(pctl, type) \
110 ((pctl)->nvprt_custops->print_##type.op)
112 #define CUSTPRTOPARG(pctl, type) \
113 ((pctl)->nvprt_custops->print_##type.arg)
115 #define RENDER(pctl, type, nvl, name, val) \
118 if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
119 done = CUSTPRTOP(pctl, type)(pctl, \
120 CUSTPRTOPARG(pctl, type), nvl, name, val); \
123 (void) DFLTPRTOP(pctl, type)(pctl, \
124 DFLTPRTOPARG(pctl, type), nvl, name, val); \
126 (void) fprintf(pctl->nvprt_fp, "%s", pctl->nvprt_eomfmt); \
129 #define ARENDER(pctl, type, nvl, name, arrp, count) \
132 if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
133 done = CUSTPRTOP(pctl, type)(pctl, \
134 CUSTPRTOPARG(pctl, type), nvl, name, arrp, count); \
137 (void) DFLTPRTOP(pctl, type)(pctl, \
138 DFLTPRTOPARG(pctl, type), nvl, name, arrp, count); \
140 (void) fprintf(pctl->nvprt_fp, "%s", pctl->nvprt_eomfmt); \
143 static void nvlist_print_with_indent(nvlist_t
*, nvlist_prtctl_t
);
146 * ======================================================================
150 * ======================================================================
154 indent(nvlist_prtctl_t pctl
, int onemore
)
158 switch (pctl
->nvprt_indent_mode
) {
159 case NVLIST_INDENT_ABS
:
160 (void) fprintf(pctl
->nvprt_fp
, "%*s",
161 pctl
->nvprt_indent
+ onemore
* pctl
->nvprt_indentinc
, "");
164 case NVLIST_INDENT_TABBED
:
165 depth
= pctl
->nvprt_indent
+ onemore
;
167 (void) fprintf(pctl
->nvprt_fp
, "\t");
172 * ======================================================================
174 * | Default nvlist member rendering functions. |
176 * ======================================================================
180 * Generate functions to print single-valued nvlist members.
182 * type_and_variant - suffix to form function name
183 * vtype - C type for the member value
184 * ptype - C type to cast value to for printing
185 * vfmt - format string for pair value, e.g "%d" or "0x%llx"
188 #define NVLIST_PRTFUNC(type_and_variant, vtype, ptype, vfmt) \
190 nvprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
191 nvlist_t *nvl, const char *name, vtype value) \
195 FILE *fp = pctl->nvprt_fp; \
197 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
198 (void) fprintf(fp, vfmt, (ptype)value); \
203 * Workaround for GCC 12+ with UBSan enabled deficencies.
205 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
206 * below as violating -Wformat-overflow.
208 #if defined(__GNUC__) && !defined(__clang__) && \
209 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
210 #pragma GCC diagnostic push
211 #pragma GCC diagnostic ignored "-Wformat-overflow"
213 NVLIST_PRTFUNC(boolean
, int, int, "%d")
214 NVLIST_PRTFUNC(boolean_value
, boolean_t
, int, "%d")
215 NVLIST_PRTFUNC(byte
, uchar_t
, uchar_t
, "0x%2.2x")
216 NVLIST_PRTFUNC(int8
, int8_t, int, "%d")
217 NVLIST_PRTFUNC(uint8
, uint8_t, uint8_t, "0x%x")
218 NVLIST_PRTFUNC(int16
, int16_t, int16_t, "%d")
219 NVLIST_PRTFUNC(uint16
, uint16_t, uint16_t, "0x%x")
220 NVLIST_PRTFUNC(int32
, int32_t, int32_t, "%d")
221 NVLIST_PRTFUNC(uint32
, uint32_t, uint32_t, "0x%x")
222 NVLIST_PRTFUNC(int64
, int64_t, longlong_t
, "%lld")
223 NVLIST_PRTFUNC(uint64
, uint64_t, u_longlong_t
, "0x%llx")
224 NVLIST_PRTFUNC(double, double, double, "0x%f")
225 NVLIST_PRTFUNC(string
, const char *, const char *, "%s")
226 NVLIST_PRTFUNC(hrtime
, hrtime_t
, hrtime_t
, "0x%llx")
227 #if defined(__GNUC__) && !defined(__clang__) && \
228 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
229 #pragma GCC diagnostic pop
233 * Generate functions to print array-valued nvlist members.
236 #define NVLIST_ARRPRTFUNC(type_and_variant, vtype, ptype, vfmt) \
238 nvaprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
239 nvlist_t *nvl, const char *name, vtype *valuep, uint_t count) \
243 FILE *fp = pctl->nvprt_fp; \
245 for (i = 0; i < count; i++) { \
246 if (i == 0 || pctl->nvprt_btwnarrfmt_nl) { \
248 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
249 if (pctl->nvprt_btwnarrfmt_nl) \
250 (void) fprintf(fp, "[%d]: ", i); \
253 (void) fprintf(fp, "%s", pctl->nvprt_btwnarrfmt); \
254 (void) fprintf(fp, vfmt, (ptype)valuep[i]); \
259 NVLIST_ARRPRTFUNC(boolean_array
, boolean_t
, boolean_t
, "%d")
260 NVLIST_ARRPRTFUNC(byte_array
, uchar_t
, uchar_t
, "0x%2.2x")
261 NVLIST_ARRPRTFUNC(int8_array
, int8_t, int8_t, "%d")
262 NVLIST_ARRPRTFUNC(uint8_array
, uint8_t, uint8_t, "0x%x")
263 NVLIST_ARRPRTFUNC(int16_array
, int16_t, int16_t, "%d")
264 NVLIST_ARRPRTFUNC(uint16_array
, uint16_t, uint16_t, "0x%x")
265 NVLIST_ARRPRTFUNC(int32_array
, int32_t, int32_t, "%d")
266 NVLIST_ARRPRTFUNC(uint32_array
, uint32_t, uint32_t, "0x%x")
267 NVLIST_ARRPRTFUNC(int64_array
, int64_t, longlong_t
, "%lld")
268 NVLIST_ARRPRTFUNC(uint64_array
, uint64_t, u_longlong_t
, "0x%llx")
269 NVLIST_ARRPRTFUNC(string_array
, const char *, const char *, "%s")
272 nvprint_nvlist(nvlist_prtctl_t pctl
, void *private,
273 nvlist_t
*nvl
, const char *name
, nvlist_t
*value
)
275 (void) private, (void) nvl
;
276 FILE *fp
= pctl
->nvprt_fp
;
279 (void) fprintf(fp
, "%s = (embedded nvlist)\n", name
);
281 pctl
->nvprt_indent
+= pctl
->nvprt_indentinc
;
282 nvlist_print_with_indent(value
, pctl
);
283 pctl
->nvprt_indent
-= pctl
->nvprt_indentinc
;
286 (void) fprintf(fp
, "(end %s)\n", name
);
292 nvaprint_nvlist_array(nvlist_prtctl_t pctl
, void *private,
293 nvlist_t
*nvl
, const char *name
, nvlist_t
**valuep
, uint_t count
)
295 (void) private, (void) nvl
;
296 FILE *fp
= pctl
->nvprt_fp
;
300 (void) fprintf(fp
, "%s = (array of embedded nvlists)\n", name
);
302 for (i
= 0; i
< count
; i
++) {
304 (void) fprintf(fp
, "(start %s[%d])\n", name
, i
);
306 pctl
->nvprt_indent
+= pctl
->nvprt_indentinc
;
307 nvlist_print_with_indent(valuep
[i
], pctl
);
308 pctl
->nvprt_indent
-= pctl
->nvprt_indentinc
;
311 (void) fprintf(fp
, "(end %s[%d])\n", name
, i
);
318 * ======================================================================
320 * | Interfaces that allow control over formatting. |
322 * ======================================================================
326 nvlist_prtctl_setdest(nvlist_prtctl_t pctl
, FILE *fp
)
332 nvlist_prtctl_getdest(nvlist_prtctl_t pctl
)
334 return (pctl
->nvprt_fp
);
339 nvlist_prtctl_setindent(nvlist_prtctl_t pctl
, enum nvlist_indent_mode mode
,
342 if (mode
< NVLIST_INDENT_ABS
|| mode
> NVLIST_INDENT_TABBED
)
343 mode
= NVLIST_INDENT_TABBED
;
351 pctl
->nvprt_indent_mode
= mode
;
352 pctl
->nvprt_indent
= start
;
353 pctl
->nvprt_indentinc
= inc
;
357 nvlist_prtctl_doindent(nvlist_prtctl_t pctl
, int onemore
)
359 indent(pctl
, onemore
);
364 nvlist_prtctl_setfmt(nvlist_prtctl_t pctl
, enum nvlist_prtctl_fmt which
,
368 case NVLIST_FMT_MEMBER_NAME
:
371 pctl
->nvprt_nmfmt
= fmt
;
374 case NVLIST_FMT_MEMBER_POSTAMBLE
:
377 pctl
->nvprt_eomfmt
= fmt
;
380 case NVLIST_FMT_BTWN_ARRAY
:
382 pctl
->nvprt_btwnarrfmt
= " ";
383 pctl
->nvprt_btwnarrfmt_nl
= 0;
385 pctl
->nvprt_btwnarrfmt
= fmt
;
386 pctl
->nvprt_btwnarrfmt_nl
= (strchr(fmt
, '\n') != NULL
);
397 nvlist_prtctl_dofmt(nvlist_prtctl_t pctl
, enum nvlist_prtctl_fmt which
, ...)
399 FILE *fp
= pctl
->nvprt_fp
;
406 case NVLIST_FMT_MEMBER_NAME
:
407 name
= va_arg(ap
, const char *);
408 (void) fprintf(fp
, pctl
->nvprt_nmfmt
, name
);
411 case NVLIST_FMT_MEMBER_POSTAMBLE
:
412 (void) fprintf(fp
, "%s", pctl
->nvprt_eomfmt
);
415 case NVLIST_FMT_BTWN_ARRAY
:
416 (void) fprintf(fp
, "%s", pctl
->nvprt_btwnarrfmt
);
427 * ======================================================================
429 * | Interfaces to allow appointment of replacement rendering functions.|
431 * ======================================================================
434 #define NVLIST_PRINTCTL_REPLACE(type, vtype) \
436 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
437 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype), \
440 CUSTPRTOP(pctl, type) = func; \
441 CUSTPRTOPARG(pctl, type) = private; \
444 NVLIST_PRINTCTL_REPLACE(boolean
, int)
445 NVLIST_PRINTCTL_REPLACE(boolean_value
, boolean_t
)
446 NVLIST_PRINTCTL_REPLACE(byte
, uchar_t
)
447 NVLIST_PRINTCTL_REPLACE(int8
, int8_t)
448 NVLIST_PRINTCTL_REPLACE(uint8
, uint8_t)
449 NVLIST_PRINTCTL_REPLACE(int16
, int16_t)
450 NVLIST_PRINTCTL_REPLACE(uint16
, uint16_t)
451 NVLIST_PRINTCTL_REPLACE(int32
, int32_t)
452 NVLIST_PRINTCTL_REPLACE(uint32
, uint32_t)
453 NVLIST_PRINTCTL_REPLACE(int64
, int64_t)
454 NVLIST_PRINTCTL_REPLACE(uint64
, uint64_t)
455 NVLIST_PRINTCTL_REPLACE(double, double)
456 NVLIST_PRINTCTL_REPLACE(string
, const char *)
457 NVLIST_PRINTCTL_REPLACE(hrtime
, hrtime_t
)
458 NVLIST_PRINTCTL_REPLACE(nvlist
, nvlist_t
*)
460 #define NVLIST_PRINTCTL_AREPLACE(type, vtype) \
462 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
463 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, \
464 uint_t), void *private) \
466 CUSTPRTOP(pctl, type) = func; \
467 CUSTPRTOPARG(pctl, type) = private; \
470 NVLIST_PRINTCTL_AREPLACE(boolean_array
, boolean_t
*)
471 NVLIST_PRINTCTL_AREPLACE(byte_array
, uchar_t
*)
472 NVLIST_PRINTCTL_AREPLACE(int8_array
, int8_t *)
473 NVLIST_PRINTCTL_AREPLACE(uint8_array
, uint8_t *)
474 NVLIST_PRINTCTL_AREPLACE(int16_array
, int16_t *)
475 NVLIST_PRINTCTL_AREPLACE(uint16_array
, uint16_t *)
476 NVLIST_PRINTCTL_AREPLACE(int32_array
, int32_t *)
477 NVLIST_PRINTCTL_AREPLACE(uint32_array
, uint32_t *)
478 NVLIST_PRINTCTL_AREPLACE(int64_array
, int64_t *)
479 NVLIST_PRINTCTL_AREPLACE(uint64_array
, uint64_t *)
480 NVLIST_PRINTCTL_AREPLACE(string_array
, const char **)
481 NVLIST_PRINTCTL_AREPLACE(nvlist_array
, nvlist_t
**)
484 * ======================================================================
486 * | Interfaces to manage nvlist_prtctl_t cookies. |
488 * ======================================================================
492 static const struct nvlist_printops defprtops
=
494 { nvprint_boolean
, NULL
},
495 { nvprint_boolean_value
, NULL
},
496 { nvprint_byte
, NULL
},
497 { nvprint_int8
, NULL
},
498 { nvprint_uint8
, NULL
},
499 { nvprint_int16
, NULL
},
500 { nvprint_uint16
, NULL
},
501 { nvprint_int32
, NULL
},
502 { nvprint_uint32
, NULL
},
503 { nvprint_int64
, NULL
},
504 { nvprint_uint64
, NULL
},
505 { nvprint_double
, NULL
},
506 { nvprint_string
, NULL
},
507 { nvprint_hrtime
, NULL
},
508 { nvprint_nvlist
, NULL
},
509 { nvaprint_boolean_array
, NULL
},
510 { nvaprint_byte_array
, NULL
},
511 { nvaprint_int8_array
, NULL
},
512 { nvaprint_uint8_array
, NULL
},
513 { nvaprint_int16_array
, NULL
},
514 { nvaprint_uint16_array
, NULL
},
515 { nvaprint_int32_array
, NULL
},
516 { nvaprint_uint32_array
, NULL
},
517 { nvaprint_int64_array
, NULL
},
518 { nvaprint_uint64_array
, NULL
},
519 { nvaprint_string_array
, NULL
},
520 { nvaprint_nvlist_array
, NULL
},
524 prtctl_defaults(FILE *fp
, struct nvlist_prtctl
*pctl
,
525 struct nvlist_printops
*ops
)
528 pctl
->nvprt_indent_mode
= NVLIST_INDENT_TABBED
;
529 pctl
->nvprt_indent
= 0;
530 pctl
->nvprt_indentinc
= 1;
531 pctl
->nvprt_nmfmt
= "%s = ";
532 pctl
->nvprt_eomfmt
= "\n";
533 pctl
->nvprt_btwnarrfmt
= " ";
534 pctl
->nvprt_btwnarrfmt_nl
= 0;
536 pctl
->nvprt_dfltops
= (struct nvlist_printops
*)&defprtops
;
537 pctl
->nvprt_custops
= ops
;
541 nvlist_prtctl_alloc(void)
543 struct nvlist_prtctl
*pctl
;
544 struct nvlist_printops
*ops
;
546 if ((pctl
= malloc(sizeof (*pctl
))) == NULL
)
549 if ((ops
= calloc(1, sizeof (*ops
))) == NULL
) {
554 prtctl_defaults(stdout
, pctl
, ops
);
560 nvlist_prtctl_free(nvlist_prtctl_t pctl
)
563 free(pctl
->nvprt_custops
);
569 * ======================================================================
571 * | Top-level print request interfaces. |
573 * ======================================================================
577 * nvlist_print - Prints elements in an event buffer
580 nvlist_print_with_indent(nvlist_t
*nvl
, nvlist_prtctl_t pctl
)
582 FILE *fp
= pctl
->nvprt_fp
;
591 (void) fprintf(fp
, "nvlist version: %d\n", NVL_VERSION(nvl
));
593 nvp
= nvlist_next_nvpair(nvl
, NULL
);
596 data_type_t type
= nvpair_type(nvp
);
598 name
= nvpair_name(nvp
);
602 case DATA_TYPE_BOOLEAN
: {
603 RENDER(pctl
, boolean
, nvl
, name
, 1);
606 case DATA_TYPE_BOOLEAN_VALUE
: {
608 (void) nvpair_value_boolean_value(nvp
, &val
);
609 RENDER(pctl
, boolean_value
, nvl
, name
, val
);
612 case DATA_TYPE_BYTE
: {
614 (void) nvpair_value_byte(nvp
, &val
);
615 RENDER(pctl
, byte
, nvl
, name
, val
);
618 case DATA_TYPE_INT8
: {
620 (void) nvpair_value_int8(nvp
, &val
);
621 RENDER(pctl
, int8
, nvl
, name
, val
);
624 case DATA_TYPE_UINT8
: {
626 (void) nvpair_value_uint8(nvp
, &val
);
627 RENDER(pctl
, uint8
, nvl
, name
, val
);
630 case DATA_TYPE_INT16
: {
632 (void) nvpair_value_int16(nvp
, &val
);
633 RENDER(pctl
, int16
, nvl
, name
, val
);
636 case DATA_TYPE_UINT16
: {
638 (void) nvpair_value_uint16(nvp
, &val
);
639 RENDER(pctl
, uint16
, nvl
, name
, val
);
642 case DATA_TYPE_INT32
: {
644 (void) nvpair_value_int32(nvp
, &val
);
645 RENDER(pctl
, int32
, nvl
, name
, val
);
648 case DATA_TYPE_UINT32
: {
650 (void) nvpair_value_uint32(nvp
, &val
);
651 RENDER(pctl
, uint32
, nvl
, name
, val
);
654 case DATA_TYPE_INT64
: {
656 (void) nvpair_value_int64(nvp
, &val
);
657 RENDER(pctl
, int64
, nvl
, name
, val
);
660 case DATA_TYPE_UINT64
: {
662 (void) nvpair_value_uint64(nvp
, &val
);
663 RENDER(pctl
, uint64
, nvl
, name
, val
);
666 case DATA_TYPE_DOUBLE
: {
668 (void) nvpair_value_double(nvp
, &val
);
669 RENDER(pctl
, double, nvl
, name
, val
);
672 case DATA_TYPE_STRING
: {
674 (void) nvpair_value_string(nvp
, &val
);
675 RENDER(pctl
, string
, nvl
, name
, val
);
678 case DATA_TYPE_BOOLEAN_ARRAY
: {
680 (void) nvpair_value_boolean_array(nvp
, &val
, &nelem
);
681 ARENDER(pctl
, boolean_array
, nvl
, name
, val
, nelem
);
684 case DATA_TYPE_BYTE_ARRAY
: {
686 (void) nvpair_value_byte_array(nvp
, &val
, &nelem
);
687 ARENDER(pctl
, byte_array
, nvl
, name
, val
, nelem
);
690 case DATA_TYPE_INT8_ARRAY
: {
692 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
693 ARENDER(pctl
, int8_array
, nvl
, name
, val
, nelem
);
696 case DATA_TYPE_UINT8_ARRAY
: {
698 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
699 ARENDER(pctl
, uint8_array
, nvl
, name
, val
, nelem
);
702 case DATA_TYPE_INT16_ARRAY
: {
704 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
705 ARENDER(pctl
, int16_array
, nvl
, name
, val
, nelem
);
708 case DATA_TYPE_UINT16_ARRAY
: {
710 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
711 ARENDER(pctl
, uint16_array
, nvl
, name
, val
, nelem
);
714 case DATA_TYPE_INT32_ARRAY
: {
716 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
717 ARENDER(pctl
, int32_array
, nvl
, name
, val
, nelem
);
720 case DATA_TYPE_UINT32_ARRAY
: {
722 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
723 ARENDER(pctl
, uint32_array
, nvl
, name
, val
, nelem
);
726 case DATA_TYPE_INT64_ARRAY
: {
728 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
729 ARENDER(pctl
, int64_array
, nvl
, name
, val
, nelem
);
732 case DATA_TYPE_UINT64_ARRAY
: {
734 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
735 ARENDER(pctl
, uint64_array
, nvl
, name
, val
, nelem
);
738 case DATA_TYPE_STRING_ARRAY
: {
740 (void) nvpair_value_string_array(nvp
, &val
, &nelem
);
741 ARENDER(pctl
, string_array
, nvl
, name
, val
, nelem
);
744 case DATA_TYPE_HRTIME
: {
746 (void) nvpair_value_hrtime(nvp
, &val
);
747 RENDER(pctl
, hrtime
, nvl
, name
, val
);
750 case DATA_TYPE_NVLIST
: {
752 (void) nvpair_value_nvlist(nvp
, &val
);
753 RENDER(pctl
, nvlist
, nvl
, name
, val
);
756 case DATA_TYPE_NVLIST_ARRAY
: {
758 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
759 ARENDER(pctl
, nvlist_array
, nvl
, name
, val
, nelem
);
763 (void) fprintf(fp
, " unknown data type (%d)", type
);
766 nvp
= nvlist_next_nvpair(nvl
, nvp
);
771 nvlist_print(FILE *fp
, nvlist_t
*nvl
)
773 struct nvlist_prtctl pc
;
775 prtctl_defaults(fp
, &pc
, NULL
);
776 nvlist_print_with_indent(nvl
, &pc
);
780 nvlist_prt(nvlist_t
*nvl
, nvlist_prtctl_t pctl
)
782 nvlist_print_with_indent(nvl
, pctl
);
785 #define NVP(elem, type, vtype, ptype, format) { \
788 (void) nvpair_value_##type(elem, &value); \
789 (void) printf("%*s%s: " format "\n", indent, "", \
790 nvpair_name(elem), (ptype)value); \
793 #define NVPA(elem, type, vtype, ptype, format) { \
797 (void) nvpair_value_##type(elem, &value, &count); \
798 for (i = 0; i < count; i++) { \
799 (void) printf("%*s%s[%d]: " format "\n", indent, "", \
800 nvpair_name(elem), i, (ptype)value[i]); \
805 * Similar to nvlist_print() but handles arrays slightly differently.
808 dump_nvlist(nvlist_t
*list
, int indent
)
810 nvpair_t
*elem
= NULL
;
811 boolean_t bool_value
;
812 nvlist_t
*nvlist_value
;
813 nvlist_t
**nvlist_array_value
;
820 while ((elem
= nvlist_next_nvpair(list
, elem
)) != NULL
) {
821 switch (nvpair_type(elem
)) {
822 case DATA_TYPE_BOOLEAN
:
823 (void) printf("%*s%s\n", indent
, "", nvpair_name(elem
));
826 case DATA_TYPE_BOOLEAN_VALUE
:
827 (void) nvpair_value_boolean_value(elem
, &bool_value
);
828 (void) printf("%*s%s: %s\n", indent
, "",
829 nvpair_name(elem
), bool_value
? "true" : "false");
833 NVP(elem
, byte
, uchar_t
, int, "%u");
837 NVP(elem
, int8
, int8_t, int, "%d");
840 case DATA_TYPE_UINT8
:
841 NVP(elem
, uint8
, uint8_t, int, "%u");
844 case DATA_TYPE_INT16
:
845 NVP(elem
, int16
, int16_t, int, "%d");
848 case DATA_TYPE_UINT16
:
849 NVP(elem
, uint16
, uint16_t, int, "%u");
852 case DATA_TYPE_INT32
:
853 NVP(elem
, int32
, int32_t, long, "%ld");
856 case DATA_TYPE_UINT32
:
857 NVP(elem
, uint32
, uint32_t, ulong_t
, "%lu");
860 case DATA_TYPE_INT64
:
861 NVP(elem
, int64
, int64_t, longlong_t
, "%lld");
864 case DATA_TYPE_UINT64
:
865 NVP(elem
, uint64
, uint64_t, u_longlong_t
, "%llu");
868 case DATA_TYPE_STRING
:
869 NVP(elem
, string
, const char *, const char *, "'%s'");
872 case DATA_TYPE_BYTE_ARRAY
:
873 NVPA(elem
, byte_array
, uchar_t
, int, "%u");
876 case DATA_TYPE_INT8_ARRAY
:
877 NVPA(elem
, int8_array
, int8_t, int, "%d");
880 case DATA_TYPE_UINT8_ARRAY
:
881 NVPA(elem
, uint8_array
, uint8_t, int, "%u");
884 case DATA_TYPE_INT16_ARRAY
:
885 NVPA(elem
, int16_array
, int16_t, int, "%d");
888 case DATA_TYPE_UINT16_ARRAY
:
889 NVPA(elem
, uint16_array
, uint16_t, int, "%u");
892 case DATA_TYPE_INT32_ARRAY
:
893 NVPA(elem
, int32_array
, int32_t, long, "%ld");
896 case DATA_TYPE_UINT32_ARRAY
:
897 NVPA(elem
, uint32_array
, uint32_t, ulong_t
, "%lu");
900 case DATA_TYPE_INT64_ARRAY
:
901 NVPA(elem
, int64_array
, int64_t, longlong_t
, "%lld");
904 case DATA_TYPE_UINT64_ARRAY
:
905 NVPA(elem
, uint64_array
, uint64_t, u_longlong_t
,
909 case DATA_TYPE_STRING_ARRAY
:
910 NVPA(elem
, string_array
, const char *, const char *,
914 case DATA_TYPE_NVLIST
:
915 (void) nvpair_value_nvlist(elem
, &nvlist_value
);
916 (void) printf("%*s%s:\n", indent
, "",
918 dump_nvlist(nvlist_value
, indent
+ 4);
921 case DATA_TYPE_NVLIST_ARRAY
:
922 (void) nvpair_value_nvlist_array(elem
,
923 &nvlist_array_value
, &count
);
924 for (i
= 0; i
< count
; i
++) {
925 (void) printf("%*s%s[%u]:\n", indent
, "",
926 nvpair_name(elem
), i
);
927 dump_nvlist(nvlist_array_value
[i
], indent
+ 4);
932 (void) printf(dgettext(TEXT_DOMAIN
, "bad config type "
933 "%d for %s\n"), nvpair_type(elem
),
940 * ======================================================================
942 * | Misc private interface. |
944 * ======================================================================
948 * Determine if string 'value' matches 'nvp' value. The 'value' string is
949 * converted, depending on the type of 'nvp', prior to match. For numeric
950 * types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
951 * is an array type, 'ai' is the index into the array against which we are
952 * checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
953 * in a regex_t compilation of value in 'value_regex' to trigger regular
954 * expression string match instead of simple strcmp().
956 * Return 1 on match, 0 on no-match, and -1 on error. If the error is
957 * related to value syntax error and 'ep' is non-NULL, *ep will point into
958 * the 'value' string at the location where the error exists.
960 * NOTE: It may be possible to move the non-regex_t version of this into
961 * common code used by library/kernel/boot.
964 nvpair_value_match_regex(nvpair_t
*nvp
, int ai
,
965 const char *value
, regex_t
*value_regex
, const char **ep
)
974 if ((nvp
== NULL
) || (value
== NULL
))
975 return (-1); /* error fail match - invalid args */
977 /* make sure array and index combination make sense */
978 if ((nvpair_type_is_array(nvp
) && (ai
< 0)) ||
979 (!nvpair_type_is_array(nvp
) && (ai
>= 0)))
980 return (-1); /* error fail match - bad index */
982 /* non-string values should be single 'chunk' */
983 if ((nvpair_type(nvp
) != DATA_TYPE_STRING
) &&
984 (nvpair_type(nvp
) != DATA_TYPE_STRING_ARRAY
)) {
985 value
+= strspn(value
, " \t");
986 evalue
= value
+ strcspn(value
, " \t");
990 return (-1); /* error fail match - syntax */
995 switch (nvpair_type(nvp
)) {
996 case DATA_TYPE_STRING
: {
999 /* check string value for match */
1000 if (nvpair_value_string(nvp
, &val
) == 0) {
1002 if (regexec(value_regex
, val
,
1003 (size_t)0, NULL
, 0) == 0)
1004 return (1); /* match */
1006 if (strcmp(value
, val
) == 0)
1007 return (1); /* match */
1012 case DATA_TYPE_STRING_ARRAY
: {
1013 const char **val_array
;
1015 /* check indexed string value of array for match */
1016 if ((nvpair_value_string_array(nvp
, &val_array
, &a_len
) == 0) &&
1019 if (regexec(value_regex
, val_array
[ai
],
1020 (size_t)0, NULL
, 0) == 0)
1023 if (strcmp(value
, val_array
[ai
]) == 0)
1029 case DATA_TYPE_BYTE
: {
1030 uchar_t val
, val_arg
;
1032 /* scanf uchar_t from value and check for match */
1033 sr
= sscanf(value
, "%c", &val_arg
);
1034 if ((sr
== 1) && (nvpair_value_byte(nvp
, &val
) == 0) &&
1039 case DATA_TYPE_BYTE_ARRAY
: {
1040 uchar_t
*val_array
, val_arg
;
1043 /* check indexed value of array for match */
1044 sr
= sscanf(value
, "%c", &val_arg
);
1046 (nvpair_value_byte_array(nvp
, &val_array
, &a_len
) == 0) &&
1048 (val_array
[ai
] == val_arg
))
1052 case DATA_TYPE_INT8
: {
1053 int8_t val
, val_arg
;
1055 /* scanf int8_t from value and check for match */
1056 sr
= sscanf(value
, "%"SCNi8
, &val_arg
);
1058 (nvpair_value_int8(nvp
, &val
) == 0) &&
1063 case DATA_TYPE_INT8_ARRAY
: {
1064 int8_t *val_array
, val_arg
;
1066 /* check indexed value of array for match */
1067 sr
= sscanf(value
, "%"SCNi8
, &val_arg
);
1069 (nvpair_value_int8_array(nvp
, &val_array
, &a_len
) == 0) &&
1071 (val_array
[ai
] == val_arg
))
1075 case DATA_TYPE_UINT8
: {
1076 uint8_t val
, val_arg
;
1078 /* scanf uint8_t from value and check for match */
1079 sr
= sscanf(value
, "%"SCNi8
, (int8_t *)&val_arg
);
1081 (nvpair_value_uint8(nvp
, &val
) == 0) &&
1086 case DATA_TYPE_UINT8_ARRAY
: {
1087 uint8_t *val_array
, val_arg
;
1089 /* check indexed value of array for match */
1090 sr
= sscanf(value
, "%"SCNi8
, (int8_t *)&val_arg
);
1092 (nvpair_value_uint8_array(nvp
, &val_array
, &a_len
) == 0) &&
1094 (val_array
[ai
] == val_arg
))
1098 case DATA_TYPE_INT16
: {
1099 int16_t val
, val_arg
;
1101 /* scanf int16_t from value and check for match */
1102 sr
= sscanf(value
, "%"SCNi16
, &val_arg
);
1104 (nvpair_value_int16(nvp
, &val
) == 0) &&
1109 case DATA_TYPE_INT16_ARRAY
: {
1110 int16_t *val_array
, val_arg
;
1112 /* check indexed value of array for match */
1113 sr
= sscanf(value
, "%"SCNi16
, &val_arg
);
1115 (nvpair_value_int16_array(nvp
, &val_array
, &a_len
) == 0) &&
1117 (val_array
[ai
] == val_arg
))
1121 case DATA_TYPE_UINT16
: {
1122 uint16_t val
, val_arg
;
1124 /* scanf uint16_t from value and check for match */
1125 sr
= sscanf(value
, "%"SCNi16
, (int16_t *)&val_arg
);
1127 (nvpair_value_uint16(nvp
, &val
) == 0) &&
1132 case DATA_TYPE_UINT16_ARRAY
: {
1133 uint16_t *val_array
, val_arg
;
1135 /* check indexed value of array for match */
1136 sr
= sscanf(value
, "%"SCNi16
, (int16_t *)&val_arg
);
1138 (nvpair_value_uint16_array(nvp
, &val_array
, &a_len
) == 0) &&
1140 (val_array
[ai
] == val_arg
))
1144 case DATA_TYPE_INT32
: {
1145 int32_t val
, val_arg
;
1147 /* scanf int32_t from value and check for match */
1148 sr
= sscanf(value
, "%"SCNi32
, &val_arg
);
1150 (nvpair_value_int32(nvp
, &val
) == 0) &&
1155 case DATA_TYPE_INT32_ARRAY
: {
1156 int32_t *val_array
, val_arg
;
1158 /* check indexed value of array for match */
1159 sr
= sscanf(value
, "%"SCNi32
, &val_arg
);
1161 (nvpair_value_int32_array(nvp
, &val_array
, &a_len
) == 0) &&
1163 (val_array
[ai
] == val_arg
))
1167 case DATA_TYPE_UINT32
: {
1168 uint32_t val
, val_arg
;
1170 /* scanf uint32_t from value and check for match */
1171 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1173 (nvpair_value_uint32(nvp
, &val
) == 0) &&
1178 case DATA_TYPE_UINT32_ARRAY
: {
1179 uint32_t *val_array
, val_arg
;
1181 /* check indexed value of array for match */
1182 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1184 (nvpair_value_uint32_array(nvp
, &val_array
, &a_len
) == 0) &&
1186 (val_array
[ai
] == val_arg
))
1190 case DATA_TYPE_INT64
: {
1191 int64_t val
, val_arg
;
1193 /* scanf int64_t from value and check for match */
1194 sr
= sscanf(value
, "%"SCNi64
, &val_arg
);
1196 (nvpair_value_int64(nvp
, &val
) == 0) &&
1201 case DATA_TYPE_INT64_ARRAY
: {
1202 int64_t *val_array
, val_arg
;
1204 /* check indexed value of array for match */
1205 sr
= sscanf(value
, "%"SCNi64
, &val_arg
);
1207 (nvpair_value_int64_array(nvp
, &val_array
, &a_len
) == 0) &&
1209 (val_array
[ai
] == val_arg
))
1213 case DATA_TYPE_UINT64
: {
1214 uint64_t val_arg
, val
;
1216 /* scanf uint64_t from value and check for match */
1217 sr
= sscanf(value
, "%"SCNi64
, (int64_t *)&val_arg
);
1219 (nvpair_value_uint64(nvp
, &val
) == 0) &&
1224 case DATA_TYPE_UINT64_ARRAY
: {
1225 uint64_t *val_array
, val_arg
;
1227 /* check indexed value of array for match */
1228 sr
= sscanf(value
, "%"SCNi64
, (int64_t *)&val_arg
);
1230 (nvpair_value_uint64_array(nvp
, &val_array
, &a_len
) == 0) &&
1232 (val_array
[ai
] == val_arg
))
1236 case DATA_TYPE_BOOLEAN_VALUE
: {
1240 /* scanf boolean_t from value and check for match */
1241 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1243 (nvpair_value_boolean_value(nvp
, &val
) == 0) &&
1248 case DATA_TYPE_BOOLEAN_ARRAY
: {
1249 boolean_t
*val_array
;
1252 /* check indexed value of array for match */
1253 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1255 (nvpair_value_boolean_array(nvp
,
1256 &val_array
, &a_len
) == 0) &&
1258 (val_array
[ai
] == val_arg
))
1262 case DATA_TYPE_HRTIME
:
1263 case DATA_TYPE_NVLIST
:
1264 case DATA_TYPE_NVLIST_ARRAY
:
1265 case DATA_TYPE_BOOLEAN
:
1266 case DATA_TYPE_DOUBLE
:
1267 case DATA_TYPE_UNKNOWN
:
1270 * unknown/unsupported data type
1272 return (-1); /* error fail match */
1276 * check to see if sscanf failed conversion, return approximate
1277 * pointer to problem
1282 return (-1); /* error fail match - syntax */
1285 return (0); /* fail match */
1289 nvpair_value_match(nvpair_t
*nvp
, int ai
, const char *value
, const char **ep
)
1291 return (nvpair_value_match_regex(nvp
, ai
, value
, NULL
, ep
));