Print this page
6447 handful of nvpair cleanups
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libnvpair/libnvpair.c
+++ new/usr/src/lib/libnvpair/libnvpair.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 24 */
25 25
26 26 #include <unistd.h>
27 -#include <strings.h>
27 +#include <string.h>
28 28 #include <libintl.h>
29 29 #include <sys/types.h>
30 30 #include <sys/inttypes.h>
31 31 #include <stdarg.h>
32 32 #include <note.h>
33 33 #include "libnvpair.h"
34 34
35 35 /*
36 36 * libnvpair - A tools library for manipulating <name, value> pairs.
37 37 *
38 38 * This library provides routines packing an unpacking nv pairs
39 39 * for transporting data across process boundaries, transporting
40 40 * between kernel and userland, and possibly saving onto disk files.
41 41 */
42 42
43 43 /*
44 44 * Print control structure.
45 45 */
46 46
47 47 #define DEFINEOP(opname, vtype) \
48 48 struct { \
49 49 int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
50 50 const char *, vtype); \
51 51 void *arg; \
52 52 } opname
53 53
54 54 #define DEFINEARROP(opname, vtype) \
55 55 struct { \
56 56 int (*op)(struct nvlist_prtctl *, void *, nvlist_t *, \
57 57 const char *, vtype, uint_t); \
58 58 void *arg; \
59 59 } opname
60 60
61 61 struct nvlist_printops {
62 62 DEFINEOP(print_boolean, int);
63 63 DEFINEOP(print_boolean_value, boolean_t);
64 64 DEFINEOP(print_byte, uchar_t);
65 65 DEFINEOP(print_int8, int8_t);
66 66 DEFINEOP(print_uint8, uint8_t);
67 67 DEFINEOP(print_int16, int16_t);
68 68 DEFINEOP(print_uint16, uint16_t);
69 69 DEFINEOP(print_int32, int32_t);
70 70 DEFINEOP(print_uint32, uint32_t);
71 71 DEFINEOP(print_int64, int64_t);
72 72 DEFINEOP(print_uint64, uint64_t);
73 73 DEFINEOP(print_double, double);
74 74 DEFINEOP(print_string, char *);
75 75 DEFINEOP(print_hrtime, hrtime_t);
76 76 DEFINEOP(print_nvlist, nvlist_t *);
77 77 DEFINEARROP(print_boolean_array, boolean_t *);
78 78 DEFINEARROP(print_byte_array, uchar_t *);
79 79 DEFINEARROP(print_int8_array, int8_t *);
80 80 DEFINEARROP(print_uint8_array, uint8_t *);
81 81 DEFINEARROP(print_int16_array, int16_t *);
82 82 DEFINEARROP(print_uint16_array, uint16_t *);
83 83 DEFINEARROP(print_int32_array, int32_t *);
84 84 DEFINEARROP(print_uint32_array, uint32_t *);
85 85 DEFINEARROP(print_int64_array, int64_t *);
86 86 DEFINEARROP(print_uint64_array, uint64_t *);
87 87 DEFINEARROP(print_string_array, char **);
88 88 DEFINEARROP(print_nvlist_array, nvlist_t **);
89 89 };
90 90
91 91 struct nvlist_prtctl {
92 92 FILE *nvprt_fp; /* output destination */
93 93 enum nvlist_indent_mode nvprt_indent_mode; /* see above */
94 94 int nvprt_indent; /* absolute indent, or tab depth */
95 95 int nvprt_indentinc; /* indent or tab increment */
96 96 const char *nvprt_nmfmt; /* member name format, max one %s */
97 97 const char *nvprt_eomfmt; /* after member format, e.g. "\n" */
98 98 const char *nvprt_btwnarrfmt; /* between array members */
99 99 int nvprt_btwnarrfmt_nl; /* nvprt_eoamfmt includes newline? */
100 100 struct nvlist_printops *nvprt_dfltops;
101 101 struct nvlist_printops *nvprt_custops;
102 102 };
103 103
104 104 #define DFLTPRTOP(pctl, type) \
105 105 ((pctl)->nvprt_dfltops->print_##type.op)
106 106
107 107 #define DFLTPRTOPARG(pctl, type) \
108 108 ((pctl)->nvprt_dfltops->print_##type.arg)
109 109
110 110 #define CUSTPRTOP(pctl, type) \
111 111 ((pctl)->nvprt_custops->print_##type.op)
112 112
113 113 #define CUSTPRTOPARG(pctl, type) \
114 114 ((pctl)->nvprt_custops->print_##type.arg)
115 115
116 116 #define RENDER(pctl, type, nvl, name, val) \
117 117 { \
118 118 int done = 0; \
119 119 if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
120 120 done = CUSTPRTOP(pctl, type)(pctl, \
121 121 CUSTPRTOPARG(pctl, type), nvl, name, val); \
122 122 } \
123 123 if (!done) { \
124 124 (void) DFLTPRTOP(pctl, type)(pctl, \
125 125 DFLTPRTOPARG(pctl, type), nvl, name, val); \
126 126 } \
127 127 (void) fprintf(pctl->nvprt_fp, pctl->nvprt_eomfmt); \
128 128 }
129 129
130 130 #define ARENDER(pctl, type, nvl, name, arrp, count) \
131 131 { \
132 132 int done = 0; \
133 133 if ((pctl)->nvprt_custops && CUSTPRTOP(pctl, type)) { \
134 134 done = CUSTPRTOP(pctl, type)(pctl, \
135 135 CUSTPRTOPARG(pctl, type), nvl, name, arrp, count); \
136 136 } \
137 137 if (!done) { \
138 138 (void) DFLTPRTOP(pctl, type)(pctl, \
139 139 DFLTPRTOPARG(pctl, type), nvl, name, arrp, count); \
140 140 } \
141 141 (void) fprintf(pctl->nvprt_fp, pctl->nvprt_eomfmt); \
142 142 }
143 143
144 144 static void nvlist_print_with_indent(nvlist_t *, nvlist_prtctl_t);
145 145
146 146 /*
147 147 * ======================================================================
148 148 * | |
149 149 * | Indentation |
150 150 * | |
151 151 * ======================================================================
152 152 */
153 153
154 154 static void
155 155 indent(nvlist_prtctl_t pctl, int onemore)
156 156 {
157 157 int depth;
158 158
159 159 switch (pctl->nvprt_indent_mode) {
160 160 case NVLIST_INDENT_ABS:
161 161 (void) fprintf(pctl->nvprt_fp, "%*s",
162 162 pctl->nvprt_indent + onemore * pctl->nvprt_indentinc, "");
163 163 break;
164 164
165 165 case NVLIST_INDENT_TABBED:
166 166 depth = pctl->nvprt_indent + onemore;
167 167 while (depth-- > 0)
168 168 (void) fprintf(pctl->nvprt_fp, "\t");
169 169 }
170 170 }
171 171
172 172 /*
173 173 * ======================================================================
174 174 * | |
175 175 * | Default nvlist member rendering functions. |
176 176 * | |
177 177 * ======================================================================
178 178 */
179 179
180 180 /*
181 181 * Generate functions to print single-valued nvlist members.
182 182 *
183 183 * type_and_variant - suffix to form function name
184 184 * vtype - C type for the member value
185 185 * ptype - C type to cast value to for printing
186 186 * vfmt - format string for pair value, e.g "%d" or "0x%llx"
187 187 */
188 188
189 189 #define NVLIST_PRTFUNC(type_and_variant, vtype, ptype, vfmt) \
190 190 static int \
191 191 nvprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
192 192 nvlist_t *nvl, const char *name, vtype value) \
193 193 { \
194 194 FILE *fp = pctl->nvprt_fp; \
195 195 NOTE(ARGUNUSED(private)) \
196 196 NOTE(ARGUNUSED(nvl)) \
197 197 indent(pctl, 1); \
198 198 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
199 199 (void) fprintf(fp, vfmt, (ptype)value); \
200 200 return (1); \
201 201 }
202 202
203 203 NVLIST_PRTFUNC(boolean, int, int, "%d")
↓ open down ↓ |
166 lines elided |
↑ open up ↑ |
204 204 NVLIST_PRTFUNC(boolean_value, boolean_t, int, "%d")
205 205 NVLIST_PRTFUNC(byte, uchar_t, uchar_t, "0x%2.2x")
206 206 NVLIST_PRTFUNC(int8, int8_t, int, "%d")
207 207 NVLIST_PRTFUNC(uint8, uint8_t, uint8_t, "0x%x")
208 208 NVLIST_PRTFUNC(int16, int16_t, int16_t, "%d")
209 209 NVLIST_PRTFUNC(uint16, uint16_t, uint16_t, "0x%x")
210 210 NVLIST_PRTFUNC(int32, int32_t, int32_t, "%d")
211 211 NVLIST_PRTFUNC(uint32, uint32_t, uint32_t, "0x%x")
212 212 NVLIST_PRTFUNC(int64, int64_t, longlong_t, "%lld")
213 213 NVLIST_PRTFUNC(uint64, uint64_t, u_longlong_t, "0x%llx")
214 -NVLIST_PRTFUNC(double, double, double, "0x%llf")
214 +NVLIST_PRTFUNC(double, double, double, "0x%f")
215 215 NVLIST_PRTFUNC(string, char *, char *, "%s")
216 216 NVLIST_PRTFUNC(hrtime, hrtime_t, hrtime_t, "0x%llx")
217 217
218 218 /*
219 219 * Generate functions to print array-valued nvlist members.
220 220 */
221 221
222 222 #define NVLIST_ARRPRTFUNC(type_and_variant, vtype, ptype, vfmt) \
223 223 static int \
224 224 nvaprint_##type_and_variant(nvlist_prtctl_t pctl, void *private, \
225 225 nvlist_t *nvl, const char *name, vtype *valuep, uint_t count) \
226 226 { \
227 227 FILE *fp = pctl->nvprt_fp; \
228 228 uint_t i; \
229 229 NOTE(ARGUNUSED(private)) \
230 230 NOTE(ARGUNUSED(nvl)) \
231 231 for (i = 0; i < count; i++) { \
232 232 if (i == 0 || pctl->nvprt_btwnarrfmt_nl) { \
233 233 indent(pctl, 1); \
234 234 (void) fprintf(fp, pctl->nvprt_nmfmt, name); \
235 235 if (pctl->nvprt_btwnarrfmt_nl) \
236 236 (void) fprintf(fp, "[%d]: ", i); \
237 237 } \
238 238 if (i != 0) \
239 239 (void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
240 240 (void) fprintf(fp, vfmt, (ptype)valuep[i]); \
241 241 } \
242 242 return (1); \
243 243 }
244 244
245 245 NVLIST_ARRPRTFUNC(boolean_array, boolean_t, boolean_t, "%d")
246 246 NVLIST_ARRPRTFUNC(byte_array, uchar_t, uchar_t, "0x%2.2x")
247 247 NVLIST_ARRPRTFUNC(int8_array, int8_t, int8_t, "%d")
248 248 NVLIST_ARRPRTFUNC(uint8_array, uint8_t, uint8_t, "0x%x")
249 249 NVLIST_ARRPRTFUNC(int16_array, int16_t, int16_t, "%d")
250 250 NVLIST_ARRPRTFUNC(uint16_array, uint16_t, uint16_t, "0x%x")
251 251 NVLIST_ARRPRTFUNC(int32_array, int32_t, int32_t, "%d")
252 252 NVLIST_ARRPRTFUNC(uint32_array, uint32_t, uint32_t, "0x%x")
253 253 NVLIST_ARRPRTFUNC(int64_array, int64_t, longlong_t, "%lld")
254 254 NVLIST_ARRPRTFUNC(uint64_array, uint64_t, u_longlong_t, "0x%llx")
255 255 NVLIST_ARRPRTFUNC(string_array, char *, char *, "%s")
256 256
257 257 /*ARGSUSED*/
258 258 static int
259 259 nvprint_nvlist(nvlist_prtctl_t pctl, void *private,
260 260 nvlist_t *nvl, const char *name, nvlist_t *value)
261 261 {
262 262 FILE *fp = pctl->nvprt_fp;
263 263
264 264 indent(pctl, 1);
265 265 (void) fprintf(fp, "%s = (embedded nvlist)\n", name);
266 266
267 267 pctl->nvprt_indent += pctl->nvprt_indentinc;
268 268 nvlist_print_with_indent(value, pctl);
269 269 pctl->nvprt_indent -= pctl->nvprt_indentinc;
270 270
271 271 indent(pctl, 1);
272 272 (void) fprintf(fp, "(end %s)\n", name);
273 273
274 274 return (1);
275 275 }
276 276
277 277 /*ARGSUSED*/
278 278 static int
279 279 nvaprint_nvlist_array(nvlist_prtctl_t pctl, void *private,
280 280 nvlist_t *nvl, const char *name, nvlist_t **valuep, uint_t count)
281 281 {
282 282 FILE *fp = pctl->nvprt_fp;
283 283 uint_t i;
284 284
285 285 indent(pctl, 1);
286 286 (void) fprintf(fp, "%s = (array of embedded nvlists)\n", name);
287 287
288 288 for (i = 0; i < count; i++) {
289 289 indent(pctl, 1);
290 290 (void) fprintf(fp, "(start %s[%d])\n", name, i);
291 291
292 292 pctl->nvprt_indent += pctl->nvprt_indentinc;
293 293 nvlist_print_with_indent(valuep[i], pctl);
294 294 pctl->nvprt_indent -= pctl->nvprt_indentinc;
295 295
296 296 indent(pctl, 1);
297 297 (void) fprintf(fp, "(end %s[%d])\n", name, i);
298 298 }
299 299
300 300 return (1);
301 301 }
302 302
303 303 /*
304 304 * ======================================================================
305 305 * | |
306 306 * | Interfaces that allow control over formatting. |
307 307 * | |
308 308 * ======================================================================
309 309 */
310 310
311 311 void
312 312 nvlist_prtctl_setdest(nvlist_prtctl_t pctl, FILE *fp)
313 313 {
314 314 pctl->nvprt_fp = fp;
315 315 }
316 316
317 317 FILE *
318 318 nvlist_prtctl_getdest(nvlist_prtctl_t pctl)
319 319 {
320 320 return (pctl->nvprt_fp);
321 321 }
322 322
323 323
324 324 void
325 325 nvlist_prtctl_setindent(nvlist_prtctl_t pctl, enum nvlist_indent_mode mode,
326 326 int start, int inc)
327 327 {
328 328 if (mode < NVLIST_INDENT_ABS || mode > NVLIST_INDENT_TABBED)
329 329 mode = NVLIST_INDENT_TABBED;
330 330
331 331 if (start < 0)
332 332 start = 0;
333 333
334 334 if (inc < 0)
335 335 inc = 1;
336 336
337 337 pctl->nvprt_indent_mode = mode;
338 338 pctl->nvprt_indent = start;
339 339 pctl->nvprt_indentinc = inc;
340 340 }
341 341
342 342 void
343 343 nvlist_prtctl_doindent(nvlist_prtctl_t pctl, int onemore)
344 344 {
345 345 indent(pctl, onemore);
346 346 }
347 347
348 348
349 349 void
350 350 nvlist_prtctl_setfmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which,
351 351 const char *fmt)
352 352 {
353 353 switch (which) {
354 354 case NVLIST_FMT_MEMBER_NAME:
355 355 if (fmt == NULL)
356 356 fmt = "%s = ";
357 357 pctl->nvprt_nmfmt = fmt;
358 358 break;
359 359
360 360 case NVLIST_FMT_MEMBER_POSTAMBLE:
361 361 if (fmt == NULL)
362 362 fmt = "\n";
363 363 pctl->nvprt_eomfmt = fmt;
364 364 break;
365 365
366 366 case NVLIST_FMT_BTWN_ARRAY:
367 367 if (fmt == NULL) {
368 368 pctl->nvprt_btwnarrfmt = " ";
369 369 pctl->nvprt_btwnarrfmt_nl = 0;
370 370 } else {
371 371 pctl->nvprt_btwnarrfmt = fmt;
372 372 pctl->nvprt_btwnarrfmt_nl = (strstr(fmt, "\n") != NULL);
373 373 }
374 374 break;
375 375
376 376 default:
377 377 break;
378 378 }
379 379 }
380 380
381 381
382 382 void
383 383 nvlist_prtctl_dofmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which, ...)
384 384 {
385 385 FILE *fp = pctl->nvprt_fp;
386 386 va_list ap;
387 387 char *name;
388 388
389 389 va_start(ap, which);
390 390
391 391 switch (which) {
392 392 case NVLIST_FMT_MEMBER_NAME:
393 393 name = va_arg(ap, char *);
394 394 (void) fprintf(fp, pctl->nvprt_nmfmt, name);
395 395 break;
396 396
397 397 case NVLIST_FMT_MEMBER_POSTAMBLE:
398 398 (void) fprintf(fp, pctl->nvprt_eomfmt);
399 399 break;
400 400
401 401 case NVLIST_FMT_BTWN_ARRAY:
402 402 (void) fprintf(fp, pctl->nvprt_btwnarrfmt); \
403 403 break;
404 404
405 405 default:
406 406 break;
407 407 }
408 408
409 409 va_end(ap);
410 410 }
411 411
412 412 /*
413 413 * ======================================================================
414 414 * | |
415 415 * | Interfaces to allow appointment of replacement rendering functions.|
416 416 * | |
417 417 * ======================================================================
418 418 */
419 419
420 420 #define NVLIST_PRINTCTL_REPLACE(type, vtype) \
421 421 void \
422 422 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
423 423 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype), \
424 424 void *private) \
425 425 { \
426 426 CUSTPRTOP(pctl, type) = func; \
427 427 CUSTPRTOPARG(pctl, type) = private; \
428 428 }
429 429
430 430 NVLIST_PRINTCTL_REPLACE(boolean, int)
431 431 NVLIST_PRINTCTL_REPLACE(boolean_value, boolean_t)
432 432 NVLIST_PRINTCTL_REPLACE(byte, uchar_t)
433 433 NVLIST_PRINTCTL_REPLACE(int8, int8_t)
434 434 NVLIST_PRINTCTL_REPLACE(uint8, uint8_t)
435 435 NVLIST_PRINTCTL_REPLACE(int16, int16_t)
436 436 NVLIST_PRINTCTL_REPLACE(uint16, uint16_t)
437 437 NVLIST_PRINTCTL_REPLACE(int32, int32_t)
438 438 NVLIST_PRINTCTL_REPLACE(uint32, uint32_t)
439 439 NVLIST_PRINTCTL_REPLACE(int64, int64_t)
440 440 NVLIST_PRINTCTL_REPLACE(uint64, uint64_t)
441 441 NVLIST_PRINTCTL_REPLACE(double, double)
442 442 NVLIST_PRINTCTL_REPLACE(string, char *)
443 443 NVLIST_PRINTCTL_REPLACE(hrtime, hrtime_t)
444 444 NVLIST_PRINTCTL_REPLACE(nvlist, nvlist_t *)
445 445
446 446 #define NVLIST_PRINTCTL_AREPLACE(type, vtype) \
447 447 void \
448 448 nvlist_prtctlop_##type(nvlist_prtctl_t pctl, \
449 449 int (*func)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, \
450 450 uint_t), void *private) \
451 451 { \
452 452 CUSTPRTOP(pctl, type) = func; \
453 453 CUSTPRTOPARG(pctl, type) = private; \
454 454 }
455 455
456 456 NVLIST_PRINTCTL_AREPLACE(boolean_array, boolean_t *)
457 457 NVLIST_PRINTCTL_AREPLACE(byte_array, uchar_t *)
458 458 NVLIST_PRINTCTL_AREPLACE(int8_array, int8_t *)
459 459 NVLIST_PRINTCTL_AREPLACE(uint8_array, uint8_t *)
460 460 NVLIST_PRINTCTL_AREPLACE(int16_array, int16_t *)
461 461 NVLIST_PRINTCTL_AREPLACE(uint16_array, uint16_t *)
462 462 NVLIST_PRINTCTL_AREPLACE(int32_array, int32_t *)
463 463 NVLIST_PRINTCTL_AREPLACE(uint32_array, uint32_t *)
464 464 NVLIST_PRINTCTL_AREPLACE(int64_array, int64_t *)
465 465 NVLIST_PRINTCTL_AREPLACE(uint64_array, uint64_t *)
466 466 NVLIST_PRINTCTL_AREPLACE(string_array, char **)
467 467 NVLIST_PRINTCTL_AREPLACE(nvlist_array, nvlist_t **)
468 468
469 469 /*
470 470 * ======================================================================
471 471 * | |
472 472 * | Interfaces to manage nvlist_prtctl_t cookies. |
473 473 * | |
474 474 * ======================================================================
475 475 */
476 476
477 477
478 478 static const struct nvlist_printops defprtops = {
479 479 { nvprint_boolean, NULL },
480 480 { nvprint_boolean_value, NULL },
481 481 { nvprint_byte, NULL },
482 482 { nvprint_int8, NULL },
483 483 { nvprint_uint8, NULL },
484 484 { nvprint_int16, NULL },
485 485 { nvprint_uint16, NULL },
486 486 { nvprint_int32, NULL },
487 487 { nvprint_uint32, NULL },
488 488 { nvprint_int64, NULL },
489 489 { nvprint_uint64, NULL },
490 490 { nvprint_double, NULL },
491 491 { nvprint_string, NULL },
492 492 { nvprint_hrtime, NULL },
493 493 { nvprint_nvlist, NULL },
494 494 { nvaprint_boolean_array, NULL },
495 495 { nvaprint_byte_array, NULL },
496 496 { nvaprint_int8_array, NULL },
497 497 { nvaprint_uint8_array, NULL },
498 498 { nvaprint_int16_array, NULL },
499 499 { nvaprint_uint16_array, NULL },
500 500 { nvaprint_int32_array, NULL },
501 501 { nvaprint_uint32_array, NULL },
502 502 { nvaprint_int64_array, NULL },
503 503 { nvaprint_uint64_array, NULL },
504 504 { nvaprint_string_array, NULL },
505 505 { nvaprint_nvlist_array, NULL },
506 506 };
507 507
508 508 static void
509 509 prtctl_defaults(FILE *fp, struct nvlist_prtctl *pctl,
510 510 struct nvlist_printops *ops)
511 511 {
512 512 pctl->nvprt_fp = fp;
513 513 pctl->nvprt_indent_mode = NVLIST_INDENT_TABBED;
514 514 pctl->nvprt_indent = 0;
515 515 pctl->nvprt_indentinc = 1;
516 516 pctl->nvprt_nmfmt = "%s = ";
517 517 pctl->nvprt_eomfmt = "\n";
518 518 pctl->nvprt_btwnarrfmt = " ";
519 519 pctl->nvprt_btwnarrfmt_nl = 0;
520 520
521 521 pctl->nvprt_dfltops = (struct nvlist_printops *)&defprtops;
522 522 pctl->nvprt_custops = ops;
523 523 }
524 524
525 525 nvlist_prtctl_t
526 526 nvlist_prtctl_alloc(void)
527 527 {
528 528 struct nvlist_prtctl *pctl;
529 529 struct nvlist_printops *ops;
530 530
531 531 if ((pctl = malloc(sizeof (*pctl))) == NULL)
532 532 return (NULL);
533 533
534 534 if ((ops = calloc(1, sizeof (*ops))) == NULL) {
535 535 free(pctl);
536 536 return (NULL);
537 537 }
538 538
539 539 prtctl_defaults(stdout, pctl, ops);
540 540
541 541 return (pctl);
542 542 }
543 543
544 544 void
545 545 nvlist_prtctl_free(nvlist_prtctl_t pctl)
546 546 {
547 547 if (pctl != NULL) {
548 548 free(pctl->nvprt_custops);
549 549 free(pctl);
550 550 }
551 551 }
552 552
553 553 /*
554 554 * ======================================================================
555 555 * | |
556 556 * | Top-level print request interfaces. |
557 557 * | |
558 558 * ======================================================================
559 559 */
560 560
561 561 /*
562 562 * nvlist_print - Prints elements in an event buffer
563 563 */
564 564 static void
565 565 nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl)
566 566 {
567 567 FILE *fp = pctl->nvprt_fp;
568 568 char *name;
569 569 uint_t nelem;
570 570 nvpair_t *nvp;
571 571
572 572 if (nvl == NULL)
573 573 return;
574 574
575 575 indent(pctl, 0);
576 576 (void) fprintf(fp, "nvlist version: %d\n", NVL_VERSION(nvl));
577 577
578 578 nvp = nvlist_next_nvpair(nvl, NULL);
579 579
580 580 while (nvp) {
581 581 data_type_t type = nvpair_type(nvp);
582 582
583 583 name = nvpair_name(nvp);
584 584 nelem = 0;
585 585
586 586 switch (type) {
587 587 case DATA_TYPE_BOOLEAN: {
588 588 RENDER(pctl, boolean, nvl, name, 1);
589 589 break;
590 590 }
591 591 case DATA_TYPE_BOOLEAN_VALUE: {
592 592 boolean_t val;
593 593 (void) nvpair_value_boolean_value(nvp, &val);
594 594 RENDER(pctl, boolean_value, nvl, name, val);
595 595 break;
596 596 }
597 597 case DATA_TYPE_BYTE: {
598 598 uchar_t val;
599 599 (void) nvpair_value_byte(nvp, &val);
600 600 RENDER(pctl, byte, nvl, name, val);
601 601 break;
602 602 }
603 603 case DATA_TYPE_INT8: {
604 604 int8_t val;
605 605 (void) nvpair_value_int8(nvp, &val);
606 606 RENDER(pctl, int8, nvl, name, val);
607 607 break;
608 608 }
609 609 case DATA_TYPE_UINT8: {
610 610 uint8_t val;
611 611 (void) nvpair_value_uint8(nvp, &val);
612 612 RENDER(pctl, uint8, nvl, name, val);
613 613 break;
614 614 }
615 615 case DATA_TYPE_INT16: {
616 616 int16_t val;
617 617 (void) nvpair_value_int16(nvp, &val);
618 618 RENDER(pctl, int16, nvl, name, val);
619 619 break;
620 620 }
621 621 case DATA_TYPE_UINT16: {
622 622 uint16_t val;
623 623 (void) nvpair_value_uint16(nvp, &val);
624 624 RENDER(pctl, uint16, nvl, name, val);
625 625 break;
626 626 }
627 627 case DATA_TYPE_INT32: {
628 628 int32_t val;
629 629 (void) nvpair_value_int32(nvp, &val);
630 630 RENDER(pctl, int32, nvl, name, val);
631 631 break;
632 632 }
633 633 case DATA_TYPE_UINT32: {
634 634 uint32_t val;
635 635 (void) nvpair_value_uint32(nvp, &val);
636 636 RENDER(pctl, uint32, nvl, name, val);
637 637 break;
638 638 }
639 639 case DATA_TYPE_INT64: {
640 640 int64_t val;
641 641 (void) nvpair_value_int64(nvp, &val);
642 642 RENDER(pctl, int64, nvl, name, val);
643 643 break;
644 644 }
645 645 case DATA_TYPE_UINT64: {
646 646 uint64_t val;
647 647 (void) nvpair_value_uint64(nvp, &val);
648 648 RENDER(pctl, uint64, nvl, name, val);
649 649 break;
650 650 }
651 651 case DATA_TYPE_DOUBLE: {
652 652 double val;
653 653 (void) nvpair_value_double(nvp, &val);
654 654 RENDER(pctl, double, nvl, name, val);
655 655 break;
656 656 }
657 657 case DATA_TYPE_STRING: {
658 658 char *val;
659 659 (void) nvpair_value_string(nvp, &val);
660 660 RENDER(pctl, string, nvl, name, val);
661 661 break;
662 662 }
663 663 case DATA_TYPE_BOOLEAN_ARRAY: {
664 664 boolean_t *val;
665 665 (void) nvpair_value_boolean_array(nvp, &val, &nelem);
666 666 ARENDER(pctl, boolean_array, nvl, name, val, nelem);
667 667 break;
668 668 }
669 669 case DATA_TYPE_BYTE_ARRAY: {
670 670 uchar_t *val;
671 671 (void) nvpair_value_byte_array(nvp, &val, &nelem);
672 672 ARENDER(pctl, byte_array, nvl, name, val, nelem);
673 673 break;
674 674 }
675 675 case DATA_TYPE_INT8_ARRAY: {
676 676 int8_t *val;
677 677 (void) nvpair_value_int8_array(nvp, &val, &nelem);
678 678 ARENDER(pctl, int8_array, nvl, name, val, nelem);
679 679 break;
680 680 }
681 681 case DATA_TYPE_UINT8_ARRAY: {
682 682 uint8_t *val;
683 683 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
684 684 ARENDER(pctl, uint8_array, nvl, name, val, nelem);
685 685 break;
686 686 }
687 687 case DATA_TYPE_INT16_ARRAY: {
688 688 int16_t *val;
689 689 (void) nvpair_value_int16_array(nvp, &val, &nelem);
690 690 ARENDER(pctl, int16_array, nvl, name, val, nelem);
691 691 break;
692 692 }
693 693 case DATA_TYPE_UINT16_ARRAY: {
694 694 uint16_t *val;
695 695 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
696 696 ARENDER(pctl, uint16_array, nvl, name, val, nelem);
697 697 break;
698 698 }
699 699 case DATA_TYPE_INT32_ARRAY: {
700 700 int32_t *val;
701 701 (void) nvpair_value_int32_array(nvp, &val, &nelem);
702 702 ARENDER(pctl, int32_array, nvl, name, val, nelem);
703 703 break;
704 704 }
705 705 case DATA_TYPE_UINT32_ARRAY: {
706 706 uint32_t *val;
707 707 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
708 708 ARENDER(pctl, uint32_array, nvl, name, val, nelem);
709 709 break;
710 710 }
711 711 case DATA_TYPE_INT64_ARRAY: {
712 712 int64_t *val;
713 713 (void) nvpair_value_int64_array(nvp, &val, &nelem);
714 714 ARENDER(pctl, int64_array, nvl, name, val, nelem);
715 715 break;
716 716 }
717 717 case DATA_TYPE_UINT64_ARRAY: {
718 718 uint64_t *val;
719 719 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
720 720 ARENDER(pctl, uint64_array, nvl, name, val, nelem);
721 721 break;
722 722 }
723 723 case DATA_TYPE_STRING_ARRAY: {
724 724 char **val;
725 725 (void) nvpair_value_string_array(nvp, &val, &nelem);
726 726 ARENDER(pctl, string_array, nvl, name, val, nelem);
727 727 break;
728 728 }
729 729 case DATA_TYPE_HRTIME: {
730 730 hrtime_t val;
731 731 (void) nvpair_value_hrtime(nvp, &val);
732 732 RENDER(pctl, hrtime, nvl, name, val);
733 733 break;
734 734 }
735 735 case DATA_TYPE_NVLIST: {
736 736 nvlist_t *val;
737 737 (void) nvpair_value_nvlist(nvp, &val);
738 738 RENDER(pctl, nvlist, nvl, name, val);
739 739 break;
740 740 }
741 741 case DATA_TYPE_NVLIST_ARRAY: {
742 742 nvlist_t **val;
743 743 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
744 744 ARENDER(pctl, nvlist_array, nvl, name, val, nelem);
745 745 break;
746 746 }
747 747 default:
748 748 (void) fprintf(fp, " unknown data type (%d)", type);
749 749 break;
750 750 }
751 751 nvp = nvlist_next_nvpair(nvl, nvp);
752 752 }
753 753 }
754 754
755 755 void
756 756 nvlist_print(FILE *fp, nvlist_t *nvl)
757 757 {
758 758 struct nvlist_prtctl pc;
759 759
760 760 prtctl_defaults(fp, &pc, NULL);
761 761 nvlist_print_with_indent(nvl, &pc);
762 762 }
763 763
764 764 void
765 765 nvlist_prt(nvlist_t *nvl, nvlist_prtctl_t pctl)
766 766 {
767 767 nvlist_print_with_indent(nvl, pctl);
768 768 }
769 769
770 770 #define NVP(elem, type, vtype, ptype, format) { \
771 771 vtype value; \
772 772 \
773 773 (void) nvpair_value_##type(elem, &value); \
774 774 (void) printf("%*s%s: " format "\n", indent, "", \
775 775 nvpair_name(elem), (ptype)value); \
776 776 }
777 777
778 778 #define NVPA(elem, type, vtype, ptype, format) { \
779 779 uint_t i, count; \
780 780 vtype *value; \
781 781 \
782 782 (void) nvpair_value_##type(elem, &value, &count); \
783 783 for (i = 0; i < count; i++) { \
784 784 (void) printf("%*s%s[%d]: " format "\n", indent, "", \
785 785 nvpair_name(elem), i, (ptype)value[i]); \
786 786 } \
787 787 }
788 788
789 789 /*
790 790 * Similar to nvlist_print() but handles arrays slightly differently.
791 791 */
792 792 void
793 793 dump_nvlist(nvlist_t *list, int indent)
794 794 {
795 795 nvpair_t *elem = NULL;
796 796 boolean_t bool_value;
797 797 boolean_t *bool_array_value;
798 798 nvlist_t *nvlist_value;
799 799 nvlist_t **nvlist_array_value;
800 800 uint_t i, count;
801 801
802 802 if (list == NULL) {
803 803 return;
804 804 }
805 805
806 806 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
807 807 switch (nvpair_type(elem)) {
808 808 case DATA_TYPE_BOOLEAN:
809 809 (void) printf("%*s%s\n", indent, "", nvpair_name(elem));
810 810 break;
811 811
812 812 case DATA_TYPE_BOOLEAN_VALUE:
813 813 (void) nvpair_value_boolean_value(elem, &bool_value);
814 814 (void) printf("%*s%s: %s\n", indent, "",
815 815 nvpair_name(elem), bool_value ? "true" : "false");
816 816 break;
817 817
818 818 case DATA_TYPE_BYTE:
819 819 NVP(elem, byte, uchar_t, int, "%u");
820 820 break;
821 821
822 822 case DATA_TYPE_INT8:
823 823 NVP(elem, int8, int8_t, int, "%d");
824 824 break;
825 825
826 826 case DATA_TYPE_UINT8:
827 827 NVP(elem, uint8, uint8_t, int, "%u");
828 828 break;
829 829
830 830 case DATA_TYPE_INT16:
831 831 NVP(elem, int16, int16_t, int, "%d");
832 832 break;
833 833
834 834 case DATA_TYPE_UINT16:
835 835 NVP(elem, uint16, uint16_t, int, "%u");
836 836 break;
837 837
838 838 case DATA_TYPE_INT32:
839 839 NVP(elem, int32, int32_t, long, "%ld");
840 840 break;
841 841
842 842 case DATA_TYPE_UINT32:
843 843 NVP(elem, uint32, uint32_t, ulong_t, "%lu");
844 844 break;
845 845
846 846 case DATA_TYPE_INT64:
847 847 NVP(elem, int64, int64_t, longlong_t, "%lld");
848 848 break;
849 849
850 850 case DATA_TYPE_UINT64:
851 851 NVP(elem, uint64, uint64_t, u_longlong_t, "%llu");
852 852 break;
853 853
854 854 case DATA_TYPE_STRING:
855 855 NVP(elem, string, char *, char *, "'%s'");
856 856 break;
857 857
858 858 case DATA_TYPE_BOOLEAN_ARRAY:
859 859 (void) nvpair_value_boolean_array(elem,
860 860 &bool_array_value, &count);
861 861 for (i = 0; i < count; i++) {
862 862 (void) printf("%*s%s[%d]: %s\n", indent, "",
863 863 nvpair_name(elem), i,
864 864 bool_array_value[i] ? "true" : "false");
865 865 }
866 866 break;
867 867
868 868 case DATA_TYPE_BYTE_ARRAY:
869 869 NVPA(elem, byte_array, uchar_t, int, "%u");
870 870 break;
871 871
872 872 case DATA_TYPE_INT8_ARRAY:
873 873 NVPA(elem, int8_array, int8_t, int, "%d");
874 874 break;
875 875
876 876 case DATA_TYPE_UINT8_ARRAY:
877 877 NVPA(elem, uint8_array, uint8_t, int, "%u");
878 878 break;
879 879
880 880 case DATA_TYPE_INT16_ARRAY:
881 881 NVPA(elem, int16_array, int16_t, int, "%d");
882 882 break;
883 883
884 884 case DATA_TYPE_UINT16_ARRAY:
885 885 NVPA(elem, uint16_array, uint16_t, int, "%u");
886 886 break;
887 887
888 888 case DATA_TYPE_INT32_ARRAY:
889 889 NVPA(elem, int32_array, int32_t, long, "%ld");
890 890 break;
891 891
892 892 case DATA_TYPE_UINT32_ARRAY:
893 893 NVPA(elem, uint32_array, uint32_t, ulong_t, "%lu");
894 894 break;
895 895
896 896 case DATA_TYPE_INT64_ARRAY:
897 897 NVPA(elem, int64_array, int64_t, longlong_t, "%lld");
898 898 break;
899 899
900 900 case DATA_TYPE_UINT64_ARRAY:
901 901 NVPA(elem, uint64_array, uint64_t, u_longlong_t,
902 902 "%llu");
903 903 break;
904 904
905 905 case DATA_TYPE_STRING_ARRAY:
906 906 NVPA(elem, string_array, char *, char *, "'%s'");
907 907 break;
908 908
909 909 case DATA_TYPE_NVLIST:
910 910 (void) nvpair_value_nvlist(elem, &nvlist_value);
911 911 (void) printf("%*s%s:\n", indent, "",
912 912 nvpair_name(elem));
913 913 dump_nvlist(nvlist_value, indent + 4);
914 914 break;
915 915
916 916 case DATA_TYPE_NVLIST_ARRAY:
917 917 (void) nvpair_value_nvlist_array(elem,
918 918 &nvlist_array_value, &count);
919 919 for (i = 0; i < count; i++) {
920 920 (void) printf("%*s%s[%u]:\n", indent, "",
921 921 nvpair_name(elem), i);
922 922 dump_nvlist(nvlist_array_value[i], indent + 4);
923 923 }
924 924 break;
925 925
926 926 default:
927 927 (void) printf(dgettext(TEXT_DOMAIN, "bad config type "
928 928 "%d for %s\n"), nvpair_type(elem),
929 929 nvpair_name(elem));
930 930 }
931 931 }
932 932 }
933 933
934 934 /*
935 935 * ======================================================================
936 936 * | |
937 937 * | Misc private interface. |
938 938 * | |
939 939 * ======================================================================
940 940 */
941 941
942 942 /*
943 943 * Determine if string 'value' matches 'nvp' value. The 'value' string is
944 944 * converted, depending on the type of 'nvp', prior to match. For numeric
945 945 * types, a radix independent sscanf conversion of 'value' is used. If 'nvp'
946 946 * is an array type, 'ai' is the index into the array against which we are
947 947 * checking for match. If nvp is of DATA_TYPE_STRING*, the caller can pass
948 948 * in a regex_t compilation of value in 'value_regex' to trigger regular
949 949 * expression string match instead of simple strcmp().
950 950 *
951 951 * Return 1 on match, 0 on no-match, and -1 on error. If the error is
952 952 * related to value syntax error and 'ep' is non-NULL, *ep will point into
953 953 * the 'value' string at the location where the error exists.
954 954 *
955 955 * NOTE: It may be possible to move the non-regex_t version of this into
956 956 * common code used by library/kernel/boot.
957 957 */
958 958 int
959 959 nvpair_value_match_regex(nvpair_t *nvp, int ai,
960 960 char *value, regex_t *value_regex, char **ep)
961 961 {
962 962 char *evalue;
963 963 uint_t a_len;
964 964 int sr;
965 965
966 966 if (ep)
967 967 *ep = NULL;
968 968
969 969 if ((nvp == NULL) || (value == NULL))
970 970 return (-1); /* error fail match - invalid args */
971 971
972 972 /* make sure array and index combination make sense */
973 973 if ((nvpair_type_is_array(nvp) && (ai < 0)) ||
974 974 (!nvpair_type_is_array(nvp) && (ai >= 0)))
975 975 return (-1); /* error fail match - bad index */
976 976
977 977 /* non-string values should be single 'chunk' */
978 978 if ((nvpair_type(nvp) != DATA_TYPE_STRING) &&
979 979 (nvpair_type(nvp) != DATA_TYPE_STRING_ARRAY)) {
980 980 value += strspn(value, " \t");
981 981 evalue = value + strcspn(value, " \t");
982 982 if (*evalue) {
983 983 if (ep)
984 984 *ep = evalue;
985 985 return (-1); /* error fail match - syntax */
986 986 }
987 987 }
988 988
989 989 sr = EOF;
990 990 switch (nvpair_type(nvp)) {
991 991 case DATA_TYPE_STRING: {
992 992 char *val;
993 993
994 994 /* check string value for match */
995 995 if (nvpair_value_string(nvp, &val) == 0) {
996 996 if (value_regex) {
997 997 if (regexec(value_regex, val,
998 998 (size_t)0, NULL, 0) == 0)
999 999 return (1); /* match */
1000 1000 } else {
1001 1001 if (strcmp(value, val) == 0)
1002 1002 return (1); /* match */
1003 1003 }
1004 1004 }
1005 1005 break;
1006 1006 }
1007 1007 case DATA_TYPE_STRING_ARRAY: {
1008 1008 char **val_array;
1009 1009
1010 1010 /* check indexed string value of array for match */
1011 1011 if ((nvpair_value_string_array(nvp, &val_array, &a_len) == 0) &&
1012 1012 (ai < a_len)) {
1013 1013 if (value_regex) {
1014 1014 if (regexec(value_regex, val_array[ai],
1015 1015 (size_t)0, NULL, 0) == 0)
1016 1016 return (1);
1017 1017 } else {
1018 1018 if (strcmp(value, val_array[ai]) == 0)
1019 1019 return (1);
1020 1020 }
1021 1021 }
1022 1022 break;
1023 1023 }
1024 1024 case DATA_TYPE_BYTE: {
1025 1025 uchar_t val, val_arg;
1026 1026
1027 1027 /* scanf uchar_t from value and check for match */
1028 1028 sr = sscanf(value, "%c", &val_arg);
1029 1029 if ((sr == 1) && (nvpair_value_byte(nvp, &val) == 0) &&
1030 1030 (val == val_arg))
1031 1031 return (1);
1032 1032 break;
1033 1033 }
1034 1034 case DATA_TYPE_BYTE_ARRAY: {
1035 1035 uchar_t *val_array, val_arg;
1036 1036
1037 1037
1038 1038 /* check indexed value of array for match */
1039 1039 sr = sscanf(value, "%c", &val_arg);
1040 1040 if ((sr == 1) &&
1041 1041 (nvpair_value_byte_array(nvp, &val_array, &a_len) == 0) &&
1042 1042 (ai < a_len) &&
1043 1043 (val_array[ai] == val_arg))
1044 1044 return (1);
1045 1045 break;
1046 1046 }
1047 1047 case DATA_TYPE_INT8: {
1048 1048 int8_t val, val_arg;
1049 1049
1050 1050 /* scanf int8_t from value and check for match */
1051 1051 sr = sscanf(value, "%"SCNi8, &val_arg);
1052 1052 if ((sr == 1) &&
1053 1053 (nvpair_value_int8(nvp, &val) == 0) &&
1054 1054 (val == val_arg))
1055 1055 return (1);
1056 1056 break;
1057 1057 }
1058 1058 case DATA_TYPE_INT8_ARRAY: {
1059 1059 int8_t *val_array, val_arg;
1060 1060
1061 1061 /* check indexed value of array for match */
1062 1062 sr = sscanf(value, "%"SCNi8, &val_arg);
1063 1063 if ((sr == 1) &&
1064 1064 (nvpair_value_int8_array(nvp, &val_array, &a_len) == 0) &&
1065 1065 (ai < a_len) &&
1066 1066 (val_array[ai] == val_arg))
1067 1067 return (1);
1068 1068 break;
1069 1069 }
1070 1070 case DATA_TYPE_UINT8: {
1071 1071 uint8_t val, val_arg;
1072 1072
1073 1073 /* scanf uint8_t from value and check for match */
1074 1074 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1075 1075 if ((sr == 1) &&
1076 1076 (nvpair_value_uint8(nvp, &val) == 0) &&
1077 1077 (val == val_arg))
1078 1078 return (1);
1079 1079 break;
1080 1080 }
1081 1081 case DATA_TYPE_UINT8_ARRAY: {
1082 1082 uint8_t *val_array, val_arg;
1083 1083
1084 1084 /* check indexed value of array for match */
1085 1085 sr = sscanf(value, "%"SCNi8, (int8_t *)&val_arg);
1086 1086 if ((sr == 1) &&
1087 1087 (nvpair_value_uint8_array(nvp, &val_array, &a_len) == 0) &&
1088 1088 (ai < a_len) &&
1089 1089 (val_array[ai] == val_arg))
1090 1090 return (1);
1091 1091 break;
1092 1092 }
1093 1093 case DATA_TYPE_INT16: {
1094 1094 int16_t val, val_arg;
1095 1095
1096 1096 /* scanf int16_t from value and check for match */
1097 1097 sr = sscanf(value, "%"SCNi16, &val_arg);
1098 1098 if ((sr == 1) &&
1099 1099 (nvpair_value_int16(nvp, &val) == 0) &&
1100 1100 (val == val_arg))
1101 1101 return (1);
1102 1102 break;
1103 1103 }
1104 1104 case DATA_TYPE_INT16_ARRAY: {
1105 1105 int16_t *val_array, val_arg;
1106 1106
1107 1107 /* check indexed value of array for match */
1108 1108 sr = sscanf(value, "%"SCNi16, &val_arg);
1109 1109 if ((sr == 1) &&
1110 1110 (nvpair_value_int16_array(nvp, &val_array, &a_len) == 0) &&
1111 1111 (ai < a_len) &&
1112 1112 (val_array[ai] == val_arg))
1113 1113 return (1);
1114 1114 break;
1115 1115 }
1116 1116 case DATA_TYPE_UINT16: {
1117 1117 uint16_t val, val_arg;
1118 1118
1119 1119 /* scanf uint16_t from value and check for match */
1120 1120 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1121 1121 if ((sr == 1) &&
1122 1122 (nvpair_value_uint16(nvp, &val) == 0) &&
1123 1123 (val == val_arg))
1124 1124 return (1);
1125 1125 break;
1126 1126 }
1127 1127 case DATA_TYPE_UINT16_ARRAY: {
1128 1128 uint16_t *val_array, val_arg;
1129 1129
1130 1130 /* check indexed value of array for match */
1131 1131 sr = sscanf(value, "%"SCNi16, (int16_t *)&val_arg);
1132 1132 if ((sr == 1) &&
1133 1133 (nvpair_value_uint16_array(nvp, &val_array, &a_len) == 0) &&
1134 1134 (ai < a_len) &&
1135 1135 (val_array[ai] == val_arg))
1136 1136 return (1);
1137 1137 break;
1138 1138 }
1139 1139 case DATA_TYPE_INT32: {
1140 1140 int32_t val, val_arg;
1141 1141
1142 1142 /* scanf int32_t from value and check for match */
1143 1143 sr = sscanf(value, "%"SCNi32, &val_arg);
1144 1144 if ((sr == 1) &&
1145 1145 (nvpair_value_int32(nvp, &val) == 0) &&
1146 1146 (val == val_arg))
1147 1147 return (1);
1148 1148 break;
1149 1149 }
1150 1150 case DATA_TYPE_INT32_ARRAY: {
1151 1151 int32_t *val_array, val_arg;
1152 1152
1153 1153 /* check indexed value of array for match */
1154 1154 sr = sscanf(value, "%"SCNi32, &val_arg);
1155 1155 if ((sr == 1) &&
1156 1156 (nvpair_value_int32_array(nvp, &val_array, &a_len) == 0) &&
1157 1157 (ai < a_len) &&
1158 1158 (val_array[ai] == val_arg))
1159 1159 return (1);
1160 1160 break;
1161 1161 }
1162 1162 case DATA_TYPE_UINT32: {
1163 1163 uint32_t val, val_arg;
1164 1164
1165 1165 /* scanf uint32_t from value and check for match */
1166 1166 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1167 1167 if ((sr == 1) &&
1168 1168 (nvpair_value_uint32(nvp, &val) == 0) &&
1169 1169 (val == val_arg))
1170 1170 return (1);
1171 1171 break;
1172 1172 }
1173 1173 case DATA_TYPE_UINT32_ARRAY: {
1174 1174 uint32_t *val_array, val_arg;
1175 1175
1176 1176 /* check indexed value of array for match */
1177 1177 sr = sscanf(value, "%"SCNi32, (int32_t *)&val_arg);
1178 1178 if ((sr == 1) &&
1179 1179 (nvpair_value_uint32_array(nvp, &val_array, &a_len) == 0) &&
1180 1180 (ai < a_len) &&
1181 1181 (val_array[ai] == val_arg))
1182 1182 return (1);
1183 1183 break;
1184 1184 }
1185 1185 case DATA_TYPE_INT64: {
1186 1186 int64_t val, val_arg;
1187 1187
1188 1188 /* scanf int64_t from value and check for match */
1189 1189 sr = sscanf(value, "%"SCNi64, &val_arg);
1190 1190 if ((sr == 1) &&
1191 1191 (nvpair_value_int64(nvp, &val) == 0) &&
1192 1192 (val == val_arg))
1193 1193 return (1);
1194 1194 break;
1195 1195 }
1196 1196 case DATA_TYPE_INT64_ARRAY: {
1197 1197 int64_t *val_array, val_arg;
1198 1198
1199 1199 /* check indexed value of array for match */
1200 1200 sr = sscanf(value, "%"SCNi64, &val_arg);
1201 1201 if ((sr == 1) &&
1202 1202 (nvpair_value_int64_array(nvp, &val_array, &a_len) == 0) &&
1203 1203 (ai < a_len) &&
1204 1204 (val_array[ai] == val_arg))
1205 1205 return (1);
1206 1206 break;
1207 1207 }
1208 1208 case DATA_TYPE_UINT64: {
1209 1209 uint64_t val_arg, val;
1210 1210
1211 1211 /* scanf uint64_t from value and check for match */
1212 1212 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1213 1213 if ((sr == 1) &&
1214 1214 (nvpair_value_uint64(nvp, &val) == 0) &&
1215 1215 (val == val_arg))
1216 1216 return (1);
1217 1217 break;
1218 1218 }
1219 1219 case DATA_TYPE_UINT64_ARRAY: {
1220 1220 uint64_t *val_array, val_arg;
1221 1221
↓ open down ↓ |
997 lines elided |
↑ open up ↑ |
1222 1222 /* check indexed value of array for match */
1223 1223 sr = sscanf(value, "%"SCNi64, (int64_t *)&val_arg);
1224 1224 if ((sr == 1) &&
1225 1225 (nvpair_value_uint64_array(nvp, &val_array, &a_len) == 0) &&
1226 1226 (ai < a_len) &&
1227 1227 (val_array[ai] == val_arg))
1228 1228 return (1);
1229 1229 break;
1230 1230 }
1231 1231 case DATA_TYPE_BOOLEAN_VALUE: {
1232 - boolean_t val, val_arg;
1232 + int32_t val_arg;
1233 + boolean_t val;
1233 1234
1234 1235 /* scanf boolean_t from value and check for match */
1235 1236 sr = sscanf(value, "%"SCNi32, &val_arg);
1236 1237 if ((sr == 1) &&
1237 1238 (nvpair_value_boolean_value(nvp, &val) == 0) &&
1238 1239 (val == val_arg))
1239 1240 return (1);
1240 1241 break;
1241 1242 }
1242 1243 case DATA_TYPE_BOOLEAN_ARRAY: {
1243 - boolean_t *val_array, val_arg;
1244 + boolean_t *val_array;
1245 + int32_t val_arg;
1244 1246
1245 1247 /* check indexed value of array for match */
1246 1248 sr = sscanf(value, "%"SCNi32, &val_arg);
1247 1249 if ((sr == 1) &&
1248 1250 (nvpair_value_boolean_array(nvp,
1249 1251 &val_array, &a_len) == 0) &&
1250 1252 (ai < a_len) &&
1251 1253 (val_array[ai] == val_arg))
1252 1254 return (1);
1253 1255 break;
1254 1256 }
1255 1257 case DATA_TYPE_HRTIME:
1256 1258 case DATA_TYPE_NVLIST:
1257 1259 case DATA_TYPE_NVLIST_ARRAY:
1258 1260 case DATA_TYPE_BOOLEAN:
1259 1261 case DATA_TYPE_DOUBLE:
1260 1262 case DATA_TYPE_UNKNOWN:
1261 1263 default:
1262 1264 /*
1263 1265 * unknown/unsupported data type
1264 1266 */
1265 1267 return (-1); /* error fail match */
1266 1268 }
1267 1269
1268 1270 /*
1269 1271 * check to see if sscanf failed conversion, return approximate
1270 1272 * pointer to problem
1271 1273 */
1272 1274 if (sr != 1) {
1273 1275 if (ep)
1274 1276 *ep = value;
1275 1277 return (-1); /* error fail match - syntax */
1276 1278 }
1277 1279
1278 1280 return (0); /* fail match */
1279 1281 }
1280 1282
1281 1283 int
1282 1284 nvpair_value_match(nvpair_t *nvp, int ai, char *value, char **ep)
1283 1285 {
1284 1286 return (nvpair_value_match_regex(nvp, ai, value, NULL, ep));
1285 1287 }
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX