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 http://www.opensolaris.org/os/licensing.
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.
31 #include "libnvpair.h"
34 * libnvpair - A tools library for manipulating <name, value> pairs.
36 * This library provides routines packing an unpacking nv pairs
37 * for transporting data across process boundaries, transporting
38 * between kernel and userland, and possibly saving onto disk files.
42 * Print control structure.
45 #define DEFINEOP(opname, vtype) \
47 int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
48 const char *, vtype); \
52 #define DEFINEARROP(opname, vtype) \
54 int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
55 const char *, vtype, uint_t); \
59 struct nvlist_printops {
60 DEFINEOP(print_boolean, int);
61 DEFINEOP(print_boolean_value, boolean_t);
62 DEFINEOP(print_byte, uchar_t);
63 DEFINEOP(print_int8, int8_t);
64 DEFINEOP(print_uint8, uint8_t);
65 DEFINEOP(print_int16, int16_t);
66 DEFINEOP(print_uint16, uint16_t);
67 DEFINEOP(print_int32, int32_t);
68 DEFINEOP(print_uint32, uint32_t);
69 DEFINEOP(print_int64, int64_t);
70 DEFINEOP(print_uint64, uint64_t);
71 DEFINEOP(print_double, double);
72 DEFINEOP(print_string, char *);
73 DEFINEOP(print_hrtime, hrtime_t);
74 DEFINEOP(print_nvlist, nvlist_t *);
75 DEFINEARROP(print_boolean_array, boolean_t *);
76 DEFINEARROP(print_byte_array, uchar_t *);
77 DEFINEARROP(print_int8_array, int8_t *);
78 DEFINEARROP(print_uint8_array, uint8_t *);
79 DEFINEARROP(print_int16_array, int16_t *);
80 DEFINEARROP(print_uint16_array, uint16_t *);
81 DEFINEARROP(print_int32_array, int32_t *);
82 DEFINEARROP(print_uint32_array, uint32_t *);
83 DEFINEARROP(print_int64_array, int64_t *);
84 DEFINEARROP(print_uint64_array, uint64_t *);
85 DEFINEARROP(print_string_array, char **);
86 DEFINEARROP(print_nvlist_array, nvlist_t **);
89 struct nvlist_prtctl {
90 FILE *nvprt_fp; /* output destination */
91 enum nvlist_indent_mode nvprt_indent_mode; /* see above */
92 int nvprt_indent; /* absolute indent, or tab depth */
93 int nvprt_indentinc; /* indent or tab increment */
94 const char *nvprt_nmfmt; /* member name format, max one %s */
95 const char *nvprt_eomfmt; /* after member format, e.g. "\n" */
96 const char *nvprt_btwnarrfmt; /* between array members */
97 int nvprt_btwnarrfmt_nl; /* nvprt_eoamfmt includes newline? */
98 struct nvlist_printops *nvprt_dfltops;
99 struct nvlist_printops *nvprt_custops;
102 #define DFLTPRTOP(pctl, type) \
103 ((pctl)->nvprt_dfltops->print_##type.op)
105 #define DFLTPRTOPARG(pctl, type) \
106 ((pctl)->nvprt_dfltops->print_##type.arg)
108 #define CUSTPRTOP(pctl, type) \
109 ((pctl)->nvprt_custops->print_##type.op)
111 #define CUSTPRTOPARG(pctl, type) \
112 ((pctl)->nvprt_custops->print_##type.arg)
114 #define RENDER(pctl, type, nvl, name, val) \
117 if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
118 done = CUSTPRTOP(pctl, type)(pctl, \
119 CUSTPRTOPARG(pctl, type), nvl, name, val); \
122 (void) DFLTPRTOP(pctl, type)(pctl, \
123 DFLTPRTOPARG(pctl, type), nvl, name, val); \
125 (void) fprintf(pctl->nvprt_fp, pctl->nvprt_eomfmt); \
128 #define ARENDER(pctl, type, nvl, name, arrp, count) \
131 if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
132 done = CUSTPRTOP(pctl, type)(pctl, \
133 CUSTPRTOPARG(pctl, type), nvl, name, arrp, count); \
136 (void) DFLTPRTOP(pctl, type)(pctl, \
137 DFLTPRTOPARG(pctl, type), nvl, name, arrp, count); \
139 (void) fprintf(pctl->nvprt_fp, pctl->nvprt_eomfmt); \
142 static void nvlist_print_with_indent(nvlist_t *, nvlist_prtctl_t);
145 * ======================================================================
149 * ======================================================================
153 indent(nvlist_prtctl_t pctl, int onemore)
157 switch (pctl->nvprt_indent_mode) {
158 case NVLIST_INDENT_ABS:
159 (void) fprintf(pctl->nvprt_fp, "%*s",
160 pctl->nvprt_indent + onemore * pctl->nvprt_indentinc, "");
163 case NVLIST_INDENT_TABBED:
164 depth = pctl->nvprt_indent + onemore;
166 (void) fprintf(pctl->nvprt_fp, "\t");
171 * ======================================================================
173 * | Default nvlist member rendering functions. |
175 * ======================================================================
179 * Generate functions to print single-valued nvlist members.
181 * type_and_variant - suffix to form function name
182 * vtype - C type for the member value
183 * ptype - C type to cast value to for printing
184 * vfmt - format string for pair value, e.g "%d" or "0x%llx"
187 #define NVLIST_PRTFUNC(type_and_variant, vtype, ptype, vfmt) \
189 nvprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
190 nvlist_t *nvl, const char *name, vtype value) \
192 FILE *fp = pctl->nvprt_fp; \
193 NOTE(ARGUNUSED(private)) \
194 NOTE(ARGUNUSED(nvl)) \
196 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
197 (void) fprintf(fp, vfmt, (ptype)value); \
201 NVLIST_PRTFUNC(boolean, int, int, "%d")
202 NVLIST_PRTFUNC(boolean_value, boolean_t, int, "%d")
203 NVLIST_PRTFUNC(byte, uchar_t, uchar_t, "0x%2.2x")
204 NVLIST_PRTFUNC(int8, int8_t, int, "%d")
205 NVLIST_PRTFUNC(uint8, uint8_t, uint8_t, "0x%x")
206 NVLIST_PRTFUNC(int16, int16_t, int16_t, "%d")
207 NVLIST_PRTFUNC(uint16, uint16_t, uint16_t, "0x%x")
208 NVLIST_PRTFUNC(int32, int32_t, int32_t, "%d")
209 NVLIST_PRTFUNC(uint32, uint32_t, uint32_t, "0x%x")
210 NVLIST_PRTFUNC(int64, int64_t, longlong_t, "%lld")
211 NVLIST_PRTFUNC(uint64, uint64_t, u_longlong_t, "0x%llx")
212 NVLIST_PRTFUNC(double, double, double, "0x%llf")
213 NVLIST_PRTFUNC(string, char *, char *, "%s")
214 NVLIST_PRTFUNC(hrtime, hrtime_t, hrtime_t, "0x%llx")
217 * Generate functions to print array-valued nvlist members.
220 #define NVLIST_ARRPRTFUNC(type_and_variant, vtype, ptype, vfmt) \
222 nvaprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
223 nvlist_t *nvl, const char *name, vtype *valuep, uint_t count) \
225 FILE *fp = pctl->nvprt_fp; \
227 NOTE(ARGUNUSED(private)) \
228 NOTE(ARGUNUSED(nvl)) \
229 for (i = 0; i < count; i++) { \
230 if (i == 0 || pctl->nvprt_btwnarrfmt_nl) { \
232 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
233 if (pctl->nvprt_btwnarrfmt_nl) \
234 (void) fprintf(fp, "[%d]: ", i); \
237 (void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
238 (void) fprintf(fp, vfmt, (ptype)valuep[i]); \
243 NVLIST_ARRPRTFUNC(boolean_array, boolean_t, boolean_t, "%d")
244 NVLIST_ARRPRTFUNC(byte_array, uchar_t, uchar_t, "0x%2.2x")
245 NVLIST_ARRPRTFUNC(int8_array, int8_t, int8_t, "%d")
246 NVLIST_ARRPRTFUNC(uint8_array, uint8_t, uint8_t, "0x%x")
247 NVLIST_ARRPRTFUNC(int16_array, int16_t, int16_t, "%d")
248 NVLIST_ARRPRTFUNC(uint16_array, uint16_t, uint16_t, "0x%x")
249 NVLIST_ARRPRTFUNC(int32_array, int32_t, int32_t, "%d")
250 NVLIST_ARRPRTFUNC(uint32_array, uint32_t, uint32_t, "0x%x")
251 NVLIST_ARRPRTFUNC(int64_array, int64_t, longlong_t, "%lld")
252 NVLIST_ARRPRTFUNC(uint64_array, uint64_t, u_longlong_t, "0x%llx")
253 NVLIST_ARRPRTFUNC(string_array, char *, char *, "%s")
257 nvprint_nvlist(nvlist_prtctl_t pctl, void *private,
258 nvlist_t *nvl, const char *name, nvlist_t *value)
260 FILE *fp = pctl->nvprt_fp;
263 (void) fprintf(fp, "%s = (embedded nvlist)\n", name);
265 pctl->nvprt_indent += pctl->nvprt_indentinc;
266 nvlist_print_with_indent(value, pctl);
267 pctl->nvprt_indent -= pctl->nvprt_indentinc;
270 (void) fprintf(fp, "(end %s)\n", name);
277 nvaprint_nvlist_array(nvlist_prtctl_t pctl, void *private,
278 nvlist_t *nvl, const char *name, nvlist_t **valuep, uint_t count)
280 FILE *fp = pctl->nvprt_fp;
284 (void) fprintf(fp, "%s = (array of embedded nvlists)\n", name);
286 for (i = 0; i < count; i++) {
288 (void) fprintf(fp, "(start %s[%d])\n", name, i);
290 pctl->nvprt_indent += pctl->nvprt_indentinc;
291 nvlist_print_with_indent(valuep[i], pctl);
292 pctl->nvprt_indent -= pctl->nvprt_indentinc;
295 (void) fprintf(fp, "(end %s[%d])\n", name, i);
302 * ======================================================================
304 * | Interfaces that allow control over formatting. |
306 * ======================================================================
310 nvlist_prtctl_setdest(nvlist_prtctl_t pctl, FILE *fp)
316 nvlist_prtctl_getdest(nvlist_prtctl_t pctl)
318 return (pctl->nvprt_fp);
323 nvlist_prtctl_setindent(nvlist_prtctl_t pctl, enum nvlist_indent_mode mode,
326 if (mode < NVLIST_INDENT_ABS || mode > NVLIST_INDENT_TABBED)
327 mode = NVLIST_INDENT_TABBED;
335 pctl->nvprt_indent_mode = mode;
336 pctl->nvprt_indent = start;
337 pctl->nvprt_indentinc = inc;
341 nvlist_prtctl_doindent(nvlist_prtctl_t pctl, int onemore)
343 indent(pctl, onemore);
348 nvlist_prtctl_setfmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which,
352 case NVLIST_FMT_MEMBER_NAME:
355 pctl->nvprt_nmfmt = fmt;
358 case NVLIST_FMT_MEMBER_POSTAMBLE:
361 pctl->nvprt_eomfmt = fmt;
364 case NVLIST_FMT_BTWN_ARRAY:
366 pctl->nvprt_btwnarrfmt = " ";
367 pctl->nvprt_btwnarrfmt_nl = 0;
369 pctl->nvprt_btwnarrfmt = fmt;
370 pctl->nvprt_btwnarrfmt_nl = (strstr(fmt, "\n") != NULL);
381 nvlist_prtctl_dofmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which, ...)
383 FILE *fp = pctl->nvprt_fp;
390 case NVLIST_FMT_MEMBER_NAME:
391 name = va_arg(ap, char *);
392 (void) fprintf(fp, pctl->nvprt_nmfmt, name);
395 case NVLIST_FMT_MEMBER_POSTAMBLE:
396 (void) fprintf(fp, pctl->nvprt_eomfmt);
399 case NVLIST_FMT_BTWN_ARRAY:
400 (void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
411 * ======================================================================
413 * | Interfaces to allow appointment of replacement rendering functions.|
415 * ======================================================================
418 #define NVLIST_PRINTCTL_REPLACE(type, vtype) \
420 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
421 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype), \
424 CUSTPRTOP(pctl, type) = func; \
425 CUSTPRTOPARG(pctl, type) = private; \
428 NVLIST_PRINTCTL_REPLACE(boolean, int)
429 NVLIST_PRINTCTL_REPLACE(boolean_value, boolean_t)
430 NVLIST_PRINTCTL_REPLACE(byte, uchar_t)
431 NVLIST_PRINTCTL_REPLACE(int8, int8_t)
432 NVLIST_PRINTCTL_REPLACE(uint8, uint8_t)
433 NVLIST_PRINTCTL_REPLACE(int16, int16_t)
434 NVLIST_PRINTCTL_REPLACE(uint16, uint16_t)
435 NVLIST_PRINTCTL_REPLACE(int32, int32_t)
436 NVLIST_PRINTCTL_REPLACE(uint32, uint32_t)
437 NVLIST_PRINTCTL_REPLACE(int64, int64_t)
438 NVLIST_PRINTCTL_REPLACE(uint64, uint64_t)
439 NVLIST_PRINTCTL_REPLACE(double, double)
440 NVLIST_PRINTCTL_REPLACE(string, char *)
441 NVLIST_PRINTCTL_REPLACE(hrtime, hrtime_t)
442 NVLIST_PRINTCTL_REPLACE(nvlist, nvlist_t *)
444 #define NVLIST_PRINTCTL_AREPLACE(type, vtype) \
446 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
447 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, \
448 uint_t), void *private) \
450 CUSTPRTOP(pctl, type) = func; \
451 CUSTPRTOPARG(pctl, type) = private; \
454 NVLIST_PRINTCTL_AREPLACE(boolean_array, boolean_t *)
455 NVLIST_PRINTCTL_AREPLACE(byte_array, uchar_t *)
456 NVLIST_PRINTCTL_AREPLACE(int8_array, int8_t *)
457 NVLIST_PRINTCTL_AREPLACE(uint8_array, uint8_t *)
458 NVLIST_PRINTCTL_AREPLACE(int16_array, int16_t *)
459 NVLIST_PRINTCTL_AREPLACE(uint16_array, uint16_t *)
460 NVLIST_PRINTCTL_AREPLACE(int32_array, int32_t *)
461 NVLIST_PRINTCTL_AREPLACE(uint32_array, uint32_t *)
462 NVLIST_PRINTCTL_AREPLACE(int64_array, int64_t *)
463 NVLIST_PRINTCTL_AREPLACE(uint64_array, uint64_t *)
464 NVLIST_PRINTCTL_AREPLACE(string_array, char **)
465 NVLIST_PRINTCTL_AREPLACE(nvlist_array, nvlist_t **)
468 * ======================================================================
470 * | Interfaces to manage nvlist_prtctl_t cookies. |
472 * ======================================================================
476 static const struct nvlist_printops defprtops = {
477 { nvprint_boolean, NULL },
478 { nvprint_boolean_value, NULL },
479 { nvprint_byte, NULL },
480 { nvprint_int8, NULL },
481 { nvprint_uint8, NULL },
482 { nvprint_int16, NULL },
483 { nvprint_uint16, NULL },
484 { nvprint_int32, NULL },
485 { nvprint_uint32, NULL },
486 { nvprint_int64, NULL },
487 { nvprint_uint64, NULL },
488 { nvprint_double, NULL },
489 { nvprint_string, NULL },
490 { nvprint_hrtime, NULL },
491 { nvprint_nvlist, NULL },
492 { nvaprint_boolean_array, NULL },
493 { nvaprint_byte_array, NULL },
494 { nvaprint_int8_array, NULL },
495 { nvaprint_uint8_array, NULL },
496 { nvaprint_int16_array, NULL },
497 { nvaprint_uint16_array, NULL },
498 { nvaprint_int32_array, NULL },
499 { nvaprint_uint32_array, NULL },
500 { nvaprint_int64_array, NULL },
501 { nvaprint_uint64_array, NULL },
502 { nvaprint_string_array, NULL },
503 { nvaprint_nvlist_array, NULL },
507 prtctl_defaults(FILE *fp, struct nvlist_prtctl *pctl,
508 struct nvlist_printops *ops)
511 pctl->nvprt_indent_mode = NVLIST_INDENT_TABBED;
512 pctl->nvprt_indent = 0;
513 pctl->nvprt_indentinc = 1;
514 pctl->nvprt_nmfmt = "%s = ";
515 pctl->nvprt_eomfmt = "\n";
516 pctl->nvprt_btwnarrfmt = " ";
517 pctl->nvprt_btwnarrfmt_nl = 0;
519 pctl->nvprt_dfltops = (struct nvlist_printops *)&defprtops;
520 pctl->nvprt_custops = ops;
524 nvlist_prtctl_alloc(void)
526 struct nvlist_prtctl *pctl;
527 struct nvlist_printops *ops;
529 if ((pctl = malloc(sizeof (*pctl))) == NULL)
532 if ((ops = calloc(1, sizeof (*ops))) == NULL) {
537 prtctl_defaults(stdout, pctl, ops);
543 nvlist_prtctl_free(nvlist_prtctl_t pctl)
546 free(pctl->nvprt_custops);
552 * ======================================================================
554 * | Top-level print request interfaces. |
556 * ======================================================================
560 * nvlist_print - Prints elements in an event buffer
563 nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl)
565 FILE *fp = pctl->nvprt_fp;
574 (void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl));
576 nvp = nvlist_next_nvpair(nvl, NULL);
579 data_type_t type = nvpair_type(nvp);
581 name = nvpair_name(nvp);
585 case DATA_TYPE_BOOLEAN: {
586 RENDER(pctl, boolean, nvl, name, 1);
589 case DATA_TYPE_BOOLEAN_VALUE: {
591 (void) nvpair_value_boolean_value(nvp, &val);
592 RENDER(pctl, boolean_value, nvl, name, val);
595 case DATA_TYPE_BYTE: {
597 (void) nvpair_value_byte(nvp, &val);
598 RENDER(pctl, byte, nvl, name, val);
601 case DATA_TYPE_INT8: {
603 (void) nvpair_value_int8(nvp, &val);
604 RENDER(pctl, int8, nvl, name, val);
607 case DATA_TYPE_UINT8: {
609 (void) nvpair_value_uint8(nvp, &val);
610 RENDER(pctl, uint8, nvl, name, val);
613 case DATA_TYPE_INT16: {
615 (void) nvpair_value_int16(nvp, &val);
616 RENDER(pctl, int16, nvl, name, val);
619 case DATA_TYPE_UINT16: {
621 (void) nvpair_value_uint16(nvp, &val);
622 RENDER(pctl, uint16, nvl, name, val);
625 case DATA_TYPE_INT32: {
627 (void) nvpair_value_int32(nvp, &val);
628 RENDER(pctl, int32, nvl, name, val);
631 case DATA_TYPE_UINT32: {
633 (void) nvpair_value_uint32(nvp, &val);
634 RENDER(pctl, uint32, nvl, name, val);
637 case DATA_TYPE_INT64: {
639 (void) nvpair_value_int64(nvp, &val);
640 RENDER(pctl, int64, nvl, name, val);
643 case DATA_TYPE_UINT64: {
645 (void) nvpair_value_uint64(nvp, &val);
646 RENDER(pctl, uint64, nvl, name, val);
649 case DATA_TYPE_DOUBLE: {
651 (void) nvpair_value_double(nvp, &val);
652 RENDER(pctl, double, nvl, name, val);
655 case DATA_TYPE_STRING: {
657 (void) nvpair_value_string(nvp, &val);
658 RENDER(pctl, string, nvl, name, val);
661 case DATA_TYPE_BOOLEAN_ARRAY: {
663 (void) nvpair_value_boolean_array(nvp, &val, &nelem);
664 ARENDER(pctl, boolean_array, nvl, name, val, nelem);
667 case DATA_TYPE_BYTE_ARRAY: {
669 (void) nvpair_value_byte_array(nvp, &val, &nelem);
670 ARENDER(pctl, byte_array, nvl, name, val, nelem);
673 case DATA_TYPE_INT8_ARRAY: {
675 (void) nvpair_value_int8_array(nvp, &val, &nelem);
676 ARENDER(pctl, int8_array, nvl, name, val, nelem);
679 case DATA_TYPE_UINT8_ARRAY: {
681 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
682 ARENDER(pctl, uint8_array, nvl, name, val, nelem);
685 case DATA_TYPE_INT16_ARRAY: {
687 (void) nvpair_value_int16_array(nvp, &val, &nelem);
688 ARENDER(pctl, int16_array, nvl, name, val, nelem);
691 case DATA_TYPE_UINT16_ARRAY: {
693 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
694 ARENDER(pctl, uint16_array, nvl, name, val, nelem);
697 case DATA_TYPE_INT32_ARRAY: {
699 (void) nvpair_value_int32_array(nvp, &val, &nelem);
700 ARENDER(pctl, int32_array, nvl, name, val, nelem);
703 case DATA_TYPE_UINT32_ARRAY: {
705 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
706 ARENDER(pctl, uint32_array, nvl, name, val, nelem);
709 case DATA_TYPE_INT64_ARRAY: {
711 (void) nvpair_value_int64_array(nvp, &val, &nelem);
712 ARENDER(pctl, int64_array, nvl, name, val, nelem);
715 case DATA_TYPE_UINT64_ARRAY: {
717 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
718 ARENDER(pctl, uint64_array, nvl, name, val, nelem);
721 case DATA_TYPE_STRING_ARRAY: {
723 (void) nvpair_value_string_array(nvp, &val, &nelem);
724 ARENDER(pctl, string_array, nvl, name, val, nelem);
727 case DATA_TYPE_HRTIME: {
729 (void) nvpair_value_hrtime(nvp, &val);
730 RENDER(pctl, hrtime, nvl, name, val);
733 case DATA_TYPE_NVLIST: {
735 (void) nvpair_value_nvlist(nvp, &val);
736 RENDER(pctl, nvlist, nvl, name, val);
739 case DATA_TYPE_NVLIST_ARRAY: {
741 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
742 ARENDER(pctl, nvlist_array, nvl, name, val, nelem);
746 (void) fprintf(fp, " unknown data type (%d)", type);
749 nvp = nvlist_next_nvpair(nvl, nvp);
754 nvlist_print(FILE *fp, nvlist_t *nvl)
756 struct nvlist_prtctl pc;
758 prtctl_defaults(fp, &pc, NULL);
759 nvlist_print_with_indent(nvl, &pc);
763 nvlist_prt(nvlist_t *nvl, nvlist_prtctl_t pctl)
765 nvlist_print_with_indent(nvl, pctl);
768 #define NVP(elem, type, vtype, ptype, format) { \
771 (void) nvpair_value_##type(elem, &value); \
772 (void) printf("%*s%s: " format "\n", indent, "", \
773 nvpair_name(elem), (ptype)value); \
776 #define NVPA(elem, type, vtype, ptype, format) { \
780 (void) nvpair_value_##type(elem, &value, &count); \
781 for (i = 0; i < count; i++) { \
782 (void) printf("%*s%s[%d]: " format "\n", indent, "", \
783 nvpair_name(elem), i, (ptype)value[i]); \
788 * Similar to nvlist_print() but handles arrays slightly differently.
791 dump_nvlist(nvlist_t *list, int indent)
793 nvpair_t *elem = NULL;
794 boolean_t bool_value;
795 nvlist_t *nvlist_value;
796 nvlist_t **nvlist_array_value;
803 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
804 switch (nvpair_type(elem)) {
805 case DATA_TYPE_BOOLEAN_VALUE:
806 (void) nvpair_value_boolean_value(elem, &bool_value);
807 (void) printf("%*s%s: %s\n", indent, "",
808 nvpair_name(elem), bool_value ? "true" : "false");
812 NVP(elem, byte, uchar_t, int, "%u");
816 NVP(elem, int8, int8_t, int, "%d");
819 case DATA_TYPE_UINT8:
820 NVP(elem, uint8, uint8_t, int, "%u");
823 case DATA_TYPE_INT16:
824 NVP(elem, int16, int16_t, int, "%d");
827 case DATA_TYPE_UINT16:
828 NVP(elem, uint16, uint16_t, int, "%u");
831 case DATA_TYPE_INT32:
832 NVP(elem, int32, int32_t, long, "%ld");
835 case DATA_TYPE_UINT32:
836 NVP(elem, uint32, uint32_t, ulong_t, "%lu");
839 case DATA_TYPE_INT64:
840 NVP(elem, int64, int64_t, longlong_t, "%lld");
843 case DATA_TYPE_UINT64:
844 NVP(elem, uint64, uint64_t, u_longlong_t, "%llu");
847 case DATA_TYPE_STRING:
848 NVP(elem, string, char *, char *, "'%s'");
851 case DATA_TYPE_BYTE_ARRAY:
852 NVPA(elem, byte_array, uchar_t, int, "%u");
855 case DATA_TYPE_INT8_ARRAY:
856 NVPA(elem, int8_array, int8_t, int, "%d");
859 case DATA_TYPE_UINT8_ARRAY:
860 NVPA(elem, uint8_array, uint8_t, int, "%u");
863 case DATA_TYPE_INT16_ARRAY:
864 NVPA(elem, int16_array, int16_t, int, "%d");
867 case DATA_TYPE_UINT16_ARRAY:
868 NVPA(elem, uint16_array, uint16_t, int, "%u");
871 case DATA_TYPE_INT32_ARRAY:
872 NVPA(elem, int32_array, int32_t, long, "%ld");
875 case DATA_TYPE_UINT32_ARRAY:
876 NVPA(elem, uint32_array, uint32_t, ulong_t, "%lu");
879 case DATA_TYPE_INT64_ARRAY:
880 NVPA(elem, int64_array, int64_t, longlong_t, "%lld");
883 case DATA_TYPE_UINT64_ARRAY:
884 NVPA(elem, uint64_array, uint64_t, u_longlong_t,
888 case DATA_TYPE_STRING_ARRAY:
889 NVPA(elem, string_array, char *, char *, "'%s'");
892 case DATA_TYPE_NVLIST:
893 (void) nvpair_value_nvlist(elem, &nvlist_value);
894 (void) printf("%*s%s:\n", indent, "",
896 dump_nvlist(nvlist_value, indent + 4);
899 case DATA_TYPE_NVLIST_ARRAY:
900 (void) nvpair_value_nvlist_array(elem,
901 &nvlist_array_value, &count);
902 for (i = 0; i < count; i++) {
903 (void) printf("%*s%s[%u]:\n", indent, "",
904 nvpair_name(elem), i);
905 dump_nvlist(nvlist_array_value[i], indent + 4);
910 (void) printf(dgettext(TEXT_DOMAIN, "bad config type "
911 "%d for %s\n"), nvpair_type(elem),
918 * ======================================================================
920 * | Misc private interface. |
922 * ======================================================================
926 * Determine if string 'value' matches 'nvp' value. The 'value' string is
927 * converted, depending on the type of 'nvp', prior to match. For numeric
928 * types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
929 * is an array type, 'ai' is the index into the array against which we are
930 * checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
931 * in a regex_t compilation of value in 'value_regex' to trigger regular
932 * expression string match instead of simple strcmp().
934 * Return 1 on match, 0 on no-match, and -1 on error. If the error is
935 * related to value syntax error and 'ep' is non-NULL, *ep will point into
936 * the 'value' string at the location where the error exists.
938 * NOTE: It may be possible to move the non-regex_t version of this into
939 * common code used by library/kernel/boot.
942 nvpair_value_match_regex(nvpair_t *nvp, int ai,
943 char *value, regex_t *value_regex, char **ep)
952 if ((nvp == NULL) || (value == NULL))
953 return (-1); /* error fail match - invalid args */
955 /* make sure array and index combination make sense */
956 if ((nvpair_type_is_array(nvp) && (ai < 0)) ||
957 (!nvpair_type_is_array(nvp) && (ai >= 0)))
958 return (-1); /* error fail match - bad index */
960 /* non-string values should be single 'chunk' */
961 if ((nvpair_type(nvp) != DATA_TYPE_STRING) &&
962 (nvpair_type(nvp) != DATA_TYPE_STRING_ARRAY)) {
963 value += strspn(value, " \t");
964 evalue = value + strcspn(value, " \t");
968 return (-1); /* error fail match - syntax */
973 switch (nvpair_type(nvp)) {
974 case DATA_TYPE_STRING: {
977 /* check string value for match */
978 if (nvpair_value_string(nvp, &val) == 0) {
980 if (regexec(value_regex, val,
981 (size_t)0, NULL, 0) == 0)
982 return (1); /* match */
984 if (strcmp(value, val) == 0)
985 return (1); /* match */
990 case DATA_TYPE_STRING_ARRAY: {
993 /* check indexed string value of array for match */
994 if ((nvpair_value_string_array(nvp, &val_array, &a_len) == 0) &&
997 if (regexec(value_regex, val_array[ai],
998 (size_t)0, NULL, 0) == 0)
1001 if (strcmp(value, val_array[ai]) == 0)
1007 case DATA_TYPE_BYTE: {
1008 uchar_t val, val_arg;
1010 /* scanf uchar_t from value and check for match */
1011 sr = sscanf(value, "%c", &val_arg);
1012 if ((sr == 1) && (nvpair_value_byte(nvp, &val) == 0) &&
1017 case DATA_TYPE_BYTE_ARRAY: {
1018 uchar_t *val_array, val_arg;
1021 /* check indexed value of array for match */
1022 sr = sscanf(value, "%c", &val_arg);
1024 (nvpair_value_byte_array(nvp, &val_array, &a_len) == 0) &&
1026 (val_array[ai] == val_arg))
1030 case DATA_TYPE_INT8: {
1031 int8_t val, val_arg;
1033 /* scanf int8_t from value and check for match */
1034 sr = sscanf(value, "%"SCNi8, &val_arg);
1036 (nvpair_value_int8(nvp, &val) == 0) &&
1041 case DATA_TYPE_INT8_ARRAY: {
1042 int8_t *val_array, val_arg;
1044 /* check indexed value of array for match */
1045 sr = sscanf(value, "%"SCNi8, &val_arg);
1047 (nvpair_value_int8_array(nvp, &val_array, &a_len) == 0) &&
1049 (val_array[ai] == val_arg))
1053 case DATA_TYPE_UINT8: {
1054 uint8_t val, val_arg;
1056 /* scanf uint8_t from value and check for match */
1057 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1059 (nvpair_value_uint8(nvp, &val) == 0) &&
1064 case DATA_TYPE_UINT8_ARRAY: {
1065 uint8_t *val_array, val_arg;
1067 /* check indexed value of array for match */
1068 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1070 (nvpair_value_uint8_array(nvp, &val_array, &a_len) == 0) &&
1072 (val_array[ai] == val_arg))
1076 case DATA_TYPE_INT16: {
1077 int16_t val, val_arg;
1079 /* scanf int16_t from value and check for match */
1080 sr = sscanf(value, "%"SCNi16, &val_arg);
1082 (nvpair_value_int16(nvp, &val) == 0) &&
1087 case DATA_TYPE_INT16_ARRAY: {
1088 int16_t *val_array, val_arg;
1090 /* check indexed value of array for match */
1091 sr = sscanf(value, "%"SCNi16, &val_arg);
1093 (nvpair_value_int16_array(nvp, &val_array, &a_len) == 0) &&
1095 (val_array[ai] == val_arg))
1099 case DATA_TYPE_UINT16: {
1100 uint16_t val, val_arg;
1102 /* scanf uint16_t from value and check for match */
1103 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1105 (nvpair_value_uint16(nvp, &val) == 0) &&
1110 case DATA_TYPE_UINT16_ARRAY: {
1111 uint16_t *val_array, val_arg;
1113 /* check indexed value of array for match */
1114 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1116 (nvpair_value_uint16_array(nvp, &val_array, &a_len) == 0) &&
1118 (val_array[ai] == val_arg))
1122 case DATA_TYPE_INT32: {
1123 int32_t val, val_arg;
1125 /* scanf int32_t from value and check for match */
1126 sr = sscanf(value, "%"SCNi32, &val_arg);
1128 (nvpair_value_int32(nvp, &val) == 0) &&
1133 case DATA_TYPE_INT32_ARRAY: {
1134 int32_t *val_array, val_arg;
1136 /* check indexed value of array for match */
1137 sr = sscanf(value, "%"SCNi32, &val_arg);
1139 (nvpair_value_int32_array(nvp, &val_array, &a_len) == 0) &&
1141 (val_array[ai] == val_arg))
1145 case DATA_TYPE_UINT32: {
1146 uint32_t val, val_arg;
1148 /* scanf uint32_t from value and check for match */
1149 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1151 (nvpair_value_uint32(nvp, &val) == 0) &&
1156 case DATA_TYPE_UINT32_ARRAY: {
1157 uint32_t *val_array, val_arg;
1159 /* check indexed value of array for match */
1160 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1162 (nvpair_value_uint32_array(nvp, &val_array, &a_len) == 0) &&
1164 (val_array[ai] == val_arg))
1168 case DATA_TYPE_INT64: {
1169 int64_t val, val_arg;
1171 /* scanf int64_t from value and check for match */
1172 sr = sscanf(value, "%"SCNi64, &val_arg);
1174 (nvpair_value_int64(nvp, &val) == 0) &&
1179 case DATA_TYPE_INT64_ARRAY: {
1180 int64_t *val_array, val_arg;
1182 /* check indexed value of array for match */
1183 sr = sscanf(value, "%"SCNi64, &val_arg);
1185 (nvpair_value_int64_array(nvp, &val_array, &a_len) == 0) &&
1187 (val_array[ai] == val_arg))
1191 case DATA_TYPE_UINT64: {
1192 uint64_t val_arg, val;
1194 /* scanf uint64_t from value and check for match */
1195 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1197 (nvpair_value_uint64(nvp, &val) == 0) &&
1202 case DATA_TYPE_UINT64_ARRAY: {
1203 uint64_t *val_array, val_arg;
1205 /* check indexed value of array for match */
1206 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1208 (nvpair_value_uint64_array(nvp, &val_array, &a_len) == 0) &&
1210 (val_array[ai] == val_arg))
1214 case DATA_TYPE_BOOLEAN_VALUE: {
1215 boolean_t val, val_arg;
1217 /* scanf boolean_t from value and check for match */
1218 sr = sscanf(value, "%"SCNi32, &val_arg);
1220 (nvpair_value_boolean_value(nvp, &val) == 0) &&
1225 case DATA_TYPE_BOOLEAN_ARRAY: {
1226 boolean_t *val_array, val_arg;
1228 /* check indexed value of array for match */
1229 sr = sscanf(value, "%"SCNi32, &val_arg);
1231 (nvpair_value_boolean_array(nvp,
1232 &val_array, &a_len) == 0) &&
1234 (val_array[ai] == val_arg))
1238 case DATA_TYPE_HRTIME:
1239 case DATA_TYPE_NVLIST:
1240 case DATA_TYPE_NVLIST_ARRAY:
1241 case DATA_TYPE_BOOLEAN:
1242 case DATA_TYPE_DOUBLE:
1243 case DATA_TYPE_UNKNOWN:
1246 * unknown/unsupported data type
1248 return (-1); /* error fail match */
1252 * check to see if sscanf failed conversion, return approximate
1253 * pointer to problem
1258 return (-1); /* error fail match - syntax */
1261 return (0); /* fail match */
1265 nvpair_value_match(nvpair_t *nvp, int ai, char *value, char **ep)
1267 return (nvpair_value_match_regex(nvp, ai, value, NULL, ep));