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