gen.c revision 170222
1135446Strhodes/*
2170222Sdougb * Copyright (C) 2004-2006  Internet Systems Consortium, Inc. ("ISC")
3135446Strhodes * Copyright (C) 1998-2003  Internet Software Consortium.
4135446Strhodes *
5135446Strhodes * Permission to use, copy, modify, and distribute this software for any
6135446Strhodes * purpose with or without fee is hereby granted, provided that the above
7135446Strhodes * copyright notice and this permission notice appear in all copies.
8135446Strhodes *
9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11135446Strhodes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15135446Strhodes * PERFORMANCE OF THIS SOFTWARE.
16135446Strhodes */
17135446Strhodes
18170222Sdougb/* $Id: gen.c,v 1.73.18.6 2006/10/02 06:36:43 marka Exp $ */
19135446Strhodes
20170222Sdougb/*! \file */
21170222Sdougb
22165071Sdougb#ifdef WIN32
23165071Sdougb/*
24165071Sdougb * Silence compiler warnings about using strcpy and friends.
25165071Sdougb */
26165071Sdougb#define _CRT_SECURE_NO_DEPRECATE 1
27165071Sdougb#endif
28135446Strhodes
29135446Strhodes#include <sys/types.h>
30135446Strhodes
31135446Strhodes#include <ctype.h>
32135446Strhodes#include <stdlib.h>
33135446Strhodes#include <stdio.h>
34135446Strhodes#include <stdlib.h>
35135446Strhodes#include <string.h>
36135446Strhodes#include <time.h>
37135446Strhodes
38135446Strhodes#ifdef WIN32
39135446Strhodes#include "gen-win32.h"
40135446Strhodes#else
41135446Strhodes#include "gen-unix.h"
42135446Strhodes#endif
43135446Strhodes
44135446Strhodes#define FROMTEXTARGS "rdclass, type, lexer, origin, options, target, callbacks"
45135446Strhodes#define FROMTEXTCLASS "rdclass"
46135446Strhodes#define FROMTEXTTYPE "type"
47135446Strhodes#define FROMTEXTDEF "result = DNS_R_UNKNOWN"
48135446Strhodes
49135446Strhodes#define TOTEXTARGS "rdata, tctx, target"
50135446Strhodes#define TOTEXTCLASS "rdata->rdclass"
51135446Strhodes#define TOTEXTTYPE "rdata->type"
52135446Strhodes#define TOTEXTDEF "use_default = ISC_TRUE"
53135446Strhodes
54135446Strhodes#define FROMWIREARGS "rdclass, type, source, dctx, options, target"
55135446Strhodes#define FROMWIRECLASS "rdclass"
56135446Strhodes#define FROMWIRETYPE "type"
57135446Strhodes#define FROMWIREDEF "use_default = ISC_TRUE"
58135446Strhodes
59135446Strhodes#define TOWIREARGS "rdata, cctx, target"
60135446Strhodes#define TOWIRECLASS "rdata->rdclass"
61135446Strhodes#define TOWIRETYPE "rdata->type"
62135446Strhodes#define TOWIREDEF "use_default = ISC_TRUE"
63135446Strhodes
64135446Strhodes#define FROMSTRUCTARGS "rdclass, type, source, target"
65135446Strhodes#define FROMSTRUCTCLASS "rdclass"
66135446Strhodes#define FROMSTRUCTTYPE "type"
67135446Strhodes#define FROMSTRUCTDEF "use_default = ISC_TRUE"
68135446Strhodes
69135446Strhodes#define TOSTRUCTARGS "rdata, target, mctx"
70135446Strhodes#define TOSTRUCTCLASS "rdata->rdclass"
71135446Strhodes#define TOSTRUCTTYPE "rdata->type"
72135446Strhodes#define TOSTRUCTDEF "use_default = ISC_TRUE"
73135446Strhodes
74135446Strhodes#define FREESTRUCTARGS "source"
75135446Strhodes#define FREESTRUCTCLASS "common->rdclass"
76135446Strhodes#define FREESTRUCTTYPE "common->rdtype"
77135446Strhodes#define FREESTRUCTDEF NULL
78135446Strhodes
79135446Strhodes#define COMPAREARGS "rdata1, rdata2"
80135446Strhodes#define COMPARECLASS "rdata1->rdclass"
81135446Strhodes#define COMPARETYPE "rdata1->type"
82135446Strhodes#define COMPAREDEF "use_default = ISC_TRUE"
83135446Strhodes
84135446Strhodes#define ADDITIONALDATAARGS "rdata, add, arg"
85135446Strhodes#define ADDITIONALDATACLASS "rdata->rdclass"
86135446Strhodes#define ADDITIONALDATATYPE "rdata->type"
87135446Strhodes#define ADDITIONALDATADEF "use_default = ISC_TRUE"
88135446Strhodes
89135446Strhodes#define DIGESTARGS "rdata, digest, arg"
90135446Strhodes#define DIGESTCLASS "rdata->rdclass"
91135446Strhodes#define DIGESTTYPE "rdata->type"
92135446Strhodes#define DIGESTDEF "use_default = ISC_TRUE"
93135446Strhodes
94135446Strhodes#define CHECKOWNERARGS "name, rdclass, type, wildcard"
95135446Strhodes#define CHECKOWNERCLASS "rdclass"
96135446Strhodes#define CHECKOWNERTYPE "type"
97135446Strhodes#define CHECKOWNERDEF "result = ISC_TRUE"
98135446Strhodes
99135446Strhodes#define CHECKNAMESARGS "rdata, owner, bad"
100135446Strhodes#define CHECKNAMESCLASS "rdata->rdclass"
101135446Strhodes#define CHECKNAMESTYPE "rdata->type"
102135446Strhodes#define CHECKNAMESDEF "result = ISC_TRUE"
103135446Strhodes
104135446Strhodesconst char copyright[] =
105135446Strhodes"/*\n"
106135446Strhodes" * Copyright (C) 2004%s Internet Systems Consortium, Inc. (\"ISC\")\n"
107135446Strhodes" * Copyright (C) 1998-2003 Internet Software Consortium.\n"
108135446Strhodes" *\n"
109135446Strhodes" * Permission to use, copy, modify, and distribute this software for any\n"
110135446Strhodes" * purpose with or without fee is hereby granted, provided that the above\n"
111135446Strhodes" * copyright notice and this permission notice appear in all copies.\n"
112135446Strhodes" *\n"
113135446Strhodes" * THE SOFTWARE IS PROVIDED \"AS IS\" AND ISC DISCLAIMS ALL WARRANTIES WITH\n"
114135446Strhodes" * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n"
115135446Strhodes" * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,\n"
116135446Strhodes" * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n"
117135446Strhodes" * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE\n"
118135446Strhodes" * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n"
119135446Strhodes" * PERFORMANCE OF THIS SOFTWARE.\n"
120135446Strhodes" */\n"
121135446Strhodes"\n"
122135446Strhodes"/***************\n"
123135446Strhodes" ***************\n"
124135446Strhodes" ***************   THIS FILE IS AUTOMATICALLY GENERATED BY gen.c.\n"
125135446Strhodes" ***************   DO NOT EDIT!\n"
126135446Strhodes" ***************\n"
127135446Strhodes" ***************/\n"
128170222Sdougb"\n"
129170222Sdougb"/*! \\file */\n"
130135446Strhodes"\n";
131135446Strhodes
132135446Strhodes#define TYPENAMES 256
133135446Strhodes
134135446Strhodesstruct cc {
135135446Strhodes	struct cc *next;
136135446Strhodes	int rdclass;
137135446Strhodes	char classname[11];
138135446Strhodes} *classes;
139135446Strhodes
140135446Strhodesstruct tt {
141135446Strhodes	struct tt *next;
142135446Strhodes	int rdclass;
143135446Strhodes	int type;
144135446Strhodes	char classname[11];
145135446Strhodes	char typename[11];
146135446Strhodes	char dirname[256];		/* XXX Should be max path length */
147135446Strhodes} *types;
148135446Strhodes
149135446Strhodesstruct ttnam {
150135446Strhodes	char typename[11];
151135446Strhodes	char macroname[11];
152135446Strhodes	char attr[256];
153135446Strhodes	unsigned int sorted;
154135446Strhodes	int type;
155135446Strhodes} typenames[TYPENAMES];
156135446Strhodes
157135446Strhodesint maxtype = -1;
158135446Strhodes
159135446Strhodeschar *
160135446Strhodesupper(char *);
161135446Strhodeschar *
162135446Strhodesfunname(const char *, char *);
163135446Strhodesvoid
164135446Strhodesdoswitch(const char *, const char *, const char *, const char *,
165135446Strhodes	 const char *, const char *);
166135446Strhodesvoid
167135446Strhodesdodecl(char *, char *, char *);
168135446Strhodesvoid
169135446Strhodesadd(int, const char *, int, const char *, const char *);
170135446Strhodesvoid
171135446Strhodessd(int, const char *, const char *, char);
172135446Strhodesvoid
173135446Strhodesinsert_into_typenames(int, const char *, const char *);
174135446Strhodes
175170222Sdougb/*%
176135446Strhodes * If you use more than 10 of these in, say, a printf(), you'll have problems.
177135446Strhodes */
178135446Strhodeschar *
179135446Strhodesupper(char *s) {
180135446Strhodes	static int buf_to_use = 0;
181135446Strhodes	static char buf[10][256];
182135446Strhodes	char *b;
183135446Strhodes	int c;
184135446Strhodes
185135446Strhodes	buf_to_use++;
186135446Strhodes	if (buf_to_use > 9)
187135446Strhodes		buf_to_use = 0;
188135446Strhodes
189135446Strhodes	b = buf[buf_to_use];
190135446Strhodes	memset(b, 0, 256);
191135446Strhodes
192135446Strhodes	while ((c = (*s++) & 0xff))
193135446Strhodes		*b++ = islower(c) ? toupper(c) : c;
194135446Strhodes	*b = '\0';
195135446Strhodes	return (buf[buf_to_use]);
196135446Strhodes}
197135446Strhodes
198135446Strhodeschar *
199135446Strhodesfunname(const char *s, char *buf) {
200135446Strhodes	char *b = buf;
201135446Strhodes	char c;
202135446Strhodes
203135446Strhodes	while ((c = *s++)) {
204135446Strhodes		*b++ = (c == '-') ? '_' : c;
205135446Strhodes	}
206135446Strhodes	*b = '\0';
207135446Strhodes	return (buf);
208135446Strhodes}
209135446Strhodes
210135446Strhodesvoid
211135446Strhodesdoswitch(const char *name, const char *function, const char *args,
212135446Strhodes	 const char *tsw, const char *csw, const char *res)
213135446Strhodes{
214135446Strhodes	struct tt *tt;
215135446Strhodes	int first = 1;
216135446Strhodes	int lasttype = 0;
217135446Strhodes	int subswitch = 0;
218135446Strhodes	char buf1[11], buf2[11];
219135446Strhodes	const char *result = " result =";
220135446Strhodes
221135446Strhodes	if (res == NULL)
222135446Strhodes		result = "";
223135446Strhodes
224135446Strhodes	for (tt = types; tt != NULL; tt = tt->next) {
225135446Strhodes		if (first) {
226135446Strhodes			fprintf(stdout, "\n#define %s \\\n", name);
227135446Strhodes			fprintf(stdout, "\tswitch (%s) { \\\n" /*}*/, tsw);
228135446Strhodes			first = 0;
229135446Strhodes		}
230135446Strhodes		if (tt->type != lasttype && subswitch) {
231135446Strhodes			if (res == NULL)
232135446Strhodes				fprintf(stdout, "\t\tdefault: break; \\\n");
233135446Strhodes			else
234135446Strhodes				fprintf(stdout,
235135446Strhodes					"\t\tdefault: %s; break; \\\n", res);
236135446Strhodes			fputs(/*{*/ "\t\t} \\\n", stdout);
237135446Strhodes			fputs("\t\tbreak; \\\n", stdout);
238135446Strhodes			subswitch = 0;
239135446Strhodes		}
240135446Strhodes		if (tt->rdclass && tt->type != lasttype) {
241135446Strhodes			fprintf(stdout, "\tcase %d: switch (%s) { \\\n" /*}*/,
242135446Strhodes				tt->type, csw);
243135446Strhodes			subswitch = 1;
244135446Strhodes		}
245135446Strhodes		if (tt->rdclass == 0)
246135446Strhodes			fprintf(stdout,
247135446Strhodes				"\tcase %d:%s %s_%s(%s); break;",
248135446Strhodes				tt->type, result, function,
249135446Strhodes				funname(tt->typename, buf1), args);
250135446Strhodes		else
251135446Strhodes			fprintf(stdout,
252135446Strhodes				"\t\tcase %d:%s %s_%s_%s(%s); break;",
253135446Strhodes				tt->rdclass, result, function,
254135446Strhodes				funname(tt->classname, buf1),
255135446Strhodes				funname(tt->typename, buf2), args);
256135446Strhodes		fputs(" \\\n", stdout);
257135446Strhodes		lasttype = tt->type;
258135446Strhodes	}
259135446Strhodes	if (subswitch) {
260135446Strhodes		if (res == NULL)
261135446Strhodes			fprintf(stdout, "\t\tdefault: break; \\\n");
262135446Strhodes		else
263135446Strhodes			fprintf(stdout, "\t\tdefault: %s; break; \\\n", res);
264135446Strhodes		fputs(/*{*/ "\t\t} \\\n", stdout);
265135446Strhodes		fputs("\t\tbreak; \\\n", stdout);
266135446Strhodes	}
267135446Strhodes	if (first) {
268135446Strhodes		if (res == NULL)
269135446Strhodes			fprintf(stdout, "\n#define %s\n", name);
270135446Strhodes		else
271135446Strhodes			fprintf(stdout, "\n#define %s %s;\n", name, res);
272135446Strhodes	} else {
273135446Strhodes		if (res == NULL)
274135446Strhodes			fprintf(stdout, "\tdefault: break; \\\n");
275135446Strhodes		else
276135446Strhodes			fprintf(stdout, "\tdefault: %s; break; \\\n", res);
277135446Strhodes		fputs(/*{*/ "\t}\n", stdout);
278135446Strhodes	}
279135446Strhodes}
280135446Strhodes
281135446Strhodesvoid
282135446Strhodesdodecl(char *type, char *function, char *args) {
283135446Strhodes	struct tt *tt;
284135446Strhodes	char buf1[11], buf2[11];
285135446Strhodes
286135446Strhodes	fputs("\n", stdout);
287135446Strhodes	for (tt = types; tt; tt = tt->next)
288135446Strhodes		if (tt->rdclass)
289135446Strhodes			fprintf(stdout,
290135446Strhodes				"static inline %s %s_%s_%s(%s);\n",
291135446Strhodes				type, function,
292135446Strhodes				funname(tt->classname, buf1),
293135446Strhodes				funname(tt->typename, buf2), args);
294135446Strhodes		else
295135446Strhodes			fprintf(stdout,
296135446Strhodes				"static inline %s %s_%s(%s);\n",
297135446Strhodes				type, function,
298135446Strhodes				funname(tt->typename, buf1), args);
299135446Strhodes}
300135446Strhodes
301135446Strhodesstatic struct ttnam *
302135446Strhodesfind_typename(int type) {
303135446Strhodes	int i;
304135446Strhodes
305135446Strhodes	for (i = 0; i < TYPENAMES; i++) {
306135446Strhodes		if (typenames[i].typename[0] != 0 &&
307135446Strhodes		    typenames[i].type == type)
308135446Strhodes			return (&typenames[i]);
309135446Strhodes	}
310135446Strhodes	return (NULL);
311135446Strhodes}
312135446Strhodes
313135446Strhodesvoid
314135446Strhodesinsert_into_typenames(int type, const char *typename, const char *attr) {
315135446Strhodes	struct ttnam *ttn = NULL;
316135446Strhodes	int c, i;
317135446Strhodes	char tmp[256];
318135446Strhodes
319135446Strhodes	for (i = 0; i < TYPENAMES; i++) {
320135446Strhodes		if (typenames[i].typename[0] != 0 &&
321135446Strhodes		    typenames[i].type == type &&
322135446Strhodes		    strcmp(typename, typenames[i].typename) != 0) {
323135446Strhodes			fprintf(stderr,
324135446Strhodes				"Error:  type %d has two names: %s, %s\n",
325135446Strhodes				type, typenames[i].typename, typename);
326135446Strhodes			exit(1);
327135446Strhodes		}
328135446Strhodes		if (typenames[i].typename[0] == 0 && ttn == NULL)
329135446Strhodes			ttn = &typenames[i];
330135446Strhodes	}
331135446Strhodes	if (ttn == NULL) {
332135446Strhodes		fprintf(stderr, "Error: typenames array too small\n");
333135446Strhodes		exit(1);
334135446Strhodes	}
335135446Strhodes
336135446Strhodes	if (strlen(typename) > sizeof(ttn->typename) - 1) {
337135446Strhodes		fprintf(stderr, "Error:  type name %s is too long\n",
338135446Strhodes			typename);
339135446Strhodes		exit(1);
340135446Strhodes	}
341135446Strhodes	strcpy(ttn->typename, typename);
342135446Strhodes	ttn->type = type;
343135446Strhodes
344135446Strhodes	strcpy(ttn->macroname, ttn->typename);
345135446Strhodes	c = strlen(ttn->macroname);
346135446Strhodes	while (c > 0) {
347135446Strhodes		if (ttn->macroname[c - 1] == '-')
348135446Strhodes			ttn->macroname[c - 1] = '_';
349135446Strhodes		c--;
350135446Strhodes	}
351135446Strhodes
352135446Strhodes	if (attr == NULL) {
353135446Strhodes		sprintf(tmp, "RRTYPE_%s_ATTRIBUTES", upper(ttn->macroname));
354135446Strhodes		attr = tmp;
355135446Strhodes	}
356135446Strhodes
357135446Strhodes	if (ttn->attr[0] != 0 && strcmp(attr, ttn->attr) != 0) {
358135446Strhodes		fprintf(stderr, "Error:  type %d has different attributes: "
359135446Strhodes			"%s, %s\n", type, ttn->attr, attr);
360135446Strhodes		exit(1);
361135446Strhodes	}
362135446Strhodes
363135446Strhodes	if (strlen(attr) > sizeof(ttn->attr) - 1) {
364135446Strhodes		fprintf(stderr, "Error:  attr (%s) [name %s] is too long\n",
365135446Strhodes			attr, typename);
366135446Strhodes		exit(1);
367135446Strhodes	}
368135446Strhodes	strcpy(ttn->attr, attr);
369135446Strhodes	ttn->sorted = 0;
370135446Strhodes	if (maxtype < type)
371135446Strhodes		maxtype = type;
372135446Strhodes}
373135446Strhodes
374135446Strhodesvoid
375135446Strhodesadd(int rdclass, const char *classname, int type, const char *typename,
376135446Strhodes    const char *dirname)
377135446Strhodes{
378135446Strhodes	struct tt *newtt = (struct tt *)malloc(sizeof(*newtt));
379135446Strhodes	struct tt *tt, *oldtt;
380135446Strhodes	struct cc *newcc;
381135446Strhodes	struct cc *cc, *oldcc;
382135446Strhodes
383135446Strhodes	insert_into_typenames(type, typename, NULL);
384135446Strhodes
385135446Strhodes	if (newtt == NULL) {
386135446Strhodes		fprintf(stderr, "malloc() failed\n");
387135446Strhodes		exit(1);
388135446Strhodes	}
389135446Strhodes
390135446Strhodes	newtt->next = NULL;
391135446Strhodes	newtt->rdclass = rdclass;
392135446Strhodes	newtt->type = type;
393135446Strhodes	strcpy(newtt->classname, classname);
394135446Strhodes	strcpy(newtt->typename, typename);
395135446Strhodes	strcpy(newtt->dirname, dirname);
396135446Strhodes
397135446Strhodes	tt = types;
398135446Strhodes	oldtt = NULL;
399135446Strhodes
400135446Strhodes	while ((tt != NULL) && (tt->type < type)) {
401135446Strhodes		oldtt = tt;
402135446Strhodes		tt = tt->next;
403135446Strhodes	}
404135446Strhodes
405135446Strhodes	while ((tt != NULL) && (tt->type == type) && (tt->rdclass < rdclass)) {
406135446Strhodes		if (strcmp(tt->typename, typename) != 0)
407135446Strhodes			exit(1);
408135446Strhodes		oldtt = tt;
409135446Strhodes		tt = tt->next;
410135446Strhodes	}
411135446Strhodes
412135446Strhodes	if ((tt != NULL) && (tt->type == type) && (tt->rdclass == rdclass))
413135446Strhodes		exit(1);
414135446Strhodes
415135446Strhodes	newtt->next = tt;
416135446Strhodes	if (oldtt != NULL)
417135446Strhodes		oldtt->next = newtt;
418135446Strhodes	else
419135446Strhodes		types = newtt;
420135446Strhodes
421135446Strhodes	/*
422135446Strhodes	 * Do a class switch for this type.
423135446Strhodes	 */
424135446Strhodes	if (rdclass == 0)
425135446Strhodes		return;
426135446Strhodes
427135446Strhodes	newcc = (struct cc *)malloc(sizeof(*newcc));
428135446Strhodes	newcc->rdclass = rdclass;
429135446Strhodes	strcpy(newcc->classname, classname);
430135446Strhodes	cc = classes;
431135446Strhodes	oldcc = NULL;
432135446Strhodes
433135446Strhodes	while ((cc != NULL) && (cc->rdclass < rdclass)) {
434135446Strhodes		oldcc = cc;
435135446Strhodes		cc = cc->next;
436135446Strhodes	}
437135446Strhodes
438135446Strhodes	if ((cc != NULL) && cc->rdclass == rdclass) {
439135446Strhodes		free((char *)newcc);
440135446Strhodes		return;
441135446Strhodes	}
442135446Strhodes
443135446Strhodes	newcc->next = cc;
444135446Strhodes	if (oldcc != NULL)
445135446Strhodes		oldcc->next = newcc;
446135446Strhodes	else
447135446Strhodes		classes = newcc;
448135446Strhodes}
449135446Strhodes
450135446Strhodesvoid
451135446Strhodessd(int rdclass, const char *classname, const char *dirname, char filetype) {
452135446Strhodes	char buf[sizeof("0123456789_65535.h")];
453135446Strhodes	char fmt[sizeof("%10[-0-9a-z]_%d.h")];
454135446Strhodes	int type;
455135446Strhodes	char typename[11];
456135446Strhodes	isc_dir_t dir;
457135446Strhodes
458135446Strhodes	if (!start_directory(dirname, &dir))
459135446Strhodes		return;
460135446Strhodes
461135446Strhodes	sprintf(fmt,"%s%c", "%10[-0-9a-z]_%d.", filetype);
462135446Strhodes	while (next_file(&dir)) {
463135446Strhodes		if (sscanf(dir.filename, fmt, typename, &type) != 2)
464135446Strhodes			continue;
465135446Strhodes		if ((type > 65535) || (type < 0))
466135446Strhodes			continue;
467135446Strhodes
468135446Strhodes		sprintf(buf, "%s_%d.%c", typename, type, filetype);
469135446Strhodes		if (strcmp(buf, dir.filename) != 0)
470135446Strhodes			continue;
471135446Strhodes		add(rdclass, classname, type, typename, dirname);
472135446Strhodes	}
473135446Strhodes
474135446Strhodes	end_directory(&dir);
475135446Strhodes}
476135446Strhodes
477135446Strhodesstatic unsigned int
478135446StrhodesHASH(char *string) {
479135446Strhodes	unsigned int n;
480135446Strhodes	unsigned char a, b;
481135446Strhodes
482135446Strhodes	n = strlen(string);
483135446Strhodes	if (n == 0) {
484135446Strhodes		fprintf(stderr, "n == 0?\n");
485135446Strhodes		exit(1);
486135446Strhodes	}
487135446Strhodes	a = tolower((unsigned char)string[0]);
488135446Strhodes	b = tolower((unsigned char)string[n - 1]);
489135446Strhodes
490135446Strhodes	return ((a + n) * b) % 256;
491135446Strhodes}
492135446Strhodes
493135446Strhodesint
494135446Strhodesmain(int argc, char **argv) {
495135446Strhodes	char buf[256];			/* XXX Should be max path length */
496135446Strhodes	char srcdir[256];		/* XXX Should be max path length */
497135446Strhodes	int rdclass;
498135446Strhodes	char classname[11];
499135446Strhodes	struct tt *tt;
500135446Strhodes	struct cc *cc;
501135446Strhodes	struct ttnam *ttn, *ttn2;
502135446Strhodes	unsigned int hash;
503135446Strhodes	struct tm *tm;
504135446Strhodes	time_t now;
505135446Strhodes	char year[11];
506135446Strhodes	int lasttype;
507135446Strhodes	int code = 1;
508135446Strhodes	int class_enum = 0;
509135446Strhodes	int type_enum = 0;
510135446Strhodes	int structs = 0;
511135446Strhodes	int depend = 0;
512135446Strhodes	int c, i, j;
513135446Strhodes	char buf1[11];
514135446Strhodes	char filetype = 'c';
515135446Strhodes	FILE *fd;
516135446Strhodes	char *prefix = NULL;
517135446Strhodes	char *suffix = NULL;
518135446Strhodes	char *file = NULL;
519135446Strhodes	isc_dir_t dir;
520135446Strhodes
521135446Strhodes	for (i = 0; i < TYPENAMES; i++)
522135446Strhodes		memset(&typenames[i], 0, sizeof(typenames[i]));
523135446Strhodes
524135446Strhodes	strcpy(srcdir, "");
525135446Strhodes	while ((c = isc_commandline_parse(argc, argv, "cdits:F:P:S:")) != -1)
526135446Strhodes		switch (c) {
527135446Strhodes		case 'c':
528135446Strhodes			code = 0;
529135446Strhodes			depend = 0;
530135446Strhodes			type_enum = 0;
531135446Strhodes			class_enum = 1;
532135446Strhodes			filetype = 'c';
533135446Strhodes			structs = 0;
534135446Strhodes			break;
535135446Strhodes		case 'd':
536135446Strhodes			code = 0;
537135446Strhodes			depend = 1;
538135446Strhodes			class_enum = 0;
539135446Strhodes			type_enum = 0;
540135446Strhodes			structs = 0;
541135446Strhodes			filetype = 'h';
542135446Strhodes			break;
543135446Strhodes		case 't':
544135446Strhodes			code = 0;
545135446Strhodes			depend = 0;
546135446Strhodes			class_enum = 0;
547135446Strhodes			type_enum = 1;
548135446Strhodes			filetype = 'c';
549135446Strhodes			structs = 0;
550135446Strhodes			break;
551135446Strhodes		case 'i':
552135446Strhodes			code = 0;
553135446Strhodes			depend = 0;
554135446Strhodes			class_enum = 0;
555135446Strhodes			type_enum = 0;
556135446Strhodes			structs = 1;
557135446Strhodes			filetype = 'h';
558135446Strhodes			break;
559135446Strhodes		case 's':
560135446Strhodes			sprintf(srcdir, "%s/", isc_commandline_argument);
561135446Strhodes			break;
562135446Strhodes		case 'F':
563135446Strhodes			file = isc_commandline_argument;
564135446Strhodes			break;
565135446Strhodes		case 'P':
566135446Strhodes			prefix = isc_commandline_argument;
567135446Strhodes			break;
568135446Strhodes		case 'S':
569135446Strhodes			suffix = isc_commandline_argument;
570135446Strhodes			break;
571135446Strhodes		case '?':
572135446Strhodes			exit(1);
573135446Strhodes		}
574135446Strhodes
575135446Strhodes	sprintf(buf, "%srdata", srcdir);
576135446Strhodes
577135446Strhodes	if (!start_directory(buf, &dir))
578135446Strhodes		exit(1);
579135446Strhodes
580135446Strhodes	while (next_file(&dir)) {
581135446Strhodes		if (sscanf(dir.filename, "%10[0-9a-z]_%d",
582135446Strhodes			   classname, &rdclass) != 2)
583135446Strhodes			continue;
584135446Strhodes		if ((rdclass > 65535) || (rdclass < 0))
585135446Strhodes			continue;
586135446Strhodes
587135446Strhodes		sprintf(buf, "%srdata/%s_%d", srcdir, classname, rdclass);
588135446Strhodes		if (strcmp(buf + 6 + strlen(srcdir), dir.filename) != 0)
589135446Strhodes			continue;
590135446Strhodes		sd(rdclass, classname, buf, filetype);
591135446Strhodes	}
592135446Strhodes	end_directory(&dir);
593135446Strhodes	sprintf(buf, "%srdata/generic", srcdir);
594135446Strhodes	sd(0, "", buf, filetype);
595135446Strhodes
596135446Strhodes	if (time(&now) != -1) {
597135446Strhodes		if ((tm = localtime(&now)) != NULL && tm->tm_year > 104)
598135446Strhodes			sprintf(year, "-%d", tm->tm_year + 1900);
599135446Strhodes		else
600135446Strhodes			year[0] = 0;
601135446Strhodes	} else
602135446Strhodes		year[0] = 0;
603135446Strhodes
604135446Strhodes	if (!depend) fprintf(stdout, copyright, year);
605135446Strhodes
606135446Strhodes	if (code) {
607135446Strhodes		fputs("#ifndef DNS_CODE_H\n", stdout);
608135446Strhodes		fputs("#define DNS_CODE_H 1\n\n", stdout);
609135446Strhodes
610135446Strhodes		fputs("#include <isc/boolean.h>\n", stdout);
611135446Strhodes		fputs("#include <isc/result.h>\n\n", stdout);
612135446Strhodes		fputs("#include <dns/name.h>\n\n", stdout);
613135446Strhodes
614135446Strhodes		for (tt = types; tt != NULL; tt = tt->next)
615135446Strhodes			fprintf(stdout, "#include \"%s/%s_%d.c\"\n",
616135446Strhodes				tt->dirname, tt->typename, tt->type);
617135446Strhodes
618135446Strhodes		fputs("\n\n", stdout);
619135446Strhodes
620135446Strhodes		doswitch("FROMTEXTSWITCH", "fromtext", FROMTEXTARGS,
621135446Strhodes			 FROMTEXTTYPE, FROMTEXTCLASS, FROMTEXTDEF);
622135446Strhodes		doswitch("TOTEXTSWITCH", "totext", TOTEXTARGS,
623135446Strhodes			 TOTEXTTYPE, TOTEXTCLASS, TOTEXTDEF);
624135446Strhodes		doswitch("FROMWIRESWITCH", "fromwire", FROMWIREARGS,
625135446Strhodes			 FROMWIRETYPE, FROMWIRECLASS, FROMWIREDEF);
626135446Strhodes		doswitch("TOWIRESWITCH", "towire", TOWIREARGS,
627135446Strhodes			 TOWIRETYPE, TOWIRECLASS, TOWIREDEF);
628135446Strhodes		doswitch("COMPARESWITCH", "compare", COMPAREARGS,
629135446Strhodes			  COMPARETYPE, COMPARECLASS, COMPAREDEF);
630135446Strhodes		doswitch("FROMSTRUCTSWITCH", "fromstruct", FROMSTRUCTARGS,
631135446Strhodes			  FROMSTRUCTTYPE, FROMSTRUCTCLASS, FROMSTRUCTDEF);
632135446Strhodes		doswitch("TOSTRUCTSWITCH", "tostruct", TOSTRUCTARGS,
633135446Strhodes			  TOSTRUCTTYPE, TOSTRUCTCLASS, TOSTRUCTDEF);
634135446Strhodes		doswitch("FREESTRUCTSWITCH", "freestruct", FREESTRUCTARGS,
635135446Strhodes			  FREESTRUCTTYPE, FREESTRUCTCLASS, FREESTRUCTDEF);
636135446Strhodes		doswitch("ADDITIONALDATASWITCH", "additionaldata",
637135446Strhodes			 ADDITIONALDATAARGS, ADDITIONALDATATYPE,
638135446Strhodes			 ADDITIONALDATACLASS, ADDITIONALDATADEF);
639135446Strhodes		doswitch("DIGESTSWITCH", "digest",
640135446Strhodes			 DIGESTARGS, DIGESTTYPE,
641135446Strhodes			 DIGESTCLASS, DIGESTDEF);
642135446Strhodes		doswitch("CHECKOWNERSWITCH", "checkowner",
643135446Strhodes			CHECKOWNERARGS, CHECKOWNERTYPE,
644135446Strhodes			CHECKOWNERCLASS, CHECKOWNERDEF);
645135446Strhodes		doswitch("CHECKNAMESSWITCH", "checknames",
646135446Strhodes			CHECKNAMESARGS, CHECKNAMESTYPE,
647135446Strhodes			CHECKNAMESCLASS, CHECKNAMESDEF);
648135446Strhodes
649135446Strhodes		/*
650135446Strhodes		 * From here down, we are processing the rdata names and
651135446Strhodes		 * attributes.
652135446Strhodes		 */
653135446Strhodes
654135446Strhodes#define PRINT_COMMA(x) (x == maxtype ? "" : ",")
655135446Strhodes
656135446Strhodes#define METANOTQUESTION  "DNS_RDATATYPEATTR_META | " \
657135446Strhodes			 "DNS_RDATATYPEATTR_NOTQUESTION"
658135446Strhodes#define METAQUESTIONONLY "DNS_RDATATYPEATTR_META | " \
659135446Strhodes			 "DNS_RDATATYPEATTR_QUESTIONONLY"
660135446Strhodes#define RESERVED "DNS_RDATATYPEATTR_RESERVED"
661135446Strhodes
662135446Strhodes		/*
663135446Strhodes		 * Add in reserved/special types.  This will let us
664135446Strhodes		 * sort them without special cases.
665135446Strhodes		 */
666135446Strhodes		insert_into_typenames(0, "reserved0", RESERVED);
667135446Strhodes		insert_into_typenames(31, "eid", RESERVED);
668135446Strhodes		insert_into_typenames(32, "nimloc", RESERVED);
669135446Strhodes		insert_into_typenames(34, "atma", RESERVED);
670135446Strhodes		insert_into_typenames(100, "uinfo", RESERVED);
671135446Strhodes		insert_into_typenames(101, "uid", RESERVED);
672135446Strhodes		insert_into_typenames(102, "gid", RESERVED);
673135446Strhodes		insert_into_typenames(251, "ixfr", METAQUESTIONONLY);
674135446Strhodes		insert_into_typenames(252, "axfr", METAQUESTIONONLY);
675135446Strhodes		insert_into_typenames(253, "mailb", METAQUESTIONONLY);
676135446Strhodes		insert_into_typenames(254, "maila", METAQUESTIONONLY);
677135446Strhodes		insert_into_typenames(255, "any", METAQUESTIONONLY);
678135446Strhodes
679135446Strhodes		/*
680135446Strhodes		 * Spit out a quick and dirty hash function.  Here,
681135446Strhodes		 * we walk through the list of type names, and calculate
682135446Strhodes		 * a hash.  This isn't perfect, but it will generate "pretty
683135446Strhodes		 * good" estimates.  Lowercase the characters before
684135446Strhodes		 * computing in all cases.
685135446Strhodes		 *
686135446Strhodes		 * Here, walk the list from top to bottom, calculating
687135446Strhodes		 * the hash (mod 256) for each name.
688135446Strhodes		 */
689135446Strhodes		fprintf(stdout, "#define RDATATYPE_COMPARE(_s, _d, _tn, _n, _tp) \\\n");
690135446Strhodes		fprintf(stdout, "\tdo { \\\n");
691135446Strhodes		fprintf(stdout, "\t\tif (sizeof(_s) - 1 == _n && \\\n"
692135446Strhodes				"\t\t    strncasecmp(_s,(_tn),"
693135446Strhodes				"(sizeof(_s) - 1)) == 0) { \\\n");
694135446Strhodes		fprintf(stdout, "\t\t\tif ((dns_rdatatype_attributes(_d) & "
695135446Strhodes		       		  "DNS_RDATATYPEATTR_RESERVED) != 0) \\\n");
696135446Strhodes		fprintf(stdout, "\t\t\t\treturn (ISC_R_NOTIMPLEMENTED); \\\n");
697135446Strhodes		fprintf(stdout, "\t\t\t*(_tp) = _d; \\\n");
698135446Strhodes		fprintf(stdout, "\t\t\treturn (ISC_R_SUCCESS); \\\n");
699135446Strhodes		fprintf(stdout, "\t\t} \\\n");
700135446Strhodes		fprintf(stdout, "\t} while (0)\n\n");
701135446Strhodes
702135446Strhodes		fprintf(stdout, "#define RDATATYPE_FROMTEXT_SW(_hash,"
703135446Strhodes				"_typename,_length,_typep) \\\n");
704135446Strhodes		fprintf(stdout, "\tswitch (_hash) { \\\n");
705135446Strhodes		for (i = 0; i <= maxtype; i++) {
706135446Strhodes			ttn = find_typename(i);
707135446Strhodes			if (ttn == NULL)
708135446Strhodes				continue;
709135446Strhodes
710135446Strhodes			/*
711135446Strhodes			 * Skip entries we already processed.
712135446Strhodes			 */
713135446Strhodes			if (ttn->sorted != 0)
714135446Strhodes				continue;
715135446Strhodes
716135446Strhodes			hash = HASH(ttn->typename);
717135446Strhodes			fprintf(stdout, "\t\tcase %u: \\\n", hash);
718135446Strhodes
719135446Strhodes			/*
720135446Strhodes			 * Find all other entries that happen to match
721135446Strhodes			 * this hash.
722135446Strhodes			 */
723135446Strhodes			for (j = 0; j <= maxtype; j++) {
724135446Strhodes				ttn2 = find_typename(j);
725135446Strhodes				if (ttn2 == NULL)
726135446Strhodes					continue;
727135446Strhodes				if (hash == HASH(ttn2->typename)) {
728135446Strhodes					fprintf(stdout, "\t\t\tRDATATYPE_COMPARE"
729135446Strhodes					       "(\"%s\", %u, "
730135446Strhodes					       "_typename, _length, _typep); \\\n",
731135446Strhodes					       ttn2->typename, ttn2->type);
732135446Strhodes					ttn2->sorted = 1;
733135446Strhodes				}
734135446Strhodes			}
735135446Strhodes			fprintf(stdout, "\t\t\tbreak; \\\n");
736135446Strhodes		}
737135446Strhodes		fprintf(stdout, "\t}\n");
738135446Strhodes
739135446Strhodes		fprintf(stdout, "#define RDATATYPE_ATTRIBUTE_SW \\\n");
740135446Strhodes		fprintf(stdout, "\tswitch (type) { \\\n");
741135446Strhodes		for (i = 0; i <= maxtype; i++) {
742135446Strhodes			ttn = find_typename(i);
743135446Strhodes			if (ttn == NULL)
744135446Strhodes				continue;
745135446Strhodes			fprintf(stdout, "\tcase %u: return (%s); \\\n",
746135446Strhodes			        i, upper(ttn->attr));
747135446Strhodes		}
748135446Strhodes		fprintf(stdout, "\t}\n");
749135446Strhodes
750135446Strhodes		fprintf(stdout, "#define RDATATYPE_TOTEXT_SW \\\n");
751135446Strhodes		fprintf(stdout, "\tswitch (type) { \\\n");
752135446Strhodes		for (i = 0; i <= maxtype; i++) {
753135446Strhodes			ttn = find_typename(i);
754135446Strhodes			if (ttn == NULL)
755135446Strhodes				continue;
756135446Strhodes			fprintf(stdout, "\tcase %u: return "
757135446Strhodes				"(str_totext(\"%s\", target)); \\\n",
758135446Strhodes			        i, upper(ttn->typename));
759135446Strhodes		}
760135446Strhodes		fprintf(stdout, "\t}\n");
761135446Strhodes
762135446Strhodes		fputs("#endif /* DNS_CODE_H */\n", stdout);
763135446Strhodes	} else if (type_enum) {
764135446Strhodes		char *s;
765135446Strhodes
766135446Strhodes		fprintf(stdout, "#ifndef DNS_ENUMTYPE_H\n");
767135446Strhodes		fprintf(stdout, "#define DNS_ENUMTYPE_H 1\n\n");
768135446Strhodes
769135446Strhodes		fprintf(stdout, "enum {\n");
770135446Strhodes		fprintf(stdout, "\tdns_rdatatype_none = 0,\n");
771135446Strhodes
772135446Strhodes		lasttype = 0;
773135446Strhodes		for (tt = types; tt != NULL; tt = tt->next)
774135446Strhodes			if (tt->type != lasttype)
775135446Strhodes				fprintf(stdout,
776135446Strhodes					"\tdns_rdatatype_%s = %d,\n",
777135446Strhodes					funname(tt->typename, buf1),
778135446Strhodes					lasttype = tt->type);
779135446Strhodes
780135446Strhodes		fprintf(stdout, "\tdns_rdatatype_ixfr = 251,\n");
781135446Strhodes		fprintf(stdout, "\tdns_rdatatype_axfr = 252,\n");
782135446Strhodes		fprintf(stdout, "\tdns_rdatatype_mailb = 253,\n");
783135446Strhodes		fprintf(stdout, "\tdns_rdatatype_maila = 254,\n");
784135446Strhodes		fprintf(stdout, "\tdns_rdatatype_any = 255\n");
785135446Strhodes
786135446Strhodes		fprintf(stdout, "};\n\n");
787135446Strhodes
788135446Strhodes		fprintf(stdout, "#define dns_rdatatype_none\t"
789135446Strhodes			"((dns_rdatatype_t)dns_rdatatype_none)\n");
790135446Strhodes
791135446Strhodes		for (tt = types; tt != NULL; tt = tt->next)
792135446Strhodes			if (tt->type != lasttype) {
793135446Strhodes				s = funname(tt->typename, buf1);
794135446Strhodes				fprintf(stdout,
795135446Strhodes					"#define dns_rdatatype_%s\t%s"
796135446Strhodes					"((dns_rdatatype_t)dns_rdatatype_%s)"
797135446Strhodes					"\n",
798135446Strhodes					s, strlen(s) < 2U ? "\t" : "", s);
799135446Strhodes				lasttype = tt->type;
800135446Strhodes			}
801135446Strhodes
802135446Strhodes		fprintf(stdout, "#define dns_rdatatype_ixfr\t"
803135446Strhodes			"((dns_rdatatype_t)dns_rdatatype_ixfr)\n");
804135446Strhodes		fprintf(stdout, "#define dns_rdatatype_axfr\t"
805135446Strhodes			"((dns_rdatatype_t)dns_rdatatype_axfr)\n");
806135446Strhodes		fprintf(stdout, "#define dns_rdatatype_mailb\t"
807135446Strhodes			"((dns_rdatatype_t)dns_rdatatype_mailb)\n");
808135446Strhodes		fprintf(stdout, "#define dns_rdatatype_maila\t"
809135446Strhodes			"((dns_rdatatype_t)dns_rdatatype_maila)\n");
810135446Strhodes		fprintf(stdout, "#define dns_rdatatype_any\t"
811135446Strhodes			"((dns_rdatatype_t)dns_rdatatype_any)\n");
812135446Strhodes
813135446Strhodes		fprintf(stdout, "\n#endif /* DNS_ENUMTYPE_H */\n");
814135446Strhodes
815135446Strhodes	} else if (class_enum) {
816135446Strhodes		char *s;
817135446Strhodes		int classnum;
818135446Strhodes
819135446Strhodes		fprintf(stdout, "#ifndef DNS_ENUMCLASS_H\n");
820135446Strhodes		fprintf(stdout, "#define DNS_ENUMCLASS_H 1\n\n");
821135446Strhodes
822135446Strhodes		fprintf(stdout, "enum {\n");
823135446Strhodes
824135446Strhodes		fprintf(stdout, "\tdns_rdataclass_reserved0 = 0,\n");
825135446Strhodes		fprintf(stdout, "#define dns_rdataclass_reserved0 \\\n\t\t\t\t"
826135446Strhodes			"((dns_rdataclass_t)dns_rdataclass_reserved0)\n");
827135446Strhodes
828135446Strhodes#define PRINTCLASS(name, num) \
829135446Strhodes	do { \
830135446Strhodes		s = funname(name, buf1); \
831135446Strhodes		classnum = num; \
832135446Strhodes		fprintf(stdout, "\tdns_rdataclass_%s = %d%s\n", s, classnum, \
833135446Strhodes		       classnum != 255 ? "," : ""); \
834135446Strhodes		fprintf(stdout, "#define dns_rdataclass_%s\t" \
835135446Strhodes		       "((dns_rdataclass_t)dns_rdataclass_%s)\n", s, s); \
836135446Strhodes	} while (0)
837135446Strhodes
838135446Strhodes		for (cc = classes; cc != NULL; cc = cc->next) {
839170222Sdougb			if (cc->rdclass == 3)
840135446Strhodes				PRINTCLASS("chaos", 3);
841170222Sdougb			else if (cc->rdclass == 255)
842135446Strhodes				PRINTCLASS("none", 254);
843135446Strhodes			PRINTCLASS(cc->classname, cc->rdclass);
844135446Strhodes		}
845135446Strhodes
846135446Strhodes#undef PRINTCLASS
847135446Strhodes
848135446Strhodes		fprintf(stdout, "};\n\n");
849135446Strhodes		fprintf(stdout, "#endif /* DNS_ENUMCLASS_H */\n");
850135446Strhodes	} else if (structs) {
851135446Strhodes		if (prefix != NULL) {
852135446Strhodes			if ((fd = fopen(prefix,"r")) != NULL) {
853135446Strhodes				while (fgets(buf, sizeof(buf), fd) != NULL)
854135446Strhodes					fputs(buf, stdout);
855135446Strhodes				fclose(fd);
856135446Strhodes			}
857135446Strhodes		}
858135446Strhodes		for (tt = types; tt != NULL; tt = tt->next) {
859135446Strhodes			sprintf(buf, "%s/%s_%d.h",
860135446Strhodes				tt->dirname, tt->typename, tt->type);
861135446Strhodes			if ((fd = fopen(buf,"r")) != NULL) {
862135446Strhodes				while (fgets(buf, sizeof(buf), fd) != NULL)
863135446Strhodes					fputs(buf, stdout);
864135446Strhodes				fclose(fd);
865135446Strhodes			}
866135446Strhodes		}
867135446Strhodes		if (suffix != NULL) {
868135446Strhodes			if ((fd = fopen(suffix,"r")) != NULL) {
869135446Strhodes				while (fgets(buf, sizeof(buf), fd) != NULL)
870135446Strhodes					fputs(buf, stdout);
871135446Strhodes				fclose(fd);
872135446Strhodes			}
873135446Strhodes		}
874135446Strhodes	} else if (depend) {
875135446Strhodes		for (tt = types; tt != NULL; tt = tt->next)
876135446Strhodes			fprintf(stdout, "%s:\t%s/%s_%d.h\n", file,
877135446Strhodes				tt->dirname, tt->typename, tt->type);
878135446Strhodes	}
879135446Strhodes
880135446Strhodes	if (ferror(stdout) != 0)
881135446Strhodes		exit(1);
882135446Strhodes
883135446Strhodes	return (0);
884135446Strhodes}
885