]>
Commit | Line | Data |
---|---|---|
d879a707 BP |
1 | #! @PYTHON@ |
2 | ||
3 | import getopt | |
00732bf5 | 4 | import os |
d879a707 | 5 | import re |
d879a707 BP |
6 | import sys |
7 | ||
99155935 BP |
8 | import ovs.json |
9 | import ovs.db.error | |
10 | import ovs.db.schema | |
d879a707 | 11 | |
89365653 | 12 | argv0 = sys.argv[0] |
45a7de56 | 13 | |
d879a707 | 14 | def parseSchema(filename): |
99155935 | 15 | return ovs.db.schema.IdlSchema.from_json(ovs.json.from_file(filename)) |
00732bf5 BP |
16 | |
17 | def annotateSchema(schemaFile, annotationFile): | |
99155935 | 18 | schemaJson = ovs.json.from_file(schemaFile) |
00732bf5 | 19 | execfile(annotationFile, globals(), {"s": schemaJson}) |
99155935 | 20 | ovs.json.to_stream(schemaJson, sys.stdout) |
d879a707 | 21 | |
02630ff2 | 22 | def constify(cType, const): |
c667d679 | 23 | if (const and cType.endswith('*') and not cType.endswith('**')): |
02630ff2 BP |
24 | return 'const %s' % cType |
25 | else: | |
26 | return cType | |
27 | ||
28 | def cMembers(prefix, columnName, column, const): | |
475281c0 | 29 | type = column.type |
99155935 | 30 | if type.n_min == 1 and type.n_max == 1: |
475281c0 BP |
31 | singleton = True |
32 | pointer = '' | |
33 | else: | |
34 | singleton = False | |
99155935 | 35 | if type.is_optional_pointer(): |
475281c0 BP |
36 | pointer = '' |
37 | else: | |
38 | pointer = '*' | |
39 | ||
40 | if type.value: | |
41 | key = {'name': "key_%s" % columnName, | |
bd76d25d | 42 | 'type': constify(type.key.toCType(prefix) + pointer, const), |
475281c0 BP |
43 | 'comment': ''} |
44 | value = {'name': "value_%s" % columnName, | |
bd76d25d | 45 | 'type': constify(type.value.toCType(prefix) + pointer, const), |
475281c0 BP |
46 | 'comment': ''} |
47 | members = [key, value] | |
48 | else: | |
49 | m = {'name': columnName, | |
bd76d25d BP |
50 | 'type': constify(type.key.toCType(prefix) + pointer, const), |
51 | 'comment': type.cDeclComment()} | |
475281c0 BP |
52 | members = [m] |
53 | ||
99155935 | 54 | if not singleton and not type.is_optional_pointer(): |
475281c0 BP |
55 | members.append({'name': 'n_%s' % columnName, |
56 | 'type': 'size_t ', | |
57 | 'comment': ''}) | |
58 | return members | |
59 | ||
00732bf5 BP |
60 | def printCIDLHeader(schemaFile): |
61 | schema = parseSchema(schemaFile) | |
c3bb4bd7 | 62 | prefix = schema.idlPrefix |
d879a707 BP |
63 | print '''\ |
64 | /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */ | |
65 | ||
66 | #ifndef %(prefix)sIDL_HEADER | |
67 | #define %(prefix)sIDL_HEADER 1 | |
68 | ||
69 | #include <stdbool.h> | |
70 | #include <stddef.h> | |
71 | #include <stdint.h> | |
a6ec9089 | 72 | #include "ovsdb-data.h" |
c3bb4bd7 | 73 | #include "ovsdb-idl-provider.h" |
d879a707 | 74 | #include "uuid.h"''' % {'prefix': prefix.upper()} |
979821c0 | 75 | |
bd76d25d | 76 | for tableName, table in sorted(schema.tables.iteritems()): |
c3bb4bd7 | 77 | structName = "%s%s" % (prefix, tableName.lower()) |
979821c0 BP |
78 | |
79 | print "\f" | |
80 | print "/* %s table. */" % tableName | |
c3bb4bd7 BP |
81 | print "struct %s {" % structName |
82 | print "\tstruct ovsdb_idl_row header_;" | |
bd76d25d | 83 | for columnName, column in sorted(table.columns.iteritems()): |
d879a707 | 84 | print "\n\t/* %s column. */" % columnName |
02630ff2 | 85 | for member in cMembers(prefix, columnName, column, False): |
475281c0 | 86 | print "\t%(type)s%(name)s;%(comment)s" % member |
979821c0 | 87 | print "};" |
c3bb4bd7 | 88 | |
979821c0 BP |
89 | # Column indexes. |
90 | printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper()) | |
bd76d25d | 91 | for columnName in sorted(table.columns)] |
979821c0 BP |
92 | + ["%s_N_COLUMNS" % structName.upper()]) |
93 | ||
94 | ||
95 | for columnName in table.columns: | |
96 | print "#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % { | |
97 | 's': structName, | |
98 | 'S': structName.upper(), | |
99 | 'c': columnName, | |
100 | 'C': columnName.upper()} | |
101 | ||
102 | print "\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper()) | |
103 | ||
104 | print ''' | |
c3bb4bd7 BP |
105 | const struct %(s)s *%(s)s_first(const struct ovsdb_idl *); |
106 | const struct %(s)s *%(s)s_next(const struct %(s)s *); | |
58bc1a52 BP |
107 | #define %(S)s_FOR_EACH(ROW, IDL) \\ |
108 | for ((ROW) = %(s)s_first(IDL); \\ | |
109 | (ROW); \\ | |
110 | (ROW) = %(s)s_next(ROW)) | |
111 | #define %(S)s_FOR_EACH_SAFE(ROW, NEXT, IDL) \\ | |
112 | for ((ROW) = %(s)s_first(IDL); \\ | |
113 | (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\ | |
114 | (ROW) = (NEXT)) | |
475281c0 BP |
115 | |
116 | void %(s)s_delete(const struct %(s)s *); | |
117 | struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *); | |
118 | ''' % {'s': structName, 'S': structName.upper()} | |
119 | ||
bd76d25d | 120 | for columnName, column in sorted(table.columns.iteritems()): |
475281c0 BP |
121 | print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName} |
122 | ||
a6ec9089 BP |
123 | print """ |
124 | /* Functions for fetching columns as \"struct ovsdb_datum\"s. (This is | |
125 | rarely useful. More often, it is easier to access columns by using | |
126 | the members of %(s)s directly.) */""" % {'s': structName} | |
127 | for columnName, column in sorted(table.columns.iteritems()): | |
128 | if column.type.value: | |
129 | valueParam = ', enum ovsdb_atomic_type value_type' | |
130 | else: | |
131 | valueParam = '' | |
132 | print 'const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % { | |
133 | 's': structName, 'c': columnName, 'v': valueParam} | |
134 | ||
475281c0 | 135 | |
bd76d25d | 136 | for columnName, column in sorted(table.columns.iteritems()): |
475281c0 BP |
137 | |
138 | print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName}, | |
139 | args = ['%(type)s%(name)s' % member for member | |
02630ff2 | 140 | in cMembers(prefix, columnName, column, True)] |
475281c0 BP |
141 | print '%s);' % ', '.join(args) |
142 | ||
979821c0 | 143 | # Table indexes. |
bd76d25d | 144 | printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()]) |
979821c0 BP |
145 | |
146 | for tableName in schema.tables: | |
147 | print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % { | |
148 | 'p': prefix, | |
149 | 'P': prefix.upper(), | |
150 | 't': tableName.lower(), | |
151 | 'T': tableName.upper()} | |
152 | print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper()) | |
153 | ||
c3bb4bd7 | 154 | print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix |
bd76d25d | 155 | print "\nvoid %sinit(void);" % prefix |
c3bb4bd7 BP |
156 | print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()} |
157 | ||
158 | def printEnum(members): | |
159 | if len(members) == 0: | |
160 | return | |
161 | ||
162 | print "\nenum {"; | |
163 | for member in members[:-1]: | |
164 | print " %s," % member | |
165 | print " %s" % members[-1] | |
166 | print "};" | |
167 | ||
00732bf5 BP |
168 | def printCIDLSource(schemaFile): |
169 | schema = parseSchema(schemaFile) | |
c3bb4bd7 BP |
170 | prefix = schema.idlPrefix |
171 | print '''\ | |
172 | /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */ | |
173 | ||
174 | #include <config.h> | |
175 | #include %s | |
bd76d25d | 176 | #include <assert.h> |
c3bb4bd7 | 177 | #include <limits.h> |
bd76d25d BP |
178 | #include "ovsdb-data.h" |
179 | #include "ovsdb-error.h" | |
180 | ||
181 | static bool inited; | |
89521e3f | 182 | ''' % schema.idlHeader |
c3bb4bd7 | 183 | |
66095e15 | 184 | # Cast functions. |
bd76d25d | 185 | for tableName, table in sorted(schema.tables.iteritems()): |
66095e15 BP |
186 | structName = "%s%s" % (prefix, tableName.lower()) |
187 | print ''' | |
188 | static struct %(s)s * | |
979821c0 | 189 | %(s)s_cast(const struct ovsdb_idl_row *row) |
66095e15 BP |
190 | { |
191 | return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL; | |
192 | }\ | |
193 | ''' % {'s': structName} | |
194 | ||
195 | ||
bd76d25d | 196 | for tableName, table in sorted(schema.tables.iteritems()): |
c3bb4bd7 BP |
197 | structName = "%s%s" % (prefix, tableName.lower()) |
198 | print "\f" | |
2e57b537 | 199 | print "/* %s table. */" % (tableName) |
c3bb4bd7 | 200 | |
979821c0 | 201 | # Parse functions. |
bd76d25d | 202 | for columnName, column in sorted(table.columns.iteritems()): |
979821c0 | 203 | print ''' |
c3bb4bd7 | 204 | static void |
979821c0 | 205 | %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum) |
c3bb4bd7 | 206 | { |
979821c0 BP |
207 | struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName, |
208 | 'c': columnName} | |
c3bb4bd7 | 209 | |
c3bb4bd7 | 210 | type = column.type |
c3bb4bd7 BP |
211 | if type.value: |
212 | keyVar = "row->key_%s" % columnName | |
213 | valueVar = "row->value_%s" % columnName | |
214 | else: | |
215 | keyVar = "row->%s" % columnName | |
216 | valueVar = None | |
217 | ||
99155935 | 218 | if (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer(): |
979821c0 | 219 | |
bd76d25d | 220 | print " assert(inited);" |
c3bb4bd7 | 221 | print " if (datum->n >= 1) {" |
99155935 BP |
222 | if not type.key.ref_table: |
223 | print " %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string()) | |
c3bb4bd7 | 224 | else: |
99155935 | 225 | print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[0].uuid));" % (keyVar, prefix, type.key.ref_table.lower(), prefix, prefix.upper(), type.key.ref_table.upper()) |
c3bb4bd7 BP |
226 | |
227 | if valueVar: | |
99155935 BP |
228 | if type.value.ref_table: |
229 | print " %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string()) | |
c3bb4bd7 | 230 | else: |
99155935 | 231 | print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[0].uuid));" % (valueVar, prefix, type.value.ref_table.lower(), prefix, prefix.upper(), type.value.ref_table.upper()) |
979821c0 | 232 | print " } else {" |
99155935 | 233 | print " %s" % type.key.initCDefault(keyVar, type.n_min == 0) |
979821c0 | 234 | if valueVar: |
99155935 | 235 | print " %s" % type.value.initCDefault(valueVar, type.n_min == 0) |
c3bb4bd7 BP |
236 | print " }" |
237 | else: | |
99155935 BP |
238 | if type.n_max != sys.maxint: |
239 | print " size_t n = MIN(%d, datum->n);" % type.n_max | |
979821c0 | 240 | nMax = "n" |
c3bb4bd7 BP |
241 | else: |
242 | nMax = "datum->n" | |
979821c0 BP |
243 | print " size_t i;" |
244 | ||
bd76d25d | 245 | print " assert(inited);" |
979821c0 BP |
246 | print " %s = NULL;" % keyVar |
247 | if valueVar: | |
248 | print " %s = NULL;" % valueVar | |
249 | print " row->n_%s = 0;" % columnName | |
c3bb4bd7 BP |
250 | print " for (i = 0; i < %s; i++) {" % nMax |
251 | refs = [] | |
99155935 BP |
252 | if type.key.ref_table: |
253 | print " struct %s%s *keyRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[i].uuid));" % (prefix, type.key.ref_table.lower(), prefix, type.key.ref_table.lower(), prefix, prefix.upper(), type.key.ref_table.upper()) | |
c3bb4bd7 BP |
254 | keySrc = "keyRow" |
255 | refs.append('keyRow') | |
256 | else: | |
99155935 BP |
257 | keySrc = "datum->keys[i].%s" % type.key.type.to_string() |
258 | if type.value and type.value.ref_table: | |
259 | print " struct %s%s *valueRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[i].uuid));" % (prefix, type.value.ref_table.lower(), prefix, type.value.ref_table.lower(), prefix, prefix.upper(), type.value.ref_table.upper()) | |
c3bb4bd7 BP |
260 | valueSrc = "valueRow" |
261 | refs.append('valueRow') | |
262 | elif valueVar: | |
99155935 | 263 | valueSrc = "datum->values[i].%s" % type.value.type.to_string() |
c3bb4bd7 BP |
264 | if refs: |
265 | print " if (%s) {" % ' && '.join(refs) | |
266 | indent = " " | |
267 | else: | |
268 | indent = " " | |
269 | print "%sif (!row->n_%s) {" % (indent, columnName) | |
270 | print "%s %s = xmalloc(%s * sizeof *%s);" % (indent, keyVar, nMax, keyVar) | |
271 | if valueVar: | |
d78ac388 | 272 | print "%s %s = xmalloc(%s * sizeof *%s);" % (indent, valueVar, nMax, valueVar) |
c3bb4bd7 BP |
273 | print "%s}" % indent |
274 | print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc) | |
275 | if valueVar: | |
66095e15 | 276 | print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc) |
c3bb4bd7 BP |
277 | print "%srow->n_%s++;" % (indent, columnName) |
278 | if refs: | |
279 | print " }" | |
280 | print " }" | |
979821c0 | 281 | print "}" |
c3bb4bd7 | 282 | |
979821c0 | 283 | # Unparse functions. |
bd76d25d | 284 | for columnName, column in sorted(table.columns.iteritems()): |
c3bb4bd7 | 285 | type = column.type |
99155935 | 286 | if (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer(): |
979821c0 | 287 | print ''' |
c3bb4bd7 | 288 | static void |
979821c0 | 289 | %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_) |
c3bb4bd7 | 290 | { |
979821c0 | 291 | struct %(s)s *row = %(s)s_cast(row_); |
bd76d25d BP |
292 | |
293 | assert(inited);''' % {'s': structName, 'c': columnName} | |
c3bb4bd7 BP |
294 | if type.value: |
295 | keyVar = "row->key_%s" % columnName | |
296 | valueVar = "row->value_%s" % columnName | |
297 | else: | |
298 | keyVar = "row->%s" % columnName | |
299 | valueVar = None | |
300 | print " free(%s);" % keyVar | |
301 | if valueVar: | |
302 | print " free(%s);" % valueVar | |
979821c0 BP |
303 | print '}' |
304 | else: | |
305 | print ''' | |
c3bb4bd7 | 306 | static void |
c69ee87c | 307 | %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED) |
979821c0 BP |
308 | { |
309 | /* Nothing to do. */ | |
310 | }''' % {'s': structName, 'c': columnName} | |
c5c7c7c5 | 311 | |
c3bb4bd7 BP |
312 | # First, next functions. |
313 | print ''' | |
66095e15 BP |
314 | const struct %(s)s * |
315 | %(s)s_first(const struct ovsdb_idl *idl) | |
c3bb4bd7 | 316 | { |
66095e15 | 317 | return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s])); |
c3bb4bd7 BP |
318 | } |
319 | ||
66095e15 BP |
320 | const struct %(s)s * |
321 | %(s)s_next(const struct %(s)s *row) | |
c3bb4bd7 | 322 | { |
66095e15 | 323 | return %(s)s_cast(ovsdb_idl_next_row(&row->header_)); |
475281c0 BP |
324 | }''' % {'s': structName, |
325 | 'p': prefix, | |
326 | 'P': prefix.upper(), | |
327 | 'T': tableName.upper()} | |
328 | ||
329 | print ''' | |
330 | void | |
9e336f49 | 331 | %(s)s_delete(const struct %(s)s *row) |
475281c0 | 332 | { |
475281c0 BP |
333 | ovsdb_idl_txn_delete(&row->header_); |
334 | } | |
335 | ||
336 | struct %(s)s * | |
337 | %(s)s_insert(struct ovsdb_idl_txn *txn) | |
338 | { | |
ce5a3e38 | 339 | return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s], NULL)); |
475281c0 BP |
340 | } |
341 | ''' % {'s': structName, | |
342 | 'p': prefix, | |
343 | 'P': prefix.upper(), | |
344 | 'T': tableName.upper()} | |
345 | ||
346 | # Verify functions. | |
bd76d25d | 347 | for columnName, column in sorted(table.columns.iteritems()): |
475281c0 BP |
348 | print ''' |
349 | void | |
350 | %(s)s_verify_%(c)s(const struct %(s)s *row) | |
351 | { | |
bd76d25d | 352 | assert(inited); |
475281c0 BP |
353 | ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]); |
354 | }''' % {'s': structName, | |
355 | 'S': structName.upper(), | |
356 | 'c': columnName, | |
357 | 'C': columnName.upper()} | |
358 | ||
a6ec9089 BP |
359 | # Get functions. |
360 | for columnName, column in sorted(table.columns.iteritems()): | |
361 | if column.type.value: | |
362 | valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED' | |
363 | valueType = '\n assert(value_type == %s);' % column.type.value.toAtomicType() | |
364 | valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType() | |
365 | else: | |
366 | valueParam = '' | |
367 | valueType = '' | |
368 | valueComment = '' | |
369 | print """ | |
370 | /* Returns the %(c)s column's value in 'row' as a struct ovsdb_datum. | |
371 | * This is useful occasionally: for example, ovsdb_datum_find_key() is an | |
372 | * easier and more efficient way to search for a given key than implementing | |
373 | * the same operation on the "cooked" form in 'row'. | |
374 | * | |
375 | * 'key_type' must be %(kt)s.%(vc)s | |
376 | * (This helps to avoid silent bugs if someone changes %(c)s's | |
377 | * type without updating the caller.) | |
378 | * | |
379 | * The caller must not modify or free the returned value. | |
380 | * | |
381 | * Various kinds of changes can invalidate the returned value: modifying | |
382 | * 'column' within 'row', deleting 'row', or completing an ongoing transaction. | |
383 | * If the returned value is needed for a long time, it is best to make a copy | |
384 | * of it with ovsdb_datum_clone(). */ | |
385 | const struct ovsdb_datum * | |
386 | %(s)s_get_%(c)s(const struct %(s)s *row, | |
387 | \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s) | |
388 | { | |
389 | assert(key_type == %(kt)s);%(vt)s | |
390 | return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s); | |
391 | }""" % {'s': structName, 'c': columnName, | |
392 | 'kt': column.type.key.toAtomicType(), | |
393 | 'v': valueParam, 'vt': valueType, 'vc': valueComment} | |
394 | ||
475281c0 | 395 | # Set functions. |
bd76d25d | 396 | for columnName, column in sorted(table.columns.iteritems()): |
475281c0 BP |
397 | type = column.type |
398 | print '\nvoid' | |
02630ff2 | 399 | members = cMembers(prefix, columnName, column, True) |
475281c0 BP |
400 | keyVar = members[0]['name'] |
401 | nVar = None | |
402 | valueVar = None | |
403 | if type.value: | |
404 | valueVar = members[1]['name'] | |
405 | if len(members) > 2: | |
406 | nVar = members[2]['name'] | |
407 | else: | |
408 | if len(members) > 1: | |
409 | nVar = members[1]['name'] | |
979821c0 | 410 | print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \ |
475281c0 BP |
411 | {'s': structName, 'c': columnName, |
412 | 'args': ', '.join(['%(type)s%(name)s' % m for m in members])} | |
413 | print "{" | |
475281c0 | 414 | print " struct ovsdb_datum datum;" |
99155935 | 415 | if type.n_min == 1 and type.n_max == 1: |
475281c0 | 416 | |
bd76d25d | 417 | print " assert(inited);" |
475281c0 BP |
418 | print " datum.n = 1;" |
419 | print " datum.keys = xmalloc(sizeof *datum.keys);" | |
99155935 | 420 | print " " + type.key.copyCValue("datum.keys[0].%s" % type.key.type.to_string(), keyVar) |
475281c0 BP |
421 | if type.value: |
422 | print " datum.values = xmalloc(sizeof *datum.values);" | |
99155935 | 423 | print " "+ type.value.copyCValue("datum.values[0].%s" % type.value.type.to_string(), valueVar) |
475281c0 BP |
424 | else: |
425 | print " datum.values = NULL;" | |
99155935 | 426 | elif type.is_optional_pointer(): |
475281c0 | 427 | |
bd76d25d | 428 | print " assert(inited);" |
475281c0 BP |
429 | print " if (%s) {" % keyVar |
430 | print " datum.n = 1;" | |
431 | print " datum.keys = xmalloc(sizeof *datum.keys);" | |
99155935 | 432 | print " " + type.key.copyCValue("datum.keys[0].%s" % type.key.type.to_string(), keyVar) |
475281c0 BP |
433 | print " } else {" |
434 | print " datum.n = 0;" | |
435 | print " datum.keys = NULL;" | |
436 | print " }" | |
437 | print " datum.values = NULL;" | |
438 | else: | |
439 | print " size_t i;" | |
440 | ||
bd76d25d | 441 | print " assert(inited);" |
475281c0 BP |
442 | print " datum.n = %s;" % nVar |
443 | print " datum.keys = xmalloc(%s * sizeof *datum.keys);" % nVar | |
444 | if type.value: | |
445 | print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar | |
446 | else: | |
447 | print " datum.values = NULL;" | |
448 | print " for (i = 0; i < %s; i++) {" % nVar | |
99155935 | 449 | print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar) |
475281c0 | 450 | if type.value: |
99155935 | 451 | print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar) |
475281c0 | 452 | print " }" |
a1ec42a3 BP |
453 | if type.value: |
454 | valueType = type.value.toAtomicType() | |
455 | else: | |
456 | valueType = "OVSDB_TYPE_VOID" | |
457 | print " ovsdb_datum_sort_unique(&datum, %s, %s);" % ( | |
458 | type.key.toAtomicType(), valueType) | |
475281c0 BP |
459 | print " ovsdb_idl_txn_write(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \ |
460 | % {'s': structName, | |
461 | 'S': structName.upper(), | |
462 | 'C': columnName.upper()} | |
463 | print "}" | |
c3bb4bd7 BP |
464 | |
465 | # Table columns. | |
bd76d25d | 466 | print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % ( |
c3bb4bd7 | 467 | structName, structName.upper()) |
bd76d25d BP |
468 | print """ |
469 | static void\n%s_columns_init(void) | |
470 | { | |
471 | struct ovsdb_idl_column *c;\ | |
472 | """ % structName | |
473 | for columnName, column in sorted(table.columns.iteritems()): | |
474 | cs = "%s_col_%s" % (structName, columnName) | |
475 | d = {'cs': cs, 'c': columnName, 's': structName} | |
476 | ||
477 | print " /* Initialize %(cs)s. */" % d | |
478 | print " c = &%(cs)s;" % d | |
479 | print " c->name = \"%(c)s\";" % d | |
480 | print column.type.cInitType(" ", "c->type") | |
481 | print " c->parse = %(s)s_parse_%(c)s;" % d | |
482 | print " c->unparse = %(s)s_unparse_%(c)s;" % d | |
483 | print "}" | |
c3bb4bd7 BP |
484 | |
485 | # Table classes. | |
486 | print "\f" | |
979821c0 | 487 | print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper()) |
bd76d25d | 488 | for tableName, table in sorted(schema.tables.iteritems()): |
c3bb4bd7 BP |
489 | structName = "%s%s" % (prefix, tableName.lower()) |
490 | print " {\"%s\"," % tableName | |
491 | print " %s_columns, ARRAY_SIZE(%s_columns)," % ( | |
492 | structName, structName) | |
979821c0 | 493 | print " sizeof(struct %s)}," % structName |
c3bb4bd7 BP |
494 | print "};" |
495 | ||
496 | # IDL class. | |
497 | print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix | |
9cb53f26 BP |
498 | print " \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % ( |
499 | schema.name, prefix, prefix) | |
c3bb4bd7 | 500 | print "};" |
d879a707 | 501 | |
bd76d25d BP |
502 | # global init function |
503 | print """ | |
504 | void | |
505 | %sinit(void) | |
506 | { | |
507 | if (inited) { | |
508 | return; | |
509 | } | |
510 | inited = true; | |
511 | """ % prefix | |
512 | for tableName, table in sorted(schema.tables.iteritems()): | |
513 | structName = "%s%s" % (prefix, tableName.lower()) | |
514 | print " %s_columns_init();" % structName | |
515 | print "}" | |
516 | ||
d879a707 BP |
517 | def ovsdb_escape(string): |
518 | def escape(match): | |
519 | c = match.group(0) | |
520 | if c == '\0': | |
99155935 | 521 | raise ovs.db.error.Error("strings may not contain null bytes") |
d879a707 BP |
522 | elif c == '\\': |
523 | return '\\\\' | |
524 | elif c == '\n': | |
525 | return '\\n' | |
526 | elif c == '\r': | |
527 | return '\\r' | |
528 | elif c == '\t': | |
529 | return '\\t' | |
530 | elif c == '\b': | |
531 | return '\\b' | |
532 | elif c == '\a': | |
533 | return '\\a' | |
534 | else: | |
535 | return '\\x%02x' % ord(c) | |
536 | return re.sub(r'["\\\000-\037]', escape, string) | |
537 | ||
45a7de56 | 538 | |
45a7de56 | 539 | |
d879a707 BP |
540 | def usage(): |
541 | print """\ | |
542 | %(argv0)s: ovsdb schema compiler | |
00732bf5 | 543 | usage: %(argv0)s [OPTIONS] COMMAND ARG... |
d879a707 | 544 | |
00732bf5 BP |
545 | The following commands are supported: |
546 | annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS | |
547 | c-idl-header IDL print C header file for IDL | |
548 | c-idl-source IDL print C source file for IDL implementation | |
89365653 | 549 | nroff IDL print schema documentation in nroff format |
d879a707 BP |
550 | |
551 | The following options are also available: | |
552 | -h, --help display this help message | |
553 | -V, --version display version information\ | |
554 | """ % {'argv0': argv0} | |
555 | sys.exit(0) | |
556 | ||
557 | if __name__ == "__main__": | |
558 | try: | |
559 | try: | |
00732bf5 BP |
560 | options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV', |
561 | ['directory', | |
562 | 'help', | |
d879a707 BP |
563 | 'version']) |
564 | except getopt.GetoptError, geo: | |
565 | sys.stderr.write("%s: %s\n" % (argv0, geo.msg)) | |
566 | sys.exit(1) | |
c5c7c7c5 | 567 | |
00732bf5 BP |
568 | for key, value in options: |
569 | if key in ['-h', '--help']: | |
570 | usage() | |
571 | elif key in ['-V', '--version']: | |
572 | print "ovsdb-idlc (Open vSwitch) @VERSION@" | |
573 | elif key in ['-C', '--directory']: | |
574 | os.chdir(value) | |
575 | else: | |
576 | sys.exit(0) | |
c5c7c7c5 | 577 | |
d879a707 | 578 | optKeys = [key for key, value in options] |
00732bf5 BP |
579 | |
580 | if not args: | |
581 | sys.stderr.write("%s: missing command argument " | |
582 | "(use --help for help)\n" % argv0) | |
d879a707 BP |
583 | sys.exit(1) |
584 | ||
00732bf5 BP |
585 | commands = {"annotate": (annotateSchema, 2), |
586 | "c-idl-header": (printCIDLHeader, 1), | |
89365653 | 587 | "c-idl-source": (printCIDLSource, 1)} |
00732bf5 BP |
588 | |
589 | if not args[0] in commands: | |
590 | sys.stderr.write("%s: unknown command \"%s\" " | |
591 | "(use --help for help)\n" % (argv0, args[0])) | |
d879a707 | 592 | sys.exit(1) |
00732bf5 BP |
593 | |
594 | func, n_args = commands[args[0]] | |
595 | if len(args) - 1 != n_args: | |
596 | sys.stderr.write("%s: \"%s\" requires %d arguments but %d " | |
597 | "provided\n" | |
598 | % (argv0, args[0], n_args, len(args) - 1)) | |
599 | sys.exit(1) | |
600 | ||
601 | func(*args[1:]) | |
99155935 BP |
602 | except ovs.db.error.Error, e: |
603 | sys.stderr.write("%s: %s\n" % (argv0, e)) | |
d879a707 BP |
604 | sys.exit(1) |
605 | ||
606 | # Local variables: | |
607 | # mode: python | |
608 | # End: |