summaryrefslogtreecommitdiffstats
path: root/tools/genlang2
blob: 7d363d4cf743775faa5ec8bb362173ebf6196b81 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/perl -s
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#
# Copyright (C) 2006 by Daniel Stenberg
#

# binary version for the binary lang file
my $langversion = 2; # 2 is the latest one used in the v1 format

# A note for future users and readers: The original v1 language system allowed
# the build to create and use a different language than english built-in. We
# removed that feature from our build-system, but the build scripts still had
# the ability. But, starting now, this ability is no longer provided since I
# figured it was boring and unnecessary to write support for now since we
# don't use it anymore.

if(!$ARGV[0]) {
    print <<MOO
Usage: genlang2 [options] <langv2 file>

 -p=<prefix>
    Make the tool create a [prefix].c and [prefix].h file.

 -b=<outfile>
    Make the tool create a binary language (.lng) file namaed [outfile].
    The use of this option requires that you also use -e.

 -u
    Update language file. Given the translated file and the most recent english
    file, you\'ll get an updated version sent to stdout. Suitable action to do
    when you intend to update a translation.

 -e=<english lang file>
    Point out the english (original source) file, to use that as master
    language template. Used in combination with -b or -u.

 -t=<target>
    Specify which target you want the translations/phrases for. Required when
    -b or -p is used.

 -v
    Enables verbose (debug) output.
MOO
;
    exit;
}

# How update works:
#
# 1) scan the english file, keep the whole <phrase> for each phrase.
# 2) read the translated file, for each end of phrase, compare:
#  A) all source strings, if there's any change there should be a comment about
#     it output
#  B) the desc fields
#
# 3) output the phrase with the comments from above
# 4) check which phrases that the translated version didn't have, and spit out
#    the english version of those
#

my $prefix = $p;
my $binary = $b;
my $update = $u;

my $english = $e;

my $check = $binary?1:0 + $prefix?1:0 + $update?1:0;

if($check > 1) {
    print "Please use only one of -p, -u and -b\n";
    exit;
}
if(!$check) {
    print "Please use at least one of -p, -u and -b\n";
    exit;
}
if(($binary || $update) && !$english) {
    print "Please use -e too when you use -b or -u\n";
    exit;
}

my $target = $t;
if(!$target && !$update) {
    print "Please specify a target (with -t)!\n";
    exit;
}
my $verbose=$v;

my %id; # string to num hash
my @idnum; # num to string array

my %source; # id string to source phrase hash
my %dest; # id string to dest phrase hash
my %voice; # id string to voice phrase hash


my $input = $ARGV[0];

my @m;
my $m="blank";

sub match {
    my ($string, $pattern)=@_;

    $pattern =~ s/\*/.?*/g;
    $pattern =~ s/\?/./g;

    return ($string =~ $pattern);
}

sub blank {
    # nothing to do
}

my %head;
sub header {
    my ($full, $n, $v)=@_;
    $head{$n}=$v;
}

my %phrase;
sub phrase {
    my ($full, $n, $v)=@_;
    $phrase{$n}=$v;
}

sub parsetarget {
    my ($debug, $strref, $full, $n, $v)=@_;
    my $string;
    my @all= split(" *, *", $n);
    my $test;
    for $test (@all) {
#        print "TEST ($debug) $target for $test\n";
        if(match($target, $test)) {
            $string = $v;
#            print "MATCH: $test => $v\n";
        }
    }
    if($string) {
        $$strref = $string;
    }
    return $string;
}

my $src;
sub source {
    parsetarget("src", \$src, @_);
}

my $dest;
sub dest {
    parsetarget("dest", \$dest, @_);
}

my $voice;
sub voice {
    parsetarget("voice", \$voice, @_);
}

my %english;
if($english) {
    # For the cases where the english file needs to be scanned/read, we do
    # it before we read the translated file. For -b it isn't necessary, but for
    # -u it is convenient.

    my $idnum=0; # start with a true number
    my %idmap;
    open(ENG, "<$english") || die "can't open $english";
    my @phrase;
    my $id;
    while(<ENG>) {

        # get rid of DOS newlines
        $_ =~ s/\r//g;

        if($_ =~ /^ *\<phrase\>/) {
            # this is the start of a phrase
        }
        elsif($_ =~ /^ *\<\/phrase\>/) {
            # this is the end of a phrase, add it to the english hash
            $english{$id}=join("", @phrase);
            undef @phrase;
        }
        elsif($_ ne "\n") {
            # gather everything related to this phrase
            push @phrase, $_;
        }

        if($_ =~ /^ *id: ([^ \t\n]+)/i) {
            $id=$1;
            # Skip voice-only entries
            if($id =~ /^VOICE_/) {
                next;
            }

            # Assign an ID number to this entry
            $idmap{$id}=$idnum;
            $idnum++;
        }
    }
    close(ENG);
}

# a function that compares the english phrase with the translated one.
# compare source strings and desc
sub compare {
    my ($engref, $locref)=@_;
    my ($edesc, $ldesc);
    my ($esource, $lsource);
    my $mode=0;
    
    for my $l (@$engref) {
        if($l =~ /^ *desc: (.*)/) {
            $edesc=$1;
        }
        elsif($l =~ / *\<source\>/i) {
            $mode=1;
        }
        elsif($mode) {
            if($l =~ / *\<\/source\>/i) {
                last;
            }
            $esource .= "$l\n";
        }
    }

    $mode = 0;
    for my $l (@$locref) {
        if($l =~ /^ *desc: (.*)/) {
            $ldesc=$1;
        }
        elsif($l =~ / *\<source\>/i) {
            $mode=1;
        }
        elsif($mode) {
            if($l =~ / *\<\/source\>/i) {
                last;
            }
            $lsource .= "$l";
        }
    }

    if($edesc ne $ldesc) {
        print "### The 'desc' field differs from the english!\n";
    }
    if($esource ne $lsource) {
        print "### The <source> section differs from the english!\n";
    }
}

my $idcount;        # counter for lang ID numbers
my $voiceid=0x8000; # counter for voice-only ID numbers

#
# Now start the scanning of the selected language string
#

open(LANG, "<$input");
my @phrase;
while(<LANG>) {

    $line++;

    # get rid of DOS newlines
    $_ =~ s/\r//g;

    if($_ =~ /^( *\#|[ \t\n\r]*\z)/) {
        # comment or empty line
        next;
    }

    my $ll = $_;

   # print "M: $m\n";

    push @phrase, $ll;

    # this is an XML-lookalike tag
    if(/ *<([^>]*)>/) {
        my $part = $1;
        #print "P: $part\n";

        if($part =~ /^\//) {
            # this was a closing tag

            if($part eq "/phrase") {
                # closing the phrase

                my $idstr = $phrase{'id'};
                my $idnum;

                # Use the ID name to figure out which id number range we should
                # use for this phrase. Voice-only strings are separated.
                if($idstr =~ /^VOICE/) {
                    $idnum = $voiceid++;
                }
                else {
                    $idnum = $idcount++;
                }

                $id{$idstr} = $idnum;
                $idnum[$idnum]=$idstr;

                $source{$idstr}=$src;
                $dest{$idstr}=$dest;
                $voice{$idstr}=$voice;

                if($verbose) {
                    print "id: $phrase{id} ($idnum)\n";
                    print "source: $src\n";
                    print "dest: $dest\n";
                    print "voice: $voice\n";
                }

                undef $src;
                undef $dest;
                undef $voice;
                undef %phrase;

                if($update) {
                    my $e = $english{$idstr};

                    if($e) {
                        # compare original english with this!
                        my @eng = split("\n", $english{$idstr});

                        compare(\@eng, \@phrase);
                    }
                    else {
                        print "### This phrase is not used, remove it!\n";
                    }
                }
                undef @phrase;

            } # end of </phrase>

            # starts with a slash, this _ends_ this section
            $m = pop @m; # get back old value, the previous level's tag
            if($update) {
                print "$ll";
            }
            next;
        } # end of tag close

        # This is an opening (sub) tag

        push @m, $m; # store old value
        $m = $1;
        if($update) {
            print "$ll";
        }
        next;
    }

    if(/^ *([^:]+): *(.*)/) {
        my ($name, $val)=($1, $2);
        &$m($_, $name, $val);
    }
    if($update) {
        print "$ll";
    }
    
}
close(LANG);

if($prefix) {
    # We create a .c and .h file

    open(HFILE, ">$prefix.h");
    open(CFILE, ">$prefix.c");

    print HFILE <<MOO
/* This file was automatically generated using genlang2 */
/*
 * The str() macro/functions is how to access strings that might be
 * translated. Use it like str(MACRO) and expect a string to be
 * returned!
 */
#define str(x) language_strings[x]

/* this is the array for holding the string pointers.
   It will be initialized at runtime. */
extern unsigned char *language_strings[];
/* this contains the concatenation of all strings, separated by \\0 chars */
extern const unsigned char language_builtin[];

/* The enum below contains all available strings */
enum {
MOO
    ;

print CFILE <<MOO
/* This file was automaticly generated using genlang2, the strings come
   from "$input" */
   
#include "$prefix.h"

unsigned char *language_strings[LANG_LAST_INDEX_IN_ARRAY];
const unsigned char language_builtin[] =
MOO
    ;

 # Output the ID names for the enum in the header file
my $i;
for $i (1 .. $idcount) {
    my $name=$idnum[$i - 1]; # get the ID name

    $name =~ s/\"//g; # cut off the quotes

    printf HFILE ("    %s,\n", $name);
}

# Output separation marker for last string ID and the upcoming voice IDs

print HFILE <<MOO
    LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */
    /* --- below this follows voice-only strings --- */
    VOICEONLY_DELIMITER = 0x8000,
MOO
    ;

# Output the ID names for the enum in the header file
my $i;
for $i (0x8000 .. ($voiceid-1)) {
    my $name=$idnum[$i]; # get the ID name

    $name =~ s/\"//g; # cut off the quotes

    printf HFILE ("    %s,\n", $name);
}

# Output end of enum
    print HFILE "\n};\n/* end of generated enum list */\n";

    # Output the target phrases for the source file
    for $i (1 .. $idcount) {
        my $name=$idnum[$i - 1]; # get the ID
        my $dest = $dest{$name}; # get the destination phrase
        
        $dest =~ s:\"$:\\0\":; # insert a \0 before the second quote

        printf CFILE ("    %s\n", $dest);
    }

# Output end of string chunk
print CFILE <<MOO
;
/* end of generated string list */
MOO
    ;

    close(HFILE);
    close(CFILE);
} # end of the c/h file generation
elsif($binary) {
    # Creation of a binary lang file was requested

    # We must first scan the english file to get the correct order of the id
    # numbers used there, as that is what sets the id order for all language
    # files. The english file is scanned before the translated file was
    # scanned.

    open(OUTF, ">$binary") or die "Can't create $binary";
    binmode OUTF;
    printf OUTF ("\x1a%c", $langversion); # magic lang file header

    # loop over the target phrases
    for $i (1 .. $idcount) {
        my $name=$idnum[$i - 1]; # get the ID
        my $dest = $dest{$name}; # get the destination phrase

        if($dest) {
            $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes

            # Now, make sure we get the number from the english sort order:
            $idnum = $idmap{$name};

            printf OUTF ("%c%c%s\x00", ($idnum>>8), ($idnum&0xff), $dest);
            if($debug) {
                printf("%02x => %s\n", $idnum, $value);
            }
        }
    }
}


if($verbose) {
    printf("%d ID strings scanned\n", $idcount);

    print "* head *\n";
    for(keys %head) {
        printf "$_: %s\n", $head{$_};
    }
}

#print "* phrase *\n";
#for(keys %phrase) {
#    print "$_\n";
#}