summaryrefslogtreecommitdiffstats
path: root/hcachever.pl
blob: 2ef04d10e751d7f4781038947e92132535c37fba (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
#!/usr/bin/perl -w
#
# Copyright (C) 2020-2021 Kevin J. McCarthy <kevin@8t8.us>
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# This file is a rewrite of hcachever.sh.in in perl.
# The rewrite removes the dependency on mutt_md5, in order to
# improve cross-compilation support.

use strict;
use warnings;
# note Digest::MD5 is standard in perl since 5.8.0 (July 18, 2002)
use Digest::MD5;


sub read_line() {
  my $line;

  while (1) {
    $line = <STDIN>;
    return "" if (!$line);

    chomp($line);
    $line =~ s/^\s+//;
    $line =~ s/\s+$//;
    $line =~ s/\s{2,}//g;

    return $line if ($line ne "");
  }
}


sub process_struct($$) {
  my ($line, $md5) = @_;
  my $struct = "";
  my @body;
  my $bodytxt;
  my $inbody = 0;

  return if $line =~ /;$/;
  if ($line =~ /{$/) {
    $inbody = 1;
  }

  while (($line = read_line()) ne "") {
    if (!$inbody) {
      return if $line =~ /;$/;
      if ($line =~ /{$/) {
        $inbody = 1;
      }
    }

    if ($line =~ /^} (.*);$/) {
      $struct = $1;
      last;
    }
    elsif ($line =~ /^}/) {
      $struct = read_line();
      if ($struct ne "") {
        $struct =~ s/;$//;
      }
      last;
    }
    elsif (($line !~ /^#/) && ($line !~ /^{/)) {
      if ($inbody) {
        push @body, $line;
      }
    }
  }

  if ($struct =~ /^(ADDRESS|LIST|BUFFER|PARAMETER|BODY|ENVELOPE|HEADER|COLOR_ATTR)$/) {
    $bodytxt = join(" ", @body);
    print " * ${struct}: ${bodytxt}\n";

    $md5->add(" ${struct} {${bodytxt}}");
  }
}


my $md5;
my $line;
my $BASEVERSION = "2";

$md5 = Digest::MD5->new;

$md5->add($BASEVERSION);
print "/* base version: $BASEVERSION\n";

while (($line = read_line()) ne "") {
  if ($line =~ /^typedef struct/) {
    process_struct($line, $md5);
  }
}

$md5->add("\n");
my $digest = substr($md5->hexdigest, 0, 8);

print " */\n";
print "#define HCACHEVER 0x${digest}\n";