]>
git.saurik.com Git - apple/libutil.git/blob - property.c
3 * Simple property list handling code.
6 * Jordan "Perky" 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
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
43 property_alloc(char *name
, char *value
)
47 if ((n
= (properties
)malloc(sizeof(struct _property
))) == NULL
)
51 if ((n
->name
= strdup(name
)) == NULL
) {
58 if ((n
->value
= strdup(value
)) == NULL
) {
69 properties_read(int fd
)
72 char hold_n
[PROPERTY_MAX_NAME
+ 1];
73 char hold_v
[PROPERTY_MAX_VALUE
+ 1];
76 enum { LOOK
, COMMENT
, NAME
, VALUE
, MVALUE
, COMMIT
, FILL
, STOP
} state
, last_state
;
77 int ch
= 0, blevel
= 0;
81 state
= last_state
= LOOK
;
82 while (state
!= STOP
) {
83 if (state
!= COMMIT
) {
92 if ((max
= read(fd
, buf
, sizeof buf
)) < 0) {
93 properties_free(head
);
100 * Restore the state from before the fill (which will be
101 * initialised to LOOK for the first FILL). This ensures that
102 * if we were part-way through eg., a VALUE state, when the
103 * buffer ran out, that the previous operation will be allowed
113 if (isspace((unsigned char)ch
))
115 /* Allow shell or lisp style comments */
116 else if (ch
== '#' || ch
== ';') {
120 else if (isalnum((unsigned char)ch
) || ch
== '_') {
121 if (n
>= PROPERTY_MAX_NAME
) {
131 state
= COMMENT
; /* Ignore the rest of the line */
140 if (ch
== '\n' || !ch
) {
146 else if (isspace((unsigned char)ch
))
148 else if (ch
== '=') {
158 if (v
== 0 && ch
== '\n') {
163 else if (v
== 0 && isspace((unsigned char)ch
))
165 else if (ch
== '{') {
169 else if (ch
== '\n' || !ch
) {
175 if (v
>= PROPERTY_MAX_VALUE
) {
186 /* multiline value */
187 if (v
>= PROPERTY_MAX_VALUE
) {
188 warn("properties_read: value exceeds max length");
192 else if (ch
== '}' && !--blevel
) {
206 if ((head
= ptr
= property_alloc(hold_n
, hold_v
)) == NULL
)
209 if ((ptr
->next
= property_alloc(hold_n
, hold_v
)) == NULL
) {
210 properties_free(head
);
220 /* we don't handle this here, but this prevents warnings */
224 if (head
== NULL
&& (head
= property_alloc(NULL
, NULL
)) == NULL
)
231 property_find(properties list
, const char *name
)
233 if (list
== NULL
|| name
== NULL
|| !name
[0])
235 while (list
!= NULL
) {
236 if (list
->name
!= NULL
&& strcmp(list
->name
, name
) == 0)
237 return (list
->value
);
244 properties_free(properties list
)