1 /* $NetBSD: config.c,v 1.2 2003/03/04 19:28:59 jmmv Exp $ */
4 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
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
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
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>
35 __RCSID("$NetBSD: config.c,v 1.2 2003/03/04 19:28:59 jmmv Exp $");
39 #include <dev/wscons/wsconsio.h>
47 #include "pathnames.h"
50 /* --------------------------------------------------------------------- */
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. */
71 p
= (struct prop
*) calloc(1, sizeof(struct prop
));
73 err(EXIT_FAILURE
, "calloc");
77 /* --------------------------------------------------------------------- */
79 /* Frees a property created with prop_new. All data stored in the property
80 * is also destroyed. */
82 prop_free(struct prop
*p
)
90 /* --------------------------------------------------------------------- */
92 /* Creates a new, empty block, with the specified type (see BLOCK_* macros).
93 * Returns a pointer to it. */
99 b
= (struct block
*) calloc(1, sizeof(struct block
));
101 err(EXIT_FAILURE
, "calloc");
106 /* --------------------------------------------------------------------- */
108 /* Frees a block created with block_new. All data contained inside the block
109 * is also destroyed. */
111 block_free(struct block
*b
)
115 if (b
->b_name
!= NULL
)
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
]);
126 /* --------------------------------------------------------------------- */
128 /* Adds a property to a block. */
130 block_add_prop(struct block
*b
, struct prop
*p
)
136 if (b
->b_prop_count
>= MAX_PROPS
)
137 errx(EXIT_FAILURE
, "too many properties for current block");
139 b
->b_prop
[b
->b_prop_count
] = p
;
144 /* --------------------------------------------------------------------- */
146 /* Adds a child (block) to a block. */
148 block_add_child(struct block
*b
, struct block
*c
)
154 if (b
->b_child_count
>= MAX_BLOCKS
)
155 errx(EXIT_FAILURE
, "too many childs for current block");
158 b
->b_child
[b
->b_child_count
] = c
;
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. */
168 block_get_propval(struct block
*b
, const char *pname
, char *def
)
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
;
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
)
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
,
205 if (b
->b_prop
[pc
]->p_value
== ptr
) {
206 warnx("expected integer in `%s' "
218 /* --------------------------------------------------------------------- */
220 /* Gets a mode block (childs of the global scope), which matches the
223 config_get_mode(const char *modename
)
230 if (strcmp(modename
, "Global") == 0)
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
];
241 /* --------------------------------------------------------------------- */
243 /* Reads the configuration file. */
245 config_read(const char *conffile
, int opt
)
250 f
= fopen(conffile
, "r");
252 Global
= config_parse(f
);
254 errx(EXIT_FAILURE
, "%s contains fatal errors",
256 } else if (errno
!= ENOENT
|| opt
) {
257 err(EXIT_FAILURE
, "cannot open %s", conffile
);
261 /* --------------------------------------------------------------------- */
263 /* Destroys all the configuration data. */