1226031Sstas#!/usr/local/bin/python
2226031Sstas# -*- coding: iso-8859-1 -*-
3226031Sstas
4226031Sstas# $Id$
5226031Sstas
6226031Sstas# Copyright (c) 2004 Kungliga Tekniska H��gskolan
7226031Sstas# (Royal Institute of Technology, Stockholm, Sweden).
8226031Sstas# All rights reserved.
9226031Sstas#
10226031Sstas# Redistribution and use in source and binary forms, with or without
11226031Sstas# modification, are permitted provided that the following conditions
12226031Sstas# are met:
13226031Sstas#
14226031Sstas# 1. Redistributions of source code must retain the above copyright
15226031Sstas#    notice, this list of conditions and the following disclaimer.
16226031Sstas#
17226031Sstas# 2. Redistributions in binary form must reproduce the above copyright
18226031Sstas#    notice, this list of conditions and the following disclaimer in the
19226031Sstas#    documentation and/or other materials provided with the distribution.
20226031Sstas#
21226031Sstas# 3. Neither the name of the Institute nor the names of its contributors
22226031Sstas#    may be used to endorse or promote products derived from this software
23226031Sstas#    without specific prior written permission.
24226031Sstas#
25226031Sstas# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26226031Sstas# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27226031Sstas# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28226031Sstas# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29226031Sstas# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30226031Sstas# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31226031Sstas# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32226031Sstas# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33226031Sstas# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34226031Sstas# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35226031Sstas# SUCH DAMAGE.
36226031Sstas
37226031Sstasimport re
38226031Sstasimport string
39226031Sstasimport sys
40226031Sstas
41226031Sstasimport generate
42226031Sstasimport rfc3454
43226031Sstasimport rfc4518
44226031Sstasimport stringprep
45226031Sstasimport util
46226031Sstas
47226031Sstasif len(sys.argv) != 3:
48226031Sstas    print "usage: %s rfc3454.txt out-dir" % sys.argv[0]
49226031Sstas    sys.exit(1)
50226031Sstas
51226031Sstastables = rfc3454.read(sys.argv[1])
52226031Sstast2 = rfc4518.read()
53226031Sstas
54226031Sstasfor x in t2.iterkeys():
55226031Sstas    tables[x] = t2[x]
56226031Sstas
57226031Sstasmap_list = stringprep.get_maplist()
58226031Sstas
59226031Sstasmap_h = generate.Header('%s/map_table.h' % sys.argv[2])
60226031Sstas
61226031Sstasmap_c = generate.Implementation('%s/map_table.c' % sys.argv[2])
62226031Sstas
63226031Sstasmap_h.file.write(
64226031Sstas'''
65226031Sstas#include "windlocl.h"
66226031Sstas
67226031Sstasstruct translation {
68226031Sstas  uint32_t key;
69226031Sstas  unsigned short val_len;
70226031Sstas  unsigned short val_offset;
71226031Sstas  wind_profile_flags flags;
72226031Sstas};
73226031Sstas
74226031Sstasextern const struct translation _wind_map_table[];
75226031Sstas
76226031Sstasextern const size_t _wind_map_table_size;
77226031Sstas
78226031Sstasextern const uint32_t _wind_map_table_val[];
79226031Sstas
80226031Sstas''')
81226031Sstas
82226031Sstasmap_c.file.write(
83226031Sstas'''
84226031Sstas#include "map_table.h"
85226031Sstas
86226031Sstasconst struct translation _wind_map_table[] = {
87226031Sstas''')
88226031Sstas
89226031Sstastrans=[]
90226031Sstas
91226031Sstasfor t in map_list.iterkeys():
92226031Sstas    for l in tables[t]:
93226031Sstas        m = re.search('^ *([0-9A-F]+)-([0-9A-F]+); *([^;]+); *(.*) *$', l)
94226031Sstas        if m:
95226031Sstas            start = int(m.group(1), 0x10)
96226031Sstas            end   = int(m.group(2), 0x10)
97226031Sstas            value = m.group(3)
98226031Sstas            desc  = m.group(4)
99226031Sstas            for key in xrange(start,end,1):
100226031Sstas                trans.append((key, value, desc, [t]))
101226031Sstas            continue
102226031Sstas        m = re.search('^ *([^;]+); *([^;]+); *(.*) *$', l)
103226031Sstas        if m:
104226031Sstas            key   = int(m.group(1), 0x10)
105226031Sstas            value = m.group(2)
106226031Sstas            desc  = m.group(3)
107226031Sstas            trans.append((key, value, desc, [t]))
108226031Sstas            continue
109226031Sstas
110226031SstasvalTable = []
111226031SstasoffsetTable = {}
112226031Sstas
113226031Sstastrans = stringprep.sort_merge_trans(trans)
114226031Sstas
115226031Sstasfor x in trans:
116226031Sstas    if x[0] == 0xad:
117226031Sstas        print "fooresult %s" % ",".join(x[3])
118226031Sstas
119226031Sstasfor x in trans:
120226031Sstas    (key, value, description, table) = x
121226031Sstas    v = value.split()
122226031Sstas    i = util.subList(valTable, v)
123226031Sstas    if i:
124226031Sstas        offsetTable[key] = i
125226031Sstas    else:
126226031Sstas        offsetTable[key] = len(valTable)
127226031Sstas        valTable.extend(v)
128226031Sstas
129226031Sstasfor x in trans:
130226031Sstas    (key, value, description, tables) = x
131226031Sstas    symbols = stringprep.symbols(map_list, tables)
132226031Sstas    if len(symbols) == 0:
133226031Sstas        print "no symbol for %s %s (%s)" % (key, description, tables)
134226031Sstas        sys.exit(1)
135226031Sstas    v = value.split()
136226031Sstas    map_c.file.write("  {0x%x, %u, %u, %s}, /* %s: %s */\n"
137226031Sstas                % (key, len(v), offsetTable[key], symbols, ",".join(tables), description))
138226031Sstas
139226031Sstasmap_c.file.write(
140226031Sstas'''
141226031Sstas};
142226031Sstas
143226031Sstas''')
144226031Sstas
145226031Sstasmap_c.file.write(
146226031Sstas    "const size_t _wind_map_table_size = %u;\n\n" % len(trans))
147226031Sstas
148226031Sstasmap_c.file.write(
149226031Sstas    "const uint32_t _wind_map_table_val[] = {\n")
150226031Sstas
151226031Sstasfor x in valTable:
152226031Sstas    map_c.file.write("  0x%s,\n" % x)
153226031Sstas
154226031Sstasmap_c.file.write(
155226031Sstas    "};\n\n")
156226031Sstas
157226031Sstasmap_h.close()
158226031Sstasmap_c.close()
159