4 # Registration Macros Generator
6 # Copyright 2022 by Moshe Kaplan
7 # Based on make-reg.pl by Luis E. Garcia Onatnon <luis.ontanon@gmail.com>
9 # Wireshark - Network traffic analyzer
10 # By Gerald Combs <gerald@wireshark.org>
11 # Copyright 1998 Gerald Combs
13 # SPDX-License-Identifier: GPL-2.0-or-later
19 def parse_file(file_content
):
20 # extract classes, functions, and internal functions
21 # and return them as a tuple
22 CLASS_PATTERN
= r
'WSLUA_CLASS_DEFINE(?:_BASE)?\050\s*([A-Za-z0-9]+)'
23 FUNCTION_PATTERN
= r
'WSLUA_FUNCTION\s+wslua_([a-z_0-9]+)'
24 INTERNAL_FUNCTION_PATTERN
= r
'WSLUA_INTERNAL_FUNCTION\s+wslua_([a-z_0-9]+)'
26 class_matches
= re
.findall(CLASS_PATTERN
, file_content
)
27 function_matches
= re
.findall(FUNCTION_PATTERN
, file_content
)
28 internal_function_matches
= re
.findall(INTERNAL_FUNCTION_PATTERN
, file_content
)
29 return class_matches
, function_matches
, internal_function_matches
32 def generate_declare_wslua_h(classes
, functions
, internal_functions
):
34 output_lines
+= ["/* This file is automatically generated by make-reg.py; do not edit! */\n"]
35 output_lines
+= ["#define WSLUA_DECLARE_CLASSES() \\"]
36 for lua_class
in classes
:
37 output_lines
+= ["\tWSLUA_CLASS_DECLARE({lua_class});\\".format(lua_class
=lua_class
)]
39 output_lines
+= ["\n"]
40 output_lines
+= ["#define WSLUA_DECLARE_FUNCTIONS() \\"]
41 for lua_function
in functions
:
42 output_lines
+= ["\tWSLUA_FUNCTION wslua_{lua_function}(lua_State* L);\\".format(lua_function
=lua_function
)]
44 for lua_internal_function
in internal_functions
:
45 output_lines
+= ["\tWSLUA_INTERNAL_FUNCTION wslua_{lua_internal_function}(lua_State* L);\\".format(lua_internal_function
=lua_internal_function
)]
47 output_lines
+= ["\n"]
48 output_lines
+= ["extern void wslua_register_classes(lua_State* L);"]
49 output_lines
+= ["extern void wslua_register_functions(lua_State* L);"]
50 output_lines
+= ["\n\n"]
51 return "\n".join(output_lines
)
54 def generate_register_wslua_c(classes
, functions
, internal_functions
):
56 output_lines
+= ["/* This file is automatically generated by make-reg.py; do not edit! */\n"]
57 output_lines
+= ['#include "config.h"']
58 output_lines
+= ['#include "wslua.h"\n']
59 output_lines
+= ['#include "lua_bitop.h"\n']
60 output_lines
+= ["static void wslua_reg_module(lua_State* L, const char *name _U_, lua_CFunction func) {"]
61 output_lines
+= ["\tlua_pushcfunction(L, func);"]
62 output_lines
+= ["\tlua_call(L, 0, 0);"]
63 output_lines
+= ["}\n"]
64 output_lines
+= ["static void wslua_reg_library(lua_State* L, const char *name, lua_CFunction func) {"]
65 output_lines
+= ['\tluaL_requiref(L, name, func, 1);']
66 output_lines
+= ['\tlua_pop(L, 1); /* remove lib */']
67 output_lines
+= ["}\n"]
68 output_lines
+= ["void wslua_register_classes(lua_State* L) {"]
69 for lua_class
in classes
:
70 output_lines
+= ["\twslua_reg_module(L, \"{lua_class}\", {lua_class}_register);".format(lua_class
=lua_class
)]
71 output_lines
+= ['\twslua_reg_library(L, "bit", luaopen_bit);']
72 output_lines
+= ['\twslua_reg_library(L, "rex_pcre2", luaopen_rex_pcre2);']
73 output_lines
+= ["}\n"]
75 output_lines
+= ["void wslua_register_functions(lua_State* L) {"]
76 for lua_function
in functions
:
77 output_lines
+= ["\tWSLUA_REGISTER_FUNCTION({lua_function});".format(lua_function
=lua_function
)]
79 for lua_internal_function
in internal_functions
:
80 output_lines
+= ["\tWSLUA_REGISTER_FUNCTION({lua_internal_function});".format(lua_internal_function
=lua_internal_function
)]
82 output_lines
+= ["}\n"]
84 return "\n".join(output_lines
)
88 parser
= argparse
.ArgumentParser(description
="Generate the registration macros for Lua code.")
89 parser
.add_argument("files", metavar
='files', nargs
='+', help="paths to Lua C code")
90 parsed_args
= parser
.parse_args()
94 lua_internal_functions
= []
95 for filename
in parsed_args
.files
:
96 with
open(filename
, encoding
='utf-8') as fh
:
97 class_matches
, function_matches
, internal_function_matches
= parse_file(fh
.read())
98 lua_classes
+= class_matches
99 lua_functions
+= function_matches
100 lua_internal_functions
+= internal_function_matches
102 declare_wslua_h_content
= generate_declare_wslua_h(lua_classes
, lua_functions
, lua_internal_functions
)
103 register_wslua_c_content
= generate_register_wslua_c(lua_classes
, lua_functions
, lua_internal_functions
)
105 with
open('register_wslua.c', mode
='w', encoding
='utf-8') as fh
:
106 fh
.write(register_wslua_c_content
)
108 with
open('declare_wslua.h', mode
='w', encoding
='utf-8') as fh
:
109 fh
.write(declare_wslua_h_content
)
112 if __name__
== '__main__':