gen-errorlist.py revision 233294
1226586Sdim#!/usr/local/bin/python
2226586Sdim# -*- coding: iso-8859-1 -*-
3226586Sdim
4226586Sdim# $Id$
5226586Sdim
6226586Sdim# Copyright (c) 2004 Kungliga Tekniska H��gskolan
7226586Sdim# (Royal Institute of Technology, Stockholm, Sweden).
8226586Sdim# All rights reserved.
9226586Sdim#
10226586Sdim# Redistribution and use in source and binary forms, with or without
11226586Sdim# modification, are permitted provided that the following conditions
12226586Sdim# are met:
13226586Sdim#
14226586Sdim# 1. Redistributions of source code must retain the above copyright
15226586Sdim#    notice, this list of conditions and the following disclaimer.
16226586Sdim#
17226586Sdim# 2. Redistributions in binary form must reproduce the above copyright
18226586Sdim#    notice, this list of conditions and the following disclaimer in the
19226586Sdim#    documentation and/or other materials provided with the distribution.
20226586Sdim#
21226586Sdim# 3. Neither the name of the Institute nor the names of its contributors
22226586Sdim#    may be used to endorse or promote products derived from this software
23226586Sdim#    without specific prior written permission.
24226586Sdim#
25226586Sdim# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26226586Sdim# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27226586Sdim# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28226586Sdim# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29226586Sdim# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30226586Sdim# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31226586Sdim# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32226586Sdim# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33226586Sdim# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34226586Sdim# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35226586Sdim# SUCH DAMAGE.
36226586Sdim
37226586Sdimimport re
38226586Sdimimport string
39226586Sdimimport sys
40226586Sdim
41226586Sdimimport generate
42226586Sdimimport rfc3454
43226586Sdimimport rfc4518
44226586Sdimimport stringprep
45226586Sdim
46226586Sdimif len(sys.argv) != 3:
47226586Sdim    print "usage: %s rfc3454.txt out-dir" % sys.argv[0]
48226586Sdim    sys.exit(1)
49226586Sdim
50226586Sdimtables = rfc3454.read(sys.argv[1])
51226586Sdimt2 = rfc4518.read()
52226586Sdim
53226586Sdimfor x in t2.iterkeys():
54226586Sdim    tables[x] = t2[x]
55226586Sdim
56226586Sdimerror_list = stringprep.get_errorlist()
57226586Sdim
58226586Sdimerrorlist_h = generate.Header('%s/errorlist_table.h' % sys.argv[2])
59226586Sdim
60226586Sdimerrorlist_c = generate.Implementation('%s/errorlist_table.c' % sys.argv[2])
61226586Sdim
62226586Sdimerrorlist_h.file.write(
63226586Sdim'''
64226586Sdim#include "windlocl.h"
65226586Sdim
66226586Sdimstruct error_entry {
67226586Sdim  uint32_t start;
68226586Sdim  unsigned len;
69226586Sdim  wind_profile_flags flags;
70226586Sdim};
71226586Sdim
72226586Sdimextern const struct error_entry _wind_errorlist_table[];
73226586Sdim
74226586Sdimextern const size_t _wind_errorlist_table_size;
75226586Sdim
76226586Sdim''')
77226586Sdim
78226586Sdimerrorlist_c.file.write(
79226586Sdim'''
80226586Sdim#include <stdlib.h>
81226586Sdim#include "errorlist_table.h"
82226586Sdim
83226586Sdimconst struct error_entry _wind_errorlist_table[] = {
84226586Sdim''')
85226586Sdim
86226586Sdimtrans=[]
87226586Sdim
88226586Sdimfor t in error_list.iterkeys():
89226586Sdim    for l in tables[t]:
90226586Sdim        m = re.search('^ *([0-9A-F]+)-([0-9A-F]+); *(.*) *$', l)
91226586Sdim        if m:
92226586Sdim            start = int(m.group(1), 0x10)
93226586Sdim            end   = int(m.group(2), 0x10)
94226586Sdim            desc  = m.group(3)
95226586Sdim            trans.append([start, end - start + 1, desc, [t]])
96226586Sdim        else:
97226586Sdim            m = re.search('^ *([0-9A-F]+); *(.*) *$', l)
98226586Sdim            if m:
99226586Sdim                trans.append([int(m.group(1), 0x10), 1, m.group(2), [t]])
100226586Sdim
101226586Sdimtrans = stringprep.sort_merge_trans(trans)
102226586Sdim
103226586Sdimfor x in trans:
104226586Sdim    (start, length, description, tables) = x
105226586Sdim    symbols = stringprep.symbols(error_list, tables)
106226586Sdim    if len(symbols) == 0:
107226586Sdim        print "no symbol for %s" % description
108226586Sdim        sys.exit(1)
109226586Sdim    errorlist_c.file.write("  {0x%x, 0x%x, %s}, /* %s: %s */\n"
110226586Sdim                % (start, length, symbols, ",".join(tables), description))
111226586Sdim
112226586Sdimerrorlist_c.file.write(
113226586Sdim'''};
114226586Sdim
115226586Sdim''')
116226586Sdim
117226586Sdimerrorlist_c.file.write(
118226586Sdim    "const size_t _wind_errorlist_table_size = %u;\n" % len(trans))
119226586Sdim
120226586Sdimerrorlist_h.close()
121226586Sdimerrorlist_c.close()
122226586Sdim