1.\" 2.\" Copyright (c) 2016 Adam Starak <starak.adam@gmail.com> 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.Dd January 3, 2025 27.Dt CNV 9 28.Os 29.Sh NAME 30.Nm cnvlist_get , 31.Nm cnvlist_take , 32.Nm cnvlist_free 33.Nd "API for managing name/value pairs by cookie" 34.Sh LIBRARY 35.Lb libnv 36.Sh SYNOPSIS 37.In sys/cnv.h 38.Ft const char * 39.Fn cnvlist_name "const void *cookie" 40.Ft int 41.Fn cnvlist_type "const void *cookie" 42.\" 43.Ft bool 44.Fn cnvlist_get_bool "const void *cookie" 45.Ft uint64_t 46.Fn cnvlist_get_number "const void *cookie" 47.Ft "const char *" 48.Fn cnvlist_get_string "const void *cookie" 49.Ft "const nvlist_t *" 50.Fn cnvlist_get_nvlist "const void *cookie" 51.Ft "const void *" 52.Fn cnvlist_get_binary "const void *cookie" "size_t *sizep" 53.Ft "const bool *" 54.Fn cnvlist_get_bool_array "const void *cookie" "size_t *nitemsp" 55.Ft "const uint64_t *" 56.Fn cnvlist_get_number_array "const void *cookie" "size_t *nitemsp" 57.Ft "const char * const *" 58.Fn cnvlist_get_string_array "const void *cookie" "size_t *nitemsp" 59.Ft "const nvlist_t * const *" 60.Fn cnvlist_get_nvlist_array "const void *cookie" "size_t *nitemsp" 61.Ft int 62.Fn cnvlist_get_descriptor "const void *cookie" 63.Ft "const int *" 64.Fn cnvlist_get_descriptor_array "const void *cookie" "size_t *nitemsp" 65.\" 66.Ft bool 67.Fn cnvlist_take_bool "void *cookie" 68.Ft uint64_t 69.Fn cnvlist_take_number "void *cookie" 70.Ft "const char *" 71.Fn cnvlist_take_string "void *cookie" 72.Ft "const nvlist_t *" 73.Fn cnvlist_take_nvlist "void *cookie" 74.Ft "const void *" 75.Fn cnvlist_take_binary "void *cookie" "size_t *sizep" 76.Ft "const bool *" 77.Fn cnvlist_take_bool_array "void *cookie" "size_t *nitemsp" 78.Ft "const uint64_t *" 79.Fn cnvlist_take_number_array "void *cookie" "size_t *nitemsp" 80.Ft "const char * const *" 81.Fn cnvlist_take_string_array "void *cookie" "size_t *nitemsp" 82.Ft "const nvlist_t * const *" 83.Fn cnvlist_take_nvlist_array "void *cookie" "size_t *nitemsp" 84.Ft int 85.Fn cnvlist_take_descriptor "void *cookie" 86.Ft "const int *" 87.Fn cnvlist_take_descriptor_array "void *cookie" "size_t *nitemsp" 88.\" 89.Ft void 90.Fn cnvlist_free_null "void *cookie" 91.Ft void 92.Fn cnvlist_free_bool "void *cookie" 93.Ft void 94.Fn cnvlist_free_number "void *cookie" 95.Ft void 96.Fn cnvlist_free_string "void *cookie" 97.Ft void 98.Fn cnvlist_free_nvlist "void *cookie" 99.Ft void 100.Fn cnvlist_free_descriptor "void *cookie" 101.Ft void 102.Fn cnvlist_free_binary "void *cookie" 103.Ft void 104.Fn cnvlist_free_bool_array "void *cookie" 105.Ft void 106.Fn cnvlist_free_number_array "void *cookie" 107.Ft void 108.Fn cnvlist_free_string_array "void *cookie" 109.Ft void 110.Fn cnvlist_free_nvlist_array "void *cookie" 111.Ft void 112.Fn cnvlist_free_descriptor_array "void *cookie" 113.Sh DESCRIPTION 114The 115.Nm libnv 116library permits easy management of name/value pairs and can send and receive 117them over sockets. 118For more information, see 119.Xr nv 9 . 120.Pp 121The concept of cookies is explained in 122.Fn nvlist_next , 123.Fn nvlist_get_parent , 124and 125.Fn nvlist_get_pararr 126from 127.Xr nv 9 . 128.Pp 129The 130.Fn cnvlist_name 131function returns the name of an element associated with 132.Fa cookie . 133.Pp 134The 135.Fn cnvlist_type 136function returns the type of an element associated with 137.Fa cookie . 138Types which can be returned are described in 139.Xr nv 9 . 140.Pp 141The 142.Nm cnvlist_get 143functions return the value associated with 144.Fa cookie . 145Returned strings, nvlists, descriptors, binaries, or arrays must not be modified 146by the user since they still belong to the nvlist. 147The nvlist must not be in an error state. 148.Pp 149The 150.Nm cnvlist_take 151functions return the value associated with the given cookie and 152remove the element from the nvlist. 153When the value is a string, binary, or array value, the caller is responsible 154for freeing the returned memory with 155.Fn free 3 . 156When the value is an nvlist, the caller is responsible for destroying the 157returned nvlist with 158.Fn nvlist_destroy . 159When the value is a descriptor, the caller is responsible for closing the 160returned descriptor with 161.Fn close 2 . 162.Pp 163The 164.Nm cnvlist_free 165functions remove the element identified by 166.Fa cookie 167and free any associated resources. 168If the element identified by 169.Fa cookie 170has the wrong type or does not exist, the 171program 172aborts. 173.Sh EXAMPLES 174The following example demonstrates how to deal with the cnvlist API. 175.Bd -literal 176int type; 177void *cookie, *scookie, *bcookie; 178nvlist_t *nvl; 179char *name; 180 181nvl = nvlist_create(0); 182nvlist_add_bool(nvl, "test", 1 == 2); 183nvlist_add_string(nvl, "test2", "cnvlist"); 184cookie = NULL; 185 186while (nvlist_next(nvl, &type, &cookie) != NULL) { 187 switch (type) { 188 case NV_TYPE_BOOL: 189 printf("test: %d\\n", cnvlist_get_bool(cookie)); 190 bcookie = cookie; 191 break; 192 case NV_TYPE_STRING: 193 printf("test2: %s\\n", cnvlist_get_string(cookie)); 194 scookie = cookie; 195 break; 196 } 197} 198 199name = cnvlist_take_string(scookie); 200cnvlist_free_bool(bcookie); 201 202printf("test2: %s\\n", name); 203free(name); 204 205printf("nvlist_empty = %d\\n", nvlist_empty(nvl)); 206nvlist_destroy(nvl); 207 208return (0); 209.Ed 210.Sh SEE ALSO 211.Xr close 2 , 212.Xr free 3 , 213.Xr nv 9 214.Sh AUTHORS 215The 216.Nm cnv 217API was created during the Google Summer Of Code 2016 by 218.An Adam Starak . 219