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.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
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, 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, 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) \
193 FILE *fp = pctl->nvprt_fp; \
194 NOTE(ARGUNUSED(private)) \
195 NOTE(ARGUNUSED(nvl)) \
197 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
198 (void) fprintf(fp, vfmt, (ptype)value); \
202 NVLIST_PRTFUNC(boolean, int, int, "%d")
203 NVLIST_PRTFUNC(boolean_value, boolean_t, int, "%d")
204 NVLIST_PRTFUNC(byte, uchar_t, uchar_t, "0x%2.2x")
205 NVLIST_PRTFUNC(int8, int8_t, int, "%d")
206 NVLIST_PRTFUNC(uint8, uint8_t, uint8_t, "0x%x")
207 NVLIST_PRTFUNC(int16, int16_t, int16_t, "%d")
208 NVLIST_PRTFUNC(uint16, uint16_t, uint16_t, "0x%x")
209 NVLIST_PRTFUNC(int32, int32_t, int32_t, "%d")
210 NVLIST_PRTFUNC(uint32, uint32_t, uint32_t, "0x%x")
211 NVLIST_PRTFUNC(int64, int64_t, longlong_t, "%lld")
212 NVLIST_PRTFUNC(uint64, uint64_t, u_longlong_t, "0x%llx")
213 NVLIST_PRTFUNC(double, double, double, "0x%f")
214 NVLIST_PRTFUNC(string, char *, char *, "%s")
215 NVLIST_PRTFUNC(hrtime, hrtime_t, hrtime_t, "0x%llx")
218 * Generate functions to print array-valued nvlist members.
221 #define NVLIST_ARRPRTFUNC(type_and_variant, vtype, ptype, vfmt) \
223 nvaprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
224 nvlist_t *nvl, const char *name, vtype *valuep, uint_t count) \
226 FILE *fp = pctl->nvprt_fp; \
228 NOTE(ARGUNUSED(private)) \
229 NOTE(ARGUNUSED(nvl)) \
230 for (i = 0; i < count; i++) { \
231 if (i == 0 || pctl->nvprt_btwnarrfmt_nl) { \
233 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
234 if (pctl->nvprt_btwnarrfmt_nl) \
235 (void) fprintf(fp, "[%d]: ", i); \
238 (void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
239 (void) fprintf(fp, vfmt, (ptype)valuep[i]); \
244 NVLIST_ARRPRTFUNC(boolean_array, boolean_t, boolean_t, "%d")
245 NVLIST_ARRPRTFUNC(byte_array, uchar_t, uchar_t, "0x%2.2x")
246 NVLIST_ARRPRTFUNC(int8_array, int8_t, int8_t, "%d")
247 NVLIST_ARRPRTFUNC(uint8_array, uint8_t, uint8_t, "0x%x")
248 NVLIST_ARRPRTFUNC(int16_array, int16_t, int16_t, "%d")
249 NVLIST_ARRPRTFUNC(uint16_array, uint16_t, uint16_t, "0x%x")
250 NVLIST_ARRPRTFUNC(int32_array, int32_t, int32_t, "%d")
251 NVLIST_ARRPRTFUNC(uint32_array, uint32_t, uint32_t, "0x%x")
252 NVLIST_ARRPRTFUNC(int64_array, int64_t, longlong_t, "%lld")
253 NVLIST_ARRPRTFUNC(uint64_array, uint64_t, u_longlong_t, "0x%llx")
254 NVLIST_ARRPRTFUNC(string_array, char *, char *, "%s")
258 nvprint_nvlist(nvlist_prtctl_t pctl, void *private,
259 nvlist_t *nvl, const char *name, nvlist_t *value)
261 FILE *fp = pctl->nvprt_fp;
264 (void) fprintf(fp, "%s = (embedded nvlist)\n", name);
266 pctl->nvprt_indent += pctl->nvprt_indentinc;
267 nvlist_print_with_indent(value, pctl);
268 pctl->nvprt_indent -= pctl->nvprt_indentinc;
271 (void) fprintf(fp, "(end %s)\n", name);
278 nvaprint_nvlist_array(nvlist_prtctl_t pctl, void *private,
279 nvlist_t *nvl, const char *name, nvlist_t **valuep, uint_t count)
281 FILE *fp = pctl->nvprt_fp;
285 (void) fprintf(fp, "%s = (array of embedded nvlists)\n", name);
287 for (i = 0; i < count; i++) {
289 (void) fprintf(fp, "(start %s[%d])\n", name, i);
291 pctl->nvprt_indent += pctl->nvprt_indentinc;
292 nvlist_print_with_indent(valuep[i], pctl);
293 pctl->nvprt_indent -= pctl->nvprt_indentinc;
296 (void) fprintf(fp, "(end %s[%d])\n", name, i);
303 * ======================================================================
305 * | Interfaces that allow control over formatting. |
307 * ======================================================================
311 nvlist_prtctl_setdest(nvlist_prtctl_t pctl, FILE *fp)
317 nvlist_prtctl_getdest(nvlist_prtctl_t pctl)
319 return (pctl->nvprt_fp);
324 nvlist_prtctl_setindent(nvlist_prtctl_t pctl, enum nvlist_indent_mode mode,
327 if (mode < NVLIST_INDENT_ABS || mode > NVLIST_INDENT_TABBED)
328 mode = NVLIST_INDENT_TABBED;
336 pctl->nvprt_indent_mode = mode;
337 pctl->nvprt_indent = start;
338 pctl->nvprt_indentinc = inc;
342 nvlist_prtctl_doindent(nvlist_prtctl_t pctl, int onemore)
344 indent(pctl, onemore);
349 nvlist_prtctl_setfmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which,
353 case NVLIST_FMT_MEMBER_NAME:
356 pctl->nvprt_nmfmt = fmt;
359 case NVLIST_FMT_MEMBER_POSTAMBLE:
362 pctl->nvprt_eomfmt = fmt;
365 case NVLIST_FMT_BTWN_ARRAY:
367 pctl->nvprt_btwnarrfmt = " ";
368 pctl->nvprt_btwnarrfmt_nl = 0;
370 pctl->nvprt_btwnarrfmt = fmt;
371 pctl->nvprt_btwnarrfmt_nl = (strstr(fmt, "\n") != NULL);
382 nvlist_prtctl_dofmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which, ...)
384 FILE *fp = pctl->nvprt_fp;
391 case NVLIST_FMT_MEMBER_NAME:
392 name = va_arg(ap, char *);
393 (void) fprintf(fp, pctl->nvprt_nmfmt, name);
396 case NVLIST_FMT_MEMBER_POSTAMBLE:
397 (void) fprintf(fp, pctl->nvprt_eomfmt);
400 case NVLIST_FMT_BTWN_ARRAY:
401 (void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
412 * ======================================================================
414 * | Interfaces to allow appointment of replacement rendering functions.|
416 * ======================================================================
419 #define NVLIST_PRINTCTL_REPLACE(type, vtype) \
421 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
422 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype), \
425 CUSTPRTOP(pctl, type) = func; \
426 CUSTPRTOPARG(pctl, type) = private; \
429 NVLIST_PRINTCTL_REPLACE(boolean, int)
430 NVLIST_PRINTCTL_REPLACE(boolean_value, boolean_t)
431 NVLIST_PRINTCTL_REPLACE(byte, uchar_t)
432 NVLIST_PRINTCTL_REPLACE(int8, int8_t)
433 NVLIST_PRINTCTL_REPLACE(uint8, uint8_t)
434 NVLIST_PRINTCTL_REPLACE(int16, int16_t)
435 NVLIST_PRINTCTL_REPLACE(uint16, uint16_t)
436 NVLIST_PRINTCTL_REPLACE(int32, int32_t)
437 NVLIST_PRINTCTL_REPLACE(uint32, uint32_t)
438 NVLIST_PRINTCTL_REPLACE(int64, int64_t)
439 NVLIST_PRINTCTL_REPLACE(uint64, uint64_t)
440 NVLIST_PRINTCTL_REPLACE(double, double)
441 NVLIST_PRINTCTL_REPLACE(string, char *)
442 NVLIST_PRINTCTL_REPLACE(hrtime, hrtime_t)
443 NVLIST_PRINTCTL_REPLACE(nvlist, nvlist_t *)
445 #define NVLIST_PRINTCTL_AREPLACE(type, vtype) \
447 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
448 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, \
449 uint_t), void *private) \
451 CUSTPRTOP(pctl, type) = func; \
452 CUSTPRTOPARG(pctl, type) = private; \
455 NVLIST_PRINTCTL_AREPLACE(boolean_array, boolean_t *)
456 NVLIST_PRINTCTL_AREPLACE(byte_array, uchar_t *)
457 NVLIST_PRINTCTL_AREPLACE(int8_array, int8_t *)
458 NVLIST_PRINTCTL_AREPLACE(uint8_array, uint8_t *)
459 NVLIST_PRINTCTL_AREPLACE(int16_array, int16_t *)
460 NVLIST_PRINTCTL_AREPLACE(uint16_array, uint16_t *)
461 NVLIST_PRINTCTL_AREPLACE(int32_array, int32_t *)
462 NVLIST_PRINTCTL_AREPLACE(uint32_array, uint32_t *)
463 NVLIST_PRINTCTL_AREPLACE(int64_array, int64_t *)
464 NVLIST_PRINTCTL_AREPLACE(uint64_array, uint64_t *)
465 NVLIST_PRINTCTL_AREPLACE(string_array, char **)
466 NVLIST_PRINTCTL_AREPLACE(nvlist_array, nvlist_t **)
469 * ======================================================================
471 * | Interfaces to manage nvlist_prtctl_t cookies. |
473 * ======================================================================
477 static const struct nvlist_printops defprtops = {
478 { nvprint_boolean, NULL },
479 { nvprint_boolean_value, NULL },
480 { nvprint_byte, NULL },
481 { nvprint_int8, NULL },
482 { nvprint_uint8, NULL },
483 { nvprint_int16, NULL },
484 { nvprint_uint16, NULL },
485 { nvprint_int32, NULL },
486 { nvprint_uint32, NULL },
487 { nvprint_int64, NULL },
488 { nvprint_uint64, NULL },
489 { nvprint_double, NULL },
490 { nvprint_string, NULL },
491 { nvprint_hrtime, NULL },
492 { nvprint_nvlist, NULL },
493 { nvaprint_boolean_array, NULL },
494 { nvaprint_byte_array, NULL },
495 { nvaprint_int8_array, NULL },
496 { nvaprint_uint8_array, NULL },
497 { nvaprint_int16_array, NULL },
498 { nvaprint_uint16_array, NULL },
499 { nvaprint_int32_array, NULL },
500 { nvaprint_uint32_array, NULL },
501 { nvaprint_int64_array, NULL },
502 { nvaprint_uint64_array, NULL },
503 { nvaprint_string_array, NULL },
504 { nvaprint_nvlist_array, NULL },
508 prtctl_defaults(FILE *fp, struct nvlist_prtctl *pctl,
509 struct nvlist_printops *ops)
512 pctl->nvprt_indent_mode = NVLIST_INDENT_TABBED;
513 pctl->nvprt_indent = 0;
514 pctl->nvprt_indentinc = 1;
515 pctl->nvprt_nmfmt = "%s = ";
516 pctl->nvprt_eomfmt = "\n";
517 pctl->nvprt_btwnarrfmt = " ";
518 pctl->nvprt_btwnarrfmt_nl = 0;
520 pctl->nvprt_dfltops = (struct nvlist_printops *)&defprtops;
521 pctl->nvprt_custops = ops;
525 nvlist_prtctl_alloc(void)
527 struct nvlist_prtctl *pctl;
528 struct nvlist_printops *ops;
530 if ((pctl = malloc(sizeof (*pctl))) == NULL)
533 if ((ops = calloc(1, sizeof (*ops))) == NULL) {
538 prtctl_defaults(stdout, pctl, ops);
544 nvlist_prtctl_free(nvlist_prtctl_t pctl)
547 free(pctl->nvprt_custops);
553 * ======================================================================
555 * | Top-level print request interfaces. |
557 * ======================================================================
561 * nvlist_print - Prints elements in an event buffer
564 nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl)
566 FILE *fp = pctl->nvprt_fp;
575 (void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl));
577 nvp = nvlist_next_nvpair(nvl, NULL);
580 data_type_t type = nvpair_type(nvp);
582 name = nvpair_name(nvp);
586 case DATA_TYPE_BOOLEAN: {
587 RENDER(pctl, boolean, nvl, name, 1);
590 case DATA_TYPE_BOOLEAN_VALUE: {
592 (void) nvpair_value_boolean_value(nvp, &val);
593 RENDER(pctl, boolean_value, nvl, name, val);
596 case DATA_TYPE_BYTE: {
598 (void) nvpair_value_byte(nvp, &val);
599 RENDER(pctl, byte, nvl, name, val);
602 case DATA_TYPE_INT8: {
604 (void) nvpair_value_int8(nvp, &val);
605 RENDER(pctl, int8, nvl, name, val);
608 case DATA_TYPE_UINT8: {
610 (void) nvpair_value_uint8(nvp, &val);
611 RENDER(pctl, uint8, nvl, name, val);
614 case DATA_TYPE_INT16: {
616 (void) nvpair_value_int16(nvp, &val);
617 RENDER(pctl, int16, nvl, name, val);
620 case DATA_TYPE_UINT16: {
622 (void) nvpair_value_uint16(nvp, &val);
623 RENDER(pctl, uint16, nvl, name, val);
626 case DATA_TYPE_INT32: {
628 (void) nvpair_value_int32(nvp, &val);
629 RENDER(pctl, int32, nvl, name, val);
632 case DATA_TYPE_UINT32: {
634 (void) nvpair_value_uint32(nvp, &val);
635 RENDER(pctl, uint32, nvl, name, val);
638 case DATA_TYPE_INT64: {
640 (void) nvpair_value_int64(nvp, &val);
641 RENDER(pctl, int64, nvl, name, val);
644 case DATA_TYPE_UINT64: {
646 (void) nvpair_value_uint64(nvp, &val);
647 RENDER(pctl, uint64, nvl, name, val);
650 case DATA_TYPE_DOUBLE: {
652 (void) nvpair_value_double(nvp, &val);
653 RENDER(pctl, double, nvl, name, val);
656 case DATA_TYPE_STRING: {
658 (void) nvpair_value_string(nvp, &val);
659 RENDER(pctl, string, nvl, name, val);
662 case DATA_TYPE_BOOLEAN_ARRAY: {
664 (void) nvpair_value_boolean_array(nvp, &val, &nelem);
665 ARENDER(pctl, boolean_array, nvl, name, val, nelem);
668 case DATA_TYPE_BYTE_ARRAY: {
670 (void) nvpair_value_byte_array(nvp, &val, &nelem);
671 ARENDER(pctl, byte_array, nvl, name, val, nelem);
674 case DATA_TYPE_INT8_ARRAY: {
676 (void) nvpair_value_int8_array(nvp, &val, &nelem);
677 ARENDER(pctl, int8_array, nvl, name, val, nelem);
680 case DATA_TYPE_UINT8_ARRAY: {
682 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
683 ARENDER(pctl, uint8_array, nvl, name, val, nelem);
686 case DATA_TYPE_INT16_ARRAY: {
688 (void) nvpair_value_int16_array(nvp, &val, &nelem);
689 ARENDER(pctl, int16_array, nvl, name, val, nelem);
692 case DATA_TYPE_UINT16_ARRAY: {
694 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
695 ARENDER(pctl, uint16_array, nvl, name, val, nelem);
698 case DATA_TYPE_INT32_ARRAY: {
700 (void) nvpair_value_int32_array(nvp, &val, &nelem);
701 ARENDER(pctl, int32_array, nvl, name, val, nelem);
704 case DATA_TYPE_UINT32_ARRAY: {
706 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
707 ARENDER(pctl, uint32_array, nvl, name, val, nelem);
710 case DATA_TYPE_INT64_ARRAY: {
712 (void) nvpair_value_int64_array(nvp, &val, &nelem);
713 ARENDER(pctl, int64_array, nvl, name, val, nelem);
716 case DATA_TYPE_UINT64_ARRAY: {
718 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
719 ARENDER(pctl, uint64_array, nvl, name, val, nelem);
722 case DATA_TYPE_STRING_ARRAY: {
724 (void) nvpair_value_string_array(nvp, &val, &nelem);
725 ARENDER(pctl, string_array, nvl, name, val, nelem);
728 case DATA_TYPE_HRTIME: {
730 (void) nvpair_value_hrtime(nvp, &val);
731 RENDER(pctl, hrtime, nvl, name, val);
734 case DATA_TYPE_NVLIST: {
736 (void) nvpair_value_nvlist(nvp, &val);
737 RENDER(pctl, nvlist, nvl, name, val);
740 case DATA_TYPE_NVLIST_ARRAY: {
742 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
743 ARENDER(pctl, nvlist_array, nvl, name, val, nelem);
747 (void) fprintf(fp, " unknown data type (%d)", type);
750 nvp = nvlist_next_nvpair(nvl, nvp);
755 nvlist_print(FILE *fp, nvlist_t *nvl)
757 struct nvlist_prtctl pc;
759 prtctl_defaults(fp, &pc, NULL);
760 nvlist_print_with_indent(nvl, &pc);
764 nvlist_prt(nvlist_t *nvl, nvlist_prtctl_t pctl)
766 nvlist_print_with_indent(nvl, pctl);
769 #define NVP(elem, type, vtype, ptype, format) { \
772 (void) nvpair_value_##type(elem, &value); \
773 (void) printf("%*s%s: " format "\n", indent, "", \
774 nvpair_name(elem), (ptype)value); \
777 #define NVPA(elem, type, vtype, ptype, format) { \
781 (void) nvpair_value_##type(elem, &value, &count); \
782 for (i = 0; i < count; i++) { \
783 (void) printf("%*s%s[%d]: " format "\n", indent, "", \
784 nvpair_name(elem), i, (ptype)value[i]); \
789 * Similar to nvlist_print() but handles arrays slightly differently.
792 dump_nvlist(nvlist_t *list, int indent)
794 nvpair_t *elem = NULL;
795 boolean_t bool_value;
796 boolean_t *bool_array_value;
797 nvlist_t *nvlist_value;
798 nvlist_t **nvlist_array_value;
805 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
806 switch (nvpair_type(elem)) {
807 case DATA_TYPE_BOOLEAN:
808 (void) printf("%*s%s\n", indent, "", nvpair_name(elem));
811 case DATA_TYPE_BOOLEAN_VALUE:
812 (void) nvpair_value_boolean_value(elem, &bool_value);
813 (void) printf("%*s%s: %s\n", indent, "",
814 nvpair_name(elem), bool_value ? "true" : "false");
818 NVP(elem, byte, uchar_t, int, "%u");
822 NVP(elem, int8, int8_t, int, "%d");
825 case DATA_TYPE_UINT8:
826 NVP(elem, uint8, uint8_t, int, "%u");
829 case DATA_TYPE_INT16:
830 NVP(elem, int16, int16_t, int, "%d");
833 case DATA_TYPE_UINT16:
834 NVP(elem, uint16, uint16_t, int, "%u");
837 case DATA_TYPE_INT32:
838 NVP(elem, int32, int32_t, long, "%ld");
841 case DATA_TYPE_UINT32:
842 NVP(elem, uint32, uint32_t, ulong_t, "%lu");
845 case DATA_TYPE_INT64:
846 NVP(elem, int64, int64_t, longlong_t, "%lld");
849 case DATA_TYPE_UINT64:
850 NVP(elem, uint64, uint64_t, u_longlong_t, "%llu");
853 case DATA_TYPE_STRING:
854 NVP(elem, string, char *, char *, "'%s'");
857 case DATA_TYPE_BOOLEAN_ARRAY:
858 (void) nvpair_value_boolean_array(elem,
859 &bool_array_value, &count);
860 for (i = 0; i < count; i++) {
861 (void) printf("%*s%s[%d]: %s\n", indent, "",
862 nvpair_name(elem), i,
863 bool_array_value[i] ? "true" : "false");
867 case DATA_TYPE_BYTE_ARRAY:
868 NVPA(elem, byte_array, uchar_t, int, "%u");
871 case DATA_TYPE_INT8_ARRAY:
872 NVPA(elem, int8_array, int8_t, int, "%d");
875 case DATA_TYPE_UINT8_ARRAY:
876 NVPA(elem, uint8_array, uint8_t, int, "%u");
879 case DATA_TYPE_INT16_ARRAY:
880 NVPA(elem, int16_array, int16_t, int, "%d");
883 case DATA_TYPE_UINT16_ARRAY:
884 NVPA(elem, uint16_array, uint16_t, int, "%u");
887 case DATA_TYPE_INT32_ARRAY:
888 NVPA(elem, int32_array, int32_t, long, "%ld");
891 case DATA_TYPE_UINT32_ARRAY:
892 NVPA(elem, uint32_array, uint32_t, ulong_t, "%lu");
895 case DATA_TYPE_INT64_ARRAY:
896 NVPA(elem, int64_array, int64_t, longlong_t, "%lld");
899 case DATA_TYPE_UINT64_ARRAY:
900 NVPA(elem, uint64_array, uint64_t, u_longlong_t,
904 case DATA_TYPE_STRING_ARRAY:
905 NVPA(elem, string_array, char *, char *, "'%s'");
908 case DATA_TYPE_NVLIST:
909 (void) nvpair_value_nvlist(elem, &nvlist_value);
910 (void) printf("%*s%s:\n", indent, "",
912 dump_nvlist(nvlist_value, indent + 4);
915 case DATA_TYPE_NVLIST_ARRAY:
916 (void) nvpair_value_nvlist_array(elem,
917 &nvlist_array_value, &count);
918 for (i = 0; i < count; i++) {
919 (void) printf("%*s%s[%u]:\n", indent, "",
920 nvpair_name(elem), i);
921 dump_nvlist(nvlist_array_value[i], indent + 4);
926 (void) printf(dgettext(TEXT_DOMAIN, "bad config type "
927 "%d for %s\n"), nvpair_type(elem),
934 * ======================================================================
936 * | Misc private interface. |
938 * ======================================================================
942 * Determine if string 'value' matches 'nvp' value. The 'value' string is
943 * converted, depending on the type of 'nvp', prior to match. For numeric
944 * types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
945 * is an array type, 'ai' is the index into the array against which we are
946 * checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
947 * in a regex_t compilation of value in 'value_regex' to trigger regular
948 * expression string match instead of simple strcmp().
950 * Return 1 on match, 0 on no-match, and -1 on error. If the error is
951 * related to value syntax error and 'ep' is non-NULL, *ep will point into
952 * the 'value' string at the location where the error exists.
954 * NOTE: It may be possible to move the non-regex_t version of this into
955 * common code used by library/kernel/boot.
958 nvpair_value_match_regex(nvpair_t *nvp, int ai,
959 char *value, regex_t *value_regex, char **ep)
968 if ((nvp == NULL) || (value == NULL))
969 return (-1); /* error fail match - invalid args */
971 /* make sure array and index combination make sense */
972 if ((nvpair_type_is_array(nvp) && (ai < 0)) ||
973 (!nvpair_type_is_array(nvp) && (ai >= 0)))
974 return (-1); /* error fail match - bad index */
976 /* non-string values should be single 'chunk' */
977 if ((nvpair_type(nvp) != DATA_TYPE_STRING) &&
978 (nvpair_type(nvp) != DATA_TYPE_STRING_ARRAY)) {
979 value += strspn(value, " \t");
980 evalue = value + strcspn(value, " \t");
984 return (-1); /* error fail match - syntax */
989 switch (nvpair_type(nvp)) {
990 case DATA_TYPE_STRING: {
993 /* check string value for match */
994 if (nvpair_value_string(nvp, &val) == 0) {
996 if (regexec(value_regex, val,
997 (size_t)0, NULL, 0) == 0)
998 return (1); /* match */
1000 if (strcmp(value, val) == 0)
1001 return (1); /* match */
1006 case DATA_TYPE_STRING_ARRAY: {
1009 /* check indexed string value of array for match */
1010 if ((nvpair_value_string_array(nvp, &val_array, &a_len) == 0) &&
1013 if (regexec(value_regex, val_array[ai],
1014 (size_t)0, NULL, 0) == 0)
1017 if (strcmp(value, val_array[ai]) == 0)
1023 case DATA_TYPE_BYTE: {
1024 uchar_t val, val_arg;
1026 /* scanf uchar_t from value and check for match */
1027 sr = sscanf(value, "%c", &val_arg);
1028 if ((sr == 1) && (nvpair_value_byte(nvp, &val) == 0) &&
1033 case DATA_TYPE_BYTE_ARRAY: {
1034 uchar_t *val_array, val_arg;
1037 /* check indexed value of array for match */
1038 sr = sscanf(value, "%c", &val_arg);
1040 (nvpair_value_byte_array(nvp, &val_array, &a_len) == 0) &&
1042 (val_array[ai] == val_arg))
1046 case DATA_TYPE_INT8: {
1047 int8_t val, val_arg;
1049 /* scanf int8_t from value and check for match */
1050 sr = sscanf(value, "%"SCNi8, &val_arg);
1052 (nvpair_value_int8(nvp, &val) == 0) &&
1057 case DATA_TYPE_INT8_ARRAY: {
1058 int8_t *val_array, val_arg;
1060 /* check indexed value of array for match */
1061 sr = sscanf(value, "%"SCNi8, &val_arg);
1063 (nvpair_value_int8_array(nvp, &val_array, &a_len) == 0) &&
1065 (val_array[ai] == val_arg))
1069 case DATA_TYPE_UINT8: {
1070 uint8_t val, val_arg;
1072 /* scanf uint8_t from value and check for match */
1073 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1075 (nvpair_value_uint8(nvp, &val) == 0) &&
1080 case DATA_TYPE_UINT8_ARRAY: {
1081 uint8_t *val_array, val_arg;
1083 /* check indexed value of array for match */
1084 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1086 (nvpair_value_uint8_array(nvp, &val_array, &a_len) == 0) &&
1088 (val_array[ai] == val_arg))
1092 case DATA_TYPE_INT16: {
1093 int16_t val, val_arg;
1095 /* scanf int16_t from value and check for match */
1096 sr = sscanf(value, "%"SCNi16, &val_arg);
1098 (nvpair_value_int16(nvp, &val) == 0) &&
1103 case DATA_TYPE_INT16_ARRAY: {
1104 int16_t *val_array, val_arg;
1106 /* check indexed value of array for match */
1107 sr = sscanf(value, "%"SCNi16, &val_arg);
1109 (nvpair_value_int16_array(nvp, &val_array, &a_len) == 0) &&
1111 (val_array[ai] == val_arg))
1115 case DATA_TYPE_UINT16: {
1116 uint16_t val, val_arg;
1118 /* scanf uint16_t from value and check for match */
1119 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1121 (nvpair_value_uint16(nvp, &val) == 0) &&
1126 case DATA_TYPE_UINT16_ARRAY: {
1127 uint16_t *val_array, val_arg;
1129 /* check indexed value of array for match */
1130 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1132 (nvpair_value_uint16_array(nvp, &val_array, &a_len) == 0) &&
1134 (val_array[ai] == val_arg))
1138 case DATA_TYPE_INT32: {
1139 int32_t val, val_arg;
1141 /* scanf int32_t from value and check for match */
1142 sr = sscanf(value, "%"SCNi32, &val_arg);
1144 (nvpair_value_int32(nvp, &val) == 0) &&
1149 case DATA_TYPE_INT32_ARRAY: {
1150 int32_t *val_array, val_arg;
1152 /* check indexed value of array for match */
1153 sr = sscanf(value, "%"SCNi32, &val_arg);
1155 (nvpair_value_int32_array(nvp, &val_array, &a_len) == 0) &&
1157 (val_array[ai] == val_arg))
1161 case DATA_TYPE_UINT32: {
1162 uint32_t val, val_arg;
1164 /* scanf uint32_t from value and check for match */
1165 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1167 (nvpair_value_uint32(nvp, &val) == 0) &&
1172 case DATA_TYPE_UINT32_ARRAY: {
1173 uint32_t *val_array, val_arg;
1175 /* check indexed value of array for match */
1176 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1178 (nvpair_value_uint32_array(nvp, &val_array, &a_len) == 0) &&
1180 (val_array[ai] == val_arg))
1184 case DATA_TYPE_INT64: {
1185 int64_t val, val_arg;
1187 /* scanf int64_t from value and check for match */
1188 sr = sscanf(value, "%"SCNi64, &val_arg);
1190 (nvpair_value_int64(nvp, &val) == 0) &&
1195 case DATA_TYPE_INT64_ARRAY: {
1196 int64_t *val_array, val_arg;
1198 /* check indexed value of array for match */
1199 sr = sscanf(value, "%"SCNi64, &val_arg);
1201 (nvpair_value_int64_array(nvp, &val_array, &a_len) == 0) &&
1203 (val_array[ai] == val_arg))
1207 case DATA_TYPE_UINT64: {
1208 uint64_t val_arg, val;
1210 /* scanf uint64_t from value and check for match */
1211 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1213 (nvpair_value_uint64(nvp, &val) == 0) &&
1218 case DATA_TYPE_UINT64_ARRAY: {
1219 uint64_t *val_array, val_arg;
1221 /* check indexed value of array for match */
1222 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1224 (nvpair_value_uint64_array(nvp, &val_array, &a_len) == 0) &&
1226 (val_array[ai] == val_arg))
1230 case DATA_TYPE_BOOLEAN_VALUE: {
1234 /* scanf boolean_t from value and check for match */
1235 sr = sscanf(value, "%"SCNi32, &val_arg);
1237 (nvpair_value_boolean_value(nvp, &val) == 0) &&
1242 case DATA_TYPE_BOOLEAN_ARRAY: {
1243 boolean_t *val_array;
1246 /* check indexed value of array for match */
1247 sr = sscanf(value, "%"SCNi32, &val_arg);
1249 (nvpair_value_boolean_array(nvp,
1250 &val_array, &a_len) == 0) &&
1252 (val_array[ai] == val_arg))
1256 case DATA_TYPE_HRTIME:
1257 case DATA_TYPE_NVLIST:
1258 case DATA_TYPE_NVLIST_ARRAY:
1259 case DATA_TYPE_BOOLEAN:
1260 case DATA_TYPE_DOUBLE:
1261 case DATA_TYPE_UNKNOWN:
1264 * unknown/unsupported data type
1266 return (-1); /* error fail match */
1270 * check to see if sscanf failed conversion, return approximate
1271 * pointer to problem
1276 return (-1); /* error fail match - syntax */
1279 return (0); /* fail match */
1283 nvpair_value_match(nvpair_t *nvp, int ai, char *value, char **ep)
1285 return (nvpair_value_match_regex(nvp, ai, value, NULL, ep));