From ccee9c2e7bbd39684f6d0d1e0c939b4aefc0043e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Boric Date: Mon, 14 Nov 2016 13:59:14 +0100 Subject: [PATCH] import common library libppath --- common/lib/libppath/Makefile.inc | 5 + common/lib/libppath/ppath.3 | 442 ++++++++++++ common/lib/libppath/ppath.c | 927 +++++++++++++++++++++++++ common/lib/libppath/ppath_bool.3 | 235 +++++++ common/lib/libppath/ppath_extant.c | 48 ++ common/lib/libppath/ppath_kmem_alloc.c | 48 ++ common/lib/libppath/ppath_malloc.c | 50 ++ common/lib/libppath/ppath_number.3 | 280 ++++++++ common/lib/libppath/ppath_object.3 | 278 ++++++++ 9 files changed, 2313 insertions(+) create mode 100644 common/lib/libppath/Makefile.inc create mode 100644 common/lib/libppath/ppath.3 create mode 100644 common/lib/libppath/ppath.c create mode 100644 common/lib/libppath/ppath_bool.3 create mode 100644 common/lib/libppath/ppath_extant.c create mode 100644 common/lib/libppath/ppath_kmem_alloc.c create mode 100644 common/lib/libppath/ppath_malloc.c create mode 100644 common/lib/libppath/ppath_number.3 create mode 100644 common/lib/libppath/ppath_object.3 diff --git a/common/lib/libppath/Makefile.inc b/common/lib/libppath/Makefile.inc new file mode 100644 index 000000000..e6344eece --- /dev/null +++ b/common/lib/libppath/Makefile.inc @@ -0,0 +1,5 @@ +# $NetBSD: Makefile.inc,v 1.1 2011/08/25 14:55:36 dyoung Exp $ + +.PATH: ${.PARSEDIR} + +SRCS+= ppath.c ppath_extant.c diff --git a/common/lib/libppath/ppath.3 b/common/lib/libppath/ppath.3 new file mode 100644 index 000000000..5f9590dbd --- /dev/null +++ b/common/lib/libppath/ppath.3 @@ -0,0 +1,442 @@ +.\" $NetBSD: ppath.3,v 1.3 2012/12/29 20:08:23 christos Exp $ +.\" +.\" Copyright (c) 2011 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by David Young . +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY EXPRESS +.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL David Young BE LIABLE FOR ANY +.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd August 24, 2011 +.Dt PPATH 3 +.Os +.Sh NAME +.Nm ppath , +.Nm ppath_idx , +.Nm ppath_key , +.\" , +.Nm ppath_component_retain , +.Nm ppath_component_release , +.\" , +.Nm ppath_create , +.Nm ppath_length , +.Nm ppath_component_idx , +.Nm ppath_component_key , +.Nm ppath_pop , +.Nm ppath_push , +.Nm ppath_component_at , +.Nm ppath_subpath , +.Nm ppath_push_idx , +.Nm ppath_push_key , +.Nm ppath_replace_idx , +.Nm ppath_replace_key , +.\" , +.Nm ppath_copy , +.Nm ppath_retain , +.Nm ppath_release , +.\" , +.Nm ppath_lookup , +.\" , +.Nd property container path library +.Sh LIBRARY +.Lb libppath +.Sh SYNOPSIS +.In ppath/ppath.h +.\" +.Ft "ppath_component_t *" +.Fn ppath_idx "unsigned int index" +.Ft "ppath_component_t *" +.Fn ppath_key "const char *key" +.\" +.Ft "ppath_component_t *" +.Fn ppath_component_retain "ppath_component_t *pc" +.Ft void +.Fn ppath_component_release "ppath_component_t *pc" +.\" +.Ft "ppath_t *" +.Fn ppath_create "void" +.Ft "unsigned int" +.Fn ppath_length "const ppath_t *p" +.Ft int +.Fn ppath_component_idx "const ppath_component_t *" +.Ft const char * +.Fn ppath_component_key "const ppath_component_t *" +.Ft "ppath_t *" +.Fn ppath_pop "ppath_t *" "ppath_component_t **" +.Ft ppath_t * +.Fn ppath_push "ppath_t *" "ppath_component_t *" +.Ft ppath_component_t * +.Fn ppath_component_at "const ppath_t *" "unsigned int" +.Ft ppath_t * +.Fn ppath_subpath "const ppath_t *" "unsigned int" "unsigned int" +.Ft ppath_t * +.Fn ppath_push_idx "ppath_t *" "unsigned int" +.Ft ppath_t * +.Fn ppath_push_key "ppath_t *" "const char *" +.Ft ppath_t * +.Fn ppath_replace_idx "ppath_t *" "unsigned int" +.Ft ppath_t * +.Fn ppath_replace_key "ppath_t *" "const char *" +.\" +.Ft ppath_t * +.Fn ppath_copy "const ppath_t *" +.Ft ppath_t * +.Fn ppath_retain "ppath_t *" +.Ft void +.Fn ppath_release "ppath_t *" +.\" +.Ft prop_object_t +.Fn ppath_lookup "prop_object_t" "const ppath_t *" +.\" +.Sh DESCRIPTION +The +.Nm +library provides functions to read, write, or delete objects in a +property list. +A property-list +.Dq path +names the object in a property list to read, write, or delete. +.Pp +A property-list path is an ordered array of zero or more array +indices and dictionary keys that names at most one +.Vt prop_object_t +in a property list. +The abstract function +.Fn E +evaluates +a property-list path against a +.Vt prop_object_t , +.Va o , +to yield a +.Vt prop_object_t +result according to the following recursive definition, where +.Fa empty +indicates the empty +.Pq zero-length +path and the operator +.Dq | +indicates the concatenation of the path on the left-hand side with +the key or index on the right-hand side: +.Bl -tag -width "E(o, p | index)" +.It Fn E "o" "empty" +Evaluates to +.Fa o . +.It Fn E "o" "p | index" +If +.Fn E "o" "p" +evaluates to a +.Vt prop_array_t , +then +.Fn E "o" "p | index" +evaluates to the +.Fa index 'th +element of that array. +Otherwise, an error occurs. +.It Fn E "o" "p | key" +If +.Fn E "o" "p" +evaluates to a +.Vt prop_dictionary_t , +then +.Fn E "o" "p | key" +evaluates to the dictionary value stored under +.Fa key . +Otherwise, an error occurs. +.El +.Pp +The programmer may think of property-list paths as working similarly +to paths in a file system, where property arrays and dictionaries +correspond to directories, and all other property types correspond +to files. +.Sh DATA TYPES +.Nm +provides two opaque types: +.Bl -tag -width ppath_component +.It Vt ppath_component_t +A property-list path component: a single key or index. +.Nm +counts references to a +.Vt ppath_component_t +and reclaims its storage when there are no more references. +.It Vt ppath_t +An array of zero or more property-list path components. +.Nm +counts references to a +.Vt ppath_t +and reclaims its storage when there are no more references. +.El +.Sh FUNCTIONS +.Nm +provides these functions for manipulating property-list paths +and their components: +.Bl -tag -width ppath +.It Fn ppath_idx "unsigned int index" +Allocate a new +.Vt ppath_component_t +for the given array index and return it. +Its reference count is initially one. +.Pp +If there is not sufficient memory to complete the request, return +.Dv NULL . +.It Fn ppath_key "const char *key" +Allocate a new +.Vt ppath_component_t +for the given dictionary key and return it. +Its reference count is initially one. +.Pp +If there is not sufficient memory to complete the request, return +.Dv NULL . +.\" +.It Fn ppath_component_retain "ppath_component_t *pc" +Increase the reference count on +.Fa pc +by one. +.It Fn ppath_component_release "ppath_component_t *pc" +Decrease the reference count on +.Fa pc +by one. +If the reference count reaches zero, reclaim the storage +for +.Fa pc . +.\" +.It Fn ppath_create "void" +Create a new property-list path and return it. +Its reference count is initially one. +The path's length is initially zero. +.Pp +If there is not sufficient memory to complete the request, return +.Dv NULL . +.It Fn ppath_length "const ppath_t *p" +Return the number of components in path +.Fa p . +.It Fn ppath_component_idx "const ppath_component_t *pc" +Return the array index represented by the component +.Fa pc , +or \-1 if +.Fa pc +does not represent an array index. +.It Fn ppath_component_key "const ppath_component_t *pc" +Return the dictionary key represented by the component +.Fa pc , +or +.Dv NULL +if +.Fa pc +does not represent a dictionary key. +.It Fn ppath_pop "ppath_t *p" "ppath_component_t **pcp" +If +.Fa p +is the empty path or +.Dv NULL , +return +.Dv NULL . +Otherwise, remove the last component from +.Fa p +and return +.Fa p , +and if +.Fa pcp +is not +.Dv NULL , +write the removed component to +.Fa "*pcp" . +.It Fn ppath_push "ppath_t *p" "ppath_component_t *pc" +If +either +.Fa p +is +.Dv NULL +or no more components can be added to +.Fa p , +return +.Dv NULL . +Otherwise, append +.Fa pc +to the end of the component array +.Fa p +and return +.Fa p . +.It Fn ppath_component_at "const ppath_t *p" "unsigned int i" +If +either +.Fa p +is +.Dv NULL +or there is no +.Fa ith +component to +.Fa p , +return +.Dv NULL . +Otherwise, return the +.Fa ith +component of +.Fa p . +Before returning a component, +.Fn ppath_component_at +increases its reference count. +.Po +The first component is 0. +.Pc +.It Fn ppath_subpath "const ppath_t *p" "unsigned int first" "unsigned int exclast" +Create a new +.Vt ppath_t +and fill it with components +.Fa first +to +.Fa exclast +.Pq exclusive +of +.Fa p . +If there are no such components as those in +.Fa p , +.Fn ppath_subpath +returns an empty +.Vt ppath_t . +If there is insufficient memory to create the new path, or if +.Fa p +is +.Dv NULL , +return +.Dv NULL . +Otherwise, return the new path. +.It Fn ppath_push_idx "ppath_t *p" "unsigned int idx" +Append an array index, +.Fa idx , +to the end of path +.Fa p . +If +.Fa p +is +.Dv NULL , +or if there is insufficient memory to complete the operation, +return +.Dv NULL . +Otherwise, return +.Fa p . +.It Fn ppath_push_key "ppath_t *" "const char *key" +Append a dictionary key, +.Fa key , +to the end of path +.Fa p . +If +.Fa p +is +.Dv NULL , +or if there is insufficient memory to complete the operation, +return +.Dv NULL . +Otherwise, return +.Fa p . +.It Fn ppath_replace_idx "ppath_t *p" "unsigned int idx" +Replace the array index at the end of path +.Fa p +with the array index +.Fa idx . +If +.Fa p +is +.Dv NULL , +if the last component of +.Fa p +is not an array index, +or if there is insufficient memory to complete the operation, +return +.Dv NULL . +Otherwise, return +.Fa p . +.It Fn ppath_replace_key "ppath_t *p" "const char *key" +Replace the dictionary key at the end of path +.Fa p +with the dictionary key +.Fa idx . +If +.Fa p +is +.Dv NULL , +if the last component of +.Fa p +is not a dictionary key, +or if there is insufficient memory to complete the operation, +return +.Dv NULL . +Otherwise, return +.Fa p . +.\" +.It Fn ppath_copy "const ppath_t *p" +Create a copy of path +.Fa p . +If +.Fa p +is +.Dv NULL , +or if there is insufficient memory to complete the operation, +return +.Dv NULL . +Otherwise, return the copy, whose reference count will be one. +.It Fn ppath_retain "ppath_t *p" +Increase the reference count on +.Fa p +and return +.Fa p . +.It Fn ppath_release "ppath_t *p" +Decrease the reference count on +.Fa p . +Reclaim the storage for +.Fa p +if the reference count reaches zero. +.\" +.It Fn ppath_lookup "prop_object_t o" "const ppath_t *p" +Return the +.Vt prop_object_t +under +.Fa o +named by +.Fa p , +or return +.Dv NULL +if no such +.Vt prop_object_t +is under +.Fa o . +.El +.Sh SEE ALSO +.\" Cross-references should be ordered by section (low to high), then in +.\" alphabetical order. +.Xr ppath_bool 3 , +.Xr ppath_data 3 , +.Xr ppath_number 3 , +.Xr ppath_object 3 , +.Xr ppath_string 3 , +.Xr proplib 3 +.Sh HISTORY +The +.Nm +property container path library first appeared in +.Nx 6.0 . +.Sh AUTHORS +.An David Young +.Aq dyoung@pobox.com +.\" .Sh CAVEATS +.\" .Sh BUGS +.\" .Sh SECURITY CONSIDERATIONS diff --git a/common/lib/libppath/ppath.c b/common/lib/libppath/ppath.c new file mode 100644 index 000000000..37f31d30d --- /dev/null +++ b/common/lib/libppath/ppath.c @@ -0,0 +1,927 @@ +/* $NetBSD: ppath.c,v 1.4 2012/12/29 20:08:23 christos Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Young . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__RCSID("$NetBSD: ppath.c,v 1.4 2012/12/29 20:08:23 christos Exp $"); + +#ifdef _KERNEL +#include +#endif +#include +#include + +enum _ppath_type { + PPATH_T_IDX = 0 + , PPATH_T_KEY = 1 +}; + +typedef enum _ppath_type ppath_type_t; + +struct _ppath_component { + unsigned int pc_refcnt; + ppath_type_t pc_type; + union { + char *u_key; + unsigned int u_idx; + } pc_u; +#define pc_key pc_u.u_key +#define pc_idx pc_u.u_idx +}; + +struct _ppath { + unsigned int p_refcnt; + unsigned int p_len; + ppath_component_t *p_cmpt[PPATH_MAX_COMPONENTS]; +}; + +static int ppath_copydel_object_of_type(prop_object_t, prop_object_t *, + const ppath_t *, prop_type_t); +static int ppath_copyset_object_helper(prop_object_t, prop_object_t *, + const ppath_t *, const prop_object_t); + +static void +ppath_strfree(char *s) +{ + size_t size = strlen(s) + 1; + + ppath_free(s, size); +} + +static char * +ppath_strdup(const char *s) +{ + size_t size = strlen(s) + 1; + char *p; + + if ((p = ppath_alloc(size)) == NULL) + return NULL; + + return strcpy(p, s); +} + +int +ppath_component_idx(const ppath_component_t *pc) +{ + if (pc->pc_type != PPATH_T_IDX) + return -1; + return pc->pc_idx; +} + +const char * +ppath_component_key(const ppath_component_t *pc) +{ + if (pc->pc_type != PPATH_T_KEY) + return NULL; + return pc->pc_key; +} + +ppath_component_t * +ppath_idx(unsigned int idx) +{ + ppath_component_t *pc; + + if ((pc = ppath_alloc(sizeof(*pc))) == NULL) + return NULL; + pc->pc_idx = idx; + pc->pc_type = PPATH_T_IDX; + pc->pc_refcnt = 1; + ppath_component_extant_inc(); + return pc; +} + +ppath_component_t * +ppath_key(const char *key) +{ + ppath_component_t *pc; + + if ((pc = ppath_alloc(sizeof(*pc))) == NULL) + return NULL; + + if ((pc->pc_key = ppath_strdup(key)) == NULL) { + ppath_free(pc, sizeof(*pc)); + return NULL; + } + pc->pc_type = PPATH_T_KEY; + pc->pc_refcnt = 1; + ppath_component_extant_inc(); + return pc; +} + +ppath_component_t * +ppath_component_retain(ppath_component_t *pc) +{ + ppath_assert(pc->pc_refcnt != 0); + pc->pc_refcnt++; + + return pc; +} + +void +ppath_component_release(ppath_component_t *pc) +{ + ppath_assert(pc->pc_refcnt != 0); + + if (--pc->pc_refcnt != 0) + return; + if (pc->pc_type == PPATH_T_KEY) + ppath_strfree(pc->pc_key); + ppath_component_extant_dec(); + ppath_free(pc, sizeof(*pc)); +} + +ppath_t * +ppath_create(void) +{ + ppath_t *p; + + if ((p = ppath_alloc(sizeof(*p))) == NULL) + return NULL; + + p->p_refcnt = 1; + ppath_extant_inc(); + + return p; +} + +ppath_t * +ppath_pop(ppath_t *p, ppath_component_t **pcp) +{ + ppath_component_t *pc; + + if (p == NULL || p->p_len == 0) + return NULL; + + pc = p->p_cmpt[--p->p_len]; + + if (pcp != NULL) + *pcp = pc; + else + ppath_component_release(pc); + + return p; +} + +ppath_t * +ppath_push(ppath_t *p, ppath_component_t *pc) +{ + if (p == NULL || p->p_len == __arraycount(p->p_cmpt)) + return NULL; + + p->p_cmpt[p->p_len++] = ppath_component_retain(pc); + + return p; +} + +ppath_component_t * +ppath_component_at(const ppath_t *p, unsigned int i) +{ + ppath_component_t *pc; + + if (p == NULL || i >= p->p_len) + return NULL; + + pc = p->p_cmpt[i]; + + return ppath_component_retain(pc); +} + +unsigned int +ppath_length(const ppath_t *p) +{ + return p->p_len; +} + +ppath_t * +ppath_subpath(const ppath_t *p, unsigned int first, unsigned int exclast) +{ + unsigned int i; + ppath_t *np; + ppath_component_t *pc; + + if (p == NULL || (np = ppath_create()) == NULL) + return NULL; + + for (i = first; i < exclast; i++) { + if ((pc = ppath_component_at(p, i)) == NULL) + break; + ppath_push(np, pc); + ppath_component_release(pc); + } + return np; +} + +ppath_t * +ppath_push_idx(ppath_t *p0, unsigned int idx) +{ + ppath_component_t *pc; + ppath_t *p; + + if ((pc = ppath_idx(idx)) == NULL) + return NULL; + + p = ppath_push(p0, pc); + ppath_component_release(pc); + return p; +} + +ppath_t * +ppath_push_key(ppath_t *p0, const char *key) +{ + ppath_component_t *pc; + ppath_t *p; + + if ((pc = ppath_key(key)) == NULL) + return NULL; + + p = ppath_push(p0, pc); + ppath_component_release(pc); + return p; +} + +ppath_t * +ppath_replace_idx(ppath_t *p, unsigned int idx) +{ + ppath_component_t *pc0, *pc; + + if (p == NULL || p->p_len == 0) + return NULL; + + pc0 = p->p_cmpt[p->p_len - 1]; + + if (pc0->pc_type != PPATH_T_IDX) + return NULL; + + if ((pc = ppath_idx(idx)) == NULL) + return NULL; + + p->p_cmpt[p->p_len - 1] = pc; + ppath_component_release(pc0); + + return p; +} + +ppath_t * +ppath_replace_key(ppath_t *p, const char *key) +{ + ppath_component_t *pc0, *pc; + + if (p == NULL || p->p_len == 0) + return NULL; + + pc0 = p->p_cmpt[p->p_len - 1]; + + if (pc0->pc_type != PPATH_T_KEY) + return NULL; + + if ((pc = ppath_key(key)) == NULL) + return NULL; + + p->p_cmpt[p->p_len - 1] = pc; + ppath_component_release(pc0); + + return p; +} + +ppath_t * +ppath_copy(const ppath_t *p0) +{ + ppath_t *p; + unsigned int i; + + if ((p = ppath_create()) == NULL) + return NULL; + + for (i = 0; i < p0->p_len; i++) + p->p_cmpt[i] = ppath_component_retain(p0->p_cmpt[i]); + p->p_len = p0->p_len; + return p; +} + +ppath_t * +ppath_retain(ppath_t *p) +{ + assert(p->p_refcnt != 0); + + p->p_refcnt++; + + return p; +} + +void +ppath_release(ppath_t *p) +{ + unsigned int i; + + assert(p->p_refcnt != 0); + + if (--p->p_refcnt != 0) + return; + + for (i = 0; i < p->p_len; i++) + ppath_component_release(p->p_cmpt[i]); + + ppath_extant_dec(); + ppath_free(p, sizeof(*p)); +} + +static prop_object_t +ppath_lookup_helper(prop_object_t o0, const ppath_t *p, prop_object_t *pop, + ppath_component_t **pcp, unsigned int *ip) +{ + unsigned int i; + prop_object_t o, po; + ppath_type_t t; + ppath_component_t *pc = NULL; + + for (po = NULL, o = o0, i = 0; i < p->p_len && o != NULL; i++) { + pc = p->p_cmpt[i]; + t = pc->pc_type; + switch (prop_object_type(o)) { + case PROP_TYPE_ARRAY: + po = o; + o = (t == PPATH_T_IDX) + ? prop_array_get(o, pc->pc_idx) + : NULL; + break; + case PROP_TYPE_DICTIONARY: + po = o; + o = (t == PPATH_T_KEY) + ? prop_dictionary_get(o, pc->pc_key) + : NULL; + break; + default: + o = NULL; + break; + } + } + if (pop != NULL) + *pop = po; + if (pcp != NULL) + *pcp = pc; + if (ip != NULL) + *ip = i; + return o; +} + +prop_object_t +ppath_lookup(prop_object_t o, const ppath_t *p) +{ + return ppath_lookup_helper(o, p, NULL, NULL, NULL); +} + +static int +ppath_create_object_and_release(prop_object_t o, const ppath_t *p, + prop_object_t v) +{ + int rc; + + rc = ppath_create_object(o, p, v); + prop_object_release(v); + return rc; +} + +int +ppath_create_string(prop_object_t o, const ppath_t *p, const char *s) +{ + return ppath_create_object_and_release(o, p, + prop_string_create_cstring(s)); +} + +int +ppath_create_data(prop_object_t o, const ppath_t *p, + const void *data, size_t size) +{ + return ppath_create_object_and_release(o, p, + prop_data_create_data(data, size)); +} + +int +ppath_create_uint64(prop_object_t o, const ppath_t *p, uint64_t u) +{ + return ppath_create_object_and_release(o, p, + prop_number_create_unsigned_integer(u)); +} + +int +ppath_create_int64(prop_object_t o, const ppath_t *p, int64_t i) +{ + return ppath_create_object_and_release(o, p, + prop_number_create_integer(i)); +} + +int +ppath_create_bool(prop_object_t o, const ppath_t *p, bool b) +{ + return ppath_create_object_and_release(o, p, prop_bool_create(b)); +} + +int +ppath_create_object(prop_object_t o, const ppath_t *p, prop_object_t v) +{ + unsigned int i; + ppath_component_t *pc; + prop_object_t po; + + if (ppath_lookup_helper(o, p, &po, &pc, &i) != NULL) + return EEXIST; + + if (i != ppath_length(p)) + return ENOENT; + + switch (pc->pc_type) { + case PPATH_T_IDX: + return prop_array_set(po, pc->pc_idx, v) ? 0 : ENOMEM; + case PPATH_T_KEY: + return prop_dictionary_set(po, pc->pc_key, v) ? 0 : ENOMEM; + default: + return ENOENT; + } +} + +int +ppath_set_object(prop_object_t o, const ppath_t *p, prop_object_t v) +{ + ppath_component_t *pc; + prop_object_t po; + + if (ppath_lookup_helper(o, p, &po, &pc, NULL) == NULL) + return ENOENT; + + switch (pc->pc_type) { + case PPATH_T_IDX: + return prop_array_set(po, pc->pc_idx, v) ? 0 : ENOMEM; + case PPATH_T_KEY: + return prop_dictionary_set(po, pc->pc_key, v) ? 0 : ENOMEM; + default: + return ENOENT; + } +} + +static int +ppath_set_object_and_release(prop_object_t o, const ppath_t *p, prop_object_t v) +{ + prop_object_t ov; + int rc; + + if (v == NULL) + return ENOMEM; + + if ((ov = ppath_lookup_helper(o, p, NULL, NULL, NULL)) == NULL) + return ENOENT; + + if (prop_object_type(ov) != prop_object_type(v)) + return EFTYPE; + + rc = ppath_set_object(o, p, v); + prop_object_release(v); + return rc; +} + +int +ppath_get_object(prop_object_t o, const ppath_t *p, prop_object_t *vp) +{ + prop_object_t v; + + if ((v = ppath_lookup_helper(o, p, NULL, NULL, NULL)) == NULL) + return ENOENT; + + if (vp != NULL) + *vp = v; + return 0; +} + +static int +ppath_get_object_of_type(prop_object_t o, const ppath_t *p, prop_object_t *vp, + prop_type_t t) +{ + int rc; + + if ((rc = ppath_get_object(o, p, vp)) != 0) + return rc; + + return (prop_object_type(*vp) == t) ? 0 : EFTYPE; +} + +int +ppath_delete_object(prop_object_t o, const ppath_t *p) +{ + ppath_component_t *pc; + prop_object_t po; + + if (ppath_lookup_helper(o, p, &po, &pc, NULL) == NULL) + return ENOENT; + + switch (pc->pc_type) { + case PPATH_T_IDX: + prop_array_remove(po, pc->pc_idx); + return 0; + case PPATH_T_KEY: + prop_dictionary_remove(po, pc->pc_key); + return 0; + default: + return ENOENT; + } +} + +static int +ppath_delete_object_of_type(prop_object_t o, const ppath_t *p, prop_type_t t) +{ + prop_object_t v; + + if ((v = ppath_lookup_helper(o, p, NULL, NULL, NULL)) == NULL) + return ENOENT; + + if (prop_object_type(v) != t) + return EFTYPE; + + return ppath_delete_object(o, p); +} + +int +ppath_copydel_string(prop_object_t o, prop_object_t *op, const ppath_t *p) +{ + return ppath_copydel_object_of_type(o, op, p, PROP_TYPE_STRING); +} + +int +ppath_copydel_data(prop_object_t o, prop_object_t *op, const ppath_t *p) +{ + return ppath_copydel_object_of_type(o, op, p, PROP_TYPE_DATA); +} + +int +ppath_copydel_uint64(prop_object_t o, prop_object_t *op, const ppath_t *p) +{ + return ppath_copydel_object_of_type(o, op, p, PROP_TYPE_NUMBER); +} + +int +ppath_copydel_int64(prop_object_t o, prop_object_t *op, const ppath_t *p) +{ + return ppath_copydel_object_of_type(o, op, p, PROP_TYPE_NUMBER); +} + +int +ppath_copydel_bool(prop_object_t o, prop_object_t *op, const ppath_t *p) +{ + return ppath_copydel_object_of_type(o, op, p, PROP_TYPE_BOOL); +} + +static int +ppath_copydel_object_of_type(prop_object_t o, prop_object_t *op, + const ppath_t *p, prop_type_t t) +{ + prop_object_t v; + + if ((v = ppath_lookup_helper(o, p, NULL, NULL, NULL)) == NULL) + return ENOENT; + + if (prop_object_type(v) != t) + return EFTYPE; + + return ppath_copydel_object(o, op, p); +} + +int +ppath_copydel_object(prop_object_t o, prop_object_t *op, const ppath_t *p) +{ + return ppath_copyset_object_helper(o, op, p, NULL); +} + +int +ppath_copyset_object(prop_object_t o, prop_object_t *op, const ppath_t *p, + const prop_object_t v) +{ + ppath_assert(v != NULL); + return ppath_copyset_object_helper(o, op, p, v); +} + +static int +ppath_copyset_object_helper(prop_object_t o, prop_object_t *op, + const ppath_t *p0, const prop_object_t v0) +{ + bool copy, success; + ppath_component_t *npc, *pc; + ppath_t *cp, *p; + prop_object_t npo = NULL, po, v; + + for (cp = p = ppath_copy(p0), v = v0; + p != NULL; + p = ppath_pop(p, NULL), v = npo) { + + if (ppath_lookup_helper(o, p, &po, &pc, NULL) == NULL) + return ENOENT; + + if (pc == NULL) + break; + + if (ppath_lookup_helper(*op, p, &npo, &npc, NULL) == NULL) + npo = po; + + copy = (npo == po); + + switch (pc->pc_type) { + case PPATH_T_IDX: + if (copy && (npo = prop_array_copy_mutable(po)) == NULL) + return ENOMEM; + success = (v == NULL) + ? (prop_array_remove(npo, pc->pc_idx), true) + : prop_array_set(npo, pc->pc_idx, v); + break; + case PPATH_T_KEY: + if (copy && + (npo = prop_dictionary_copy_mutable(po)) == NULL) + return ENOMEM; + success = (v == NULL) + ? (prop_dictionary_remove(npo, pc->pc_key), true) + : prop_dictionary_set(npo, pc->pc_key, v); + break; + default: + return ENOENT; + } + if (!success) { + if (copy) + prop_object_release(npo); + return ENOMEM; + } + } + + if (cp == NULL) + return ENOMEM; + + ppath_release(cp); + + if (op != NULL && npo != NULL) + *op = npo; + else if (npo != NULL) + prop_object_release(npo); + + return 0; +} + +static int +ppath_copyset_object_and_release(prop_object_t o, prop_object_t *op, + const ppath_t *p, prop_object_t v) +{ + prop_object_t ov; + int rc; + + if (v == NULL) + return ENOMEM; + + if ((ov = ppath_lookup_helper(o, p, NULL, NULL, NULL)) == NULL) + return ENOENT; + + if (prop_object_type(ov) != prop_object_type(v)) + return EFTYPE; + + rc = ppath_copyset_object(o, op, p, v); + prop_object_release(v); + return rc; +} + +int +ppath_copyset_bool(prop_object_t o, prop_object_t *op, const ppath_t *p, bool b) +{ + return ppath_copyset_object_and_release(o, op, p, prop_bool_create(b)); +} + +int +ppath_set_bool(prop_object_t o, const ppath_t *p, bool b) +{ + return ppath_set_object_and_release(o, p, prop_bool_create(b)); +} + +int +ppath_get_bool(prop_object_t o, const ppath_t *p, bool *bp) +{ + prop_object_t v; + int rc; + + if ((rc = ppath_get_object_of_type(o, p, &v, PROP_TYPE_BOOL)) != 0) + return rc; + + if (bp != NULL) + *bp = prop_bool_true(v); + + return 0; +} + +int +ppath_delete_bool(prop_object_t o, const ppath_t *p) +{ + return ppath_delete_object_of_type(o, p, PROP_TYPE_BOOL); +} + +int +ppath_copyset_data(prop_object_t o, prop_object_t *op, const ppath_t *p, + const void *data, size_t size) +{ + return ppath_copyset_object_and_release(o, op, p, + prop_data_create_data(data, size)); +} + +int +ppath_set_data(prop_object_t o, const ppath_t *p, const void *data, size_t size) +{ + return ppath_set_object_and_release(o, p, + prop_data_create_data(data, size)); +} + +int +ppath_get_data(prop_object_t o, const ppath_t *p, const void **datap, + size_t *sizep) +{ + prop_object_t v; + int rc; + + if ((rc = ppath_get_object_of_type(o, p, &v, PROP_TYPE_DATA)) != 0) + return rc; + + if (datap != NULL) + *datap = prop_data_data_nocopy(v); + if (sizep != NULL) + *sizep = prop_data_size(v); + + return 0; +} + +int +ppath_dup_data(prop_object_t o, const ppath_t *p, void **datap, size_t *sizep) +{ + prop_object_t v; + int rc; + + if ((rc = ppath_get_object_of_type(o, p, &v, PROP_TYPE_DATA)) != 0) + return rc; + + if (datap != NULL) + *datap = prop_data_data(v); + if (sizep != NULL) + *sizep = prop_data_size(v); + + return 0; +} + +int +ppath_delete_data(prop_object_t o, const ppath_t *p) +{ + return ppath_delete_object_of_type(o, p, PROP_TYPE_DATA); +} + +int +ppath_copyset_int64(prop_object_t o, prop_object_t *op, const ppath_t *p, + int64_t i) +{ + return ppath_copyset_object_and_release(o, op, p, + prop_number_create_integer(i)); +} + +int +ppath_set_int64(prop_object_t o, const ppath_t *p, int64_t i) +{ + return ppath_set_object_and_release(o, p, + prop_number_create_integer(i)); +} + +int +ppath_get_int64(prop_object_t o, const ppath_t *p, int64_t *ip) +{ + prop_object_t v; + int rc; + + if ((rc = ppath_get_object_of_type(o, p, &v, PROP_TYPE_DATA)) != 0) + return rc; + + if (prop_number_unsigned(v)) + return EFTYPE; + + if (ip != NULL) + *ip = prop_number_integer_value(v); + + return 0; +} + +int +ppath_delete_int64(prop_object_t o, const ppath_t *p) +{ + return ppath_delete_object_of_type(o, p, PROP_TYPE_NUMBER); +} + +int +ppath_copyset_string(prop_object_t o, prop_object_t *op, const ppath_t *p, + const char *s) +{ + return ppath_copyset_object_and_release(o, op, p, + prop_string_create_cstring(s)); +} + +int +ppath_set_string(prop_object_t o, const ppath_t *p, const char *s) +{ + return ppath_set_object_and_release(o, p, + prop_string_create_cstring(s)); +} + +int +ppath_get_string(prop_object_t o, const ppath_t *p, const char **sp) +{ + int rc; + prop_object_t v; + + if ((rc = ppath_get_object_of_type(o, p, &v, PROP_TYPE_STRING)) != 0) + return rc; + + if (sp != NULL) + *sp = prop_string_cstring_nocopy(v); + + return 0; +} + +int +ppath_dup_string(prop_object_t o, const ppath_t *p, char **sp) +{ + int rc; + prop_object_t v; + + if ((rc = ppath_get_object_of_type(o, p, &v, PROP_TYPE_STRING)) != 0) + return rc; + + if (sp != NULL) + *sp = prop_string_cstring(v); + + return 0; +} + +int +ppath_delete_string(prop_object_t o, const ppath_t *p) +{ + return ppath_delete_object_of_type(o, p, PROP_TYPE_STRING); +} + +int +ppath_copyset_uint64(prop_object_t o, prop_object_t *op, const ppath_t *p, + uint64_t u) +{ + return ppath_copyset_object_and_release(o, op, p, + prop_number_create_unsigned_integer(u)); +} + +int +ppath_set_uint64(prop_object_t o, const ppath_t *p, uint64_t u) +{ + return ppath_set_object_and_release(o, p, + prop_number_create_unsigned_integer(u)); +} + +int +ppath_get_uint64(prop_object_t o, const ppath_t *p, uint64_t *up) +{ + prop_object_t v; + int rc; + + if ((rc = ppath_get_object_of_type(o, p, &v, PROP_TYPE_DATA)) != 0) + return rc; + + if (!prop_number_unsigned(v)) + return EFTYPE; + + if (up != NULL) + *up = prop_number_unsigned_integer_value(v); + + return 0; +} + +int +ppath_delete_uint64(prop_object_t o, const ppath_t *p) +{ + return ppath_delete_object_of_type(o, p, PROP_TYPE_NUMBER); +} diff --git a/common/lib/libppath/ppath_bool.3 b/common/lib/libppath/ppath_bool.3 new file mode 100644 index 000000000..01d6bd2aa --- /dev/null +++ b/common/lib/libppath/ppath_bool.3 @@ -0,0 +1,235 @@ +.\" $NetBSD: ppath_bool.3,v 1.6 2012/12/29 20:08:23 christos Exp $ +.\" +.\" Copyright (c) 2011 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by David Young . +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY EXPRESS +.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL David Young BE LIABLE FOR ANY +.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd September 13, 2011 +.Dt PPATH_BOOL 3 +.Os +.Sh NAME +.Nm ppath_bool , +.\" , +.Nm ppath_copydel_bool , +.Nm ppath_copyset_bool , +.Nm ppath_set_bool , +.Nm ppath_get_bool , +.Nm ppath_delete_bool +.Nd boolean property path operations +.Sh LIBRARY +.Lb libppath +.Sh SYNOPSIS +.In ppath/ppath.h +.\" +.Ft int +.Fn ppath_copydel_bool "prop_object_t" "prop_object_t *" "const ppath_t *" +.Ft int +.Fn ppath_copyset_bool "prop_object_t" "prop_object_t *" "const ppath_t *" \ + "bool" +.Ft int +.Fn ppath_set_bool "prop_object_t" "const ppath_t *" "bool" +.Ft int +.Fn ppath_get_bool "prop_object_t" "const ppath_t *" "bool *" +.Ft int +.Fn ppath_delete_bool "prop_object_t" "const ppath_t *" +.Sh DESCRIPTION +The +.Nm +routines read, write, or +delete boolean values in a property list by path. +.Sh FUNCTIONS +.Nm +provides these functions for manipulating boolean values in a property list +by the values' paths: +.Bl -tag -width ppath +.It Fn ppath_copydel_bool "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" +Create a copy of the property list +.Fa o +at +.Fa *op . +Delete from the copy the +.Vt prop_bool_t +named by +.Fa p . +.Pp +If +.Fa *op +is +.Dv NULL , +.Fn ppath_copydel_bool +creates a shallow copy of +.Fa o +at +.Fa *op . +If +.Fa *op +is not +.Dv NULL , +.Fn ppath_copydel_bool +expects for +.Fa *op +to be an existing shallow copy of +.Fa o . +.Pp +For the purposes of +.Fn ppath_copydel_bool , +.Fa *op +is a shallow copy of property list +.Fa o +if equal properties at equal paths are shared between the two. +Before +.Fn ppath_copydel_bool +modifies a property shared by +.Fa *op +and +.Fa o , +it creates a private copy of the property for +.Fa *op . +.It Fn ppath_copyset_bool "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" "bool v" +Create a copy of the property list +.Fa o +at +.Fa *op . +In the copy, replace with +.Fa v +the +.Vt prop_bool_t +named by +.Fa p . +.Pp +If +.Fa *op +is +.Dv NULL , +.Fn ppath_copyset_bool +creates a shallow copy of +.Fa o +at +.Fa *op . +If +.Fa *op +is not +.Dv NULL , +.Fn ppath_copyset_bool +expects for +.Fa *op +to be an existing shallow copy of +.Fa o . +.Pp +For the purposes of +.Fn ppath_copyset_bool , +.Fa *op +is a shallow copy of property list +.Fa o +if equal properties at equal paths are shared between the two. +Before +.Fn ppath_copydel_bool +modifies a property shared by +.Fa *op +and +.Fa o , +it creates a private copy of the property for +.Fa *op . +.It Fn ppath_set_bool "prop_object_t o" "const ppath_t *p" "bool v" +Replace with +.Fa v +the +.Vt prop_bool_t +in +.Fa o +named by +.Fa p . +.It Fn ppath_get_bool "prop_object_t o" "const ppath_t *p" "bool *vp" +Retrieve the +.Vt prop_bool_t +named by +.Fa p +from +.Fa o , +and write it to +.Fa *vp . +.It Fn ppath_delete_bool "prop_object_t o" "const ppath_t *p" +Delete the +.Vt prop_bool_t +named by +.Fa p +from +.Fa o . +.Fn ppath_delete_bool +decreases by one the deleted boolean value's reference count. +.El +.\" +.\" This next request is for sections 2 and 3 function return values only. +.Sh RETURN VALUES +.Nm +routines return 0 on success, and non-zero on error. +.\" The next request is for sections 2 and 3 error and signal handling only. +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EFTYPE +A +.Nm +operation returns +.Er EFTYPE +when the object named by the path is not a +.Vt prop_bool_t . +.It Bq Er ENOENT +.Nm +routines return +.Er ENOENT +if the path +.Fa p +does not exist in +.Fa o . +.It Bq Er ENOMEM +.Fn ppath_set_bool , +and +.Fn ppath_copyset_bool +return +.Er ENOMEM +if there was insufficient memory to complete the operation. +.El +.Sh SEE ALSO +.\" Cross-references should be ordered by section (low to high), then in +.\" alphabetical order. +.Xr ppath 3 , +.Xr ppath_data 3 , +.Xr ppath_object 3 , +.Xr ppath_string 3 , +.Xr proplib 3 +.Sh HISTORY +The +.Nm +property container path library first appeared in +.Nx 6.0 . +.Sh AUTHORS +.An David Young +.Aq dyoung@pobox.com +.\" .Sh CAVEATS +.\" .Sh BUGS +.\" .Sh SECURITY CONSIDERATIONS diff --git a/common/lib/libppath/ppath_extant.c b/common/lib/libppath/ppath_extant.c new file mode 100644 index 000000000..2951f56bc --- /dev/null +++ b/common/lib/libppath/ppath_extant.c @@ -0,0 +1,48 @@ +/* $NetBSD: ppath_extant.c,v 1.2 2012/12/29 20:08:23 christos Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Young . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__RCSID("$NetBSD: ppath_extant.c,v 1.2 2012/12/29 20:08:23 christos Exp $"); + +#include + +void null_extant(void); + +__weak_alias(ppath_component_extant_inc, null_extant) +__weak_alias(ppath_component_extant_dec, null_extant) +__weak_alias(ppath_extant_inc, null_extant) +__weak_alias(ppath_extant_dec, null_extant) + +void +null_extant(void) +{ + /* do nothing */ +} diff --git a/common/lib/libppath/ppath_kmem_alloc.c b/common/lib/libppath/ppath_kmem_alloc.c new file mode 100644 index 000000000..d29775562 --- /dev/null +++ b/common/lib/libppath/ppath_kmem_alloc.c @@ -0,0 +1,48 @@ +/* $NetBSD: ppath_kmem_alloc.c,v 1.2 2012/12/29 20:08:23 christos Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Young . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +__RCSID("$NetBSD: ppath_kmem_alloc.c,v 1.2 2012/12/29 20:08:23 christos Exp $"); + +#include + +void * +ppath_alloc(size_t size) +{ + return kmem_zalloc(size, KM_NOSLEEP); +} + +void +ppath_free(void *p, size_t size) +{ + kmem_free(p, size); +} diff --git a/common/lib/libppath/ppath_malloc.c b/common/lib/libppath/ppath_malloc.c new file mode 100644 index 000000000..07938d839 --- /dev/null +++ b/common/lib/libppath/ppath_malloc.c @@ -0,0 +1,50 @@ +/* $NetBSD: ppath_malloc.c,v 1.2 2012/12/29 20:08:23 christos Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by David Young . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__RCSID("$NetBSD: ppath_malloc.c,v 1.2 2012/12/29 20:08:23 christos Exp $"); + +#include + +#include + +void * +ppath_alloc(size_t size) +{ + return calloc(1, size); +} + +/*ARGSUSED*/ +void +ppath_free(void *p, size_t size) +{ + free(p); +} diff --git a/common/lib/libppath/ppath_number.3 b/common/lib/libppath/ppath_number.3 new file mode 100644 index 000000000..decd226d2 --- /dev/null +++ b/common/lib/libppath/ppath_number.3 @@ -0,0 +1,280 @@ +.\" $NetBSD: ppath_number.3,v 1.4 2012/12/29 20:08:23 christos Exp $ +.\" +.\" Copyright (c) 2011 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by David Young . +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY EXPRESS +.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL David Young BE LIABLE FOR ANY +.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd September 13, 2011 +.Dt PPATH_NUMBER 3 +.Os +.Sh NAME +.Nm ppath_number , +.\" , +.Nm ppath_copydel_int64 , +.Nm ppath_copyset_int64 , +.Nm ppath_set_int64 , +.Nm ppath_get_int64 , +.Nm ppath_delete_int64 , +.\" , +.Nm ppath_copydel_uint64 , +.Nm ppath_copyset_uint64 , +.Nm ppath_set_uint64 , +.Nm ppath_get_uint64 , +.Nm ppath_delete_uint64 +.Nd integer property path operations +.Sh LIBRARY +.Lb libppath +.Sh SYNOPSIS +.In ppath/ppath.h +.\" +.Ft int +.Fn ppath_copydel_int64 "prop_object_t" "prop_object_t *" "const ppath_t *" +.Ft int +.Fn ppath_copyset_int64 "prop_object_t" "prop_object_t *" "const ppath_t *" \ + "int64_t" +.Ft int +.Fn ppath_set_int64 "prop_object_t" "const ppath_t *" "int64_t" +.Ft int +.Fn ppath_get_int64 "prop_object_t" "const ppath_t *" "int64_t *" +.Ft int +.Fn ppath_delete_int64 "prop_object_t" "const ppath_t *" +.\" +.Ft int +.Fn ppath_copydel_uint64 "prop_object_t" "prop_object_t *" "const ppath_t *" +.Ft int +.Fn ppath_copyset_uint64 "prop_object_t" "prop_object_t *" "const ppath_t *" \ + "uint64_t" +.Ft int +.Fn ppath_set_uint64 "prop_object_t" "const ppath_t *" "uint64_t" +.Ft int +.Fn ppath_get_uint64 "prop_object_t" "const ppath_t *" "uint64_t *" +.Ft int +.Fn ppath_delete_uint64 "prop_object_t" "const ppath_t *" +.Sh DESCRIPTION +The +.Nm +routines read, write, or +delete integers in a property list by path. +.Sh FUNCTIONS +.Nm +provides these functions for manipulating integers in a property list +by the integers' paths: +.Bl -tag -width ppath +.It Fn ppath_copydel_int64 "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" +.It Fn ppath_copydel_uint64 "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" +Create a copy of the property list +.Fa o +at +.Fa *op . +Delete from the copy the +.Vt prop_number_t +named by +.Fa p . +.Pp +If +.Fa *op +is +.Dv NULL , +.Fn ppath_copydel_int64 +and +.Fn ppath_copydel_uint64 +create a shallow copy of +.Fa o +at +.Fa *op . +If +.Fa *op +is not +.Dv NULL , +.Fn ppath_copydel_int64 +and +.Fn ppath_copydel_uint64 +expect for +.Fa *op +to be an existing shallow copy of +.Fa o . +.Pp +For the purposes of +.Fn ppath_copydel_int64 +and +.Fn ppath_copydel_uint64 , +.Fa *op +is a shallow copy of property list +.Fa o +if equal properties at equal paths are shared between the two. +Before +.Fn ppath_copydel_int64 +and +.Fn ppath_copydel_uint64 +modify a property shared by +.Fa *op +and +.Fa o , +they create a private copy of the property for +.Fa *op . +.It Fn ppath_copyset_int64 "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" "int64_t v" +.It Fn ppath_copyset_uint64 "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" "uint64_t v" +Create a copy of the property list +.Fa o +at +.Fa *op . +In the copy, replace with +.Fa v +the +.Vt prop_number_t +named by +.Fa p . +.Pp +If +.Fa *op +is +.Dv NULL , +.Fn ppath_copyset_int64 +and +.Fn ppath_copyset_uint64 +create a shallow copy of +.Fa o +at +.Fa *op . +If +.Fa *op +is not +.Dv NULL , +.Fn ppath_copyset_int64 +and +.Fn ppath_copyset_uint64 +expect for +.Fa *op +to be an existing shallow copy of +.Fa o . +.Pp +For the purposes of +.Fn ppath_copyset_int64 +and +.Fn ppath_copyset_uint64 , +.Fa *op +is a shallow copy of property list +.Fa o +if equal properties at equal paths are shared between the two. +Before +.Fn ppath_copydel_int64 +and +.Fn ppath_copydel_uint64 +modify a property shared by +.Fa *op +and +.Fa o , +they create a private copy of the property for +.Fa *op . +.It Fn ppath_set_int64 "prop_object_t o" "const ppath_t *p" "int64_t v" +.It Fn ppath_set_uint64 "prop_object_t o" "const ppath_t *" "uint64_t v" +Replace with +.Fa v +the +.Vt prop_number_t +in +.Fa o +named by +.Fa p . +.It Fn ppath_get_int64 "prop_object_t o" "const ppath_t *p" "int64_t *vp" +.It Fn ppath_get_uint64 "prop_object_t o" "const ppath_t *p" "uint64_t *vp" +Retrieve the +.Vt prop_number_t +named by +.Fa p +from +.Fa o , +and write it to +.Fa *vp . +.It Fn ppath_delete_int64 "prop_object_t o" "const ppath_t *p" +.It Fn ppath_delete_uint64 "prop_object_t o" "const ppath_t *p" +Delete the +.Vt prop_number_t +named by +.Fa p +from +.Fa o . +.Fn ppath_delete_int64 +and +.Fn ppath_delete_uint64 +decrease by one the deleted number's reference count. +.El +.\" +.\" This next request is for sections 2 and 3 function return values only. +.Sh RETURN VALUES +.Nm +routines return 0 on success, and non-zero on error. +.\" The next request is for sections 2 and 3 error and signal handling only. +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EFTYPE +A +.Nm +operation returns +.Er EFTYPE +when the object named by the path is not a +.Vt prop_number_t . +.It Bq Er ENOENT +.Nm +routines return +.Er ENOENT +if the path +.Fa p +does not exist in +.Fa o . +.It Bq Er ENOMEM +.Fn ppath_set_int64 , +.Fn ppath_set_uint64 , +.Fn ppath_copyset_int64 , +and +.Fn ppath_copyset_uint64 +return +.Er ENOMEM +if there was insufficient memory to complete the operation. +.El +.Sh SEE ALSO +.\" Cross-references should be ordered by section (low to high), then in +.\" alphabetical order. +.Xr ppath 3 , +.Xr ppath_data 3 , +.Xr ppath_object 3 , +.Xr ppath_string 3 , +.Xr proplib 3 +.Sh HISTORY +The +.Nm +property container path library first appeared in +.Nx 6.0 . +.Sh AUTHORS +.An David Young +.Aq dyoung@pobox.com +.\" .Sh CAVEATS +.\" .Sh BUGS +.\" .Sh SECURITY CONSIDERATIONS diff --git a/common/lib/libppath/ppath_object.3 b/common/lib/libppath/ppath_object.3 new file mode 100644 index 000000000..51c375f47 --- /dev/null +++ b/common/lib/libppath/ppath_object.3 @@ -0,0 +1,278 @@ +.\" $NetBSD: ppath_object.3,v 1.3 2012/12/29 20:08:23 christos Exp $ +.\" +.\" Copyright (c) 2011 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by David Young . +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY EXPRESS +.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL David Young BE LIABLE FOR ANY +.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd August 24, 2011 +.Dt PPATH_OBJECT 3 +.Os +.Sh NAME +.Nm ppath_object , +.\" , +.Nm ppath_copydel_object , +.Nm ppath_copyset_object , +.Nm ppath_set_object , +.Nm ppath_get_object , +.Nm ppath_delete_object , +.\" , +.Nm ppath_copydel_data , +.Nm ppath_copyset_data , +.Nm ppath_set_data , +.Nm ppath_get_data , +.Nm ppath_dup_data , +.Nm ppath_delete_data , +.\" , +.Nm ppath_copydel_string , +.Nm ppath_copyset_string , +.Nm ppath_set_string , +.Nm ppath_get_string , +.Nm ppath_dup_string , +.Nm ppath_delete_string +.Nd property object path operations +.Sh LIBRARY +.Lb libppath +.Sh SYNOPSIS +.In ppath/ppath.h +.\" +.Ft int +.Fn ppath_copydel_object "prop_object_t" "prop_object_t *" "const ppath_t *" +.Ft int +.Fn ppath_copyset_object "prop_object_t" "prop_object_t *" "const ppath_t *" \ + "prop_object_t" +.Ft int +.Fn ppath_set_object "prop_object_t" "const ppath_t *" "prop_object_t" +.Ft int +.Fn ppath_get_object "prop_object_t" "const ppath_t *" "prop_object_t *" +.Ft int +.Fn ppath_delete_object "prop_object_t" "const ppath_t *" +.\" +.Ft int +.Fn ppath_copydel_data "prop_object_t" "prop_object_t *" "const ppath_t *" +.Ft int +.Fn ppath_copyset_data "prop_object_t" "prop_object_t *" "const ppath_t *" \ + "const void *" "size_t" +.Ft int +.Fn ppath_set_data "prop_object_t" "const ppath_t *" "const void *" "size_t" +.Ft int +.Fn ppath_get_data "prop_object_t" "const ppath_t *" "const void **" "size_t *" +.Ft int +.Fn ppath_dup_data "prop_object_t" "const ppath_t *" "void **" "size_t *" +.Ft int +.Fn ppath_delete_data "prop_object_t" "const ppath_t *" +.\" +.Ft int +.Fn ppath_copydel_string "prop_object_t" "prop_object_t *" "const ppath_t *" +.Ft int +.Fn ppath_copyset_string "prop_object_t" "prop_object_t *" "const ppath_t *" \ + "const char *" +.Ft int +.Fn ppath_set_string "prop_object_t" "const ppath_t *" "const char *" +.Ft int +.Fn ppath_get_string "prop_object_t" "const ppath_t *" "const char **" +.Ft int +.Fn ppath_dup_string "prop_object_t" "const ppath_t *" "char **" +.Ft int +.Fn ppath_delete_string "prop_object_t" "const ppath_t *" +.Sh DESCRIPTION +The +.Nm +routines read, write, or +delete objects in a property list by path. +.Sh FUNCTIONS +.Nm +provides these functions for manipulating objects in a property list +by the objects' paths: +.Bl -tag -width ppath +.It Fn ppath_copydel_object "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" +Create a copy of the property list +.Fa o +at +.Fa *op . +Delete from the copy the property named by +.Fa p . +.Pp +If +.Fa *op +is +.Dv NULL , +.Fn ppath_copydel_object +creates a shallow copy of +.Fa o +at +.Fa *op . +If +.Fa *op +is not +.Dv NULL , +.Fn ppath_copydel_object +expects for +.Fa *op +to be an existing shallow copy of +.Fa o . +.Pp +For the purposes of +.Fn ppath_copydel_object , +.Fa *op +is a shallow copy of property list +.Fa o +if equal properties at equal paths are shared between the two. +Before +.Fn ppath_copydel_object +modifies a property shared by +.Fa *op +and +.Fa o , +it creates a private copy of the property for +.Fa *op . +.It Fn ppath_copyset_object "prop_object_t o" "prop_object_t *op" \ + "const ppath_t *p" "prop_object_t v" +Create a copy of the property list +.Fa o +at +.Fa *op . +In the copy, replace with +.Fa v +the property named by +.Fa p . +.Pp +If +.Fa *op +is +.Dv NULL , +.Fn ppath_copyset_object +creates a shallow copy of +.Fa o +at +.Fa *op . +If +.Fa *op +is not +.Dv NULL , +.Fn ppath_copyset_object +expects for +.Fa *op +to be an existing shallow copy of +.Fa o . +.Pp +For the purposes of +.Fn ppath_copyset_object , +.Fa *op +is a shallow copy of property list +.Fa o +if equal properties at equal paths are shared between the two. +Before +.Fn ppath_copydel_object +modifies a property shared by +.Fa *op +and +.Fa o , +it creates a private copy of the property for +.Fa *op . +.It Fn ppath_set_object "prop_object_t o" "const ppath_t *p" "prop_object_t v" +Replace with +.Fa v +the +.Vt prop_object_t +in +.Fa o +named by +.Fa p . +.It Fn ppath_get_object "prop_object_t o" "const ppath_t *p" "prop_object_t *vp" +Retrieve the +.Vt prop_object_t +named by +.Fa p +from +.Fa o , +and write it to +.Fa *vp . +.Fn ppath_get_object +does +.Em not +increase the reference count of the retrieved object. +.It Fn ppath_delete_object "prop_object_t o" "const ppath_t *p" +Delete the +.Vt prop_object_t +named by +.Fa p +from +.Fa o . +.Fn ppath_delete_object +decreases by one the deleted object's reference count. +.El +.\" +.\" This next request is for sections 2 and 3 function return values only. +.Sh RETURN VALUES +.Nm +routines return 0 on success, and non-zero on error. +.\" The next request is for sections 2 and 3 error and signal handling only. +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EFTYPE +The +.Nm +call requested a +.It Bq Er ENOENT +.Fn ppath_copyset_object , +.Fn ppath_delete_object , +.Fn ppath_get_object , +and +.Fn ppath_set_object +return +.Er ENOENT +if the path +.Fa p +does not exist in +.Fa o . +.It Bq Er ENOMEM +.Fn ppath_set_object +and +.Fn ppath_copyset_object +will return +.Er ENOMEM +if there was insufficient memory to complete the operation. +.El +.Sh SEE ALSO +.\" Cross-references should be ordered by section (low to high), then in +.\" alphabetical order. +.Xr ppath 3 , +.Xr ppath_data 3 , +.Xr ppath_number 3 , +.Xr ppath_string 3 , +.Xr proplib 3 +.Sh HISTORY +The +.Nm +property container path library first appeared in +.Nx 6.0 . +.Sh AUTHORS +.An David Young +.Aq dyoung@pobox.com +.\" .Sh CAVEATS +.\" .Sh BUGS +.\" .Sh SECURITY CONSIDERATIONS