merge a bit of duplicated code.

This commit is contained in:
christos 2014-11-10 21:13:04 +00:00
parent 0d06f659c0
commit b552d112cd

View File

@ -1,4 +1,4 @@
/* $NetBSD: mkdevsw.c,v 1.11 2014/10/29 17:14:50 christos Exp $ */
/* $NetBSD: mkdevsw.c,v 1.12 2014/11/10 21:13:04 christos Exp $ */
/*
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -34,7 +34,7 @@
#endif
#include <sys/cdefs.h>
__RCSID("$NetBSD: mkdevsw.c,v 1.11 2014/10/29 17:14:50 christos Exp $");
__RCSID("$NetBSD: mkdevsw.c,v 1.12 2014/11/10 21:13:04 christos Exp $");
#include <stdio.h>
#include <string.h>
@ -89,37 +89,54 @@ emitheader(FILE *fp)
"#include <sys/conf.h>\n", fp);
}
static void
dentry(FILE *fp, struct hashtab *t, devmajor_t i, char p)
{
const struct devm *dm;
char mstr[16];
(void)snprintf(mstr, sizeof(mstr), "%d", i);
if ((dm = ht_lookup(t, intern(mstr))) == NULL)
return;
fprintf(fp, "extern const struct %cdevsw %s_%cdevsw;\n",
p, dm->dm_name, p);
}
static void
pentry(FILE *fp, struct hashtab *t, devmajor_t i, char p)
{
const struct devm *dm;
char mstr[16];
(void)snprintf(mstr, sizeof(mstr), "%d", i);
dm = ht_lookup(t, intern(mstr));
if (dm)
fprintf(fp, "\t&%s_%cdevsw", dm->dm_name, p);
else
fputs("\tNULL", fp);
fprintf(fp, ",\t// %3d\n", i);
}
/*
* Emit device switch table for character/block device.
*/
static void
emitdevm(FILE *fp)
{
struct devm *dm;
char mstr[16];
devmajor_t i;
fputs("\n/* device switch table for block device */\n", fp);
for (i = 0 ; i <= maxbdevm ; i++) {
(void)snprintf(mstr, sizeof(mstr), "%d", i);
if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL)
continue;
fprintf(fp, "extern const struct bdevsw %s_bdevsw;\n",
dm->dm_name);
}
for (i = 0; i <= maxbdevm ; i++)
dentry(fp, cdevmtab, i, 'b');
fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp);
for (i = 0 ; i <= maxbdevm ; i++) {
(void)snprintf(mstr, sizeof(mstr), "%d", i);
if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL) {
fprintf(fp, "\tNULL,\n");
} else {
fprintf(fp, "\t&%s_bdevsw,\n", dm->dm_name);
}
}
for (i = 0; i <= maxbdevm; i++)
pentry(fp, bdevmtab, i, 'b');
fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp);
@ -128,25 +145,13 @@ emitdevm(FILE *fp)
fputs("\n/* device switch table for character device */\n", fp);
for (i = 0 ; i <= maxcdevm ; i++) {
(void)snprintf(mstr, sizeof(mstr), "%d", i);
if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
continue;
fprintf(fp, "extern const struct cdevsw %s_cdevsw;\n",
dm->dm_name);
}
for (i = 0; i <= maxcdevm; i++)
dentry(fp, cdevmtab, i, 'c');
fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp);
for (i = 0 ; i <= maxcdevm ; i++) {
(void)snprintf(mstr, sizeof(mstr), "%d", i);
if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) {
fprintf(fp, "\tNULL,\n");
} else {
fprintf(fp, "\t&%s_cdevsw,\n", dm->dm_name);
}
}
for (i = 0; i <= maxcdevm; i++)
pentry(fp, cdevmtab, i, 'c');
fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp);