ovsdb-idlc: Add "const" to "set" function arguments that should have it.
[cascardo/ovs.git] / ovsdb / ovsdb-idlc.in
1 #! @PYTHON@
2
3 import getopt
4 import re
5 import sys
6
7 sys.path.insert(0, "@abs_top_srcdir@/ovsdb")
8 import simplejson as json
9
10 argv0 = sys.argv[0]
11
12 class Error(Exception):
13     def __init__(self, msg):
14         Exception.__init__(self)
15         self.msg = msg
16
17 def getMember(json, name, validTypes, description, default=None):
18     if name in json:
19         member = json[name]
20         if type(member) not in validTypes:
21             raise Error("%s: type mismatch for '%s' member"
22                         % (description, name))
23         return member
24     return default
25
26 def mustGetMember(json, name, expectedType, description):
27     member = getMember(json, name, expectedType, description)
28     if member == None:
29         raise Error("%s: missing '%s' member" % (description, name))
30     return member
31
32 class DbSchema:
33     def __init__(self, name, comment, tables, idlPrefix, idlHeader):
34         self.name = name
35         self.comment = comment
36         self.tables = tables
37         self.idlPrefix = idlPrefix
38         self.idlHeader = idlHeader
39
40     @staticmethod
41     def fromJson(json):
42         name = mustGetMember(json, 'name', [unicode], 'database')
43         comment = getMember(json, 'comment', [unicode], 'database')
44         tablesJson = mustGetMember(json, 'tables', [dict], 'database')
45         tables = {}
46         for name, tableJson in tablesJson.iteritems():
47             tables[name] = TableSchema.fromJson(tableJson, "%s table" % name)
48         idlPrefix = mustGetMember(json, 'idlPrefix', [unicode], 'database')
49         idlHeader = mustGetMember(json, 'idlHeader', [unicode], 'database')
50         return DbSchema(name, comment, tables, idlPrefix, idlHeader)
51
52     def toJson(self):
53         d = {"name": self.name,
54              "tables": {}}
55         for name, table in self.tables.iteritems():
56             d["tables"][name] = table.toJson()
57         if self.comment != None:
58             d["comment"] = self.comment
59         return d
60
61 class TableSchema:
62     def __init__(self, comment, columns):
63         self.comment = comment
64         self.columns = columns
65
66     @staticmethod
67     def fromJson(json, description):
68         comment = getMember(json, 'comment', [unicode], description)
69         columnsJson = mustGetMember(json, 'columns', [dict], description)
70         columns = {}
71         for name, json in columnsJson.iteritems():
72             columns[name] = ColumnSchema.fromJson(
73                 json, "column %s in %s" % (name, description))
74         return TableSchema(comment, columns)
75
76     def toJson(self):
77         d = {"columns": {}}
78         for name, column in self.columns.iteritems():
79             d["columns"][name] = column.toJson()
80         if self.comment != None:
81             d["comment"] = self.comment
82         return d
83
84 class ColumnSchema:
85     def __init__(self, comment, type, persistent):
86         self.comment = comment
87         self.type = type
88         self.persistent = persistent
89
90     @staticmethod
91     def fromJson(json, description):
92         comment = getMember(json, 'comment', [unicode], description)
93         type = Type.fromJson(mustGetMember(json, 'type', [dict, unicode],
94                                            description),
95                              'type of %s' % description)
96         ephemeral = getMember(json, 'ephemeral', [bool], description)
97         persistent = ephemeral != True
98         return ColumnSchema(comment, type, persistent)
99
100     def toJson(self):
101         d = {"type": self.type.toJson()}
102         if self.persistent == False:
103             d["ephemeral"] = True
104         if self.comment != None:
105             d["comment"] = self.comment
106         return d
107
108 class Type:
109     def __init__(self, key, keyRefTable=None, value=None, valueRefTable=None,
110                  min=1, max=1):
111         self.key = key
112         self.keyRefTable = keyRefTable
113         self.value = value
114         self.valueRefTable = valueRefTable
115         self.min = min
116         self.max = max
117     
118     @staticmethod
119     def fromJson(json, description):
120         if type(json) == unicode:
121             return Type(json)
122         else:
123             key = mustGetMember(json, 'key', [unicode], description)
124             keyRefTable = getMember(json, 'keyRefTable', [unicode], description)
125             value = getMember(json, 'value', [unicode], description)
126             valueRefTable = getMember(json, 'valueRefTable', [unicode], description)
127             min = getMember(json, 'min', [int], description, 1)
128             max = getMember(json, 'max', [int, unicode], description, 1)
129             return Type(key, keyRefTable, value, valueRefTable, min, max)
130
131     def toJson(self):
132         if self.value == None and self.min == 1 and self.max == 1:
133             return self.key
134         else:
135             d = {"key": self.key}
136             if self.value != None:
137                 d["value"] = self.value
138             if self.min != 1:
139                 d["min"] = self.min
140             if self.max != 1:
141                 d["max"] = self.max
142             return d
143
144 def parseSchema(filename):
145     file = open(filename, "r")
146     s = ""
147     for line in file:
148         if not line.startswith('//'):
149             s += line
150     return DbSchema.fromJson(json.loads(s))
151
152 def cBaseType(prefix, type, refTable=None):
153     if type == 'uuid' and refTable:
154         return "struct %s%s *" % (prefix, refTable.lower())
155     else:
156         return {'integer': 'int64_t ',
157                 'real': 'double ',
158                 'uuid': 'struct uuid ',
159                 'boolean': 'bool ',
160                 'string': 'char *'}[type]
161
162 def cCopyType(indent, dstVar, dst, src, type, refTable=None):
163     args = {'indent': indent,
164             'dstVar': dstVar,
165             'dst': dst,
166             'src': src}
167     if type == 'uuid' and refTable:
168         return ("%(indent)s%(dstVar)s = %(src)s;\n" +
169                 "%(indent)s%(dst)s = %(src)s->header_.uuid;") % args
170     elif type == 'string':
171         return "%(indent)s%(dstVar)s = %(dst)s = xstrdup(%(src)s);" % args
172     else:
173         return "%(dstVar)s = %(dst)s = %(src)s;" % args
174
175 def typeIsOptionalPointer(type):
176     return (type.min == 0 and type.max == 1 and not type.value
177             and (type.key == 'string'
178                  or (type.key == 'uuid' and type.keyRefTable)))
179
180 def cDeclComment(type):
181     if type.min == 1 and type.max == 1 and type.key == "string":
182         return "\t/* Always nonnull. */"
183     else:
184         return ""
185
186 def constify(cType, const):
187     if (const
188         and cType.endswith('*') and not cType.endswith('**')
189         and (cType.startswith('struct uuid') or cType.startswith('char'))):
190         return 'const %s' % cType
191     else:
192         return cType
193
194 def cMembers(prefix, columnName, column, const):
195     type = column.type
196     if type.min == 1 and type.max == 1:
197         singleton = True
198         pointer = ''
199     else:
200         singleton = False
201         if typeIsOptionalPointer(type):
202             pointer = ''
203         else:
204             pointer = '*'
205
206     if type.value:
207         key = {'name': "key_%s" % columnName,
208                'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
209                'comment': ''}
210         value = {'name': "value_%s" % columnName,
211                  'type': constify(cBaseType(prefix, type.value, type.valueRefTable) + pointer, const),
212                  'comment': ''}
213         members = [key, value]
214     else:
215         m = {'name': columnName,
216              'type': constify(cBaseType(prefix, type.key, type.keyRefTable) + pointer, const),
217              'comment': cDeclComment(type)}
218         members = [m]
219
220     if not singleton and not typeIsOptionalPointer(type):
221         members.append({'name': 'n_%s' % columnName,
222                         'type': 'size_t ',
223                         'comment': ''})
224     return members
225
226 def printCIDLHeader(schema):
227     prefix = schema.idlPrefix
228     print '''\
229 /* Generated automatically -- do not modify!    -*- buffer-read-only: t -*- */
230
231 #ifndef %(prefix)sIDL_HEADER
232 #define %(prefix)sIDL_HEADER 1
233
234 #include <stdbool.h>
235 #include <stddef.h>
236 #include <stdint.h>
237 #include "ovsdb-idl-provider.h"
238 #include "uuid.h"''' % {'prefix': prefix.upper()}
239     for tableName, table in schema.tables.iteritems():
240         print
241         print "/* %s table. */" % tableName
242         structName = "%s%s" % (prefix, tableName.lower())
243         print "struct %s {" % structName
244         print "\tstruct ovsdb_idl_row header_;"
245         for columnName, column in table.columns.iteritems():
246             print "\n\t/* %s column. */" % columnName
247             for member in cMembers(prefix, columnName, column, False):
248                 print "\t%(type)s%(name)s;%(comment)s" % member
249         print '''\
250 };
251
252 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
253 const struct %(s)s *%(s)s_next(const struct %(s)s *);
254 #define %(S)s_FOR_EACH(ROW, IDL) for ((ROW) = %(s)s_first(IDL); (ROW); (ROW) = %(s)s_next(ROW))
255
256 void %(s)s_delete(const struct %(s)s *);
257 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
258 ''' % {'s': structName, 'S': structName.upper()}
259
260         for columnName, column in table.columns.iteritems():
261             print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
262
263         print
264         for columnName, column in table.columns.iteritems():
265
266             print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
267             args = ['%(type)s%(name)s' % member for member
268                     in cMembers(prefix, columnName, column, True)]
269             print '%s);' % ', '.join(args)
270
271     print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
272     print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
273
274 def printEnum(members):
275     if len(members) == 0:
276         return
277
278     print "\nenum {";
279     for member in members[:-1]:
280         print "    %s," % member
281     print "    %s" % members[-1]
282     print "};"
283
284 def printCIDLSource(schema):
285     prefix = schema.idlPrefix
286     print '''\
287 /* Generated automatically -- do not modify!    -*- buffer-read-only: t -*- */
288
289 #include <config.h>
290 #include %s
291 #include <limits.h>
292 #include "ovsdb-data.h"''' % schema.idlHeader
293
294     # Table indexes.
295     printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in schema.tables] + ["%sN_TABLES" % prefix.upper()])
296     print "\nstatic struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
297
298     # Cast functions.
299     for tableName, table in schema.tables.iteritems():
300         structName = "%s%s" % (prefix, tableName.lower())
301         print '''
302 static struct %(s)s *
303 %(s)s_cast(struct ovsdb_idl_row *row)
304 {
305     return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
306 }\
307 ''' % {'s': structName}
308
309
310     for tableName, table in schema.tables.iteritems():
311         structName = "%s%s" % (prefix, tableName.lower())
312         print "\f"
313         if table.comment != None:
314             print "/* %s table (%s). */" % (tableName, table.comment)
315         else:
316             print "/* %s table. */" % (tableName)
317
318         # Column indexes.
319         printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
320                    for columnName in table.columns]
321                   + ["%s_N_COLUMNS" % structName.upper()])
322
323         print "\nstatic struct ovsdb_idl_column %s_columns[];" % structName
324
325         # Parse function.
326         print '''
327 static void
328 %s_parse(struct ovsdb_idl_row *row_)
329 {
330     struct %s *row = %s_cast(row_);
331     const struct ovsdb_datum *datum;
332     size_t i UNUSED;
333
334     memset(row_ + 1, 0, sizeof *row - sizeof *row_);''' % (structName, structName, structName)
335
336
337         for columnName, column in table.columns.iteritems():
338             type = column.type
339             refKey = type.key == "uuid" and type.keyRefTable
340             refValue = type.value == "uuid" and type.valueRefTable
341             print
342             print "    datum = &row_->old[%s_COL_%s];" % (structName.upper(), columnName.upper())
343             if type.value:
344                 keyVar = "row->key_%s" % columnName
345                 valueVar = "row->value_%s" % columnName
346             else:
347                 keyVar = "row->%s" % columnName
348                 valueVar = None
349
350             if (type.min == 1 and type.max == 1) or typeIsOptionalPointer(type):
351                 print "    if (datum->n >= 1) {"
352                 if not refKey:
353                     print "        %s = datum->keys[0].%s;" % (keyVar, type.key)
354                 else:
355                     print "        %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[0].uuid));" % (keyVar, prefix, type.keyRefTable.lower(), prefix, prefix.upper(), type.keyRefTable.upper())
356
357                 if valueVar:
358                     if refValue:
359                         print "        %s = datum->values[0].%s;" % (valueVar, type.value)
360                     else:
361                         print "        %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[0].uuid));" % (valueVar, prefix, type.valueRefTable.lower(), prefix, prefix.upper(), type.valueRefTable.upper())
362                 if (not typeIsOptionalPointer(type) and
363                     (type.key == "string" or type.value == "string")):
364                     print "    } else {"
365                     if type.key == "string":
366                         print "        %s = \"\";" % keyVar
367                     if type.value == "string":
368                         print "        %s = \"\";" % valueVar
369                 print "    }"
370
371             else:
372                 if type.max != 'unlimited':
373                     nMax = "MIN(%d, datum->n)" % type.max
374                 else:
375                     nMax = "datum->n"
376                 print "    for (i = 0; i < %s; i++) {" % nMax
377                 refs = []
378                 if refKey:
379                     print "        struct %s%s *keyRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[i].uuid));" % (prefix, type.keyRefTable.lower(), prefix, type.keyRefTable.lower(), prefix, prefix.upper(), type.keyRefTable.upper())
380                     keySrc = "keyRow"
381                     refs.append('keyRow')
382                 else:
383                     keySrc = "datum->keys[i].%s" % type.key
384                 if refValue:
385                     print "        struct %s%s *valueRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[i].uuid));" % (prefix, type.valueRefTable.lower(), prefix, type.valueRefTable.lower(), prefix, prefix.upper(), type.valueRefTable.upper())
386                     valueSrc = "valueRow"
387                     refs.append('valueRow')
388                 elif valueVar:
389                     valueSrc = "datum->values[i].%s" % type.value
390                 if refs:
391                     print "        if (%s) {" % ' && '.join(refs)
392                     indent = "            "
393                 else:
394                     indent = "        "
395                 print "%sif (!row->n_%s) {" % (indent, columnName)
396                 print "%s    %s = xmalloc(%s * sizeof *%s);" % (indent, keyVar, nMax, keyVar)
397                 if valueVar:
398                     print "%s    %s = xmalloc(%s * sizeof %s);" % (indent, valueVar, nMax, valueVar)
399                 print "%s}" % indent
400                 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
401                 if valueVar:
402                     print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
403                 print "%srow->n_%s++;" % (indent, columnName)
404                 if refs:
405                     print "        }"
406                 print "    }"
407         print "}"
408
409         # Unparse function.
410         nArrays = 0
411         for columnName, column in table.columns.iteritems():
412             type = column.type
413             if (type.min != 1 or type.max != 1) and not typeIsOptionalPointer(type):
414                 if not nArrays:
415                     print '''
416 static void
417 %s_unparse(struct ovsdb_idl_row *row_)
418 {
419     struct %s *row = %s_cast(row_);
420 ''' % (structName, structName, structName)
421                 if type.value:
422                     keyVar = "row->key_%s" % columnName
423                     valueVar = "row->value_%s" % columnName
424                 else:
425                     keyVar = "row->%s" % columnName
426                     valueVar = None
427                 print "    free(%s);" % keyVar
428                 if valueVar:
429                     print "    free(%s);" % valueVar
430                 nArrays += 1
431         if not nArrays:
432             print '''
433 static void
434 %s_unparse(struct ovsdb_idl_row *row UNUSED)
435 {''' % (structName)
436         print "}"
437
438         # First, next functions.
439         print '''
440 const struct %(s)s *
441 %(s)s_first(const struct ovsdb_idl *idl)
442 {
443     return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
444 }
445
446 const struct %(s)s *
447 %(s)s_next(const struct %(s)s *row)
448 {
449     return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
450 }''' % {'s': structName,
451         'p': prefix,
452         'P': prefix.upper(),
453         'T': tableName.upper()}
454
455         print '''
456 void
457 %(s)s_delete(const struct %(s)s *row_)
458 {
459     struct %(s)s *row = (struct %(s)s *) row_;
460     ovsdb_idl_txn_delete(&row->header_);
461 }
462
463 struct %(s)s *
464 %(s)s_insert(struct ovsdb_idl_txn *txn)
465 {
466     return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
467 }
468 ''' % {'s': structName,
469        'p': prefix,
470        'P': prefix.upper(),
471        'T': tableName.upper()}
472
473         # Verify functions.
474         for columnName, column in table.columns.iteritems():
475             print '''
476 void
477 %(s)s_verify_%(c)s(const struct %(s)s *row)
478 {
479     ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
480 }''' % {'s': structName,
481         'S': structName.upper(),
482         'c': columnName,
483         'C': columnName.upper()}
484
485         # Set functions.
486         for columnName, column in table.columns.iteritems():
487             type = column.type
488             print '\nvoid'
489             members = cMembers(prefix, columnName, column, True)
490             keyVar = members[0]['name']
491             nVar = None
492             valueVar = None
493             if type.value:
494                 valueVar = members[1]['name']
495                 if len(members) > 2:
496                     nVar = members[2]['name']
497             else:
498                 if len(members) > 1:
499                     nVar = members[1]['name']
500             print '%(s)s_set_%(c)s(const struct %(s)s *row_, %(args)s)' % \
501                 {'s': structName, 'c': columnName,
502                  'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
503             print "{"
504             print "    struct %(s)s *row = (struct %(s)s *) row_;" % {'s': structName}
505             print "    struct ovsdb_datum datum;"
506             if type.min == 1 and type.max == 1:
507                 print
508                 print "    datum.n = 1;"
509                 print "    datum.keys = xmalloc(sizeof *datum.keys);"
510                 print cCopyType("    ", "row->%s" % keyVar, "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
511                 if type.value:
512                     print "    datum.values = xmalloc(sizeof *datum.values);"
513                     print cCopyType("    ", "row->%s" % valueVar, "datum.values[0].%s" % type.value, valueVar, type.value, type.valueRefTable)
514                 else:
515                     print "    datum.values = NULL;"
516             elif typeIsOptionalPointer(type):
517                 print
518                 print "    if (%s) {" % keyVar
519                 print "        datum.n = 1;"
520                 print "        datum.keys = xmalloc(sizeof *datum.keys);"
521                 print cCopyType("        ", "row->%s" % keyVar, "datum.keys[0].%s" % type.key, keyVar, type.key, type.keyRefTable)
522                 print "    } else {"
523                 print "        datum.n = 0;"
524                 print "        datum.keys = NULL;"
525                 print "        row->%s = NULL;" % keyVar
526                 print "    }"
527                 print "    datum.values = NULL;"
528             else:
529                 print "    size_t i;"
530                 print
531                 print "    free(row->%s);" % keyVar
532                 print "    row->%s = %s ? xmalloc(%s * sizeof *row->%s) : NULL;" % (keyVar, nVar, nVar, keyVar)
533                 print "    row->%s = %s;" % (nVar, nVar)
534                 if type.value:
535                     print "    free(row->%s);" % valueVar
536                     print "    row->%s = xmalloc(%s * sizeof *row->%s);" % (valueVar, nVar, valueVar)
537                 print "    datum.n = %s;" % nVar
538                 print "    datum.keys = xmalloc(%s * sizeof *datum.keys);" % nVar
539                 if type.value:
540                     print "    datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
541                 else:
542                     print "    datum.values = NULL;"
543                 print "    for (i = 0; i < %s; i++) {" % nVar
544                 print cCopyType("        ", "row->%s[i]" % keyVar, "datum.keys[i].%s" % type.key, "%s[i]" % keyVar, type.key, type.keyRefTable)
545                 if type.value:
546                     print cCopyType("        ", "row->%s[i]" % valueVar, "datum.values[i].%s" % type.value, "%s[i]" % valueVar, type.value, type.valueRefTable)
547                 print "    }"
548             print "    ovsdb_idl_txn_write(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
549                 % {'s': structName,
550                    'S': structName.upper(),
551                    'C': columnName.upper()}
552             print "}"
553
554         # Table columns.
555         print "\nstatic struct ovsdb_idl_column %s_columns[%s_N_COLUMNS] = {" % (
556             structName, structName.upper())
557         for columnName, column in table.columns.iteritems():
558             type = column.type
559             
560             if type.value:
561                 valueTypeName = type.value.upper()
562             else:
563                 valueTypeName = "VOID"
564             if type.max == "unlimited":
565                 max = "UINT_MAX"
566             else:
567                 max = type.max
568             print "    {\"%s\", {OVSDB_TYPE_%s, OVSDB_TYPE_%s, %d, %s}}," % (
569                 columnName, type.key.upper(), valueTypeName,
570                 type.min, max)
571         print "};"
572
573     # Table classes.
574     print "\f"
575     print "static struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
576     for tableName, table in schema.tables.iteritems():
577         structName = "%s%s" % (prefix, tableName.lower())
578         print "    {\"%s\"," % tableName
579         print "     %s_columns, ARRAY_SIZE(%s_columns)," % (
580             structName, structName)
581         print "     sizeof(struct %s)," % structName
582         print "     %s_parse," % structName
583         print "     %s_unparse}," % structName
584     print "};"
585
586     # IDL class.
587     print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
588     print "    %stable_classes, ARRAY_SIZE(%stable_classes)" % (prefix, prefix)
589     print "};"
590
591 def ovsdb_escape(string):
592     def escape(match):
593         c = match.group(0)
594         if c == '\0':
595             raise Error("strings may not contain null bytes")
596         elif c == '\\':
597             return '\\\\'
598         elif c == '\n':
599             return '\\n'
600         elif c == '\r':
601             return '\\r'
602         elif c == '\t':
603             return '\\t'
604         elif c == '\b':
605             return '\\b'
606         elif c == '\a':
607             return '\\a'
608         else:
609             return '\\x%02x' % ord(c)
610     return re.sub(r'["\\\000-\037]', escape, string)
611
612 def printOVSDBSchema(schema):
613     json.dump(schema.toJson(), sys.stdout, sort_keys=True, indent=2)
614
615 def usage():
616     print """\
617 %(argv0)s: ovsdb schema compiler
618 usage: %(argv0)s [OPTIONS] ACTION SCHEMA
619 where SCHEMA is the ovsdb schema to read (in JSON format).
620
621 One of the following actions must specified:
622   validate                    validate schema without taking any other action
623   c-idl-header                print C header file for IDL
624   c-idl-source                print C source file for IDL implementation
625   ovsdb-schema                print ovsdb parseable schema
626
627 The following options are also available:
628   -h, --help                  display this help message
629   -V, --version               display version information\
630 """ % {'argv0': argv0}
631     sys.exit(0)
632
633 if __name__ == "__main__":
634     try:
635         try:
636             options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
637                                               ['help',
638                                                'version'])
639         except getopt.GetoptError, geo:
640             sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
641             sys.exit(1)
642             
643         optKeys = [key for key, value in options]
644         if '-h' in optKeys or '--help' in optKeys:
645             usage()
646         elif '-V' in optKeys or '--version' in optKeys:
647             print "ovsdb-idlc (Open vSwitch) @VERSION@"
648             sys.exit(0)
649
650         if len(args) != 2:
651             sys.stderr.write("%s: exactly two non-option arguments are "
652                              "required (use --help for help)\n" % argv0)
653             sys.exit(1)
654
655         action, inputFile = args
656         schema = parseSchema(inputFile)
657         if action == 'validate':
658             pass
659         elif action == 'ovsdb-schema':
660             printOVSDBSchema(schema)
661         elif action == 'c-idl-header':
662             printCIDLHeader(schema)
663         elif action == 'c-idl-source':
664             printCIDLSource(schema)
665         else:
666             sys.stderr.write(
667                 "%s: unknown action '%s' (use --help for help)\n" %
668                 (argv0, action))
669             sys.exit(1)
670     except Error, e:
671         sys.stderr.write("%s: %s\n" % (argv0, e.msg))
672         sys.exit(1)
673
674 # Local variables:
675 # mode: python
676 # End: