No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / config / mkdevsw.c
blob294d42a8f56fe1ba842a09870a8e4870a0f0adee
1 /* $NetBSD: mkdevsw.c,v 1.6 2008/04/28 20:24:12 martin Exp $ */
3 /*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by MAEKAWA Masahide (gehenna@NetBSD.org).
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.
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.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <err.h>
41 #include "defs.h"
43 static void emitconv(FILE *);
44 static void emitdev(FILE *);
45 static void emitdevm(FILE *);
46 static void emitheader(FILE *);
48 int
49 mkdevsw(void)
51 FILE *fp;
53 if ((fp = fopen("devsw.c.tmp", "w")) == NULL) {
54 warn("cannot create devsw.c");
55 return (1);
58 emitheader(fp);
59 emitdevm(fp);
60 emitconv(fp);
61 emitdev(fp);
63 fflush(fp);
64 if (ferror(fp)) {
65 warn("error writing devsw.c");
66 fclose(fp);
67 return 1;
70 (void)fclose(fp);
72 if (moveifchanged("devsw.c.tmp", "devsw.c") != 0) {
73 warn("error renaming devsw.c");
74 return (1);
77 return (0);
80 static void
81 emitheader(FILE *fp)
83 autogen_comment(fp, "devsw.c");
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);
92 * Emit device switch table for character/block device.
94 static void
95 emitdevm(FILE *fp)
97 struct devm *dm;
98 char mstr[16];
99 devmajor_t i;
101 fputs("\n/* device switch table for block device */\n", fp);
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;
108 fprintf(fp, "extern const struct bdevsw %s_bdevsw;\n",
109 dm->dm_name);
112 fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp);
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);
123 fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp);
125 fputs("const int sys_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n"
126 "int max_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n", fp);
128 fputs("\n/* device switch table for character device */\n", fp);
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;
135 fprintf(fp, "extern const struct cdevsw %s_cdevsw;\n",
136 dm->dm_name);
139 fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp);
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);
150 fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp);
152 fputs("const int sys_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n"
153 "int max_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n", fp);
157 * Emit device major conversion table.
159 static void
160 emitconv(FILE *fp)
162 struct devm *dm;
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);
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);
177 * Emit specific device major informations.
179 static void
180 emitdev(FILE *fp)
182 struct devm *dm;
183 char mstr[16];
185 fputs("\n", fp);
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);
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);
199 fputs("\n/* mem_no is only used in iskmemdev() */\n", fp);
200 fprintf(fp, "const int mem_no = %d;\n", dm->dm_cmajor);