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.2 2003/06/17 04:26:52 dillon Exp $
42 #include <sys/types.h>
46 property_alloc(char *name, char *value)
50 n = (properties)malloc(sizeof(struct _property));
52 n->name = name ? strdup(name) : NULL;
53 n->value = value ? strdup(value) : NULL;
58 properties_read(int fd)
61 char hold_n[PROPERTY_MAX_NAME + 1];
62 char hold_v[PROPERTY_MAX_VALUE + 1];
65 enum { LOOK, COMMENT, NAME, VALUE, MVALUE, COMMIT, FILL, STOP } state;
66 int ch = 0, blevel = 0;
71 while (state != STOP) {
72 if (state != COMMIT) {
80 if ((max = read(fd, buf, sizeof buf)) <= 0) {
89 /* Fall through deliberately since we already have a character and state == LOOK */
94 /* Allow shell or lisp style comments */
95 else if (ch == '#' || ch == ';') {
99 else if (isalnum(ch) || ch == '_') {
100 if (n >= PROPERTY_MAX_NAME) {
110 state = COMMENT; /* Ignore the rest of the line */
119 if (ch == '\n' || !ch) {
125 else if (isspace(ch))
127 else if (ch == '=') {
137 if (v == 0 && ch == '\n') {
142 else if (v == 0 && isspace(ch))
144 else if (ch == '{') {
148 else if (ch == '\n' || !ch) {
154 if (v >= PROPERTY_MAX_VALUE) {
165 /* multiline value */
166 if (v >= PROPERTY_MAX_VALUE) {
167 warn("properties_read: value exceeds max length");
171 else if (ch == '}' && !--blevel) {
185 head = ptr = property_alloc(hold_n, hold_v);
187 ptr->next = property_alloc(hold_n, hold_v);
195 /* we don't handle this here, but this prevents warnings */
203 property_find(properties list, const char *name)
205 if (!list || !name || !name[0])
208 if (!strcmp(list->name, name))
216 properties_free(properties list)