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