2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <machine/stdarg.h>
51 #include <sys/nv_impl.h>
55 #define DNVLIST_GET(ftype, type) \
57 dnvlist_get_##type(const nvlist_t *nvl, const char *name, ftype defval) \
60 if (nvlist_exists_##type(nvl, name)) \
61 return (nvlist_get_##type(nvl, name)); \
66 DNVLIST_GET(bool, bool)
67 DNVLIST_GET(uint64_t, number)
68 DNVLIST_GET(const char *, string)
69 DNVLIST_GET(const nvlist_t *, nvlist)
71 DNVLIST_GET(int, descriptor)
77 dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep,
78 const void *defval, size_t defsize)
82 if (nvlist_exists_binary(nvl, name))
83 value = nvlist_get_binary(nvl, name, sizep);
93 #define DNVLIST_GETF(ftype, type) \
95 dnvlist_getf_##type(const nvlist_t *nvl, ftype defval, \
96 const char *namefmt, ...) \
101 va_start(nameap, namefmt); \
102 value = dnvlist_getv_##type(nvl, defval, namefmt, nameap); \
108 DNVLIST_GETF(bool, bool)
109 DNVLIST_GETF(uint64_t, number)
110 DNVLIST_GETF(const char *, string)
111 DNVLIST_GETF(const nvlist_t *, nvlist)
112 DNVLIST_GETF(int, descriptor)
117 dnvlist_getf_binary(const nvlist_t *nvl, size_t *sizep, const void *defval,
118 size_t defsize, const char *namefmt, ...)
123 va_start(nameap, namefmt);
124 value = dnvlist_getv_binary(nvl, sizep, defval, defsize, namefmt,
131 #define DNVLIST_GETV(ftype, type) \
133 dnvlist_getv_##type(const nvlist_t *nvl, ftype defval, \
134 const char *namefmt, va_list nameap) \
139 vasprintf(&name, namefmt, nameap); \
142 value = dnvlist_get_##type(nvl, name, defval); \
147 DNVLIST_GETV(bool, bool)
148 DNVLIST_GETV(uint64_t, number)
149 DNVLIST_GETV(const char *, string)
150 DNVLIST_GETV(const nvlist_t *, nvlist)
151 DNVLIST_GETV(int, descriptor)
156 dnvlist_getv_binary(const nvlist_t *nvl, size_t *sizep, const void *defval,
157 size_t defsize, const char *namefmt, va_list nameap)
162 nv_vasprintf(&name, namefmt, nameap);
164 value = dnvlist_get_binary(nvl, name, sizep, defval, defsize);
175 #define DNVLIST_TAKE(ftype, type) \
177 dnvlist_take_##type(nvlist_t *nvl, const char *name, ftype defval) \
180 if (nvlist_exists_##type(nvl, name)) \
181 return (nvlist_take_##type(nvl, name)); \
186 DNVLIST_TAKE(bool, bool)
187 DNVLIST_TAKE(uint64_t, number)
188 DNVLIST_TAKE(char *, string)
189 DNVLIST_TAKE(nvlist_t *, nvlist)
191 DNVLIST_TAKE(int, descriptor)
197 dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep,
198 void *defval, size_t defsize)
202 if (nvlist_exists_binary(nvl, name))
203 value = nvlist_take_binary(nvl, name, sizep);
213 #define DNVLIST_TAKEF(ftype, type) \
215 dnvlist_takef_##type(nvlist_t *nvl, ftype defval, \
216 const char *namefmt, ...) \
221 va_start(nameap, namefmt); \
222 value = dnvlist_takev_##type(nvl, defval, namefmt, nameap); \
228 DNVLIST_TAKEF(bool, bool)
229 DNVLIST_TAKEF(uint64_t, number)
230 DNVLIST_TAKEF(char *, string)
231 DNVLIST_TAKEF(nvlist_t *, nvlist)
232 DNVLIST_TAKEF(int, descriptor)
237 dnvlist_takef_binary(nvlist_t *nvl, size_t *sizep, void *defval,
238 size_t defsize, const char *namefmt, ...)
243 va_start(nameap, namefmt);
244 value = dnvlist_takev_binary(nvl, sizep, defval, defsize, namefmt,
251 #define DNVLIST_TAKEV(ftype, type) \
253 dnvlist_takev_##type(nvlist_t *nvl, ftype defval, const char *namefmt, \
259 vasprintf(&name, namefmt, nameap); \
262 value = dnvlist_take_##type(nvl, name, defval); \
267 DNVLIST_TAKEV(bool, bool)
268 DNVLIST_TAKEV(uint64_t, number)
269 DNVLIST_TAKEV(char *, string)
270 DNVLIST_TAKEV(nvlist_t *, nvlist)
271 DNVLIST_TAKEV(int, descriptor)
276 dnvlist_takev_binary(nvlist_t *nvl, size_t *sizep, void *defval,
277 size_t defsize, const char *namefmt, va_list nameap)
282 nv_vasprintf(&name, namefmt, nameap);
284 value = dnvlist_take_binary(nvl, name, sizep, defval, defsize);