xref: /freebsd/lib/libutil/property.c (revision 779092a489af71ac21d78870132207e85f59224a)
1 /*
2  *
3  * Simple property list handling code.
4  *
5  * Copyright (c) 1998
6  *	Jordan Hubbard.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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
14  *    point in the file.
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <ctype.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <err.h>
41 #include <sys/types.h>
42 #include <libutil.h>
43 
44 static properties
45 property_alloc(char *name, char *value)
46 {
47     properties n;
48 
49     if ((n = (properties)malloc(sizeof(struct _property))) == NULL)
50 	return (NULL);
51     n->next = NULL;
52     if (name != NULL) {
53 	if ((n->name = strdup(name)) == NULL) {
54 	    free(n);
55 	    return (NULL);
56 	}
57     } else
58 	n->name = NULL;
59     if (value != NULL) {
60 	if ((n->value = strdup(value)) == NULL) {
61 	    free(n->name);
62 	    free(n);
63 	    return (NULL);
64 	}
65     } else
66 	n->value = NULL;
67     return (n);
68 }
69 
70 properties
71 properties_read(int fd)
72 {
73     properties head, ptr;
74     char hold_n[PROPERTY_MAX_NAME + 1];
75     char hold_v[PROPERTY_MAX_VALUE + 1];
76     char buf[BUFSIZ * 4];
77     int bp, n, v, max;
78     enum { LOOK, COMMENT, NAME, VALUE, MVALUE, COMMIT, FILL, STOP } state;
79     int ch = 0, blevel = 0;
80 
81     n = v = bp = max = 0;
82     head = ptr = NULL;
83     state = LOOK;
84     while (state != STOP) {
85 	if (state != COMMIT) {
86 	    if (bp == max)
87 		state = FILL;
88 	    else
89 		ch = buf[bp++];
90 	}
91 	switch(state) {
92 	case FILL:
93 	    if ((max = read(fd, buf, sizeof buf)) <= 0) {
94 		state = STOP;
95 		break;
96 	    }
97 	    else {
98 		state = LOOK;
99 		ch = buf[0];
100 		bp = 1;
101 	    }
102 	    /* FALLTHROUGH deliberately since we already have a character and state == LOOK */
103 
104 	case LOOK:
105 	    if (isspace((unsigned char)ch))
106 		continue;
107 	    /* Allow shell or lisp style comments */
108 	    else if (ch == '#' || ch == ';') {
109 		state = COMMENT;
110 		continue;
111 	    }
112 	    else if (isalnum((unsigned char)ch) || ch == '_') {
113 		if (n >= PROPERTY_MAX_NAME) {
114 		    n = 0;
115 		    state = COMMENT;
116 		}
117 		else {
118 		    hold_n[n++] = ch;
119 		    state = NAME;
120 		}
121 	    }
122 	    else
123 		state = COMMENT;	/* Ignore the rest of the line */
124 	    break;
125 
126 	case COMMENT:
127 	    if (ch == '\n')
128 		state = LOOK;
129 	    break;
130 
131 	case NAME:
132 	    if (ch == '\n' || !ch) {
133 		hold_n[n] = '\0';
134 		hold_v[0] = '\0';
135 		v = n = 0;
136 		state = COMMIT;
137 	    }
138 	    else if (isspace((unsigned char)ch))
139 		continue;
140 	    else if (ch == '=') {
141 		hold_n[n] = '\0';
142 		v = n = 0;
143 		state = VALUE;
144 	    }
145 	    else
146 		hold_n[n++] = ch;
147 	    break;
148 
149 	case VALUE:
150 	    if (v == 0 && ch == '\n') {
151 	        hold_v[v] = '\0';
152 	        v = n = 0;
153 	        state = COMMIT;
154 	    }
155 	    else if (v == 0 && isspace((unsigned char)ch))
156 		continue;
157 	    else if (ch == '{') {
158 		state = MVALUE;
159 		++blevel;
160 	    }
161 	    else if (ch == '\n' || !ch) {
162 		hold_v[v] = '\0';
163 		v = n = 0;
164 		state = COMMIT;
165 	    }
166 	    else {
167 		if (v >= PROPERTY_MAX_VALUE) {
168 		    state = COMMENT;
169 		    v = n = 0;
170 		    break;
171 		}
172 		else
173 		    hold_v[v++] = ch;
174 	    }
175 	    break;
176 
177 	case MVALUE:
178 	    /* multiline value */
179 	    if (v >= PROPERTY_MAX_VALUE) {
180 		warn("properties_read: value exceeds max length");
181 		state = COMMENT;
182 		n = v = 0;
183 	    }
184 	    else if (ch == '}' && !--blevel) {
185 		hold_v[v] = '\0';
186 		v = n = 0;
187 		state = COMMIT;
188 	    }
189 	    else {
190 		hold_v[v++] = ch;
191 		if (ch == '{')
192 		    ++blevel;
193 	    }
194 	    break;
195 
196 	case COMMIT:
197 	    if (head == NULL) {
198 		if ((head = ptr = property_alloc(hold_n, hold_v)) == NULL)
199 		    return (NULL);
200 	    } else {
201 		if ((ptr->next = property_alloc(hold_n, hold_v)) == NULL) {
202 		    properties_free(head);
203 		    return (NULL);
204 		}
205 		ptr = ptr->next;
206 	    }
207 	    state = LOOK;
208 	    v = n = 0;
209 	    break;
210 
211 	case STOP:
212 	    /* we don't handle this here, but this prevents warnings */
213 	    break;
214 	}
215     }
216     return (head);
217 }
218 
219 char *
220 property_find(properties list, const char *name)
221 {
222     if (!list || !name || !name[0])
223 	return NULL;
224     while (list) {
225 	if (!strcmp(list->name, name))
226 	    return list->value;
227 	list = list->next;
228     }
229     return NULL;
230 }
231 
232 void
233 properties_free(properties list)
234 {
235     properties tmp;
236 
237     while (list) {
238 	tmp = list->next;
239 	if (list->name)
240 	    free(list->name);
241 	if (list->value)
242 	    free(list->value);
243 	free(list);
244 	list = tmp;
245     }
246 }
247