Index: stable/10/cddl/contrib/opensolaris/lib/libnvpair/libnvpair.h =================================================================== --- stable/10/cddl/contrib/opensolaris/lib/libnvpair/libnvpair.h (revision 277588) +++ stable/10/cddl/contrib/opensolaris/lib/libnvpair/libnvpair.h (revision 277589) @@ -1,194 +1,196 @@ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, Joyent, Inc. All rights reserved. */ #ifndef _LIBNVPAIR_H #define _LIBNVPAIR_H #include #include #include #include #ifdef __cplusplus extern "C" { #endif /* * All interfaces described in this file are private to Solaris, and * are subject to change at any time and without notice. The public * nvlist/nvpair interfaces, as documented in manpage sections 3NVPAIR, * are all imported from included above. */ extern int nvpair_value_match(nvpair_t *, int, char *, char **); extern int nvpair_value_match_regex(nvpair_t *, int, char *, regex_t *, char **); extern void nvlist_print(FILE *, nvlist_t *); +extern int nvlist_print_json(FILE *, nvlist_t *); extern void dump_nvlist(nvlist_t *, int); /* * Private nvlist printing interface that allows the caller some control * over output rendering (as opposed to nvlist_print and dump_nvlist). * * Obtain an opaque nvlist_prtctl_t cookie using nvlist_prtctl_alloc * (NULL on failure); on return the cookie is set up for default formatting * and rendering. Quote the cookie in subsequent customisation functions and * then pass the cookie to nvlist_prt to render the nvlist. Finally, * use nvlist_prtctl_free to release the cookie. * * For all nvlist_lookup_xxx and nvlist_lookup_xxx_array functions * we have a corresponding brace of functions that appoint replacement * rendering functions: * * extern void nvlist_prtctl_xxx(nvlist_prtctl_t, * void (*)(nvlist_prtctl_t ctl, void *private, const char *name, * xxxtype value)) * * and * * extern void nvlist_prtctl_xxx_array(nvlist_prtctl_t, * void (*)(nvlist_prtctl_t ctl, void *private, const char *name, * xxxtype value, uint_t count)) * * where xxxtype is the C datatype corresponding to xxx, eg int8_t for "int8" * and char * for "string". The function that is appointed to render the * specified datatype receives as arguments the cookie, the nvlist * member name, the value of that member (or a pointer for array function), * and (for array rendering functions) a count of the number of elements. */ typedef struct nvlist_prtctl *nvlist_prtctl_t; /* opaque */ enum nvlist_indent_mode { NVLIST_INDENT_ABS, /* Absolute indentation */ NVLIST_INDENT_TABBED /* Indent with tabstops */ }; extern nvlist_prtctl_t nvlist_prtctl_alloc(void); extern void nvlist_prtctl_free(nvlist_prtctl_t); extern void nvlist_prt(nvlist_t *, nvlist_prtctl_t); /* Output stream */ extern void nvlist_prtctl_setdest(nvlist_prtctl_t, FILE *); extern FILE *nvlist_prtctl_getdest(nvlist_prtctl_t); /* Indentation mode, start indent, indent increment; default tabbed/0/1 */ extern void nvlist_prtctl_setindent(nvlist_prtctl_t, enum nvlist_indent_mode, int, int); extern void nvlist_prtctl_doindent(nvlist_prtctl_t, int); enum nvlist_prtctl_fmt { NVLIST_FMT_MEMBER_NAME, /* name fmt; default "%s = " */ NVLIST_FMT_MEMBER_POSTAMBLE, /* after nvlist member; default "\n" */ NVLIST_FMT_BTWN_ARRAY /* between array members; default " " */ }; extern void nvlist_prtctl_setfmt(nvlist_prtctl_t, enum nvlist_prtctl_fmt, const char *); extern void nvlist_prtctl_dofmt(nvlist_prtctl_t, enum nvlist_prtctl_fmt, ...); /* * Function prototypes for interfaces that appoint a new rendering function * for single-valued nvlist members. * * A replacement function receives arguments as follows: * * nvlist_prtctl_t Print control structure; do not change preferences * for this object from a print callback function. * * void * The function-private cookie argument registered * when the replacement function was appointed. * * nvlist_t * The full nvlist that is being processed. The * rendering function is called to render a single * member (name and value passed as below) but it may * want to reference or incorporate other aspects of * the full nvlist. * * const char * Member name to render * * valtype Value of the member to render * * The function must return non-zero if it has rendered output for this * member, or 0 if it wants to default to standard rendering for this * one member. */ #define NVLIST_PRINTCTL_SVDECL(funcname, valtype) \ extern void funcname(nvlist_prtctl_t, \ int (*)(nvlist_prtctl_t, void *, nvlist_t *, const char *, valtype), \ void *) NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_boolean, int); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_boolean_value, boolean_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_byte, uchar_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int8, int8_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint8, uint8_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int16, int16_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint16, uint16_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int32, int32_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint32, uint32_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_int64, int64_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_uint64, uint64_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_double, double); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_string, char *); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_hrtime, hrtime_t); NVLIST_PRINTCTL_SVDECL(nvlist_prtctlop_nvlist, nvlist_t *); #undef NVLIST_PRINTCTL_SVDECL /* was just for "clarity" above */ /* * Function prototypes for interfaces that appoint a new rendering function * for array-valued nvlist members. * * One additional argument is taken: uint_t for the number of array elements * * Return values as above. */ #define NVLIST_PRINTCTL_AVDECL(funcname, vtype) \ extern void funcname(nvlist_prtctl_t, \ int (*)(nvlist_prtctl_t, void *, nvlist_t *, const char *, vtype, uint_t), \ void *) NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_boolean_array, boolean_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_byte_array, uchar_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int8_array, int8_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint8_array, uint8_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int16_array, int16_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint16_array, uint16_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int32_array, int32_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint32_array, uint32_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_int64_array, int64_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_uint64_array, uint64_t *); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_string_array, char **); NVLIST_PRINTCTL_AVDECL(nvlist_prtctlop_nvlist_array, nvlist_t **); #undef NVLIST_PRINTCTL_AVDECL /* was just for "clarity" above */ #ifdef __cplusplus } #endif #endif /* _LIBNVPAIR_H */ Index: stable/10/cddl/contrib/opensolaris/lib/libnvpair/nvpair_json.c =================================================================== --- stable/10/cddl/contrib/opensolaris/lib/libnvpair/nvpair_json.c (nonexistent) +++ stable/10/cddl/contrib/opensolaris/lib/libnvpair/nvpair_json.c (revision 277589) @@ -0,0 +1,403 @@ +/* + * This file and its contents are supplied under the terms of the + * Common Development and Distribution License ("CDDL"), version 1.0. + * You may only use this file in accordance with the terms of version + * 1.0 of the CDDL. + * + * A full copy of the text of the CDDL should have accompanied this + * source. A copy of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + */ +/* + * Copyright (c) 2014, Joyent, Inc. + */ + +#include +#include +#include +#include +#include + +#include "libnvpair.h" + +#define FPRINTF(fp, ...) \ + do { \ + if (fprintf(fp, __VA_ARGS__) < 0) \ + return (-1); \ + } while (0) + +/* + * When formatting a string for JSON output we must escape certain characters, + * as described in RFC4627. This applies to both member names and + * DATA_TYPE_STRING values. + * + * This function will only operate correctly if the following conditions are + * met: + * + * 1. The input String is encoded in the current locale. + * + * 2. The current locale includes the Basic Multilingual Plane (plane 0) + * as defined in the Unicode standard. + * + * The output will be entirely 7-bit ASCII (as a subset of UTF-8) with all + * representable Unicode characters included in their escaped numeric form. + */ +static int +nvlist_print_json_string(FILE *fp, const char *input) +{ + mbstate_t mbr; + wchar_t c; + size_t sz; + + bzero(&mbr, sizeof (mbr)); + + FPRINTF(fp, "\""); + while ((sz = mbrtowc(&c, input, MB_CUR_MAX, &mbr)) > 0) { + switch (c) { + case '"': + FPRINTF(fp, "\\\""); + break; + case '\n': + FPRINTF(fp, "\\n"); + break; + case '\r': + FPRINTF(fp, "\\r"); + break; + case '\\': + FPRINTF(fp, "\\\\"); + break; + case '\f': + FPRINTF(fp, "\\f"); + break; + case '\t': + FPRINTF(fp, "\\t"); + break; + case '\b': + FPRINTF(fp, "\\b"); + break; + default: + if ((c >= 0x00 && c <= 0x1f) || + (c > 0x7f && c <= 0xffff)) { + /* + * Render both Control Characters and Unicode + * characters in the Basic Multilingual Plane + * as JSON-escaped multibyte characters. + */ + FPRINTF(fp, "\\u%04x", (int)(0xffff & c)); + } else if (c >= 0x20 && c <= 0x7f) { + /* + * Render other 7-bit ASCII characters directly + * and drop other, unrepresentable characters. + */ + FPRINTF(fp, "%c", (int)(0xff & c)); + } + break; + } + input += sz; + } + + if (sz == (size_t)-1 || sz == (size_t)-2) { + /* + * We last read an invalid multibyte character sequence, + * so return an error. + */ + return (-1); + } + + FPRINTF(fp, "\""); + return (0); +} + +/* + * Dump a JSON-formatted representation of an nvlist to the provided FILE *. + * This routine does not output any new-lines or additional whitespace other + * than that contained in strings, nor does it call fflush(3C). + */ +int +nvlist_print_json(FILE *fp, nvlist_t *nvl) +{ + nvpair_t *curr; + boolean_t first = B_TRUE; + + FPRINTF(fp, "{"); + + for (curr = nvlist_next_nvpair(nvl, NULL); curr; + curr = nvlist_next_nvpair(nvl, curr)) { + data_type_t type = nvpair_type(curr); + + if (!first) + FPRINTF(fp, ","); + else + first = B_FALSE; + + if (nvlist_print_json_string(fp, nvpair_name(curr)) == -1) + return (-1); + FPRINTF(fp, ":"); + + switch (type) { + case DATA_TYPE_STRING: { + char *string = fnvpair_value_string(curr); + if (nvlist_print_json_string(fp, string) == -1) + return (-1); + break; + } + + case DATA_TYPE_BOOLEAN: { + FPRINTF(fp, "true"); + break; + } + + case DATA_TYPE_BOOLEAN_VALUE: { + FPRINTF(fp, "%s", fnvpair_value_boolean_value(curr) == + B_TRUE ? "true" : "false"); + break; + } + + case DATA_TYPE_BYTE: { + FPRINTF(fp, "%hhu", fnvpair_value_byte(curr)); + break; + } + + case DATA_TYPE_INT8: { + FPRINTF(fp, "%hhd", fnvpair_value_int8(curr)); + break; + } + + case DATA_TYPE_UINT8: { + FPRINTF(fp, "%hhu", fnvpair_value_uint8_t(curr)); + break; + } + + case DATA_TYPE_INT16: { + FPRINTF(fp, "%hd", fnvpair_value_int16(curr)); + break; + } + + case DATA_TYPE_UINT16: { + FPRINTF(fp, "%hu", fnvpair_value_uint16(curr)); + break; + } + + case DATA_TYPE_INT32: { + FPRINTF(fp, "%d", fnvpair_value_int32(curr)); + break; + } + + case DATA_TYPE_UINT32: { + FPRINTF(fp, "%u", fnvpair_value_uint32(curr)); + break; + } + + case DATA_TYPE_INT64: { + FPRINTF(fp, "%lld", + (long long)fnvpair_value_int64(curr)); + break; + } + + case DATA_TYPE_UINT64: { + FPRINTF(fp, "%llu", + (unsigned long long)fnvpair_value_uint64(curr)); + break; + } + + case DATA_TYPE_HRTIME: { + hrtime_t val; + VERIFY0(nvpair_value_hrtime(curr, &val)); + FPRINTF(fp, "%llu", (unsigned long long)val); + break; + } + + case DATA_TYPE_DOUBLE: { + double val; + VERIFY0(nvpair_value_double(curr, &val)); + FPRINTF(fp, "%f", val); + break; + } + + case DATA_TYPE_NVLIST: { + if (nvlist_print_json(fp, + fnvpair_value_nvlist(curr)) == -1) + return (-1); + break; + } + + case DATA_TYPE_STRING_ARRAY: { + char **val; + uint_t valsz, i; + VERIFY0(nvpair_value_string_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + if (nvlist_print_json_string(fp, val[i]) == -1) + return (-1); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_NVLIST_ARRAY: { + nvlist_t **val; + uint_t valsz, i; + VERIFY0(nvpair_value_nvlist_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + if (nvlist_print_json(fp, val[i]) == -1) + return (-1); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_BOOLEAN_ARRAY: { + boolean_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_boolean_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, val[i] == B_TRUE ? + "true" : "false"); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_BYTE_ARRAY: { + uchar_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_byte_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%hhu", val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_UINT8_ARRAY: { + uint8_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_uint8_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%hhu", val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_INT8_ARRAY: { + int8_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_int8_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%hhd", val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_UINT16_ARRAY: { + uint16_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_uint16_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%hu", val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_INT16_ARRAY: { + int16_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_int16_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%hd", val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_UINT32_ARRAY: { + uint32_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_uint32_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%u", val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_INT32_ARRAY: { + int32_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_int32_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%d", val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_UINT64_ARRAY: { + uint64_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_uint64_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%llu", + (unsigned long long)val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_INT64_ARRAY: { + int64_t *val; + uint_t valsz, i; + VERIFY0(nvpair_value_int64_array(curr, &val, &valsz)); + FPRINTF(fp, "["); + for (i = 0; i < valsz; i++) { + if (i > 0) + FPRINTF(fp, ","); + FPRINTF(fp, "%lld", (long long)val[i]); + } + FPRINTF(fp, "]"); + break; + } + + case DATA_TYPE_UNKNOWN: + return (-1); + } + } + + FPRINTF(fp, "}"); + return (0); +} Property changes on: stable/10/cddl/contrib/opensolaris/lib/libnvpair/nvpair_json.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: stable/10/cddl/lib/libnvpair/Makefile =================================================================== --- stable/10/cddl/lib/libnvpair/Makefile (revision 277588) +++ stable/10/cddl/lib/libnvpair/Makefile (revision 277589) @@ -1,24 +1,25 @@ # $FreeBSD$ .PATH: ${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libnvpair .PATH: ${.CURDIR}/../../../sys/cddl/contrib/opensolaris/common/nvpair LIB= nvpair SRCS= libnvpair.c \ nvpair_alloc_system.c \ nvpair_alloc_fixed.c \ nvpair.c \ + nvpair_json.c \ fnvpair.c WARNS?= 0 CFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/include CFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/lib/libzpool/common CFLAGS+= -I${.CURDIR}/../../../sys/cddl/compat/opensolaris CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common CFLAGS+= -I${.CURDIR}/../../../sys/cddl/contrib/opensolaris/uts/common/fs/zfs CFLAGS+= -I${.CURDIR}/../../../sys CFLAGS+= -I${.CURDIR}/../../../cddl/contrib/opensolaris/head CFLAGS+= -I${.CURDIR}/../../../cddl/compat/opensolaris/lib/libumem .include Index: stable/10 =================================================================== --- stable/10 (revision 277588) +++ stable/10 (revision 277589) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r275552