Home | History | Annotate | Line # | Download | only in wsmoused
      1  1.3  jmmv /* $NetBSD: config.c,v 1.3 2003/08/06 18:07:53 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.3  jmmv  * by Julio M. Merino Vidal.
      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.3  jmmv __RCSID("$NetBSD: config.c,v 1.3 2003/08/06 18:07:53 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.3  jmmv 
     41  1.1  jmmv #include <stdio.h>
     42  1.1  jmmv #include <stdlib.h>
     43  1.1  jmmv #include <err.h>
     44  1.1  jmmv #include <errno.h>
     45  1.1  jmmv #include <string.h>
     46  1.1  jmmv 
     47  1.1  jmmv #include "pathnames.h"
     48  1.1  jmmv #include "wsmoused.h"
     49  1.1  jmmv 
     50  1.3  jmmv /* --------------------------------------------------------------------- */
     51  1.3  jmmv 
     52  1.3  jmmv /*
     53  1.3  jmmv  * Global variables.
     54  1.3  jmmv  */
     55  1.3  jmmv 
     56  1.1  jmmv static struct block *Global = NULL;
     57  1.1  jmmv 
     58  1.3  jmmv /* --------------------------------------------------------------------- */
     59  1.3  jmmv 
     60  1.1  jmmv /* Prototypes for config_yacc.y (only used here) */
     61  1.1  jmmv struct block *config_parse(FILE *);
     62  1.1  jmmv 
     63  1.3  jmmv /* --------------------------------------------------------------------- */
     64  1.3  jmmv 
     65  1.3  jmmv /* Creates a new, empty property.  Returns a pointer to it. */
     66  1.1  jmmv struct prop *
     67  1.1  jmmv prop_new(void)
     68  1.1  jmmv {
     69  1.1  jmmv 	struct prop *p;
     70  1.1  jmmv 
     71  1.1  jmmv 	p = (struct prop *) calloc(1, sizeof(struct prop));
     72  1.1  jmmv 	if (p == NULL)
     73  1.1  jmmv 		err(EXIT_FAILURE, "calloc");
     74  1.1  jmmv 	return p;
     75  1.1  jmmv }
     76  1.1  jmmv 
     77  1.3  jmmv /* --------------------------------------------------------------------- */
     78  1.3  jmmv 
     79  1.3  jmmv /* Frees a property created with prop_new.  All data stored in the property
     80  1.3  jmmv  * is also destroyed. */
     81  1.1  jmmv void
     82  1.1  jmmv prop_free(struct prop *p)
     83  1.1  jmmv {
     84  1.3  jmmv 
     85  1.1  jmmv 	free(p->p_name);
     86  1.1  jmmv 	free(p->p_value);
     87  1.1  jmmv 	free(p);
     88  1.1  jmmv }
     89  1.1  jmmv 
     90  1.3  jmmv /* --------------------------------------------------------------------- */
     91  1.3  jmmv 
     92  1.3  jmmv /* Creates a new, empty block, with the specified type (see BLOCK_* macros).
     93  1.3  jmmv  * Returns a pointer to it. */
     94  1.1  jmmv struct block *
     95  1.1  jmmv block_new(int type)
     96  1.1  jmmv {
     97  1.1  jmmv 	struct block *b;
     98  1.1  jmmv 
     99  1.1  jmmv 	b = (struct block *) calloc(1, sizeof(struct block));
    100  1.1  jmmv 	if (b == NULL)
    101  1.1  jmmv 		err(EXIT_FAILURE, "calloc");
    102  1.1  jmmv 	b->b_type = type;
    103  1.1  jmmv 	return b;
    104  1.1  jmmv }
    105  1.1  jmmv 
    106  1.3  jmmv /* --------------------------------------------------------------------- */
    107  1.3  jmmv 
    108  1.3  jmmv /* Frees a block created with block_new.  All data contained inside the block
    109  1.3  jmmv  * is also destroyed. */
    110  1.1  jmmv void
    111  1.1  jmmv block_free(struct block *b)
    112  1.1  jmmv {
    113  1.1  jmmv 	int i;
    114  1.1  jmmv 
    115  1.1  jmmv 	if (b->b_name != NULL)
    116  1.1  jmmv 		free(b->b_name);
    117  1.1  jmmv 
    118  1.1  jmmv 	for (i = 0; i < b->b_prop_count; i++)
    119  1.1  jmmv 		prop_free(b->b_prop[i]);
    120  1.1  jmmv 	for (i = 0; i < b->b_child_count; i++)
    121  1.1  jmmv 		block_free(b->b_child[i]);
    122  1.1  jmmv 
    123  1.1  jmmv 	free(b);
    124  1.1  jmmv }
    125  1.1  jmmv 
    126  1.3  jmmv /* --------------------------------------------------------------------- */
    127  1.3  jmmv 
    128  1.3  jmmv /* Adds a property to a block. */
    129  1.1  jmmv void
    130  1.1  jmmv block_add_prop(struct block *b, struct prop *p)
    131  1.1  jmmv {
    132  1.3  jmmv 
    133  1.1  jmmv 	if (p == NULL)
    134  1.1  jmmv 		return;
    135  1.1  jmmv 
    136  1.1  jmmv 	if (b->b_prop_count >= MAX_PROPS)
    137  1.1  jmmv 		errx(EXIT_FAILURE, "too many properties for current block");
    138  1.1  jmmv 	else {
    139  1.1  jmmv 		b->b_prop[b->b_prop_count] = p;
    140  1.1  jmmv 		b->b_prop_count++;
    141  1.1  jmmv 	}
    142  1.1  jmmv }
    143  1.1  jmmv 
    144  1.3  jmmv /* --------------------------------------------------------------------- */
    145  1.3  jmmv 
    146  1.3  jmmv /* Adds a child (block) to a block. */
    147  1.1  jmmv void
    148  1.1  jmmv block_add_child(struct block *b, struct block *c)
    149  1.1  jmmv {
    150  1.3  jmmv 
    151  1.1  jmmv 	if (c == NULL)
    152  1.1  jmmv 		return;
    153  1.1  jmmv 
    154  1.1  jmmv 	if (b->b_child_count >= MAX_BLOCKS)
    155  1.1  jmmv 		errx(EXIT_FAILURE, "too many childs for current block");
    156  1.1  jmmv 	else {
    157  1.1  jmmv 		c->b_parent = b;
    158  1.1  jmmv 		b->b_child[b->b_child_count] = c;
    159  1.1  jmmv 		b->b_child_count++;
    160  1.1  jmmv 	}
    161  1.1  jmmv }
    162  1.1  jmmv 
    163  1.3  jmmv /* --------------------------------------------------------------------- */
    164  1.3  jmmv 
    165  1.3  jmmv /* Get the value of a property in the specified block (or in its parents).
    166  1.3  jmmv  * If not found, return the value given in def. */
    167  1.1  jmmv char *
    168  1.3  jmmv block_get_propval(struct block *b, const char *pname, char *def)
    169  1.1  jmmv {
    170  1.1  jmmv 	int pc;
    171  1.1  jmmv 
    172  1.1  jmmv 	if (b == NULL)
    173  1.1  jmmv 		return def;
    174  1.1  jmmv 
    175  1.1  jmmv 	while (b != NULL) {
    176  1.1  jmmv 		for (pc = 0; pc < b->b_prop_count; pc++)
    177  1.1  jmmv 			if (strcmp(b->b_prop[pc]->p_name, pname) == 0)
    178  1.1  jmmv 				return b->b_prop[pc]->p_value;
    179  1.1  jmmv 		b = b->b_parent;
    180  1.1  jmmv 	}
    181  1.1  jmmv 
    182  1.1  jmmv 	return def;
    183  1.1  jmmv }
    184  1.1  jmmv 
    185  1.3  jmmv /* --------------------------------------------------------------------- */
    186  1.3  jmmv 
    187  1.3  jmmv /* Get the value of a property in the specified block converting it to an
    188  1.1  jmmv  * integer, if possible.  If the property cannot be found in the given
    189  1.1  jmmv  * block, all its parents are tried.  If after all not found (or conversion
    190  1.3  jmmv  * not possible), return the value given in def. */
    191  1.1  jmmv int
    192  1.3  jmmv block_get_propval_int(struct block *b, const char *pname, int def)
    193  1.1  jmmv {
    194  1.3  jmmv 	char *ptr;
    195  1.1  jmmv 	int pc, ret;
    196  1.1  jmmv 
    197  1.1  jmmv 	if (b == NULL)
    198  1.1  jmmv 		return def;
    199  1.1  jmmv 
    200  1.1  jmmv 	while (b != NULL) {
    201  1.1  jmmv 		for (pc = 0; pc < b->b_prop_count; pc++)
    202  1.1  jmmv 			if (strcmp(b->b_prop[pc]->p_name, pname) == 0) {
    203  1.1  jmmv 				ret = (int) strtol(b->b_prop[pc]->p_value,
    204  1.1  jmmv 				    &ptr, 10);
    205  1.1  jmmv 				if (b->b_prop[pc]->p_value == ptr) {
    206  1.1  jmmv 					warnx("expected integer in `%s' "
    207  1.1  jmmv 					    "property", pname);
    208  1.1  jmmv 					return def;
    209  1.1  jmmv 				}
    210  1.1  jmmv 				return ret;
    211  1.1  jmmv 			}
    212  1.1  jmmv 		b = b->b_parent;
    213  1.1  jmmv 	}
    214  1.1  jmmv 
    215  1.1  jmmv 	return def;
    216  1.1  jmmv }
    217  1.1  jmmv 
    218  1.3  jmmv /* --------------------------------------------------------------------- */
    219  1.3  jmmv 
    220  1.3  jmmv /* Gets a mode block (childs of the global scope), which matches the
    221  1.3  jmmv  * specified name. */
    222  1.1  jmmv struct block *
    223  1.3  jmmv config_get_mode(const char *modename)
    224  1.1  jmmv {
    225  1.1  jmmv 	int bc;
    226  1.3  jmmv 	struct block *b;
    227  1.3  jmmv 
    228  1.3  jmmv 	b = Global;
    229  1.3  jmmv 
    230  1.3  jmmv 	if (strcmp(modename, "Global") == 0)
    231  1.3  jmmv 		return Global;
    232  1.1  jmmv 
    233  1.1  jmmv 	if (b != NULL)
    234  1.1  jmmv 		for (bc = 0; bc < b->b_child_count; bc++)
    235  1.1  jmmv 			if (strcmp(b->b_child[bc]->b_name, modename) == 0)
    236  1.1  jmmv 				return b->b_child[bc];
    237  1.1  jmmv 
    238  1.1  jmmv 	return NULL;
    239  1.1  jmmv }
    240  1.1  jmmv 
    241  1.3  jmmv /* --------------------------------------------------------------------- */
    242  1.3  jmmv 
    243  1.3  jmmv /* Reads the configuration file. */
    244  1.1  jmmv void
    245  1.3  jmmv config_read(const char *conffile, int opt)
    246  1.1  jmmv {
    247  1.1  jmmv 	FILE *f;
    248  1.1  jmmv 
    249  1.1  jmmv 	errno = 0;
    250  1.1  jmmv 	f = fopen(conffile, "r");
    251  1.1  jmmv 	if (f != NULL) {
    252  1.1  jmmv 		Global = config_parse(f);
    253  1.1  jmmv 		if (Global == NULL)
    254  1.1  jmmv 			errx(EXIT_FAILURE, "%s contains fatal errors",
    255  1.1  jmmv 			     conffile);
    256  1.1  jmmv 	} else if (errno != ENOENT || opt) {
    257  1.1  jmmv 		err(EXIT_FAILURE, "cannot open %s", conffile);
    258  1.1  jmmv 	}
    259  1.1  jmmv }
    260  1.1  jmmv 
    261  1.3  jmmv /* --------------------------------------------------------------------- */
    262  1.3  jmmv 
    263  1.3  jmmv /* Destroys all the configuration data. */
    264  1.1  jmmv void
    265  1.1  jmmv config_free(void)
    266  1.1  jmmv {
    267  1.3  jmmv 
    268  1.2  jmmv 	if (Global != NULL)
    269  1.2  jmmv 		block_free(Global);
    270  1.1  jmmv }
    271