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