Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 3d2d3b5

Browse files
author
Fraser J. Gordon
committed
Add a number of perl scripts replacing .rev scripts
1 parent 591f010 commit 3d2d3b5

File tree

6 files changed

+771
-0
lines changed

6 files changed

+771
-0
lines changed

util/compress_data.pl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env perl
2+
3+
use warnings;
4+
5+
# For gzip compression
6+
use IO::Compress::Gzip qw(gzip $GzipError);
7+
8+
# Incoming arguments
9+
my $sourceFile = $ARGV[0];
10+
my $destFile = $ARGV[1];
11+
my $varName = $ARGV[2];
12+
13+
# Read in and compress the source file
14+
my $compressed;
15+
gzip $sourceFile => \$compressed, BinModeIn => 1
16+
or die "Could not compress source data: $GzipError";
17+
18+
# Split into bytes
19+
my @bytes = unpack('C*', $compressed);
20+
21+
# Convert to a list of uint8_t values appropriate for an array in C
22+
my $cArray = "";
23+
my $index;
24+
my $length = scalar @bytes;
25+
for ($index = 0; $index < $length; $index++)
26+
{
27+
$cArray .= sprintf("0x%02x, ", $bytes[$index]);
28+
if ($index % 16 == 15)
29+
{
30+
$cArray .= "\n\t";
31+
}
32+
}
33+
34+
# Tidy up the output
35+
if ($index % 16 == 0)
36+
{
37+
substr($cArray, -4) = "";
38+
}
39+
else
40+
{
41+
substr($cArray, -2) = "";
42+
}
43+
44+
# Write to the output file
45+
open OUTPUT, ">$destFile"
46+
or die "Could not open output file \"$destFile\": $!";
47+
print OUTPUT "unsigned char ${varName}[] = \n{\n\t$cArray\n};\n";
48+
print OUTPUT "unsigned int ${varName}_length = $length;\n";
49+
close OUTPUT;

util/encode_data.pl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env perl
2+
3+
use warnings;
4+
5+
# Incoming arguments
6+
my $sourceFile = $ARGV[0];
7+
my $destFile = $ARGV[1];
8+
my $varName = $ARGV[2];
9+
10+
# Read in the source file
11+
open INPUT, "<$sourceFile"
12+
or die "Could not open source file \"$sourceFile\": $!";
13+
my @lines = <INPUT>;
14+
close INPUT;
15+
my $input = join('', @lines);
16+
17+
# Split into bytes
18+
my @bytes = unpack('C*', $lines);
19+
20+
# Convert to a list of uint8_t values appropriate for an array in C
21+
my $cArray = "";
22+
my $index;
23+
my $length = scalar @bytes;
24+
for ($index = 0; $index < $length; $index++)
25+
{
26+
$cArray .= sprintf("0x%02x, ", $bytes[$index]);
27+
if ($index % 16 == 15)
28+
{
29+
$cArray .= "\n\t";
30+
}
31+
}
32+
33+
# Tidy up the output
34+
if ($index % 16 == 0)
35+
{
36+
substr($cArray, -4) = "";
37+
}
38+
else
39+
{
40+
substr($cArray, -2) = "";
41+
}
42+
43+
# Write to the output file
44+
open OUTPUT, ">$destFile"
45+
or die "Could not open output file \"$destFile\": $!";
46+
print OUTPUT "unsigned char ${varName}[] = \n{\n\t$cArray\n};\n";
47+
print OUTPUT "unsigned int ${varName}_length = $length;\n";
48+
close OUTPUT;

util/encode_errors.pl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env perl
2+
3+
use warnings;
4+
5+
sub generateErrorsList
6+
{
7+
my $sourceFile = $_[0];
8+
my $name = $_[1];
9+
10+
my $array = "const char * ${name} = \n";
11+
12+
open SOURCE, "<$sourceFile"
13+
or die "Could not open \"$sourceFile\": $!";
14+
my @lines = <SOURCE>;
15+
close SOURCE;
16+
17+
my $found = 0;
18+
foreach $line (@lines)
19+
{
20+
# If the first word of the line is "enum" we have found the error list
21+
if ($line =~ /^\s*enum\s/)
22+
{
23+
$found = 1;
24+
}
25+
26+
# Continue reading lines until we get to the enum
27+
if (!$found)
28+
{
29+
next;
30+
}
31+
32+
# End of the enum
33+
if ($line =~ /};/)
34+
{
35+
last;
36+
}
37+
38+
# The comment contains the error message for this error
39+
if ($line =~ m|^\s*//\s*\{|)
40+
{
41+
# Remove the newline character
42+
substr($line, -1) = "";
43+
44+
# Remove the prefix from the error message
45+
$line =~ s|^\s*//\s*\{[^\}]*\}\s*|| ;
46+
47+
# Protect any quotation marks
48+
$line =~ s/\"/\\\"/g ;
49+
50+
# Output the message
51+
# tab & quote & line & "\n" & quote & return
52+
$array .= "\t\"${line}\\n\"\n";
53+
}
54+
}
55+
56+
$array .= ";\n";
57+
return $array;
58+
}
59+
60+
# Need to generate the error lists for both the parse and execution errors
61+
my $path = $ARGV[0];
62+
my $outputFile = $ARGV[1];
63+
64+
my $output = "";
65+
$output .= generateErrorsList("${path}/executionerrors.h", "MCexecutionerrors");
66+
$output .= "\n";
67+
$output .= generateErrorsList("${path}/parseerrors.h", "MCparsingerrors");
68+
69+
# Write out the error lists
70+
open OUTPUT, ">$outputFile"
71+
or die "Could not open output file \"$outputFile\": $!";
72+
print OUTPUT $output;
73+
close OUTPUT;

util/encode_version.pl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env perl
2+
3+
use warnings;
4+
5+
sub trim
6+
{
7+
my $trimmed = $_[0];
8+
$trimmed =~ s/^\s+|\s+$//g ;
9+
return $trimmed;
10+
}
11+
12+
# Open the file containing the variables
13+
open VARIABLES, "<../version"
14+
or die "Could not open version information: $!";
15+
my @variables = <VARIABLES>;
16+
close VARIABLES;
17+
18+
# Open the template header
19+
open TEMPLATE, "<include/revbuild.h.in"
20+
or die "Could not open template header: $!";
21+
my @template = <TEMPLATE>;
22+
close TEMPLATE;
23+
24+
# Flatten the input
25+
my $output = join('', @template);
26+
27+
# Replace each instance of the variable in the template file
28+
foreach $variable (@variables)
29+
{
30+
my @parts = split('=', $variable);
31+
my $varName = trim($parts[0]);
32+
my $varValue = trim($parts[1]);
33+
$output =~ s/\$${varName}/${varValue}/g;
34+
}
35+
36+
# Create the output file
37+
open OUTPUT, ">include/revbuild.h"
38+
or die "Could not open output file: $!";
39+
print OUTPUT $output;
40+
close OUTPUT;

util/hash_strings.pl

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env perl
2+
3+
use warnings;
4+
use File::Basename;
5+
use File::Temp qw(tempfile);
6+
7+
# Arguments
8+
my $sourceFile = $ARGV[0];
9+
my $targetFile = $ARGV[1];
10+
my $perfectCmd = $ARGV[2];
11+
12+
# Get the input
13+
open SOURCE, "<$sourceFile"
14+
or die "Could not open source file \"$sourceFile\": $!";
15+
my @sourceLines = <SOURCE>;
16+
close SOURCE;
17+
18+
# List of tokens
19+
my %tokens = ();
20+
21+
# Look for the lines with the following properties:
22+
# The first token is "{"
23+
# The second token is a C-string
24+
foreach my $line (@sourceLines)
25+
{
26+
# Does the line begin with a { token?
27+
if (!($line =~ s/^\s*\{\s*//g))
28+
{
29+
next;
30+
}
31+
32+
# Is the next token a C-string?
33+
if (substr($line, 0, 1) ne '"')
34+
{
35+
next;
36+
}
37+
38+
# Scan to the end of the string
39+
# NOTE: embedded quotation marks are not handled correctly!
40+
my $end = index($line, '"', 1);
41+
if ($end == -1)
42+
{
43+
next;
44+
}
45+
46+
# Copied over from hash_strings.rev
47+
if (substr($line, 1, 1) eq '\\')
48+
{
49+
next;
50+
}
51+
52+
# Add to the list of tokens
53+
my $token = substr($line, 1, $end-1);
54+
$tokens{$token} = 1;
55+
}
56+
57+
# Write the list of tokens out to a temporary file
58+
($tempFH, $tempName) = tempfile();
59+
($tempFH2, $tempName2) = tempfile();
60+
print $tempFH join("\n", keys %tokens);
61+
close $tempFH;
62+
63+
# Path to the "perfect" executable
64+
my $perfectExe = dirname($0) . "/perfect/$perfectCmd";
65+
66+
# Execute the appropriate "perfect" executable
67+
my $result = system("\"$perfectExe\" <\"$tempName\" >\"$tempName2\"");
68+
die unless ($result == 0);
69+
70+
# Strip of any leading warning message
71+
my @lines = <$tempFH2>;
72+
close $tempFH2;
73+
unlink $tempName;
74+
unlink $tempName2;
75+
if (substr($lines[0], 0, 1) ne '#')
76+
{
77+
splice(@lines, 0, 1);
78+
}
79+
80+
# Write to the output file
81+
open OUTPUT, ">$targetFile"
82+
or die "Couldn't open output file: $1";
83+
print OUTPUT join('', @lines);
84+
close OUTPUT;

0 commit comments

Comments
 (0)