You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_preprocess.pl 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env perl
  2. use warnings FATAL => 'all';
  3. use strict;
  4. use Digest::MD5;
  5. my ( $in_dir, $out_dir ) = @ARGV;
  6. my @files = <$in_dir/*.lua>;
  7. sub quote_file {
  8. my ( $in, $out ) = @_;
  9. while (<$in>) {
  10. if (/^--.USE\s*"(\S+)"$/) {
  11. open( my $inc, '<', "$in_dir/$1.lua.in" )
  12. or die "missing include $1";
  13. quote_file( $inc, $out );
  14. }
  15. else {
  16. s/^\s*//; # remove unnecessary spaces at the beginning
  17. next if /^--/; # skip comments
  18. next if /^\s*$/; # skip empty lines
  19. s/(.)/'$1',/g; # split as 'c',
  20. s/\'\\\'/\'\\\\'/g; # escape backslashes
  21. s/\'\'\'/\'\\\'\'/g; # escape single quotes
  22. print $out "$_'\\n',";
  23. }
  24. }
  25. }
  26. sub digest_for_file {
  27. my ($file) = @_;
  28. open( my $in, '<', $file ) or die "file missing";
  29. my $digest = Digest::MD5->new->addfile($in)->hexdigest;
  30. return $digest;
  31. }
  32. sub changed {
  33. my ( $file, $outfile ) = @_;
  34. open( my $out, '<', $outfile ) or return 1;
  35. my $in_checksum = digest_for_file($file);
  36. my $ln = <$out>;
  37. if ( $ln =~ /^.*id:(\S+)\s.*$/ ) {
  38. if ( $in_checksum ne $1 ) {
  39. return 1;
  40. }
  41. else {
  42. return 0;
  43. }
  44. }
  45. return 1;
  46. }
  47. foreach my $file (@files) {
  48. if ( $file =~ /([^\/.]+)(.lua)$/ ) {
  49. my $fname = "$1$2";
  50. my $varname = "rspamadm_script_$1";
  51. my $definename = uc $varname;
  52. my $outfile = "$out_dir/$fname.h";
  53. if ( changed( $file, $outfile ) ) {
  54. open( my $in, '<', $file ) or die "input missing";
  55. open( my $out, '>', $outfile ) or die "output missing";
  56. my $checksum = digest_for_file($file);
  57. print $out <<EOD;
  58. /* id:$checksum */
  59. #ifndef ${definename}_GUARD_H
  60. #define ${definename}_GUARD_H
  61. static const char ${varname}\[\] = {
  62. EOD
  63. quote_file( $in, $out );
  64. print $out <<EOD;
  65. '\\0'};
  66. #endif
  67. EOD
  68. }
  69. }
  70. }