fix red.
[kugel-rb/myfork.git] / apps / plugins / lua / rocklib_aux.pl
blobc9d590975c0cf92d5f2b6b31daa7ae80c44a075d
1 #!/usr/bin/env perl
2 ############################################################################
3 # __________ __ ___.
4 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 # \/ \/ \/ \/ \/
9 # $Id$
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 ############################################################################
21 sub trim
23 my $text = $_[0];
24 $text =~ s/^\s+//;
25 $text =~ s/\s+$//;
26 return $text;
29 sub rand_string
31 my @chars=('a'..'z');
32 my $ret;
33 foreach (1..5)
35 $ret .= $chars[rand @chars];
37 return $ret;
41 my @functions;
42 my @ported_functions;
43 my @forbidden_functions = ('^open$',
44 '^close$',
45 '^read$',
46 '^write$',
47 '^lseek$',
48 '^ftruncate$',
49 '^filesize$',
50 '^fdprintf$',
51 '^read_line$',
52 '^[a-z]+dir$',
53 '^__.+$',
54 '^.+_(un)?cached$',
55 '^audio_play$');
57 my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]);
58 open ROCKLIB, "<$rocklib" or die("Couldn't open $rocklib: $!");
59 while(<ROCKLIB>)
61 if(/^RB_WRAP\(([^)]+)\)/)
63 push(@ported_functions, $1);
66 close ROCKLIB;
68 # Parse plugin.h
69 my $start = 0;
70 while(<STDIN>)
72 if(/struct plugin_api \{/)
74 $start = 1;
76 elsif($start && /\};/)
78 $start = 0;
81 if($start == 1)
83 my $line = $_;
84 while($line !~ /;/)
86 $line .= <STDIN>;
89 $line =~ s/(\n|\r)//g;
91 if($line =~ /([a-zA-Z *_]+)?\s?\(\*([^)]+)?\)\(([^)]+)\).*?;/)
93 $return_type = $1;
94 $name = $2;
95 $arguments = $3;
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$';
111 # Print the header
112 print <<EOF
113 /* Automatically generated of $svnrev from rocklib.c & plugin.h */
115 #define lrocklib_c
116 #define LUA_LIB
118 #define _ROCKCONF_H_ /* We don't need strcmp() etc. wrappers */
119 #include "lua.h"
120 #include "lauxlib.h"
121 #include "plugin.h"
126 my %in_types = ('void' => \&in_void,
127 'int' => \&in_int,
128 'unsigned' => \&in_int,
129 'unsignedint' => \&in_int,
130 'signed' => \&in_int,
131 'signedint' => \&in_int,
132 'short' => \&in_int,
133 'unsignedshort' => \&in_int,
134 'signedshort' => \&in_int,
135 'long' => \&in_int,
136 'unsignedlong' => \&in_int,
137 'signedlong' => \&in_int,
138 'char' => \&in_int,
139 'unsignedchar' => \&in_int,
140 'signedchar' => \&in_int,
141 'char*' => \&in_string,
142 'signedchar*' => \&in_string,
143 'unsignedchar*' => \&in_string,
144 'bool' => \&in_bool,
145 '_Bool' => \&in_bool
146 ), %out_types = ('void' => \&out_void,
147 'int' => \&out_int,
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,
155 'long' => \&out_int,
156 'unsignedlong' => \&out_int,
157 'signedlong' => \&out_int,
158 'char' => \&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
168 sub in_void
170 return "\t(void)L;\n";
173 sub in_int
175 my ($name, $type, $pos) = @_;
176 return sprintf("\t%s %s = (%s) luaL_checkint(L, %d);\n", $type, $name, $type, $pos);
179 sub in_string
181 my ($name, $type, $pos) = @_;
182 return sprintf("\t%s %s = (%s) luaL_checkstring(L, %d);\n", $type, $name, $type, $pos)
185 sub in_bool
187 my ($name, $type, $pos) = @_;
188 return sprintf("\tbool %s = luaL_checkboolean(L, %d);\n", $name, $pos)
191 sub out_void
193 my $name = $_[0];
194 return sprintf("\t%s;\n\treturn 0;\n", $name);
197 sub out_int
199 my ($name, $type) = @_;
200 return sprintf("\t%s result = %s;\n\tlua_pushinteger(L, result);\n\treturn 1;\n", $type, $name);
203 sub out_string
205 my ($name, $type) = @_;
206 return sprintf("\t%s result = %s;\n\tlua_pushstring(L, result);\n\treturn 1;\n", $type, $name);
209 sub out_bool
211 my ($name, $type) = @_;
212 return sprintf("\tbool result = %s;\n\tlua_pushboolean(L, result);\n\treturn 1;\n", $name);
215 # Print the functions
216 my @valid_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 = "";
232 else
234 $literal_type = trim($1), $name = trim($2), $type = trim($1);
235 $type =~ s/(\s|const)//g;
237 if($name eq "")
239 $name = rand_string();
243 #printf "/* %s: %s|%s */\n", @$function{'name'}, $type, $name;
244 if(!defined $in_types{$type})
246 $valid = 0;
247 break;
250 push(@arguments, {'name' => $name,
251 'type' => $type,
252 'literal_type' => $literal_type
255 else
257 $valid = 0;
258 break;
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})
268 $valid = 0;
271 if($valid == 1)
273 # Print the header
274 printf "static int rock_%s(lua_State *L)\n".
275 "{\n",
276 @$function{'name'};
278 # Print the arguments
279 my $i = 1;
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);
295 # Print the footer
296 print $out_types{$return}->($func, @$function{'return'});
297 print "}\n\n";
299 push(@valid_functions, $function);
303 # Print the C array
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";