aboutsummaryrefslogtreecommitdiffstats
path: root/maketables
blob: 07264858d45203ffb99026b63fef8f1e4f459ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
#
# Copyright (c) 2008-2010, 2013 Wind River Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License version 2.1 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the Lesser GNU General Public License for more details.
#
# You should have received a copy of the Lesser GNU General Public License
# version 2.1 along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
#
"""convert tables.in files to enums, tables, and support code.

Inputs are a type name, prefix, and a list of columns, followed by a list of
names with optional "= value" suffixes, plus optional additional columns.
The names are used to create enums and a table of strings, as well as
to/from lookups between the ids and names.  If additional columns are
defined, each column (separated by ", ") is used to create an additional
table of the given name, and a lookup function from ids.  Example:
        foo: FFF; const char *bar = "GOZINTA"
        hello, "yah"
        world, "nope"
produces:
        typedef enum {
                FFF_UNKNOWN = -1,
                FFF_MIN = 0,
                FFF_NONE = 0,
                FFF_HELLO,
                FFF_WORLD,
                FFF_MAX
        } foo_id_t;
        extern const char *foo_name(foo_id_t id);
        extern foo_id_t foo_id(const char *name);
        extern const char *foo_bar(foo_id_t id);

such that foo_name(1) => "hello" and foo_bar(1) => "yah".  If there
is an assigned value for a column description, missing column values
yield that value, otherwise they yield "unknown".

Values out of range yield "unknown", and unrecognized names yield the
value -1.  Note that the "MAX" value is one more than the highest defined
value.  (This is for consistency with C array bounds.)
"""

import glob
import sys
import string
from templatefile import TemplateFile

class DataType:
    """a set of related DataItem objects"""

    def __init__(self, path):
        """read the first line of path, then make tuples of the rest"""
        source = file(path)
        definition = source.readline().rstrip()
        self.name, qualifiers = string.split(definition, ': ', 2)
        if '; ' in qualifiers:
            self.prefix, columns = string.split(qualifiers, '; ')
        else:
            self.prefix = qualifiers
            columns = []
        self.flags = False
        if len(columns):
            self.columns = []
            columns = string.split(columns, ', ')
            for col in columns:
                indexed = False
                if col.startswith("FLAGS"):
                    print("Flags: set for %s" % self.name)
                    self.flags = True
                    continue
                if col.startswith("INDEXED "):
                    col = col[8:]
                    indexed = True
                if "=" in col:
                    name, default = string.split(col, ' = ')
                else:
                    name, default = col, ""
                if " " in name:
                    words = string.split(name, ' ')
                    name = words[-1]
                    del words[-1]
                    type = ' '.join(words)
                else:
                    type = "char *"
                self.columns.append({"indexed":indexed, "type":type, "name":name, "value":default})
        else:
            self.columns = []
        self.data = []
        self.comments = []
        index = 1
        for line in source.readlines():
            item = {}
            if line.startswith('#'):
                self.comments.append(line.rstrip().replace('#', ''))
                continue
            # first entry on the line is the "real" name/id, following hunks
            # are additional columns
            cols = string.split(line.rstrip(), ', ')
            item["name"] = cols.pop(0)
            item["upper"] = item["name"].replace('-', '_').upper()
            column_list = []
            for col in self.columns:
                if len(cols) > 0:
                    value = cols.pop(0)
                    if col["indexed"]:
                        if not "max" in col:
                            col["max"] = value
                        if value > col["max"]:
                            col["max"] = value
                        if not "min" in col:
                            col["min"] = value
                        if value < col["min"]:
                            col["min"] = value
                    column_list.append({"name":col["name"], "value":value})
                else:
                    column_list.append({"name":col["name"], "value":col["value"]})
            item["cols"] = column_list
            item["index"] = index
            index = index + 1
            self.data.append(item)

    def __getitem__(self, key):
        """Make this object look like a dict for Templates to use"""
        attr = getattr(self, key)

        if callable(attr):
            return attr()
        else:
            return attr

    def __repr__(self):
        column = 0
        out = ""
        out += "type: %s_t" % self.name
        out += " (prefix '%s_ENUM')\n" % self.prefix
        for col in self.columns:
            out += "  extra column: %s %s (default %s)\n" % (col["type"], col["name"], col["value"])
        out += "   "
        for item in self.data:
            column = column + 1
            if column > 4 and column % 4 == 1:
                out += "\n   "
            out += "%-19s" % item["name"]
#           for col in item["cols"]:
#               out += "\t%s(%s)\n" % (col["name"], col["value"])
        return out

    def comment(self):
        if len(self.comments):
            return '/*' + '\n *'.join(self.comments) + ' */\n'
        else:
            return ''

    def names(self):
        return ',\n\t'.join('"%s"' % x["name"] for x in self.data)

    def enums(self):
        return ',\n\t'.join('%s_%s' % (self.prefix, x["upper"]) for x in self.data)

    def flag_enums(self):
        if not self.flags:
            return ""
        enum_lines = []
        enum_lines.append('typedef enum {') 
        prefix = self.prefix + 'F'
        for x in self.data:
            enum_lines.append('\t%s_%s = (1 << %s_%s),' %
                (prefix, x["upper"], self.prefix, x["upper"]))
        enum_lines.append('} pseudo_%s_f;' % self.name)
        return '\n'.join(enum_lines)

    def column_names(self):
        decl_lines = []
        column = 0
        for col in self.columns:
            decl_lines.append("static %s %s_id_to_%s[] = {" % (col["type"], self.name, col["name"]))
            decl_lines.append('\t%s,' % col["value"])
            for item in self.data:
                decl_lines.append('\t%s,' % item["cols"][column]["value"])
            decl_lines.append('\t0')
            decl_lines.append("};")
            if col["indexed"]:
                decl_lines.append("static int %s_%s_to_id[] = {" % (self.name, col["name"]))
                for item in self.data:
                    decl_lines.append('\t[%s] = %d,' % (item["cols"][column]["value"], item["index"]))
                decl_lines.append("};")
            column = column + 1
        return '\n'.join(decl_lines)

    def column_funcs(self):
        decl_lines = []
        for col in self.columns:
            decl_lines.append('extern %s' % col["type"])
            decl_lines.append('pseudo_%s_%s(pseudo_%s_t id) {' %
                (self.name, col["name"], self.name))
            decl_lines.append('\tif (id < 0 || id >= %s_MAX)' % (self.prefix))
            decl_lines.append('\t\treturn %s;' % col["value"])
            decl_lines.append('\treturn %s_id_to_%s[id];' %
                (self.name, col["name"]))
            decl_lines.append('}')
            if col["indexed"]:
                table_name = '%s_%s_to_id' % (self.name, col["name"])
                decl_lines.append('extern int')
                decl_lines.append('pseudo_%s_%s_id(%s val) {' %
                    (self.name, col["name"], col["type"]))
                decl_lines.append('\tif ((val < %s) || (val > %s)) {' % (col["min"], col["max"]))
                decl_lines.append('\t\treturn -1;')
                decl_lines.append('\t}')
                decl_lines.append('\tif (%s[val] != 0) {' % table_name)
                decl_lines.append('\t\treturn %s[val];' % table_name)
                decl_lines.append('\t}')
                decl_lines.append('\treturn -1;')
                decl_lines.append('}')
        return '\n'.join(decl_lines)

    def column_protos(self):
        decl_lines = []
        for col in self.columns:
            decl_lines.append('extern %s pseudo_%s_%s(pseudo_%s_t id);' %
                (col["type"], self.name, col["name"], self.name))
            if col["indexed"]:
                decl_lines.append('extern int pseudo_%s_%s_id(%s val);' %
                    (self.name, col["name"], col["type"]))
        return '\n'.join(decl_lines)

def main():
    """Read in function defintions, write out files based on templates."""
    datatypes = []
    templates = []

    # error checking helpfully provided by the exception handler
    copyright_file = open('guts/COPYRIGHT')
    TemplateFile.copyright = copyright_file.read()
    copyright_file.close()

    for path in glob.glob('table_templates/*'):
        try:
            template_file = TemplateFile(path)
            template_file.emit('copyright')
            template_file.emit('header')
            templates.append(template_file)
        except IOError:
            print("Invalid or malformed template %s.  Aborting." % path)
            exit(1)

    for filename in sys.argv[1:]:
        # read in the datatype
        sys.stdout.write("%s: " % filename)
        datatype = DataType(filename)
        datatypes.append(datatype)
        print(datatype.__repr__())
        print("")

    print("Writing datatypes...")
    for datatype in datatypes:
        # populate various tables and files with each datatype
        for template_file in templates:
            template_file.emit('body', datatype)
    print("done.  Cleaning up.")
    
    for template_file in templates:
        # clean up files
        template_file.emit('footer')
        template_file.close()

if __name__ == '__main__':
    main()