xref: /openbsd/usr.sbin/config/config.h (revision 7b36286a)
1 /*	$OpenBSD: config.h,v 1.23 2007/11/25 08:26:59 deraadt Exp $	*/
2 /*	$NetBSD: config.h,v 1.30 1997/02/02 21:12:30 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This software was developed by the Computer Systems Engineering group
9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10  * contributed to Berkeley.
11  *
12  * All advertising materials mentioning features or use of this software
13  * must display the following acknowledgement:
14  *	This product includes software developed by the University of
15  *	California, Lawrence Berkeley Laboratories.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	from: @(#)config.h	8.1 (Berkeley) 6/6/93
42  */
43 
44 /*
45  * config.h:  Global definitions for "config"
46  */
47 
48 #include <sys/types.h>
49 #include <sys/param.h>
50 
51 #if !defined(MAKE_BOOTSTRAP) && defined(BSD)
52 #include <sys/cdefs.h>
53 #include <paths.h>
54 #endif /* ...BSD */
55 
56 #include <stdlib.h>
57 #include <unistd.h>
58 
59 /* These are really for MAKE_BOOTSTRAP but harmless. */
60 #ifndef __dead
61 #define __dead
62 #endif
63 #ifndef _PATH_DEVNULL
64 #define _PATH_DEVNULL "/dev/null"
65 #endif
66 
67 
68 /*
69  * Name/value lists.  Values can be strings or pointers and/or can carry
70  * integers.  The names can be NULL, resulting in simple value lists.
71  */
72 struct nvlist {
73 	struct	nvlist *nv_next;
74 	const char *nv_name;
75 	union {
76 		const char *un_str;
77 		void *un_ptr;
78 	} nv_un;
79 #define	nv_str	nv_un.un_str
80 #define	nv_ptr	nv_un.un_ptr
81 	int	nv_int;
82 };
83 
84 /*
85  * Kernel configurations.
86  */
87 struct config {
88 	struct	config *cf_next;	/* linked list */
89 	const char *cf_name;		/* "vmunix" */
90 	int	cf_lineno;		/* source line */
91 	struct	nvlist *cf_root;	/* "root on ra0a" */
92 	struct	nvlist *cf_swap;	/* "swap on ra0b and ra1b" */
93 	struct	nvlist *cf_dump;	/* "dumps on ra0b" */
94 };
95 
96 /*
97  * Attributes.  These come in two flavors: "plain" and "interface".
98  * Plain attributes (e.g., "ether") simply serve to pull in files.
99  * Interface attributes (e.g., "scsi") carry three lists: locators,
100  * child devices, and references.  The locators are those things
101  * that must be specified in order to configure a device instance
102  * using this attribute (e.g., "tg0 at scsi0").  The a_devs field
103  * lists child devices that can connect here (e.g., "tg"s), while
104  * the a_refs are parents that carry the attribute (e.g., actual
105  * SCSI host adapter drivers such as the SPARC "esp").
106  */
107 struct attr {
108 	const char *a_name;		/* name of this attribute */
109 	int	a_iattr;		/* true => allows children */
110 	struct	nvlist *a_locs;		/* locators required */
111 	int	a_loclen;		/* length of above list */
112 	struct	nvlist *a_devs;		/* children */
113 	struct	nvlist *a_refs;		/* parents */
114 };
115 
116 /*
117  * The "base" part (struct devbase) of a device ("uba", "sd"; but not
118  * "uba2" or "sd0").  It may be found "at" one or more attributes,
119  * including "at root" (this is represented by a NULL attribute), as
120  * specified by the device attachments (struct deva).
121  *
122  * Each device may also export attributes.  If any provide an output
123  * interface (e.g., "esp" provides "scsi"), other devices (e.g.,
124  * "tg"s) can be found at instances of this one (e.g., "esp"s).
125  * Such a connection must provide locators as specified by that
126  * interface attribute (e.g., "target").  The base device can
127  * export both output (aka `interface') attributes, as well as
128  * import input (`plain') attributes.  Device attachments may
129  * only import input attributes; it makes no sense to have a
130  * specific attachment export a new interface to other devices.
131  *
132  * Each base carries a list of instances (via d_ihead).  Note that this
133  * list "skips over" aliases; those must be found through the instances
134  * themselves.  Each base also carries a list of possible attachments,
135  * each of which specify a set of devices that the device can attach
136  * to, as well as the device instances that are actually using that
137  * attachment.
138  */
139 struct devbase {
140 	const char *d_name;		/* e.g., "sd" */
141 	struct	devbase *d_next;	/* linked list */
142 	int	d_isdef;		/* set once properly defined */
143 	int	d_ispseudo;		/* is a pseudo-device */
144 	int	d_major;		/* used for "root on sd0", e.g. */
145 	struct	nvlist *d_attrs;	/* attributes, if any */
146 	int	d_umax;			/* highest unit number + 1 */
147 	struct	devi *d_ihead;		/* first instance, if any */
148 	struct	devi **d_ipp;		/* used for tacking on more instances */
149 	struct	deva *d_ahead;		/* first attachment, if any */
150 	struct	deva **d_app;		/* used for tacking on attachments */
151 };
152 
153 struct deva {
154 	const char *d_name;		/* name of attachment, e.g. "com_isa" */
155 	struct	deva *d_next;		/* linked list */
156 	struct	deva *d_bsame;		/* list on same base */
157 	int	d_isdef;		/* set once properly defined */
158 	struct	devbase *d_devbase;	/* the base device */
159 	struct	nvlist *d_atlist;	/* e.g., "at tg" (attr list) */
160 	struct	nvlist *d_attrs;	/* attributes, if any */
161 	struct	devi *d_ihead;		/* first instance, if any */
162 	struct	devi **d_ipp;		/* used for tacking on more instances */
163 };
164 
165 /*
166  * An "instance" of a device.  The same instance may be listed more
167  * than once, e.g., "xx0 at isa? port FOO" + "xx0 at isa? port BAR".
168  *
169  * After everything has been read in and verified, the devi's are
170  * "packed" to collect all the information needed to generate ioconf.c.
171  * In particular, we try to collapse multiple aliases into a single entry.
172  * We then assign each "primary" (non-collapsed) instance a cfdata index.
173  * Note that there may still be aliases among these.
174  */
175 struct devi {
176 	/* created while parsing config file */
177 	const char *i_name;	/* e.g., "sd0" */
178 	int	i_unit;		/* unit from name, e.g., 0 */
179 	int	i_disable;	/* device is disabled */
180 	struct	devbase *i_base;/* e.g., pointer to "sd" base */
181 	struct	devi *i_next;	/* list of all instances */
182 	struct	devi *i_bsame;	/* list on same base */
183 	struct	devi *i_asame;	/* list on same base attachment */
184 	struct	devi *i_alias;	/* other aliases of this instance */
185 	const char *i_at;	/* where this is "at" (NULL if at root) */
186 	struct	attr *i_atattr;	/* attr that allowed attach */
187 	struct	devbase *i_atdev;/* if "at <devname><unit>", else NULL */
188 	struct	deva *i_atdeva;
189 	const char **i_locs;	/* locators (as given by i_atattr) */
190 	int	i_atunit;	/* unit from "at" */
191 	int	i_cfflags;	/* flags from config line */
192 	int	i_lineno;	/* line # in config, for later errors */
193 
194 	/* created during packing or ioconf.c generation */
195 /*		i_loclen	   via i_atattr->a_loclen */
196 	short	i_collapsed;	/* set => this alias no longer needed */
197 	short	i_cfindex;	/* our index in cfdata */
198 	short	i_pvlen;	/* number of parents */
199 	short	i_pvoff;	/* offset in parents.vec */
200 	short	i_locoff;	/* offset in locators.vec */
201 	struct	devi **i_parents;/* the parents themselves */
202 	int	i_locnami;	/* my index into locnami[] */
203 	int	i_plocnami;	/* parent's locnami[] index */
204 };
205 /* special units */
206 #define	STAR	(-1)		/* unit number for, e.g., "sd*" */
207 #define	WILD	(-2)		/* unit number for, e.g., "sd?" */
208 
209 /*
210  * Files.  Each file is either standard (always included) or optional,
211  * depending on whether it has names on which to *be* optional.  The
212  * options field (fi_optx) is actually an expression tree, with nodes
213  * for OR, AND, and NOT, as well as atoms (words) representing some
214  * particular option.  The node type is stored in the nv_int field.
215  * Subexpressions appear in the `next' field; for the binary operators
216  * AND and OR, the left subexpression is first stored in the nv_ptr field.
217  *
218  * For any file marked as needs-count or needs-flag, fixfiles() will
219  * build fi_optf, a `flat list' of the options with nv_int fields that
220  * contain counts or `need' flags; this is used in mkheaders().
221  */
222 struct files {
223 	struct	files *fi_next;	/* linked list */
224 	const char *fi_srcfile;	/* the name of the "files" file that got us */
225 	u_short	fi_srcline;	/* and the line number */
226 	u_char	fi_flags;	/* as below */
227 	char	fi_lastc;	/* last char from path */
228 	struct nvlist *fi_nvpath; /* list of paths */
229 	const char *fi_base;	/* tail minus ".c" (or whatever) */
230 	struct  nvlist *fi_optx;/* options expression */
231 	struct  nvlist *fi_optf;/* flattened version of above, if needed */
232 	const char *fi_mkrule[2];/* special make rules, if any */
233 };
234 
235 /*
236  * Objects and libraries.  This allows precompiled object and library
237  * files (e.g. binary-only device drivers) to be linked in.
238  */
239 struct objects {
240 	struct  objects *oi_next;/* linked list */
241 	const char *oi_srcfile; /* the name of the "objects" file that got us */
242 	u_short oi_srcline;	/* and the line number */
243 	u_char  oi_flags;	/* as below */
244 	char    oi_lastc;	/* last char from path */
245 	const char *oi_path;    /* full object path */
246 	struct  nvlist *oi_optx;/* options expression */
247 	struct  nvlist *oi_optf;/* flattened version of above, if needed */
248 };
249 
250 #define	OI_SEL		0x01	/* selected */
251 #define	OI_NEEDSFLAG	0x02	/* needs-flag */
252 
253 #define	FX_ATOM		0	/* atom (in nv_name) */
254 #define	FX_NOT		1	/* NOT expr (subexpression in nv_next) */
255 #define	FX_AND		2	/* AND expr (lhs in nv_ptr, rhs in nv_next) */
256 #define	FX_OR		3	/* OR expr (lhs in nv_ptr, rhs in nv_next) */
257 
258 /* flags */
259 #define	FI_SEL		0x01	/* selected */
260 #define	FI_NEEDSCOUNT	0x02	/* needs-count */
261 #define	FI_NEEDSFLAG	0x04	/* needs-flag */
262 #define	FI_HIDDEN	0x08	/* obscured by other(s), base names overlap */
263 
264 /*
265  * Hash tables look up name=value pairs.  The pointer value of the name
266  * is assumed to be constant forever; this can be arranged by interning
267  * the name.  (This is fairly convenient since our lexer does this for
268  * all identifier-like strings---it has to save them anyway, lest yacc's
269  * look-ahead wipe out the current one.)
270  */
271 struct hashtab;
272 
273 const char *conffile;		/* source file, e.g., "GENERIC.sparc" */
274 const char *machine;		/* machine type, e.g., "sparc" or "sun3" */
275 const char *machinearch;	/* machine arch, e.g., "sparc" or "m68k" */
276 const char *srcdir;		/* path to source directory (rel. to build) */
277 const char *builddir;		/* path to build directory */
278 const char *defbuilddir;	/* default build directory */
279 int	errors;			/* counts calls to error() */
280 int	minmaxusers;		/* minimum "maxusers" parameter */
281 int	defmaxusers;		/* default "maxusers" parameter */
282 int	maxmaxusers;		/* default "maxusers" parameter */
283 int	maxusers;		/* configuration's "maxusers" parameter */
284 int	maxpartitions;		/* configuration's "maxpartitions" parameter */
285 struct	nvlist *options;	/* options */
286 struct	nvlist *defoptions;	/* "defopt"'d options */
287 struct	nvlist *mkoptions;	/* makeoptions */
288 struct	hashtab *devbasetab;	/* devbase lookup */
289 struct	hashtab *devatab;	/* devbase attachment lookup */
290 struct	hashtab *selecttab;	/* selects things that are "optional foo" */
291 struct	hashtab *needcnttab;	/* retains names marked "needs-count" */
292 struct	hashtab *opttab;	/* table of configured options */
293 struct	hashtab *defopttab;	/* options that have been "defopt"'d */
294 struct	devbase *allbases;	/* list of all devbase structures */
295 struct	deva *alldevas;		/* list of all devbase attachment structures */
296 struct	config *allcf;		/* list of configured kernels */
297 struct	devi *alldevi;		/* list of all instances */
298 struct	devi *allpseudo;	/* list of all pseudo-devices */
299 int	ndevi;			/* number of devi's (before packing) */
300 int	npseudo;		/* number of pseudo's */
301 
302 struct	files *allfiles;	/* list of all kernel source files */
303 struct objects *allobjects;	/* list of all kernel object and library files */
304 
305 struct	devi **packed;		/* arrayified table for packed devi's */
306 int	npacked;		/* size of packed table, <= ndevi */
307 
308 struct {			/* pv[] table for config */
309 	short	*vec;
310 	int	used;
311 } parents;
312 struct {			/* loc[] table for config */
313 	const char **vec;
314 	int	used;
315 } locators;
316 
317 /* files.c */
318 void	initfiles(void);
319 void	checkfiles(void);
320 int	fixfiles(void);		/* finalize */
321 int	fixobjects(void);
322 void	addfile(struct nvlist *, struct nvlist *, int, const char *, const char *);
323 void	addobject(const char *, struct nvlist *, int);
324 
325 /* hash.c */
326 struct	hashtab *ht_new(void);
327 int	ht_insrep(struct hashtab *, const char *, void *, int);
328 int	ht_remove(struct hashtab *, const char *);
329 #define	ht_insert(ht, nam, val) ht_insrep(ht, nam, val, 0)
330 #define	ht_replace(ht, nam, val) ht_insrep(ht, nam, val, 1)
331 void	*ht_lookup(struct hashtab *, const char *);
332 void	initintern(void);
333 const char *intern(const char *);
334 
335 /* main.c */
336 void	addoption(const char *name, const char *value);
337 void	removeoption(const char *name);
338 void	addmkoption(const char *name, const char *value);
339 void	defoption(const char *name);
340 int	devbase_has_instances(struct devbase *, int);
341 int	deva_has_instances(struct deva *, int);
342 void	setupdirs(void);
343 
344 /* mkheaders.c */
345 int	mkheaders(void);
346 
347 /* mkioconf.c */
348 int	mkioconf(void);
349 
350 /* mkmakefile.c */
351 int	mkmakefile(void);
352 
353 /* mkswap.c */
354 int	mkswap(void);
355 
356 /* pack.c */
357 void	pack(void);
358 
359 /* scan.l */
360 int	currentline(void);
361 int	firstfile(const char *);
362 int	include(const char *, int);
363 
364 /* sem.c, other than for yacc actions */
365 void	initsem(void);
366 
367 /* util.c */
368 void	*emalloc(size_t);
369 void	*erealloc(void *, size_t);
370 char	*sourcepath(const char *);
371 void	error(const char *, ...);			/* immediate errs */
372 void	xerror(const char *, int, const char *, ...);	/* delayed errs */
373 __dead void panic(const char *, ...);
374 struct nvlist *newnv(const char *, const char *, void *, int, struct nvlist *);
375 void	nvfree(struct nvlist *);
376 void	nvfreel(struct nvlist *);
377 
378 int	ukc(char *, char *, int, int);
379