C2x scanf %b support
[glibc.git] / scripts / gen-rrtypes.py
blob0e2e0c49d31b80a003dfa74c53861c2d85ad79d3
1 #!/usr/bin/python3
2 # Generate DNS RR type constants for resolv header files.
3 # Copyright (C) 2016-2023 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, see
18 # <https://www.gnu.org/licenses/>.
20 """Generate DNS RR type constants for resolv header files.
22 resolv/arpa/nameser.h and resolv/arpa/nameser_compat.h contain lists
23 of RR type constants. This script downloads the current definitions
24 from the IANA DNS Parameters protocol registry and translates it into
25 the two different lists.
27 Two lists are written to standard output. The first one contains enum
28 constants for resolv/arpa/nameser.h. The second one lists the
29 preprocessor macros for resolv/arpa/nameser_compat.h.
31 """
33 # URL of the IANA registry.
34 source = "http://www.iana.org/assignments/dns-parameters/dns-parameters-4.csv"
36 import collections
37 import csv
38 import io
39 import urllib.request
41 Type = collections.namedtuple("Type", "name number comment")
43 def get_types(source):
44 for row in csv.reader(io.TextIOWrapper(urllib.request.urlopen(source))):
45 if row[0] in ('TYPE', 'Unassigned', 'Private use', 'Reserved'):
46 continue
47 name, number, comment = row[:3]
48 if name == '*':
49 name = 'ANY'
50 comment = 'request for all cached records'
51 number = int(number)
52 yield Type(name, number, comment)
54 types = list(get_types(source))
56 print("// enum constants for resolv/arpa/nameser.h")
57 print()
58 for typ in types:
59 name = typ.name.replace("-", "_").lower()
60 print(" ns_t_{0} = {1.number},".format(name, typ))
61 print()
63 print("// macro aliases resolv/arpa/nameser_compat.h")
64 print()
65 for typ in types:
66 name = typ.name.replace("-", "_")
67 print("#define T_{0} ns_t_{1}".format(name.upper(), name.lower()))
68 print()