Home | History | Annotate | Line # | Download | only in dns
gen.c revision 1.3
      1  1.2  christos /*	$NetBSD: gen.c,v 1.3 2019/01/09 16:55:11 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.1  christos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     11  1.1  christos  * information regarding copyright ownership.
     12  1.1  christos  */
     13  1.1  christos 
     14  1.1  christos /*! \file */
     15  1.1  christos 
     16  1.1  christos #ifdef WIN32
     17  1.1  christos /*
     18  1.1  christos  * Silence compiler warnings about using strcpy and friends.
     19  1.1  christos  */
     20  1.1  christos #define _CRT_SECURE_NO_DEPRECATE 1
     21  1.1  christos /*
     22  1.1  christos  * We use snprintf which was defined late in Windows even it is in C99.
     23  1.1  christos  */
     24  1.1  christos #if _MSC_VER < 1900
     25  1.1  christos #define snprintf _snprintf
     26  1.1  christos #endif
     27  1.1  christos #endif
     28  1.1  christos 
     29  1.1  christos #include <sys/types.h>
     30  1.1  christos 
     31  1.1  christos #include <ctype.h>
     32  1.1  christos #include <stdlib.h>
     33  1.1  christos #include <stdio.h>
     34  1.1  christos #include <stdlib.h>
     35  1.1  christos #include <string.h>
     36  1.1  christos #include <time.h>
     37  1.1  christos 
     38  1.1  christos #ifdef WIN32
     39  1.1  christos #include "gen-win32.h"
     40  1.1  christos #else
     41  1.1  christos #include "gen-unix.h"
     42  1.1  christos #endif
     43  1.1  christos 
     44  1.1  christos #define INSIST(cond) \
     45  1.1  christos 	if (!(cond)) { \
     46  1.1  christos 		fprintf(stderr, "%s:%d: INSIST(%s)\n", \
     47  1.1  christos 			 __FILE__, __LINE__, #cond); \
     48  1.1  christos 		abort(); \
     49  1.1  christos 	}
     50  1.1  christos 
     51  1.1  christos #define FROMTEXTARGS "rdclass, type, lexer, origin, options, target, callbacks"
     52  1.1  christos #define FROMTEXTCLASS "rdclass"
     53  1.1  christos #define FROMTEXTTYPE "type"
     54  1.1  christos #define FROMTEXTDEF "result = DNS_R_UNKNOWN"
     55  1.1  christos 
     56  1.1  christos #define TOTEXTARGS "rdata, tctx, target"
     57  1.1  christos #define TOTEXTCLASS "rdata->rdclass"
     58  1.1  christos #define TOTEXTTYPE "rdata->type"
     59  1.3  christos #define TOTEXTDEF "use_default = true"
     60  1.1  christos 
     61  1.1  christos #define FROMWIREARGS "rdclass, type, source, dctx, options, target"
     62  1.1  christos #define FROMWIRECLASS "rdclass"
     63  1.1  christos #define FROMWIRETYPE "type"
     64  1.3  christos #define FROMWIREDEF "use_default = true"
     65  1.1  christos 
     66  1.1  christos #define TOWIREARGS "rdata, cctx, target"
     67  1.1  christos #define TOWIRECLASS "rdata->rdclass"
     68  1.1  christos #define TOWIRETYPE "rdata->type"
     69  1.3  christos #define TOWIREDEF "use_default = true"
     70  1.1  christos 
     71  1.1  christos #define FROMSTRUCTARGS "rdclass, type, source, target"
     72  1.1  christos #define FROMSTRUCTCLASS "rdclass"
     73  1.1  christos #define FROMSTRUCTTYPE "type"
     74  1.3  christos #define FROMSTRUCTDEF "use_default = true"
     75  1.1  christos 
     76  1.1  christos #define TOSTRUCTARGS "rdata, target, mctx"
     77  1.1  christos #define TOSTRUCTCLASS "rdata->rdclass"
     78  1.1  christos #define TOSTRUCTTYPE "rdata->type"
     79  1.3  christos #define TOSTRUCTDEF "use_default = true"
     80  1.1  christos 
     81  1.1  christos #define FREESTRUCTARGS "source"
     82  1.1  christos #define FREESTRUCTCLASS "common->rdclass"
     83  1.1  christos #define FREESTRUCTTYPE "common->rdtype"
     84  1.1  christos #define FREESTRUCTDEF NULL
     85  1.1  christos 
     86  1.1  christos #define COMPAREARGS "rdata1, rdata2"
     87  1.1  christos #define COMPARECLASS "rdata1->rdclass"
     88  1.1  christos #define COMPARETYPE "rdata1->type"
     89  1.3  christos #define COMPAREDEF "use_default = true"
     90  1.1  christos 
     91  1.1  christos #define ADDITIONALDATAARGS "rdata, add, arg"
     92  1.1  christos #define ADDITIONALDATACLASS "rdata->rdclass"
     93  1.1  christos #define ADDITIONALDATATYPE "rdata->type"
     94  1.3  christos #define ADDITIONALDATADEF "use_default = true"
     95  1.1  christos 
     96  1.1  christos #define DIGESTARGS "rdata, digest, arg"
     97  1.1  christos #define DIGESTCLASS "rdata->rdclass"
     98  1.1  christos #define DIGESTTYPE "rdata->type"
     99  1.3  christos #define DIGESTDEF "use_default = true"
    100  1.1  christos 
    101  1.1  christos #define CHECKOWNERARGS "name, rdclass, type, wildcard"
    102  1.1  christos #define CHECKOWNERCLASS "rdclass"
    103  1.1  christos #define CHECKOWNERTYPE "type"
    104  1.3  christos #define CHECKOWNERDEF "result = true"
    105  1.1  christos 
    106  1.1  christos #define CHECKNAMESARGS "rdata, owner, bad"
    107  1.1  christos #define CHECKNAMESCLASS "rdata->rdclass"
    108  1.1  christos #define CHECKNAMESTYPE "rdata->type"
    109  1.3  christos #define CHECKNAMESDEF "result = true"
    110  1.1  christos 
    111  1.1  christos static const char copyright[] =
    112  1.1  christos "/*\n"
    113  1.1  christos " * Copyright (C) 1998%s  Internet Systems Consortium, Inc. (\"ISC\")\n"
    114  1.1  christos " *\n"
    115  1.1  christos " * This Source Code Form is subject to the terms of the Mozilla Public\n"
    116  1.1  christos " * License, v. 2.0. If a copy of the MPL was not distributed with this\n"
    117  1.1  christos " * file, You can obtain one at http://mozilla.org/MPL/2.0/.\n"
    118  1.1  christos " */\n"
    119  1.1  christos "\n"
    120  1.1  christos "/***************\n"
    121  1.1  christos " ***************\n"
    122  1.1  christos " ***************   THIS FILE IS AUTOMATICALLY GENERATED BY gen.c.\n"
    123  1.1  christos " ***************   DO NOT EDIT!\n"
    124  1.1  christos " ***************\n"
    125  1.1  christos " ***************/\n"
    126  1.1  christos "\n"
    127  1.1  christos "/*! \\file */\n"
    128  1.1  christos "\n";
    129  1.1  christos 
    130  1.1  christos #define STR_EXPAND(tok) #tok
    131  1.1  christos #define STR(tok) STR_EXPAND(tok)
    132  1.1  christos 
    133  1.1  christos #define TYPENAMES 256
    134  1.1  christos #define TYPECLASSLEN 20		/* DNS mnemonic size. Must be less than 100. */
    135  1.1  christos #define TYPECLASSBUF (TYPECLASSLEN + 1)
    136  1.1  christos #define TYPECLASSFMT "%" STR(TYPECLASSLEN) "[-0-9a-z]_%d"
    137  1.1  christos #define ATTRIBUTESIZE 256
    138  1.1  christos #define DIRNAMESIZE 256
    139  1.1  christos 
    140  1.1  christos static struct cc {
    141  1.1  christos 	struct cc *next;
    142  1.1  christos 	int rdclass;
    143  1.3  christos 	char classbuf[TYPECLASSBUF];
    144  1.1  christos } *classes;
    145  1.1  christos 
    146  1.1  christos static struct tt {
    147  1.1  christos 	struct tt *next;
    148  1.1  christos 	int rdclass;
    149  1.1  christos 	int type;
    150  1.3  christos 	char classbuf[TYPECLASSBUF];
    151  1.3  christos 	char typebuf[TYPECLASSBUF];
    152  1.3  christos 	char dirbuf[DIRNAMESIZE-30];	/* XXX Should be max path length */
    153  1.1  christos } *types;
    154  1.1  christos 
    155  1.1  christos static struct ttnam {
    156  1.3  christos 	char typebuf[TYPECLASSBUF];
    157  1.1  christos 	char macroname[TYPECLASSBUF];
    158  1.1  christos 	char attr[ATTRIBUTESIZE];
    159  1.1  christos 	unsigned int sorted;
    160  1.1  christos 	int type;
    161  1.1  christos } typenames[TYPENAMES];
    162  1.1  christos 
    163  1.1  christos static int maxtype = -1;
    164  1.1  christos 
    165  1.1  christos static char *
    166  1.1  christos upper(char *);
    167  1.1  christos static char *
    168  1.1  christos funname(const char *, char *);
    169  1.1  christos static void
    170  1.1  christos doswitch(const char *, const char *, const char *, const char *,
    171  1.1  christos 	 const char *, const char *);
    172  1.1  christos static void
    173  1.1  christos add(int, const char *, int, const char *, const char *);
    174  1.1  christos static void
    175  1.1  christos sd(int, const char *, const char *, char);
    176  1.1  christos static void
    177  1.1  christos insert_into_typenames(int, const char *, const char *);
    178  1.1  christos 
    179  1.1  christos /*%
    180  1.1  christos  * If you use more than 10 of these in, say, a printf(), you'll have problems.
    181  1.1  christos  */
    182  1.1  christos static char *
    183  1.1  christos upper(char *s) {
    184  1.1  christos 	static int buf_to_use = 0;
    185  1.1  christos 	static char buf[10][256];
    186  1.1  christos 	char *b;
    187  1.1  christos 	int c;
    188  1.1  christos 
    189  1.1  christos 	buf_to_use++;
    190  1.1  christos 	if (buf_to_use > 9)
    191  1.1  christos 		buf_to_use = 0;
    192  1.1  christos 
    193  1.1  christos 	b = buf[buf_to_use];
    194  1.1  christos 	memset(b, 0, 256);
    195  1.1  christos 
    196  1.1  christos 	while ((c = (*s++) & 0xff))
    197  1.1  christos 		*b++ = islower(c) ? toupper(c) : c;
    198  1.1  christos 	*b = '\0';
    199  1.1  christos 	return (buf[buf_to_use]);
    200  1.1  christos }
    201  1.1  christos 
    202  1.1  christos static char *
    203  1.1  christos funname(const char *s, char *buf) {
    204  1.1  christos 	char *b = buf;
    205  1.1  christos 	char c;
    206  1.1  christos 
    207  1.1  christos 	INSIST(strlen(s) < TYPECLASSBUF);
    208  1.1  christos 	while ((c = *s++)) {
    209  1.1  christos 		*b++ = (c == '-') ? '_' : c;
    210  1.1  christos 	}
    211  1.1  christos 	*b = '\0';
    212  1.1  christos 	return (buf);
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos static void
    216  1.1  christos doswitch(const char *name, const char *function, const char *args,
    217  1.1  christos 	 const char *tsw, const char *csw, const char *res)
    218  1.1  christos {
    219  1.1  christos 	struct tt *tt;
    220  1.1  christos 	int first = 1;
    221  1.1  christos 	int lasttype = 0;
    222  1.1  christos 	int subswitch = 0;
    223  1.1  christos 	char buf1[TYPECLASSBUF], buf2[TYPECLASSBUF];
    224  1.1  christos 	const char *result = " result =";
    225  1.1  christos 
    226  1.1  christos 	if (res == NULL)
    227  1.1  christos 		result = "";
    228  1.1  christos 
    229  1.1  christos 	for (tt = types; tt != NULL; tt = tt->next) {
    230  1.1  christos 		if (first) {
    231  1.1  christos 			fprintf(stdout, "\n#define %s \\\n", name);
    232  1.1  christos 			fprintf(stdout, "\tswitch (%s) { \\\n" /*}*/, tsw);
    233  1.1  christos 			first = 0;
    234  1.1  christos 		}
    235  1.1  christos 		if (tt->type != lasttype && subswitch) {
    236  1.1  christos 			if (res == NULL)
    237  1.1  christos 				fprintf(stdout, "\t\tdefault: break; \\\n");
    238  1.1  christos 			else
    239  1.1  christos 				fprintf(stdout,
    240  1.1  christos 					"\t\tdefault: %s; break; \\\n", res);
    241  1.1  christos 			fputs(/*{*/ "\t\t} \\\n", stdout);
    242  1.1  christos 			fputs("\t\tbreak; \\\n", stdout);
    243  1.1  christos 			subswitch = 0;
    244  1.1  christos 		}
    245  1.1  christos 		if (tt->rdclass && tt->type != lasttype) {
    246  1.1  christos 			fprintf(stdout, "\tcase %d: switch (%s) { \\\n" /*}*/,
    247  1.1  christos 				tt->type, csw);
    248  1.1  christos 			subswitch = 1;
    249  1.1  christos 		}
    250  1.1  christos 		if (tt->rdclass == 0)
    251  1.1  christos 			fprintf(stdout,
    252  1.1  christos 				"\tcase %d:%s %s_%s(%s); break;",
    253  1.1  christos 				tt->type, result, function,
    254  1.3  christos 				funname(tt->typebuf, buf1), args);
    255  1.1  christos 		else
    256  1.1  christos 			fprintf(stdout,
    257  1.1  christos 				"\t\tcase %d:%s %s_%s_%s(%s); break;",
    258  1.1  christos 				tt->rdclass, result, function,
    259  1.3  christos 				funname(tt->classbuf, buf1),
    260  1.3  christos 				funname(tt->typebuf, buf2), args);
    261  1.1  christos 		fputs(" \\\n", stdout);
    262  1.1  christos 		lasttype = tt->type;
    263  1.1  christos 	}
    264  1.1  christos 	if (subswitch) {
    265  1.1  christos 		if (res == NULL)
    266  1.1  christos 			fprintf(stdout, "\t\tdefault: break; \\\n");
    267  1.1  christos 		else
    268  1.1  christos 			fprintf(stdout, "\t\tdefault: %s; break; \\\n", res);
    269  1.1  christos 		fputs(/*{*/ "\t\t} \\\n", stdout);
    270  1.1  christos 		fputs("\t\tbreak; \\\n", stdout);
    271  1.1  christos 	}
    272  1.1  christos 	if (first) {
    273  1.1  christos 		if (res == NULL)
    274  1.1  christos 			fprintf(stdout, "\n#define %s\n", name);
    275  1.1  christos 		else
    276  1.1  christos 			fprintf(stdout, "\n#define %s %s;\n", name, res);
    277  1.1  christos 	} else {
    278  1.1  christos 		if (res == NULL)
    279  1.1  christos 			fprintf(stdout, "\tdefault: break; \\\n");
    280  1.1  christos 		else
    281  1.1  christos 			fprintf(stdout, "\tdefault: %s; break; \\\n", res);
    282  1.1  christos 		fputs(/*{*/ "\t}\n", stdout);
    283  1.1  christos 	}
    284  1.1  christos }
    285  1.1  christos 
    286  1.1  christos static struct ttnam *
    287  1.1  christos find_typename(int type) {
    288  1.1  christos 	int i;
    289  1.1  christos 
    290  1.1  christos 	for (i = 0; i < TYPENAMES; i++) {
    291  1.3  christos 		if (typenames[i].typebuf[0] != 0 &&
    292  1.1  christos 		    typenames[i].type == type)
    293  1.3  christos 		{
    294  1.1  christos 			return (&typenames[i]);
    295  1.3  christos 		}
    296  1.1  christos 	}
    297  1.1  christos 	return (NULL);
    298  1.1  christos }
    299  1.1  christos 
    300  1.1  christos static void
    301  1.3  christos insert_into_typenames(int type, const char *typebuf, const char *attr) {
    302  1.1  christos 	struct ttnam *ttn = NULL;
    303  1.1  christos 	size_t c;
    304  1.1  christos 	int i, n;
    305  1.1  christos 	char tmp[256];
    306  1.1  christos 
    307  1.3  christos 	INSIST(strlen(typebuf) < TYPECLASSBUF);
    308  1.1  christos 	for (i = 0; i < TYPENAMES; i++) {
    309  1.3  christos 		if (typenames[i].typebuf[0] != 0 &&
    310  1.1  christos 		    typenames[i].type == type &&
    311  1.3  christos 		    strcmp(typebuf, typenames[i].typebuf) != 0)
    312  1.3  christos 		{
    313  1.1  christos 			fprintf(stderr,
    314  1.1  christos 				"Error:  type %d has two names: %s, %s\n",
    315  1.3  christos 				type, typenames[i].typebuf, typebuf);
    316  1.1  christos 			exit(1);
    317  1.1  christos 		}
    318  1.3  christos 		if (typenames[i].typebuf[0] == 0 && ttn == NULL) {
    319  1.1  christos 			ttn = &typenames[i];
    320  1.3  christos 		}
    321  1.1  christos 	}
    322  1.1  christos 	if (ttn == NULL) {
    323  1.1  christos 		fprintf(stderr, "Error: typenames array too small\n");
    324  1.1  christos 		exit(1);
    325  1.1  christos 	}
    326  1.1  christos 
    327  1.1  christos 	/* XXXMUKS: This is redundant due to the INSIST above. */
    328  1.3  christos 	if (strlen(typebuf) > sizeof(ttn->typebuf) - 1) {
    329  1.1  christos 		fprintf(stderr, "Error:  type name %s is too long\n",
    330  1.3  christos 			typebuf);
    331  1.1  christos 		exit(1);
    332  1.1  christos 	}
    333  1.1  christos 
    334  1.3  christos 	strncpy(ttn->typebuf, typebuf, sizeof(ttn->typebuf));
    335  1.3  christos 	ttn->typebuf[sizeof(ttn->typebuf) - 1] = '\0';
    336  1.1  christos 
    337  1.3  christos 	strncpy(ttn->macroname, ttn->typebuf, sizeof(ttn->macroname));
    338  1.1  christos 	ttn->macroname[sizeof(ttn->macroname) - 1] = '\0';
    339  1.1  christos 
    340  1.1  christos 	ttn->type = type;
    341  1.1  christos 	c = strlen(ttn->macroname);
    342  1.1  christos 	while (c > 0) {
    343  1.3  christos 		if (ttn->macroname[c - 1] == '-') {
    344  1.1  christos 			ttn->macroname[c - 1] = '_';
    345  1.3  christos 		}
    346  1.1  christos 		c--;
    347  1.1  christos 	}
    348  1.1  christos 
    349  1.1  christos 	if (attr == NULL) {
    350  1.1  christos 		n = snprintf(tmp, sizeof(tmp),
    351  1.1  christos 			     "RRTYPE_%s_ATTRIBUTES", upper(ttn->macroname));
    352  1.1  christos 		INSIST(n > 0 && (unsigned)n < sizeof(tmp));
    353  1.1  christos 		attr = tmp;
    354  1.1  christos 	}
    355  1.1  christos 
    356  1.1  christos 	if (ttn->attr[0] != 0 && strcmp(attr, ttn->attr) != 0) {
    357  1.1  christos 		fprintf(stderr, "Error:  type %d has different attributes: "
    358  1.1  christos 			"%s, %s\n", type, ttn->attr, attr);
    359  1.1  christos 		exit(1);
    360  1.1  christos 	}
    361  1.1  christos 
    362  1.1  christos 	if (strlen(attr) > sizeof(ttn->attr) - 1) {
    363  1.1  christos 		fprintf(stderr, "Error:  attr (%s) [name %s] is too long\n",
    364  1.3  christos 			attr, typebuf);
    365  1.1  christos 		exit(1);
    366  1.1  christos 	}
    367  1.1  christos 
    368  1.1  christos 	strncpy(ttn->attr, attr, sizeof(ttn->attr));
    369  1.1  christos 	ttn->attr[sizeof(ttn->attr) - 1] = '\0';
    370  1.1  christos 
    371  1.1  christos 	ttn->sorted = 0;
    372  1.3  christos 	if (maxtype < type) {
    373  1.1  christos 		maxtype = type;
    374  1.3  christos 	}
    375  1.1  christos }
    376  1.1  christos 
    377  1.1  christos static void
    378  1.3  christos add(int rdclass, const char *classbuf, int type, const char *typebuf,
    379  1.3  christos     const char *dirbuf)
    380  1.1  christos {
    381  1.1  christos 	struct tt *newtt = (struct tt *)malloc(sizeof(*newtt));
    382  1.1  christos 	struct tt *tt, *oldtt;
    383  1.1  christos 	struct cc *newcc;
    384  1.1  christos 	struct cc *cc, *oldcc;
    385  1.1  christos 
    386  1.3  christos 	INSIST(strlen(typebuf) < TYPECLASSBUF);
    387  1.3  christos 	INSIST(strlen(classbuf) < TYPECLASSBUF);
    388  1.3  christos 	INSIST(strlen(dirbuf) < DIRNAMESIZE);
    389  1.1  christos 
    390  1.3  christos 	insert_into_typenames(type, typebuf, NULL);
    391  1.1  christos 
    392  1.1  christos 	if (newtt == NULL) {
    393  1.1  christos 		fprintf(stderr, "malloc() failed\n");
    394  1.1  christos 		exit(1);
    395  1.1  christos 	}
    396  1.1  christos 
    397  1.1  christos 	newtt->next = NULL;
    398  1.1  christos 	newtt->rdclass = rdclass;
    399  1.1  christos 	newtt->type = type;
    400  1.1  christos 
    401  1.3  christos 	strncpy(newtt->classbuf, classbuf, sizeof(newtt->classbuf));
    402  1.3  christos 	newtt->classbuf[sizeof(newtt->classbuf) - 1] = '\0';
    403  1.1  christos 
    404  1.3  christos 	strncpy(newtt->typebuf, typebuf, sizeof(newtt->typebuf));
    405  1.3  christos 	newtt->typebuf[sizeof(newtt->typebuf) - 1] = '\0';
    406  1.1  christos 
    407  1.3  christos 	if (strncmp(dirbuf, "./", 2) == 0) {
    408  1.3  christos 		dirbuf += 2;
    409  1.3  christos 	}
    410  1.3  christos 	strncpy(newtt->dirbuf, dirbuf, sizeof(newtt->dirbuf));
    411  1.3  christos 	newtt->dirbuf[sizeof(newtt->dirbuf) - 1] = '\0';
    412  1.1  christos 
    413  1.1  christos 	tt = types;
    414  1.1  christos 	oldtt = NULL;
    415  1.1  christos 
    416  1.1  christos 	while ((tt != NULL) && (tt->type < type)) {
    417  1.1  christos 		oldtt = tt;
    418  1.1  christos 		tt = tt->next;
    419  1.1  christos 	}
    420  1.1  christos 
    421  1.1  christos 	while ((tt != NULL) && (tt->type == type) && (tt->rdclass < rdclass)) {
    422  1.3  christos 		if (strcmp(tt->typebuf, typebuf) != 0) {
    423  1.1  christos 			exit(1);
    424  1.3  christos 		}
    425  1.1  christos 		oldtt = tt;
    426  1.1  christos 		tt = tt->next;
    427  1.1  christos 	}
    428  1.1  christos 
    429  1.3  christos 	if ((tt != NULL) && (tt->type == type) && (tt->rdclass == rdclass)) {
    430  1.1  christos 		exit(1);
    431  1.3  christos 	}
    432  1.1  christos 
    433  1.1  christos 	newtt->next = tt;
    434  1.3  christos 	if (oldtt != NULL) {
    435  1.1  christos 		oldtt->next = newtt;
    436  1.3  christos 	} else {
    437  1.1  christos 		types = newtt;
    438  1.3  christos 	}
    439  1.1  christos 
    440  1.1  christos 	/*
    441  1.1  christos 	 * Do a class switch for this type.
    442  1.1  christos 	 */
    443  1.3  christos 	if (rdclass == 0) {
    444  1.1  christos 		return;
    445  1.3  christos 	}
    446  1.1  christos 
    447  1.1  christos 	newcc = (struct cc *)malloc(sizeof(*newcc));
    448  1.1  christos 	if (newcc == NULL) {
    449  1.1  christos 		fprintf(stderr, "malloc() failed\n");
    450  1.1  christos 		exit(1);
    451  1.1  christos 	}
    452  1.1  christos 	newcc->rdclass = rdclass;
    453  1.3  christos 	strncpy(newcc->classbuf, classbuf, sizeof(newcc->classbuf));
    454  1.3  christos 	newcc->classbuf[sizeof(newcc->classbuf) - 1] = '\0';
    455  1.1  christos 	cc = classes;
    456  1.1  christos 	oldcc = NULL;
    457  1.1  christos 
    458  1.1  christos 	while ((cc != NULL) && (cc->rdclass < rdclass)) {
    459  1.1  christos 		oldcc = cc;
    460  1.1  christos 		cc = cc->next;
    461  1.1  christos 	}
    462  1.1  christos 
    463  1.1  christos 	if ((cc != NULL) && cc->rdclass == rdclass) {
    464  1.1  christos 		free((char *)newcc);
    465  1.1  christos 		return;
    466  1.1  christos 	}
    467  1.1  christos 
    468  1.1  christos 	newcc->next = cc;
    469  1.3  christos 	if (oldcc != NULL) {
    470  1.1  christos 		oldcc->next = newcc;
    471  1.3  christos 	} else {
    472  1.1  christos 		classes = newcc;
    473  1.3  christos 	}
    474  1.1  christos }
    475  1.1  christos 
    476  1.1  christos static void
    477  1.3  christos sd(int rdclass, const char *classbuf, const char *dirbuf, char filetype) {
    478  1.1  christos 	char buf[TYPECLASSLEN + sizeof("_65535.h")];
    479  1.3  christos 	char typebuf[TYPECLASSBUF];
    480  1.1  christos 	int type, n;
    481  1.1  christos 	isc_dir_t dir;
    482  1.1  christos 
    483  1.3  christos 	if (!start_directory(dirbuf, &dir)) {
    484  1.1  christos 		return;
    485  1.3  christos 	}
    486  1.1  christos 
    487  1.1  christos 	while (next_file(&dir)) {
    488  1.3  christos 		if (sscanf(dir.filename, TYPECLASSFMT, typebuf, &type) != 2) {
    489  1.1  christos 			continue;
    490  1.3  christos 		}
    491  1.3  christos 		if ((type > 65535) || (type < 0)) {
    492  1.1  christos 			continue;
    493  1.3  christos 		}
    494  1.1  christos 
    495  1.3  christos 		n = snprintf(buf, sizeof(buf), "%s_%d.%c", typebuf,
    496  1.1  christos 			     type, filetype);
    497  1.1  christos 		INSIST(n > 0 && (unsigned)n < sizeof(buf));
    498  1.3  christos 		if (strcmp(buf, dir.filename) != 0) {
    499  1.1  christos 			continue;
    500  1.3  christos 		}
    501  1.3  christos 		add(rdclass, classbuf, type, typebuf, dirbuf);
    502  1.1  christos 	}
    503  1.1  christos 
    504  1.1  christos 	end_directory(&dir);
    505  1.1  christos }
    506  1.1  christos 
    507  1.1  christos static unsigned int
    508  1.1  christos HASH(char *string) {
    509  1.1  christos 	size_t n;
    510  1.1  christos 	unsigned char a, b;
    511  1.1  christos 
    512  1.1  christos 	n = strlen(string);
    513  1.1  christos 	if (n == 0) {
    514  1.1  christos 		fprintf(stderr, "n == 0?\n");
    515  1.1  christos 		exit(1);
    516  1.1  christos 	}
    517  1.1  christos 	a = tolower((unsigned char)string[0]);
    518  1.1  christos 	b = tolower((unsigned char)string[n - 1]);
    519  1.1  christos 
    520  1.1  christos 	return ((a + n) * b) % 256;
    521  1.1  christos }
    522  1.1  christos 
    523  1.1  christos int
    524  1.1  christos main(int argc, char **argv) {
    525  1.1  christos 	char buf[DIRNAMESIZE];		/* XXX Should be max path length */
    526  1.1  christos 	char srcdir[DIRNAMESIZE];	/* XXX Should be max path length */
    527  1.1  christos 	int rdclass;
    528  1.3  christos 	char classbuf[TYPECLASSBUF];
    529  1.1  christos 	struct tt *tt;
    530  1.1  christos 	struct cc *cc;
    531  1.1  christos 	struct ttnam *ttn, *ttn2;
    532  1.1  christos 	unsigned int hash;
    533  1.1  christos 	struct tm *tm;
    534  1.1  christos 	time_t now;
    535  1.1  christos 	char year[11];
    536  1.1  christos 	int lasttype;
    537  1.1  christos 	int code = 1;
    538  1.1  christos 	int class_enum = 0;
    539  1.1  christos 	int type_enum = 0;
    540  1.1  christos 	int structs = 0;
    541  1.1  christos 	int depend = 0;
    542  1.1  christos 	int c, i, j, n;
    543  1.1  christos 	char buf1[TYPECLASSBUF];
    544  1.1  christos 	char filetype = 'c';
    545  1.1  christos 	FILE *fd;
    546  1.1  christos 	char *prefix = NULL;
    547  1.1  christos 	char *suffix = NULL;
    548  1.1  christos 	char *file = NULL;
    549  1.1  christos 	isc_dir_t dir;
    550  1.1  christos 
    551  1.1  christos 	for (i = 0; i < TYPENAMES; i++)
    552  1.1  christos 		memset(&typenames[i], 0, sizeof(typenames[i]));
    553  1.1  christos 
    554  1.1  christos 	srcdir[0] = '\0';
    555  1.1  christos 	while ((c = isc_commandline_parse(argc, argv, "cdits:F:P:S:")) != -1)
    556  1.1  christos 		switch (c) {
    557  1.1  christos 		case 'c':
    558  1.1  christos 			code = 0;
    559  1.1  christos 			depend = 0;
    560  1.1  christos 			type_enum = 0;
    561  1.1  christos 			class_enum = 1;
    562  1.1  christos 			filetype = 'c';
    563  1.1  christos 			structs = 0;
    564  1.1  christos 			break;
    565  1.1  christos 		case 'd':
    566  1.1  christos 			code = 0;
    567  1.1  christos 			depend = 1;
    568  1.1  christos 			class_enum = 0;
    569  1.1  christos 			type_enum = 0;
    570  1.1  christos 			structs = 0;
    571  1.1  christos 			filetype = 'h';
    572  1.1  christos 			break;
    573  1.1  christos 		case 't':
    574  1.1  christos 			code = 0;
    575  1.1  christos 			depend = 0;
    576  1.1  christos 			class_enum = 0;
    577  1.1  christos 			type_enum = 1;
    578  1.1  christos 			filetype = 'c';
    579  1.1  christos 			structs = 0;
    580  1.1  christos 			break;
    581  1.1  christos 		case 'i':
    582  1.1  christos 			code = 0;
    583  1.1  christos 			depend = 0;
    584  1.1  christos 			class_enum = 0;
    585  1.1  christos 			type_enum = 0;
    586  1.1  christos 			structs = 1;
    587  1.1  christos 			filetype = 'h';
    588  1.1  christos 			break;
    589  1.1  christos 		case 's':
    590  1.1  christos 			if (strlen(isc_commandline_argument) >
    591  1.1  christos 			    DIRNAMESIZE - 2 * TYPECLASSLEN  -
    592  1.3  christos 			    sizeof("/rdata/_65535_65535"))
    593  1.3  christos 			{
    594  1.1  christos 				fprintf(stderr, "\"%s\" too long\n",
    595  1.1  christos 					isc_commandline_argument);
    596  1.1  christos 				exit(1);
    597  1.1  christos 			}
    598  1.1  christos 			n = snprintf(srcdir, sizeof(srcdir), "%s/",
    599  1.1  christos 				     isc_commandline_argument);
    600  1.1  christos 			INSIST(n > 0 && (unsigned)n < sizeof(srcdir));
    601  1.1  christos 			break;
    602  1.1  christos 		case 'F':
    603  1.1  christos 			file = isc_commandline_argument;
    604  1.1  christos 			break;
    605  1.1  christos 		case 'P':
    606  1.1  christos 			prefix = isc_commandline_argument;
    607  1.1  christos 			break;
    608  1.1  christos 		case 'S':
    609  1.1  christos 			suffix = isc_commandline_argument;
    610  1.1  christos 			break;
    611  1.1  christos 		case '?':
    612  1.1  christos 			exit(1);
    613  1.1  christos 		}
    614  1.1  christos 
    615  1.1  christos 	n = snprintf(buf, sizeof(buf), "%srdata", srcdir);
    616  1.1  christos 	INSIST(n > 0 && (unsigned)n < sizeof(srcdir));
    617  1.1  christos 
    618  1.3  christos 	if (!start_directory(buf, &dir)) {
    619  1.1  christos 		exit(1);
    620  1.3  christos 	}
    621  1.1  christos 
    622  1.1  christos 	while (next_file(&dir)) {
    623  1.3  christos 		if (sscanf(dir.filename, TYPECLASSFMT, classbuf,
    624  1.1  christos 			   &rdclass) != 2)
    625  1.3  christos 		{
    626  1.1  christos 			continue;
    627  1.3  christos 		}
    628  1.3  christos 		if ((rdclass > 65535) || (rdclass < 0)) {
    629  1.1  christos 			continue;
    630  1.3  christos 		}
    631  1.1  christos 
    632  1.1  christos 		n = snprintf(buf, sizeof(buf), "%srdata/%s_%d",
    633  1.3  christos 			     srcdir, classbuf, rdclass);
    634  1.1  christos 		INSIST(n > 0 && (unsigned)n < sizeof(buf));
    635  1.3  christos 		if (strcmp(buf + 6 + strlen(srcdir), dir.filename) != 0) {
    636  1.1  christos 			continue;
    637  1.3  christos 		}
    638  1.3  christos 		sd(rdclass, classbuf, buf, filetype);
    639  1.1  christos 	}
    640  1.1  christos 	end_directory(&dir);
    641  1.1  christos 	n = snprintf(buf, sizeof(buf), "%srdata/generic", srcdir);
    642  1.1  christos 	INSIST(n > 0 && (unsigned)n < sizeof(srcdir));
    643  1.1  christos 	sd(0, "", buf, filetype);
    644  1.1  christos 
    645  1.1  christos 	if (time(&now) != -1) {
    646  1.1  christos 		if ((tm = localtime(&now)) != NULL && tm->tm_year > 104) {
    647  1.1  christos 			n = snprintf(year, sizeof(year), "-%d",
    648  1.1  christos 				     tm->tm_year + 1900);
    649  1.1  christos 			INSIST(n > 0 && (unsigned)n < sizeof(year));
    650  1.1  christos 		} else {
    651  1.1  christos 			snprintf(year, sizeof(year), "-2016");
    652  1.1  christos 		}
    653  1.1  christos 	} else {
    654  1.1  christos 		snprintf(year, sizeof(year), "-2016");
    655  1.1  christos 	}
    656  1.1  christos 
    657  1.3  christos 	if (!depend) {
    658  1.1  christos 		fprintf(stdout, copyright, year);
    659  1.3  christos 	}
    660  1.1  christos 
    661  1.1  christos 	if (code) {
    662  1.1  christos 		fputs("#ifndef DNS_CODE_H\n", stdout);
    663  1.1  christos 		fputs("#define DNS_CODE_H 1\n\n", stdout);
    664  1.1  christos 
    665  1.3  christos 		fputs("#include <stdbool.h>\n", stdout);
    666  1.1  christos 		fputs("#include <isc/result.h>\n\n", stdout);
    667  1.1  christos 		fputs("#include <dns/name.h>\n\n", stdout);
    668  1.1  christos 
    669  1.3  christos 		for (tt = types; tt != NULL; tt = tt->next) {
    670  1.1  christos 			fprintf(stdout, "#include \"%s/%s_%d.c\"\n",
    671  1.3  christos 				tt->dirbuf, tt->typebuf, tt->type);
    672  1.3  christos 		}
    673  1.1  christos 
    674  1.1  christos 		fputs("\n\n", stdout);
    675  1.1  christos 
    676  1.1  christos 		doswitch("FROMTEXTSWITCH", "fromtext", FROMTEXTARGS,
    677  1.1  christos 			 FROMTEXTTYPE, FROMTEXTCLASS, FROMTEXTDEF);
    678  1.1  christos 		doswitch("TOTEXTSWITCH", "totext", TOTEXTARGS,
    679  1.1  christos 			 TOTEXTTYPE, TOTEXTCLASS, TOTEXTDEF);
    680  1.1  christos 		doswitch("FROMWIRESWITCH", "fromwire", FROMWIREARGS,
    681  1.1  christos 			 FROMWIRETYPE, FROMWIRECLASS, FROMWIREDEF);
    682  1.1  christos 		doswitch("TOWIRESWITCH", "towire", TOWIREARGS,
    683  1.1  christos 			 TOWIRETYPE, TOWIRECLASS, TOWIREDEF);
    684  1.1  christos 		doswitch("COMPARESWITCH", "compare", COMPAREARGS,
    685  1.1  christos 			  COMPARETYPE, COMPARECLASS, COMPAREDEF);
    686  1.1  christos 		doswitch("CASECOMPARESWITCH", "casecompare", COMPAREARGS,
    687  1.1  christos 			  COMPARETYPE, COMPARECLASS, COMPAREDEF);
    688  1.1  christos 		doswitch("FROMSTRUCTSWITCH", "fromstruct", FROMSTRUCTARGS,
    689  1.1  christos 			  FROMSTRUCTTYPE, FROMSTRUCTCLASS, FROMSTRUCTDEF);
    690  1.1  christos 		doswitch("TOSTRUCTSWITCH", "tostruct", TOSTRUCTARGS,
    691  1.1  christos 			  TOSTRUCTTYPE, TOSTRUCTCLASS, TOSTRUCTDEF);
    692  1.1  christos 		doswitch("FREESTRUCTSWITCH", "freestruct", FREESTRUCTARGS,
    693  1.1  christos 			  FREESTRUCTTYPE, FREESTRUCTCLASS, FREESTRUCTDEF);
    694  1.1  christos 		doswitch("ADDITIONALDATASWITCH", "additionaldata",
    695  1.1  christos 			 ADDITIONALDATAARGS, ADDITIONALDATATYPE,
    696  1.1  christos 			 ADDITIONALDATACLASS, ADDITIONALDATADEF);
    697  1.1  christos 		doswitch("DIGESTSWITCH", "digest",
    698  1.1  christos 			 DIGESTARGS, DIGESTTYPE,
    699  1.1  christos 			 DIGESTCLASS, DIGESTDEF);
    700  1.1  christos 		doswitch("CHECKOWNERSWITCH", "checkowner",
    701  1.1  christos 			CHECKOWNERARGS, CHECKOWNERTYPE,
    702  1.1  christos 			CHECKOWNERCLASS, CHECKOWNERDEF);
    703  1.1  christos 		doswitch("CHECKNAMESSWITCH", "checknames",
    704  1.1  christos 			CHECKNAMESARGS, CHECKNAMESTYPE,
    705  1.1  christos 			CHECKNAMESCLASS, CHECKNAMESDEF);
    706  1.1  christos 
    707  1.1  christos 		/*
    708  1.1  christos 		 * From here down, we are processing the rdata names and
    709  1.1  christos 		 * attributes.
    710  1.1  christos 		 */
    711  1.1  christos 
    712  1.1  christos #define PRINT_COMMA(x) (x == maxtype ? "" : ",")
    713  1.1  christos 
    714  1.1  christos #define METANOTQUESTION  "DNS_RDATATYPEATTR_META | " \
    715  1.1  christos 			 "DNS_RDATATYPEATTR_NOTQUESTION"
    716  1.1  christos #define METAQUESTIONONLY "DNS_RDATATYPEATTR_META | " \
    717  1.1  christos 			 "DNS_RDATATYPEATTR_QUESTIONONLY"
    718  1.3  christos #define RESERVEDNAME	 "0"
    719  1.3  christos #define RESERVED	 "DNS_RDATATYPEATTR_RESERVED"
    720  1.1  christos 
    721  1.1  christos 		/*
    722  1.1  christos 		 * Add in reserved/special types.  This will let us
    723  1.1  christos 		 * sort them without special cases.
    724  1.1  christos 		 */
    725  1.1  christos 		insert_into_typenames(0, "reserved0", RESERVED);
    726  1.3  christos 		insert_into_typenames(100, "uinfo", RESERVEDNAME);
    727  1.3  christos 		insert_into_typenames(101, "uid", RESERVEDNAME);
    728  1.3  christos 		insert_into_typenames(102, "gid", RESERVEDNAME);
    729  1.1  christos 		insert_into_typenames(251, "ixfr", METAQUESTIONONLY);
    730  1.1  christos 		insert_into_typenames(252, "axfr", METAQUESTIONONLY);
    731  1.1  christos 		insert_into_typenames(253, "mailb", METAQUESTIONONLY);
    732  1.1  christos 		insert_into_typenames(254, "maila", METAQUESTIONONLY);
    733  1.1  christos 		insert_into_typenames(255, "any", METAQUESTIONONLY);
    734  1.1  christos 
    735  1.1  christos 		/*
    736  1.1  christos 		 * Spit out a quick and dirty hash function.  Here,
    737  1.1  christos 		 * we walk through the list of type names, and calculate
    738  1.1  christos 		 * a hash.  This isn't perfect, but it will generate "pretty
    739  1.1  christos 		 * good" estimates.  Lowercase the characters before
    740  1.1  christos 		 * computing in all cases.
    741  1.1  christos 		 *
    742  1.1  christos 		 * Here, walk the list from top to bottom, calculating
    743  1.1  christos 		 * the hash (mod 256) for each name.
    744  1.1  christos 		 */
    745  1.1  christos 		fprintf(stdout, "#define RDATATYPE_COMPARE(_s, _d, _tn, _n, _tp) \\\n");
    746  1.1  christos 		fprintf(stdout, "\tdo { \\\n");
    747  1.1  christos 		fprintf(stdout, "\t\tif (sizeof(_s) - 1 == _n && \\\n"
    748  1.1  christos 				"\t\t    strncasecmp(_s,(_tn),"
    749  1.1  christos 				"(sizeof(_s) - 1)) == 0) { \\\n");
    750  1.1  christos 		fprintf(stdout, "\t\t\tif ((dns_rdatatype_attributes(_d) & "
    751  1.1  christos 				  "DNS_RDATATYPEATTR_RESERVED) != 0) \\\n");
    752  1.1  christos 		fprintf(stdout, "\t\t\t\treturn (ISC_R_NOTIMPLEMENTED); \\\n");
    753  1.1  christos 		fprintf(stdout, "\t\t\t*(_tp) = _d; \\\n");
    754  1.1  christos 		fprintf(stdout, "\t\t\treturn (ISC_R_SUCCESS); \\\n");
    755  1.1  christos 		fprintf(stdout, "\t\t} \\\n");
    756  1.2  christos 		fprintf(stdout, "\t} while (/*CONSTCOND*/0)\n\n");
    757  1.1  christos 
    758  1.1  christos 		fprintf(stdout, "#define RDATATYPE_FROMTEXT_SW(_hash,"
    759  1.1  christos 				"_typename,_length,_typep) \\\n");
    760  1.1  christos 		fprintf(stdout, "\tswitch (_hash) { \\\n");
    761  1.1  christos 		for (i = 0; i <= maxtype; i++) {
    762  1.1  christos 			ttn = find_typename(i);
    763  1.3  christos 			if (ttn == NULL) {
    764  1.1  christos 				continue;
    765  1.3  christos 			}
    766  1.1  christos 
    767  1.1  christos 			/*
    768  1.1  christos 			 * Skip entries we already processed.
    769  1.1  christos 			 */
    770  1.3  christos 			if (ttn->sorted != 0) {
    771  1.1  christos 				continue;
    772  1.3  christos 			}
    773  1.1  christos 
    774  1.3  christos 			hash = HASH(ttn->typebuf);
    775  1.1  christos 			fprintf(stdout, "\t\tcase %u: \\\n", hash);
    776  1.1  christos 
    777  1.1  christos 			/*
    778  1.1  christos 			 * Find all other entries that happen to match
    779  1.1  christos 			 * this hash.
    780  1.1  christos 			 */
    781  1.1  christos 			for (j = 0; j <= maxtype; j++) {
    782  1.1  christos 				ttn2 = find_typename(j);
    783  1.3  christos 				if (ttn2 == NULL) {
    784  1.1  christos 					continue;
    785  1.3  christos 				}
    786  1.3  christos 				if (hash == HASH(ttn2->typebuf)) {
    787  1.3  christos 					fprintf(stdout, "\t\t\t"
    788  1.3  christos 						"RDATATYPE_COMPARE"
    789  1.3  christos 						"(\"%s\", %d, _typename, "
    790  1.3  christos 						" _length, _typep); \\\n",
    791  1.3  christos 						ttn2->typebuf, ttn2->type);
    792  1.1  christos 					ttn2->sorted = 1;
    793  1.1  christos 				}
    794  1.1  christos 			}
    795  1.1  christos 			fprintf(stdout, "\t\t\tbreak; \\\n");
    796  1.1  christos 		}
    797  1.1  christos 		fprintf(stdout, "\t}\n");
    798  1.1  christos 
    799  1.1  christos 		fprintf(stdout, "#define RDATATYPE_ATTRIBUTE_SW \\\n");
    800  1.1  christos 		fprintf(stdout, "\tswitch (type) { \\\n");
    801  1.1  christos 		for (i = 0; i <= maxtype; i++) {
    802  1.1  christos 			ttn = find_typename(i);
    803  1.3  christos 			if (ttn == NULL) {
    804  1.1  christos 				continue;
    805  1.3  christos 			}
    806  1.1  christos 			fprintf(stdout, "\tcase %d: return (%s); \\\n",
    807  1.1  christos 				i, upper(ttn->attr));
    808  1.1  christos 		}
    809  1.1  christos 		fprintf(stdout, "\t}\n");
    810  1.1  christos 
    811  1.1  christos 		fprintf(stdout, "#define RDATATYPE_TOTEXT_SW \\\n");
    812  1.1  christos 		fprintf(stdout, "\tswitch (type) { \\\n");
    813  1.1  christos 		for (i = 0; i <= maxtype; i++) {
    814  1.1  christos 			ttn = find_typename(i);
    815  1.3  christos 			if (ttn == NULL) {
    816  1.1  christos 				continue;
    817  1.3  christos 			}
    818  1.1  christos 			/*
    819  1.1  christos 			 * Remove KEYDATA (65533) from the type to memonic
    820  1.1  christos 			 * translation as it is internal use only.  This
    821  1.1  christos 			 * stops the tools from displaying KEYDATA instead
    822  1.1  christos 			 * of TYPE65533.
    823  1.1  christos 			 */
    824  1.3  christos 			if (i == 65533U) {
    825  1.1  christos 				continue;
    826  1.3  christos 			}
    827  1.1  christos 			fprintf(stdout, "\tcase %d: return "
    828  1.1  christos 				"(str_totext(\"%s\", target)); \\\n",
    829  1.3  christos 				i, upper(ttn->typebuf));
    830  1.1  christos 		}
    831  1.1  christos 		fprintf(stdout, "\t}\n");
    832  1.1  christos 
    833  1.1  christos 		fputs("#endif /* DNS_CODE_H */\n", stdout);
    834  1.1  christos 	} else if (type_enum) {
    835  1.1  christos 		char *s;
    836  1.1  christos 
    837  1.1  christos 		fprintf(stdout, "#ifndef DNS_ENUMTYPE_H\n");
    838  1.1  christos 		fprintf(stdout, "#define DNS_ENUMTYPE_H 1\n\n");
    839  1.1  christos 
    840  1.1  christos 		fprintf(stdout, "enum {\n");
    841  1.1  christos 		fprintf(stdout, "\tdns_rdatatype_none = 0,\n");
    842  1.1  christos 
    843  1.1  christos 		lasttype = 0;
    844  1.3  christos 		for (tt = types; tt != NULL; tt = tt->next) {
    845  1.3  christos 			if (tt->type != lasttype) {
    846  1.1  christos 				fprintf(stdout,
    847  1.1  christos 					"\tdns_rdatatype_%s = %d,\n",
    848  1.3  christos 					funname(tt->typebuf, buf1),
    849  1.1  christos 					lasttype = tt->type);
    850  1.3  christos 			}
    851  1.3  christos 		}
    852  1.1  christos 
    853  1.1  christos 		fprintf(stdout, "\tdns_rdatatype_ixfr = 251,\n");
    854  1.1  christos 		fprintf(stdout, "\tdns_rdatatype_axfr = 252,\n");
    855  1.1  christos 		fprintf(stdout, "\tdns_rdatatype_mailb = 253,\n");
    856  1.1  christos 		fprintf(stdout, "\tdns_rdatatype_maila = 254,\n");
    857  1.1  christos 		fprintf(stdout, "\tdns_rdatatype_any = 255\n");
    858  1.1  christos 
    859  1.1  christos 		fprintf(stdout, "};\n\n");
    860  1.1  christos 
    861  1.1  christos 		fprintf(stdout, "#define dns_rdatatype_none\t"
    862  1.1  christos 			"((dns_rdatatype_t)dns_rdatatype_none)\n");
    863  1.1  christos 
    864  1.3  christos 		for (tt = types; tt != NULL; tt = tt->next) {
    865  1.1  christos 			if (tt->type != lasttype) {
    866  1.3  christos 				s = funname(tt->typebuf, buf1);
    867  1.1  christos 				fprintf(stdout,
    868  1.1  christos 					"#define dns_rdatatype_%s\t%s"
    869  1.1  christos 					"((dns_rdatatype_t)dns_rdatatype_%s)"
    870  1.1  christos 					"\n",
    871  1.1  christos 					s, strlen(s) < 2U ? "\t" : "", s);
    872  1.1  christos 				lasttype = tt->type;
    873  1.1  christos 			}
    874  1.3  christos 		}
    875  1.1  christos 
    876  1.1  christos 		fprintf(stdout, "#define dns_rdatatype_ixfr\t"
    877  1.1  christos 			"((dns_rdatatype_t)dns_rdatatype_ixfr)\n");
    878  1.1  christos 		fprintf(stdout, "#define dns_rdatatype_axfr\t"
    879  1.1  christos 			"((dns_rdatatype_t)dns_rdatatype_axfr)\n");
    880  1.1  christos 		fprintf(stdout, "#define dns_rdatatype_mailb\t"
    881  1.1  christos 			"((dns_rdatatype_t)dns_rdatatype_mailb)\n");
    882  1.1  christos 		fprintf(stdout, "#define dns_rdatatype_maila\t"
    883  1.1  christos 			"((dns_rdatatype_t)dns_rdatatype_maila)\n");
    884  1.1  christos 		fprintf(stdout, "#define dns_rdatatype_any\t"
    885  1.1  christos 			"((dns_rdatatype_t)dns_rdatatype_any)\n");
    886  1.1  christos 
    887  1.1  christos 		fprintf(stdout, "\n#endif /* DNS_ENUMTYPE_H */\n");
    888  1.1  christos 
    889  1.1  christos 	} else if (class_enum) {
    890  1.1  christos 		char *s;
    891  1.1  christos 		int classnum;
    892  1.1  christos 
    893  1.1  christos 		fprintf(stdout, "#ifndef DNS_ENUMCLASS_H\n");
    894  1.1  christos 		fprintf(stdout, "#define DNS_ENUMCLASS_H 1\n\n");
    895  1.1  christos 
    896  1.1  christos 		fprintf(stdout, "enum {\n");
    897  1.1  christos 
    898  1.1  christos 		fprintf(stdout, "\tdns_rdataclass_reserved0 = 0,\n");
    899  1.1  christos 		fprintf(stdout, "#define dns_rdataclass_reserved0 \\\n\t\t\t\t"
    900  1.1  christos 			"((dns_rdataclass_t)dns_rdataclass_reserved0)\n");
    901  1.1  christos 
    902  1.1  christos #define PRINTCLASS(name, num) \
    903  1.1  christos 	do { \
    904  1.1  christos 		s = funname(name, buf1); \
    905  1.1  christos 		classnum = num; \
    906  1.1  christos 		fprintf(stdout, "\tdns_rdataclass_%s = %d%s\n", s, classnum, \
    907  1.1  christos 		       classnum != 255 ? "," : ""); \
    908  1.1  christos 		fprintf(stdout, "#define dns_rdataclass_%s\t" \
    909  1.1  christos 		       "((dns_rdataclass_t)dns_rdataclass_%s)\n", s, s); \
    910  1.2  christos 	} while (/*CONSTCOND*/0)
    911  1.1  christos 
    912  1.1  christos 		for (cc = classes; cc != NULL; cc = cc->next) {
    913  1.3  christos 			if (cc->rdclass == 3) {
    914  1.1  christos 				PRINTCLASS("chaos", 3);
    915  1.3  christos 			} else if (cc->rdclass == 255) {
    916  1.1  christos 				PRINTCLASS("none", 254);
    917  1.3  christos 			}
    918  1.3  christos 			PRINTCLASS(cc->classbuf, cc->rdclass);
    919  1.1  christos 		}
    920  1.1  christos 
    921  1.1  christos #undef PRINTCLASS
    922  1.1  christos 
    923  1.1  christos 		fprintf(stdout, "};\n\n");
    924  1.1  christos 		fprintf(stdout, "#endif /* DNS_ENUMCLASS_H */\n");
    925  1.1  christos 	} else if (structs) {
    926  1.1  christos 		if (prefix != NULL) {
    927  1.1  christos 			if ((fd = fopen(prefix,"r")) != NULL) {
    928  1.3  christos 				while (fgets(buf, sizeof(buf), fd) != NULL) {
    929  1.1  christos 					fputs(buf, stdout);
    930  1.3  christos 				}
    931  1.1  christos 				fclose(fd);
    932  1.1  christos 			}
    933  1.1  christos 		}
    934  1.1  christos 		for (tt = types; tt != NULL; tt = tt->next) {
    935  1.1  christos 			snprintf(buf, sizeof(buf), "%s/%s_%d.h",
    936  1.3  christos 				tt->dirbuf, tt->typebuf, tt->type);
    937  1.1  christos 			if ((fd = fopen(buf,"r")) != NULL) {
    938  1.3  christos 				while (fgets(buf, sizeof(buf), fd) != NULL) {
    939  1.1  christos 					fputs(buf, stdout);
    940  1.3  christos 				}
    941  1.1  christos 				fclose(fd);
    942  1.1  christos 			}
    943  1.1  christos 		}
    944  1.1  christos 		if (suffix != NULL) {
    945  1.1  christos 			if ((fd = fopen(suffix,"r")) != NULL) {
    946  1.3  christos 				while (fgets(buf, sizeof(buf), fd) != NULL) {
    947  1.1  christos 					fputs(buf, stdout);
    948  1.3  christos 				}
    949  1.1  christos 				fclose(fd);
    950  1.1  christos 			}
    951  1.1  christos 		}
    952  1.1  christos 	} else if (depend) {
    953  1.3  christos 		for (tt = types; tt != NULL; tt = tt->next) {
    954  1.1  christos 			fprintf(stdout, "%s:\t%s/%s_%d.h\n", file,
    955  1.3  christos 				tt->dirbuf, tt->typebuf, tt->type);
    956  1.3  christos 		}
    957  1.1  christos 	}
    958  1.1  christos 
    959  1.3  christos 	if (ferror(stdout) != 0) {
    960  1.1  christos 		exit(1);
    961  1.3  christos 	}
    962  1.1  christos 
    963  1.1  christos 	return (0);
    964  1.1  christos }
    965