Index: head/share/man/man9/cnv.9 =================================================================== --- head/share/man/man9/cnv.9 (revision 325018) +++ head/share/man/man9/cnv.9 (revision 325019) @@ -1,199 +1,214 @@ .\" .\" Copyright (c) 2016 Adam Starak .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd August 27, 2016 +.Dd October 26, 2017 .Dt CNV 9 .Os .Sh NAME .Nm cnvlist_get, .Nm cnvlist_take, .Nm cnvlist_free, .Nd "API for managing name/value pairs by cookie." .Sh LIBRARY .Lb libnv .Sh SYNOPSIS .In sys/cnv.h +.Ft const char * +.Fn cnvlist_name "void *cookiep" +.Ft int +.Fn cnvlist_type "void *cookiep" +.\" .Ft bool .Fn cnvlist_get_bool "void *cookiep" .Ft uint64_t .Fn cnvlist_get_number "void *cookiep" .Ft "const char *" .Fn cnvlist_get_string "void *cookiep" .Ft "const nvlist_t *" .Fn cnvlist_get_nvlist "void *cookiep" .Ft "const void *" .Fn cnvlist_get_binary "void *cookiep" "size_t *sizep" .Ft "const bool *" .Fn cnvlist_get_bool_array "void *cookiep" "size_t *nitemsp" .Ft "const uint64_t *" .Fn cnvlist_get_number_array "void *cookiep" "size_t *nitemsp" .Ft "const char * const *" .Fn cnvlist_get_string_array "void *cookiep" "size_t *nitemsp" .Ft "const nvlist_t * const *" .Fn cnvlist_get_nvlist_array "void *cookiep" "size_t *nitemsp" .Ft int .Fn cnvlist_get_descriptor "void *cookiep" .Ft "const int *" .Fn cnvlist_get_descriptor_array "void *cookiep" "size_t *nitemsp" .\" .Ft bool .Fn cnvlist_take_bool "void *cookiep" .Ft uint64_t .Fn cnvlist_take_number "void *cookiep" .Ft "const char *" .Fn cnvlist_take_string "void *cookiep" .Ft "const nvlist_t *" .Fn cnvlist_take_nvlist "void *cookiep" .Ft "const void *" .Fn cnvlist_take_binary "void *cookiep" "size_t *sizep" .Ft "const bool *" .Fn cnvlist_take_bool_array "void *cookiep" "size_t *nitemsp" .Ft "const uint64_t *" .Fn cnvlist_take_number_array "void *cookiep" "size_t *nitemsp" .Ft "const char * const *" .Fn cnvlist_take_string_array "void *cookiep" "size_t *nitemsp" .Ft "const nvlist_t * const *" .Fn cnvlist_take_nvlist_array "void *cookiep" "size_t *nitemsp" .Ft int .Fn cnvlist_take_descriptor "void *cookiep" .Ft "const int *" .Fn cnvlist_take_descriptor_array "void *cookiep" "size_t *nitemsp" .\" .Ft void .Fn cnvlist_free_null "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_bool "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_number "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_string "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_nvlist "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_descriptor "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_binary "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_bool_array "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_number_array "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_string_array "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_nvlist_array "nvlist_t *nvl" "void *cookiep" .Ft void .Fn cnvlist_free_descriptor_array "nvlist_t *nvl" "void *cookiep" .Sh DESCRIPTION The .Nm libnv library permits easy management of name/value pairs and can send and receive them over sockets. For more information, also see .Xr nv 9 . .Pp The concept of cookies is explained in .Fn nvlist_next , .Fn nvlist_get_parent , and .Fn nvlist_get_pararr from +.Xr nv 9 . +.Pp +The +.Fn cnvlist_name +function returns the name of an element associated with the given cookie. +.Pp +The +.Fn cnvlist_type +function returns the type of an element associated with the given cookie. +Types which can be returned are described in .Xr nv 9 . .Pp The .Nm cnvlist_get family of functions obtains the value associated with the given cookie. Returned strings, nvlists, descriptors, binaries, or arrays must not be modified by the user, since they still belong to the nvlist. The nvlist must not be in an error state. .Pp The .Nm cnvlist_take family of functions returns the value associated with the given cookie and removes the element from the nvlist. When the value is a string, binary, or array value, the caller is responsible for freeing the returned memory with .Fn free 3 . When the value is an nvlist, the caller is responsible for destroying the returned nvlist with .Fn nvlist_destroy . When the value is a descriptor, the caller is responsible for closing the returned descriptor with the .Fn close 2 . .Pp The .Nm cnvlist_free family of functions removes an element of the supplied cookie and frees all resources. If an element of the given cookie has the wrong type or does not exist, the program is aborted. .Sh EXAMPLE The following example demonstrates how to deal with cnvlist API. .Bd -literal int type; void *cookie, *scookie, *bcookie; nvlist_t *nvl; char *name; nvl = nvlist_create(0); nvlist_add_bool(nvl, "test", 1 == 2); nvlist_add_string(nvl, "test2", "cnvlist"); cookie = NULL; while (nvlist_next(nvl, &type, &cookie) != NULL) { switch (type) { case NV_TYPE_BOOL: printf("test: %d\\n", cnvlist_get_bool(cookie)); bcookie = cookie; break; case NV_TYPE_STRING: printf("test2: %s\\n", cnvlist_get_string(cookie)); scookie = cookie; break; } } name = cnvlist_take_string(nvl, scookie); cnvlist_free_bool(nvl, bcookie); printf("test2: %s\\n", name); free(name); printf("nvlist_empty = %d\\n", nvlist_empty(nvl)); nvlist_destroy(nvl); return (0); .Ed .Sh SEE ALSO .Xr close 2 , .Xr free 3 , .Xr nv 9 .Sh AUTHORS The .Nm cnv API was created during the Google Summer Of Code 2016 by .An Adam Starak . Index: head/sys/contrib/libnv/cnvlist.c =================================================================== --- head/sys/contrib/libnv/cnvlist.c (revision 325018) +++ head/sys/contrib/libnv/cnvlist.c (revision 325019) @@ -1,197 +1,211 @@ /*- * Copyright (c) 2016 Adam Starak * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include __FBSDID("$FreeBSD$"); #ifdef _KERNEL #include #include #include #include #include #include #else #include #include #include #include #endif #include #include #include "nv_impl.h" #include "nvlist_impl.h" #include "nvpair_impl.h" +const char * +cnvlist_name(void *cookiep) +{ + + return (nvpair_name(cookiep)); +} + +int +cnvlist_type(void *cookiep) +{ + + return (nvpair_type(cookiep)); +} + #define CNVLIST_GET(ftype, type, NVTYPE) \ ftype \ cnvlist_get_##type(void *cookiep) \ { \ \ if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookiep)); \ } \ return (nvpair_get_##type(cookiep)); \ } CNVLIST_GET(bool, bool, BOOL) CNVLIST_GET(uint64_t, number, NUMBER) CNVLIST_GET(const char *, string, STRING) CNVLIST_GET(const nvlist_t *, nvlist, NVLIST) #ifndef _KERNEL CNVLIST_GET(int, descriptor, DESCRIPTOR) #endif #undef CNVLIST_GET #define CNVLIST_GET_ARRAY(ftype, type, NVTYPE) \ ftype \ cnvlist_get_##type(void *cookiep, size_t *nitemsp) \ { \ \ if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookiep)); \ } \ return (nvpair_get_##type(cookiep, nitemsp)); \ } CNVLIST_GET_ARRAY(const bool *, bool_array, BOOL_ARRAY) CNVLIST_GET_ARRAY(const uint64_t *, number_array, NUMBER_ARRAY) CNVLIST_GET_ARRAY(const char * const *, string_array, STRING_ARRAY) CNVLIST_GET_ARRAY(const nvlist_t * const *, nvlist_array, NVLIST_ARRAY) #ifndef _KERNEL CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY) #endif #undef CNVLIST_GET_ARRAY const void * cnvlist_get_binary(void *cookiep, size_t *sizep) { if (nvpair_type(cookiep) != NV_TYPE_BINARY) nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookiep)); return (nvpair_get_binary(cookiep, sizep)); } #define CNVLIST_TAKE(ftype, type, NVTYPE) \ ftype \ cnvlist_take_##type(nvlist_t *nvl, void *cookiep) \ { \ ftype value; \ \ if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookiep)); \ } \ value = (ftype)(intptr_t)nvpair_get_##type(cookiep); \ nvlist_remove_nvpair(nvl, cookiep); \ nvpair_free_structure(cookiep); \ return (value); \ } CNVLIST_TAKE(bool, bool, BOOL) CNVLIST_TAKE(uint64_t, number, NUMBER) CNVLIST_TAKE(char *, string, STRING) CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST) #ifndef _KERNEL CNVLIST_TAKE(int, descriptor, DESCRIPTOR) #endif #undef CNVLIST_TAKE #define CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE) \ ftype \ cnvlist_take_##type(nvlist_t *nvl, void *cookiep, size_t *nitemsp) \ { \ ftype value; \ \ if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookiep)); \ } \ value = (ftype)(intptr_t)nvpair_get_##type(cookiep, nitemsp); \ nvlist_remove_nvpair(nvl, cookiep); \ nvpair_free_structure(cookiep); \ return (value); \ } CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY) CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY) CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY) CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY) #ifndef _KERNEL CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY); #endif #undef CNVLIST_TAKE_ARRAY void * cnvlist_take_binary(nvlist_t *nvl, void *cookiep, size_t *sizep) { void *value; if (nvpair_type(cookiep) != NV_TYPE_BINARY) nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookiep)); value = (void *)(intptr_t)nvpair_get_binary(cookiep, sizep); nvlist_remove_nvpair(nvl, cookiep); nvpair_free_structure(cookiep); return (value); } #define CNVLIST_FREE(type) \ void \ cnvlist_free_##type(nvlist_t *nvl, void *cookiep) \ { \ \ nvlist_free_nvpair(nvl, cookiep); \ } CNVLIST_FREE(bool) CNVLIST_FREE(number) CNVLIST_FREE(string) CNVLIST_FREE(nvlist) CNVLIST_FREE(binary); CNVLIST_FREE(bool_array) CNVLIST_FREE(number_array) CNVLIST_FREE(string_array) CNVLIST_FREE(nvlist_array) #ifndef _KERNEL CNVLIST_FREE(descriptor) CNVLIST_FREE(descriptor_array) #endif #undef CNVLIST_FREE Index: head/sys/sys/cnv.h =================================================================== --- head/sys/sys/cnv.h (revision 325018) +++ head/sys/sys/cnv.h (revision 325019) @@ -1,112 +1,118 @@ /*- * Copyright (c) 2016 Adam Starak * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _CNV_H_ #define _CNV_H_ #include #ifndef _KERNEL #include #include #include #include #endif #ifndef _NVLIST_T_DECLARED #define _NVLIST_T_DECLARED struct nvlist; typedef struct nvlist nvlist_t; #endif __BEGIN_DECLS /* + * Functions which returns information about the given cookie. + */ +const char *cnvlist_name(void *cookiep); +int cnvlist_type(void *cookiep); + +/* * The cnvlist_get functions returns value associated with the given cookie. * If it returns a pointer, the pointer represents internal buffer and should * not be freed by the caller. */ bool cnvlist_get_bool(void *cookiep); uint64_t cnvlist_get_number(void *cookiep); const char *cnvlist_get_string(void *cookiep); const nvlist_t *cnvlist_get_nvlist(void *cookiep); const void *cnvlist_get_binary(void *cookiep, size_t *sizep); const bool *cnvlist_get_bool_array(void *cookiep, size_t *nitemsp); const uint64_t *cnvlist_get_number_array(void *cookiep, size_t *nitemsp); const char * const *cnvlist_get_string_array(void *cookiep, size_t *nitemsp); const nvlist_t * const *cnvlist_get_nvlist_array(void *cookiep, size_t *nitemsp); #ifndef _KERNEL int cnvlist_get_descriptor(void *cookiep); const int *cnvlist_get_descriptor_array(void *cookiep, size_t *nitemsp); #endif /* * The cnvlist_take functions returns value associated with the given cookie and * remove the given entry from the nvlist. * The caller is responsible for freeing received data. */ bool cnvlist_take_bool(nvlist_t *nvl, void *cookiep); uint64_t cnvlist_take_number(nvlist_t *nvl, void *cookiep); char *cnvlist_take_string(nvlist_t *nvl, void *cookiep); nvlist_t *cnvlist_take_nvlist(nvlist_t *nvl, void *cookiep); void *cnvlist_take_binary(nvlist_t *nvl, void *cookiep, size_t *sizep); bool *cnvlist_take_bool_array(nvlist_t *nvl, void *cookiep, size_t *nitemsp); uint64_t *cnvlist_take_number_array(nvlist_t *nvl, void *cookiep, size_t *nitemsp); char **cnvlist_take_string_array(nvlist_t *nvl, void *cookiep, size_t *nitemsp); nvlist_t **cnvlist_take_nvlist_array(nvlist_t *nvl, void *cookiep, size_t *nitemsp); #ifndef _KERNEL int cnvlist_take_descriptor(nvlist_t *nvl, void *cookiep); int *cnvlist_take_descriptor_array(nvlist_t *nvl, void *cookiep, size_t *nitemsp); #endif /* * The cnvlist_free functions removes the given name/value pair from the nvlist based on cookie * and frees memory associated with it. */ void cnvlist_free_bool(nvlist_t *nvl, void *cookiep); void cnvlist_free_number(nvlist_t *nvl, void *cookiep); void cnvlist_free_string(nvlist_t *nvl, void *cookiep); void cnvlist_free_nvlist(nvlist_t *nvl, void *cookiep); void cnvlist_free_binary(nvlist_t *nvl, void *cookiep); void cnvlist_free_bool_array(nvlist_t *nvl, void *cookiep); void cnvlist_free_number_array(nvlist_t *nvl, void *cookiep); void cnvlist_free_string_array(nvlist_t *nvl, void *cookiep); void cnvlist_free_nvlist_array(nvlist_t *nvl, void *cookiep); #ifndef _KERNEL void cnvlist_free_descriptor(nvlist_t *nvl, void *cookiep); void cnvlist_free_descriptor_array(nvlist_t *nvl, void *cookiep); #endif __END_DECLS #endif /* !_CNV_H_ */