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