1aaaa20b6SVladimir Sementsov-Ogievskiy#! /usr/bin/env python3 2aaaa20b6SVladimir Sementsov-Ogievskiy"""Generate coroutine wrappers for block subsystem. 3aaaa20b6SVladimir Sementsov-Ogievskiy 4aaaa20b6SVladimir Sementsov-OgievskiyThe program parses one or several concatenated c files from stdin, 576a2f554SEmanuele Giuseppe Espositosearches for functions with the 'co_wrapper' specifier 6aaaa20b6SVladimir Sementsov-Ogievskiyand generates corresponding wrappers on stdout. 7aaaa20b6SVladimir Sementsov-Ogievskiy 8aaaa20b6SVladimir Sementsov-OgievskiyUsage: block-coroutine-wrapper.py generated-file.c FILE.[ch]... 9aaaa20b6SVladimir Sementsov-Ogievskiy 10aaaa20b6SVladimir Sementsov-OgievskiyCopyright (c) 2020 Virtuozzo International GmbH. 11aaaa20b6SVladimir Sementsov-Ogievskiy 12aaaa20b6SVladimir Sementsov-OgievskiyThis program is free software; you can redistribute it and/or modify 13aaaa20b6SVladimir Sementsov-Ogievskiyit under the terms of the GNU General Public License as published by 14aaaa20b6SVladimir Sementsov-Ogievskiythe Free Software Foundation; either version 2 of the License, or 15aaaa20b6SVladimir Sementsov-Ogievskiy(at your option) any later version. 16aaaa20b6SVladimir Sementsov-Ogievskiy 17aaaa20b6SVladimir Sementsov-OgievskiyThis program is distributed in the hope that it will be useful, 18aaaa20b6SVladimir Sementsov-Ogievskiybut WITHOUT ANY WARRANTY; without even the implied warranty of 19aaaa20b6SVladimir Sementsov-OgievskiyMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20aaaa20b6SVladimir Sementsov-OgievskiyGNU General Public License for more details. 21aaaa20b6SVladimir Sementsov-Ogievskiy 22aaaa20b6SVladimir Sementsov-OgievskiyYou should have received a copy of the GNU General Public License 23aaaa20b6SVladimir Sementsov-Ogievskiyalong with this program. If not, see <http://www.gnu.org/licenses/>. 24aaaa20b6SVladimir Sementsov-Ogievskiy""" 25aaaa20b6SVladimir Sementsov-Ogievskiy 26aaaa20b6SVladimir Sementsov-Ogievskiyimport sys 27aaaa20b6SVladimir Sementsov-Ogievskiyimport re 28aaaa20b6SVladimir Sementsov-Ogievskiyfrom typing import Iterator 29aaaa20b6SVladimir Sementsov-Ogievskiy 30aaaa20b6SVladimir Sementsov-Ogievskiy 31aaaa20b6SVladimir Sementsov-Ogievskiydef gen_header(): 32aaaa20b6SVladimir Sementsov-Ogievskiy copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL) 33aaaa20b6SVladimir Sementsov-Ogievskiy copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE) 34aaaa20b6SVladimir Sementsov-Ogievskiy copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE) 35aaaa20b6SVladimir Sementsov-Ogievskiy return f"""\ 36aaaa20b6SVladimir Sementsov-Ogievskiy/* 37aaaa20b6SVladimir Sementsov-Ogievskiy * File is generated by scripts/block-coroutine-wrapper.py 38aaaa20b6SVladimir Sementsov-Ogievskiy * 39aaaa20b6SVladimir Sementsov-Ogievskiy{copyright} 40aaaa20b6SVladimir Sementsov-Ogievskiy */ 41aaaa20b6SVladimir Sementsov-Ogievskiy 42aaaa20b6SVladimir Sementsov-Ogievskiy#include "qemu/osdep.h" 43aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/coroutines.h" 44aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/block-gen.h" 45e2c1c34fSMarkus Armbruster#include "block/block_int.h" 46e2c1c34fSMarkus Armbruster#include "block/dirty-bitmap.h" 47aaaa20b6SVladimir Sementsov-Ogievskiy""" 48aaaa20b6SVladimir Sementsov-Ogievskiy 49aaaa20b6SVladimir Sementsov-Ogievskiy 50aaaa20b6SVladimir Sementsov-Ogievskiyclass ParamDecl: 51aaaa20b6SVladimir Sementsov-Ogievskiy param_re = re.compile(r'(?P<decl>' 52aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<type>.*[ *])' 53aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<name>[a-z][a-z0-9_]*)' 54aaaa20b6SVladimir Sementsov-Ogievskiy r')') 55aaaa20b6SVladimir Sementsov-Ogievskiy 56aaaa20b6SVladimir Sementsov-Ogievskiy def __init__(self, param_decl: str) -> None: 57aaaa20b6SVladimir Sementsov-Ogievskiy m = self.param_re.match(param_decl.strip()) 58aaaa20b6SVladimir Sementsov-Ogievskiy if m is None: 59aaaa20b6SVladimir Sementsov-Ogievskiy raise ValueError(f'Wrong parameter declaration: "{param_decl}"') 60aaaa20b6SVladimir Sementsov-Ogievskiy self.decl = m.group('decl') 61aaaa20b6SVladimir Sementsov-Ogievskiy self.type = m.group('type') 62aaaa20b6SVladimir Sementsov-Ogievskiy self.name = m.group('name') 63aaaa20b6SVladimir Sementsov-Ogievskiy 64aaaa20b6SVladimir Sementsov-Ogievskiy 65aaaa20b6SVladimir Sementsov-Ogievskiyclass FuncDecl: 66*d6ee2e32SKevin Wolf def __init__(self, wrapper_type: str, return_type: str, name: str, 67*d6ee2e32SKevin Wolf args: str, variant: str) -> None: 68aaaa20b6SVladimir Sementsov-Ogievskiy self.return_type = return_type.strip() 69aaaa20b6SVladimir Sementsov-Ogievskiy self.name = name.strip() 7076a2f554SEmanuele Giuseppe Esposito self.struct_name = snake_to_camel(self.name) 71aaaa20b6SVladimir Sementsov-Ogievskiy self.args = [ParamDecl(arg.strip()) for arg in args.split(',')] 7276a2f554SEmanuele Giuseppe Esposito self.create_only_co = 'mixed' not in variant 73e6d3f7a6SEmanuele Giuseppe Esposito self.graph_rdlock = 'bdrv_rdlock' in variant 7476a2f554SEmanuele Giuseppe Esposito 75*d6ee2e32SKevin Wolf self.wrapper_type = wrapper_type 76*d6ee2e32SKevin Wolf 77*d6ee2e32SKevin Wolf if wrapper_type == 'co': 7876a2f554SEmanuele Giuseppe Esposito subsystem, subname = self.name.split('_', 1) 79*d6ee2e32SKevin Wolf self.target_name = f'{subsystem}_co_{subname}' 80*d6ee2e32SKevin Wolf else: 81*d6ee2e32SKevin Wolf assert wrapper_type == 'no_co' 82*d6ee2e32SKevin Wolf subsystem, co_infix, subname = self.name.split('_', 2) 83*d6ee2e32SKevin Wolf if co_infix != 'co': 84*d6ee2e32SKevin Wolf raise ValueError(f"Invalid no_co function name: {self.name}") 85*d6ee2e32SKevin Wolf if not self.create_only_co: 86*d6ee2e32SKevin Wolf raise ValueError(f"no_co function can't be mixed: {self.name}") 87*d6ee2e32SKevin Wolf if self.graph_rdlock: 88*d6ee2e32SKevin Wolf raise ValueError(f"no_co function can't be rdlock: {self.name}") 89*d6ee2e32SKevin Wolf self.target_name = f'{subsystem}_{subname}' 9076a2f554SEmanuele Giuseppe Esposito 9176a2f554SEmanuele Giuseppe Esposito t = self.args[0].type 9276a2f554SEmanuele Giuseppe Esposito if t == 'BlockDriverState *': 930582fb82SEmanuele Giuseppe Esposito ctx = 'bdrv_get_aio_context(bs)' 9476a2f554SEmanuele Giuseppe Esposito elif t == 'BdrvChild *': 950582fb82SEmanuele Giuseppe Esposito ctx = 'bdrv_get_aio_context(child->bs)' 960582fb82SEmanuele Giuseppe Esposito elif t == 'BlockBackend *': 970582fb82SEmanuele Giuseppe Esposito ctx = 'blk_get_aio_context(blk)' 9876a2f554SEmanuele Giuseppe Esposito else: 990582fb82SEmanuele Giuseppe Esposito ctx = 'qemu_get_aio_context()' 1000582fb82SEmanuele Giuseppe Esposito self.ctx = ctx 101aaaa20b6SVladimir Sementsov-Ogievskiy 1025b317b8dSEmanuele Giuseppe Esposito self.get_result = 's->ret = ' 1035b317b8dSEmanuele Giuseppe Esposito self.ret = 'return s.ret;' 1045b317b8dSEmanuele Giuseppe Esposito self.co_ret = 'return ' 1055b317b8dSEmanuele Giuseppe Esposito self.return_field = self.return_type + " ret;" 1065b317b8dSEmanuele Giuseppe Esposito if self.return_type == 'void': 1075b317b8dSEmanuele Giuseppe Esposito self.get_result = '' 1085b317b8dSEmanuele Giuseppe Esposito self.ret = '' 1095b317b8dSEmanuele Giuseppe Esposito self.co_ret = '' 1105b317b8dSEmanuele Giuseppe Esposito self.return_field = '' 1115b317b8dSEmanuele Giuseppe Esposito 112aaaa20b6SVladimir Sementsov-Ogievskiy def gen_list(self, format: str) -> str: 113aaaa20b6SVladimir Sementsov-Ogievskiy return ', '.join(format.format_map(arg.__dict__) for arg in self.args) 114aaaa20b6SVladimir Sementsov-Ogievskiy 115aaaa20b6SVladimir Sementsov-Ogievskiy def gen_block(self, format: str) -> str: 116aaaa20b6SVladimir Sementsov-Ogievskiy return '\n'.join(format.format_map(arg.__dict__) for arg in self.args) 117aaaa20b6SVladimir Sementsov-Ogievskiy 118aaaa20b6SVladimir Sementsov-Ogievskiy 11976a2f554SEmanuele Giuseppe Esposito# Match wrappers declared with a co_wrapper mark 1206700dfb1SEmanuele Giuseppe Espositofunc_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)' 121*d6ee2e32SKevin Wolf r'(\s*coroutine_fn)?' 122*d6ee2e32SKevin Wolf r'\s*(?P<wrapper_type>(no_)?co)_wrapper' 12376a2f554SEmanuele Giuseppe Esposito r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*' 124aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<wrapper_name>[a-z][a-z0-9_]*)' 125aaaa20b6SVladimir Sementsov-Ogievskiy r'\((?P<args>[^)]*)\);$', re.MULTILINE) 126aaaa20b6SVladimir Sementsov-Ogievskiy 127aaaa20b6SVladimir Sementsov-Ogievskiy 128aaaa20b6SVladimir Sementsov-Ogievskiydef func_decl_iter(text: str) -> Iterator: 129aaaa20b6SVladimir Sementsov-Ogievskiy for m in func_decl_re.finditer(text): 130*d6ee2e32SKevin Wolf yield FuncDecl(wrapper_type=m.group('wrapper_type'), 131*d6ee2e32SKevin Wolf return_type=m.group('return_type'), 132aaaa20b6SVladimir Sementsov-Ogievskiy name=m.group('wrapper_name'), 13376a2f554SEmanuele Giuseppe Esposito args=m.group('args'), 13476a2f554SEmanuele Giuseppe Esposito variant=m.group('variant')) 135aaaa20b6SVladimir Sementsov-Ogievskiy 136aaaa20b6SVladimir Sementsov-Ogievskiy 137aaaa20b6SVladimir Sementsov-Ogievskiydef snake_to_camel(func_name: str) -> str: 138aaaa20b6SVladimir Sementsov-Ogievskiy """ 139aaaa20b6SVladimir Sementsov-Ogievskiy Convert underscore names like 'some_function_name' to camel-case like 140aaaa20b6SVladimir Sementsov-Ogievskiy 'SomeFunctionName' 141aaaa20b6SVladimir Sementsov-Ogievskiy """ 142aaaa20b6SVladimir Sementsov-Ogievskiy words = func_name.split('_') 143aaaa20b6SVladimir Sementsov-Ogievskiy words = [w[0].upper() + w[1:] for w in words] 144aaaa20b6SVladimir Sementsov-Ogievskiy return ''.join(words) 145aaaa20b6SVladimir Sementsov-Ogievskiy 146aaaa20b6SVladimir Sementsov-Ogievskiy 14776a2f554SEmanuele Giuseppe Espositodef create_mixed_wrapper(func: FuncDecl) -> str: 14876a2f554SEmanuele Giuseppe Esposito """ 14976a2f554SEmanuele Giuseppe Esposito Checks if we are already in coroutine 15076a2f554SEmanuele Giuseppe Esposito """ 151*d6ee2e32SKevin Wolf name = func.target_name 15276a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 153e6d3f7a6SEmanuele Giuseppe Esposito graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else '' 154e6d3f7a6SEmanuele Giuseppe Esposito 15576a2f554SEmanuele Giuseppe Esposito return f"""\ 1566700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') }) 15776a2f554SEmanuele Giuseppe Esposito{{ 15876a2f554SEmanuele Giuseppe Esposito if (qemu_in_coroutine()) {{ 159e6d3f7a6SEmanuele Giuseppe Esposito {graph_assume_lock} 1605b317b8dSEmanuele Giuseppe Esposito {func.co_ret}{name}({ func.gen_list('{name}') }); 16176a2f554SEmanuele Giuseppe Esposito }} else {{ 16276a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 1630582fb82SEmanuele Giuseppe Esposito .poll_state.ctx = {func.ctx}, 16476a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 16576a2f554SEmanuele Giuseppe Esposito 16676a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 16776a2f554SEmanuele Giuseppe Esposito }}; 16876a2f554SEmanuele Giuseppe Esposito 16976a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 17076a2f554SEmanuele Giuseppe Esposito 1716700dfb1SEmanuele Giuseppe Esposito bdrv_poll_co(&s.poll_state); 1725b317b8dSEmanuele Giuseppe Esposito {func.ret} 17376a2f554SEmanuele Giuseppe Esposito }} 17476a2f554SEmanuele Giuseppe Esposito}}""" 17576a2f554SEmanuele Giuseppe Esposito 17676a2f554SEmanuele Giuseppe Esposito 17776a2f554SEmanuele Giuseppe Espositodef create_co_wrapper(func: FuncDecl) -> str: 17876a2f554SEmanuele Giuseppe Esposito """ 17976a2f554SEmanuele Giuseppe Esposito Assumes we are not in coroutine, and creates one 18076a2f554SEmanuele Giuseppe Esposito """ 181*d6ee2e32SKevin Wolf name = func.target_name 18276a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 18376a2f554SEmanuele Giuseppe Esposito return f"""\ 1846700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') }) 18576a2f554SEmanuele Giuseppe Esposito{{ 18676a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 1870582fb82SEmanuele Giuseppe Esposito .poll_state.ctx = {func.ctx}, 18876a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 18976a2f554SEmanuele Giuseppe Esposito 19076a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 19176a2f554SEmanuele Giuseppe Esposito }}; 19276a2f554SEmanuele Giuseppe Esposito assert(!qemu_in_coroutine()); 19376a2f554SEmanuele Giuseppe Esposito 19476a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 19576a2f554SEmanuele Giuseppe Esposito 1966700dfb1SEmanuele Giuseppe Esposito bdrv_poll_co(&s.poll_state); 1975b317b8dSEmanuele Giuseppe Esposito {func.ret} 19876a2f554SEmanuele Giuseppe Esposito}}""" 19976a2f554SEmanuele Giuseppe Esposito 20076a2f554SEmanuele Giuseppe Esposito 201*d6ee2e32SKevin Wolfdef gen_co_wrapper(func: FuncDecl) -> str: 202bb436948SVladimir Sementsov-Ogievskiy assert not '_co_' in func.name 203*d6ee2e32SKevin Wolf assert func.wrapper_type == 'co' 204aaaa20b6SVladimir Sementsov-Ogievskiy 205*d6ee2e32SKevin Wolf name = func.target_name 20676a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 207bb436948SVladimir Sementsov-Ogievskiy 208e6d3f7a6SEmanuele Giuseppe Esposito graph_lock='' 209e6d3f7a6SEmanuele Giuseppe Esposito graph_unlock='' 210e6d3f7a6SEmanuele Giuseppe Esposito if func.graph_rdlock: 211e6d3f7a6SEmanuele Giuseppe Esposito graph_lock=' bdrv_graph_co_rdlock();' 212e6d3f7a6SEmanuele Giuseppe Esposito graph_unlock=' bdrv_graph_co_rdunlock();' 213e6d3f7a6SEmanuele Giuseppe Esposito 21476a2f554SEmanuele Giuseppe Esposito creation_function = create_mixed_wrapper 21576a2f554SEmanuele Giuseppe Esposito if func.create_only_co: 21676a2f554SEmanuele Giuseppe Esposito creation_function = create_co_wrapper 217aaaa20b6SVladimir Sementsov-Ogievskiy 218aaaa20b6SVladimir Sementsov-Ogievskiy return f"""\ 219aaaa20b6SVladimir Sementsov-Ogievskiy/* 220aaaa20b6SVladimir Sementsov-Ogievskiy * Wrappers for {name} 221aaaa20b6SVladimir Sementsov-Ogievskiy */ 222aaaa20b6SVladimir Sementsov-Ogievskiy 223aaaa20b6SVladimir Sementsov-Ogievskiytypedef struct {struct_name} {{ 224aaaa20b6SVladimir Sementsov-Ogievskiy BdrvPollCo poll_state; 2255b317b8dSEmanuele Giuseppe Esposito {func.return_field} 226aaaa20b6SVladimir Sementsov-Ogievskiy{ func.gen_block(' {decl};') } 227aaaa20b6SVladimir Sementsov-Ogievskiy}} {struct_name}; 228aaaa20b6SVladimir Sementsov-Ogievskiy 229aaaa20b6SVladimir Sementsov-Ogievskiystatic void coroutine_fn {name}_entry(void *opaque) 230aaaa20b6SVladimir Sementsov-Ogievskiy{{ 231aaaa20b6SVladimir Sementsov-Ogievskiy {struct_name} *s = opaque; 232aaaa20b6SVladimir Sementsov-Ogievskiy 233e6d3f7a6SEmanuele Giuseppe Esposito{graph_lock} 2345b317b8dSEmanuele Giuseppe Esposito {func.get_result}{name}({ func.gen_list('s->{name}') }); 235e6d3f7a6SEmanuele Giuseppe Esposito{graph_unlock} 236aaaa20b6SVladimir Sementsov-Ogievskiy s->poll_state.in_progress = false; 237aaaa20b6SVladimir Sementsov-Ogievskiy 238aaaa20b6SVladimir Sementsov-Ogievskiy aio_wait_kick(); 239aaaa20b6SVladimir Sementsov-Ogievskiy}} 240aaaa20b6SVladimir Sementsov-Ogievskiy 24176a2f554SEmanuele Giuseppe Esposito{creation_function(func)}""" 242aaaa20b6SVladimir Sementsov-Ogievskiy 243aaaa20b6SVladimir Sementsov-Ogievskiy 244*d6ee2e32SKevin Wolfdef gen_no_co_wrapper(func: FuncDecl) -> str: 245*d6ee2e32SKevin Wolf assert '_co_' in func.name 246*d6ee2e32SKevin Wolf assert func.wrapper_type == 'no_co' 247*d6ee2e32SKevin Wolf 248*d6ee2e32SKevin Wolf name = func.target_name 249*d6ee2e32SKevin Wolf struct_name = func.struct_name 250*d6ee2e32SKevin Wolf 251*d6ee2e32SKevin Wolf return f"""\ 252*d6ee2e32SKevin Wolf/* 253*d6ee2e32SKevin Wolf * Wrappers for {name} 254*d6ee2e32SKevin Wolf */ 255*d6ee2e32SKevin Wolf 256*d6ee2e32SKevin Wolftypedef struct {struct_name} {{ 257*d6ee2e32SKevin Wolf Coroutine *co; 258*d6ee2e32SKevin Wolf {func.return_field} 259*d6ee2e32SKevin Wolf{ func.gen_block(' {decl};') } 260*d6ee2e32SKevin Wolf}} {struct_name}; 261*d6ee2e32SKevin Wolf 262*d6ee2e32SKevin Wolfstatic void {name}_bh(void *opaque) 263*d6ee2e32SKevin Wolf{{ 264*d6ee2e32SKevin Wolf {struct_name} *s = opaque; 265*d6ee2e32SKevin Wolf 266*d6ee2e32SKevin Wolf {func.get_result}{name}({ func.gen_list('s->{name}') }); 267*d6ee2e32SKevin Wolf 268*d6ee2e32SKevin Wolf aio_co_wake(s->co); 269*d6ee2e32SKevin Wolf}} 270*d6ee2e32SKevin Wolf 271*d6ee2e32SKevin Wolf{func.return_type} coroutine_fn {func.name}({ func.gen_list('{decl}') }) 272*d6ee2e32SKevin Wolf{{ 273*d6ee2e32SKevin Wolf {struct_name} s = {{ 274*d6ee2e32SKevin Wolf .co = qemu_coroutine_self(), 275*d6ee2e32SKevin Wolf{ func.gen_block(' .{name} = {name},') } 276*d6ee2e32SKevin Wolf }}; 277*d6ee2e32SKevin Wolf assert(qemu_in_coroutine()); 278*d6ee2e32SKevin Wolf 279*d6ee2e32SKevin Wolf aio_bh_schedule_oneshot(qemu_get_aio_context(), {name}_bh, &s); 280*d6ee2e32SKevin Wolf qemu_coroutine_yield(); 281*d6ee2e32SKevin Wolf 282*d6ee2e32SKevin Wolf {func.ret} 283*d6ee2e32SKevin Wolf}}""" 284*d6ee2e32SKevin Wolf 285*d6ee2e32SKevin Wolf 286aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrappers(input_code: str) -> str: 287aaaa20b6SVladimir Sementsov-Ogievskiy res = '' 288aaaa20b6SVladimir Sementsov-Ogievskiy for func in func_decl_iter(input_code): 289aaaa20b6SVladimir Sementsov-Ogievskiy res += '\n\n\n' 290*d6ee2e32SKevin Wolf if func.wrapper_type == 'co': 291*d6ee2e32SKevin Wolf res += gen_co_wrapper(func) 292*d6ee2e32SKevin Wolf else: 293*d6ee2e32SKevin Wolf res += gen_no_co_wrapper(func) 294aaaa20b6SVladimir Sementsov-Ogievskiy 295aaaa20b6SVladimir Sementsov-Ogievskiy return res 296aaaa20b6SVladimir Sementsov-Ogievskiy 297aaaa20b6SVladimir Sementsov-Ogievskiy 298aaaa20b6SVladimir Sementsov-Ogievskiyif __name__ == '__main__': 299aaaa20b6SVladimir Sementsov-Ogievskiy if len(sys.argv) < 3: 300aaaa20b6SVladimir Sementsov-Ogievskiy exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') 301aaaa20b6SVladimir Sementsov-Ogievskiy 302aaaa20b6SVladimir Sementsov-Ogievskiy with open(sys.argv[1], 'w', encoding='utf-8') as f_out: 303aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_header()) 304aaaa20b6SVladimir Sementsov-Ogievskiy for fname in sys.argv[2:]: 305aaaa20b6SVladimir Sementsov-Ogievskiy with open(fname, encoding='utf-8') as f_in: 306aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_wrappers(f_in.read())) 307aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write('\n') 308