Comparison

tools/dnsregistry.lua @ 12237:b35714c57442

tools.dnsregistry: For converting IANA DNS registry data to Lua table
author Kim Alvefur <zash@zash.se>
date Sun, 04 Oct 2020 19:26:53 +0200
child 12271:f31bb79f51d7
comparison
equal deleted inserted replaced
12236:d0dfd48806f9 12237:b35714c57442
1 -- Generate util/dnsregistry.lua from IANA HTTP status code registry
2 local xml = require "util.xml";
3 local registries = xml.parse(io.read("*a"));
4
5 print("-- Source: https://www.iana.org/assignments/dns-parameters/dns-parameters.xml");
6 print(os.date("-- Generated on %Y-%m-%d"))
7
8 local registry_mapping = {
9 ["dns-parameters-2"] = "classes";
10 ["dns-parameters-4"] = "types";
11 ["dns-parameters-6"] = "errors";
12 };
13
14 print("return {");
15 for registry in registries:childtags("registry") do
16 local registry_name = registry_mapping[registry.attr.id];
17 if registry_name then
18 print("\t" .. registry_name .. " = {");
19 for record in registry:childtags("record") do
20 local record_name = record:get_child_text("name");
21 local record_type = record:get_child_text("type");
22 local record_desc = record:get_child_text("description");
23 local record_code = tonumber(record:get_child_text("value"));
24
25 if tostring(record):lower():match("reserved") or tostring(record):lower():match("reserved") then
26 record_code = nil;
27 end
28
29 if registry_name == "classes" and record_code then
30 record_type = record_desc and record_desc:match("%((%w+)%)$")
31 if record_type then
32 print(("\t\t[%q] = %d; [%d] = %q;"):format(record_type, record_code, record_code, record_type))
33 end
34 elseif registry_name == "types" and record_type and record_code then
35 print(("\t\t[%q] = %d; [%d] = %q;"):format(record_type, record_code, record_code, record_type))
36 elseif registry_name == "errors" and record_code and record_name then
37 print(("\t\t[%d] = %q; [%q] = %q;"):format(record_code, record_name, record_name, record_desc or record_name));
38 end
39 end
40 print("\t};");
41 end
42 end
43 print("};");