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: head/lib/libutil/property.c 152886 2005-11-28 16:30:16Z jhb $
34 #include <sys/types.h>
44 property_alloc(char *name
, char *value
)
48 if ((n
= (properties
)malloc(sizeof(struct _property
))) == NULL
)
52 if ((n
->name
= strdup(name
)) == NULL
) {
59 if ((n
->value
= strdup(value
)) == NULL
) {
70 properties_read(int fd
)
73 char hold_n
[PROPERTY_MAX_NAME
+ 1];
74 char hold_v
[PROPERTY_MAX_VALUE
+ 1];
77 enum { LOOK
, COMMENT
, NAME
, VALUE
, MVALUE
, COMMIT
, FILL
, STOP
} state
, last_state
;
78 int ch
= 0, blevel
= 0;
82 state
= last_state
= LOOK
;
83 while (state
!= STOP
) {
84 if (state
!= COMMIT
) {
93 if ((max
= read(fd
, buf
, sizeof buf
)) < 0) {
94 properties_free(head
);
101 * Restore the state from before the fill (which will be
102 * initialised to LOOK for the first FILL). This ensures that
103 * if we were part-way through eg., a VALUE state, when the
104 * buffer ran out, that the previous operation will be allowed
114 if (isspace((unsigned char)ch
))
116 /* Allow shell or lisp style comments */
117 else if (ch
== '#' || ch
== ';') {
121 else if (isalnum((unsigned char)ch
) || ch
== '_') {
122 if (n
>= PROPERTY_MAX_NAME
) {
132 state
= COMMENT
; /* Ignore the rest of the line */
141 if (ch
== '\n' || !ch
) {
147 else if (isspace((unsigned char)ch
))
149 else if (ch
== '=') {
159 if (v
== 0 && ch
== '\n') {
164 else if (v
== 0 && isspace((unsigned char)ch
))
166 else if (ch
== '{') {
170 else if (ch
== '\n' || !ch
) {
176 if (v
>= PROPERTY_MAX_VALUE
) {
187 /* multiline value */
188 if (v
>= PROPERTY_MAX_VALUE
) {
189 warn("properties_read: value exceeds max length");
193 else if (ch
== '}' && !--blevel
) {
207 if ((head
= ptr
= property_alloc(hold_n
, hold_v
)) == NULL
)
210 if ((ptr
->next
= property_alloc(hold_n
, hold_v
)) == NULL
) {
211 properties_free(head
);
221 /* we don't handle this here, but this prevents warnings */
225 if (head
== NULL
&& (head
= property_alloc(NULL
, NULL
)) == NULL
)
232 property_find(properties list
, const char *name
)
234 if (list
== NULL
|| name
== NULL
|| !name
[0])
236 while (list
!= NULL
) {
237 if (list
->name
!= NULL
&& strcmp(list
->name
, name
) == 0)
238 return (list
->value
);
245 properties_free(properties list
)