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