3 * Simple property list handling code.
6 * Jordan Hubbard. All rights reserved.
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 * verbatim and that no modifications are made prior to this
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR HIS PETS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/lib/libutil/property.c,v 1.5.6.1 2000/11/22 03:49:49 murray Exp $
32 * $DragonFly: src/lib/libutil/property.c,v 1.3 2005/03/04 05:31:11 cpressey Exp $
36 #include <sys/types.h>
48 property_alloc(char *name
, char *value
)
52 n
= (properties
)malloc(sizeof(struct _property
));
54 n
->name
= name
? strdup(name
) : NULL
;
55 n
->value
= value
? strdup(value
) : NULL
;
60 properties_read(int fd
)
63 char hold_n
[PROPERTY_MAX_NAME
+ 1];
64 char hold_v
[PROPERTY_MAX_VALUE
+ 1];
67 enum { LOOK
, COMMENT
, NAME
, VALUE
, MVALUE
, COMMIT
, FILL
, STOP
} state
;
68 int ch
= 0, blevel
= 0;
73 while (state
!= STOP
) {
74 if (state
!= COMMIT
) {
82 if ((max
= read(fd
, buf
, sizeof buf
)) <= 0) {
91 /* Fall through deliberately since we already have a character and state == LOOK */
96 /* Allow shell or lisp style comments */
97 else if (ch
== '#' || ch
== ';') {
101 else if (isalnum(ch
) || ch
== '_') {
102 if (n
>= PROPERTY_MAX_NAME
) {
112 state
= COMMENT
; /* Ignore the rest of the line */
121 if (ch
== '\n' || !ch
) {
127 else if (isspace(ch
))
129 else if (ch
== '=') {
139 if (v
== 0 && ch
== '\n') {
144 else if (v
== 0 && isspace(ch
))
146 else if (ch
== '{') {
150 else if (ch
== '\n' || !ch
) {
156 if (v
>= PROPERTY_MAX_VALUE
) {
167 /* multiline value */
168 if (v
>= PROPERTY_MAX_VALUE
) {
169 warn("properties_read: value exceeds max length");
173 else if (ch
== '}' && !--blevel
) {
187 head
= ptr
= property_alloc(hold_n
, hold_v
);
189 ptr
->next
= property_alloc(hold_n
, hold_v
);
197 /* we don't handle this here, but this prevents warnings */
205 property_find(properties list
, const char *name
)
207 if (!list
|| !name
|| !name
[0])
210 if (!strcmp(list
->name
, name
))
218 properties_free(properties list
)