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