summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/rbdefines_helper.pl
blob: e788855e874c7e778ce23f9692346b1c1881759c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env perl
############################################################################
#             __________               __   ___.                  
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___  
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /  
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <   
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/ 
# $Id$
#
# Copyright (C) 2019 by William Wilgus
#
# All files in this archive are subject to the GNU General Public License.
# See the file COPYING in the source tree root for full license agreement.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
############################################################################

#rockbox to lua define generator, add names of constants to the array to include

if ($#ARGV + 1 != 1) {
    warn "no definition type defined";
    exit;
}

my $def_type = $ARGV[0];
#warn "$def_type\n";
my $lua_table;
my @rockbox_defines;

if ($def_type eq "rb_defines") {
    $lua_table = "rb";
    @rockbox_defines = (
        '^HZ$',
        '^LCD_(DEPTH|HEIGHT|WIDTH)$',
        '^MODEL_NAME$',
        '^SCREEN_MAIN$',
        '^LCD_DEFAULT_(FG|BG)$',
        '^LCD_REMOTE_(DEPTH|HEIGHT|WIDTH)$',
        '^LCD_.+(BRIGHT|DARK)COLOR',
        '^SCREEN_REMOTE$',
        '^FONT_SYSFIXED$',
        '^FONT_UI$',
        '^PLAYBACK_EVENT_.*',
        '^PLAYLIST_(INSERT|PREPEND|REPLACE)',
        '^TOUCHSCREEN_(POINT|BUTTON)$',
        '^SYS_CHARGER_(DIS|)CONNECTED$',
        '^SYS_(TIMEOUT|POWEROFF|BATTERY_UPDATE)$',
        '^SYS_USB_(DIS|)CONNECTED$',
        '^HOME_DIR$',
        '^PLUGIN_DIR$',
        '^PLUGIN(_APPS_|_GAMES_|_)DATA_DIR$',
        '^ROCKBOX_DIR$',
        '^STYLE_(NONE|DEFAULT|INVERT|COLORBAR|GRADIENT|COLORED)',
        '^VIEWERS_DATA_DIR$');
}
elsif ($def_type eq "sound_defines") {
    $lua_table = "rb.sound_settings";
    @rockbox_defines = (
        '^(?!.*LAST_SETTING)SOUND_');
}

my @captured_defines;
my @names_seen;
my @all_defines;

############# precompile regex for speed #############
my $def_regex = qr/#define\s+([^\s\r\n]+)\s+([^\r\n]+)/;
my $quot_regex = qr/.*([\"\']).*/;
my $num_regex = qr/.*([\+\-\*\\|&\d]).*/;
my $configh_regex = qr/^\s*#define\s*__CONFIG_H__\s*$/;
my $config_h = "?";
my $exclude_regex = qr/^#define\s*_?POSIX.*/;
my $exclude_enum_regex = qr/^#define\s*(_SC_|_PC_|_CS_).*/;
print <<EOF
#include <stdio.h>
#include <stdbool.h>

#define stringify(s) #s

/* auto generated defines */


EOF
;

while(my $line = <STDIN>)
{
        if($config_h eq "?" && $line =~ $configh_regex) { $config_h = $line; }
        next if($config_h eq "?"); #don't capture till we get to the config file
        next if($line =~ $exclude_regex);

        if($line =~ $def_regex) #does it begin with #define?
        {
            push(@all_defines, $line); #save all defines for possible ambiguous macros
            $name = $1;
            next if $name =~ /^(ATTRIBUTE_|print|__).*/; #don't add reserved
            $value = $2;

            if(grep($name =~ $_, @rockbox_defines))
            {
                push(@names_seen, $name);
                push(@captured_defines, {'name' => $name, 'value' => $value});
                print $line
            }
            else
            {
                $name =~ s{(\(.*\))}{}gx; #remove (...) on function type macros
                #ifndef guard so we don't clash with the host
                printf "#ifndef %s\n%s#endif\n\n", $name, $line;
            }
        }
        elsif($line =~ /^(?!.*;)enum.*{/) #enum {
        {
            next if($line =~ /enum\s*__.*/); #don't add reserved
            print $line;
            next if($line =~ /.*};.*/);
            do_enum($line)
        }
        elsif($line =~ /^(?!.*[;\(\)])enum.*/) #enum
        {
            next if($line =~ /enum\s*__.*/); #don't add reserved
            #need to be careful might be a function returning enum
            my $lastline = $line;
            next if($line =~ /.*};.*/);
            ($line = <STDIN>);
            if($line =~ /.*{.*/)
            {
                print $lastline;
                print $line;
            }
            else { next; }
            do_enum($line)
        }
        elsif($line =~ /^enum.*{[^;]+};.*/) #enum {
        {
            next if($line =~ /enum\s*__.*/); #don't add reserved
            next if(do_enum($line));
            
        }
}
#warn "total defines: ".scalar @all_defines;
#warn "captured defines: ".scalar @captured_defines;
#Sort the functions
my @sorted_defines = sort { @$a{'name'} cmp @$b{'name'} } @captured_defines;

printf "int main(void)\n{\n";
printf "\tprintf(\"--[[Autogenerated rockbox constants]]\\n\\n\");\n\n";
printf "\tprintf(\"%s = %s or {}\\n\");\n", $lua_table, $lua_table;
# Print the C array
foreach my $define (@sorted_defines)
{
    if(@$define{'value'} =~ /^0[xX][0-9a-fA-F]+$/) #hex number
    {
        printf "\tprintf(\"%s[\\\"%%s\\\"] = 0x%%x\\n\", stringify(%s), %s);\n", $lua_table, @$define{'name'}, @$define{'name'};
    }
    elsif(@$define{'value'} =~ /^[0-9]+$/) #number
    {
        printf "\tprintf(\"%s[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", $lua_table, @$define{'name'}, @$define{'name'};
    }
    else #might be a string but we don't know since the macro isn't expanded far enough
    {
        my $max_depth = 10;
        my $var = @$define{'value'};
        while($max_depth > 0) #follow the chain of #defines until we can see what type
        {
            $max_depth--;
            $var = "\\\s*#define\\s+$var\\s+.*";
            #warn $var;
            if(my ($x) = grep($_ =~ /($var)/, @all_defines)) #check all the defines
            {
                #warn $x;
                if($x =~ $def_regex)
                {
                    $var = $2;
                    #warn $var;
                    last if ($var =~ $quot_regex);
                    last if ($var =~ $num_regex);
                    next
                }
                #warn "end ".$var;
                last
            }
            else
            {
                $var = @$define{'value'};
                last
            }
        }
        if ($var =~$quot_regex) #has a quote it is a string
        {
            #guard with empty literals "" so gcc throws an error if it isn't a string
            printf "\tprintf(\"%s[\\\"%%s\\\"] = \\\"%%s\\\"\\n\", stringify(%s), \"\" %s \"\");\n", $lua_table, @$define{'name'}, @$define{'name'};
        }
        elsif ($var =~$num_regex) #it must be a number
        {
            printf "\tprintf(\"%s[\\\"%%s\\\"] = %%d\\n\", stringify(%s), %s);\n", $lua_table, @$define{'name'}, @$define{'name'};
        }
        else { warn "Skipping ".@$define{'name'}." indeterminate macro type\n"; }
    }
}

print <<EOF
    return 0;
}

EOF
;

sub do_enum {
    my ($line) = @_;
    if($line =~ /.*enum.*{(.*)};.*/) #single line enums
    {
        print $line;
        $value = "0"; #enums are always integers
        my $enum = $1;
        $enum =~ s/\s+//g;;
        my @values = split(',', $enum);

        foreach my $name(@values) {
            if(grep($name =~ $_, @rockbox_defines))
            {
                push(@names_seen, $name);
                push(@captured_defines, {'name' => $name, 'value' => $value});
            }
        }
        return 1;
    }

    while($line = <STDIN>)
    {
        next if($line =~ $exclude_enum_regex);
        if($line =~ /.*};.*/)
        {
            print $line;
            last
        }
        elsif($line =~ /([^\s,\t]+)\s*=?.*,?/)
        {
            $name = $1;
            #printf "%s WHATTT?", $name;
            $value = "0"; #enums are always integers
        }
        print $line;
        if(grep($name =~ $_, @rockbox_defines))
        {
            push(@names_seen, $name);
            push(@captured_defines, {'name' => $name, 'value' => $value});
        }
        
    }
    return 0;
}