2016-11-29 20:51:50 +01:00
|
|
|
#! /usr/bin/python3
|
|
|
|
# Read from stdin, spit out C header or body.
|
|
|
|
|
|
|
|
from optparse import OptionParser
|
|
|
|
from collections import namedtuple
|
|
|
|
import fileinput
|
|
|
|
import re
|
|
|
|
|
|
|
|
Enumtype = namedtuple('Enumtype', ['name', 'value'])
|
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
class FieldType(object):
|
|
|
|
def __init__(self,name):
|
|
|
|
self.name = name
|
|
|
|
self.tsize = FieldType._typesize(name)
|
|
|
|
|
|
|
|
def is_assignable(self):
|
2017-01-04 04:39:21 +01:00
|
|
|
return self.name == 'u8' or self.name == 'u16' or self.name == 'u32' or self.name == 'u64' or self.name == 'bool'
|
2017-01-04 04:39:21 +01:00
|
|
|
|
|
|
|
# Returns typename and base size
|
|
|
|
@staticmethod
|
|
|
|
def _typesize(typename):
|
|
|
|
if typename == 'pad':
|
|
|
|
return 1
|
|
|
|
elif typename == 'struct channel_id':
|
|
|
|
return 8
|
|
|
|
elif typename == 'struct ipv6':
|
|
|
|
return 16
|
|
|
|
elif typename == 'struct signature':
|
|
|
|
return 64
|
|
|
|
elif typename == 'struct pubkey':
|
|
|
|
return 33
|
|
|
|
elif typename == 'struct sha256':
|
|
|
|
return 32
|
|
|
|
elif typename == 'u64':
|
|
|
|
return 8
|
|
|
|
elif typename == 'u32':
|
|
|
|
return 4
|
|
|
|
elif typename == 'u16':
|
|
|
|
return 2
|
|
|
|
elif typename == 'u8':
|
|
|
|
return 1
|
2017-01-04 04:39:21 +01:00
|
|
|
elif typename == 'bool':
|
|
|
|
return 1
|
2017-01-04 04:39:21 +01:00
|
|
|
else:
|
|
|
|
raise ValueError('Unknown typename {}'.format(typename))
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
class Field(object):
|
2017-01-04 04:39:21 +01:00
|
|
|
def __init__(self,message,name,size,comments,typename=None):
|
2016-11-29 20:51:50 +01:00
|
|
|
self.message = message
|
2017-01-04 04:39:21 +01:00
|
|
|
self.comments = comments
|
2016-11-29 20:51:50 +01:00
|
|
|
self.name = name.replace('-', '_')
|
2017-01-04 04:39:20 +01:00
|
|
|
self.is_len_var = False
|
2017-01-04 04:39:21 +01:00
|
|
|
self.lenvar = None
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
# Size could be a literal number (eg. 33), or a field (eg 'len'), or
|
|
|
|
# a multiplier of a field (eg. num-htlc-timeouts*64).
|
2016-11-29 20:51:50 +01:00
|
|
|
try:
|
2017-01-04 04:39:21 +01:00
|
|
|
base_size = int(size)
|
2016-11-29 20:51:50 +01:00
|
|
|
except ValueError:
|
|
|
|
# If it's a multiplicitive expression, must end in basesize.
|
|
|
|
if '*' in size:
|
2017-01-04 04:39:21 +01:00
|
|
|
base_size = int(size.split('*')[1])
|
|
|
|
self.lenvar = size.split('*')[0]
|
2016-11-29 20:51:50 +01:00
|
|
|
else:
|
2017-01-04 04:39:21 +01:00
|
|
|
base_size = 0
|
|
|
|
self.lenvar = size
|
|
|
|
self.lenvar = self.lenvar.replace('-','_')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
if typename is None:
|
|
|
|
self.fieldtype = Field._guess_type(message,self.name,base_size)
|
|
|
|
else:
|
|
|
|
self.fieldtype = FieldType(typename)
|
|
|
|
|
|
|
|
if base_size % self.fieldtype.tsize != 0:
|
|
|
|
raise ValueError('Invalid size {} for {}.{} not a multiple of {}'.format(base_size,self.message,self.name,self.fieldtype.tsize))
|
|
|
|
self.num_elems = int(base_size / self.fieldtype.tsize)
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
def is_padding(self):
|
|
|
|
return self.name.startswith('pad')
|
|
|
|
|
|
|
|
# Padding is always treated as an array.
|
|
|
|
def is_array(self):
|
|
|
|
return self.num_elems > 1 or self.is_padding()
|
|
|
|
|
|
|
|
def is_variable_size(self):
|
2017-01-04 04:39:21 +01:00
|
|
|
return self.lenvar is not None
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
def is_assignable(self):
|
|
|
|
if self.is_array() or self.is_variable_size():
|
|
|
|
return False
|
2017-01-04 04:39:21 +01:00
|
|
|
return self.fieldtype.is_assignable()
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
# Returns FieldType
|
2016-11-29 20:51:50 +01:00
|
|
|
@staticmethod
|
2017-01-04 04:39:21 +01:00
|
|
|
def _guess_type(message, fieldname, base_size):
|
2016-11-29 20:51:50 +01:00
|
|
|
if fieldname.startswith('pad'):
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('pad')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
if fieldname.endswith('channel_id'):
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('struct channel_id')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
if message == 'node_announcement' and fieldname == 'ipv6':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('struct ipv6')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
if message == 'node_announcement' and fieldname == 'alias':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
2017-01-04 04:39:20 +01:00
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
if fieldname.endswith('features'):
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
# We translate signatures and pubkeys.
|
|
|
|
if 'signature' in fieldname:
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('struct signature')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
# We whitelist specific things here, otherwise we'd treat everything
|
|
|
|
# as a u8 array.
|
|
|
|
if message == 'update_fail_htlc' and fieldname == 'reason':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
2016-11-29 20:51:50 +01:00
|
|
|
if message == 'update_add_htlc' and fieldname == 'onion_routing_packet':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
2016-11-29 20:51:50 +01:00
|
|
|
if message == 'node_announcement' and fieldname == 'alias':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
2016-11-29 20:51:50 +01:00
|
|
|
if message == 'error' and fieldname == 'data':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
2016-11-29 20:51:50 +01:00
|
|
|
if message == 'shutdown' and fieldname == 'scriptpubkey':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
2016-11-29 20:51:50 +01:00
|
|
|
if message == 'node_announcement' and fieldname == 'rgb_color':
|
2017-01-04 04:39:21 +01:00
|
|
|
return FieldType('u8')
|
|
|
|
if message == 'node_announcement' and fieldname == 'addresses':
|
|
|
|
return FieldType('u8')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
# The remainder should be fixed sizes.
|
|
|
|
if base_size == 33:
|
|
|
|
return FieldType('struct pubkey')
|
|
|
|
if base_size == 32:
|
|
|
|
return FieldType('struct sha256')
|
|
|
|
if base_size == 8:
|
|
|
|
return FieldType('u64')
|
|
|
|
if base_size == 4:
|
|
|
|
return FieldType('u32')
|
|
|
|
if base_size == 2:
|
|
|
|
return FieldType('u16')
|
|
|
|
if base_size == 1:
|
|
|
|
return FieldType('u8')
|
|
|
|
|
|
|
|
raise ValueError('Unknown size {} for {}'.format(base_size,fieldname))
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
class Message(object):
|
2017-01-04 04:39:21 +01:00
|
|
|
def __init__(self,name,enum,comments):
|
2016-11-29 20:51:50 +01:00
|
|
|
self.name = name
|
|
|
|
self.enum = enum
|
2017-01-04 04:39:21 +01:00
|
|
|
self.comments = comments
|
2016-11-29 20:51:50 +01:00
|
|
|
self.fields = []
|
2017-01-04 04:39:20 +01:00
|
|
|
self.has_variable_fields = False
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
def checkLenField(self,field):
|
|
|
|
for f in self.fields:
|
|
|
|
if f.name == field.lenvar:
|
2017-01-04 04:39:21 +01:00
|
|
|
if f.fieldtype.name != 'u16':
|
2016-11-29 20:51:50 +01:00
|
|
|
raise ValueError('Field {} has non-u16 length variable {}'
|
|
|
|
.format(field.name, field.lenvar))
|
|
|
|
|
|
|
|
if f.is_array() or f.is_variable_size():
|
|
|
|
raise ValueError('Field {} has non-simple length variable {}'
|
|
|
|
.format(field.name, field.lenvar))
|
2017-01-04 04:39:20 +01:00
|
|
|
f.is_len_var = True;
|
2016-11-29 20:51:50 +01:00
|
|
|
return
|
|
|
|
raise ValueError('Field {} unknown length variable {}'
|
|
|
|
.format(field.name, field.lenvar))
|
|
|
|
|
|
|
|
def addField(self,field):
|
|
|
|
# We assume field lengths are 16 bit, to avoid overflow issues and
|
|
|
|
# massive allocations.
|
|
|
|
if field.is_variable_size():
|
|
|
|
self.checkLenField(field)
|
2017-01-04 04:39:20 +01:00
|
|
|
self.has_variable_fields = True
|
2016-11-29 20:51:50 +01:00
|
|
|
self.fields.append(field)
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
def print_fromwire(self,is_header):
|
|
|
|
if self.has_variable_fields:
|
|
|
|
ctx_arg = 'const tal_t *ctx, '
|
|
|
|
else:
|
|
|
|
ctx_arg = ''
|
|
|
|
|
|
|
|
print('bool fromwire_{}({}const void *p, size_t *plen'
|
|
|
|
.format(self.name, ctx_arg), end='')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
for f in self.fields:
|
2017-01-04 04:39:20 +01:00
|
|
|
if f.is_len_var:
|
|
|
|
continue
|
|
|
|
if f.is_padding():
|
|
|
|
continue
|
2016-11-29 20:51:50 +01:00
|
|
|
if f.is_array():
|
2017-01-04 04:39:21 +01:00
|
|
|
print(', {} {}[{}]'.format(f.fieldtype.name, f.name, f.num_elems), end='')
|
2016-11-29 20:51:50 +01:00
|
|
|
elif f.is_variable_size():
|
2017-01-04 04:39:21 +01:00
|
|
|
print(', {} **{}'.format(f.fieldtype.name, f.name), end='')
|
2016-11-29 20:51:50 +01:00
|
|
|
else:
|
2017-01-04 04:39:21 +01:00
|
|
|
print(', {} *{}'.format(f.fieldtype.name, f.name), end='')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
if is_header:
|
|
|
|
print(');')
|
2017-01-04 04:39:20 +01:00
|
|
|
return
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
print(')\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
'{')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
for f in self.fields:
|
|
|
|
if f.is_len_var:
|
2017-01-04 04:39:21 +01:00
|
|
|
print('\t{} {};'.format(f.fieldtype.name, f.name));
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
print('\tconst u8 *cursor = p;\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
'\tsize_t tmp_len;\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
'\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
'\tif (!plen) {{\n'
|
|
|
|
'\t\ttmp_len = tal_count(p);\n'
|
|
|
|
'\t\tplen = &tmp_len;\n'
|
|
|
|
'\t}}\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
'\tif (fromwire_u16(&cursor, plen) != {})\n'
|
|
|
|
'\t\treturn false;'
|
|
|
|
.format(self.enum.name))
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
for f in self.fields:
|
2017-01-04 04:39:21 +01:00
|
|
|
basetype=f.fieldtype.name
|
|
|
|
if f.fieldtype.name.startswith('struct '):
|
|
|
|
basetype=f.fieldtype.name[7:]
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
for c in f.comments:
|
|
|
|
print('\t/*{} */'.format(c))
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
if f.is_padding():
|
|
|
|
print('\tfromwire_pad(&cursor, plen, {});'
|
|
|
|
.format(f.num_elems))
|
|
|
|
elif f.is_array():
|
2016-11-30 16:58:58 +01:00
|
|
|
print("\t//1th case", f.name)
|
2017-01-04 04:39:20 +01:00
|
|
|
print('\tfromwire_{}_array(&cursor, plen, {}, {});'
|
2016-12-02 08:41:06 +01:00
|
|
|
.format(basetype, f.name, f.num_elems))
|
2016-11-29 20:51:50 +01:00
|
|
|
elif f.is_variable_size():
|
2016-11-30 16:58:58 +01:00
|
|
|
print("\t//2th case", f.name)
|
2017-01-04 04:39:20 +01:00
|
|
|
print('\t*{} = tal_arr(ctx, {}, {});'
|
2017-01-04 04:39:21 +01:00
|
|
|
.format(f.name, f.fieldtype.name, f.lenvar))
|
2017-01-04 04:39:20 +01:00
|
|
|
print('\tfromwire_{}_array(&cursor, plen, *{}, {});'
|
2016-12-02 08:41:06 +01:00
|
|
|
.format(basetype, f.name, f.lenvar))
|
2016-11-29 20:51:50 +01:00
|
|
|
elif f.is_assignable():
|
2016-11-30 16:58:58 +01:00
|
|
|
print("\t//3th case", f.name)
|
2017-01-04 04:39:20 +01:00
|
|
|
if f.is_len_var:
|
|
|
|
print('\t{} = fromwire_{}(&cursor, plen);'
|
|
|
|
.format(f.name, basetype))
|
|
|
|
else:
|
|
|
|
print('\t*{} = fromwire_{}(&cursor, plen);'
|
|
|
|
.format(f.name, basetype))
|
2016-11-29 20:51:50 +01:00
|
|
|
else:
|
2016-11-30 16:58:58 +01:00
|
|
|
print("\t//4th case", f.name)
|
2017-01-04 04:39:20 +01:00
|
|
|
print('\tfromwire_{}(&cursor, plen, {});'
|
2016-12-02 08:41:06 +01:00
|
|
|
.format(basetype, f.name))
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
print('\n'
|
2017-01-04 04:39:20 +01:00
|
|
|
'\treturn cursor != NULL;\n'
|
2016-11-29 20:51:50 +01:00
|
|
|
'}\n')
|
2017-01-04 04:39:20 +01:00
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
def print_towire(self,is_header):
|
2017-01-04 04:39:20 +01:00
|
|
|
print('u8 *towire_{}(const tal_t *ctx'
|
|
|
|
.format(self.name), end='')
|
|
|
|
|
|
|
|
for f in self.fields:
|
|
|
|
if f.is_padding():
|
|
|
|
continue
|
|
|
|
if f.is_array():
|
2017-01-04 04:39:21 +01:00
|
|
|
print(', const {} {}[{}]'.format(f.fieldtype.name, f.name, f.num_elems), end='')
|
2017-01-04 04:39:20 +01:00
|
|
|
elif f.is_assignable():
|
2017-01-04 04:39:21 +01:00
|
|
|
print(', {} {}'.format(f.fieldtype.name, f.name), end='')
|
2017-01-04 04:39:20 +01:00
|
|
|
else:
|
2017-01-04 04:39:21 +01:00
|
|
|
print(', const {} *{}'.format(f.fieldtype.name, f.name), end='')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
if is_header:
|
2017-01-04 04:39:20 +01:00
|
|
|
print(');')
|
2016-11-29 20:51:50 +01:00
|
|
|
return
|
2017-01-04 04:39:20 +01:00
|
|
|
|
|
|
|
print(')\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
'{{\n'
|
2016-11-29 20:51:50 +01:00
|
|
|
'\tu8 *p = tal_arr(ctx, u8, 0);\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
''
|
|
|
|
'\ttowire_u16(&p, {});'.format(self.enum.name))
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
for f in self.fields:
|
2017-01-04 04:39:21 +01:00
|
|
|
basetype=f.fieldtype.name
|
|
|
|
if f.fieldtype.name.startswith('struct '):
|
|
|
|
basetype=f.fieldtype.name[7:]
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
for c in f.comments:
|
|
|
|
print('\t/*{} */'.format(c))
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
if f.is_padding():
|
|
|
|
print('\ttowire_pad(&p, {});'
|
|
|
|
.format(f.num_elems))
|
|
|
|
elif f.is_array():
|
|
|
|
print('\ttowire_{}_array(&p, {}, {});'
|
2016-12-02 08:41:06 +01:00
|
|
|
.format(basetype, f.name, f.num_elems))
|
2016-11-29 20:51:50 +01:00
|
|
|
elif f.is_variable_size():
|
2017-01-04 04:39:20 +01:00
|
|
|
print('\ttowire_{}_array(&p, {}, {});'
|
2016-12-02 08:41:06 +01:00
|
|
|
.format(basetype, f.name, f.lenvar))
|
2016-11-29 20:51:50 +01:00
|
|
|
else:
|
2017-01-04 04:39:20 +01:00
|
|
|
print('\ttowire_{}(&p, {});'
|
2016-12-02 08:41:06 +01:00
|
|
|
.format(basetype, f.name))
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
# Make sure we haven't encoded any uninitialzied fields!
|
2016-11-29 20:51:50 +01:00
|
|
|
print('\n'
|
2017-01-04 04:39:20 +01:00
|
|
|
'\treturn memcheck(p, tal_count(p));\n'
|
2016-11-29 20:51:50 +01:00
|
|
|
'}\n')
|
|
|
|
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("--header",
|
|
|
|
action="store_true", dest="output_header", default=False,
|
2017-01-04 04:39:20 +01:00
|
|
|
help="Create wire header")
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
if len(args) != 2:
|
|
|
|
parser.error("Expect headerfilename and enumname")
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
if options.output_header:
|
2017-01-04 04:39:20 +01:00
|
|
|
idem = re.sub(r'[^A-Z]+', '_', args[0].upper())
|
|
|
|
print('#ifndef LIGHTNING_{0}\n'
|
|
|
|
'#define LIGHTNING_{0}\n'
|
2016-11-29 20:51:50 +01:00
|
|
|
'#include <ccan/tal/tal.h>\n'
|
|
|
|
'#include <wire/wire.h>\n'
|
2017-01-04 04:39:20 +01:00
|
|
|
''.format(idem))
|
2016-11-29 20:51:50 +01:00
|
|
|
else:
|
2017-01-04 04:39:20 +01:00
|
|
|
print('#include <{}>\n'
|
2017-01-04 04:39:20 +01:00
|
|
|
'#include <ccan/mem/mem.h>\n'
|
2017-01-04 04:39:21 +01:00
|
|
|
'#include <ccan/tal/str/str.h>\n'
|
2017-01-04 04:39:20 +01:00
|
|
|
''.format(args[0]))
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
# Maps message names to messages
|
2017-01-04 04:39:21 +01:00
|
|
|
messages = []
|
2017-01-04 04:39:21 +01:00
|
|
|
comments = []
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
# Read csv lines. Single comma is the message values, more is offset/len.
|
2017-01-04 04:39:20 +01:00
|
|
|
for line in fileinput.input(args[2:]):
|
2017-01-04 04:39:21 +01:00
|
|
|
by_comments = line.rstrip().split('#')
|
|
|
|
|
|
|
|
# Emit a comment if they included one
|
|
|
|
if by_comments[1:]:
|
|
|
|
comments.append(' '.join(by_comments[1:]))
|
|
|
|
|
|
|
|
parts = by_comments[0].split(',')
|
|
|
|
if parts == ['']:
|
|
|
|
continue
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
if len(parts) == 2:
|
|
|
|
# eg commit_sig,132
|
2017-01-04 04:39:21 +01:00
|
|
|
messages.append(Message(parts[0],Enumtype("WIRE_" + parts[0].upper(), int(parts[1],0)),comments))
|
2017-01-04 04:39:21 +01:00
|
|
|
comments=[]
|
2016-11-29 20:51:50 +01:00
|
|
|
else:
|
2017-01-04 04:39:21 +01:00
|
|
|
# eg commit_sig,0,channel-id,8 OR
|
|
|
|
# commit_sig,0,channel-id,8,u64
|
2017-01-04 04:39:21 +01:00
|
|
|
for m in messages:
|
|
|
|
if m.name == parts[0]:
|
2017-01-04 04:39:21 +01:00
|
|
|
if len(parts) == 4:
|
|
|
|
m.addField(Field(parts[0], parts[2], parts[3], comments))
|
|
|
|
else:
|
|
|
|
m.addField(Field(parts[0], parts[2], parts[3], comments,
|
|
|
|
parts[4]))
|
2017-01-04 04:39:21 +01:00
|
|
|
break
|
2017-01-04 04:39:21 +01:00
|
|
|
comments=[]
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
if options.output_header:
|
|
|
|
# Dump out enum, sorted by value order.
|
2017-01-04 04:39:20 +01:00
|
|
|
print('enum {} {{'.format(args[1]))
|
2017-01-04 04:39:21 +01:00
|
|
|
for m in messages:
|
2017-01-04 04:39:21 +01:00
|
|
|
for c in m.comments:
|
|
|
|
print('\t/*{} */'.format(c))
|
2016-11-29 20:51:50 +01:00
|
|
|
print('\t{} = {},'.format(m.enum.name, m.enum.value))
|
|
|
|
print('};')
|
2017-01-04 04:39:21 +01:00
|
|
|
print('const char *{}_name(int e);'.format(args[1]))
|
|
|
|
else:
|
|
|
|
print('const char *{}_name(int e)'.format(args[1]))
|
|
|
|
print('{{\n'
|
|
|
|
'\tswitch ((enum {})e) {{'.format(args[1]));
|
|
|
|
for m in messages:
|
|
|
|
print('\tcase {0}: return "{0}";'.format(m.enum.name))
|
|
|
|
print('\t}\n'
|
|
|
|
'\treturn tal_fmt(NULL, "**INVALID** %i", e);\n'
|
|
|
|
'}\n'
|
|
|
|
'')
|
2016-11-29 20:51:50 +01:00
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
for m in messages:
|
2016-11-29 20:51:50 +01:00
|
|
|
m.print_fromwire(options.output_header)
|
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
for m in messages:
|
2016-11-29 20:51:50 +01:00
|
|
|
m.print_towire(options.output_header)
|
|
|
|
|
|
|
|
if options.output_header:
|
2017-01-04 04:39:20 +01:00
|
|
|
print('#endif /* LIGHTNING_{} */\n'.format(idem))
|