No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / gen.c
blobab6d32034db0938a28835624e9e6a3e035e61c63
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: gen.c,v 1.85 2009/12/04 22:06:37 tbox Exp */
22 /*! \file */
24 #ifdef WIN32
26 * Silence compiler warnings about using strcpy and friends.
28 #define _CRT_SECURE_NO_DEPRECATE 1
29 #endif
31 #include <sys/types.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
40 #ifdef WIN32
41 #include "gen-win32.h"
42 #else
43 #include "gen-unix.h"
44 #endif
46 #define TYPECLASSLEN 21
48 #define FROMTEXTARGS "rdclass, type, lexer, origin, options, target, callbacks"
49 #define FROMTEXTCLASS "rdclass"
50 #define FROMTEXTTYPE "type"
51 #define FROMTEXTDEF "result = DNS_R_UNKNOWN"
53 #define TOTEXTARGS "rdata, tctx, target"
54 #define TOTEXTCLASS "rdata->rdclass"
55 #define TOTEXTTYPE "rdata->type"
56 #define TOTEXTDEF "use_default = ISC_TRUE"
58 #define FROMWIREARGS "rdclass, type, source, dctx, options, target"
59 #define FROMWIRECLASS "rdclass"
60 #define FROMWIRETYPE "type"
61 #define FROMWIREDEF "use_default = ISC_TRUE"
63 #define TOWIREARGS "rdata, cctx, target"
64 #define TOWIRECLASS "rdata->rdclass"
65 #define TOWIRETYPE "rdata->type"
66 #define TOWIREDEF "use_default = ISC_TRUE"
68 #define FROMSTRUCTARGS "rdclass, type, source, target"
69 #define FROMSTRUCTCLASS "rdclass"
70 #define FROMSTRUCTTYPE "type"
71 #define FROMSTRUCTDEF "use_default = ISC_TRUE"
73 #define TOSTRUCTARGS "rdata, target, mctx"
74 #define TOSTRUCTCLASS "rdata->rdclass"
75 #define TOSTRUCTTYPE "rdata->type"
76 #define TOSTRUCTDEF "use_default = ISC_TRUE"
78 #define FREESTRUCTARGS "source"
79 #define FREESTRUCTCLASS "common->rdclass"
80 #define FREESTRUCTTYPE "common->rdtype"
81 #define FREESTRUCTDEF NULL
83 #define COMPAREARGS "rdata1, rdata2"
84 #define COMPARECLASS "rdata1->rdclass"
85 #define COMPARETYPE "rdata1->type"
86 #define COMPAREDEF "use_default = ISC_TRUE"
88 #define ADDITIONALDATAARGS "rdata, add, arg"
89 #define ADDITIONALDATACLASS "rdata->rdclass"
90 #define ADDITIONALDATATYPE "rdata->type"
91 #define ADDITIONALDATADEF "use_default = ISC_TRUE"
93 #define DIGESTARGS "rdata, digest, arg"
94 #define DIGESTCLASS "rdata->rdclass"
95 #define DIGESTTYPE "rdata->type"
96 #define DIGESTDEF "use_default = ISC_TRUE"
98 #define CHECKOWNERARGS "name, rdclass, type, wildcard"
99 #define CHECKOWNERCLASS "rdclass"
100 #define CHECKOWNERTYPE "type"
101 #define CHECKOWNERDEF "result = ISC_TRUE"
103 #define CHECKNAMESARGS "rdata, owner, bad"
104 #define CHECKNAMESCLASS "rdata->rdclass"
105 #define CHECKNAMESTYPE "rdata->type"
106 #define CHECKNAMESDEF "result = ISC_TRUE"
108 const char copyright[] =
109 "/*\n"
110 " * Copyright (C) 2004%s Internet Systems Consortium, Inc. (\"ISC\")\n"
111 " * Copyright (C) 1998-2003 Internet Software Consortium.\n"
112 " *\n"
113 " * Permission to use, copy, modify, and distribute this software for any\n"
114 " * purpose with or without fee is hereby granted, provided that the above\n"
115 " * copyright notice and this permission notice appear in all copies.\n"
116 " *\n"
117 " * THE SOFTWARE IS PROVIDED \"AS IS\" AND ISC DISCLAIMS ALL WARRANTIES WITH\n"
118 " * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n"
119 " * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,\n"
120 " * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n"
121 " * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE\n"
122 " * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n"
123 " * PERFORMANCE OF THIS SOFTWARE.\n"
124 " */\n"
125 "\n"
126 "/***************\n"
127 " ***************\n"
128 " *************** THIS FILE IS AUTOMATICALLY GENERATED BY gen.c.\n"
129 " *************** DO NOT EDIT!\n"
130 " ***************\n"
131 " ***************/\n"
132 "\n"
133 "/*! \\file */\n"
134 "\n";
136 #define TYPENAMES 256
138 struct cc {
139 struct cc *next;
140 int rdclass;
141 char classname[TYPECLASSLEN];
142 } *classes;
144 struct tt {
145 struct tt *next;
146 int rdclass;
147 int type;
148 char classname[TYPECLASSLEN];
149 char typename[TYPECLASSLEN];
150 char dirname[256]; /* XXX Should be max path length */
151 } *types;
153 struct ttnam {
154 char typename[TYPECLASSLEN];
155 char macroname[TYPECLASSLEN];
156 char attr[256];
157 unsigned int sorted;
158 int type;
159 } typenames[TYPENAMES];
161 int maxtype = -1;
163 char *
164 upper(char *);
165 char *
166 funname(const char *, char *);
167 void
168 doswitch(const char *, const char *, const char *, const char *,
169 const char *, const char *);
170 void
171 dodecl(char *, char *, char *);
172 void
173 add(int, const char *, int, const char *, const char *);
174 void
175 sd(int, const char *, const char *, char);
176 void
177 insert_into_typenames(int, const char *, const char *);
180 * If you use more than 10 of these in, say, a printf(), you'll have problems.
182 char *
183 upper(char *s) {
184 static int buf_to_use = 0;
185 static char buf[10][256];
186 char *b;
187 int c;
189 buf_to_use++;
190 if (buf_to_use > 9)
191 buf_to_use = 0;
193 b = buf[buf_to_use];
194 memset(b, 0, 256);
196 while ((c = (*s++) & 0xff))
197 *b++ = islower(c) ? toupper(c) : c;
198 *b = '\0';
199 return (buf[buf_to_use]);
202 char *
203 funname(const char *s, char *buf) {
204 char *b = buf;
205 char c;
207 while ((c = *s++)) {
208 *b++ = (c == '-') ? '_' : c;
210 *b = '\0';
211 return (buf);
214 void
215 doswitch(const char *name, const char *function, const char *args,
216 const char *tsw, const char *csw, const char *res)
218 struct tt *tt;
219 int first = 1;
220 int lasttype = 0;
221 int subswitch = 0;
222 char buf1[TYPECLASSLEN], buf2[TYPECLASSLEN];
223 const char *result = " result =";
225 if (res == NULL)
226 result = "";
228 for (tt = types; tt != NULL; tt = tt->next) {
229 if (first) {
230 fprintf(stdout, "\n#define %s \\\n", name);
231 fprintf(stdout, "\tswitch (%s) { \\\n" /*}*/, tsw);
232 first = 0;
234 if (tt->type != lasttype && subswitch) {
235 if (res == NULL)
236 fprintf(stdout, "\t\tdefault: break; \\\n");
237 else
238 fprintf(stdout,
239 "\t\tdefault: %s; break; \\\n", res);
240 fputs(/*{*/ "\t\t} \\\n", stdout);
241 fputs("\t\tbreak; \\\n", stdout);
242 subswitch = 0;
244 if (tt->rdclass && tt->type != lasttype) {
245 fprintf(stdout, "\tcase %d: switch (%s) { \\\n" /*}*/,
246 tt->type, csw);
247 subswitch = 1;
249 if (tt->rdclass == 0)
250 fprintf(stdout,
251 "\tcase %d:%s %s_%s(%s); break;",
252 tt->type, result, function,
253 funname(tt->typename, buf1), args);
254 else
255 fprintf(stdout,
256 "\t\tcase %d:%s %s_%s_%s(%s); break;",
257 tt->rdclass, result, function,
258 funname(tt->classname, buf1),
259 funname(tt->typename, buf2), args);
260 fputs(" \\\n", stdout);
261 lasttype = tt->type;
263 if (subswitch) {
264 if (res == NULL)
265 fprintf(stdout, "\t\tdefault: break; \\\n");
266 else
267 fprintf(stdout, "\t\tdefault: %s; break; \\\n", res);
268 fputs(/*{*/ "\t\t} \\\n", stdout);
269 fputs("\t\tbreak; \\\n", stdout);
271 if (first) {
272 if (res == NULL)
273 fprintf(stdout, "\n#define %s\n", name);
274 else
275 fprintf(stdout, "\n#define %s %s;\n", name, res);
276 } else {
277 if (res == NULL)
278 fprintf(stdout, "\tdefault: break; \\\n");
279 else
280 fprintf(stdout, "\tdefault: %s; break; \\\n", res);
281 fputs(/*{*/ "\t}\n", stdout);
285 void
286 dodecl(char *type, char *function, char *args) {
287 struct tt *tt;
288 char buf1[TYPECLASSLEN], buf2[TYPECLASSLEN];
290 fputs("\n", stdout);
291 for (tt = types; tt; tt = tt->next)
292 if (tt->rdclass)
293 fprintf(stdout,
294 "static inline %s %s_%s_%s(%s);\n",
295 type, function,
296 funname(tt->classname, buf1),
297 funname(tt->typename, buf2), args);
298 else
299 fprintf(stdout,
300 "static inline %s %s_%s(%s);\n",
301 type, function,
302 funname(tt->typename, buf1), args);
305 static struct ttnam *
306 find_typename(int type) {
307 int i;
309 for (i = 0; i < TYPENAMES; i++) {
310 if (typenames[i].typename[0] != 0 &&
311 typenames[i].type == type)
312 return (&typenames[i]);
314 return (NULL);
317 void
318 insert_into_typenames(int type, const char *typename, const char *attr) {
319 struct ttnam *ttn = NULL;
320 int c, i;
321 char tmp[256];
323 for (i = 0; i < TYPENAMES; i++) {
324 if (typenames[i].typename[0] != 0 &&
325 typenames[i].type == type &&
326 strcmp(typename, typenames[i].typename) != 0) {
327 fprintf(stderr,
328 "Error: type %d has two names: %s, %s\n",
329 type, typenames[i].typename, typename);
330 exit(1);
332 if (typenames[i].typename[0] == 0 && ttn == NULL)
333 ttn = &typenames[i];
335 if (ttn == NULL) {
336 fprintf(stderr, "Error: typenames array too small\n");
337 exit(1);
340 if (strlen(typename) > sizeof(ttn->typename) - 1) {
341 fprintf(stderr, "Error: type name %s is too long\n",
342 typename);
343 exit(1);
345 strcpy(ttn->typename, typename);
346 ttn->type = type;
348 strcpy(ttn->macroname, ttn->typename);
349 c = strlen(ttn->macroname);
350 while (c > 0) {
351 if (ttn->macroname[c - 1] == '-')
352 ttn->macroname[c - 1] = '_';
353 c--;
356 if (attr == NULL) {
357 sprintf(tmp, "RRTYPE_%s_ATTRIBUTES", upper(ttn->macroname));
358 attr = tmp;
361 if (ttn->attr[0] != 0 && strcmp(attr, ttn->attr) != 0) {
362 fprintf(stderr, "Error: type %d has different attributes: "
363 "%s, %s\n", type, ttn->attr, attr);
364 exit(1);
367 if (strlen(attr) > sizeof(ttn->attr) - 1) {
368 fprintf(stderr, "Error: attr (%s) [name %s] is too long\n",
369 attr, typename);
370 exit(1);
372 strcpy(ttn->attr, attr);
373 ttn->sorted = 0;
374 if (maxtype < type)
375 maxtype = type;
378 void
379 add(int rdclass, const char *classname, int type, const char *typename,
380 const char *dirname)
382 struct tt *newtt = (struct tt *)malloc(sizeof(*newtt));
383 struct tt *tt, *oldtt;
384 struct cc *newcc;
385 struct cc *cc, *oldcc;
387 insert_into_typenames(type, typename, NULL);
389 if (newtt == NULL) {
390 fprintf(stderr, "malloc() failed\n");
391 exit(1);
394 newtt->next = NULL;
395 newtt->rdclass = rdclass;
396 newtt->type = type;
397 strcpy(newtt->classname, classname);
398 strcpy(newtt->typename, typename);
399 if (strncmp(dirname, "./", 2) == 0)
400 dirname += 2;
401 strcpy(newtt->dirname, dirname);
403 tt = types;
404 oldtt = NULL;
406 while ((tt != NULL) && (tt->type < type)) {
407 oldtt = tt;
408 tt = tt->next;
411 while ((tt != NULL) && (tt->type == type) && (tt->rdclass < rdclass)) {
412 if (strcmp(tt->typename, typename) != 0)
413 exit(1);
414 oldtt = tt;
415 tt = tt->next;
418 if ((tt != NULL) && (tt->type == type) && (tt->rdclass == rdclass))
419 exit(1);
421 newtt->next = tt;
422 if (oldtt != NULL)
423 oldtt->next = newtt;
424 else
425 types = newtt;
428 * Do a class switch for this type.
430 if (rdclass == 0)
431 return;
433 newcc = (struct cc *)malloc(sizeof(*newcc));
434 newcc->rdclass = rdclass;
435 strcpy(newcc->classname, classname);
436 cc = classes;
437 oldcc = NULL;
439 while ((cc != NULL) && (cc->rdclass < rdclass)) {
440 oldcc = cc;
441 cc = cc->next;
444 if ((cc != NULL) && cc->rdclass == rdclass) {
445 free((char *)newcc);
446 return;
449 newcc->next = cc;
450 if (oldcc != NULL)
451 oldcc->next = newcc;
452 else
453 classes = newcc;
456 void
457 sd(int rdclass, const char *classname, const char *dirname, char filetype) {
458 char buf[sizeof("01234567890123456789_65535.h")];
459 char fmt[sizeof("%20[-0-9a-z]_%d.h")];
460 int type;
461 char typename[TYPECLASSLEN];
462 isc_dir_t dir;
464 if (!start_directory(dirname, &dir))
465 return;
467 sprintf(fmt,"%s%c", "%20[-0-9a-z]_%d.", filetype);
468 while (next_file(&dir)) {
469 if (sscanf(dir.filename, fmt, typename, &type) != 2)
470 continue;
471 if ((type > 65535) || (type < 0))
472 continue;
474 sprintf(buf, "%s_%d.%c", typename, type, filetype);
475 if (strcmp(buf, dir.filename) != 0)
476 continue;
477 add(rdclass, classname, type, typename, dirname);
480 end_directory(&dir);
483 static unsigned int
484 HASH(char *string) {
485 unsigned int n;
486 unsigned char a, b;
488 n = strlen(string);
489 if (n == 0) {
490 fprintf(stderr, "n == 0?\n");
491 exit(1);
493 a = tolower((unsigned char)string[0]);
494 b = tolower((unsigned char)string[n - 1]);
496 return ((a + n) * b) % 256;
500 main(int argc, char **argv) {
501 char buf[256]; /* XXX Should be max path length */
502 char srcdir[256]; /* XXX Should be max path length */
503 int rdclass;
504 char classname[TYPECLASSLEN];
505 struct tt *tt;
506 struct cc *cc;
507 struct ttnam *ttn, *ttn2;
508 unsigned int hash;
509 struct tm *tm;
510 time_t now;
511 char year[11];
512 int lasttype;
513 int code = 1;
514 int class_enum = 0;
515 int type_enum = 0;
516 int structs = 0;
517 int depend = 0;
518 int c, i, j;
519 char buf1[TYPECLASSLEN];
520 char filetype = 'c';
521 FILE *fd;
522 char *prefix = NULL;
523 char *suffix = NULL;
524 char *file = NULL;
525 isc_dir_t dir;
527 for (i = 0; i < TYPENAMES; i++)
528 memset(&typenames[i], 0, sizeof(typenames[i]));
530 strcpy(srcdir, "");
531 while ((c = isc_commandline_parse(argc, argv, "cdits:F:P:S:")) != -1)
532 switch (c) {
533 case 'c':
534 code = 0;
535 depend = 0;
536 type_enum = 0;
537 class_enum = 1;
538 filetype = 'c';
539 structs = 0;
540 break;
541 case 'd':
542 code = 0;
543 depend = 1;
544 class_enum = 0;
545 type_enum = 0;
546 structs = 0;
547 filetype = 'h';
548 break;
549 case 't':
550 code = 0;
551 depend = 0;
552 class_enum = 0;
553 type_enum = 1;
554 filetype = 'c';
555 structs = 0;
556 break;
557 case 'i':
558 code = 0;
559 depend = 0;
560 class_enum = 0;
561 type_enum = 0;
562 structs = 1;
563 filetype = 'h';
564 break;
565 case 's':
566 sprintf(srcdir, "%s/", isc_commandline_argument);
567 break;
568 case 'F':
569 file = isc_commandline_argument;
570 break;
571 case 'P':
572 prefix = isc_commandline_argument;
573 break;
574 case 'S':
575 suffix = isc_commandline_argument;
576 break;
577 case '?':
578 exit(1);
581 sprintf(buf, "%srdata", srcdir);
583 if (!start_directory(buf, &dir))
584 exit(1);
586 while (next_file(&dir)) {
587 if (sscanf(dir.filename, "%10[0-9a-z]_%d",
588 classname, &rdclass) != 2)
589 continue;
590 if ((rdclass > 65535) || (rdclass < 0))
591 continue;
593 sprintf(buf, "%srdata/%s_%d", srcdir, classname, rdclass);
594 if (strcmp(buf + 6 + strlen(srcdir), dir.filename) != 0)
595 continue;
596 sd(rdclass, classname, buf, filetype);
598 end_directory(&dir);
599 sprintf(buf, "%srdata/generic", srcdir);
600 sd(0, "", buf, filetype);
602 if (time(&now) != -1) {
603 if ((tm = localtime(&now)) != NULL && tm->tm_year > 104)
604 sprintf(year, "-%d", tm->tm_year + 1900);
605 else
606 year[0] = 0;
607 } else
608 year[0] = 0;
610 if (!depend) fprintf(stdout, copyright, year);
612 if (code) {
613 fputs("#ifndef DNS_CODE_H\n", stdout);
614 fputs("#define DNS_CODE_H 1\n\n", stdout);
616 fputs("#include <isc/boolean.h>\n", stdout);
617 fputs("#include <isc/result.h>\n\n", stdout);
618 fputs("#include <dns/name.h>\n\n", stdout);
620 for (tt = types; tt != NULL; tt = tt->next)
621 fprintf(stdout, "#include \"%s/%s_%d.c\"\n",
622 tt->dirname, tt->typename, tt->type);
624 fputs("\n\n", stdout);
626 doswitch("FROMTEXTSWITCH", "fromtext", FROMTEXTARGS,
627 FROMTEXTTYPE, FROMTEXTCLASS, FROMTEXTDEF);
628 doswitch("TOTEXTSWITCH", "totext", TOTEXTARGS,
629 TOTEXTTYPE, TOTEXTCLASS, TOTEXTDEF);
630 doswitch("FROMWIRESWITCH", "fromwire", FROMWIREARGS,
631 FROMWIRETYPE, FROMWIRECLASS, FROMWIREDEF);
632 doswitch("TOWIRESWITCH", "towire", TOWIREARGS,
633 TOWIRETYPE, TOWIRECLASS, TOWIREDEF);
634 doswitch("COMPARESWITCH", "compare", COMPAREARGS,
635 COMPARETYPE, COMPARECLASS, COMPAREDEF);
636 doswitch("CASECOMPARESWITCH", "casecompare", COMPAREARGS,
637 COMPARETYPE, COMPARECLASS, COMPAREDEF);
638 doswitch("FROMSTRUCTSWITCH", "fromstruct", FROMSTRUCTARGS,
639 FROMSTRUCTTYPE, FROMSTRUCTCLASS, FROMSTRUCTDEF);
640 doswitch("TOSTRUCTSWITCH", "tostruct", TOSTRUCTARGS,
641 TOSTRUCTTYPE, TOSTRUCTCLASS, TOSTRUCTDEF);
642 doswitch("FREESTRUCTSWITCH", "freestruct", FREESTRUCTARGS,
643 FREESTRUCTTYPE, FREESTRUCTCLASS, FREESTRUCTDEF);
644 doswitch("ADDITIONALDATASWITCH", "additionaldata",
645 ADDITIONALDATAARGS, ADDITIONALDATATYPE,
646 ADDITIONALDATACLASS, ADDITIONALDATADEF);
647 doswitch("DIGESTSWITCH", "digest",
648 DIGESTARGS, DIGESTTYPE,
649 DIGESTCLASS, DIGESTDEF);
650 doswitch("CHECKOWNERSWITCH", "checkowner",
651 CHECKOWNERARGS, CHECKOWNERTYPE,
652 CHECKOWNERCLASS, CHECKOWNERDEF);
653 doswitch("CHECKNAMESSWITCH", "checknames",
654 CHECKNAMESARGS, CHECKNAMESTYPE,
655 CHECKNAMESCLASS, CHECKNAMESDEF);
658 * From here down, we are processing the rdata names and
659 * attributes.
662 #define PRINT_COMMA(x) (x == maxtype ? "" : ",")
664 #define METANOTQUESTION "DNS_RDATATYPEATTR_META | " \
665 "DNS_RDATATYPEATTR_NOTQUESTION"
666 #define METAQUESTIONONLY "DNS_RDATATYPEATTR_META | " \
667 "DNS_RDATATYPEATTR_QUESTIONONLY"
668 #define RESERVED "DNS_RDATATYPEATTR_RESERVED"
671 * Add in reserved/special types. This will let us
672 * sort them without special cases.
674 insert_into_typenames(0, "reserved0", RESERVED);
675 insert_into_typenames(31, "eid", RESERVED);
676 insert_into_typenames(32, "nimloc", RESERVED);
677 insert_into_typenames(34, "atma", RESERVED);
678 insert_into_typenames(100, "uinfo", RESERVED);
679 insert_into_typenames(101, "uid", RESERVED);
680 insert_into_typenames(102, "gid", RESERVED);
681 insert_into_typenames(251, "ixfr", METAQUESTIONONLY);
682 insert_into_typenames(252, "axfr", METAQUESTIONONLY);
683 insert_into_typenames(253, "mailb", METAQUESTIONONLY);
684 insert_into_typenames(254, "maila", METAQUESTIONONLY);
685 insert_into_typenames(255, "any", METAQUESTIONONLY);
688 * Spit out a quick and dirty hash function. Here,
689 * we walk through the list of type names, and calculate
690 * a hash. This isn't perfect, but it will generate "pretty
691 * good" estimates. Lowercase the characters before
692 * computing in all cases.
694 * Here, walk the list from top to bottom, calculating
695 * the hash (mod 256) for each name.
697 fprintf(stdout, "#define RDATATYPE_COMPARE(_s, _d, _tn, _n, _tp) \\\n");
698 fprintf(stdout, "\tdo { \\\n");
699 fprintf(stdout, "\t\tif (sizeof(_s) - 1 == _n && \\\n"
700 "\t\t strncasecmp(_s,(_tn),"
701 "(sizeof(_s) - 1)) == 0) { \\\n");
702 fprintf(stdout, "\t\t\tif ((dns_rdatatype_attributes(_d) & "
703 "DNS_RDATATYPEATTR_RESERVED) != 0) \\\n");
704 fprintf(stdout, "\t\t\t\treturn (ISC_R_NOTIMPLEMENTED); \\\n");
705 fprintf(stdout, "\t\t\t*(_tp) = _d; \\\n");
706 fprintf(stdout, "\t\t\treturn (ISC_R_SUCCESS); \\\n");
707 fprintf(stdout, "\t\t} \\\n");
708 fprintf(stdout, "\t} while (0)\n\n");
710 fprintf(stdout, "#define RDATATYPE_FROMTEXT_SW(_hash,"
711 "_typename,_length,_typep) \\\n");
712 fprintf(stdout, "\tswitch (_hash) { \\\n");
713 for (i = 0; i <= maxtype; i++) {
714 ttn = find_typename(i);
715 if (ttn == NULL)
716 continue;
719 * Skip entries we already processed.
721 if (ttn->sorted != 0)
722 continue;
724 hash = HASH(ttn->typename);
725 fprintf(stdout, "\t\tcase %u: \\\n", hash);
728 * Find all other entries that happen to match
729 * this hash.
731 for (j = 0; j <= maxtype; j++) {
732 ttn2 = find_typename(j);
733 if (ttn2 == NULL)
734 continue;
735 if (hash == HASH(ttn2->typename)) {
736 fprintf(stdout, "\t\t\tRDATATYPE_COMPARE"
737 "(\"%s\", %u, "
738 "_typename, _length, _typep); \\\n",
739 ttn2->typename, ttn2->type);
740 ttn2->sorted = 1;
743 fprintf(stdout, "\t\t\tbreak; \\\n");
745 fprintf(stdout, "\t}\n");
747 fprintf(stdout, "#define RDATATYPE_ATTRIBUTE_SW \\\n");
748 fprintf(stdout, "\tswitch (type) { \\\n");
749 for (i = 0; i <= maxtype; i++) {
750 ttn = find_typename(i);
751 if (ttn == NULL)
752 continue;
753 fprintf(stdout, "\tcase %u: return (%s); \\\n",
754 i, upper(ttn->attr));
756 fprintf(stdout, "\t}\n");
758 fprintf(stdout, "#define RDATATYPE_TOTEXT_SW \\\n");
759 fprintf(stdout, "\tswitch (type) { \\\n");
760 for (i = 0; i <= maxtype; i++) {
761 ttn = find_typename(i);
762 if (ttn == NULL)
763 continue;
764 fprintf(stdout, "\tcase %u: return "
765 "(str_totext(\"%s\", target)); \\\n",
766 i, upper(ttn->typename));
768 fprintf(stdout, "\t}\n");
770 fputs("#endif /* DNS_CODE_H */\n", stdout);
771 } else if (type_enum) {
772 char *s;
774 fprintf(stdout, "#ifndef DNS_ENUMTYPE_H\n");
775 fprintf(stdout, "#define DNS_ENUMTYPE_H 1\n\n");
777 fprintf(stdout, "enum {\n");
778 fprintf(stdout, "\tdns_rdatatype_none = 0,\n");
780 lasttype = 0;
781 for (tt = types; tt != NULL; tt = tt->next)
782 if (tt->type != lasttype)
783 fprintf(stdout,
784 "\tdns_rdatatype_%s = %d,\n",
785 funname(tt->typename, buf1),
786 lasttype = tt->type);
788 fprintf(stdout, "\tdns_rdatatype_ixfr = 251,\n");
789 fprintf(stdout, "\tdns_rdatatype_axfr = 252,\n");
790 fprintf(stdout, "\tdns_rdatatype_mailb = 253,\n");
791 fprintf(stdout, "\tdns_rdatatype_maila = 254,\n");
792 fprintf(stdout, "\tdns_rdatatype_any = 255\n");
794 fprintf(stdout, "};\n\n");
796 fprintf(stdout, "#define dns_rdatatype_none\t"
797 "((dns_rdatatype_t)dns_rdatatype_none)\n");
799 for (tt = types; tt != NULL; tt = tt->next)
800 if (tt->type != lasttype) {
801 s = funname(tt->typename, buf1);
802 fprintf(stdout,
803 "#define dns_rdatatype_%s\t%s"
804 "((dns_rdatatype_t)dns_rdatatype_%s)"
805 "\n",
806 s, strlen(s) < 2U ? "\t" : "", s);
807 lasttype = tt->type;
810 fprintf(stdout, "#define dns_rdatatype_ixfr\t"
811 "((dns_rdatatype_t)dns_rdatatype_ixfr)\n");
812 fprintf(stdout, "#define dns_rdatatype_axfr\t"
813 "((dns_rdatatype_t)dns_rdatatype_axfr)\n");
814 fprintf(stdout, "#define dns_rdatatype_mailb\t"
815 "((dns_rdatatype_t)dns_rdatatype_mailb)\n");
816 fprintf(stdout, "#define dns_rdatatype_maila\t"
817 "((dns_rdatatype_t)dns_rdatatype_maila)\n");
818 fprintf(stdout, "#define dns_rdatatype_any\t"
819 "((dns_rdatatype_t)dns_rdatatype_any)\n");
821 fprintf(stdout, "\n#endif /* DNS_ENUMTYPE_H */\n");
823 } else if (class_enum) {
824 char *s;
825 int classnum;
827 fprintf(stdout, "#ifndef DNS_ENUMCLASS_H\n");
828 fprintf(stdout, "#define DNS_ENUMCLASS_H 1\n\n");
830 fprintf(stdout, "enum {\n");
832 fprintf(stdout, "\tdns_rdataclass_reserved0 = 0,\n");
833 fprintf(stdout, "#define dns_rdataclass_reserved0 \\\n\t\t\t\t"
834 "((dns_rdataclass_t)dns_rdataclass_reserved0)\n");
836 #define PRINTCLASS(name, num) \
837 do { \
838 s = funname(name, buf1); \
839 classnum = num; \
840 fprintf(stdout, "\tdns_rdataclass_%s = %d%s\n", s, classnum, \
841 classnum != 255 ? "," : ""); \
842 fprintf(stdout, "#define dns_rdataclass_%s\t" \
843 "((dns_rdataclass_t)dns_rdataclass_%s)\n", s, s); \
844 } while (0)
846 for (cc = classes; cc != NULL; cc = cc->next) {
847 if (cc->rdclass == 3)
848 PRINTCLASS("chaos", 3);
849 else if (cc->rdclass == 255)
850 PRINTCLASS("none", 254);
851 PRINTCLASS(cc->classname, cc->rdclass);
854 #undef PRINTCLASS
856 fprintf(stdout, "};\n\n");
857 fprintf(stdout, "#endif /* DNS_ENUMCLASS_H */\n");
858 } else if (structs) {
859 if (prefix != NULL) {
860 if ((fd = fopen(prefix,"r")) != NULL) {
861 while (fgets(buf, sizeof(buf), fd) != NULL)
862 fputs(buf, stdout);
863 fclose(fd);
866 for (tt = types; tt != NULL; tt = tt->next) {
867 sprintf(buf, "%s/%s_%d.h",
868 tt->dirname, tt->typename, tt->type);
869 if ((fd = fopen(buf,"r")) != NULL) {
870 while (fgets(buf, sizeof(buf), fd) != NULL)
871 fputs(buf, stdout);
872 fclose(fd);
875 if (suffix != NULL) {
876 if ((fd = fopen(suffix,"r")) != NULL) {
877 while (fgets(buf, sizeof(buf), fd) != NULL)
878 fputs(buf, stdout);
879 fclose(fd);
882 } else if (depend) {
883 for (tt = types; tt != NULL; tt = tt->next)
884 fprintf(stdout, "%s:\t%s/%s_%d.h\n", file,
885 tt->dirname, tt->typename, tt->type);
888 if (ferror(stdout) != 0)
889 exit(1);
891 return (0);