xref: /netbsd-src/usr.bin/config/mkdevsw.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: mkdevsw.c,v 1.2 2005/07/30 06:40:30 yamt Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by MAEKAWA Masahide (gehenna@NetBSD.org).
9  *
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. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	  This product includes software developed by the NetBSD
21  *	  Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42 
43 #include <stdio.h>
44 #include <string.h>
45 #include <errno.h>
46 
47 #include "defs.h"
48 
49 static int emitconv(FILE *);
50 static int emitdev(FILE *);
51 static int emitdevm(FILE *);
52 static int emitheader(FILE *);
53 
54 int
55 mkdevsw(void)
56 {
57 	FILE *fp;
58 
59 	if ((fp = fopen("devsw.c.tmp", "w")) == NULL) {
60 		(void)fprintf(stderr, "config: cannot write devsw.c: %s\n",
61 			      strerror(errno));
62 		return (1);
63 	}
64 
65 	if (emitheader(fp) || emitdevm(fp) || emitconv(fp) || emitdev(fp)) {
66 		(void)fprintf(stderr, "config: error writing devsw.c: %s\n",
67 			      strerror(errno));
68 		(void)fclose(fp);
69 		return (1);
70 	}
71 
72 	(void)fclose(fp);
73 
74 	if (moveifchanged("devsw.c.tmp", "devsw.c") != 0) {
75 		(void)fprintf(stderr, "config: error renaming devsw.c: %s\n",
76 			      strerror(errno));
77 		return (1);
78 	}
79 
80 	return (0);
81 }
82 
83 static int
84 emitheader(FILE *fp)
85 {
86 	if (fprintf(fp, "/*\n * MACHINE GENERATED: DO NOT EDIT\n *\n"
87 		    " * devsw.c, from \"%s\"\n */\n\n", conffile) < 0) {
88 		return (1);
89 	}
90 
91 	if (fputs("#include <sys/param.h>\n"
92 		  "#include <sys/conf.h>\n"
93 		  "\n#define\tDEVSW_ARRAY_SIZE(x)\t"
94 		  "(sizeof((x))/sizeof((x)[0]))\n", fp) < 0) {
95 		return (1);
96 	}
97 
98 	return (0);
99 }
100 
101 /*
102  * Emit device switch table for character/block device.
103  */
104 static int
105 emitdevm(FILE *fp)
106 {
107 	struct devm *dm;
108 	char mstr[16];
109 	int i;
110 
111 	if (fputs("\n/* device switch table for block device */\n", fp) < 0)
112 		return (1);
113 
114 	for (i = 0 ; i <= maxbdevm ; i++) {
115 		(void)snprintf(mstr, sizeof(mstr), "%d", i);
116 		if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL)
117 			continue;
118 
119 		if (fprintf(fp, "extern const struct bdevsw %s_bdevsw;\n",
120 			    dm->dm_name) < 0)
121 			return (1);
122 	}
123 
124 	if (fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp) < 0)
125 		return (1);
126 
127 	for (i = 0 ; i <= maxbdevm ; i++) {
128 		(void)snprintf(mstr, sizeof(mstr), "%d", i);
129 		if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL) {
130 			if (fprintf(fp, "\tNULL,\n") < 0)
131 				return (1);
132 		} else {
133 			if (fprintf(fp, "\t&%s_bdevsw,\n", dm->dm_name) < 0)
134 				return (1);
135 		}
136 	}
137 
138 	if (fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp) < 0)
139 		return (1);
140 
141 	if (fputs("const int sys_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n"
142 		  "int max_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n", fp) < 0)
143 		return (1);
144 
145 	if (fputs("\n/* device switch table for character device */\n", fp) < 0)
146 		return (1);
147 
148 	for (i = 0 ; i <= maxcdevm ; i++) {
149 		(void)snprintf(mstr, sizeof(mstr), "%d", i);
150 		if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
151 			continue;
152 
153 		if (fprintf(fp, "extern const struct cdevsw %s_cdevsw;\n",
154 			    dm->dm_name) < 0)
155 			return (1);
156 	}
157 
158 	if (fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp) < 0)
159 		return (1);
160 
161 	for (i = 0 ; i <= maxcdevm ; i++) {
162 		(void)snprintf(mstr, sizeof(mstr), "%d", i);
163 		if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) {
164 			if (fprintf(fp, "\tNULL,\n") < 0)
165 				return (1);
166 		} else {
167 			if (fprintf(fp, "\t&%s_cdevsw,\n", dm->dm_name) < 0)
168 				return (1);
169 		}
170 	}
171 
172 	if (fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp) < 0)
173 		return (1);
174 
175 	if (fputs("const int sys_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n"
176 		  "int max_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n", fp) < 0)
177 		return (1);
178 
179 	return (0);
180 }
181 
182 /*
183  * Emit device major conversion table.
184  */
185 static int
186 emitconv(FILE *fp)
187 {
188 	struct devm *dm;
189 
190 	if (fputs("\n/* device conversion table */\n"
191 		  "struct devsw_conv devsw_conv0[] = {\n", fp) < 0)
192 		return (-1);
193 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
194 		if (fprintf(fp, "\t{ \"%s\", %d, %d },\n", dm->dm_name,
195 			    dm->dm_bmajor, dm->dm_cmajor) < 0)
196 			return (1);
197 	}
198 	if (fputs("};\n\n"
199 		  "struct devsw_conv *devsw_conv = devsw_conv0;\n"
200 		  "int max_devsw_convs = DEVSW_ARRAY_SIZE(devsw_conv0);\n",
201 		  fp) < 0)
202 		return (1);
203 
204 	return (0);
205 }
206 
207 /*
208  * Emit specific device major informations.
209  */
210 static int
211 emitdev(FILE *fp)
212 {
213 	struct devm *dm;
214 	char mstr[16];
215 
216 	if (fputs("\n", fp) < 0)
217 		return (1);
218 
219 	(void)strlcpy(mstr, "swap", sizeof(mstr));
220 	if ((dm = ht_lookup(bdevmtab, intern(mstr))) != NULL) {
221 		if (fprintf(fp, "const dev_t swapdev = makedev(%d, 0);\n",
222 			    dm->dm_bmajor) < 0)
223 			return (1);
224 	}
225 
226 	(void)strlcpy(mstr, "mem", sizeof(mstr));
227 	if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
228 		panic("memory device is not configured");
229 	if (fprintf(fp, "const dev_t zerodev = makedev(%d, DEV_ZERO);\n",
230 		    dm->dm_cmajor) < 0)
231 		return (1);
232 
233 	if (fputs("\n/* mem_no is only used in iskmemdev() */\n", fp) < 0)
234 		return (1);
235 	if (fprintf(fp, "const int mem_no = %d;\n", dm->dm_cmajor) < 0)
236 		return (1);
237 
238 	return (0);
239 }
240