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