config.c revision 1.2 1 1.2 jmmv /* $NetBSD: config.c,v 1.2 2003/03/04 19:28:59 jmmv Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jmmv * by Julio Merino.
9 1.1 jmmv *
10 1.1 jmmv * Redistribution and use in source and binary forms, with or without
11 1.1 jmmv * modification, are permitted provided that the following conditions
12 1.1 jmmv * are met:
13 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
14 1.1 jmmv * notice, this list of conditions and the following disclaimer.
15 1.1 jmmv * 2. The name authors may not be used to endorse or promote products
16 1.1 jmmv * derived from this software without specific prior written
17 1.1 jmmv * permission.
18 1.1 jmmv *
19 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
20 1.1 jmmv * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 jmmv * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 jmmv * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
23 1.1 jmmv * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 jmmv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 1.1 jmmv * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1 jmmv * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 1.1 jmmv * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 1.1 jmmv * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 jmmv */
31 1.1 jmmv
32 1.1 jmmv #include <sys/cdefs.h>
33 1.1 jmmv
34 1.1 jmmv #ifndef lint
35 1.2 jmmv __RCSID("$NetBSD: config.c,v 1.2 2003/03/04 19:28:59 jmmv Exp $");
36 1.1 jmmv #endif /* not lint */
37 1.1 jmmv
38 1.1 jmmv #include <sys/time.h>
39 1.1 jmmv #include <dev/wscons/wsconsio.h>
40 1.1 jmmv #include <stdio.h>
41 1.1 jmmv #include <stdlib.h>
42 1.1 jmmv #include <err.h>
43 1.1 jmmv #include <errno.h>
44 1.1 jmmv #include <string.h>
45 1.1 jmmv
46 1.1 jmmv #include "pathnames.h"
47 1.1 jmmv #include "wsmoused.h"
48 1.1 jmmv
49 1.1 jmmv static struct block *Global = NULL;
50 1.1 jmmv
51 1.1 jmmv /* Prototypes for config_yacc.y (only used here) */
52 1.1 jmmv struct block *config_parse(FILE *);
53 1.1 jmmv
54 1.1 jmmv /*
55 1.1 jmmv * Creates a new, empty property. Returns a pointer to it.
56 1.1 jmmv */
57 1.1 jmmv struct prop *
58 1.1 jmmv prop_new(void)
59 1.1 jmmv {
60 1.1 jmmv struct prop *p;
61 1.1 jmmv
62 1.1 jmmv p = (struct prop *) calloc(1, sizeof(struct prop));
63 1.1 jmmv if (p == NULL)
64 1.1 jmmv err(EXIT_FAILURE, "calloc");
65 1.1 jmmv return p;
66 1.1 jmmv }
67 1.1 jmmv
68 1.1 jmmv /*
69 1.1 jmmv * Frees a property created with prop_new. All data stored in the property
70 1.1 jmmv * is also destroyed.
71 1.1 jmmv */
72 1.1 jmmv void
73 1.1 jmmv prop_free(struct prop *p)
74 1.1 jmmv {
75 1.1 jmmv free(p->p_name);
76 1.1 jmmv free(p->p_value);
77 1.1 jmmv free(p);
78 1.1 jmmv }
79 1.1 jmmv
80 1.1 jmmv /*
81 1.1 jmmv * Creates a new, empty block, with the specified type (see BLOCK_* macros).
82 1.1 jmmv * Returns a pointer to it.
83 1.1 jmmv */
84 1.1 jmmv struct block *
85 1.1 jmmv block_new(int type)
86 1.1 jmmv {
87 1.1 jmmv struct block *b;
88 1.1 jmmv
89 1.1 jmmv b = (struct block *) calloc(1, sizeof(struct block));
90 1.1 jmmv if (b == NULL)
91 1.1 jmmv err(EXIT_FAILURE, "calloc");
92 1.1 jmmv b->b_type = type;
93 1.1 jmmv return b;
94 1.1 jmmv }
95 1.1 jmmv
96 1.1 jmmv /*
97 1.1 jmmv * Frees a block created with block_new. All data contained inside the block
98 1.1 jmmv * is also destroyed.
99 1.1 jmmv */
100 1.1 jmmv void
101 1.1 jmmv block_free(struct block *b)
102 1.1 jmmv {
103 1.1 jmmv int i;
104 1.1 jmmv
105 1.1 jmmv if (b->b_name != NULL)
106 1.1 jmmv free(b->b_name);
107 1.1 jmmv
108 1.1 jmmv for (i = 0; i < b->b_prop_count; i++)
109 1.1 jmmv prop_free(b->b_prop[i]);
110 1.1 jmmv for (i = 0; i < b->b_child_count; i++)
111 1.1 jmmv block_free(b->b_child[i]);
112 1.1 jmmv
113 1.1 jmmv free(b);
114 1.1 jmmv }
115 1.1 jmmv
116 1.1 jmmv /*
117 1.1 jmmv * Add a property to a block.
118 1.1 jmmv */
119 1.1 jmmv void
120 1.1 jmmv block_add_prop(struct block *b, struct prop *p)
121 1.1 jmmv {
122 1.1 jmmv if (p == NULL)
123 1.1 jmmv return;
124 1.1 jmmv
125 1.1 jmmv if (b->b_prop_count >= MAX_PROPS)
126 1.1 jmmv errx(EXIT_FAILURE, "too many properties for current block");
127 1.1 jmmv else {
128 1.1 jmmv b->b_prop[b->b_prop_count] = p;
129 1.1 jmmv b->b_prop_count++;
130 1.1 jmmv }
131 1.1 jmmv }
132 1.1 jmmv
133 1.1 jmmv /*
134 1.1 jmmv * Add a child (block) to a block.
135 1.1 jmmv */
136 1.1 jmmv void
137 1.1 jmmv block_add_child(struct block *b, struct block *c)
138 1.1 jmmv {
139 1.1 jmmv if (c == NULL)
140 1.1 jmmv return;
141 1.1 jmmv
142 1.1 jmmv if (b->b_child_count >= MAX_BLOCKS)
143 1.1 jmmv errx(EXIT_FAILURE, "too many childs for current block");
144 1.1 jmmv else {
145 1.1 jmmv c->b_parent = b;
146 1.1 jmmv b->b_child[b->b_child_count] = c;
147 1.1 jmmv b->b_child_count++;
148 1.1 jmmv }
149 1.1 jmmv }
150 1.1 jmmv
151 1.1 jmmv /*
152 1.1 jmmv * Get the value of a property in the specified block (or in its parents).
153 1.1 jmmv * If not found, return the value given in def.
154 1.1 jmmv */
155 1.1 jmmv char *
156 1.1 jmmv block_get_propval(struct block *b, char *pname, char *def)
157 1.1 jmmv {
158 1.1 jmmv int pc;
159 1.1 jmmv
160 1.1 jmmv if (b == NULL)
161 1.1 jmmv return def;
162 1.1 jmmv
163 1.1 jmmv while (b != NULL) {
164 1.1 jmmv for (pc = 0; pc < b->b_prop_count; pc++)
165 1.1 jmmv if (strcmp(b->b_prop[pc]->p_name, pname) == 0)
166 1.1 jmmv return b->b_prop[pc]->p_value;
167 1.1 jmmv b = b->b_parent;
168 1.1 jmmv }
169 1.1 jmmv
170 1.1 jmmv return def;
171 1.1 jmmv }
172 1.1 jmmv
173 1.1 jmmv /*
174 1.1 jmmv * Get the value of a property in the specified block converting it to an
175 1.1 jmmv * integer, if possible. If the property cannot be found in the given
176 1.1 jmmv * block, all its parents are tried. If after all not found (or conversion
177 1.1 jmmv * not possible), return the value given in def.
178 1.1 jmmv */
179 1.1 jmmv int
180 1.1 jmmv block_get_propval_int(struct block *b, char *pname, int def)
181 1.1 jmmv {
182 1.1 jmmv int pc, ret;
183 1.1 jmmv char *ptr;
184 1.1 jmmv
185 1.1 jmmv if (b == NULL)
186 1.1 jmmv return def;
187 1.1 jmmv
188 1.1 jmmv while (b != NULL) {
189 1.1 jmmv for (pc = 0; pc < b->b_prop_count; pc++)
190 1.1 jmmv if (strcmp(b->b_prop[pc]->p_name, pname) == 0) {
191 1.1 jmmv ret = (int) strtol(b->b_prop[pc]->p_value,
192 1.1 jmmv &ptr, 10);
193 1.1 jmmv if (b->b_prop[pc]->p_value == ptr) {
194 1.1 jmmv warnx("expected integer in `%s' "
195 1.1 jmmv "property", pname);
196 1.1 jmmv return def;
197 1.1 jmmv }
198 1.1 jmmv return ret;
199 1.1 jmmv }
200 1.1 jmmv b = b->b_parent;
201 1.1 jmmv }
202 1.1 jmmv
203 1.1 jmmv return def;
204 1.1 jmmv }
205 1.1 jmmv
206 1.1 jmmv /*
207 1.1 jmmv * Get a mode block (childs of the global scope), which matches the specified
208 1.1 jmmv * name.
209 1.1 jmmv */
210 1.1 jmmv struct block *
211 1.1 jmmv config_get_mode(char *modename)
212 1.1 jmmv {
213 1.1 jmmv struct block *b = Global;
214 1.1 jmmv int bc;
215 1.1 jmmv
216 1.1 jmmv if (b != NULL)
217 1.1 jmmv for (bc = 0; bc < b->b_child_count; bc++)
218 1.1 jmmv if (strcmp(b->b_child[bc]->b_name, modename) == 0)
219 1.1 jmmv return b->b_child[bc];
220 1.1 jmmv
221 1.1 jmmv return NULL;
222 1.1 jmmv }
223 1.1 jmmv
224 1.1 jmmv /*
225 1.1 jmmv * Read the configuration file.
226 1.1 jmmv */
227 1.1 jmmv void
228 1.1 jmmv config_read(char *conffile, int opt)
229 1.1 jmmv {
230 1.1 jmmv FILE *f;
231 1.1 jmmv
232 1.1 jmmv errno = 0;
233 1.1 jmmv f = fopen(conffile, "r");
234 1.1 jmmv if (f != NULL) {
235 1.1 jmmv Global = config_parse(f);
236 1.1 jmmv if (Global == NULL)
237 1.1 jmmv errx(EXIT_FAILURE, "%s contains fatal errors",
238 1.1 jmmv conffile);
239 1.1 jmmv } else if (errno != ENOENT || opt) {
240 1.1 jmmv err(EXIT_FAILURE, "cannot open %s", conffile);
241 1.1 jmmv }
242 1.1 jmmv }
243 1.1 jmmv
244 1.1 jmmv /*
245 1.1 jmmv * Destroy all the configuration data.
246 1.1 jmmv */
247 1.1 jmmv void
248 1.1 jmmv config_free(void)
249 1.1 jmmv {
250 1.2 jmmv if (Global != NULL)
251 1.2 jmmv block_free(Global);
252 1.1 jmmv }
253