rgb-cln/tools/gen/print_impl_template

110 lines
3.2 KiB
Plaintext
Raw Normal View History

/* This file was generated by generate-bolts.py */
/* Do not modify this file! Modify the _csv file it was generated from. */
#include "${options.header_filename}"
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <inttypes.h>
#include <stdio.h>
void 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");
printwire_${msg.name}("${msg.name}", msg);
return;
% endfor
}
printf("UNKNOWN: %s\\n", tal_hex(msg, msg));
}
void print${options.enum_name}_tlv_message(const char *tlv_name, const u8 *msg)
{
% if not bool(tlvs):
printf("~~ No TLV definition found for %s ~~\\n", tlv_name);
% else:
% for tlv in tlvs:
if (strcmp(tlv_name, "${tlv.name}") == 0) {
printwire_${tlv.name}("${tlv.name}", msg);
return;
}
% endfor
printf("ERR: Unknown TLV message type: %s\n", tlv_name);
% endif
}
## 'component' for 'truncate check
<%def name="truncate_check(nested=False)">
if (!${ '*' if nested else '' }cursor) {
printf("**TRUNCATED**\n");
return;
}
</%def> \
## definition for printing field sets
<%def name="print_fieldset(fields, nested, cursor, plen)">
## FIXME: optional field handling omitted since we only generate these for bolts rn
% 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(nested)} <% continue %> \
% endif
printf("${f.name}=");
% if 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()});
% else:
printf("[");
for (size_t i = 0; i < ${f.size()}; i++) {
${f.type_obj.type_name()} v;
% if f.type_obj.is_assignable():
v = fromwire_${f.type_obj.name}(${cursor}, ${plen});
% else:
fromwire_${f.type_obj.name}(${cursor}, ${plen}, &v);
% endif
${truncate_check(nested)} \
printwire_${f.type_obj.name}(tal_fmt(NULL, "%s.${f.name}", fieldname), &v);
}
printf("]");
% endif
${truncate_check(nested)} \
% else:
% if f.type_obj.is_assignable():
${f.type_obj.type_name()} ${f.name} = fromwire_${f.type_obj.name}(${cursor}, ${plen});
% else:
${f.type_obj.type_name()} ${f.name};
fromwire_${f.type_obj.name}(${cursor}, ${plen}, &${f.name});
% endif
printwire_${f.type_obj.name}(tal_fmt(NULL, "%s.${f.name}", fieldname), &${f.name}); ${truncate_check(nested)} \
% endif
% endfor
</%def> \
## Definitions for 'subtypes'
% for subtype in subtypes:
static void printwire_${subtype.name}(const char *fieldname, const u9 **cursor, size_t *plen)
{
${print_fieldset(subtype.fields.values(), True, 'cursor', 'plen')}
}
% endfor
## FIXME: handling for tlv's :/
% for msg in messages:
void printwire_${msg.name}(const char *fieldname, const u8 *cursor)
{
size_t plen = tal_count(cursor);
if (fromwire_u16(&cursor, &plen) != ${msg.enum_name()}) {
printf("WRONG TYPE?!\n");
return;
}
${print_fieldset(msg.fields.values(), False, '&cursor', '&plen')}
## Length check
if (plen != 0)
printf("EXTRA: %s\n", tal_hexstr(NULL, cursor, plen));
}
% endfor