/contrib/bind9/lib/dns/gen.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 890 lines · 725 code · 113 blank · 52 comment · 185 complexity · cc83ce1285b8f7cc4151d72efecdd875 MD5 · raw file

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