api: improve api string safety
[vpp.git] / src / tools / vppapigen / vppapigen_c.py
1 # C generation
2 import datetime
3 import os
4 import time
5 import sys
6 from io import StringIO
7 import shutil
8
9 datestring = datetime.datetime.utcfromtimestamp(
10     int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
11 input_filename = 'inputfil'
12 top_boilerplate = '''\
13 /*
14  * VLIB API definitions {datestring}
15  * Input file: {input_filename}
16  * Automatically generated: please edit the input file NOT this file!
17  */
18
19 #include <stdbool.h>
20 #if defined(vl_msg_id)||defined(vl_union_id) \\
21     || defined(vl_printfun) ||defined(vl_endianfun) \\
22     || defined(vl_api_version)||defined(vl_typedefs) \\
23     || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
24     || defined(vl_api_version_tuple)
25 /* ok, something was selected */
26 #else
27 #warning no content included from {input_filename}
28 #endif
29
30 #define VL_API_PACKED(x) x __attribute__ ((packed))
31 '''
32
33 bottom_boilerplate = '''\
34 /****** API CRC (whole file) *****/
35
36 #ifdef vl_api_version
37 vl_api_version({input_filename}, {file_crc:#08x})
38
39 #endif
40 '''
41
42
43 def msg_ids(s):
44     output = '''\
45
46 /****** Message ID / handler enum ******/
47
48 #ifdef vl_msg_id
49 '''
50
51     for t in s['Define']:
52         output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
53                   (t.name.upper(), t.name)
54     output += "#endif"
55
56     return output
57
58
59 def msg_names(s):
60     output = '''\
61
62 /****** Message names ******/
63
64 #ifdef vl_msg_name
65 '''
66
67     for t in s['Define']:
68         dont_trace = 0 if t.dont_trace else 1
69         output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
70     output += "#endif"
71
72     return output
73
74
75 def msg_name_crc_list(s, suffix):
76     output = '''\
77
78 /****** Message name, crc list ******/
79
80 #ifdef vl_msg_name_crc_list
81 '''
82     output += "#define foreach_vl_msg_name_crc_%s " % suffix
83
84     for t in s['Define']:
85         output += "\\\n_(VL_API_%s, %s, %08x) " % \
86                    (t.name.upper(), t.name, t.crc)
87     output += "\n#endif"
88
89     return output
90
91
92 def api2c(fieldtype):
93     mappingtable = {'string': 'vl_api_string_t', }
94     if fieldtype in mappingtable:
95         return mappingtable[fieldtype]
96     return fieldtype
97
98
99 def typedefs(filename):
100
101     output = '''\
102
103 /****** Typedefs ******/
104
105 #ifdef vl_typedefs
106 #include "{include}.api_types.h"
107 #endif
108 '''.format(include=filename)
109     return output
110
111
112 format_strings = {'u8': '%u',
113                   'bool': '%u',
114                   'i8': '%d',
115                   'u16': '%u',
116                   'i16': '%d',
117                   'u32': '%u',
118                   'i32': '%ld',
119                   'u64': '%llu',
120                   'i64': '%lld',
121                   'f64': '%.2f'}
122
123 noprint_fields = {'_vl_msg_id': None,
124                   'client_index': None,
125                   'context': None}
126
127
128 class Printfun():
129     _dispatch = {}
130
131     def __init__(self, stream):
132         self.stream = stream
133
134     def print_string(self, o, stream):
135         write = stream.write
136         if o.modern_vla:
137             write('    if (vl_api_string_len(&a->{f}) > 0) {{\n'
138                   .format(f=o.fieldname))
139             write('        s = format(s, "\\n%U{f}: %U", '
140                   'format_white_space, indent, '
141                   'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname))
142             write('    } else {\n')
143             write('        s = format(s, "\\n%U{f}:", '
144                   'format_white_space, indent);\n'.format(f=o.fieldname))
145             write('    }\n')
146         else:
147             write('    s = format(s, "\\n%U{f}: %s", '
148                   'format_white_space, indent, a->{f});\n'
149                   .format(f=o.fieldname))
150
151     def print_field(self, o, stream):
152         write = stream.write
153         if o.fieldname in noprint_fields:
154             return
155         if o.fieldtype in format_strings:
156             f = format_strings[o.fieldtype]
157             write('    s = format(s, "\\n%U{n}: {f}", '
158                   'format_white_space, indent, a->{n});\n'
159                   .format(n=o.fieldname, f=f))
160         else:
161             write('    s = format(s, "\\n%U{n}: %U", '
162                   'format_white_space, indent, '
163                   'format_{t}, &a->{n}, indent);\n'
164                   .format(n=o.fieldname, t=o.fieldtype))
165
166     _dispatch['Field'] = print_field
167
168     def print_array(self, o, stream):
169         write = stream.write
170
171         forloop = '''\
172     for (i = 0; i < {lfield}; i++) {{
173         s = format(s, "\\n%U{n}: %U",
174                    format_white_space, indent, format_{t}, &a->{n}[i], indent);
175     }}
176 '''
177
178         forloop_format = '''\
179     for (i = 0; i < {lfield}; i++) {{
180         s = format(s, "\\n%U{n}: {t}",
181                    format_white_space, indent, a->{n}[i]);
182     }}
183 '''
184
185         if o.fieldtype == 'string':
186             return self.print_string(o, stream)
187
188         if o.fieldtype == 'u8':
189             if o.lengthfield:
190                 write('    s = format(s, "\\n%U{n}: %U", format_white_space, '
191                       'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
192                       .format(n=o.fieldname, lfield=o.lengthfield))
193             else:
194                 write('    s = format(s, "\\n%U{n}: %U", format_white_space, '
195                       'indent, format_hex_bytes, a, {lfield});\n'
196                       .format(n=o.fieldname, lfield=o.length))
197             return
198
199         lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
200         if o.fieldtype in format_strings:
201             write(forloop_format.format(lfield=lfield,
202                                         t=format_strings[o.fieldtype],
203                                         n=o.fieldname))
204         else:
205             write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
206
207     _dispatch['Array'] = print_array
208
209     def print_alias(self, k, v, stream):
210         write = stream.write
211         if ('length' in v.alias and v.alias['length'] and
212                 v.alias['type'] == 'u8'):
213             write('    return format(s, "%U", format_hex_bytes, a, {});\n'
214                   .format(v.alias['length']))
215         elif v.alias['type'] in format_strings:
216             write('    return format(s, "{}", *a);\n'
217                   .format(format_strings[v.alias['type']]))
218         else:
219             write('    return format(s, "{} (print not implemented)");\n'
220                   .format(k))
221
222     def print_enum(self, o, stream):
223         write = stream.write
224         write("    switch(*a) {\n")
225         for b in o:
226             write("    case %s:\n" % b[1])
227             write('        return format(s, "{}");\n'.format(b[0]))
228         write('    }\n')
229
230     _dispatch['Enum'] = print_enum
231
232     def print_obj(self, o, stream):
233         write = stream.write
234
235         if o.type in self._dispatch:
236             self._dispatch[o.type](self, o, stream)
237         else:
238             write('    s = format(s, "\\n{} {} {} (print not implemented");\n'
239                   .format(o.type, o.fieldtype, o.fieldname))
240
241
242 def printfun(objs, stream, modulename):
243     write = stream.write
244
245     h = '''\
246 /****** Print functions *****/
247 #ifdef vl_printfun
248 #ifndef included_{module}_printfun
249 #define included_{module}_printfun
250
251 #ifdef LP64
252 #define _uword_fmt \"%lld\"
253 #define _uword_cast (long long)
254 #else
255 #define _uword_fmt \"%ld\"
256 #define _uword_cast long
257 #endif
258
259 '''
260
261     signature = '''\
262 static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle)
263 {{
264     u8 *s = 0;
265     u32 indent __attribute__((unused)) = 2;
266     int i __attribute__((unused));
267 '''
268
269     h = h.format(module=modulename)
270     write(h)
271
272     pp = Printfun(stream)
273     for t in objs:
274         if t.manual_print:
275             write("/***** manual: vl_api_%s_t_print  *****/\n\n" % t.name)
276             continue
277         write(signature.format(name=t.name))
278         write('    /* Message definition: vl_api_{}_t: */\n'.format(t.name))
279         write("    s = format(s, \"vl_api_%s_t:\");\n" % t.name)
280         for o in t.block:
281             pp.print_obj(o, stream)
282         write('    vec_add1(s, 0);\n')
283         write('    vl_print (handle, (char *)s);\n')
284         write('    vec_free (s);\n')
285         write('    return handle;\n')
286         write('}\n\n')
287
288     write("\n#endif")
289     write("\n#endif /* vl_printfun */\n")
290
291     return ''
292
293
294 def printfun_types(objs, stream, modulename):
295     write = stream.write
296     pp = Printfun(stream)
297
298     h = '''\
299 /****** Print functions *****/
300 #ifdef vl_printfun
301 #ifndef included_{module}_printfun_types
302 #define included_{module}_printfun_types
303
304 '''
305     h = h.format(module=modulename)
306     write(h)
307
308     signature = '''\
309 static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
310 {{
311     vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
312     u32 indent __attribute__((unused)) = va_arg (*args, u32);
313     int i __attribute__((unused));
314     indent += 2;
315 '''
316
317     for t in objs:
318         if t.__class__.__name__ == 'Enum':
319             write(signature.format(name=t.name))
320             pp.print_enum(t.block, stream)
321             write('    return s;\n')
322             write('}\n\n')
323             continue
324
325         if t.manual_print:
326             write("/***** manual: vl_api_%s_t_print  *****/\n\n" % t.name)
327             continue
328
329         if t.__class__.__name__ == 'Using':
330             write(signature.format(name=t.name))
331             pp.print_alias(t.name, t, stream)
332             write('}\n\n')
333             continue
334
335         write(signature.format(name=t.name))
336         for o in t.block:
337             pp.print_obj(o, stream)
338
339         write('    return s;\n')
340         write('}\n\n')
341
342     write("\n#endif")
343     write("\n#endif /* vl_printfun_types */\n")
344
345
346 def imports(imports):
347     output = '/* Imported API files */\n'
348     output += '#ifndef vl_api_version\n'
349
350     for i in imports:
351         s = i.filename.replace('plugins/', '')
352         output += '#include <{}.h>\n'.format(s)
353     output += '#endif\n'
354     return output
355
356
357 endian_strings = {
358     'u16': 'clib_net_to_host_u16',
359     'u32': 'clib_net_to_host_u32',
360     'u64': 'clib_net_to_host_u64',
361     'i16': 'clib_net_to_host_i16',
362     'i32': 'clib_net_to_host_i32',
363     'i64': 'clib_net_to_host_i64',
364     'f64': 'clib_net_to_host_f64',
365 }
366
367
368 def endianfun_array(o):
369     forloop = '''\
370     for (i = 0; i < {length}; i++) {{
371         a->{name}[i] = {format}(a->{name}[i]);
372     }}
373 '''
374
375     forloop_format = '''\
376     for (i = 0; i < {length}; i++) {{
377         {type}_endian(&a->{name}[i]);
378     }}
379 '''
380
381     output = ''
382     if o.fieldtype == 'u8' or o.fieldtype == 'string':
383         output += '    /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
384     else:
385         lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
386         if o.fieldtype in endian_strings:
387             output += (forloop
388                        .format(length=lfield,
389                                format=endian_strings[o.fieldtype],
390                                name=o.fieldname))
391         else:
392             output += (forloop_format
393                        .format(length=lfield, type=o.fieldtype,
394                                name=o.fieldname))
395     return output
396
397
398 def endianfun_obj(o):
399     output = ''
400     if o.type == 'Array':
401         return endianfun_array(o)
402     elif o.type != 'Field':
403         output += ('    s = format(s, "\\n{} {} {} (print not implemented");\n'
404                    .format(o.type, o.fieldtype, o.fieldname))
405         return output
406     if o.fieldtype in endian_strings:
407         output += ('    a->{name} = {format}(a->{name});\n'
408                    .format(name=o.fieldname,
409                            format=endian_strings[o.fieldtype]))
410     elif o.fieldtype.startswith('vl_api_'):
411         output += ('    {type}_endian(&a->{name});\n'
412                    .format(type=o.fieldtype, name=o.fieldname))
413     else:
414         output += '    /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
415
416     return output
417
418
419 def endianfun(objs, modulename):
420     output = '''\
421
422 /****** Endian swap functions *****/\n\
423 #ifdef vl_endianfun
424 #ifndef included_{module}_endianfun
425 #define included_{module}_endianfun
426
427 #undef clib_net_to_host_uword
428 #ifdef LP64
429 #define clib_net_to_host_uword clib_net_to_host_u64
430 #else
431 #define clib_net_to_host_uword clib_net_to_host_u32
432 #endif
433
434 '''
435     output = output.format(module=modulename)
436
437     signature = '''\
438 static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
439 {{
440     int i __attribute__((unused));
441 '''
442
443     for t in objs:
444         if t.__class__.__name__ == 'Enum':
445             output += signature.format(name=t.name)
446             if t.enumtype in endian_strings:
447                 output += ('    *a = {}(*a);\n'
448                            .format(endian_strings[t.enumtype]))
449             else:
450                 output += ('    /* a->{name} = a->{name} (no-op) */\n'
451                            .format(name=t.name))
452
453             output += '}\n\n'
454             continue
455
456         if t.manual_endian:
457             output += "/***** manual: vl_api_%s_t_endian  *****/\n\n" % t.name
458             continue
459
460         if t.__class__.__name__ == 'Using':
461             output += signature.format(name=t.name)
462             if ('length' in t.alias and t.alias['length'] and
463                     t.alias['type'] == 'u8'):
464                 output += ('    /* a->{name} = a->{name} (no-op) */\n'
465                            .format(name=t.name))
466             elif t.alias['type'] in format_strings:
467                 output += ('    *a = {}(*a);\n'
468                            .format(endian_strings[t.alias['type']]))
469             else:
470                 output += '    /* Not Implemented yet {} */'.format(t.name)
471             output += '}\n\n'
472             continue
473
474         output += signature.format(name=t.name)
475
476         for o in t.block:
477             output += endianfun_obj(o)
478         output += '}\n\n'
479
480     output += "\n#endif"
481     output += "\n#endif /* vl_endianfun */\n\n"
482
483     return output
484
485
486 def version_tuple(s, module):
487     output = '''\
488 /****** Version tuple *****/
489
490 #ifdef vl_api_version_tuple
491
492 '''
493     if 'version' in s['Option']:
494         v = s['Option']['version']
495         (major, minor, patch) = v.split('.')
496         output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
497                   (module, major, minor, patch)
498
499     output += "\n#endif /* vl_api_version_tuple */\n\n"
500
501     return output
502
503
504 def generate_include_enum(s, module, stream):
505     write = stream.write
506
507     if len(s['Define']):
508         write('typedef enum {\n')
509         for t in s['Define']:
510             write('   VL_API_{},\n'.format(t.name.upper()))
511         write('   VL_MSG_FIRST_AVAILABLE\n')
512         write('}} vl_api_{}_enum_t;\n'.format(module))
513
514
515 #
516 # Generate separate API _types file.
517 #
518 def generate_include_types(s, module, stream):
519     write = stream.write
520
521     write('#ifndef included_{module}_api_types_h\n'.format(module=module))
522     write('#define included_{module}_api_types_h\n'.format(module=module))
523
524     if len(s['Import']):
525         write('/* Imported API files */\n')
526         for i in s['Import']:
527             filename = i.filename.replace('plugins/', '')
528             write('#include <{}_types.h>\n'.format(filename))
529
530     for o in s['types'] + s['Define']:
531         tname = o.__class__.__name__
532         if tname == 'Using':
533             if 'length' in o.alias:
534                 write('typedef %s vl_api_%s_t[%s];\n' % (o.alias['type'], o.name, o.alias['length']))
535             else:
536                 write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
537         elif tname == 'Enum':
538             if o.enumtype == 'u32':
539                 write("typedef enum {\n")
540             else:
541                 write("typedef enum __attribute__((packed)) {\n")
542
543             for b in o.block:
544                 write("    %s = %s,\n" % (b[0], b[1]))
545             write('} vl_api_%s_t;\n' % o.name)
546             if o.enumtype != 'u32':
547                 size1 = 'sizeof(vl_api_%s_t)' % o.name
548                 size2 = 'sizeof(%s)' % o.enumtype
549                 err_str = 'size of API enum %s is wrong' % o.name
550                 write('STATIC_ASSERT(%s == %s, "%s");\n'
551                       % (size1, size2, err_str))
552         else:
553             if tname == 'Union':
554                 write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" % o.name)
555             else:
556                 write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
557                            % o.name)
558             for b in o.block:
559                 if b.type == 'Option':
560                     continue
561                 if b.type == 'Field':
562                       write("    %s %s;\n" % (api2c(b.fieldtype),
563                                               b.fieldname))
564                 elif b.type == 'Array':
565                     if b.lengthfield:
566                       write("    %s %s[0];\n" % (api2c(b.fieldtype),
567                                                  b.fieldname))
568                     else:
569                         # Fixed length strings decay to nul terminated u8
570                         if b.fieldtype == 'string':
571                             if b.modern_vla:
572                                 write('    {} {};\n'
573                                       .format(api2c(b.fieldtype),
574                                               b.fieldname))
575                             else:
576                                 write('    u8 {}[{}];\n'
577                                       .format(b.fieldname, b.length))
578                         else:
579                             write("    %s %s[%s];\n" %
580                                   (api2c(b.fieldtype), b.fieldname,
581                                    b.length))
582                 else:
583                     raise ValueError("Error in processing type {} for {}"
584                                      .format(b, o.name))
585
586             write('} vl_api_%s_t;\n' % o.name)
587
588     for t in s['Define']:
589         write('#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'
590               .format(n=t.name, ID=t.name.upper(), crc=t.crc))
591
592     write("\n#endif\n")
593
594
595 def generate_c_boilerplate(services, defines, file_crc, module, stream):
596     write = stream.write
597
598     hdr = '''\
599 #define vl_endianfun            /* define message structures */
600 #include "{module}.api.h"
601 #undef vl_endianfun
602
603 /* instantiate all the print functions we know about */
604 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
605 #define vl_printfun
606 #include "{module}.api.h"
607 #undef vl_printfun
608
609 '''
610
611     write(hdr.format(module=module))
612     write('static u16\n')
613     write('setup_message_id_table (void) {\n')
614     write('   api_main_t *am = my_api_main;\n')
615     write('   u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", VL_MSG_FIRST_AVAILABLE);\n'
616           .format(module, crc=file_crc))
617
618
619     for d in defines:
620         write('   vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
621               '                                VL_API_{ID} + msg_id_base);\n'
622               .format(n=d.name, ID=d.name.upper(), crc=d.crc))
623     for s in services:
624         write('   vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
625               '                           vl_api_{n}_t_handler, vl_noop_handler,\n'
626               '                           vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
627               '                           sizeof(vl_api_{n}_t), 1);\n'
628               .format(n=s.caller, ID=s.caller.upper()))
629
630     write('   return msg_id_base;\n')
631     write('}\n')
632
633
634 def generate_c_test_plugin_boilerplate(services, defines, file_crc, module, stream):
635     write = stream.write
636
637     define_hash = {d.name:d for d in defines}
638     replies = {}
639
640     hdr = '''\
641 #define vl_endianfun            /* define message structures */
642 #include "{module}.api.h"
643 #undef vl_endianfun
644
645 /* instantiate all the print functions we know about */
646 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
647 #define vl_printfun
648 #include "{module}.api.h"
649 #undef vl_printfun
650
651 '''
652
653     write(hdr.format(module=module))
654     for s in services:
655         try:
656             d = define_hash[s.reply]
657         except:
658             continue
659         if d.manual_print:
660             write('/* Manual definition requested for: vl_api_{n}_t_handler() */\n'
661                   .format(n=s.reply))
662             continue
663         if not define_hash[s.caller].autoreply:
664             write('/* Only autoreply is supported (vl_api_{n}_t_handler()) */\n'
665                   .format(n=s.reply))
666             continue
667         write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper()))
668         write('static void\n')
669         write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=s.reply))
670         write('   vat_main_t * vam = {}_test_main.vat_main;\n'.format(module))
671         write('   i32 retval = ntohl(mp->retval);\n')
672         write('   if (vam->async_mode) {\n')
673         write('      vam->async_errors += (retval < 0);\n')
674         write('   } else {\n')
675         write('      vam->retval = retval;\n')
676         write('      vam->result_ready = 1;\n')
677         write('   }\n')
678         write('}\n')
679         write('#endif\n')
680
681         for e in s.events:
682             if define_hash[e].manual_print:
683                 continue
684             write('static void\n')
685             write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e))
686             write('    vl_print(0, "{n} event called:");\n'.format(n=e))
687             write('    vl_api_{n}_t_print(mp, 0);\n'.format(n=e))
688             write('}\n')
689
690     write('static void\n')
691     write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
692     for s in services:
693         write('   vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
694               '                           vl_api_{n}_t_handler, vl_noop_handler,\n'
695               '                           vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
696               '                           sizeof(vl_api_{n}_t), 1);\n'
697               .format(n=s.reply, ID=s.reply.upper()))
698         write('   hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'.format(n=s.caller))
699         try:
700             write('   hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
701                   .format(n=s.caller, help=define_hash[s.caller].options['vat_help']))
702         except:
703             pass
704
705         # Events
706         for e in s.events:
707             write('   vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, "{n}",\n'
708                   '                           vl_api_{n}_t_handler, vl_noop_handler,\n'
709                   '                           vl_api_{n}_t_endian, vl_api_{n}_t_print,\n'
710                   '                           sizeof(vl_api_{n}_t), 1);\n'
711                   .format(n=e, ID=e.upper()))
712
713     write('}\n')
714
715     write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
716     write('{\n')
717     write('   {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
718     write('   mainp->vat_main = vam;\n')
719     write('   mainp->msg_id_base = vl_client_get_first_plugin_msg_id ("{n}_{crc:08x}");\n'
720           .format(n=module, crc=file_crc))
721     write('   if (mainp->msg_id_base == (u16) ~0)\n')
722     write('      return clib_error_return (0, "{} plugin not loaded...");\n'.format(module))
723     write('   setup_message_id_table (vam, mainp->msg_id_base);\n')
724     write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
725     write('    VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
726     write('#endif\n')
727     write('   return 0;\n')
728     write('}\n')
729
730
731 #
732 # Plugin entry point
733 #
734 def run(args, input_filename, s):
735     stream = StringIO()
736
737     if not args.outputdir:
738         sys.stderr.write('Missing --outputdir argument')
739         return None
740
741     basename = os.path.basename(input_filename)
742     filename, file_extension = os.path.splitext(basename)
743     modulename = filename.replace('.', '_')
744     filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h')
745     filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h')
746     filename_c = os.path.join(args.outputdir + '/' + basename + '.c')
747     filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c')
748
749     # Generate separate types file
750     st = StringIO()
751     generate_include_types(s, modulename, st)
752     with open (filename_types, 'w') as fd:
753         st.seek (0)
754         shutil.copyfileobj (st, fd)
755     st.close()
756
757     # Generate separate enum file
758     st = StringIO()
759     generate_include_enum(s, modulename, st)
760     with open (filename_enum, 'w') as fd:
761         st.seek (0)
762         shutil.copyfileobj (st, fd)
763     st.close()
764
765     # Generate separate C file
766     st = StringIO()
767     generate_c_boilerplate(s['Service'], s['Define'], s['file_crc'],
768                            modulename, st)
769     with open (filename_c, 'w') as fd:
770         st.seek (0)
771         shutil.copyfileobj(st, fd)
772     st.close()
773
774     # Generate separate C test file
775     # This is only supported for plugins at the moment
776     st = StringIO()
777     generate_c_test_plugin_boilerplate(s['Service'], s['Define'], s['file_crc'],
778                                        modulename, st)
779     with open (filename_c_test, 'w') as fd:
780         st.seek (0)
781         shutil.copyfileobj(st, fd)
782     st.close()
783
784     output = top_boilerplate.format(datestring=datestring,
785                                     input_filename=basename)
786     output += imports(s['Import'])
787     output += msg_ids(s)
788     output += msg_names(s)
789     output += msg_name_crc_list(s, filename)
790     output += typedefs(modulename)
791     printfun_types(s['types'], stream, modulename)
792     printfun(s['Define'], stream, modulename)
793     output += stream.getvalue()
794     stream.close()
795     output += endianfun(s['types'] + s['Define'], modulename)
796     output += version_tuple(s, basename)
797     output += bottom_boilerplate.format(input_filename=basename,
798                                         file_crc=s['file_crc'])
799
800     return output