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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
45 property_alloc(char *name, char *value)
49 if ((n = (properties)malloc(sizeof(struct _property))) == NULL)
53 if ((n->name = strdup(name)) == NULL) {
60 if ((n->value = strdup(value)) == NULL) {
71 properties_read(int fd)
74 char hold_n[PROPERTY_MAX_NAME + 1];
75 char hold_v[PROPERTY_MAX_VALUE + 1];
78 enum { LOOK, COMMENT, NAME, VALUE, MVALUE, COMMIT, FILL, STOP } state, last_state;
79 int ch = 0, blevel = 0;
83 state = last_state = LOOK;
84 while (state != STOP) {
85 if (state != COMMIT) {
94 if ((max = read(fd, buf, sizeof buf)) < 0) {
95 properties_free(head);
102 * Restore the state from before the fill (which will be
103 * initialised to LOOK for the first FILL). This ensures that
104 * if we were part-way through eg., a VALUE state, when the
105 * buffer ran out, that the previous operation will be allowed
115 if (isspace((unsigned char)ch))
117 /* Allow shell or lisp style comments */
118 else if (ch == '#' || ch == ';') {
122 else if (isalnum((unsigned char)ch) || ch == '_') {
123 if (n >= PROPERTY_MAX_NAME) {
133 state = COMMENT; /* Ignore the rest of the line */
142 if (ch == '\n' || !ch) {
148 else if (isspace((unsigned char)ch))
150 else if (ch == '=') {
160 if (v == 0 && ch == '\n') {
165 else if (v == 0 && isspace((unsigned char)ch))
167 else if (ch == '{') {
171 else if (ch == '\n' || !ch) {
177 if (v >= PROPERTY_MAX_VALUE) {
188 /* multiline value */
189 if (v >= PROPERTY_MAX_VALUE) {
190 warn("properties_read: value exceeds max length");
194 else if (ch == '}' && !--blevel) {
208 if ((head = ptr = property_alloc(hold_n, hold_v)) == NULL)
211 if ((ptr->next = property_alloc(hold_n, hold_v)) == NULL) {
212 properties_free(head);
222 /* we don't handle this here, but this prevents warnings */
226 if (head == NULL && (head = property_alloc(NULL, NULL)) == NULL)
233 property_find(properties list, const char *name)
235 if (list == NULL || name == NULL || !name[0])
237 while (list != NULL) {
238 if (list->name != NULL && strcmp(list->name, name) == 0)
239 return (list->value);
246 properties_free(properties list)