2 ############################################################################
4 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 # Copyright (C) 2009 by Maurus Cuelenaere
13 # All files in this archive are subject to the GNU General Public License.
14 # See the file COPYING in the source tree root for full license agreement.
16 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 # KIND, either express or implied.
19 ############################################################################
35 $ret .= $chars[rand @chars];
43 my @forbidden_functions = ('^open$',
57 my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]);
58 open ROCKLIB
, "<$rocklib" or die("Couldn't open $rocklib: $!");
61 if(/^RB_WRAP\(([^)]+)\)/)
63 push(@ported_functions, $1);
72 if(/struct plugin_api \{/)
76 elsif($start && /\};/)
89 $line =~ s/(\n|\r)//g;
91 if($line =~ /([a-zA-Z *_]+)?\s?\(\*([^)]+)?\)\(([^)]+)\).*?;/)
97 $return_type = trim
($return_type);
98 $arguments =~ s/\s{2,}/ /g;
100 if( !grep($_ eq $name, @ported_functions) &&
101 !grep($name =~ $_, @forbidden_functions))
103 push(@functions, {'name' => $name, 'return' => $return_type, 'arg' => $arguments});
109 my $svnrev = '$Revision$';
113 /* Automatically generated of $svnrev from rocklib.c & plugin.h */
118 #define _ROCKCONF_H_ /* We don't need strcmp() etc. wrappers */
126 my %in_types = ('void' => \
&in_void
,
128 'unsigned' => \
&in_int
,
129 'unsignedint' => \
&in_int
,
130 'signed' => \
&in_int
,
131 'signedint' => \
&in_int
,
133 'unsignedshort' => \
&in_int
,
134 'signedshort' => \
&in_int
,
136 'unsignedlong' => \
&in_int
,
137 'signedlong' => \
&in_int
,
139 'unsignedchar' => \
&in_int
,
140 'signedchar' => \
&in_int
,
141 'char*' => \
&in_string
,
142 'signedchar*' => \
&in_string
,
143 'unsignedchar*' => \
&in_string
,
146 ), %out_types = ('void' => \
&out_void
,
148 'unsigned' => \
&out_int
,
149 'unsignedint' => \
&out_int
,
150 'signed' => \
&out_int
,
151 'signedint' => \
&out_int
,
152 'short' => \
&out_int
,
153 'unsignedshort' => \
&out_int
,
154 'signedshort' => \
&out_int
,
156 'unsignedlong' => \
&out_int
,
157 'signedlong' => \
&out_int
,
159 'unsignedchar' => \
&out_int
,
160 'signedchar' => \
&out_int
,
161 'char*' => \
&out_string
,
162 'signedchar*' => \
&out_string
,
163 'unsignedchar*' => \
&out_string
,
164 'bool' => \
&out_bool
,
165 '_Bool' => \
&out_bool
170 return "\t(void)L;\n";
175 my ($name, $type, $pos) = @_;
176 return sprintf("\t%s %s = (%s) luaL_checkint(L, %d);\n", $type, $name, $type, $pos);
181 my ($name, $type, $pos) = @_;
182 return sprintf("\t%s %s = (%s) luaL_checkstring(L, %d);\n", $type, $name, $type, $pos)
187 my ($name, $type, $pos) = @_;
188 return sprintf("\tbool %s = luaL_checkboolean(L, %d);\n", $name, $pos)
194 return sprintf("\t%s;\n\treturn 0;\n", $name);
199 my ($name, $type) = @_;
200 return sprintf("\t%s result = %s;\n\tlua_pushinteger(L, result);\n\treturn 1;\n", $type, $name);
205 my ($name, $type) = @_;
206 return sprintf("\t%s result = %s;\n\tlua_pushstring(L, result);\n\treturn 1;\n", $type, $name);
211 my ($name, $type) = @_;
212 return sprintf("\tbool result = %s;\n\tlua_pushboolean(L, result);\n\treturn 1;\n", $name);
215 # Print the functions
217 foreach my $function (@functions)
219 my $valid = 1, @arguments = ();
220 # Check for supported arguments
221 foreach my $argument (split(/,/, @
$function{'arg'}))
223 $argument = trim
($argument);
224 if($argument !~ /\[.+\]/ && ($argument =~ /^(.+[\s*])([^[*\s]*)/
225 || $argument eq "void"))
227 my $literal_type, $type, $name;
228 if($argument eq "void")
230 $literal_type = "void", $type = "void", $name = "";
234 $literal_type = trim
($1), $name = trim
($2), $type = trim
($1);
235 $type =~ s/(\s|const)//g;
239 $name = rand_string
();
243 #printf "/* %s: %s|%s */\n", @$function{'name'}, $type, $name;
244 if(!defined $in_types{$type})
250 push(@arguments, {'name' => $name,
252 'literal_type' => $literal_type
262 # Check for supported return value
263 my $return = @
$function{'return'};
264 $return =~ s/(\s|const)//g;
265 #printf "/* %s: %s [%d] */\n", @$function{'name'}, $return, $valid;
266 if(!defined $out_types{$return})
274 printf "static int rock_%s(lua_State *L)\n".
278 # Print the arguments
280 foreach my $argument (@arguments)
282 print $in_types{@
$argument{'type'}}->(@
$argument{'name'}, @
$argument{'literal_type'}, $i++);
285 # Generate the arguments string
286 my $func_args = $arguments[0]{'name'};
287 for(my $i = 1; $i < $#arguments + 1; $i++)
289 $func_args .= ", ".$arguments[$i]{'name'};
292 # Print the function call
293 my $func = sprintf("rb->%s(%s)", @
$function{'name'}, $func_args);
296 print $out_types{$return}->($func, @
$function{'return'});
299 push(@valid_functions, $function);
304 print "const luaL_Reg rocklib_aux[] =\n{\n";
305 foreach my $function (@valid_functions)
307 printf "\t{\"%s\", rock_%s},\n", @
$function{'name'}, @
$function{'name'};
309 print "\t{NULL, NULL}\n};\n\n";