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