aboutsummaryrefslogtreecommitdiff
path: root/libevent-2.0.20-stable/event_rpcgen.py
blob: e051aa9d4e2c347f51ae530447c058e453a84824 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
#!/usr/bin/env python
#
# Copyright (c) 2005-2007 Niels Provos <provos@citi.umich.edu>
# Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
# All rights reserved.
#
# Generates marshaling code based on libevent.

# TODO:
# 1) use optparse to allow the strategy shell to parse options, and
#    to allow the instantiated factory (for the specific output language)
#    to parse remaining options
# 2) move the globals into a class that manages execution (including the
#    progress outputs that space stderr at the moment)
# 3) emit other languages

import sys
import re

_NAME = "event_rpcgen.py"
_VERSION = "0.1"

# Globals
line_count = 0

white = re.compile(r'\s+')
cppcomment = re.compile(r'\/\/.*$')
nonident = re.compile(r'[^a-zA-Z0-9_]')
structref = re.compile(r'^struct\[([a-zA-Z_][a-zA-Z0-9_]*)\]$')
structdef = re.compile(r'^struct +[a-zA-Z_][a-zA-Z0-9_]* *{$')

headerdirect = []
cppdirect = []

def TranslateList(mylist, mydict):
    return map(lambda x: x % mydict, mylist)

# Exception class for parse errors
class RpcGenError(Exception):
        def __init__(self, why):
                self.why = why
        def __str__(self):
                return str(self.why)

# Holds everything that makes a struct
class Struct:
    def __init__(self, name):
        self._name = name
        self._entries = []
        self._tags = {}
        print >>sys.stderr, '  Created struct: %s' % name

    def AddEntry(self, entry):
        if self._tags.has_key(entry.Tag()):
            raise RpcGenError(
                'Entry "%s" duplicates tag number %d from "%s" '
                'around line %d' % (entry.Name(), entry.Tag(),
                                    self._tags[entry.Tag()], line_count))
        self._entries.append(entry)
        self._tags[entry.Tag()] = entry.Name()
        print >>sys.stderr, '    Added entry: %s' % entry.Name()

    def Name(self):
        return self._name

    def EntryTagName(self, entry):
        """Creates the name inside an enumeration for distinguishing data
        types."""
        name = "%s_%s" % (self._name, entry.Name())
        return name.upper()

    def PrintIndented(self, file, ident, code):
        """Takes an array, add indentation to each entry and prints it."""
        for entry in code:
            print >>file, '%s%s' % (ident, entry)

class StructCCode(Struct):
    """ Knows how to generate C code for a struct """

    def __init__(self, name):
        Struct.__init__(self, name)

    def PrintTags(self, file):
        """Prints the tag definitions for a structure."""
        print >>file, '/* Tag definition for %s */' % self._name
        print >>file, 'enum %s_ {' % self._name.lower()
        for entry in self._entries:
            print >>file, '  %s=%d,' % (self.EntryTagName(entry),
                                        entry.Tag())
        print >>file, '  %s_MAX_TAGS' % (self._name.upper())
        print >>file, '};\n'

    def PrintForwardDeclaration(self, file):
        print >>file, 'struct %s;' % self._name

    def PrintDeclaration(self, file):
        print >>file, '/* Structure declaration for %s */' % self._name
        print >>file, 'struct %s_access_ {' % self._name
        for entry in self._entries:
            dcl = entry.AssignDeclaration('(*%s_assign)' % entry.Name())
            dcl.extend(
                entry.GetDeclaration('(*%s_get)' % entry.Name()))
            if entry.Array():
                dcl.extend(
                    entry.AddDeclaration('(*%s_add)' % entry.Name()))
            self.PrintIndented(file, '  ', dcl)
        print >>file, '};\n'

        print >>file, 'struct %s {' % self._name
        print >>file, '  struct %s_access_ *base;\n' % self._name
        for entry in self._entries:
            dcl = entry.Declaration()
            self.PrintIndented(file, '  ', dcl)
        print >>file, ''
        for entry in self._entries:
            print >>file, '  ev_uint8_t %s_set;' % entry.Name()
        print >>file, '};\n'

        print >>file, \
"""struct %(name)s *%(name)s_new(void);
struct %(name)s *%(name)s_new_with_arg(void *);
void %(name)s_free(struct %(name)s *);
void %(name)s_clear(struct %(name)s *);
void %(name)s_marshal(struct evbuffer *, const struct %(name)s *);
int %(name)s_unmarshal(struct %(name)s *, struct evbuffer *);
int %(name)s_complete(struct %(name)s *);
void evtag_marshal_%(name)s(struct evbuffer *, ev_uint32_t,
    const struct %(name)s *);
int evtag_unmarshal_%(name)s(struct evbuffer *, ev_uint32_t,
    struct %(name)s *);""" % { 'name' : self._name }


        # Write a setting function of every variable
        for entry in self._entries:
            self.PrintIndented(file, '', entry.AssignDeclaration(
                entry.AssignFuncName()))
            self.PrintIndented(file, '', entry.GetDeclaration(
                entry.GetFuncName()))
            if entry.Array():
                self.PrintIndented(file, '', entry.AddDeclaration(
                    entry.AddFuncName()))

        print >>file, '/* --- %s done --- */\n' % self._name

    def PrintCode(self, file):
        print >>file, ('/*\n'
                       ' * Implementation of %s\n'
                       ' */\n') % self._name

        print >>file, \
              'static struct %(name)s_access_ __%(name)s_base = {' % \
              { 'name' : self._name }
        for entry in self._entries:
            self.PrintIndented(file, '  ', entry.CodeBase())
        print >>file, '};\n'

        # Creation
        print >>file, (
            'struct %(name)s *\n'
            '%(name)s_new(void)\n'
            '{\n'
            '  return %(name)s_new_with_arg(NULL);\n'
            '}\n'
            '\n'
            'struct %(name)s *\n'
            '%(name)s_new_with_arg(void *unused)\n'
            '{\n'
            '  struct %(name)s *tmp;\n'
            '  if ((tmp = malloc(sizeof(struct %(name)s))) == NULL) {\n'
            '    event_warn("%%s: malloc", __func__);\n'
            '    return (NULL);\n'
            '  }\n'
            '  tmp->base = &__%(name)s_base;\n') % { 'name' : self._name }

        for entry in self._entries:
            self.PrintIndented(file, '  ', entry.CodeInitialize('tmp'))
            print >>file, '  tmp->%s_set = 0;\n' % entry.Name()

        print >>file, (
            '  return (tmp);\n'
            '}\n')

        # Adding
        for entry in self._entries:
            if entry.Array():
                self.PrintIndented(file, '', entry.CodeAdd())
            print >>file, ''

        # Assigning
        for entry in self._entries:
            self.PrintIndented(file, '', entry.CodeAssign())
            print >>file, ''

        # Getting
        for entry in self._entries:
            self.PrintIndented(file, '', entry.CodeGet())
            print >>file, ''

        # Clearing
        print >>file, ( 'void\n'
                        '%(name)s_clear(struct %(name)s *tmp)\n'
                        '{'
                        ) % { 'name' : self._name }
        for entry in self._entries:
            self.PrintIndented(file, '  ', entry.CodeClear('tmp'))

        print >>file, '}\n'

        # Freeing
        print >>file, ( 'void\n'
                        '%(name)s_free(struct %(name)s *tmp)\n'
                        '{'
                        ) % { 'name' : self._name }

        for entry in self._entries:
            self.PrintIndented(file, '  ', entry.CodeFree('tmp'))

        print >>file, ('  free(tmp);\n'
                       '}\n')

        # Marshaling
        print >>file, ('void\n'
                       '%(name)s_marshal(struct evbuffer *evbuf, '
                       'const struct %(name)s *tmp)'
                       '{') % { 'name' : self._name }
        for entry in self._entries:
            indent = '  '
            # Optional entries do not have to be set
            if entry.Optional():
                indent += '  '
                print >>file, '  if (tmp->%s_set) {' % entry.Name()
            self.PrintIndented(
                file, indent,
                entry.CodeMarshal('evbuf', self.EntryTagName(entry),
                                  entry.GetVarName('tmp'),
                                  entry.GetVarLen('tmp')))
            if entry.Optional():
                print >>file, '  }'

        print >>file, '}\n'

        # Unmarshaling
        print >>file, ('int\n'
                       '%(name)s_unmarshal(struct %(name)s *tmp, '
                       ' struct evbuffer *evbuf)\n'
                       '{\n'
                       '  ev_uint32_t tag;\n'
                       '  while (evbuffer_get_length(evbuf) > 0) {\n'
                       '    if (evtag_peek(evbuf, &tag) == -1)\n'
                       '      return (-1);\n'
                       '    switch (tag) {\n'
                       ) % { 'name' : self._name }
        for entry in self._entries:
            print >>file, '      case %s:\n' % self.EntryTagName(entry)
            if not entry.Array():
                print >>file, (
                    '        if (tmp->%s_set)\n'
                    '          return (-1);'
                    ) % (entry.Name())

            self.PrintIndented(
                file, '        ',
                entry.CodeUnmarshal('evbuf',
                                    self.EntryTagName(entry),
                                    entry.GetVarName('tmp'),
                                    entry.GetVarLen('tmp')))

            print >>file, ( '        tmp->%s_set = 1;\n' % entry.Name() +
                            '        break;\n' )
        print >>file, ( '      default:\n'
                        '        return -1;\n'
                        '    }\n'
                        '  }\n' )
        # Check if it was decoded completely
        print >>file, ( '  if (%(name)s_complete(tmp) == -1)\n'
                        '    return (-1);'
                        ) % { 'name' : self._name }

        # Successfully decoded
        print >>file, ( '  return (0);\n'
                        '}\n')

        # Checking if a structure has all the required data
        print >>file, (
            'int\n'
            '%(name)s_complete(struct %(name)s *msg)\n'
            '{' ) % { 'name' : self._name }
        for entry in self._entries:
            if not entry.Optional():
                code = [
                    'if (!msg->%(name)s_set)',
                    '  return (-1);' ]
                code = TranslateList(code, entry.GetTranslation())
                self.PrintIndented(
                    file, '  ', code)

            self.PrintIndented(
                file, '  ',
                entry.CodeComplete('msg', entry.GetVarName('msg')))
        print >>file, (
            '  return (0);\n'
            '}\n' )

        # Complete message unmarshaling
        print >>file, (
            'int\n'
            'evtag_unmarshal_%(name)s(struct evbuffer *evbuf, '
            'ev_uint32_t need_tag, struct %(name)s *msg)\n'
            '{\n'
            '  ev_uint32_t tag;\n'
            '  int res = -1;\n'
            '\n'
            '  struct evbuffer *tmp = evbuffer_new();\n'
            '\n'
            '  if (evtag_unmarshal(evbuf, &tag, tmp) == -1'
            ' || tag != need_tag)\n'
            '    goto error;\n'
            '\n'
            '  if (%(name)s_unmarshal(msg, tmp) == -1)\n'
            '    goto error;\n'
            '\n'
            '  res = 0;\n'
            '\n'
            ' error:\n'
            '  evbuffer_free(tmp);\n'
            '  return (res);\n'
            '}\n' ) % { 'name' : self._name }

        # Complete message marshaling
        print >>file, (
            'void\n'
            'evtag_marshal_%(name)s(struct evbuffer *evbuf, ev_uint32_t tag, '
            'const struct %(name)s *msg)\n'
            '{\n'
            '  struct evbuffer *_buf = evbuffer_new();\n'
            '  assert(_buf != NULL);\n'
            '  %(name)s_marshal(_buf, msg);\n'
            '  evtag_marshal_buffer(evbuf, tag, _buf);\n '
            '  evbuffer_free(_buf);\n'
            '}\n' ) % { 'name' : self._name }

class Entry:
    def __init__(self, type, name, tag):
        self._type = type
        self._name = name
        self._tag = int(tag)
        self._ctype = type
        self._optional = 0
        self._can_be_array = 0
        self._array = 0
        self._line_count = -1
        self._struct = None
        self._refname = None

        self._optpointer = True
        self._optaddarg = True

    def GetInitializer(self):
        assert 0, "Entry does not provide initializer"

    def SetStruct(self, struct):
        self._struct = struct

    def LineCount(self):
        assert self._line_count != -1
        return self._line_count

    def SetLineCount(self, number):
        self._line_count = number

    def Array(self):
        return self._array

    def Optional(self):
        return self._optional

    def Tag(self):
        return self._tag

    def Name(self):
        return self._name

    def Type(self):
        return self._type

    def MakeArray(self, yes=1):
        self._array = yes

    def MakeOptional(self):
        self._optional = 1

    def Verify(self):
        if self.Array() and not self._can_be_array:
            raise RpcGenError(
                'Entry "%s" cannot be created as an array '
                'around line %d' % (self._name, self.LineCount()))
        if not self._struct:
            raise RpcGenError(
                'Entry "%s" does not know which struct it belongs to '
                'around line %d' % (self._name, self.LineCount()))
        if self._optional and self._array:
            raise RpcGenError(
                'Entry "%s" has illegal combination of optional and array '
                'around line %d' % (self._name, self.LineCount()))

    def GetTranslation(self, extradict = {}):
        mapping = {
            "parent_name" : self._struct.Name(),
            "name" : self._name,
            "ctype" : self._ctype,
            "refname" : self._refname,
            "optpointer" : self._optpointer and "*" or "",
            "optreference" : self._optpointer and "&" or "",
            "optaddarg" :
            self._optaddarg and ", const %s value" % self._ctype or ""
            }
        for (k, v) in extradict.items():
            mapping[k] = v

        return mapping

    def GetVarName(self, var):
        return '%(var)s->%(name)s_data' % self.GetTranslation({ 'var' : var })

    def GetVarLen(self, var):
        return 'sizeof(%s)' % self._ctype

    def GetFuncName(self):
        return '%s_%s_get' % (self._struct.Name(), self._name)

    def GetDeclaration(self, funcname):
        code = [ 'int %s(struct %s *, %s *);' % (
            funcname, self._struct.Name(), self._ctype ) ]
        return code

    def CodeGet(self):
        code = (
            'int',
            '%(parent_name)s_%(name)s_get(struct %(parent_name)s *msg, '
            '%(ctype)s *value)',
            '{',
            '  if (msg->%(name)s_set != 1)',
            '    return (-1);',
            '  *value = msg->%(name)s_data;',
            '  return (0);',
            '}' )
        code = '\n'.join(code)
        code = code % self.GetTranslation()
        return code.split('\n')

    def AssignFuncName(self):
        return '%s_%s_assign' % (self._struct.Name(), self._name)

    def AddFuncName(self):
        return '%s_%s_add' % (self._struct.Name(), self._name)

    def AssignDeclaration(self, funcname):
        code = [ 'int %s(struct %s *, const %s);' % (
            funcname, self._struct.Name(), self._ctype ) ]
        return code

    def CodeAssign(self):
        code = [ 'int',
                 '%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg,'
                 ' const %(ctype)s value)',
                 '{',
                 '  msg->%(name)s_set = 1;',
                 '  msg->%(name)s_data = value;',
                 '  return (0);',
                 '}' ]
        code = '\n'.join(code)
        code = code % self.GetTranslation()
        return code.split('\n')

    def CodeClear(self, structname):
        code = [ '%s->%s_set = 0;' % (structname, self.Name()) ]

        return code

    def CodeComplete(self, structname, var_name):
        return []

    def CodeFree(self, name):
        return []

    def CodeBase(self):
        code = [
            '%(parent_name)s_%(name)s_assign,',
            '%(parent_name)s_%(name)s_get,'
            ]
        if self.Array():
            code.append('%(parent_name)s_%(name)s_add,')

        code = '\n'.join(code)
        code = code % self.GetTranslation()
        return code.split('\n')

class EntryBytes(Entry):
    def __init__(self, type, name, tag, length):
        # Init base class
        Entry.__init__(self, type, name, tag)

        self._length = length
        self._ctype = 'ev_uint8_t'

    def GetInitializer(self):
        return "NULL"

    def GetVarLen(self, var):
        return '(%s)' % self._length

    def CodeArrayAdd(self, varname, value):
        # XXX: copy here
        return [ '%(varname)s = NULL;' % { 'varname' : varname } ]

    def GetDeclaration(self, funcname):
        code = [ 'int %s(struct %s *, %s **);' % (
            funcname, self._struct.Name(), self._ctype ) ]
        return code

    def AssignDeclaration(self, funcname):
        code = [ 'int %s(struct %s *, const %s *);' % (
            funcname, self._struct.Name(), self._ctype ) ]
        return code

    def Declaration(self):
        dcl  = ['ev_uint8_t %s_data[%s];' % (self._name, self._length)]

        return dcl

    def CodeGet(self):
        name = self._name
        code = [ 'int',
                 '%s_%s_get(struct %s *msg, %s **value)' % (
            self._struct.Name(), name,
            self._struct.Name(), self._ctype),
                 '{',
                 '  if (msg->%s_set != 1)' % name,
                 '    return (-1);',
                 '  *value = msg->%s_data;' % name,
                 '  return (0);',
                 '}' ]
        return code

    def CodeAssign(self):
        name = self._name
        code = [ 'int',
                 '%s_%s_assign(struct %s *msg, const %s *value)' % (
            self._struct.Name(), name,
            self._struct.Name(), self._ctype),
                 '{',
                 '  msg->%s_set = 1;' % name,
                 '  memcpy(msg->%s_data, value, %s);' % (
            name, self._length),
                 '  return (0);',
                 '}' ]
        return code

    def CodeUnmarshal(self, buf, tag_name, var_name, var_len):
        code = [  'if (evtag_unmarshal_fixed(%(buf)s, %(tag)s, '
                  '%(var)s, %(varlen)s) == -1) {',
                  '  event_warnx("%%s: failed to unmarshal %(name)s", __func__);',
                  '  return (-1);',
                  '}'
                  ]
        return TranslateList(code,
                             self.GetTranslation({
            'var' : var_name,
            'varlen' : var_len,
            'buf' : buf,
            'tag' : tag_name }))

    def CodeMarshal(self, buf, tag_name, var_name, var_len):
        code = ['evtag_marshal(%s, %s, %s, %s);' % (
            buf, tag_name, var_name, var_len)]
        return code

    def CodeClear(self, structname):
        code = [ '%s->%s_set = 0;' % (structname, self.Name()),
                 'memset(%s->%s_data, 0, sizeof(%s->%s_data));' % (
            structname, self._name, structname, self._name)]

        return code

    def CodeInitialize(self, name):
        code  = ['memset(%s->%s_data, 0, sizeof(%s->%s_data));' % (
            name, self._name, name, self._name)]
        return code

    def Verify(self):
        if not self._length:
            raise RpcGenError(
                'Entry "%s" needs a length '
                'around line %d' % (self._name, self.LineCount()))

        Entry.Verify(self)

class EntryInt(Entry):
    def __init__(self, type, name, tag, bits=32):
        # Init base class
        Entry.__init__(self, type, name, tag)

        self._can_be_array = 1
        if bits == 32:
            self._ctype = 'ev_uint32_t'
            self._marshal_type = 'int'
        if bits == 64:
            self._ctype = 'ev_uint64_t'
            self._marshal_type = 'int64'

    def GetInitializer(self):
        return "0"

    def CodeArrayFree(self, var):
        return []

    def CodeArrayAssign(self, varname, srcvar):
        return [ '%(varname)s = %(srcvar)s;' % { 'varname' : varname,
                                                'srcvar' : srcvar } ]

    def CodeArrayAdd(self, varname, value):
        """Returns a new entry of this type."""
        return [ '%(varname)s = %(value)s;' % { 'varname' : varname,
                                              'value' : value } ]

    def CodeUnmarshal(self, buf, tag_name, var_name, var_len):
        code = [
            'if (evtag_unmarshal_%(ma)s(%(buf)s, %(tag)s, &%(var)s) == -1) {',
            '  event_warnx("%%s: failed to unmarshal %(name)s", __func__);',
            '  return (-1);',
            '}' ]
        code = '\n'.join(code) % self.GetTranslation({
            'ma'  : self._marshal_type,
            'buf' : buf,
            'tag' : tag_name,
            'var' : var_name })
        return code.split('\n')

    def CodeMarshal(self, buf, tag_name, var_name, var_len):
        code = [
            'evtag_marshal_%s(%s, %s, %s);' % (
            self._marshal_type, buf, tag_name, var_name)]
        return code

    def Declaration(self):
        dcl  = ['%s %s_data;' % (self._ctype, self._name)]

        return dcl

    def CodeInitialize(self, name):
        code = ['%s->%s_data = 0;' % (name, self._name)]
        return code

class EntryString(Entry):
    def __init__(self, type, name, tag):
        # Init base class
        Entry.__init__(self, type, name, tag)

        self._can_be_array = 1
        self._ctype = 'char *'

    def GetInitializer(self):
        return "NULL"

    def CodeArrayFree(self, varname):
        code = [
            'if (%(var)s != NULL) free(%(var)s);' ]

        return TranslateList(code, { 'var' : varname })

    def CodeArrayAssign(self, varname, srcvar):
        code = [
            'if (%(var)s != NULL)',
            '  free(%(var)s);',
            '%(var)s = strdup(%(srcvar)s);',
            'if (%(var)s == NULL) {',
            '  event_warnx("%%s: strdup", __func__);',
            '  return (-1);',
            '}' ]

        return TranslateList(code, { 'var' : varname,
                                     'srcvar' : srcvar })

    def CodeArrayAdd(self, varname, value):
        code = [
            'if (%(value)s != NULL) {',
            '  %(var)s = strdup(%(value)s);',
            '  if (%(var)s == NULL) {',
            '    goto error;',
            '  }',
            '} else {',
            '  %(var)s = NULL;',
            '}' ]

        return TranslateList(code, { 'var' : varname,
                                     'value' : value })

    def GetVarLen(self, var):
        return 'strlen(%s)' % self.GetVarName(var)

    def CodeMakeInitalize(self, varname):
        return '%(varname)s = NULL;' % { 'varname' : varname }

    def CodeAssign(self):
        name = self._name
        code = """int
%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg,
    const %(ctype)s value)
{
  if (msg->%(name)s_data != NULL)
    free(msg->%(name)s_data);
  if ((msg->%(name)s_data = strdup(value)) == NULL)
    return (-1);
  msg->%(name)s_set = 1;
  return (0);
}""" % self.GetTranslation()

        return code.split('\n')

    def CodeUnmarshal(self, buf, tag_name, var_name, var_len):
        code = ['if (evtag_unmarshal_string(%(buf)s, %(tag)s, &%(var)s) == -1) {',
                '  event_warnx("%%s: failed to unmarshal %(name)s", __func__);',
                '  return (-1);',
                '}'
                ]
        code = '\n'.join(code) % self.GetTranslation({
            'buf' : buf,
            'tag' : tag_name,
            'var' : var_name })
        return code.split('\n')

    def CodeMarshal(self, buf, tag_name, var_name, var_len):
        code = ['evtag_marshal_string(%s, %s, %s);' % (
            buf, tag_name, var_name)]
        return code

    def CodeClear(self, structname):
        code = [ 'if (%s->%s_set == 1) {' % (structname, self.Name()),
                 '  free(%s->%s_data);' % (structname, self.Name()),
                 '  %s->%s_data = NULL;' % (structname, self.Name()),
                 '  %s->%s_set = 0;' % (structname, self.Name()),
                 '}'
                 ]

        return code

    def CodeInitialize(self, name):
        code  = ['%s->%s_data = NULL;' % (name, self._name)]
        return code

    def CodeFree(self, name):
        code  = ['if (%s->%s_data != NULL)' % (name, self._name),
                 '    free (%s->%s_data);' % (name, self._name)]

        return code

    def Declaration(self):
        dcl  = ['char *%s_data;' % self._name]

        return dcl

class EntryStruct(Entry):
    def __init__(self, type, name, tag, refname):
        # Init base class
        Entry.__init__(self, type, name, tag)

        self._optpointer = False
        self._can_be_array = 1
        self._refname = refname
        self._ctype = 'struct %s*' % refname
        self._optaddarg = False

    def GetInitializer(self):
        return "NULL"

    def GetVarLen(self, var):
        return '-1'

    def CodeArrayAdd(self, varname, value):
        code = [
            '%(varname)s = %(refname)s_new();',
            'if (%(varname)s == NULL)',
            '  goto error;' ]

        return TranslateList(code, self.GetTranslation({ 'varname' : varname }))

    def CodeArrayFree(self, var):
        code = [ '%(refname)s_free(%(var)s);' % self.GetTranslation(
            { 'var' : var }) ]
        return code

    def CodeArrayAssign(self, var, srcvar):
        code = [
            'int had_error = 0;',
            'struct evbuffer *tmp = NULL;',
            '%(refname)s_clear(%(var)s);',
            'if ((tmp = evbuffer_new()) == NULL) {',
            '  event_warn("%%s: evbuffer_new()", __func__);',
            '  had_error = 1;',
            '  goto done;',
            '}',
            '%(refname)s_marshal(tmp, %(srcvar)s);',
            'if (%(refname)s_unmarshal(%(var)s, tmp) == -1) {',
            '  event_warnx("%%s: %(refname)s_unmarshal", __func__);',
            '  had_error = 1;',
            '  goto done;',
            '}',
            'done:'
            'if (tmp != NULL)',
            '  evbuffer_free(tmp);',
            'if (had_error) {',
            '  %(refname)s_clear(%(var)s);',
            '  return (-1);',
            '}' ]

        return TranslateList(code, self.GetTranslation({
            'var' : var,
            'srcvar' : srcvar}))

    def CodeGet(self):
        name = self._name
        code = [ 'int',
                 '%s_%s_get(struct %s *msg, %s *value)' % (
            self._struct.Name(), name,
            self._struct.Name(), self._ctype),
                 '{',
                 '  if (msg->%s_set != 1) {' % name,
                 '    msg->%s_data = %s_new();' % (name, self._refname),
                 '    if (msg->%s_data == NULL)' % name,
                 '      return (-1);',
                 '    msg->%s_set = 1;' % name,
                 '  }',
                 '  *value = msg->%s_data;' % name,
                 '  return (0);',
                 '}' ]
        return code

    def CodeAssign(self):
        name = self._name
        code = """int
%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg,
    const %(ctype)s value)
{
   struct evbuffer *tmp = NULL;
   if (msg->%(name)s_set) {
     %(refname)s_clear(msg->%(name)s_data);
     msg->%(name)s_set = 0;
   } else {
     msg->%(name)s_data = %(refname)s_new();
     if (msg->%(name)s_data == NULL) {
       event_warn("%%s: %(refname)s_new()", __func__);
       goto error;
     }
   }
   if ((tmp = evbuffer_new()) == NULL) {
     event_warn("%%s: evbuffer_new()", __func__);
     goto error;
   }
   %(refname)s_marshal(tmp, value);
   if (%(refname)s_unmarshal(msg->%(name)s_data, tmp) == -1) {
     event_warnx("%%s: %(refname)s_unmarshal", __func__);
     goto error;
   }
   msg->%(name)s_set = 1;
   evbuffer_free(tmp);
   return (0);
 error:
   if (tmp != NULL)
     evbuffer_free(tmp);
   if (msg->%(name)s_data != NULL) {
     %(refname)s_free(msg->%(name)s_data);
     msg->%(name)s_data = NULL;
   }
   return (-1);
}""" % self.GetTranslation()
        return code.split('\n')

    def CodeComplete(self, structname, var_name):
        code = [ 'if (%(structname)s->%(name)s_set && '
                 '%(refname)s_complete(%(var)s) == -1)',
                 '  return (-1);' ]

        return TranslateList(code, self.GetTranslation({
            'structname' : structname,
            'var' : var_name }))

    def CodeUnmarshal(self, buf, tag_name, var_name, var_len):
        code = ['%(var)s = %(refname)s_new();',
                'if (%(var)s == NULL)',
                '  return (-1);',
                'if (evtag_unmarshal_%(refname)s(%(buf)s, %(tag)s, '
                '%(var)s) == -1) {',
                  '  event_warnx("%%s: failed to unmarshal %(name)s", __func__);',
                '  return (-1);',
                '}'
                ]
        code = '\n'.join(code) % self.GetTranslation({
            'buf' : buf,
            'tag' : tag_name,
            'var' : var_name })
        return code.split('\n')

    def CodeMarshal(self, buf, tag_name, var_name, var_len):
        code = ['evtag_marshal_%s(%s, %s, %s);' % (
            self._refname, buf, tag_name, var_name)]
        return code

    def CodeClear(self, structname):
        code = [ 'if (%s->%s_set == 1) {' % (structname, self.Name()),
                 '  %s_free(%s->%s_data);' % (
            self._refname, structname, self.Name()),
                 '  %s->%s_data = NULL;' % (structname, self.Name()),
                 '  %s->%s_set = 0;' % (structname, self.Name()),
                 '}'
                 ]

        return code

    def CodeInitialize(self, name):
        code  = ['%s->%s_data = NULL;' % (name, self._name)]
        return code

    def CodeFree(self, name):
        code  = ['if (%s->%s_data != NULL)' % (name, self._name),
                 '    %s_free(%s->%s_data);' % (
            self._refname, name, self._name)]

        return code

    def Declaration(self):
        dcl  = ['%s %s_data;' % (self._ctype, self._name)]

        return dcl

class EntryVarBytes(Entry):
    def __init__(self, type, name, tag):
        # Init base class
        Entry.__init__(self, type, name, tag)

        self._ctype = 'ev_uint8_t *'

    def GetInitializer(self):
        return "NULL"

    def GetVarLen(self, var):
        return '%(var)s->%(name)s_length' % self.GetTranslation({ 'var' : var })

    def CodeArrayAdd(self, varname, value):
        # xxx: copy
        return [ '%(varname)s = NULL;' % { 'varname' : varname } ]

    def GetDeclaration(self, funcname):
        code = [ 'int %s(struct %s *, %s *, ev_uint32_t *);' % (
            funcname, self._struct.Name(), self._ctype ) ]
        return code

    def AssignDeclaration(self, funcname):
        code = [ 'int %s(struct %s *, const %s, ev_uint32_t);' % (
            funcname, self._struct.Name(), self._ctype ) ]
        return code

    def CodeAssign(self):
        name = self._name
        code = [ 'int',
                 '%s_%s_assign(struct %s *msg, '
                 'const %s value, ev_uint32_t len)' % (
            self._struct.Name(), name,
            self._struct.Name(), self._ctype),
                 '{',
                 '  if (msg->%s_data != NULL)' % name,
                 '    free (msg->%s_data);' % name,
                 '  msg->%s_data = malloc(len);' % name,
                 '  if (msg->%s_data == NULL)' % name,
                 '    return (-1);',
                 '  msg->%s_set = 1;' % name,
                 '  msg->%s_length = len;' % name,
                 '  memcpy(msg->%s_data, value, len);' % name,
                 '  return (0);',
                 '}' ]
        return code

    def CodeGet(self):
        name = self._name
        code = [ 'int',
                 '%s_%s_get(struct %s *msg, %s *value, ev_uint32_t *plen)' % (
            self._struct.Name(), name,
            self._struct.Name(), self._ctype),
                 '{',
                 '  if (msg->%s_set != 1)' % name,
                 '    return (-1);',
                 '  *value = msg->%s_data;' % name,
                 '  *plen = msg->%s_length;' % name,
                 '  return (0);',
                 '}' ]
        return code

    def CodeUnmarshal(self, buf, tag_name, var_name, var_len):
        code = ['if (evtag_payload_length(%(buf)s, &%(varlen)s) == -1)',
                '  return (-1);',
                # We do not want DoS opportunities
                'if (%(varlen)s > evbuffer_get_length(%(buf)s))',
                '  return (-1);',
                'if ((%(var)s = malloc(%(varlen)s)) == NULL)',
                '  return (-1);',
                'if (evtag_unmarshal_fixed(%(buf)s, %(tag)s, %(var)s, '
                '%(varlen)s) == -1) {',
                '  event_warnx("%%s: failed to unmarshal %(name)s", __func__);',
                '  return (-1);',
                '}'
                ]
        code = '\n'.join(code) % self.GetTranslation({
            'buf' : buf,
            'tag' : tag_name,
            'var' : var_name,
            'varlen' : var_len })
        return code.split('\n')

    def CodeMarshal(self, buf, tag_name, var_name, var_len):
        code = ['evtag_marshal(%s, %s, %s, %s);' % (
            buf, tag_name, var_name, var_len)]
        return code

    def CodeClear(self, structname):
        code = [ 'if (%s->%s_set == 1) {' % (structname, self.Name()),
                 '  free (%s->%s_data);' % (structname, self.Name()),
                 '  %s->%s_data = NULL;' % (structname, self.Name()),
                 '  %s->%s_length = 0;' % (structname, self.Name()),
                 '  %s->%s_set = 0;' % (structname, self.Name()),
                 '}'
                 ]

        return code

    def CodeInitialize(self, name):
        code  = ['%s->%s_data = NULL;' % (name, self._name),
                 '%s->%s_length = 0;' % (name, self._name) ]
        return code

    def CodeFree(self, name):
        code  = ['if (%s->%s_data != NULL)' % (name, self._name),
                 '    free(%s->%s_data);' % (name, self._name)]

        return code

    def Declaration(self):
        dcl  = ['ev_uint8_t *%s_data;' % self._name,
                'ev_uint32_t %s_length;' % self._name]

        return dcl

class EntryArray(Entry):
    def __init__(self, entry):
        # Init base class
        Entry.__init__(self, entry._type, entry._name, entry._tag)

        self._entry = entry
        self._refname = entry._refname
        self._ctype = self._entry._ctype
        self._optional = True
        self._optpointer = self._entry._optpointer
        self._optaddarg = self._entry._optaddarg

        # provide a new function for accessing the variable name
        def GetVarName(var_name):
            return '%(var)s->%(name)s_data[%(index)s]' % \
                   self._entry.GetTranslation({'var' : var_name,
                                               'index' : self._index})
        self._entry.GetVarName = GetVarName

    def GetInitializer(self):
        return "NULL"

    def GetVarName(self, var_name):
        return var_name

    def GetVarLen(self, var_name):
        return '-1'

    def GetDeclaration(self, funcname):
        """Allows direct access to elements of the array."""
        code = [
            'int %(funcname)s(struct %(parent_name)s *, int, %(ctype)s *);' %
            self.GetTranslation({ 'funcname' : funcname }) ]
        return code

    def AssignDeclaration(self, funcname):
        code = [ 'int %s(struct %s *, int, const %s);' % (
            funcname, self._struct.Name(), self._ctype ) ]
        return code

    def AddDeclaration(self, funcname):
        code = [
            '%(ctype)s %(optpointer)s '
            '%(funcname)s(struct %(parent_name)s *msg%(optaddarg)s);' % \
            self.GetTranslation({ 'funcname' : funcname }) ]
        return code

    def CodeGet(self):
        code = """int
%(parent_name)s_%(name)s_get(struct %(parent_name)s *msg, int offset,
    %(ctype)s *value)
{
  if (!msg->%(name)s_set || offset < 0 || offset >= msg->%(name)s_length)
    return (-1);
  *value = msg->%(name)s_data[offset];
  return (0);
}""" % self.GetTranslation()

        return code.split('\n')

    def CodeAssign(self):
        code = [
            'int',
            '%(parent_name)s_%(name)s_assign(struct %(parent_name)s *msg, int off,',
            '    const %(ctype)s value)',
            '{',
            '  if (!msg->%(name)s_set || off < 0 || off >= msg->%(name)s_length)',
            '    return (-1);\n',
            '  {' ]
        code = TranslateList(code, self.GetTranslation())

        codearrayassign = self._entry.CodeArrayAssign(
            'msg->%(name)s_data[off]' % self.GetTranslation(), 'value')
        code += map(lambda x: '    ' + x, codearrayassign)

        code += TranslateList([
            '  }',
            '  return (0);',
            '}' ], self.GetTranslation())

        return code

    def CodeAdd(self):
        codearrayadd = self._entry.CodeArrayAdd(
            'msg->%(name)s_data[msg->%(name)s_length - 1]' % self.GetTranslation(),
            'value')
        code = [
            'static int',
            '%(parent_name)s_%(name)s_expand_to_hold_more('
            'struct %(parent_name)s *msg)',
            '{',
            '  int tobe_allocated = msg->%(name)s_num_allocated;',
            '  %(ctype)s* new_data = NULL;',
            '  tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;',
            '  new_data = (%(ctype)s*) realloc(msg->%(name)s_data,',
            '      tobe_allocated * sizeof(%(ctype)s));',
            '  if (new_data == NULL)',
            '    return -1;',
            '  msg->%(name)s_data = new_data;',
            '  msg->%(name)s_num_allocated = tobe_allocated;',
            '  return 0;'
            '}',
            '',
            '%(ctype)s %(optpointer)s',
            '%(parent_name)s_%(name)s_add('
            'struct %(parent_name)s *msg%(optaddarg)s)',
            '{',
            '  if (++msg->%(name)s_length >= msg->%(name)s_num_allocated) {',
            '    if (%(parent_name)s_%(name)s_expand_to_hold_more(msg)<0)',
            '      goto error;',
            '  }' ]

        code = TranslateList(code, self.GetTranslation())

        code += map(lambda x: '  ' + x, codearrayadd)

        code += TranslateList([
            '  msg->%(name)s_set = 1;',
            '  return %(optreference)s(msg->%(name)s_data['
            'msg->%(name)s_length - 1]);',
            'error:',
            '  --msg->%(name)s_length;',
            '  return (NULL);',
            '}' ], self.GetTranslation())

        return code

    def CodeComplete(self, structname, var_name):
        self._index = 'i'
        tmp = self._entry.CodeComplete(structname, self._entry.GetVarName(var_name))
        # skip the whole loop if there is nothing to check
        if not tmp:
            return []

        translate = self.GetTranslation({ 'structname' : structname })
        code = [
            '{',
            '  int i;',
            '  for (i = 0; i < %(structname)s->%(name)s_length; ++i) {' ]

        code = TranslateList(code, translate)

        code += map(lambda x: '    ' + x, tmp)

        code += [
            '  }',
            '}' ]

        return code

    def CodeUnmarshal(self, buf, tag_name, var_name, var_len):
        translate = self.GetTranslation({ 'var' : var_name,
                                          'buf' : buf,
                                          'tag' : tag_name,
                                          'init' : self._entry.GetInitializer()})
        code = [
            'if (%(var)s->%(name)s_length >= %(var)s->%(name)s_num_allocated &&',
            '    %(parent_name)s_%(name)s_expand_to_hold_more(%(var)s) < 0) {',
            '  puts("HEY NOW");',
            '  return (-1);',
            '}']

        # the unmarshal code directly returns
        code = TranslateList(code, translate)

        self._index = '%(var)s->%(name)s_length' % translate
        code += self._entry.CodeUnmarshal(buf, tag_name,
                                        self._entry.GetVarName(var_name),
                                        self._entry.GetVarLen(var_name))

        code += [ '++%(var)s->%(name)s_length;' % translate ]

        return code

    def CodeMarshal(self, buf, tag_name, var_name, var_len):
        code = ['{',
                '  int i;',
                '  for (i = 0; i < %(var)s->%(name)s_length; ++i) {' ]

        self._index = 'i'
        code += self._entry.CodeMarshal(buf, tag_name,
                                        self._entry.GetVarName(var_name),
                                        self._entry.GetVarLen(var_name))
        code += ['  }',
                 '}'
                 ]

        code = "\n".join(code) % self.GetTranslation({ 'var' : var_name })

        return code.split('\n')

    def CodeClear(self, structname):
        translate = self.GetTranslation({ 'structname' : structname })
        codearrayfree = self._entry.CodeArrayFree(
            '%(structname)s->%(name)s_data[i]' % self.GetTranslation(
            { 'structname' : structname } ))

        code = [ 'if (%(structname)s->%(name)s_set == 1) {' ]

        if codearrayfree:
            code += [
                '  int i;',
                '  for (i = 0; i < %(structname)s->%(name)s_length; ++i) {' ]

        code = TranslateList(code, translate)

        if codearrayfree:
            code += map(lambda x: '    ' + x, codearrayfree)
            code += [
                '  }' ]

        code += TranslateList([
                 '  free(%(structname)s->%(name)s_data);',
                 '  %(structname)s->%(name)s_data = NULL;',
                 '  %(structname)s->%(name)s_set = 0;',
                 '  %(structname)s->%(name)s_length = 0;',
                 '  %(structname)s->%(name)s_num_allocated = 0;',
                 '}'
                 ], translate)

        return code

    def CodeInitialize(self, name):
        code  = ['%s->%s_data = NULL;' % (name, self._name),
                 '%s->%s_length = 0;' % (name, self._name),
                 '%s->%s_num_allocated = 0;' % (name, self._name)]
        return code

    def CodeFree(self, structname):
        code = self.CodeClear(structname);

        code += TranslateList([
            'free(%(structname)s->%(name)s_data);' ],
                              self.GetTranslation({'structname' : structname }))

        return code

    def Declaration(self):
        dcl  = ['%s *%s_data;' % (self._ctype, self._name),
                'int %s_length;' % self._name,
                'int %s_num_allocated;' % self._name ]

        return dcl

def NormalizeLine(line):
    global white
    global cppcomment

    line = cppcomment.sub('', line)
    line = line.strip()
    line = white.sub(' ', line)

    return line

def ProcessOneEntry(factory, newstruct, entry):
    optional = 0
    array = 0
    entry_type = ''
    name = ''
    tag = ''
    tag_set = None
    separator = ''
    fixed_length = ''

    tokens = entry.split(' ')
    while tokens:
        token = tokens[0]
        tokens = tokens[1:]

        if not entry_type:
            if not optional and token == 'optional':
                optional = 1
                continue

            if not array and token == 'array':
                array = 1
                continue

        if not entry_type:
            entry_type = token
            continue

        if not name:
            res = re.match(r'^([^\[\]]+)(\[.*\])?$', token)
            if not res:
                 raise RpcGenError(
                     'Cannot parse name: \"%s\" '
                     'around line %d' % (entry, line_count))
            name = res.group(1)
            fixed_length = res.group(2)
            if fixed_length:
                fixed_length = fixed_length[1:-1]
            continue

        if not separator:
            separator = token
            if separator != '=':
                 raise RpcGenError('Expected "=" after name \"%s\" got %s'
                                   % (name, token))
            continue

        if not tag_set:
            tag_set = 1
            if not re.match(r'^(0x)?[0-9]+$', token):
                raise RpcGenError('Expected tag number: \"%s\"' % entry)
            tag = int(token, 0)
            continue

        raise RpcGenError('Cannot parse \"%s\"' % entry)

    if not tag_set:
        raise RpcGenError('Need tag number: \"%s\"' % entry)

    # Create the right entry
    if entry_type == 'bytes':
        if fixed_length:
            newentry = factory.EntryBytes(entry_type, name, tag, fixed_length)
        else:
            newentry = factory.EntryVarBytes(entry_type, name, tag)
    elif entry_type == 'int' and not fixed_length:
        newentry = factory.EntryInt(entry_type, name, tag)
    elif entry_type == 'int64' and not fixed_length:
        newentry = factory.EntryInt(entry_type, name, tag, bits=64)
    elif entry_type == 'string' and not fixed_length:
        newentry = factory.EntryString(entry_type, name, tag)
    else:
        res = structref.match(entry_type)
        if res:
            # References another struct defined in our file
            newentry = factory.EntryStruct(entry_type, name, tag, res.group(1))
        else:
            raise RpcGenError('Bad type: "%s" in "%s"' % (entry_type, entry))

    structs = []

    if optional:
        newentry.MakeOptional()
    if array:
        newentry.MakeArray()

    newentry.SetStruct(newstruct)
    newentry.SetLineCount(line_count)
    newentry.Verify()

    if array:
        # We need to encapsulate this entry into a struct
        newname = newentry.Name()+ '_array'

        # Now borgify the new entry.
        newentry = factory.EntryArray(newentry)
        newentry.SetStruct(newstruct)
        newentry.SetLineCount(line_count)
        newentry.MakeArray()

    newstruct.AddEntry(newentry)

    return structs

def ProcessStruct(factory, data):
    tokens = data.split(' ')

    # First three tokens are: 'struct' 'name' '{'
    newstruct = factory.Struct(tokens[1])

    inside = ' '.join(tokens[3:-1])

    tokens = inside.split(';')

    structs = []

    for entry in tokens:
        entry = NormalizeLine(entry)
        if not entry:
            continue

        # It's possible that new structs get defined in here
        structs.extend(ProcessOneEntry(factory, newstruct, entry))

    structs.append(newstruct)
    return structs

def GetNextStruct(file):
    global line_count
    global cppdirect

    got_struct = 0

    processed_lines = []

    have_c_comment = 0
    data = ''
    while 1:
        line = file.readline()
        if not line:
            break

        line_count += 1
        line = line[:-1]

        if not have_c_comment and re.search(r'/\*', line):
            if re.search(r'/\*.*?\*/', line):
                line = re.sub(r'/\*.*?\*/', '', line)
            else:
                line = re.sub(r'/\*.*$', '', line)
                have_c_comment = 1

        if have_c_comment:
            if not re.search(r'\*/', line):
                continue
            have_c_comment = 0
            line = re.sub(r'^.*\*/', '', line)

        line = NormalizeLine(line)

        if not line:
            continue

        if not got_struct:
            if re.match(r'#include ["<].*[>"]', line):
                cppdirect.append(line)
                continue

            if re.match(r'^#(if( |def)|endif)', line):
                cppdirect.append(line)
                continue

            if re.match(r'^#define', line):
                headerdirect.append(line)
                continue

            if not structdef.match(line):
                raise RpcGenError('Missing struct on line %d: %s'
                                  % (line_count, line))
            else:
                got_struct = 1
                data += line
            continue

        # We are inside the struct
        tokens = line.split('}')
        if len(tokens) == 1:
            data += ' ' + line
            continue

        if len(tokens[1]):
            raise RpcGenError('Trailing garbage after struct on line %d'
                              % line_count)

        # We found the end of the struct
        data += ' %s}' % tokens[0]
        break

    # Remove any comments, that might be in there
    data = re.sub(r'/\*.*\*/', '', data)

    return data


def Parse(factory, file):
    """
    Parses the input file and returns C code and corresponding header file.
    """

    entities = []

    while 1:
        # Just gets the whole struct nicely formatted
        data = GetNextStruct(file)

        if not data:
            break

        entities.extend(ProcessStruct(factory, data))

    return entities

class CCodeGenerator:
    def __init__(self):
        pass

    def GuardName(self, name):
        # Use the complete provided path to the input file, with all
        # non-identifier characters replaced with underscores, to
        # reduce the chance of a collision between guard macros.
        return '_' + nonident.sub('_', name).upper() + '_'

    def HeaderPreamble(self, name):
        guard = self.GuardName(name)
        pre = (
            '/*\n'
            ' * Automatically generated from %s\n'
            ' */\n\n'
            '#ifndef %s\n'
            '#define %s\n\n' ) % (
            name, guard, guard)

        for statement in headerdirect:
            pre += '%s\n' % statement
        if headerdirect:
            pre += '\n'

        pre += (
            '#include <event2/util.h> /* for ev_uint*_t */\n'
            '#include <event2/rpc.h>\n'
        )

        return pre

    def HeaderPostamble(self, name):
        guard = self.GuardName(name)
        return '#endif  /* %s */' % guard

    def BodyPreamble(self, name, header_file):
        global _NAME
        global _VERSION

        slash = header_file.rfind('/')
        if slash != -1:
            header_file = header_file[slash+1:]

        pre = ( '/*\n'
                ' * Automatically generated from %s\n'
                ' * by %s/%s.  DO NOT EDIT THIS FILE.\n'
                ' */\n\n' ) % (name, _NAME, _VERSION)
        pre += ( '#include <stdlib.h>\n'
                 '#include <string.h>\n'
                 '#include <assert.h>\n'
                 '#include <event2/event-config.h>\n'
                 '#include <event2/event.h>\n'
                 '#include <event2/buffer.h>\n'
                 '#include <event2/tag.h>\n\n'
                 '#ifdef _EVENT___func__\n'
                 '#define __func__ _EVENT___func__\n'
                 '#endif\n\n'
                 )

        for statement in cppdirect:
            pre += '%s\n' % statement

        pre += '\n#include "%s"\n\n' % header_file

        pre += 'void event_warn(const char *fmt, ...);\n'
        pre += 'void event_warnx(const char *fmt, ...);\n\n'

        return pre

    def HeaderFilename(self, filename):
        return '.'.join(filename.split('.')[:-1]) + '.h'

    def CodeFilename(self, filename):
        return '.'.join(filename.split('.')[:-1]) + '.gen.c'

    def Struct(self, name):
        return StructCCode(name)

    def EntryBytes(self, entry_type, name, tag, fixed_length):
        return EntryBytes(entry_type, name, tag, fixed_length)

    def EntryVarBytes(self, entry_type, name, tag):
        return EntryVarBytes(entry_type, name, tag)

    def EntryInt(self, entry_type, name, tag, bits=32):
        return EntryInt(entry_type, name, tag, bits)

    def EntryString(self, entry_type, name, tag):
        return EntryString(entry_type, name, tag)

    def EntryStruct(self, entry_type, name, tag, struct_name):
        return EntryStruct(entry_type, name, tag, struct_name)

    def EntryArray(self, entry):
        return EntryArray(entry)

class Usage(RpcGenError):
    def __init__(self, argv0):
        RpcGenError.__init__("usage: %s input.rpc [[output.h] output.c]"
                             % argv0)

class CommandLine:
    def __init__(self, argv):
        """Initialize a command-line to launch event_rpcgen, as if
           from a command-line with CommandLine(sys.argv).  If you're
           calling this directly, remember to provide a dummy value
           for sys.argv[0]
        """
        self.filename = None
        self.header_file = None
        self.impl_file = None
        self.factory = CCodeGenerator()

        if len(argv) < 2 or len(argv) > 4:
            raise Usage(argv[0])

        self.filename = argv[1].replace('\\', '/')
        if len(argv) == 3:
            self.impl_file = argv[2].replace('\\', '/')
        if len(argv) == 4:
            self.header_file = argv[2].replace('\\', '/')
            self.impl_file = argv[3].replace('\\', '/')

        if not self.filename:
            raise Usage(argv[0])

        if not self.impl_file:
            self.impl_file = self.factory.CodeFilename(self.filename)

        if not self.header_file:
            self.header_file = self.factory.HeaderFilename(self.impl_file)

        if not self.impl_file.endswith('.c'):
            raise RpcGenError("can only generate C implementation files")
        if not self.header_file.endswith('.h'):
            raise RpcGenError("can only generate C header files")

    def run(self):
        filename = self.filename
        header_file = self.header_file
        impl_file = self.impl_file
        factory = self.factory

        print >>sys.stderr, 'Reading \"%s\"' % filename

        fp = open(filename, 'r')
        entities = Parse(factory, fp)
        fp.close()

        print >>sys.stderr, '... creating "%s"' % header_file
        header_fp = open(header_file, 'w')
        print >>header_fp, factory.HeaderPreamble(filename)

        # Create forward declarations: allows other structs to reference
        # each other
        for entry in entities:
            entry.PrintForwardDeclaration(header_fp)
        print >>header_fp, ''

        for entry in entities:
            entry.PrintTags(header_fp)
            entry.PrintDeclaration(header_fp)
        print >>header_fp, factory.HeaderPostamble(filename)
        header_fp.close()

        print >>sys.stderr, '... creating "%s"' % impl_file
        impl_fp = open(impl_file, 'w')
        print >>impl_fp, factory.BodyPreamble(filename, header_file)
        for entry in entities:
            entry.PrintCode(impl_fp)
        impl_fp.close()

if __name__ == '__main__':
    try:
        CommandLine(sys.argv).run()
        sys.exit(0)

    except RpcGenError, e:
        print >>sys.stderr, e
        sys.exit(1)

    except EnvironmentError, e:
        if e.filename and e.strerror:
            print >>sys.stderr, "%s: %s" % (e.filename, e.strerror)
            sys.exit(1)
        elif e.strerror:
            print >> sys.stderr, e.strerror
            sys.exit(1)
        else:
            raise