]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libnv/nv.h
Import libc++ trunk r224926. This fixes a number of bugs, completes
[FreeBSD/FreeBSD.git] / lib / libnv / nv.h
1 /*-
2  * Copyright (c) 2009-2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #ifndef _NV_H_
33 #define _NV_H_
34
35 #include <sys/cdefs.h>
36
37 #include <stdarg.h>
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include <stdio.h>
41
42 #ifndef _NVLIST_T_DECLARED
43 #define _NVLIST_T_DECLARED
44 struct nvlist;
45
46 typedef struct nvlist nvlist_t;
47 #endif
48
49 #define NV_NAME_MAX     2048
50
51 #define NV_TYPE_NONE                    0
52
53 #define NV_TYPE_NULL                    1
54 #define NV_TYPE_BOOL                    2
55 #define NV_TYPE_NUMBER                  3
56 #define NV_TYPE_STRING                  4
57 #define NV_TYPE_NVLIST                  5
58 #define NV_TYPE_DESCRIPTOR              6
59 #define NV_TYPE_BINARY                  7
60
61 /*
62  * Perform case-insensitive lookups of provided names.
63  */
64 #define NV_FLAG_IGNORE_CASE             0x01
65
66 nvlist_t        *nvlist_create(int flags);
67 void             nvlist_destroy(nvlist_t *nvl);
68 int              nvlist_error(const nvlist_t *nvl);
69 bool             nvlist_empty(const nvlist_t *nvl);
70
71 nvlist_t *nvlist_clone(const nvlist_t *nvl);
72
73 void nvlist_dump(const nvlist_t *nvl, int fd);
74 void nvlist_fdump(const nvlist_t *nvl, FILE *fp);
75
76 size_t           nvlist_size(const nvlist_t *nvl);
77 void            *nvlist_pack(const nvlist_t *nvl, size_t *sizep);
78 nvlist_t        *nvlist_unpack(const void *buf, size_t size);
79
80 int nvlist_send(int sock, const nvlist_t *nvl);
81 nvlist_t *nvlist_recv(int sock);
82 nvlist_t *nvlist_xfer(int sock, nvlist_t *nvl);
83
84 const char *nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep);
85
86 const nvlist_t *nvlist_get_parent(const nvlist_t *nvl);
87
88 /*
89  * The nvlist_exists functions check if the given name (optionally of the given
90  * type) exists on nvlist.
91  */
92
93 bool nvlist_exists(const nvlist_t *nvl, const char *name);
94 bool nvlist_exists_type(const nvlist_t *nvl, const char *name, int type);
95
96 bool nvlist_exists_null(const nvlist_t *nvl, const char *name);
97 bool nvlist_exists_bool(const nvlist_t *nvl, const char *name);
98 bool nvlist_exists_number(const nvlist_t *nvl, const char *name);
99 bool nvlist_exists_string(const nvlist_t *nvl, const char *name);
100 bool nvlist_exists_nvlist(const nvlist_t *nvl, const char *name);
101 bool nvlist_exists_descriptor(const nvlist_t *nvl, const char *name);
102 bool nvlist_exists_binary(const nvlist_t *nvl, const char *name);
103
104 /*
105  * The nvlist_add functions add the given name/value pair.
106  * If a pointer is provided, nvlist_add will internally allocate memory for the
107  * given data (in other words it won't consume provided buffer).
108  */
109
110 void nvlist_add_null(nvlist_t *nvl, const char *name);
111 void nvlist_add_bool(nvlist_t *nvl, const char *name, bool value);
112 void nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value);
113 void nvlist_add_string(nvlist_t *nvl, const char *name, const char *value);
114 void nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...) __printflike(3, 4);
115 void nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt, va_list valueap) __printflike(3, 0);
116 void nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value);
117 void nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value);
118 void nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value, size_t size);
119
120 /*
121  * The nvlist_move functions add the given name/value pair.
122  * The functions consumes provided buffer.
123  */
124
125 void nvlist_move_string(nvlist_t *nvl, const char *name, char *value);
126 void nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value);
127 void nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value);
128 void nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size);
129
130 /*
131  * The nvlist_get functions returns value associated with the given name.
132  * If it returns a pointer, the pointer represents internal buffer and should
133  * not be freed by the caller.
134  */
135
136 bool             nvlist_get_bool(const nvlist_t *nvl, const char *name);
137 uint64_t         nvlist_get_number(const nvlist_t *nvl, const char *name);
138 const char      *nvlist_get_string(const nvlist_t *nvl, const char *name);
139 const nvlist_t  *nvlist_get_nvlist(const nvlist_t *nvl, const char *name);
140 int              nvlist_get_descriptor(const nvlist_t *nvl, const char *name);
141 const void      *nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep);
142
143 /*
144  * The nvlist_take functions returns value associated with the given name and
145  * remove the given entry from the nvlist.
146  * The caller is responsible for freeing received data.
147  */
148
149 bool             nvlist_take_bool(nvlist_t *nvl, const char *name);
150 uint64_t         nvlist_take_number(nvlist_t *nvl, const char *name);
151 char            *nvlist_take_string(nvlist_t *nvl, const char *name);
152 nvlist_t        *nvlist_take_nvlist(nvlist_t *nvl, const char *name);
153 int              nvlist_take_descriptor(nvlist_t *nvl, const char *name);
154 void            *nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep);
155
156 /*
157  * The nvlist_free functions removes the given name/value pair from the nvlist
158  * and frees memory associated with it.
159  */
160
161 void nvlist_free(nvlist_t *nvl, const char *name);
162 void nvlist_free_type(nvlist_t *nvl, const char *name, int type);
163
164 void nvlist_free_null(nvlist_t *nvl, const char *name);
165 void nvlist_free_bool(nvlist_t *nvl, const char *name);
166 void nvlist_free_number(nvlist_t *nvl, const char *name);
167 void nvlist_free_string(nvlist_t *nvl, const char *name);
168 void nvlist_free_nvlist(nvlist_t *nvl, const char *name);
169 void nvlist_free_descriptor(nvlist_t *nvl, const char *name);
170 void nvlist_free_binary(nvlist_t *nvl, const char *name);
171
172 /*
173  * Below are the same functions, but which operate on format strings and
174  * variable argument lists.
175  */
176
177 bool nvlist_existsf(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
178 bool nvlist_existsf_type(const nvlist_t *nvl, int type, const char *namefmt, ...) __printflike(3, 4);
179
180 bool nvlist_existsf_null(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
181 bool nvlist_existsf_bool(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
182 bool nvlist_existsf_number(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
183 bool nvlist_existsf_string(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
184 bool nvlist_existsf_nvlist(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
185 bool nvlist_existsf_descriptor(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
186 bool nvlist_existsf_binary(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
187
188 bool nvlist_existsv(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
189 bool nvlist_existsv_type(const nvlist_t *nvl, int type, const char *namefmt, va_list nameap) __printflike(3, 0);
190
191 bool nvlist_existsv_null(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
192 bool nvlist_existsv_bool(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
193 bool nvlist_existsv_number(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
194 bool nvlist_existsv_string(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
195 bool nvlist_existsv_nvlist(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
196 bool nvlist_existsv_descriptor(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
197 bool nvlist_existsv_binary(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
198
199 void nvlist_addf_null(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
200 void nvlist_addf_bool(nvlist_t *nvl, bool value, const char *namefmt, ...) __printflike(3, 4);
201 void nvlist_addf_number(nvlist_t *nvl, uint64_t value, const char *namefmt, ...) __printflike(3, 4);
202 void nvlist_addf_string(nvlist_t *nvl, const char *value, const char *namefmt, ...) __printflike(3, 4);
203 void nvlist_addf_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, ...) __printflike(3, 4);
204 void nvlist_addf_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4);
205 void nvlist_addf_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, ...) __printflike(4, 5);
206
207 void nvlist_addv_null(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
208 void nvlist_addv_bool(nvlist_t *nvl, bool value, const char *namefmt, va_list nameap) __printflike(3, 0);
209 void nvlist_addv_number(nvlist_t *nvl, uint64_t value, const char *namefmt, va_list nameap) __printflike(3, 0);
210 void nvlist_addv_string(nvlist_t *nvl, const char *value, const char *namefmt, va_list nameap) __printflike(3, 0);
211 void nvlist_addv_nvlist(nvlist_t *nvl, const nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0);
212 void nvlist_addv_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0);
213 void nvlist_addv_binary(nvlist_t *nvl, const void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0);
214
215 void nvlist_movef_string(nvlist_t *nvl, char *value, const char *namefmt, ...) __printflike(3, 4);
216 void nvlist_movef_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, ...) __printflike(3, 4);
217 void nvlist_movef_descriptor(nvlist_t *nvl, int value, const char *namefmt, ...) __printflike(3, 4);
218 void nvlist_movef_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, ...) __printflike(4, 5);
219
220 void nvlist_movev_string(nvlist_t *nvl, char *value, const char *namefmt, va_list nameap) __printflike(3, 0);
221 void nvlist_movev_nvlist(nvlist_t *nvl, nvlist_t *value, const char *namefmt, va_list nameap) __printflike(3, 0);
222 void nvlist_movev_descriptor(nvlist_t *nvl, int value, const char *namefmt, va_list nameap) __printflike(3, 0);
223 void nvlist_movev_binary(nvlist_t *nvl, void *value, size_t size, const char *namefmt, va_list nameap) __printflike(4, 0);
224
225 bool             nvlist_getf_bool(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
226 uint64_t         nvlist_getf_number(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
227 const char      *nvlist_getf_string(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
228 const nvlist_t  *nvlist_getf_nvlist(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
229 int              nvlist_getf_descriptor(const nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
230 const void      *nvlist_getf_binary(const nvlist_t *nvl, size_t *sizep, const char *namefmt, ...) __printflike(3, 4);
231
232 bool             nvlist_getv_bool(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
233 uint64_t         nvlist_getv_number(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
234 const char      *nvlist_getv_string(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
235 const nvlist_t  *nvlist_getv_nvlist(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
236 int              nvlist_getv_descriptor(const nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
237 const void      *nvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const char *namefmt, va_list nameap) __printflike(3, 0);
238
239 bool             nvlist_takef_bool(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
240 uint64_t         nvlist_takef_number(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
241 char            *nvlist_takef_string(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
242 nvlist_t        *nvlist_takef_nvlist(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
243 int              nvlist_takef_descriptor(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
244 void            *nvlist_takef_binary(nvlist_t *nvl, size_t *sizep, const char *namefmt, ...) __printflike(3, 4);
245
246 bool             nvlist_takev_bool(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
247 uint64_t         nvlist_takev_number(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
248 char            *nvlist_takev_string(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
249 nvlist_t        *nvlist_takev_nvlist(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
250 int              nvlist_takev_descriptor(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
251 void            *nvlist_takev_binary(nvlist_t *nvl, size_t *sizep, const char *namefmt, va_list nameap) __printflike(3, 0);
252
253 void nvlist_freef(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
254 void nvlist_freef_type(nvlist_t *nvl, int type, const char *namefmt, ...) __printflike(3, 4);
255
256 void nvlist_freef_null(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
257 void nvlist_freef_bool(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
258 void nvlist_freef_number(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
259 void nvlist_freef_string(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
260 void nvlist_freef_nvlist(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
261 void nvlist_freef_descriptor(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
262 void nvlist_freef_binary(nvlist_t *nvl, const char *namefmt, ...) __printflike(2, 3);
263
264 void nvlist_freev(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
265 void nvlist_freev_type(nvlist_t *nvl, int type, const char *namefmt, va_list nameap) __printflike(3, 0);
266
267 void nvlist_freev_null(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
268 void nvlist_freev_bool(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
269 void nvlist_freev_number(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
270 void nvlist_freev_string(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
271 void nvlist_freev_nvlist(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
272 void nvlist_freev_descriptor(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
273 void nvlist_freev_binary(nvlist_t *nvl, const char *namefmt, va_list nameap) __printflike(2, 0);
274
275 #endif  /* !_NV_H_ */