xref: /netbsd-src/usr.bin/config/mkdevsw.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: mkdevsw.c,v 1.6 2008/04/28 20:24:12 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <err.h>
40 
41 #include "defs.h"
42 
43 static void emitconv(FILE *);
44 static void emitdev(FILE *);
45 static void emitdevm(FILE *);
46 static void emitheader(FILE *);
47 
48 int
49 mkdevsw(void)
50 {
51 	FILE *fp;
52 
53 	if ((fp = fopen("devsw.c.tmp", "w")) == NULL) {
54 		warn("cannot create devsw.c");
55 		return (1);
56 	}
57 
58 	emitheader(fp);
59 	emitdevm(fp);
60 	emitconv(fp);
61 	emitdev(fp);
62 
63 	fflush(fp);
64 	if (ferror(fp)) {
65 		warn("error writing devsw.c");
66 		fclose(fp);
67 		return 1;
68 	}
69 
70 	(void)fclose(fp);
71 
72 	if (moveifchanged("devsw.c.tmp", "devsw.c") != 0) {
73 		warn("error renaming devsw.c");
74 		return (1);
75 	}
76 
77 	return (0);
78 }
79 
80 static void
81 emitheader(FILE *fp)
82 {
83 	autogen_comment(fp, "devsw.c");
84 
85 	fputs("#include <sys/param.h>\n"
86 		  "#include <sys/conf.h>\n"
87 		  "\n#define\tDEVSW_ARRAY_SIZE(x)\t"
88 		  "(sizeof((x))/sizeof((x)[0]))\n", fp);
89 }
90 
91 /*
92  * Emit device switch table for character/block device.
93  */
94 static void
95 emitdevm(FILE *fp)
96 {
97 	struct devm *dm;
98 	char mstr[16];
99 	int i;
100 
101 	fputs("\n/* device switch table for block device */\n", fp);
102 
103 	for (i = 0 ; i <= maxbdevm ; i++) {
104 		(void)snprintf(mstr, sizeof(mstr), "%d", i);
105 		if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL)
106 			continue;
107 
108 		fprintf(fp, "extern const struct bdevsw %s_bdevsw;\n",
109 			    dm->dm_name);
110 	}
111 
112 	fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp);
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 			fprintf(fp, "\tNULL,\n");
118 		} else {
119 			fprintf(fp, "\t&%s_bdevsw,\n", dm->dm_name);
120 		}
121 	}
122 
123 	fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp);
124 
125 	fputs("const int sys_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n"
126 		  "int max_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n", fp);
127 
128 	fputs("\n/* device switch table for character device */\n", fp);
129 
130 	for (i = 0 ; i <= maxcdevm ; i++) {
131 		(void)snprintf(mstr, sizeof(mstr), "%d", i);
132 		if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
133 			continue;
134 
135 		fprintf(fp, "extern const struct cdevsw %s_cdevsw;\n",
136 			    dm->dm_name);
137 	}
138 
139 	fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp);
140 
141 	for (i = 0 ; i <= maxcdevm ; i++) {
142 		(void)snprintf(mstr, sizeof(mstr), "%d", i);
143 		if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) {
144 			fprintf(fp, "\tNULL,\n");
145 		} else {
146 			fprintf(fp, "\t&%s_cdevsw,\n", dm->dm_name);
147 		}
148 	}
149 
150 	fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp);
151 
152 	fputs("const int sys_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n"
153 		  "int max_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n", fp);
154 }
155 
156 /*
157  * Emit device major conversion table.
158  */
159 static void
160 emitconv(FILE *fp)
161 {
162 	struct devm *dm;
163 
164 	fputs("\n/* device conversion table */\n"
165 		  "struct devsw_conv devsw_conv0[] = {\n", fp);
166 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
167 		fprintf(fp, "\t{ \"%s\", %d, %d },\n", dm->dm_name,
168 			    dm->dm_bmajor, dm->dm_cmajor);
169 	}
170 	fputs("};\n\n"
171 		  "struct devsw_conv *devsw_conv = devsw_conv0;\n"
172 		  "int max_devsw_convs = DEVSW_ARRAY_SIZE(devsw_conv0);\n",
173 		  fp);
174 }
175 
176 /*
177  * Emit specific device major informations.
178  */
179 static void
180 emitdev(FILE *fp)
181 {
182 	struct devm *dm;
183 	char mstr[16];
184 
185 	fputs("\n", fp);
186 
187 	(void)strlcpy(mstr, "swap", sizeof(mstr));
188 	if ((dm = ht_lookup(bdevmtab, intern(mstr))) != NULL) {
189 		fprintf(fp, "const dev_t swapdev = makedev(%d, 0);\n",
190 			    dm->dm_bmajor);
191 	}
192 
193 	(void)strlcpy(mstr, "mem", sizeof(mstr));
194 	if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
195 		panic("memory device is not configured");
196 	fprintf(fp, "const dev_t zerodev = makedev(%d, DEV_ZERO);\n",
197 		    dm->dm_cmajor);
198 
199 	fputs("\n/* mem_no is only used in iskmemdev() */\n", fp);
200 	fprintf(fp, "const int mem_no = %d;\n", dm->dm_cmajor);
201 }
202