files.c revision 1.19 1 1.18 uebayasi /* $NetBSD: files.c,v 1.19 2015/08/28 08:56:39 uebayasi Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1992, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This software was developed by the Computer Systems Engineering group
8 1.1 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 thorpej * contributed to Berkeley.
10 1.1 thorpej *
11 1.1 thorpej * All advertising materials mentioning features or use of this software
12 1.1 thorpej * must display the following acknowledgement:
13 1.1 thorpej * This product includes software developed by the University of
14 1.1 thorpej * California, Lawrence Berkeley Laboratories.
15 1.1 thorpej *
16 1.1 thorpej * Redistribution and use in source and binary forms, with or without
17 1.1 thorpej * modification, are permitted provided that the following conditions
18 1.1 thorpej * are met:
19 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
20 1.1 thorpej * notice, this list of conditions and the following disclaimer.
21 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
23 1.1 thorpej * documentation and/or other materials provided with the distribution.
24 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
25 1.1 thorpej * may be used to endorse or promote products derived from this software
26 1.1 thorpej * without specific prior written permission.
27 1.1 thorpej *
28 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 thorpej * SUCH DAMAGE.
39 1.1 thorpej *
40 1.1 thorpej * from: @(#)files.c 8.1 (Berkeley) 6/6/93
41 1.1 thorpej */
42 1.1 thorpej
43 1.1 thorpej #if HAVE_NBTOOL_CONFIG_H
44 1.1 thorpej #include "nbtool_config.h"
45 1.1 thorpej #endif
46 1.1 thorpej
47 1.17 christos #include <sys/cdefs.h>
48 1.17 christos __RCSID("$NetBSD: files.c,v 1.19 2015/08/28 08:56:39 uebayasi Exp $");
49 1.17 christos
50 1.1 thorpej #include <sys/param.h>
51 1.1 thorpej #include <errno.h>
52 1.1 thorpej #include <stdio.h>
53 1.1 thorpej #include <stdlib.h>
54 1.1 thorpej #include <string.h>
55 1.3 christos #include <util.h>
56 1.1 thorpej #include "defs.h"
57 1.1 thorpej
58 1.1 thorpej extern const char *yyfile;
59 1.1 thorpej
60 1.1 thorpej /*
61 1.1 thorpej * We check that each full path name is unique. File base names
62 1.1 thorpej * should generally also be unique, e.g., having both a net/xx.c and
63 1.1 thorpej * a kern/xx.c (or, worse, a net/xx.c and a new/xx.c++) is probably
64 1.1 thorpej * wrong, but is permitted under some conditions.
65 1.1 thorpej */
66 1.1 thorpej static struct hashtab *basetab; /* file base names */
67 1.1 thorpej static struct hashtab *pathtab; /* full path names */
68 1.1 thorpej
69 1.1 thorpej static struct files **unchecked;
70 1.1 thorpej
71 1.14 uebayasi static void addfiletoattr(const char *, struct files *);
72 1.1 thorpej static int checkaux(const char *, void *);
73 1.1 thorpej static int fixcount(const char *, void *);
74 1.1 thorpej static int fixfsel(const char *, void *);
75 1.1 thorpej static int fixsel(const char *, void *);
76 1.1 thorpej
77 1.1 thorpej void
78 1.1 thorpej initfiles(void)
79 1.1 thorpej {
80 1.1 thorpej
81 1.1 thorpej basetab = ht_new();
82 1.1 thorpej pathtab = ht_new();
83 1.1 thorpej TAILQ_INIT(&allfiles);
84 1.1 thorpej unchecked = &TAILQ_FIRST(&allfiles);
85 1.1 thorpej TAILQ_INIT(&allobjects);
86 1.1 thorpej }
87 1.1 thorpej
88 1.1 thorpej void
89 1.17 christos addfile(const char *path, struct condexpr *optx, u_char flags, const char *rule)
90 1.1 thorpej {
91 1.1 thorpej struct files *fi;
92 1.1 thorpej const char *dotp, *tail;
93 1.1 thorpej size_t baselen;
94 1.1 thorpej int needc, needf;
95 1.1 thorpej char base[200];
96 1.1 thorpej
97 1.1 thorpej /* check various errors */
98 1.1 thorpej needc = flags & FI_NEEDSCOUNT;
99 1.1 thorpej needf = flags & FI_NEEDSFLAG;
100 1.1 thorpej if (needc && needf) {
101 1.6 christos cfgerror("cannot mix needs-count and needs-flag");
102 1.1 thorpej goto bad;
103 1.1 thorpej }
104 1.1 thorpej if (optx == NULL && (needc || needf)) {
105 1.6 christos cfgerror("nothing to %s for %s", needc ? "count" : "flag",
106 1.6 christos path);
107 1.1 thorpej goto bad;
108 1.1 thorpej }
109 1.19 uebayasi if (*path == '/') {
110 1.19 uebayasi cfgerror("path must be relative");
111 1.19 uebayasi goto bad;
112 1.19 uebayasi }
113 1.1 thorpej
114 1.1 thorpej /* find last part of pathname, and same without trailing suffix */
115 1.1 thorpej tail = strrchr(path, '/');
116 1.1 thorpej if (tail == NULL)
117 1.1 thorpej tail = path;
118 1.1 thorpej else
119 1.1 thorpej tail++;
120 1.1 thorpej dotp = strrchr(tail, '.');
121 1.1 thorpej if (dotp == NULL || dotp[1] == 0 ||
122 1.17 christos (baselen = (size_t)(dotp - tail)) >= sizeof(base)) {
123 1.6 christos cfgerror("invalid pathname `%s'", path);
124 1.1 thorpej goto bad;
125 1.1 thorpej }
126 1.1 thorpej
127 1.1 thorpej /*
128 1.1 thorpej * Commit this file to memory. We will decide later whether it
129 1.1 thorpej * will be used after all.
130 1.1 thorpej */
131 1.1 thorpej fi = ecalloc(1, sizeof *fi);
132 1.1 thorpej if (ht_insert(pathtab, path, fi)) {
133 1.1 thorpej free(fi);
134 1.1 thorpej if ((fi = ht_lookup(pathtab, path)) == NULL)
135 1.1 thorpej panic("addfile: ht_lookup(%s)", path);
136 1.1 thorpej
137 1.1 thorpej /*
138 1.1 thorpej * If it's a duplicate entry, it is must specify a make
139 1.1 thorpej * rule, and only a make rule, and must come from
140 1.1 thorpej * a different source file than the original entry.
141 1.1 thorpej * If it does otherwise, it is disallowed. This allows
142 1.1 thorpej * machine-dependent files to override the compilation
143 1.1 thorpej * options for specific files.
144 1.1 thorpej */
145 1.1 thorpej if (rule != NULL && optx == NULL && flags == 0 &&
146 1.1 thorpej yyfile != fi->fi_srcfile) {
147 1.1 thorpej fi->fi_mkrule = rule;
148 1.1 thorpej return;
149 1.1 thorpej }
150 1.6 christos cfgerror("duplicate file %s", path);
151 1.6 christos cfgxerror(fi->fi_srcfile, fi->fi_srcline,
152 1.1 thorpej "here is the original definition");
153 1.1 thorpej goto bad;
154 1.1 thorpej }
155 1.1 thorpej memcpy(base, tail, baselen);
156 1.1 thorpej base[baselen] = 0;
157 1.1 thorpej fi->fi_srcfile = yyfile;
158 1.1 thorpej fi->fi_srcline = currentline();
159 1.1 thorpej fi->fi_flags = flags;
160 1.1 thorpej fi->fi_path = path;
161 1.1 thorpej fi->fi_tail = tail;
162 1.1 thorpej fi->fi_base = intern(base);
163 1.1 thorpej fi->fi_prefix = SLIST_EMPTY(&prefixes) ? NULL :
164 1.1 thorpej SLIST_FIRST(&prefixes)->pf_prefix;
165 1.18 uebayasi fi->fi_len = strlen(path);
166 1.18 uebayasi fi->fi_suffix = path[fi->fi_len - 1];
167 1.1 thorpej fi->fi_optx = optx;
168 1.1 thorpej fi->fi_optf = NULL;
169 1.1 thorpej fi->fi_mkrule = rule;
170 1.14 uebayasi fi->fi_attr = NULL;
171 1.1 thorpej TAILQ_INSERT_TAIL(&allfiles, fi, fi_next);
172 1.1 thorpej return;
173 1.1 thorpej bad:
174 1.12 dholland if (optx != NULL) {
175 1.12 dholland condexpr_destroy(optx);
176 1.12 dholland }
177 1.1 thorpej }
178 1.1 thorpej
179 1.1 thorpej void
180 1.17 christos addobject(const char *path, struct condexpr *optx, u_char flags)
181 1.1 thorpej {
182 1.1 thorpej struct objects *oi;
183 1.1 thorpej
184 1.1 thorpej /*
185 1.1 thorpej * Commit this object to memory. We will decide later whether it
186 1.1 thorpej * will be used after all.
187 1.1 thorpej */
188 1.1 thorpej oi = ecalloc(1, sizeof *oi);
189 1.1 thorpej if (ht_insert(pathtab, path, oi)) {
190 1.1 thorpej free(oi);
191 1.1 thorpej if ((oi = ht_lookup(pathtab, path)) == NULL)
192 1.1 thorpej panic("addfile: ht_lookup(%s)", path);
193 1.6 christos cfgerror("duplicate file %s", path);
194 1.6 christos cfgxerror(oi->oi_srcfile, oi->oi_srcline,
195 1.1 thorpej "here is the original definition");
196 1.1 thorpej }
197 1.1 thorpej oi->oi_srcfile = yyfile;
198 1.1 thorpej oi->oi_srcline = currentline();
199 1.1 thorpej oi->oi_flags = flags;
200 1.1 thorpej oi->oi_path = path;
201 1.1 thorpej oi->oi_prefix = SLIST_EMPTY(&prefixes) ? NULL :
202 1.1 thorpej SLIST_FIRST(&prefixes)->pf_prefix;
203 1.1 thorpej oi->oi_optx = optx;
204 1.1 thorpej oi->oi_optf = NULL;
205 1.1 thorpej TAILQ_INSERT_TAIL(&allobjects, oi, oi_next);
206 1.1 thorpej return;
207 1.1 thorpej }
208 1.1 thorpej
209 1.14 uebayasi static void
210 1.14 uebayasi addfiletoattr(const char *name, struct files *fi)
211 1.14 uebayasi {
212 1.14 uebayasi struct attr *a;
213 1.14 uebayasi
214 1.14 uebayasi a = ht_lookup(attrtab, name);
215 1.15 uebayasi if (a == NULL) {
216 1.15 uebayasi CFGDBG(1, "attr `%s' not found", name);
217 1.15 uebayasi } else {
218 1.15 uebayasi fi->fi_attr = a;
219 1.14 uebayasi TAILQ_INSERT_TAIL(&a->a_files, fi, fi_anext);
220 1.14 uebayasi }
221 1.14 uebayasi }
222 1.14 uebayasi
223 1.1 thorpej /*
224 1.1 thorpej * We have finished reading some "files" file, either ../../conf/files
225 1.1 thorpej * or ./files.$machine. Make sure that everything that is flagged as
226 1.1 thorpej * needing a count is reasonable. (This prevents ../../conf/files from
227 1.1 thorpej * depending on some machine-specific device.)
228 1.1 thorpej */
229 1.1 thorpej void
230 1.1 thorpej checkfiles(void)
231 1.1 thorpej {
232 1.1 thorpej struct files *fi, *last;
233 1.1 thorpej
234 1.1 thorpej last = NULL;
235 1.1 thorpej for (fi = *unchecked; fi != NULL;
236 1.1 thorpej last = fi, fi = TAILQ_NEXT(fi, fi_next)) {
237 1.1 thorpej if ((fi->fi_flags & FI_NEEDSCOUNT) != 0)
238 1.1 thorpej (void)expr_eval(fi->fi_optx, checkaux, fi);
239 1.1 thorpej }
240 1.1 thorpej if (last != NULL)
241 1.1 thorpej unchecked = &TAILQ_NEXT(last, fi_next);
242 1.1 thorpej }
243 1.1 thorpej
244 1.1 thorpej /*
245 1.1 thorpej * Auxiliary function for checkfiles, called from expr_eval.
246 1.1 thorpej * We are not actually interested in the expression's value.
247 1.1 thorpej */
248 1.1 thorpej static int
249 1.1 thorpej checkaux(const char *name, void *context)
250 1.1 thorpej {
251 1.1 thorpej struct files *fi = context;
252 1.1 thorpej
253 1.1 thorpej if (ht_lookup(devbasetab, name) == NULL) {
254 1.6 christos cfgxerror(fi->fi_srcfile, fi->fi_srcline,
255 1.1 thorpej "`%s' is not a countable device",
256 1.1 thorpej name);
257 1.1 thorpej /* keep fixfiles() from complaining again */
258 1.1 thorpej fi->fi_flags |= FI_HIDDEN;
259 1.1 thorpej }
260 1.1 thorpej return (0);
261 1.1 thorpej }
262 1.1 thorpej
263 1.1 thorpej /*
264 1.1 thorpej * We have finished reading everything. Tack the files down: calculate
265 1.1 thorpej * selection and counts as needed. Check that the object files built
266 1.1 thorpej * from the selected sources do not collide.
267 1.1 thorpej */
268 1.1 thorpej int
269 1.1 thorpej fixfiles(void)
270 1.1 thorpej {
271 1.1 thorpej struct files *fi, *ofi;
272 1.1 thorpej struct nvlist *flathead, **flatp;
273 1.1 thorpej int err, sel;
274 1.1 thorpej
275 1.1 thorpej err = 0;
276 1.1 thorpej TAILQ_FOREACH(fi, &allfiles, fi_next) {
277 1.2 erh
278 1.1 thorpej /* Skip files that generated counted-device complaints. */
279 1.1 thorpej if (fi->fi_flags & FI_HIDDEN)
280 1.1 thorpej continue;
281 1.1 thorpej
282 1.16 uebayasi if (fi->fi_optx != NULL) {
283 1.14 uebayasi if (fi->fi_optx->cx_type == CX_ATOM) {
284 1.14 uebayasi addfiletoattr(fi->fi_optx->cx_u.atom, fi);
285 1.14 uebayasi }
286 1.1 thorpej flathead = NULL;
287 1.1 thorpej flatp = &flathead;
288 1.1 thorpej sel = expr_eval(fi->fi_optx,
289 1.1 thorpej fi->fi_flags & FI_NEEDSCOUNT ? fixcount :
290 1.1 thorpej fi->fi_flags & FI_NEEDSFLAG ? fixfsel :
291 1.1 thorpej fixsel,
292 1.1 thorpej &flatp);
293 1.1 thorpej fi->fi_optf = flathead;
294 1.1 thorpej if (!sel)
295 1.1 thorpej continue;
296 1.1 thorpej }
297 1.1 thorpej
298 1.1 thorpej /* We like this file. Make sure it generates a unique .o. */
299 1.1 thorpej if (ht_insert(basetab, fi->fi_base, fi)) {
300 1.1 thorpej if ((ofi = ht_lookup(basetab, fi->fi_base)) == NULL)
301 1.1 thorpej panic("fixfiles ht_lookup(%s)", fi->fi_base);
302 1.1 thorpej /*
303 1.1 thorpej * If the new file comes from a different source,
304 1.1 thorpej * allow the new one to override the old one.
305 1.1 thorpej */
306 1.1 thorpej if (fi->fi_path != ofi->fi_path) {
307 1.1 thorpej if (ht_replace(basetab, fi->fi_base, fi) != 1)
308 1.1 thorpej panic("fixfiles ht_replace(%s)",
309 1.1 thorpej fi->fi_base);
310 1.17 christos ofi->fi_flags &= (u_char)~FI_SEL;
311 1.1 thorpej ofi->fi_flags |= FI_HIDDEN;
312 1.1 thorpej } else {
313 1.6 christos cfgxerror(fi->fi_srcfile, fi->fi_srcline,
314 1.1 thorpej "object file collision on %s.o, from %s",
315 1.1 thorpej fi->fi_base, fi->fi_path);
316 1.6 christos cfgxerror(ofi->fi_srcfile, ofi->fi_srcline,
317 1.1 thorpej "here is the previous file: %s",
318 1.1 thorpej ofi->fi_path);
319 1.1 thorpej err = 1;
320 1.1 thorpej }
321 1.1 thorpej }
322 1.1 thorpej fi->fi_flags |= FI_SEL;
323 1.15 uebayasi CFGDBG(3, "file selected `%s'", fi->fi_path);
324 1.15 uebayasi
325 1.15 uebayasi /* Add other files to the default "netbsd" attribute. */
326 1.15 uebayasi if (fi->fi_attr == NULL) {
327 1.15 uebayasi addfiletoattr(allattr.a_name, fi);
328 1.15 uebayasi }
329 1.15 uebayasi CFGDBG(3, "file `%s' belongs to attr `%s'", fi->fi_path,
330 1.15 uebayasi fi->fi_attr->a_name);
331 1.1 thorpej }
332 1.1 thorpej return (err);
333 1.1 thorpej }
334 1.1 thorpej
335 1.1 thorpej /*
336 1.1 thorpej * We have finished reading everything. Tack the objects down: calculate
337 1.1 thorpej * selection.
338 1.1 thorpej */
339 1.1 thorpej int
340 1.1 thorpej fixobjects(void)
341 1.1 thorpej {
342 1.1 thorpej struct objects *oi;
343 1.1 thorpej struct nvlist *flathead, **flatp;
344 1.1 thorpej int err, sel;
345 1.1 thorpej
346 1.1 thorpej err = 0;
347 1.1 thorpej TAILQ_FOREACH(oi, &allobjects, oi_next) {
348 1.1 thorpej /* Optional: see if it is to be included. */
349 1.1 thorpej if (oi->oi_optx != NULL) {
350 1.1 thorpej flathead = NULL;
351 1.1 thorpej flatp = &flathead;
352 1.1 thorpej sel = expr_eval(oi->oi_optx,
353 1.1 thorpej oi->oi_flags & OI_NEEDSFLAG ? fixfsel :
354 1.1 thorpej fixsel,
355 1.1 thorpej &flatp);
356 1.1 thorpej oi->oi_optf = flathead;
357 1.1 thorpej if (!sel)
358 1.1 thorpej continue;
359 1.1 thorpej }
360 1.1 thorpej
361 1.1 thorpej oi->oi_flags |= OI_SEL;
362 1.1 thorpej }
363 1.1 thorpej return (err);
364 1.1 thorpej }
365 1.1 thorpej
366 1.1 thorpej /*
367 1.1 thorpej * We have finished reading everything. Tack the devsws down: calculate
368 1.1 thorpej * selection.
369 1.1 thorpej */
370 1.1 thorpej int
371 1.1 thorpej fixdevsw(void)
372 1.1 thorpej {
373 1.5 alc int error;
374 1.1 thorpej struct devm *dm, *res;
375 1.1 thorpej struct hashtab *fixdevmtab;
376 1.1 thorpej char mstr[16];
377 1.1 thorpej
378 1.5 alc error = 0;
379 1.1 thorpej fixdevmtab = ht_new();
380 1.1 thorpej
381 1.1 thorpej TAILQ_FOREACH(dm, &alldevms, dm_next) {
382 1.1 thorpej res = ht_lookup(fixdevmtab, intern(dm->dm_name));
383 1.1 thorpej if (res != NULL) {
384 1.1 thorpej if (res->dm_cmajor != dm->dm_cmajor ||
385 1.1 thorpej res->dm_bmajor != dm->dm_bmajor) {
386 1.6 christos cfgxerror(res->dm_srcfile, res->dm_srcline,
387 1.7 dsl "device-major '%s' "
388 1.7 dsl "block %d, char %d redefined"
389 1.7 dsl " at %s:%d as block %d, char %d",
390 1.7 dsl res->dm_name,
391 1.7 dsl res->dm_bmajor, res->dm_cmajor,
392 1.7 dsl dm->dm_srcfile, dm->dm_srcline,
393 1.7 dsl dm->dm_bmajor, dm->dm_cmajor);
394 1.1 thorpej } else {
395 1.7 dsl cfgxerror(res->dm_srcfile, res->dm_srcline,
396 1.7 dsl "device-major '%s' "
397 1.7 dsl "(block %d, char %d) duplicated"
398 1.7 dsl " at %s:%d",
399 1.7 dsl dm->dm_name, dm->dm_bmajor,
400 1.7 dsl dm->dm_cmajor,
401 1.7 dsl dm->dm_srcfile, dm->dm_srcline);
402 1.1 thorpej }
403 1.7 dsl error = 1;
404 1.7 dsl goto out;
405 1.1 thorpej }
406 1.1 thorpej if (ht_insert(fixdevmtab, intern(dm->dm_name), dm)) {
407 1.1 thorpej panic("fixdevsw: %s char %d block %d",
408 1.1 thorpej dm->dm_name, dm->dm_cmajor, dm->dm_bmajor);
409 1.1 thorpej }
410 1.1 thorpej
411 1.1 thorpej if (dm->dm_opts != NULL &&
412 1.1 thorpej !expr_eval(dm->dm_opts, fixsel, NULL))
413 1.1 thorpej continue;
414 1.1 thorpej
415 1.9 drochner if (dm->dm_cmajor != NODEVMAJOR) {
416 1.1 thorpej if (ht_lookup(cdevmtab, intern(dm->dm_name)) != NULL) {
417 1.6 christos cfgxerror(dm->dm_srcfile, dm->dm_srcline,
418 1.1 thorpej "device-major of character device '%s' "
419 1.1 thorpej "is already defined", dm->dm_name);
420 1.5 alc error = 1;
421 1.4 christos goto out;
422 1.1 thorpej }
423 1.1 thorpej (void)snprintf(mstr, sizeof(mstr), "%d", dm->dm_cmajor);
424 1.1 thorpej if (ht_lookup(cdevmtab, intern(mstr)) != NULL) {
425 1.6 christos cfgxerror(dm->dm_srcfile, dm->dm_srcline,
426 1.1 thorpej "device-major of character major '%d' "
427 1.1 thorpej "is already defined", dm->dm_cmajor);
428 1.5 alc error = 1;
429 1.4 christos goto out;
430 1.1 thorpej }
431 1.1 thorpej if (ht_insert(cdevmtab, intern(dm->dm_name), dm) ||
432 1.1 thorpej ht_insert(cdevmtab, intern(mstr), dm)) {
433 1.1 thorpej panic("fixdevsw: %s character major %d",
434 1.1 thorpej dm->dm_name, dm->dm_cmajor);
435 1.1 thorpej }
436 1.1 thorpej }
437 1.9 drochner if (dm->dm_bmajor != NODEVMAJOR) {
438 1.1 thorpej if (ht_lookup(bdevmtab, intern(dm->dm_name)) != NULL) {
439 1.6 christos cfgxerror(dm->dm_srcfile, dm->dm_srcline,
440 1.1 thorpej "device-major of block device '%s' "
441 1.1 thorpej "is already defined", dm->dm_name);
442 1.5 alc error = 1;
443 1.4 christos goto out;
444 1.1 thorpej }
445 1.1 thorpej (void)snprintf(mstr, sizeof(mstr), "%d", dm->dm_bmajor);
446 1.1 thorpej if (ht_lookup(bdevmtab, intern(mstr)) != NULL) {
447 1.6 christos cfgxerror(dm->dm_srcfile, dm->dm_srcline,
448 1.1 thorpej "device-major of block major '%d' "
449 1.1 thorpej "is already defined", dm->dm_bmajor);
450 1.5 alc error = 1;
451 1.4 christos goto out;
452 1.1 thorpej }
453 1.1 thorpej if (ht_insert(bdevmtab, intern(dm->dm_name), dm) ||
454 1.1 thorpej ht_insert(bdevmtab, intern(mstr), dm)) {
455 1.1 thorpej panic("fixdevsw: %s block major %d",
456 1.1 thorpej dm->dm_name, dm->dm_bmajor);
457 1.1 thorpej }
458 1.1 thorpej }
459 1.1 thorpej }
460 1.1 thorpej
461 1.4 christos out:
462 1.4 christos ht_free(fixdevmtab);
463 1.5 alc return (error);
464 1.1 thorpej }
465 1.1 thorpej
466 1.1 thorpej /*
467 1.1 thorpej * Called when evaluating a needs-count expression. Make sure the
468 1.1 thorpej * atom is a countable device. The expression succeeds iff there
469 1.1 thorpej * is at least one of them (note that while `xx*' will not always
470 1.1 thorpej * set xx's d_umax > 0, you cannot mix '*' and needs-count). The
471 1.1 thorpej * mkheaders() routine wants a flattened, in-order list of the
472 1.1 thorpej * atoms for `#define name value' lines, so we build that as we
473 1.1 thorpej * are called to eval each atom.
474 1.1 thorpej */
475 1.1 thorpej static int
476 1.1 thorpej fixcount(const char *name, void *context)
477 1.1 thorpej {
478 1.1 thorpej struct nvlist ***p = context;
479 1.1 thorpej struct devbase *dev;
480 1.1 thorpej struct nvlist *nv;
481 1.1 thorpej
482 1.1 thorpej dev = ht_lookup(devbasetab, name);
483 1.1 thorpej if (dev == NULL) /* cannot occur here; we checked earlier */
484 1.1 thorpej panic("fixcount(%s)", name);
485 1.1 thorpej nv = newnv(name, NULL, NULL, dev->d_umax, NULL);
486 1.1 thorpej **p = nv;
487 1.1 thorpej *p = &nv->nv_next;
488 1.1 thorpej (void)ht_insert(needcnttab, name, nv);
489 1.1 thorpej return (dev->d_umax != 0);
490 1.1 thorpej }
491 1.1 thorpej
492 1.1 thorpej /*
493 1.1 thorpej * Called from fixfiles when eval'ing a selection expression for a
494 1.1 thorpej * file that will generate a .h with flags. We will need the flat list.
495 1.1 thorpej */
496 1.1 thorpej static int
497 1.1 thorpej fixfsel(const char *name, void *context)
498 1.1 thorpej {
499 1.1 thorpej struct nvlist ***p = context;
500 1.1 thorpej struct nvlist *nv;
501 1.1 thorpej int sel;
502 1.1 thorpej
503 1.1 thorpej sel = ht_lookup(selecttab, name) != NULL;
504 1.1 thorpej nv = newnv(name, NULL, NULL, sel, NULL);
505 1.1 thorpej **p = nv;
506 1.1 thorpej *p = &nv->nv_next;
507 1.1 thorpej return (sel);
508 1.1 thorpej }
509 1.1 thorpej
510 1.1 thorpej /*
511 1.1 thorpej * As for fixfsel above, but we do not need the flat list.
512 1.1 thorpej */
513 1.1 thorpej static int
514 1.6 christos /*ARGSUSED*/
515 1.1 thorpej fixsel(const char *name, void *context)
516 1.1 thorpej {
517 1.1 thorpej
518 1.1 thorpej return (ht_lookup(selecttab, name) != NULL);
519 1.1 thorpej }
520 1.1 thorpej
521 1.1 thorpej /*
522 1.1 thorpej * Eval an expression tree. Calls the given function on each node,
523 1.1 thorpej * passing it the given context & the name; return value is &/|/! of
524 1.1 thorpej * results of evaluating atoms.
525 1.1 thorpej *
526 1.1 thorpej * No short circuiting ever occurs. fn must return 0 or 1 (otherwise
527 1.1 thorpej * our mixing of C's bitwise & boolean here may give surprises).
528 1.1 thorpej */
529 1.10 cube int
530 1.11 dholland expr_eval(struct condexpr *expr, int (*fn)(const char *, void *), void *ctx)
531 1.1 thorpej {
532 1.1 thorpej int lhs, rhs;
533 1.1 thorpej
534 1.11 dholland switch (expr->cx_type) {
535 1.1 thorpej
536 1.11 dholland case CX_ATOM:
537 1.11 dholland return ((*fn)(expr->cx_atom, ctx));
538 1.1 thorpej
539 1.11 dholland case CX_NOT:
540 1.11 dholland return (!expr_eval(expr->cx_not, fn, ctx));
541 1.1 thorpej
542 1.11 dholland case CX_AND:
543 1.11 dholland lhs = expr_eval(expr->cx_and.left, fn, ctx);
544 1.11 dholland rhs = expr_eval(expr->cx_and.right, fn, ctx);
545 1.1 thorpej return (lhs & rhs);
546 1.1 thorpej
547 1.11 dholland case CX_OR:
548 1.11 dholland lhs = expr_eval(expr->cx_or.left, fn, ctx);
549 1.11 dholland rhs = expr_eval(expr->cx_or.right, fn, ctx);
550 1.1 thorpej return (lhs | rhs);
551 1.1 thorpej }
552 1.11 dholland panic("invalid condexpr type %d", (int)expr->cx_type);
553 1.1 thorpej /* NOTREACHED */
554 1.1 thorpej return (0);
555 1.1 thorpej }
556 1.1 thorpej
557 1.1 thorpej #ifdef DEBUG
558 1.1 thorpej /*
559 1.1 thorpej * Print expression tree.
560 1.1 thorpej */
561 1.1 thorpej void
562 1.1 thorpej prexpr(struct nvlist *expr)
563 1.1 thorpej {
564 1.1 thorpej static void pr0();
565 1.1 thorpej
566 1.1 thorpej printf("expr =");
567 1.1 thorpej pr0(expr);
568 1.1 thorpej printf("\n");
569 1.1 thorpej (void)fflush(stdout);
570 1.1 thorpej }
571 1.1 thorpej
572 1.1 thorpej static void
573 1.1 thorpej pr0(struct nvlist *e)
574 1.1 thorpej {
575 1.1 thorpej
576 1.8 christos switch (e->nv_num) {
577 1.1 thorpej case FX_ATOM:
578 1.1 thorpej printf(" %s", e->nv_name);
579 1.1 thorpej return;
580 1.1 thorpej case FX_NOT:
581 1.1 thorpej printf(" (!");
582 1.1 thorpej break;
583 1.1 thorpej case FX_AND:
584 1.1 thorpej printf(" (&");
585 1.1 thorpej break;
586 1.1 thorpej case FX_OR:
587 1.1 thorpej printf(" (|");
588 1.1 thorpej break;
589 1.1 thorpej default:
590 1.8 christos printf(" (?%lld?", e->nv_num);
591 1.1 thorpej break;
592 1.1 thorpej }
593 1.1 thorpej if (e->nv_ptr)
594 1.1 thorpej pr0(e->nv_ptr);
595 1.1 thorpej pr0(e->nv_next);
596 1.1 thorpej printf(")");
597 1.1 thorpej }
598 1.1 thorpej #endif
599