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
, 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
, 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
, char *, 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
, char *, 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
= (strstr(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
, 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
, 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
, 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
, char *, 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
, char *, char *, "'%s'");
913 case DATA_TYPE_NVLIST
:
914 (void) nvpair_value_nvlist(elem
, &nvlist_value
);
915 (void) printf("%*s%s:\n", indent
, "",
917 dump_nvlist(nvlist_value
, indent
+ 4);
920 case DATA_TYPE_NVLIST_ARRAY
:
921 (void) nvpair_value_nvlist_array(elem
,
922 &nvlist_array_value
, &count
);
923 for (i
= 0; i
< count
; i
++) {
924 (void) printf("%*s%s[%u]:\n", indent
, "",
925 nvpair_name(elem
), i
);
926 dump_nvlist(nvlist_array_value
[i
], indent
+ 4);
931 (void) printf(dgettext(TEXT_DOMAIN
, "bad config type "
932 "%d for %s\n"), nvpair_type(elem
),
939 * ======================================================================
941 * | Misc private interface. |
943 * ======================================================================
947 * Determine if string 'value' matches 'nvp' value. The 'value' string is
948 * converted, depending on the type of 'nvp', prior to match. For numeric
949 * types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
950 * is an array type, 'ai' is the index into the array against which we are
951 * checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
952 * in a regex_t compilation of value in 'value_regex' to trigger regular
953 * expression string match instead of simple strcmp().
955 * Return 1 on match, 0 on no-match, and -1 on error. If the error is
956 * related to value syntax error and 'ep' is non-NULL, *ep will point into
957 * the 'value' string at the location where the error exists.
959 * NOTE: It may be possible to move the non-regex_t version of this into
960 * common code used by library/kernel/boot.
963 nvpair_value_match_regex(nvpair_t
*nvp
, int ai
,
964 char *value
, regex_t
*value_regex
, char **ep
)
973 if ((nvp
== NULL
) || (value
== NULL
))
974 return (-1); /* error fail match - invalid args */
976 /* make sure array and index combination make sense */
977 if ((nvpair_type_is_array(nvp
) && (ai
< 0)) ||
978 (!nvpair_type_is_array(nvp
) && (ai
>= 0)))
979 return (-1); /* error fail match - bad index */
981 /* non-string values should be single 'chunk' */
982 if ((nvpair_type(nvp
) != DATA_TYPE_STRING
) &&
983 (nvpair_type(nvp
) != DATA_TYPE_STRING_ARRAY
)) {
984 value
+= strspn(value
, " \t");
985 evalue
= value
+ strcspn(value
, " \t");
989 return (-1); /* error fail match - syntax */
994 switch (nvpair_type(nvp
)) {
995 case DATA_TYPE_STRING
: {
998 /* check string value for match */
999 if (nvpair_value_string(nvp
, &val
) == 0) {
1001 if (regexec(value_regex
, val
,
1002 (size_t)0, NULL
, 0) == 0)
1003 return (1); /* match */
1005 if (strcmp(value
, val
) == 0)
1006 return (1); /* match */
1011 case DATA_TYPE_STRING_ARRAY
: {
1014 /* check indexed string value of array for match */
1015 if ((nvpair_value_string_array(nvp
, &val_array
, &a_len
) == 0) &&
1018 if (regexec(value_regex
, val_array
[ai
],
1019 (size_t)0, NULL
, 0) == 0)
1022 if (strcmp(value
, val_array
[ai
]) == 0)
1028 case DATA_TYPE_BYTE
: {
1029 uchar_t val
, val_arg
;
1031 /* scanf uchar_t from value and check for match */
1032 sr
= sscanf(value
, "%c", &val_arg
);
1033 if ((sr
== 1) && (nvpair_value_byte(nvp
, &val
) == 0) &&
1038 case DATA_TYPE_BYTE_ARRAY
: {
1039 uchar_t
*val_array
, val_arg
;
1042 /* check indexed value of array for match */
1043 sr
= sscanf(value
, "%c", &val_arg
);
1045 (nvpair_value_byte_array(nvp
, &val_array
, &a_len
) == 0) &&
1047 (val_array
[ai
] == val_arg
))
1051 case DATA_TYPE_INT8
: {
1052 int8_t val
, val_arg
;
1054 /* scanf int8_t from value and check for match */
1055 sr
= sscanf(value
, "%"SCNi8
, &val_arg
);
1057 (nvpair_value_int8(nvp
, &val
) == 0) &&
1062 case DATA_TYPE_INT8_ARRAY
: {
1063 int8_t *val_array
, val_arg
;
1065 /* check indexed value of array for match */
1066 sr
= sscanf(value
, "%"SCNi8
, &val_arg
);
1068 (nvpair_value_int8_array(nvp
, &val_array
, &a_len
) == 0) &&
1070 (val_array
[ai
] == val_arg
))
1074 case DATA_TYPE_UINT8
: {
1075 uint8_t val
, val_arg
;
1077 /* scanf uint8_t from value and check for match */
1078 sr
= sscanf(value
, "%"SCNi8
, (int8_t *)&val_arg
);
1080 (nvpair_value_uint8(nvp
, &val
) == 0) &&
1085 case DATA_TYPE_UINT8_ARRAY
: {
1086 uint8_t *val_array
, val_arg
;
1088 /* check indexed value of array for match */
1089 sr
= sscanf(value
, "%"SCNi8
, (int8_t *)&val_arg
);
1091 (nvpair_value_uint8_array(nvp
, &val_array
, &a_len
) == 0) &&
1093 (val_array
[ai
] == val_arg
))
1097 case DATA_TYPE_INT16
: {
1098 int16_t val
, val_arg
;
1100 /* scanf int16_t from value and check for match */
1101 sr
= sscanf(value
, "%"SCNi16
, &val_arg
);
1103 (nvpair_value_int16(nvp
, &val
) == 0) &&
1108 case DATA_TYPE_INT16_ARRAY
: {
1109 int16_t *val_array
, val_arg
;
1111 /* check indexed value of array for match */
1112 sr
= sscanf(value
, "%"SCNi16
, &val_arg
);
1114 (nvpair_value_int16_array(nvp
, &val_array
, &a_len
) == 0) &&
1116 (val_array
[ai
] == val_arg
))
1120 case DATA_TYPE_UINT16
: {
1121 uint16_t val
, val_arg
;
1123 /* scanf uint16_t from value and check for match */
1124 sr
= sscanf(value
, "%"SCNi16
, (int16_t *)&val_arg
);
1126 (nvpair_value_uint16(nvp
, &val
) == 0) &&
1131 case DATA_TYPE_UINT16_ARRAY
: {
1132 uint16_t *val_array
, val_arg
;
1134 /* check indexed value of array for match */
1135 sr
= sscanf(value
, "%"SCNi16
, (int16_t *)&val_arg
);
1137 (nvpair_value_uint16_array(nvp
, &val_array
, &a_len
) == 0) &&
1139 (val_array
[ai
] == val_arg
))
1143 case DATA_TYPE_INT32
: {
1144 int32_t val
, val_arg
;
1146 /* scanf int32_t from value and check for match */
1147 sr
= sscanf(value
, "%"SCNi32
, &val_arg
);
1149 (nvpair_value_int32(nvp
, &val
) == 0) &&
1154 case DATA_TYPE_INT32_ARRAY
: {
1155 int32_t *val_array
, val_arg
;
1157 /* check indexed value of array for match */
1158 sr
= sscanf(value
, "%"SCNi32
, &val_arg
);
1160 (nvpair_value_int32_array(nvp
, &val_array
, &a_len
) == 0) &&
1162 (val_array
[ai
] == val_arg
))
1166 case DATA_TYPE_UINT32
: {
1167 uint32_t val
, val_arg
;
1169 /* scanf uint32_t from value and check for match */
1170 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1172 (nvpair_value_uint32(nvp
, &val
) == 0) &&
1177 case DATA_TYPE_UINT32_ARRAY
: {
1178 uint32_t *val_array
, val_arg
;
1180 /* check indexed value of array for match */
1181 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1183 (nvpair_value_uint32_array(nvp
, &val_array
, &a_len
) == 0) &&
1185 (val_array
[ai
] == val_arg
))
1189 case DATA_TYPE_INT64
: {
1190 int64_t val
, val_arg
;
1192 /* scanf int64_t from value and check for match */
1193 sr
= sscanf(value
, "%"SCNi64
, &val_arg
);
1195 (nvpair_value_int64(nvp
, &val
) == 0) &&
1200 case DATA_TYPE_INT64_ARRAY
: {
1201 int64_t *val_array
, val_arg
;
1203 /* check indexed value of array for match */
1204 sr
= sscanf(value
, "%"SCNi64
, &val_arg
);
1206 (nvpair_value_int64_array(nvp
, &val_array
, &a_len
) == 0) &&
1208 (val_array
[ai
] == val_arg
))
1212 case DATA_TYPE_UINT64
: {
1213 uint64_t val_arg
, val
;
1215 /* scanf uint64_t from value and check for match */
1216 sr
= sscanf(value
, "%"SCNi64
, (int64_t *)&val_arg
);
1218 (nvpair_value_uint64(nvp
, &val
) == 0) &&
1223 case DATA_TYPE_UINT64_ARRAY
: {
1224 uint64_t *val_array
, val_arg
;
1226 /* check indexed value of array for match */
1227 sr
= sscanf(value
, "%"SCNi64
, (int64_t *)&val_arg
);
1229 (nvpair_value_uint64_array(nvp
, &val_array
, &a_len
) == 0) &&
1231 (val_array
[ai
] == val_arg
))
1235 case DATA_TYPE_BOOLEAN_VALUE
: {
1239 /* scanf boolean_t from value and check for match */
1240 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1242 (nvpair_value_boolean_value(nvp
, &val
) == 0) &&
1247 case DATA_TYPE_BOOLEAN_ARRAY
: {
1248 boolean_t
*val_array
;
1251 /* check indexed value of array for match */
1252 sr
= sscanf(value
, "%"SCNi32
, (int32_t *)&val_arg
);
1254 (nvpair_value_boolean_array(nvp
,
1255 &val_array
, &a_len
) == 0) &&
1257 (val_array
[ai
] == val_arg
))
1261 case DATA_TYPE_HRTIME
:
1262 case DATA_TYPE_NVLIST
:
1263 case DATA_TYPE_NVLIST_ARRAY
:
1264 case DATA_TYPE_BOOLEAN
:
1265 case DATA_TYPE_DOUBLE
:
1266 case DATA_TYPE_UNKNOWN
:
1269 * unknown/unsupported data type
1271 return (-1); /* error fail match */
1275 * check to see if sscanf failed conversion, return approximate
1276 * pointer to problem
1281 return (-1); /* error fail match - syntax */
1284 return (0); /* fail match */
1288 nvpair_value_match(nvpair_t
*nvp
, int ai
, char *value
, char **ep
)
1290 return (nvpair_value_match_regex(nvp
, ai
, value
, NULL
, ep
));