summaryrefslogtreecommitdiff
path: root/Master/tlpkg/bin/cmp-textfiles
blob: 053020c2264712187e1218fa4c4fa7a60f1d0767 (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
#!/usr/bin/env perl
# $Id$
# Public domain.  Originally written 2008, Karl Berry.
# Compare two files considering CR, LF, and CRLF as equivalent,
# ignoring blank lines,
# and ignoring %% lines (see below).
# 
# Used in place and tlpkg-ctan-check in TeX Live.

exit (&main ());

sub main {
  if (@ARGV != 2) {
    warn <<END_USAGE;
Usage: $0 FILE1 FILE2.
Compare as text files, ignoring line endings and %% lines.
Exit status is zero if the same, 1 if different, something else if trouble.
END_USAGE
    exit $ARGV[0] eq "--help" ? 0 : 2;
  }
  
  my $file1 = &read_file ($ARGV[0]);
  my $file2 = &read_file ($ARGV[1]);
  
  return $file1 eq $file2 ? 0 : 1;
}


# Return contents of FNAME as a string, converting all of CR, LF, and
# CRLF to just LF.
# 
# Also, annoyingly, ignore lines consisting only of "%%".  For an
# unknown reason, derived files on CTAN often contain these lines, while
# the same files regenerated by us do not.  CTAN's general policy is not
# to hold derived files, but there are too many exceptions and it is not
# worth the time to continually contact CTAN and authors.
# 
sub read_file {
  my ($fname) = @_;
  my $ret = "";
  
  open (my $FILE, $fname) || die "open($fname) failed: $!";
  while (<$FILE>) {
    s/\r\n?/\n/g;
    next if /^\s*%%\s*$/; # ignore %% lines, see above.
    #warn "line is |$_|";
    $ret .= $_;
  }
  close ($FILE) || warn "close($fname) failed: $!";

  # if the file did not have a trailing newline, add one for purposes of
  # comparison, since it can slip in if we edit it, etc.
  $ret .= "\n" if substr ($ret, -1) ne "\n";
  
  return $ret;
}