gen.c 21.2 KB
Newer Older
1
/*
David Lawrence's avatar
David Lawrence committed
2
 * Copyright (C) 1998-2000  Internet Software Consortium.
3
 *
4
5
6
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
7
 *
8
9
10
11
12
13
14
15
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17
 */

18
/* $Id: gen.c,v 1.58 2000/11/20 20:29:00 bwelling Exp $ */
Bob Halley's avatar
Bob Halley committed
19
20

#include <config.h>
Mark Andrews's avatar
Mark Andrews committed
21

22
23
24
#include <sys/types.h>

#include <ctype.h>
25
#include <stdlib.h>
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

David Lawrence's avatar
David Lawrence committed
31
32
33
34
35
#ifdef WIN32
#include "gen-win32.h"
#else
#include "gen-unix.h"
#endif
36

37
38
#define FROMTEXTARGS "rdclass, type, lexer, origin, downcase, target"
#define FROMTEXTCLASS "rdclass"
39
#define FROMTEXTTYPE "type"
40
#define FROMTEXTDEF "result = DNS_R_UNKNOWN"
41

42
#define TOTEXTARGS "rdata, tctx, target"
43
#define TOTEXTCLASS "rdata->rdclass"
44
#define TOTEXTTYPE "rdata->type"
Mark Andrews's avatar
Mark Andrews committed
45
#define TOTEXTDEF "use_default = ISC_TRUE"
46

47
48
#define FROMWIREARGS "rdclass, type, source, dctx, downcase, target"
#define FROMWIRECLASS "rdclass"
49
#define FROMWIRETYPE "type"
Mark Andrews's avatar
Mark Andrews committed
50
#define FROMWIREDEF "use_default = ISC_TRUE"
51
52

#define TOWIREARGS "rdata, cctx, target"
53
#define TOWIRECLASS "rdata->rdclass"
54
#define TOWIRETYPE "rdata->type"
Mark Andrews's avatar
Mark Andrews committed
55
#define TOWIREDEF "use_default = ISC_TRUE"
56

57
58
#define FROMSTRUCTARGS "rdclass, type, source, target"
#define FROMSTRUCTCLASS "rdclass"
59
#define FROMSTRUCTTYPE "type"
Mark Andrews's avatar
Mark Andrews committed
60
#define FROMSTRUCTDEF "use_default = ISC_TRUE"
61

62
#define TOSTRUCTARGS "rdata, target, mctx"
63
#define TOSTRUCTCLASS "rdata->rdclass"
64
#define TOSTRUCTTYPE "rdata->type"
Mark Andrews's avatar
Mark Andrews committed
65
#define TOSTRUCTDEF "use_default = ISC_TRUE"
66

67
68
69
70
71
#define FREESTRUCTARGS "source"
#define FREESTRUCTCLASS "common->rdclass"
#define FREESTRUCTTYPE "common->rdtype"
#define FREESTRUCTDEF NULL

72
#define COMPAREARGS "rdata1, rdata2"
73
#define COMPARECLASS "rdata1->rdclass"
74
#define COMPARETYPE "rdata1->type"
Mark Andrews's avatar
Mark Andrews committed
75
#define COMPAREDEF "use_default = ISC_TRUE"
76

77
78
79
80
81
#define ADDITIONALDATAARGS "rdata, add, arg"
#define ADDITIONALDATACLASS "rdata->rdclass"
#define ADDITIONALDATATYPE "rdata->type"
#define ADDITIONALDATADEF "use_default = ISC_TRUE"

Bob Halley's avatar
Bob Halley committed
82
83
84
85
86
#define DIGESTARGS "rdata, digest, arg"
#define DIGESTCLASS "rdata->rdclass"
#define DIGESTTYPE "rdata->type"
#define DIGESTDEF "use_default = ISC_TRUE"

87
char copyright[] =
88
89
90
91
92
93
94
"/*\n"
" * Copyright (C) 1998%s Internet Software Consortium.\n"
" *\n"
" * Permission to use, copy, modify, and distribute this software for any\n"
" * purpose with or without fee is hereby granted, provided that the above\n"
" * copyright notice and this permission notice appear in all copies.\n"
" *\n"
95
96
97
98
99
100
101
102
" * THE SOFTWARE IS PROVIDED \"AS IS\" AND INTERNET SOFTWARE CONSORTIUM\n"
" * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL\n"
" * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL\n"
" * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,\n"
" * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING\n"
" * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,\n"
" * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION\n"
" * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n"
103
104
105
106
107
108
109
110
111
" */\n"
"\n"
"/***************\n"
" ***************\n"
" ***************   THIS FILE IS AUTOMATICALLY GENERATED BY gen.c.\n"
" ***************   DO NOT EDIT!\n"
" ***************\n"
" ***************/\n"
"\n";
112
113
114

struct cc {
	struct cc *next;
115
	int rdclass;
116
	char classname[11];
117
118
119
120
} *classes;

struct tt {
	struct tt *next;
121
	int rdclass;
122
123
124
	int type;
	char classname[11];
	char typename[11];
125
	char dirname[256];		/* XXX Should be max path length */
126
127
} *types;

128
129
130
struct ttnam {
	char typename[11];
	char macroname[11];
131
	char attr[256];
132
	unsigned int sorted;
133
134
} typenames[256];

David Lawrence's avatar
David Lawrence committed
135
136
137
char *
upper(char *);
char *
138
funname(const char *, char *);
David Lawrence's avatar
David Lawrence committed
139
140
141
142
143
144
145
146
147
148
149
void
doswitch(const char *, const char *, const char *, const char *,
	 const char *, const char *);
void
dodecl(char *, char *, char *);
void
add(int, const char *, int, const char *, const char *);
void
sd(int, const char *, const char *, char);
void
insert_into_typenames(int, const char *, const char *);
150

151
152
153
/*
 * If you use more than 10 of these in, say, a printf(), you'll have problems.
 */
154
155
char *
upper(char *s) {
156
	static int buf_to_use = 0;
157
	static char buf[10][256];
158
	char *b;
159
	int c;
160

161
162
163
164
165
	buf_to_use++;
	if (buf_to_use > 9)
		buf_to_use = 0;

	b = buf[buf_to_use];
166
	memset(b, 0, 256);
167

168
	while ((c = (*s++) & 0xff))
169
170
		*b++ = islower(c) ? toupper(c) : c;
	*b = '\0';
171
	return (buf[buf_to_use]);
172
173
}

174
char *
175
funname(const char *s, char *buf) {
176
177
178
179
180
181
182
183
184
185
	char *b = buf;
	char c;

	while ((c = *s++)) {
		*b++ = (c == '-') ? '_' : c;
	}
	*b = '\0';
	return (buf);
}

186
void
David Lawrence's avatar
David Lawrence committed
187
188
doswitch(const char *name, const char *function, const char *args,
	 const char *tsw, const char *csw, const char *res)
189
190
191
192
193
{
	struct tt *tt;
	int first = 1;
	int lasttype = 0;
	int subswitch = 0;
194
	char buf1[11], buf2[11];
David Lawrence's avatar
David Lawrence committed
195
	const char *result = " result =";
196
197
198

	if (res == NULL)
		result = "";
199
200
201
202
203
204
205
206

	for (tt = types; tt != NULL ; tt = tt->next) {
		if (first) {
			fprintf(stdout, "\n#define %s \\\n", name);
			fprintf(stdout, "\tswitch (%s) { \\\n" /*}*/, tsw);
			first = 0;
		}
		if (tt->type != lasttype && subswitch) {
207
208
209
210
211
			if (res == NULL)
				fprintf(stdout, "\t\tdefault: break; \\\n");
			else
				fprintf(stdout,
					"\t\tdefault: %s; break; \\\n", res);
212
			fputs(/*{*/ "\t\t} \\\n", stdout);
213
			fputs("\t\tbreak; \\\n", stdout);
214
215
			subswitch = 0;
		}
216
		if (tt->rdclass && tt->type != lasttype) {
217
			fprintf(stdout, "\tcase %d: switch (%s) { \\\n" /*}*/,
218
				tt->type, csw);
219
220
			subswitch = 1;
		}
221
		if (tt->rdclass == 0)
222
			fprintf(stdout,
223
224
				"\tcase %d:%s %s_%s(%s); break;",
				tt->type, result, function,
225
				funname(tt->typename, buf1), args);
226
227
		else
			fprintf(stdout,
228
			        "\t\tcase %d:%s %s_%s_%s(%s); break;",
229
				tt->rdclass, result, function,
230
231
				funname(tt->classname, buf1),
				funname(tt->typename, buf2), args);
232
233
234
235
		fputs(" \\\n", stdout);
		lasttype = tt->type;
	}
	if (subswitch) {
236
237
		if (res == NULL)
			fprintf(stdout, "\t\tdefault: break; \\\n");
238
		else
239
			fprintf(stdout, "\t\tdefault: %s; break; \\\n", res);
240
		fputs(/*{*/ "\t\t} \\\n", stdout);
241
		fputs("\t\tbreak; \\\n", stdout);
242
	}
243
244
245
246
247
248
249
250
251
252
	if (first) {
		if (res == NULL)
			fprintf(stdout, "\n#define %s\n", name);
		else
			fprintf(stdout, "\n#define %s %s;\n", name, res);
	} else {
		if (res == NULL)
			fprintf(stdout, "\tdefault: break; \\\n");
		else
			fprintf(stdout, "\tdefault: %s; break; \\\n", res);
253
254
255
256
257
258
259
		fputs(/*{*/ "\t}\n", stdout);
	}
}

void
dodecl(char *type, char *function, char *args) {
	struct tt *tt;
260
	char buf1[11], buf2[11];
261
262
263

	fputs("\n", stdout);
	for (tt = types; tt ; tt = tt->next)
264
		if (tt->rdclass)
265
			fprintf(stdout,
266
				"static inline %s %s_%s_%s(%s);\n",
267
268
269
				type, function,
				funname(tt->classname, buf1),
				funname(tt->typename, buf2), args);
270
271
		else
			fprintf(stdout,
272
				"static inline %s %s_%s(%s);\n",
273
				type, function,
274
				funname(tt->typename, buf1), args);
275
276
277
}

void
David Lawrence's avatar
David Lawrence committed
278
insert_into_typenames(int type, const char *typename, const char *attr) {
279
	struct ttnam *ttn;
280
281
	int c;
	char tmp[256];
282
283
284
285
286
287
288
289
290
291
292
293
294
295

	ttn = &typenames[type];
	if (ttn->typename[0] == 0) {
		if (strlen(typename) > sizeof(ttn->typename) - 1) {
			fprintf(stderr, "Error:  type name %s is too long\n",
				typename);
			exit(1);
		}
		strcpy(ttn->typename, typename);
	} else if (strcmp(typename, ttn->typename) != 0) {
		fprintf(stderr, "Error:  type %d has two names: %s, %s\n",
			type, ttn->typename, typename);
		exit(1);
	}
296

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
	strcpy(ttn->macroname, ttn->typename);
	c = strlen(ttn->macroname);
	while (c > 0) {
		if (ttn->macroname[c - 1] == '-')
			ttn->macroname[c - 1] = '_';
		c--;
	}

	if (attr == NULL) {
		sprintf(tmp, "RRTYPE_%s_ATTRIBUTES", upper(ttn->macroname));
		attr = tmp;
	}

	if (ttn->attr[0] != 0 && strcmp(attr, ttn->attr) != 0) {
		fprintf(stderr, "Error:  type %d has different attributes: "
			"%s, %s\n", type, ttn->attr, attr);
		exit(1);
	}

	if (strlen(attr) > sizeof(ttn->attr) - 1) {
		fprintf(stderr, "Error:  attr (%s) [name %s] is too long\n",
			attr, typename);
		exit(1);
	}
	strcpy(ttn->attr, attr);
322
	ttn->sorted = 0;
323
324
325
}

void
David Lawrence's avatar
David Lawrence committed
326
327
328
add(int rdclass, const char *classname, int type, const char *typename,
    const char *dirname)
{
329
330
331
332
333
334
335
336
337
338
339
340
	struct tt *newtt = (struct tt *)malloc(sizeof *newtt);
	struct tt *tt, *oldtt;
	struct cc *newcc;
	struct cc *cc, *oldcc;

	insert_into_typenames(type, typename, NULL);

	if (newtt == NULL) {
		fprintf(stderr, "malloc() failed\n");
		exit(1);
	}

341
	newtt->next = NULL;
342
	newtt->rdclass = rdclass;
343
344
345
346
347
348
349
350
	newtt->type = type;
	strcpy(newtt->classname, classname);
	strcpy(newtt->typename, typename);
	strcpy(newtt->dirname, dirname);

	tt = types;
	oldtt = NULL;

Mark Andrews's avatar
Mark Andrews committed
351
	while ((tt != NULL) && (tt->type < type)) {
352
353
354
355
		oldtt = tt;
		tt = tt->next;
	}

356
	while ((tt != NULL) && (tt->type == type) && (tt->rdclass < rdclass)) {
357
358
359
360
361
362
		if (strcmp(tt->typename, typename) != 0)
			exit(1);
		oldtt = tt;
		tt = tt->next;
	}

363
	if ((tt != NULL) && (tt->type == type) && (tt->rdclass == rdclass))
364
365
366
		exit(1);

	newtt->next = tt;
Mark Andrews's avatar
Mark Andrews committed
367
	if (oldtt != NULL)
368
369
370
371
		oldtt->next = newtt;
	else
		types = newtt;

372
373
374
	/*
	 * Do a class switch for this type.
	 */
375
	if (rdclass == 0)
376
377
378
		return;

	newcc = (struct cc *)malloc(sizeof *newcc);
379
	newcc->rdclass = rdclass;
380
	strcpy(newcc->classname, classname);
381
382
	cc = classes;
	oldcc = NULL;
383

384
	while ((cc != NULL) && (cc->rdclass < rdclass)) {
385
386
387
388
		oldcc = cc;
		cc = cc->next;
	}

389
	if ((cc != NULL) && cc->rdclass == rdclass) {
390
391
392
393
394
		free((char *)newcc);
		return;
	}

	newcc->next = cc;
Mark Andrews's avatar
Mark Andrews committed
395
	if (oldcc != NULL)
396
397
398
399
400
401
		oldcc->next = newcc;
	else
		classes = newcc;
}

void
David Lawrence's avatar
David Lawrence committed
402
sd(int rdclass, const char *classname, const char *dirname, char filetype) {
403
	char buf[sizeof "0123456789_65535.h"];
404
	char fmt[sizeof "%10[-0-9a-z]_%d.h"];
405
406
	int type;
	char typename[11];
David Lawrence's avatar
David Lawrence committed
407
	isc_dir_t dir;
408

David Lawrence's avatar
David Lawrence committed
409
	if (!start_directory(dirname, &dir))
410
411
		return;

412
	sprintf(fmt,"%s%c", "%10[-0-9a-z]_%d.", filetype);
David Lawrence's avatar
David Lawrence committed
413
414
	while (next_file(&dir)) {
		if (sscanf(dir.filename, fmt, typename, &type) != 2)
415
416
417
418
			continue;
		if ((type > 65535) || (type < 0))
			continue;

419
		sprintf(buf, "%s_%d.%c", typename, type, filetype);
David Lawrence's avatar
David Lawrence committed
420
		if (strcmp(buf, dir.filename) != 0)
421
			continue;
David Lawrence's avatar
David Lawrence committed
422
		add(rdclass, classname, type, typename, dirname);
423
	}
David Lawrence's avatar
David Lawrence committed
424
425

	end_directory(&dir);
426
427
}

428
static unsigned int
David Lawrence's avatar
David Lawrence committed
429
HASH(char *string) {
430
431
432
433
	unsigned int n;
	unsigned char a, b;

	n = strlen(string);
Michael Graff's avatar
Michael Graff committed
434
435
436
437
438
439
	if (n == 0) {
		fprintf(stderr, "n == 0?\n");
		exit(1);
	}
	a = tolower((unsigned char)string[0]);
	b = tolower((unsigned char)string[n - 1]);
440
441
442
443

	return ((a + n) * b) % 256;
}

444
445
int
main(int argc, char **argv) {
446
447
	char buf[256];			/* XXX Should be max path length */
	char srcdir[256];		/* XXX Should be max path length */
448
	int rdclass;
449
450
	char classname[11];
	struct tt *tt;
451
	struct cc *cc;
452
453
	struct ttnam *ttn, *ttn2;
	unsigned int hash;
454
455
456
	struct tm *tm;
	time_t now;
	char year[11];
457
	int lasttype;
Mark Andrews's avatar
Mark Andrews committed
458
459
460
	int code = 1;
	int class_enum = 0;
	int type_enum = 0;
461
	int structs = 0;
462
	int depend = 0;
463
	int c, i, j;
464
	char buf1[11];
465
466
	char filetype = 'c';
	FILE *fd;
467
468
	char *prefix = NULL;
	char *suffix = NULL;
469
	char *file = NULL;
David Lawrence's avatar
David Lawrence committed
470
	isc_dir_t dir;
471

472
473
	for (i = 0 ; i <= 255 ; i++)
		memset(&typenames[i], 0, sizeof(typenames[i]));
Mark Andrews's avatar
Mark Andrews committed
474

475
	strcpy(srcdir, "");
476
	while ((c = isc_commandline_parse(argc, argv, "cdits:F:P:S:")) != -1)
Mark Andrews's avatar
Mark Andrews committed
477
478
479
		switch (c) {
		case 'c':
			code = 0;
480
			depend = 0;
Mark Andrews's avatar
Mark Andrews committed
481
482
			type_enum = 0;
			class_enum = 1;
483
484
			filetype = 'c';
			structs = 0;
Mark Andrews's avatar
Mark Andrews committed
485
			break;
486
487
488
489
490
491
492
493
		case 'd':
			code = 0;
			depend = 1;
			class_enum = 0;
			type_enum = 0;
			structs = 0;
			filetype = 'h';
			break;
Mark Andrews's avatar
Mark Andrews committed
494
495
		case 't':
			code = 0;
496
			depend = 0;
Mark Andrews's avatar
Mark Andrews committed
497
498
			class_enum = 0;
			type_enum = 1;
499
500
501
502
503
			filetype = 'c';
			structs = 0;
			break;
		case 'i':
			code = 0;
504
			depend = 0;
505
506
507
508
			class_enum = 0;
			type_enum = 0;
			structs = 1;
			filetype = 'h';
Mark Andrews's avatar
Mark Andrews committed
509
			break;
510
		case 's':
David Lawrence's avatar
David Lawrence committed
511
			sprintf(srcdir, "%s/", isc_commandline_argument);
512
			break;
513
514
515
		case 'F':
			file = isc_commandline_argument;
			break;
516
		case 'P':
David Lawrence's avatar
David Lawrence committed
517
			prefix = isc_commandline_argument;
518
519
			break;
		case 'S':
David Lawrence's avatar
David Lawrence committed
520
			suffix = isc_commandline_argument;
521
			break;
Mark Andrews's avatar
Mark Andrews committed
522
523
524
		case '?':
			exit(1);
		}
525

526
	sprintf(buf, "%srdata", srcdir);
David Lawrence's avatar
David Lawrence committed
527
528

	if (!start_directory(buf, &dir))
529
530
		exit(1);

David Lawrence's avatar
David Lawrence committed
531
532
	while (next_file(&dir)) {
		if (sscanf(dir.filename, "%10[0-9a-z]_%d",
533
			   classname, &rdclass) != 2)
534
			continue;
535
		if ((rdclass > 65535) || (rdclass < 0))
536
537
			continue;

538
		sprintf(buf, "%srdata/%s_%d", srcdir, classname, rdclass);
David Lawrence's avatar
David Lawrence committed
539
		if (strcmp(buf + 6 + strlen(srcdir), dir.filename) != 0)
540
			continue;
541
		sd(rdclass, classname, buf, filetype);
542
	}
David Lawrence's avatar
David Lawrence committed
543
	end_directory(&dir);
544
545
	sprintf(buf, "%srdata/generic", srcdir);
	sd(0, "", buf, filetype);
546
547
548
549
550
551
552
553
554

	if (time(&now) != -1) {
		if ((tm = localtime(&now)) != NULL && tm->tm_year > 98)
			sprintf(year, "-%d", tm->tm_year + 1900);
		else
			year[0] = 0;
	} else
		year[0] = 0;

555
	if (!depend) fprintf(stdout, copyright, year);
556

Mark Andrews's avatar
Mark Andrews committed
557
	if (code) {
558
559
560
561
562
563
564
		fputs("#ifndef DNS_CODE_H\n", stdout);
		fputs("#define DNS_CODE_H 1\n\n", stdout);

		fputs("#include <isc/boolean.h>\n", stdout);
		fputs("#include <isc/result.h>\n\n", stdout);
		fputs("#include <dns/name.h>\n\n", stdout);

565
566
567
		for (tt = types; tt != NULL ; tt = tt->next)
			fprintf(stdout, "#include \"%s/%s_%d.c\"\n",
				tt->dirname, tt->typename, tt->type);
568

569
		fputs("\n\n", stdout);
Mark Andrews's avatar
Mark Andrews committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

		doswitch("FROMTEXTSWITCH", "fromtext", FROMTEXTARGS,
			 FROMTEXTTYPE, FROMTEXTCLASS, FROMTEXTDEF);
		doswitch("TOTEXTSWITCH", "totext", TOTEXTARGS,
			 TOTEXTTYPE, TOTEXTCLASS, TOTEXTDEF);
		doswitch("FROMWIRESWITCH", "fromwire", FROMWIREARGS,
			 FROMWIRETYPE, FROMWIRECLASS, FROMWIREDEF);
		doswitch("TOWIRESWITCH", "towire", TOWIREARGS,
			 TOWIRETYPE, TOWIRECLASS, TOWIREDEF);
		doswitch("COMPARESWITCH", "compare", COMPAREARGS,
			  COMPARETYPE, COMPARECLASS, COMPAREDEF);
		doswitch("FROMSTRUCTSWITCH", "fromstruct", FROMSTRUCTARGS,
			  FROMSTRUCTTYPE, FROMSTRUCTCLASS, FROMSTRUCTDEF);
		doswitch("TOSTRUCTSWITCH", "tostruct", TOSTRUCTARGS,
			  TOSTRUCTTYPE, TOSTRUCTCLASS, TOSTRUCTDEF);
585
586
		doswitch("FREESTRUCTSWITCH", "freestruct", FREESTRUCTARGS,
			  FREESTRUCTTYPE, FREESTRUCTCLASS, FREESTRUCTDEF);
587
588
589
		doswitch("ADDITIONALDATASWITCH", "additionaldata",
			 ADDITIONALDATAARGS, ADDITIONALDATATYPE,
			 ADDITIONALDATACLASS, ADDITIONALDATADEF);
Bob Halley's avatar
Bob Halley committed
590
591
592
		doswitch("DIGESTSWITCH", "digest",
			 DIGESTARGS, DIGESTTYPE,
			 DIGESTCLASS, DIGESTDEF);
Mark Andrews's avatar
Mark Andrews committed
593

594
595
596
597
		/*
		 * From here down, we are processing the rdata names and
		 * attributes.
		 */
Mark Andrews's avatar
Mark Andrews committed
598

599
600
#define PRINT_COMMA(x) (x == 255 ? "" : ",")

601
602
603
604
#define METANOTQUESTION  "DNS_RDATATYPEATTR_META | " \
			 "DNS_RDATATYPEATTR_NOTQUESTION"
#define METAQUESTIONONLY "DNS_RDATATYPEATTR_META | " \
			 "DNS_RDATATYPEATTR_QUESTIONONLY"
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#define RESERVED "DNS_RDATATYPEATTR_RESERVED"

		/*
		 * Add in reserved/special types.  This will let us
		 * sort them without special cases.
		 */
		insert_into_typenames(0, "reserved0", RESERVED);
		insert_into_typenames(31, "eid", RESERVED);
		insert_into_typenames(32, "nimloc", RESERVED);
		insert_into_typenames(34, "atma", RESERVED);
		insert_into_typenames(100, "uinfo", RESERVED);
		insert_into_typenames(101, "uid", RESERVED);
		insert_into_typenames(102, "gid", RESERVED);
		insert_into_typenames(251, "ixfr", METAQUESTIONONLY);
		insert_into_typenames(252, "axfr", METAQUESTIONONLY);
		insert_into_typenames(253, "mailb", METAQUESTIONONLY);
		insert_into_typenames(254, "maila", METAQUESTIONONLY);
		insert_into_typenames(255, "any", METAQUESTIONONLY);
623

624
		printf("\ntypedef struct {\n");
David Lawrence's avatar
David Lawrence committed
625
		printf("\tconst char *name;\n");
626
627
628
629
630
		printf("\tunsigned int flags;\n");
		printf("} typeattr_t;\n");
		printf("static typeattr_t typeattr[] = {\n");
		for (i = 0 ; i <= 255 ; i++) {
			ttn = &typenames[i];
631
			if (ttn->typename[0] == 0) {
632
633
634
635
636
637
638
639
				const char *attrs;
				if (i >= 128 && i < 255)
					attrs = "DNS_RDATATYPEATTR_UNKNOWN | "
						"DNS_RDATATYPEATTR_META";
				else
					attrs = "DNS_RDATATYPEATTR_UNKNOWN";
				printf("\t{ \"TYPE%d\", %s}%s\n",
				       i, attrs, PRINT_COMMA(i));
640
641
			} else {
				printf("\t{ \"%s\", %s }%s\n",
642
				       upper(ttn->typename),
643
				       upper(ttn->attr),
644
645
646
647
648
				       PRINT_COMMA(i));
			}
		}
		printf("};\n");

649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
		/*
		 * Run through the list of types and pre-mark the unused
		 * ones as "sorted" so we simply ignore them below.
		 */
		for (i = 0 ; i <= 255 ; i++) {
			ttn = &typenames[i];
			if (ttn->typename[0] == 0)
				ttn->sorted = 1;
		}

		/*
		 * Spit out a quick and dirty hash function.  Here,
		 * we walk through the list of type names, and calculate
		 * a hash.  This isn't perfect, but it will generate "pretty
		 * good" estimates.  Lowercase the characters before
		 * computing in all cases.
		 *
		 * Here, walk the list from top to bottom, calculating
		 * the hash (mod 256) for each name.
		 */
669
670
671
		printf("#define RDATATYPE_COMPARE(_s, _d, _tn, _tp) \\\n");
		printf("\tdo { \\\n");
		printf("\t\tif (strcasecmp(_s,(_tn)) == 0) { \\\n");
672
673
		printf("\t\t\tif ((typeattr[_d].flags & "
		       		  "DNS_RDATATYPEATTR_RESERVED) != 0) \\\n");
674
675
676
677
678
679
		printf("\t\t\t\treturn (ISC_R_NOTIMPLEMENTED); \\\n");
		printf("\t\t\t*(_tp) = _d; \\\n");
		printf("\t\t\treturn (ISC_R_SUCCESS); \\\n");
		printf("\t\t} \\\n");
		printf("\t} while (0)\n\n");

680
681
		printf("#define RDATATYPE_FROMTEXT_SW(_hash,_typename,_typep) "
		       "\\\n");
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
		printf("\tswitch (_hash) { \\\n");
		for (i = 0 ; i <= 255 ; i++) {
			ttn = &typenames[i];

			/*
			 * Skip entries we already processed.
			 */
			if (ttn->sorted != 0)
				continue;

			hash = HASH(ttn->typename);
			printf("\t\tcase %u: \\\n", hash);

			/*
			 * Find all other entries that happen to match
			 * this hash.
			 */
699
			for (j = 0 ; j <= 255 ; j++) {
700
				ttn2 = &typenames[j];
701
				if (ttn2->sorted != 0)
Michael Graff's avatar
Michael Graff committed
702
					continue;
703
				if (hash == HASH(ttn2->typename)) {
704
705
706
					printf("\t\t\tRDATATYPE_COMPARE"
					       "(\"%s\", %u, "
					       "_typename, _typep); \\\n",
707
					       ttn2->typename, j);
708
709
710
711
712
713
714
					ttn2->sorted = 1;
				}
			}
			printf("\t\t\tbreak; \\\n");
		}
		printf("\t}\n");

715
		fputs("#endif /* DNS_CODE_H */\n", stdout);
Mark Andrews's avatar
Mark Andrews committed
716
	} else if (type_enum) {
717
718
		char *s;

719
		fprintf(stdout, "#ifndef DNS_ENUMTYPE_H\n");
720
		fprintf(stdout, "#define DNS_ENUMTYPE_H 1\n\n");
721

722
723
		fprintf(stdout, "enum {\n");
		fprintf(stdout, "\tdns_rdatatype_none = 0,\n");
Mark Andrews's avatar
Mark Andrews committed
724
725
726
727

		lasttype = 0;
		for (tt = types; tt != NULL ; tt = tt->next)
			if (tt->type != lasttype)
728
				fprintf(stdout,
729
					"\tdns_rdatatype_%s = %d,\n",
730
					funname(tt->typename, buf1),
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
					lasttype = tt->type);

		fprintf(stdout, "\tdns_rdatatype_ixfr = 251,\n");
		fprintf(stdout, "\tdns_rdatatype_axfr = 252,\n");
		fprintf(stdout, "\tdns_rdatatype_mailb = 253,\n");
		fprintf(stdout, "\tdns_rdatatype_maila = 254,\n");
		fprintf(stdout, "\tdns_rdatatype_any = 255\n");

		fprintf(stdout, "};\n\n");

		fprintf(stdout, "#define dns_rdatatype_none\t"
			"((dns_rdatatype_t)dns_rdatatype_none)\n");

		for (tt = types; tt != NULL ; tt = tt->next)
			if (tt->type != lasttype) {
				s = funname(tt->typename, buf1);
				fprintf(stdout,
					"#define dns_rdatatype_%s\t%s"
					"((dns_rdatatype_t)dns_rdatatype_%s)"
					"\n",
					s, strlen(s) < 2 ? "\t" : "", s);
				lasttype = tt->type;
			}

		fprintf(stdout, "#define dns_rdatatype_ixfr\t"
			"((dns_rdatatype_t)dns_rdatatype_ixfr)\n");
		fprintf(stdout, "#define dns_rdatatype_axfr\t"
			"((dns_rdatatype_t)dns_rdatatype_axfr)\n");
		fprintf(stdout, "#define dns_rdatatype_mailb\t"
			"((dns_rdatatype_t)dns_rdatatype_mailb)\n");
		fprintf(stdout, "#define dns_rdatatype_maila\t"
			"((dns_rdatatype_t)dns_rdatatype_maila)\n");
		fprintf(stdout, "#define dns_rdatatype_any\t"
			"((dns_rdatatype_t)dns_rdatatype_any)\n");
765

766
767
		fprintf(stdout, "\n#endif /* DNS_ENUMTYPE_H */\n");

Mark Andrews's avatar
Mark Andrews committed
768
	} else if (class_enum) {
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
		char *s;
		int classnum;

		printf("#ifndef DNS_ENUMCLASS_H\n");
		printf("#define DNS_ENUMCLASS_H 1\n\n");

		printf("enum {\n");

		printf("\tdns_rdataclass_reserved0 = 0,\n");
		printf("#define dns_rdataclass_reserved0 \\\n\t\t\t\t"
			"((dns_rdataclass_t)dns_rdataclass_reserved0)\n");

#define PRINTCLASS(name, num) \
	do { \
		s = funname(name, buf1); \
		classnum = num; \
785
786
		printf("\tdns_rdataclass_%s = %d%s\n", s, classnum, \
		       classnum != 255 ? "," : ""); \
787
788
789
		printf("#define dns_rdataclass_%s\t" \
		       "((dns_rdataclass_t)dns_rdataclass_%s)\n", s, s); \
	} while (0)
Mark Andrews's avatar
Mark Andrews committed
790

791
		for (cc = classes; cc != NULL; cc = cc->next) {
792
793
794
795
796
797
798
799
			if (cc->rdclass == 4) {
				PRINTCLASS("ch", 3);
				PRINTCLASS("chaos", 3);

			} else if (cc->rdclass == 255) {
				PRINTCLASS("none", 254);
			}
			PRINTCLASS(cc->classname, cc->rdclass);
800
		}
801
802
803
804
805

#undef PRINTCLASS

		printf("};\n\n");
		printf("#endif /* DNS_ENUMCLASS_H */\n");
806
	} else if (structs) {
807
808
809
810
811
812
813
		if (prefix != NULL) {
			if ((fd = fopen(prefix,"r")) != NULL) {
				while (fgets(buf, sizeof buf, fd) != NULL)
					fputs(buf, stdout);
				fclose(fd);
			}
		}
814
815
816
817
818
819
820
821
822
		for (tt = types; tt != NULL ; tt = tt->next) {
			sprintf(buf, "%s/%s_%d.h",
				tt->dirname, tt->typename, tt->type);
			if ((fd = fopen(buf,"r")) != NULL) {
				while (fgets(buf, sizeof buf, fd) != NULL)
					fputs(buf, stdout);
				fclose(fd);
			}
		}
823
824
825
826
827
828
829
		if (suffix != NULL) {
			if ((fd = fopen(suffix,"r")) != NULL) {
				while (fgets(buf, sizeof buf, fd) != NULL)
					fputs(buf, stdout);
				fclose(fd);
			}
		}
830
831
832
833
	} else if (depend) {
		for (tt = types; tt != NULL ; tt = tt->next)
			fprintf(stdout, "%s:\t%s/%s_%d.h\n", file,
				tt->dirname, tt->typename, tt->type);
Mark Andrews's avatar
Mark Andrews committed
834
	}
835
836
837
838

	if (ferror(stdout) != 0)
		exit(1);

Bob Halley's avatar
lint    
Bob Halley committed
839
	return (0);
840
}