Module Name: src Committed By: christos Date: Mon Nov 10 21:13:04 UTC 2014
Modified Files: src/usr.bin/config: mkdevsw.c Log Message: merge a bit of duplicated code. To generate a diff of this commit: cvs rdiff -u -r1.11 -r1.12 src/usr.bin/config/mkdevsw.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/config/mkdevsw.c diff -u src/usr.bin/config/mkdevsw.c:1.11 src/usr.bin/config/mkdevsw.c:1.12 --- src/usr.bin/config/mkdevsw.c:1.11 Wed Oct 29 13:14:50 2014 +++ src/usr.bin/config/mkdevsw.c Mon Nov 10 16:13:04 2014 @@ -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);