xref: /freebsd/share/man/man9/cnv.9 (revision 4f8f43b06ed07e96a250855488cc531799d5b78f)
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 June 18, 2018
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, also 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 the given cookie.
132.Pp
133The
134.Fn cnvlist_type
135function returns the type of an element associated with the given cookie.
136Types which can be returned are described in
137.Xr nv 9 .
138.Pp
139The
140.Nm cnvlist_get
141family of functions obtains the value associated with the given cookie.
142Returned strings, nvlists, descriptors, binaries, or arrays must not be modified
143by the user, since they still belong to the nvlist.
144The nvlist must not be in an error state.
145.Pp
146The
147.Nm cnvlist_take
148family of functions returns the value associated with the given cookie and
149removes the element from the nvlist.
150When the value is a string, binary, or array value, the caller is responsible
151for freeing the returned memory with
152.Fn free 3 .
153When the value is an nvlist, the caller is responsible for destroying the
154returned nvlist with
155.Fn nvlist_destroy .
156When the value is a descriptor, the caller is responsible for closing the
157returned descriptor with the
158.Fn close 2 .
159.Pp
160The
161.Nm cnvlist_free
162family of functions removes an element of the supplied cookie and frees all
163resources.
164If an element of the given cookie has the wrong type or does not exist, the
165program
166is aborted.
167.Sh EXAMPLES
168The following example demonstrates how to deal with cnvlist API.
169.Bd -literal
170int type;
171void *cookie, *scookie, *bcookie;
172nvlist_t *nvl;
173char *name;
174
175nvl = nvlist_create(0);
176nvlist_add_bool(nvl, "test", 1 == 2);
177nvlist_add_string(nvl, "test2", "cnvlist");
178cookie = NULL;
179
180while (nvlist_next(nvl, &type, &cookie) != NULL) {
181        switch (type) {
182        case NV_TYPE_BOOL:
183                printf("test: %d\\n", cnvlist_get_bool(cookie));
184                bcookie = cookie;
185                break;
186        case NV_TYPE_STRING:
187                printf("test2: %s\\n", cnvlist_get_string(cookie));
188                scookie = cookie;
189                break;
190        }
191}
192
193name = cnvlist_take_string(scookie);
194cnvlist_free_bool(bcookie);
195
196printf("test2: %s\\n", name);
197free(name);
198
199printf("nvlist_empty = %d\\n", nvlist_empty(nvl));
200nvlist_destroy(nvl);
201
202return (0);
203.Ed
204.Sh SEE ALSO
205.Xr close 2 ,
206.Xr free 3 ,
207.Xr nv 9
208.Sh AUTHORS
209The
210.Nm cnv
211API was created during the Google Summer Of Code 2016 by
212.An Adam Starak .
213