ovsdb-idlc: Remove "*_get_*" warning from header file.
[cascardo/ovs.git] / ovsdb / ovsdb-idlc.in
1 #! @PYTHON@
2
3 import getopt
4 import os
5 import re
6 import sys
7
8 import ovs.json
9 import ovs.db.error
10 import ovs.db.schema
11
12 argv0 = sys.argv[0]
13
14 def parseSchema(filename):
15     return ovs.db.schema.IdlSchema.from_json(ovs.json.from_file(filename))
16
17 def annotateSchema(schemaFile, annotationFile):
18     schemaJson = ovs.json.from_file(schemaFile)
19     execfile(annotationFile, globals(), {"s": schemaJson})
20     ovs.json.to_stream(schemaJson, sys.stdout)
21     sys.stdout.write('\n')
22
23 def constify(cType, const):
24     if (const
25         and cType.endswith('*') and
26         (cType == 'char **' or not cType.endswith('**'))):
27         return 'const %s' % cType
28     else:
29         return cType
30
31 def cMembers(prefix, columnName, column, const):
32     type = column.type
33
34     if type.is_smap():
35         return [{'name': columnName,
36                  'type': 'struct smap ',
37                  'comment': ''}]
38
39     if type.n_min == 1 and type.n_max == 1:
40         singleton = True
41         pointer = ''
42     else:
43         singleton = False
44         if type.is_optional_pointer():
45             pointer = ''
46         else:
47             pointer = '*'
48
49     if type.value:
50         key = {'name': "key_%s" % columnName,
51                'type': constify(type.key.toCType(prefix) + pointer, const),
52                'comment': ''}
53         value = {'name': "value_%s" % columnName,
54                  'type': constify(type.value.toCType(prefix) + pointer, const),
55                  'comment': ''}
56         members = [key, value]
57     else:
58         m = {'name': columnName,
59              'type': constify(type.key.toCType(prefix) + pointer, const),
60              'comment': type.cDeclComment()}
61         members = [m]
62
63     if not singleton and not type.is_optional_pointer():
64         members.append({'name': 'n_%s' % columnName,
65                         'type': 'size_t ',
66                         'comment': ''})
67     return members
68
69 def printCIDLHeader(schemaFile):
70     schema = parseSchema(schemaFile)
71     prefix = schema.idlPrefix
72     print '''\
73 /* Generated automatically -- do not modify!    -*- buffer-read-only: t -*- */
74
75 #ifndef %(prefix)sIDL_HEADER
76 #define %(prefix)sIDL_HEADER 1
77
78 #include <stdbool.h>
79 #include <stddef.h>
80 #include <stdint.h>
81 #include "ovsdb-data.h"
82 #include "ovsdb-idl-provider.h"
83 #include "smap.h"
84 #include "uuid.h"''' % {'prefix': prefix.upper()}
85
86     for tableName, table in sorted(schema.tables.iteritems()):
87         structName = "%s%s" % (prefix, tableName.lower())
88
89         print "\f"
90         print "/* %s table. */" % tableName
91         print "struct %s {" % structName
92         print "\tstruct ovsdb_idl_row header_;"
93         for columnName, column in sorted(table.columns.iteritems()):
94             print "\n\t/* %s column. */" % columnName
95             for member in cMembers(prefix, columnName, column, False):
96                 print "\t%(type)s%(name)s;%(comment)s" % member
97         print "};"
98
99         # Column indexes.
100         printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
101                    for columnName in sorted(table.columns)]
102                   + ["%s_N_COLUMNS" % structName.upper()])
103
104         print
105         for columnName in table.columns:
106             print "#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % {
107                 's': structName,
108                 'S': structName.upper(),
109                 'c': columnName,
110                 'C': columnName.upper()}
111
112         print "\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper())
113
114         print '''
115 const struct %(s)s *%(s)s_get_for_uuid(const struct ovsdb_idl *, const struct uuid *);
116 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
117 const struct %(s)s *%(s)s_next(const struct %(s)s *);
118 #define %(S)s_FOR_EACH(ROW, IDL) \\
119         for ((ROW) = %(s)s_first(IDL); \\
120              (ROW); \\
121              (ROW) = %(s)s_next(ROW))
122 #define %(S)s_FOR_EACH_SAFE(ROW, NEXT, IDL) \\
123         for ((ROW) = %(s)s_first(IDL); \\
124              (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\
125              (ROW) = (NEXT))
126
127 void %(s)s_init(struct %(s)s *);
128 void %(s)s_delete(const struct %(s)s *);
129 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
130 ''' % {'s': structName, 'S': structName.upper()}
131
132         for columnName, column in sorted(table.columns.iteritems()):
133             print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
134
135         print
136         for columnName, column in sorted(table.columns.iteritems()):
137             if column.type.value:
138                 valueParam = ', enum ovsdb_atomic_type value_type'
139             else:
140                 valueParam = ''
141             print 'const struct ovsdb_datum *%(s)s_get_%(c)s(const struct %(s)s *, enum ovsdb_atomic_type key_type%(v)s);' % {
142                 's': structName, 'c': columnName, 'v': valueParam}
143
144         print
145         for columnName, column in sorted(table.columns.iteritems()):
146             print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
147             if column.type.is_smap():
148                 args = ['const struct smap *']
149             else:
150                 args = ['%(type)s%(name)s' % member for member
151                         in cMembers(prefix, columnName, column, True)]
152             print '%s);' % ', '.join(args)
153
154         print
155
156     # Table indexes.
157     printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
158     print
159     for tableName in schema.tables:
160         print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
161             'p': prefix,
162             'P': prefix.upper(),
163             't': tableName.lower(),
164             'T': tableName.upper()}
165     print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
166
167     print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
168     print "\nvoid %sinit(void);" % prefix
169
170     print "\nconst char * %sget_db_version(void);" % prefix
171     print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
172
173 def printEnum(members):
174     if len(members) == 0:
175         return
176
177     print "\nenum {";
178     for member in members[:-1]:
179         print "    %s," % member
180     print "    %s" % members[-1]
181     print "};"
182
183 def printCIDLSource(schemaFile):
184     schema = parseSchema(schemaFile)
185     prefix = schema.idlPrefix
186     print '''\
187 /* Generated automatically -- do not modify!    -*- buffer-read-only: t -*- */
188
189 #include <config.h>
190 #include %s
191 #include <limits.h>
192 #include "ovs-thread.h"
193 #include "ovsdb-data.h"
194 #include "ovsdb-error.h"
195 #include "util.h"
196
197 #ifdef __CHECKER__
198 /* Sparse dislikes sizeof(bool) ("warning: expression using sizeof bool"). */
199 enum { sizeof_bool = 1 };
200 #else
201 enum { sizeof_bool = sizeof(bool) };
202 #endif
203
204 static bool inited;
205 ''' % schema.idlHeader
206
207     # Cast functions.
208     for tableName, table in sorted(schema.tables.iteritems()):
209         structName = "%s%s" % (prefix, tableName.lower())
210         print '''
211 static struct %(s)s *
212 %(s)s_cast(const struct ovsdb_idl_row *row)
213 {
214     return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
215 }\
216 ''' % {'s': structName}
217
218
219     for tableName, table in sorted(schema.tables.iteritems()):
220         structName = "%s%s" % (prefix, tableName.lower())
221         print "\f"
222         print "/* %s table. */" % (tableName)
223
224         # Parse functions.
225         for columnName, column in sorted(table.columns.iteritems()):
226             print '''
227 static void
228 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
229 {
230     struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
231                                                 'c': columnName}
232             type = column.type
233             if type.value:
234                 keyVar = "row->key_%s" % columnName
235                 valueVar = "row->value_%s" % columnName
236             else:
237                 keyVar = "row->%s" % columnName
238                 valueVar = None
239
240             if type.is_smap():
241                 print "    size_t i;"
242                 print
243                 print "    ovs_assert(inited);"
244                 print "    smap_init(&row->%s);" % columnName
245                 print "    for (i = 0; i < datum->n; i++) {"
246                 print "        smap_add(&row->%s," % columnName
247                 print "                 datum->keys[i].string,"
248                 print "                 datum->values[i].string);"
249                 print "    }"
250             elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
251                 print
252                 print "    ovs_assert(inited);"
253                 print "    if (datum->n >= 1) {"
254                 if not type.key.ref_table:
255                     print "        %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_string())
256                 else:
257                     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.name.lower(), prefix, prefix.upper(), type.key.ref_table.name.upper())
258
259                 if valueVar:
260                     if type.value.ref_table:
261                         print "        %s = datum->values[0].%s;" % (valueVar, type.value.type.to_string())
262                     else:
263                         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.name.lower(), prefix, prefix.upper(), type.value.ref_table.name.upper())
264                 print "    } else {"
265                 print "        %s" % type.key.initCDefault(keyVar, type.n_min == 0)
266                 if valueVar:
267                     print "        %s" % type.value.initCDefault(valueVar, type.n_min == 0)
268                 print "    }"
269             else:
270                 if type.n_max != sys.maxint:
271                     print "    size_t n = MIN(%d, datum->n);" % type.n_max
272                     nMax = "n"
273                 else:
274                     nMax = "datum->n"
275                 print "    size_t i;"
276                 print
277                 print "    ovs_assert(inited);"
278                 print "    %s = NULL;" % keyVar
279                 if valueVar:
280                     print "    %s = NULL;" % valueVar
281                 print "    row->n_%s = 0;" % columnName
282                 print "    for (i = 0; i < %s; i++) {" % nMax
283                 refs = []
284                 if type.key.ref_table:
285                     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.name.lower(), prefix, type.key.ref_table.name.lower(), prefix, prefix.upper(), type.key.ref_table.name.upper())
286                     keySrc = "keyRow"
287                     refs.append('keyRow')
288                 else:
289                     keySrc = "datum->keys[i].%s" % type.key.type.to_string()
290                 if type.value and type.value.ref_table:
291                     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.name.lower(), prefix, type.value.ref_table.name.lower(), prefix, prefix.upper(), type.value.ref_table.name.upper())
292                     valueSrc = "valueRow"
293                     refs.append('valueRow')
294                 elif valueVar:
295                     valueSrc = "datum->values[i].%s" % type.value.type.to_string()
296                 if refs:
297                     print "        if (%s) {" % ' && '.join(refs)
298                     indent = "            "
299                 else:
300                     indent = "        "
301                 print "%sif (!row->n_%s) {" % (indent, columnName)
302
303                 # Special case for boolean types.  This is only here because
304                 # sparse does not like the "normal" case ("warning: expression
305                 # using sizeof bool").
306                 if type.key.type == ovs.db.types.BooleanType:
307                     sizeof = "sizeof_bool"
308                 else:
309                     sizeof = "sizeof *%s" % keyVar
310                 print "%s    %s = xmalloc(%s * %s);" % (indent, keyVar, nMax,
311                                                         sizeof)
312                 if valueVar:
313                     # Special case for boolean types (see above).
314                     if type.value.type == ovs.db.types.BooleanType:
315                         sizeof = " * sizeof_bool"
316                     else:
317                         sizeof = "sizeof *%s" % valueVar
318                     print "%s    %s = xmalloc(%s * %s);" % (indent, valueVar,
319                                                             nMax, sizeof)
320                 print "%s}" % indent
321                 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
322                 if valueVar:
323                     print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
324                 print "%srow->n_%s++;" % (indent, columnName)
325                 if refs:
326                     print "        }"
327                 print "    }"
328             print "}"
329
330         # Unparse functions.
331         for columnName, column in sorted(table.columns.iteritems()):
332             type = column.type
333             if type.is_smap() or (type.n_min != 1 or type.n_max != 1) and not type.is_optional_pointer():
334                 print '''
335 static void
336 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
337 {
338     struct %(s)s *row = %(s)s_cast(row_);
339
340     ovs_assert(inited);''' % {'s': structName, 'c': columnName}
341
342                 if type.is_smap():
343                     print "    smap_destroy(&row->%s);" % columnName
344                 else:
345                     if type.value:
346                         keyVar = "row->key_%s" % columnName
347                         valueVar = "row->value_%s" % columnName
348                     else:
349                         keyVar = "row->%s" % columnName
350                         valueVar = None
351                     print "    free(%s);" % keyVar
352                     if valueVar:
353                         print "    free(%s);" % valueVar
354                 print '}'
355             else:
356                 print '''
357 static void
358 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row OVS_UNUSED)
359 {
360     /* Nothing to do. */
361 }''' % {'s': structName, 'c': columnName}
362
363         # Generic Row Initialization function.
364         print """
365 static void
366 %(s)s_init__(struct ovsdb_idl_row *row)
367 {
368     %(s)s_init(%(s)s_cast(row));
369 }""" % {'s': structName}
370
371         # Row Initialization function.
372         print """
373 /* Clears the contents of 'row' in table "%(t)s". */
374 void
375 %(s)s_init(struct %(s)s *row)
376 {
377     memset(row, 0, sizeof *row); """ % {'s': structName, 't': tableName}
378         for columnName, column in sorted(table.columns.iteritems()):
379             if column.type.is_smap():
380                 print "    smap_init(&row->%s);" % columnName
381         print "}"
382
383         # First, next functions.
384         print '''
385 /* Searches table "%(t)s" in 'idl' for a row with UUID 'uuid'.  Returns
386  * a pointer to the row if there is one, otherwise a null pointer.  */
387 const struct %(s)s *
388 %(s)s_get_for_uuid(const struct ovsdb_idl *idl, const struct uuid *uuid)
389 {
390     return %(s)s_cast(ovsdb_idl_get_row_for_uuid(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s], uuid));
391 }
392
393 /* Returns a row in table "%(t)s" in 'idl', or a null pointer if that
394  * table is empty.
395  *
396  * Database tables are internally maintained as hash tables, so adding or
397  * removing rows while traversing the same table can cause some rows to be
398  * visited twice or not at apply. */
399 const struct %(s)s *
400 %(s)s_first(const struct ovsdb_idl *idl)
401 {
402     return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
403 }
404
405 /* Returns a row following 'row' within its table, or a null pointer if 'row'
406  * is the last row in its table. */
407 const struct %(s)s *
408 %(s)s_next(const struct %(s)s *row)
409 {
410     return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
411 }''' % {'s': structName,
412         'p': prefix,
413         'P': prefix.upper(),
414         't': tableName,
415         'T': tableName.upper()}
416
417         print '''
418 /* Deletes 'row' from table "%(t)s".  'row' may be freed, so it must not be
419  * accessed afterward.
420  *
421  * The caller must have started a transaction with ovsdb_idl_txn_create(). */
422 void
423 %(s)s_delete(const struct %(s)s *row)
424 {
425     ovsdb_idl_txn_delete(&row->header_);
426 }
427
428 /* Inserts and returns a new row in the table "%(t)s" in the database
429  * with open transaction 'txn'.
430  *
431  * The new row is assigned a randomly generated provisional UUID.
432  * ovsdb-server will assign a different UUID when 'txn' is committed,
433  * but the IDL will replace any uses of the provisional UUID in the
434  * data to be to be committed by the UUID assigned by ovsdb-server. */
435 struct %(s)s *
436 %(s)s_insert(struct ovsdb_idl_txn *txn)
437 {
438     return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s], NULL));
439 }''' % {'s': structName,
440         'p': prefix,
441         'P': prefix.upper(),
442         't': tableName,
443         'T': tableName.upper()}
444
445         # Verify functions.
446         for columnName, column in sorted(table.columns.iteritems()):
447             print '''
448 /* Causes the original contents of column "%(c)s" in 'row' to be
449  * verified as a prerequisite to completing the transaction.  That is, if
450  * "%(c)s" in 'row' changed (or if 'row' was deleted) between the
451  * time that the IDL originally read its contents and the time that the
452  * transaction commits, then the transaction aborts and ovsdb_idl_txn_commit()
453  * returns TXN_AGAIN_WAIT or TXN_AGAIN_NOW (depending on whether the database
454  * change has already been received).
455  *
456  * The intention is that, to ensure that no transaction commits based on dirty
457  * reads, an application should call this function any time "%(c)s" is
458  * read as part of a read-modify-write operation.
459  *
460  * In some cases this function reduces to a no-op, because the current value
461  * of "%(c)s" is already known:
462  *
463  *   - If 'row' is a row created by the current transaction (returned by
464  *     %(s)s_insert()).
465  *
466  *   - If "%(c)s" has already been modified (with
467  *     %(s)s_set_%(c)s()) within the current transaction.
468  *
469  * Because of the latter property, always call this function *before*
470  * %(s)s_set_%(c)s() for a given read-modify-write.
471  *
472  * The caller must have started a transaction with ovsdb_idl_txn_create(). */
473 void
474 %(s)s_verify_%(c)s(const struct %(s)s *row)
475 {
476     ovs_assert(inited);
477     ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
478 }''' % {'s': structName,
479         'S': structName.upper(),
480         'c': columnName,
481         'C': columnName.upper()}
482
483         # Get functions.
484         for columnName, column in sorted(table.columns.iteritems()):
485             if column.type.value:
486                 valueParam = ',\n\tenum ovsdb_atomic_type value_type OVS_UNUSED'
487                 valueType = '\n    ovs_assert(value_type == %s);' % column.type.value.toAtomicType()
488                 valueComment = "\n * 'value_type' must be %s." % column.type.value.toAtomicType()
489             else:
490                 valueParam = ''
491                 valueType = ''
492                 valueComment = ''
493             print """
494 /* Returns the "%(c)s" column's value from the "%(t)s" table in 'row'
495  * as a struct ovsdb_datum.  This is useful occasionally: for example,
496  * ovsdb_datum_find_key() is an easier and more efficient way to search
497  * for a given key than implementing the same operation on the "cooked"
498  * form in 'row'.
499  *
500  * 'key_type' must be %(kt)s.%(vc)s
501  * (This helps to avoid silent bugs if someone changes %(c)s's
502  * type without updating the caller.)
503  *
504  * The caller must not modify or free the returned value.
505  *
506  * Various kinds of changes can invalidate the returned value: modifying
507  * 'column' within 'row', deleting 'row', or completing an ongoing transaction.
508  * If the returned value is needed for a long time, it is best to make a copy
509  * of it with ovsdb_datum_clone().
510  *
511  * This function is rarely useful, since it is easier to access the value
512  * directly through the "%(c)s" member in %(s)s. */
513 const struct ovsdb_datum *
514 %(s)s_get_%(c)s(const struct %(s)s *row,
515 \tenum ovsdb_atomic_type key_type OVS_UNUSED%(v)s)
516 {
517     ovs_assert(key_type == %(kt)s);%(vt)s
518     return ovsdb_idl_read(&row->header_, &%(s)s_col_%(c)s);
519 }""" % {'t': tableName, 's': structName, 'c': columnName,
520        'kt': column.type.key.toAtomicType(),
521        'v': valueParam, 'vt': valueType, 'vc': valueComment}
522
523         # Set functions.
524         for columnName, column in sorted(table.columns.iteritems()):
525             type = column.type
526
527             if type.is_smap():
528                 print """
529 void
530 %(s)s_set_%(c)s(const struct %(s)s *row, const struct smap *%(c)s)
531 {
532     struct ovsdb_datum datum;
533
534     ovs_assert(inited);
535     if (%(c)s) {
536         struct smap_node *node;
537         size_t i;
538
539         datum.n = smap_count(%(c)s);
540         datum.keys = xmalloc(datum.n * sizeof *datum.keys);
541         datum.values = xmalloc(datum.n * sizeof *datum.values);
542
543         i = 0;
544         SMAP_FOR_EACH (node, %(c)s) {
545             datum.keys[i].string = xstrdup(node->key);
546             datum.values[i].string = xstrdup(node->value);
547             i++;
548         }
549         ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
550     } else {
551         ovsdb_datum_init_empty(&datum);
552     }
553     ovsdb_idl_txn_write(&row->header_,
554                         &%(s)s_columns[%(S)s_COL_%(C)s],
555                         &datum);
556 }
557 """ % {'s': structName,
558        'S': structName.upper(),
559        'c': columnName,
560        'C': columnName.upper()}
561                 continue
562
563
564             print '\nvoid'
565             members = cMembers(prefix, columnName, column, True)
566             keyVar = members[0]['name']
567             nVar = None
568             valueVar = None
569             if type.value:
570                 valueVar = members[1]['name']
571                 if len(members) > 2:
572                     nVar = members[2]['name']
573             else:
574                 if len(members) > 1:
575                     nVar = members[1]['name']
576             print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
577                 {'s': structName, 'c': columnName,
578                  'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
579             print "{"
580             print "    struct ovsdb_datum datum;"
581             if type.n_min == 1 and type.n_max == 1:
582                 print "    union ovsdb_atom key;"
583                 if type.value:
584                     print "    union ovsdb_atom value;"
585                 print
586                 print "    ovs_assert(inited);"
587                 print "    datum.n = 1;"
588                 print "    datum.keys = &key;"
589                 print "    " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
590                 if type.value:
591                     print "    datum.values = &value;"
592                     print "    "+ type.value.assign_c_value_casting_away_const("value.%s" % type.value.type.to_string(), valueVar)
593                 else:
594                     print "    datum.values = NULL;"
595                 txn_write_func = "ovsdb_idl_txn_write_clone"
596             elif type.is_optional_pointer():
597                 print "    union ovsdb_atom key;"
598                 print
599                 print "    ovs_assert(inited);"
600                 print "    if (%s) {" % keyVar
601                 print "        datum.n = 1;"
602                 print "        datum.keys = &key;"
603                 print "        " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), keyVar)
604                 print "    } else {"
605                 print "        datum.n = 0;"
606                 print "        datum.keys = NULL;"
607                 print "    }"
608                 print "    datum.values = NULL;"
609                 txn_write_func = "ovsdb_idl_txn_write_clone"
610             elif type.n_max == 1:
611                 print "    union ovsdb_atom key;"
612                 print
613                 print "    ovs_assert(inited);"
614                 print "    if (%s) {" % nVar
615                 print "        datum.n = 1;"
616                 print "        datum.keys = &key;"
617                 print "        " + type.key.assign_c_value_casting_away_const("key.%s" % type.key.type.to_string(), "*" + keyVar)
618                 print "    } else {"
619                 print "        datum.n = 0;"
620                 print "        datum.keys = NULL;"
621                 print "    }"
622                 print "    datum.values = NULL;"
623                 txn_write_func = "ovsdb_idl_txn_write_clone"
624             else:
625                 print "    size_t i;"
626                 print
627                 print "    ovs_assert(inited);"
628                 print "    datum.n = %s;" % nVar
629                 print "    datum.keys = %s ? xmalloc(%s * sizeof *datum.keys) : NULL;" % (nVar, nVar)
630                 if type.value:
631                     print "    datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
632                 else:
633                     print "    datum.values = NULL;"
634                 print "    for (i = 0; i < %s; i++) {" % nVar
635                 print "        " + type.key.copyCValue("datum.keys[i].%s" % type.key.type.to_string(), "%s[i]" % keyVar)
636                 if type.value:
637                     print "        " + type.value.copyCValue("datum.values[i].%s" % type.value.type.to_string(), "%s[i]" % valueVar)
638                 print "    }"
639                 if type.value:
640                     valueType = type.value.toAtomicType()
641                 else:
642                     valueType = "OVSDB_TYPE_VOID"
643                 print "    ovsdb_datum_sort_unique(&datum, %s, %s);" % (
644                     type.key.toAtomicType(), valueType)
645                 txn_write_func = "ovsdb_idl_txn_write"
646             print "    %(f)s(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
647                 % {'f': txn_write_func,
648                    's': structName,
649                    'S': structName.upper(),
650                    'C': columnName.upper()}
651             print "}"
652
653         # Table columns.
654         print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
655             structName, structName.upper())
656         print """
657 static void\n%s_columns_init(void)
658 {
659     struct ovsdb_idl_column *c;\
660 """ % structName
661         for columnName, column in sorted(table.columns.iteritems()):
662             cs = "%s_col_%s" % (structName, columnName)
663             d = {'cs': cs, 'c': columnName, 's': structName}
664             if column.mutable:
665                 mutable = "true"
666             else:
667                 mutable = "false"
668             print
669             print "    /* Initialize %(cs)s. */" % d
670             print "    c = &%(cs)s;" % d
671             print "    c->name = \"%(c)s\";" % d
672             print column.type.cInitType("    ", "c->type")
673             print "    c->mutable = %s;" % mutable
674             print "    c->parse = %(s)s_parse_%(c)s;" % d
675             print "    c->unparse = %(s)s_unparse_%(c)s;" % d
676         print "}"
677
678     # Table classes.
679     print "\f"
680     print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
681     for tableName, table in sorted(schema.tables.iteritems()):
682         structName = "%s%s" % (prefix, tableName.lower())
683         if table.is_root:
684             is_root = "true"
685         else:
686             is_root = "false"
687         print "    {\"%s\", %s," % (tableName, is_root)
688         print "     %s_columns, ARRAY_SIZE(%s_columns)," % (
689             structName, structName)
690         print "     sizeof(struct %s), %s_init__}," % (structName, structName)
691     print "};"
692
693     # IDL class.
694     print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
695     print "    \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
696         schema.name, prefix, prefix)
697     print "};"
698
699     # global init function
700     print """
701 void
702 %sinit(void)
703 {
704     if (inited) {
705         return;
706     }
707     assert_single_threaded();
708     inited = true;
709 """ % prefix
710     for tableName, table in sorted(schema.tables.iteritems()):
711         structName = "%s%s" % (prefix, tableName.lower())
712         print "    %s_columns_init();" % structName
713     print "}"
714
715     print """
716 /* Return the schema version.  The caller must not free the returned value. */
717 const char *
718 %sget_db_version(void)
719 {
720     return "%s";
721 }
722 """ % (prefix, schema.version)
723
724
725
726 def ovsdb_escape(string):
727     def escape(match):
728         c = match.group(0)
729         if c == '\0':
730             raise ovs.db.error.Error("strings may not contain null bytes")
731         elif c == '\\':
732             return '\\\\'
733         elif c == '\n':
734             return '\\n'
735         elif c == '\r':
736             return '\\r'
737         elif c == '\t':
738             return '\\t'
739         elif c == '\b':
740             return '\\b'
741         elif c == '\a':
742             return '\\a'
743         else:
744             return '\\x%02x' % ord(c)
745     return re.sub(r'["\\\000-\037]', escape, string)
746
747 def usage():
748     print """\
749 %(argv0)s: ovsdb schema compiler
750 usage: %(argv0)s [OPTIONS] COMMAND ARG...
751
752 The following commands are supported:
753   annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
754   c-idl-header IDL            print C header file for IDL
755   c-idl-source IDL            print C source file for IDL implementation
756   nroff IDL                   print schema documentation in nroff format
757
758 The following options are also available:
759   -h, --help                  display this help message
760   -V, --version               display version information\
761 """ % {'argv0': argv0}
762     sys.exit(0)
763
764 if __name__ == "__main__":
765     try:
766         try:
767             options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
768                                               ['directory',
769                                                'help',
770                                                'version'])
771         except getopt.GetoptError, geo:
772             sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
773             sys.exit(1)
774
775         for key, value in options:
776             if key in ['-h', '--help']:
777                 usage()
778             elif key in ['-V', '--version']:
779                 print "ovsdb-idlc (Open vSwitch) @VERSION@"
780             elif key in ['-C', '--directory']:
781                 os.chdir(value)
782             else:
783                 sys.exit(0)
784
785         optKeys = [key for key, value in options]
786
787         if not args:
788             sys.stderr.write("%s: missing command argument "
789                              "(use --help for help)\n" % argv0)
790             sys.exit(1)
791
792         commands = {"annotate": (annotateSchema, 2),
793                     "c-idl-header": (printCIDLHeader, 1),
794                     "c-idl-source": (printCIDLSource, 1)}
795
796         if not args[0] in commands:
797             sys.stderr.write("%s: unknown command \"%s\" "
798                              "(use --help for help)\n" % (argv0, args[0]))
799             sys.exit(1)
800
801         func, n_args = commands[args[0]]
802         if len(args) - 1 != n_args:
803             sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
804                              "provided\n"
805                              % (argv0, args[0], n_args, len(args) - 1))
806             sys.exit(1)
807
808         func(*args[1:])
809     except ovs.db.error.Error, e:
810         sys.stderr.write("%s: %s\n" % (argv0, e))
811         sys.exit(1)
812
813 # Local variables:
814 # mode: python
815 # End: