1258065Spjd/*-
2258065Spjd * Copyright (c) 2013 The FreeBSD Foundation
3258065Spjd * All rights reserved.
4258065Spjd *
5258065Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from
6258065Spjd * the FreeBSD Foundation.
7258065Spjd *
8258065Spjd * Redistribution and use in source and binary forms, with or without
9258065Spjd * modification, are permitted provided that the following conditions
10258065Spjd * are met:
11258065Spjd * 1. Redistributions of source code must retain the above copyright
12258065Spjd *    notice, this list of conditions and the following disclaimer.
13258065Spjd * 2. Redistributions in binary form must reproduce the above copyright
14258065Spjd *    notice, this list of conditions and the following disclaimer in the
15258065Spjd *    documentation and/or other materials provided with the distribution.
16258065Spjd *
17258065Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18258065Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19258065Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20258065Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21258065Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22258065Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23258065Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24258065Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25258065Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26258065Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27258065Spjd * SUCH DAMAGE.
28258065Spjd */
29258065Spjd
30258065Spjd#include <sys/cdefs.h>
31258065Spjd__FBSDID("$FreeBSD$");
32258065Spjd
33279438Srstone#ifdef _KERNEL
34279438Srstone
35279438Srstone#include <sys/types.h>
36279438Srstone#include <sys/param.h>
37279438Srstone#include <sys/kernel.h>
38279438Srstone#include <sys/systm.h>
39279438Srstone#include <sys/malloc.h>
40279438Srstone
41279438Srstone#include <machine/stdarg.h>
42279438Srstone
43279438Srstone#else
44258065Spjd#include <stdarg.h>
45258065Spjd#include <stdbool.h>
46258065Spjd#include <stdint.h>
47279435Srstone#include <stdlib.h>
48279438Srstone#endif
49258065Spjd
50279439Srstone#include <sys/nv.h>
51279439Srstone#include <sys/nv_impl.h>
52258065Spjd
53279439Srstone#include <sys/dnv.h>
54258065Spjd
55258065Spjd#define	DNVLIST_GET(ftype, type)					\
56258065Spjdftype									\
57258065Spjddnvlist_get_##type(const nvlist_t *nvl, const char *name, ftype defval)	\
58258065Spjd{									\
59258065Spjd									\
60279435Srstone	if (nvlist_exists_##type(nvl, name))				\
61279435Srstone		return (nvlist_get_##type(nvl, name));			\
62279435Srstone	else								\
63279435Srstone		return (defval);					\
64258065Spjd}
65258065Spjd
66258065SpjdDNVLIST_GET(bool, bool)
67258065SpjdDNVLIST_GET(uint64_t, number)
68258065SpjdDNVLIST_GET(const char *, string)
69258065SpjdDNVLIST_GET(const nvlist_t *, nvlist)
70279438Srstone#ifndef _KERNEL
71258065SpjdDNVLIST_GET(int, descriptor)
72279438Srstone#endif
73258065Spjd
74258065Spjd#undef	DNVLIST_GET
75258065Spjd
76258065Spjdconst void *
77258065Spjddnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep,
78258065Spjd    const void *defval, size_t defsize)
79258065Spjd{
80279435Srstone	const void *value;
81258065Spjd
82279435Srstone	if (nvlist_exists_binary(nvl, name))
83279435Srstone		value = nvlist_get_binary(nvl, name, sizep);
84279435Srstone	else {
85279435Srstone		if (sizep != NULL)
86279435Srstone			*sizep = defsize;
87279435Srstone		value = defval;
88279435Srstone	}
89279435Srstone	return (value);
90258065Spjd}
91258065Spjd
92258065Spjd#define	DNVLIST_TAKE(ftype, type)					\
93258065Spjdftype									\
94258065Spjddnvlist_take_##type(nvlist_t *nvl, const char *name, ftype defval)	\
95258065Spjd{									\
96258065Spjd									\
97279435Srstone	if (nvlist_exists_##type(nvl, name))				\
98279435Srstone		return (nvlist_take_##type(nvl, name));			\
99279435Srstone	else								\
100279435Srstone		return (defval);					\
101258065Spjd}
102258065Spjd
103258065SpjdDNVLIST_TAKE(bool, bool)
104258065SpjdDNVLIST_TAKE(uint64_t, number)
105258065SpjdDNVLIST_TAKE(char *, string)
106258065SpjdDNVLIST_TAKE(nvlist_t *, nvlist)
107279438Srstone#ifndef _KERNEL
108258065SpjdDNVLIST_TAKE(int, descriptor)
109279438Srstone#endif
110258065Spjd
111258065Spjd#undef	DNVLIST_TAKE
112258065Spjd
113258065Spjdvoid *
114258065Spjddnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep,
115258065Spjd    void *defval, size_t defsize)
116258065Spjd{
117279435Srstone	void *value;
118258065Spjd
119279435Srstone	if (nvlist_exists_binary(nvl, name))
120279435Srstone		value = nvlist_take_binary(nvl, name, sizep);
121279435Srstone	else {
122279435Srstone		if (sizep != NULL)
123279435Srstone			*sizep = defsize;
124279435Srstone		value = defval;
125279435Srstone	}
126279435Srstone	return (value);
127258065Spjd}
128258065Spjd
129