forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_impl_template
129 lines (118 loc) · 3.46 KB
/
print_impl_template
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
/* This file was generated by generate-wire.py */
/* Do not modify this file! Modify the .csv file it was generated from. */
#include <${header_filename}>
#include <ccan/array_size/array_size.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <inttypes.h>
#include <stdio.h>
% for i in includes:
${i}
% endfor
% if enum_sets:
bool print${options.enum_name}_message(const u8 *msg)
{
switch ((enum ${options.enum_name})fromwire_peektype(msg)) {
% for msg in enum_sets[0]['set']:
case ${msg.enum_name()}:
printf("${msg.enum_name()}:\n");
return printwire_${msg.name}("${msg.name}", msg);
% endfor
}
printf("UNKNOWN: %s\\n", tal_hex(msg, msg));
return false;
}
% endif
## 'component' for 'truncate check
<%def name="truncate_check()">
if (!*cursor) {
printf("**TRUNCATED**\n");
return false;
}
</%def> \
## definition for printing field sets
<%def name="print_fieldset(fields)">
% for f in fields:
% if f.len_field_of:
${f.type_obj.type_name()} ${f.name} = fromwire_${f.type_obj.name}(cursor, plen);${truncate_check()} <% continue %> \
% endif
printf("${f.name}=");
% if f.type_obj.is_tlv():
printwire_${f.type_obj.tlv.name}(tal_fmt(NULL, "%s.${f.name}", fieldname), cursor, plen);
% elif f.is_array() or f.is_varlen():
% if f.type_obj.has_array_helper():
printwire_${f.type_obj.name}_array(tal_fmt(NULL, "%s.${f.name}", fieldname), cursor, plen, ${f.size('*plen')});
% else:
printf("[");
% if f.is_implicit_len():
while (*plen) {
% else:
for (size_t i = 0; i < ${f.size()}; i++) {
% endif
if (!printwire_${f.type_obj.name}(tal_fmt(NULL, "%s.${f.name}", fieldname), cursor, plen))
return false;
}
printf("]");
% endif
% else:
if (!printwire_${f.type_obj.name}(tal_fmt(NULL, "%s.${f.name}", fieldname), cursor, plen))
return false;
% endif
% endfor
</%def> \
## Definitions for 'subtypes'
% for subtype in subtypes:
<%
static = '' if options.expose_subtypes else 'static '
%>\
${static}bool printwire_${subtype.name}(const char *fieldname, const u8 **cursor, size_t *plen)
{
${print_fieldset(subtype.fields.values())}
return true;
}
% endfor
% for tlv in tlvs.values():
% for msg in tlv.messages.values():
static bool printwire_${msg.struct_name()}(const char *fieldname, const u8 **cursor, size_t *plen)
{
printf("(msg_name=%s)\n", "${msg.name}");
${print_fieldset(msg.fields.values())}
return true;
}
% endfor
static const struct tlv_print_record_type print_tlvs_${tlv.name}[] = {
% for msg in tlv.messages.values():
{ ${msg.number}, printwire_${msg.struct_name()} },
% endfor
};
% endfor
% for msg in messages:
bool printwire_${msg.name}(const char *fieldname, const u8 *msg)
{
size_t msglen = tal_count(msg);
const u8 **cursor = &msg;
size_t *plen = &msglen;
if (fromwire_u16(cursor, plen) != ${msg.enum_name()}) {
printf("WRONG TYPE?!\n");
return false;
}
${print_fieldset(msg.fields.values())}
## Length check
if (*plen != 0)
printf("EXTRA: %s\n", tal_hexstr(NULL, *cursor, *plen));
return *cursor != NULL;
}
% endfor
% for tlv in tlvs.values():
bool printwire_${tlv.name}(const char *fieldname, const u8 **cursor, size_t *plen)
{
return printwire_tlvs(fieldname, cursor, plen, print_tlvs_${tlv.name}, ARRAY_SIZE(print_tlvs_${tlv.name}));
}
% endfor
const struct tlv_print${options.enum_name}_byname tlvs_print${options.enum_name}_byname[] = {
% for tlv in tlvs.values():
{ "${tlv.name}", printwire_${tlv.name} },
% endfor
{ NULL, NULL }
};