forked from rspamd/rspamd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_preprocess.pl
86 lines (70 loc) · 1.71 KB
/
lua_preprocess.pl
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
#!/usr/bin/env perl
use warnings FATAL => 'all';
use strict;
use Digest::MD5;
my ( $in_dir, $out_dir ) = @ARGV;
my @files = <$in_dir/*.lua>;
sub quote_file {
my ( $in, $out ) = @_;
while (<$in>) {
if (/^--.USE\s*"(\S+)"$/) {
open( my $inc, '<', "$in_dir/$1.lua.in" )
or die "missing include $1";
quote_file( $inc, $out );
}
else {
s/^\s*//; # remove unnecessary spaces at the beginning
next if /^--/; # skip comments
next if /^\s*$/; # skip empty lines
s/(.)/'$1',/g; # split as 'c',
s/\'\\\'/\'\\\\'/g; # escape backslashes
s/\'\'\'/\'\\\'\'/g; # escape single quotes
print $out "$_'\\n',";
}
}
}
sub digest_for_file {
my ($file) = @_;
open( my $in, '<', $file ) or die "file missing";
my $digest = Digest::MD5->new->addfile($in)->hexdigest;
return $digest;
}
sub changed {
my ( $file, $outfile ) = @_;
open( my $out, '<', $outfile ) or return 1;
my $in_checksum = digest_for_file($file);
my $ln = <$out>;
if ( $ln =~ /^.*id:(\S+)\s.*$/ ) {
if ( $in_checksum ne $1 ) {
return 1;
}
else {
return 0;
}
}
return 1;
}
foreach my $file (@files) {
if ( $file =~ /([^\/.]+)(.lua)$/ ) {
my $fname = "$1$2";
my $varname = "rspamadm_script_$1";
my $definename = uc $varname;
my $outfile = "$out_dir/$fname.h";
if ( changed( $file, $outfile ) ) {
open( my $in, '<', $file ) or die "input missing";
open( my $out, '>', $outfile ) or die "output missing";
my $checksum = digest_for_file($file);
print $out <<EOD;
/* id:$checksum */
#ifndef ${definename}_GUARD_H
#define ${definename}_GUARD_H
static const char ${varname}\[\] = {
EOD
quote_file( $in, $out );
print $out <<EOD;
'\\0'};
#endif
EOD
}
}
}