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$',
56 '^round_value_to_list32$');
58 my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]);
59 open ROCKLIB
, "<$rocklib" or die("Couldn't open $rocklib: $!");
62 if(/^RB_WRAP\(([^)]+)\)/)
64 push(@ported_functions, $1);
73 if(/struct plugin_api \{/)
77 elsif($start && /\};/)
90 $line =~ s/(\n|\r)//g;
92 if($line =~ /([a-zA-Z *_]+)?\s?\(\*([^)]+)?\)\(([^)]+)\).*?;/)
98 $return_type = trim
($return_type);
99 $arguments =~ s/\s{2,}/ /g;
101 if( !grep($_ eq $name, @ported_functions) &&
102 !grep($name =~ $_, @forbidden_functions))
104 push(@functions, {'name' => $name, 'return' => $return_type, 'arg' => $arguments});
110 my $svnrev = '$Revision$';
114 /* Automatically generated of $svnrev from rocklib.c & plugin.h */
119 #define _ROCKCONF_H_ /* We don't need strcmp() etc. wrappers */
127 my %in_types = ('void' => \
&in_void
,
129 'unsigned' => \
&in_int
,
130 'unsignedint' => \
&in_int
,
131 'signed' => \
&in_int
,
132 'signedint' => \
&in_int
,
134 'unsignedshort' => \
&in_int
,
135 'signedshort' => \
&in_int
,
137 'unsignedlong' => \
&in_int
,
138 'signedlong' => \
&in_int
,
140 'unsignedchar' => \
&in_int
,
141 'signedchar' => \
&in_int
,
142 'size_t' => \
&in_int
,
143 'ssize_t' => \
&in_int
,
145 'char*' => \
&in_string
,
146 'signedchar*' => \
&in_string
,
147 'unsignedchar*' => \
&in_string
,
150 ), %out_types = ('void' => \
&out_void
,
152 'unsigned' => \
&out_int
,
153 'unsignedint' => \
&out_int
,
154 'signed' => \
&out_int
,
155 'signedint' => \
&out_int
,
156 'short' => \
&out_int
,
157 'unsignedshort' => \
&out_int
,
158 'signedshort' => \
&out_int
,
160 'unsignedlong' => \
&out_int
,
161 'signedlong' => \
&out_int
,
163 'unsignedchar' => \
&out_int
,
164 'signedchar' => \
&out_int
,
165 'size_t' => \
&out_int
,
166 'ssize_t' => \
&out_int
,
167 'off_t' => \
&out_int
,
168 'char*' => \
&out_string
,
169 'signedchar*' => \
&out_string
,
170 'unsignedchar*' => \
&out_string
,
171 'bool' => \
&out_bool
,
172 '_Bool' => \
&out_bool
177 return "\t(void)L;\n";
182 my ($name, $type, $pos) = @_;
183 return sprintf("\t%s %s = (%s) luaL_checkint(L, %d);\n", $type, $name, $type, $pos);
188 my ($name, $type, $pos) = @_;
189 return sprintf("\t%s %s = (%s) luaL_checkstring(L, %d);\n", $type, $name, $type, $pos)
194 my ($name, $type, $pos) = @_;
195 return sprintf("\tbool %s = luaL_checkboolean(L, %d);\n", $name, $pos)
201 return sprintf("\t%s;\n\treturn 0;\n", $name);
206 my ($name, $type) = @_;
207 return sprintf("\t%s result = %s;\n\tlua_pushinteger(L, result);\n\treturn 1;\n", $type, $name);
212 my ($name, $type) = @_;
213 return sprintf("\t%s result = %s;\n\tlua_pushstring(L, result);\n\treturn 1;\n", $type, $name);
218 my ($name, $type) = @_;
219 return sprintf("\tbool result = %s;\n\tlua_pushboolean(L, result);\n\treturn 1;\n", $name);
222 # Print the functions
224 foreach my $function (@functions)
226 my $valid = 1, @arguments = ();
227 # Check for supported arguments
228 foreach my $argument (split(/,/, @
$function{'arg'}))
230 $argument = trim
($argument);
231 if($argument !~ /\[.+\]/ && ($argument =~ /^(.+[\s*])([^[*\s]*)/
232 || $argument eq "void"))
234 my $literal_type, $type, $name;
235 if($argument eq "void")
237 $literal_type = "void", $type = "void", $name = "";
241 $literal_type = trim
($1), $name = trim
($2), $type = trim
($1);
242 $type =~ s/(\s|const)//g;
246 $name = rand_string
();
250 #printf "/* %s: %s|%s */\n", @$function{'name'}, $type, $name;
251 if(!defined $in_types{$type})
257 push(@arguments, {'name' => $name,
259 'literal_type' => $literal_type
269 # Check for supported return value
270 my $return = @
$function{'return'};
271 $return =~ s/(\s|const)//g;
272 #printf "/* %s: %s [%d] */\n", @$function{'name'}, $return, $valid;
273 if(!defined $out_types{$return})
281 printf "static int rock_%s(lua_State *L)\n".
285 # Print the arguments
287 foreach my $argument (@arguments)
289 print $in_types{@
$argument{'type'}}->(@
$argument{'name'}, @
$argument{'literal_type'}, $i++);
292 # Generate the arguments string
293 my $func_args = $arguments[0]{'name'};
294 for(my $i = 1; $i < $#arguments + 1; $i++)
296 $func_args .= ", ".$arguments[$i]{'name'};
299 # Print the function call
300 my $func = sprintf("rb->%s(%s)", @
$function{'name'}, $func_args);
303 print $out_types{$return}->($func, @
$function{'return'});
306 push(@valid_functions, $function);
311 print "const luaL_Reg rocklib_aux[] =\n{\n";
312 foreach my $function (@valid_functions)
314 printf "\t{\"%s\", rock_%s},\n", @
$function{'name'}, @
$function{'name'};
316 print "\t{NULL, NULL}\n};\n\n";