4 # Extract structs from C headers to generate a function that
5 # pushes a lua table into the stack containing the elements of
8 # (c) 2006 Luis E. Garcia Ontanon <luis@ontanon.org>
12 # Wireshark - Network traffic analyzer
13 # By Gerald Combs <gerald@wireshark.org>
14 # Copyright 2006 Gerald Combs
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation; either version 2
19 # of the License, or (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 'gchar[]' => 'lua_pushstring(L,(char*)v->%s);',
34 'gchar*' => 'lua_pushstring(L,(char*)v->%s);',
35 'guint' => 'lua_pushnumber(L,(lua_Number)v->%s);',
36 'guint8' => 'lua_pushnumber(L,(lua_Number)v->%s);',
37 'guint16' => 'lua_pushnumber(L,(lua_Number)v->%s);',
38 'guint32' => 'lua_pushnumber(L,(lua_Number)v->%s);',
39 'gint' => 'lua_pushnumber(L,(lua_Number)v->%s);',
40 'gint8' => 'lua_pushnumber(L,(lua_Number)v->%s);',
41 'gint16' => 'lua_pushnumber(L,(lua_Number)v->%s);',
42 'gint32' => 'lua_pushnumber(L,(lua_Number)v->%s);',
43 'gboolean' => 'lua_pushboolean(L,(int)v->%s);',
44 'address' => '{ Address a = (Address)g_malloc(sizeof(address)); COPY_ADDRESS(a, &(v->%s)); pushAddress(L,a); }',
45 'address*' => '{ Address a = (Address)g_malloc(sizeof(address)); COPY_ADDRESS(a, v->%s); pushAddress(L,a); }',
46 'int' => 'lua_pushnumber(L,(lua_Number)v->%s);',
47 'nstime_t' => '{lua_Number t = (lua_Number) v->%s.secs; t += v->%s.nsecs * 1e-9; lua_pushnumber(L,t); }',
48 'nstime_t*' => '{lua_Number t = (lua_Number) v->%s->secs; t += v->%s->nsecs * 1e-9; lua_pushnumber(L,t); }',
52 'gchar[]' => 'string',
56 'guint16' => 'number',
57 'guint32' => 'number',
62 'gboolean' => 'boolean',
63 'address' => 'Address',
64 'address*' => 'Address',
66 'nstime_t' => 'number (seconds, since 1-1-1970 if absolute)',
67 'nstime_t*' => 'number (seconds, since 1-1-1970 if absolute)',
76 my ($tname,$fname,$sname,@enums) = @_;
79 open FILE
, "< $fname";
85 $buf =~ s@
/\*.*?\*/@@
;
87 for my $ename (@enums) {
89 my $a = $enums{$ename};
91 my $enumre = "typedef\\s+enum[^{]*{([^}]*)}[\\s\\n]*" . ${ename
} . "[\\s\\n]*;";
92 if ($buf =~ s/$enumre//ms ) {
93 $types{$ename} = "/*$ename*/ lua_pushnumber(L,(lua_Number)v->%s);";
96 $comments{$ename} = "$ename: { $ebody }";
97 $comments{$ename} =~ s/,/|/g;
98 for (split /,/, $ebody) {
104 my $re = "typedef\\s+struct.*?{([^}]*)}[\\s\\n]*($sname)[\\s\\n]*;";
107 while ($buf =~ s/$re//ms) {
111 die "could not find typedef $sname in $fname" if not defined $body and $sname ne "void";
115 while($body =~ s/\s*(.*?)([\w\d_]+)\s*\[\s*\d+\s*\]\s*;//) {
116 my ($k,$v) = ($2,$1 . "[]");
121 while($body =~ s/\s*(.*?)([\w\d_]+)\s*;//) {
122 my ($k,$v) = ($2,$1);
127 my $code = "static void wslua_${tname}_to_table(lua_State* L, const void* p) { $sname* v _U_; v = ($sname*)p; lua_newtable(L);\n";
128 my $doc = "Tap: $tname\n";
130 for my $n (sort keys %elems) {
131 my $fmt = $types{$elems{$n}};
134 $code .= "\tlua_pushstring(L,\"$n\"); ";
135 $code .= sprintf($fmt,$n,$n) . " lua_settable(L,-3);\n";
136 $doc .= "\t$n: $comments{$elems{$n}}\n";
144 $functs{$tname} = "wslua_${tname}_to_table";
150 open TAPSFILE
, "< $ARGV[0]";
151 open CFILE
, "> $ARGV[1]";
152 open DOCFILE
, "> $ARGV[2]";
154 print CFILE
<<"HEADER";
155 /* This file is autogenerated from ./taps by ./make-taps.pl */
169 my ($tname,$fname,$sname,@enums) = split /\s+/;
170 my ($c,$doc) = dotap
($tname,$fname,$sname,@enums);
171 print CFILE
"#include \"$fname\"\n";
176 print CFILE
<<"TBLHDR";
177 static tappable_t tappables[] = {
180 for my $tname (sort keys %functs) {
181 print CFILE
<<"TBLELEM";
182 {"$tname", $functs{$tname} },
186 print CFILE
<<"TBLFTR";
191 int wslua_set_tap_enums(lua_State* L _U_) {
195 for my $ename (sort keys %enums) {
196 print CFILE
"\n\t/*\n\t * $ename\n\t */\n\tlua_newtable(L);\n";
197 for my $a (@
{$enums{$ename}}) {
198 print CFILE
<<"ENUMELEM";
199 lua_pushnumber(L,(lua_Number)$a); lua_setglobal(L,"$a");
200 lua_pushnumber(L,(lua_Number)$a); lua_pushstring(L,"$a"); lua_settable(L,-3);
203 print CFILE
"\tlua_setglobal(L,\"$ename\");\n";
206 print CFILE
<<"TAIL";
211 tap_extractor_t wslua_get_tap_extractor(const gchar* name) {
213 for(t = tappables; t->name; t++ ) {
214 if (g_str_equal(t->name,name)) return t->extractor;