-
Notifications
You must be signed in to change notification settings - Fork 11
/
licence.pl
executable file
·134 lines (115 loc) · 3.71 KB
/
licence.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env perl
# This script generates licence.h (containing the PuTTY licence in the
# form of macros expanding to C string literals) from the LICENCE
# master file. It also regenerates the licence-related Halibut input
# files.
use warnings;
use File::Basename;
use Getopt::Long;
my $usage = "usage: licence.pl (--header|--licencedoc|--copyrightdoc) " .
"[-o OUTFILE]\n";
my $mode = undef;
my $output = undef;
GetOptions("--header" => sub {$mode = "header"},
"--licencedoc" => sub {$mode = "licencedoc"},
"--copyrightdoc" => sub {$mode = "copyrightdoc"},
"o|output=s" => \$output)
and defined $mode
or die $usage;
# Read the input file. We expect to find that alongside this script.
my $infile = (dirname __FILE__) . "/LICENCE";
open my $in, $infile or die "$infile: open: $!\n";
my @lines = ();
while (<$in>) {
y/\r//d;
chomp;
push @lines, $_;
}
close $in;
# Format into paragraphs.
my @paras = ();
my $para = undef;
for my $line (@lines) {
if ($line eq "") {
$para = undef;
} elsif (!defined $para) {
push @paras, $line;
$para = \$paras[$#paras];
} else {
$$para .= " " . $line;
}
}
# Get the copyright years and short form of copyright holder.
die "bad format of first paragraph\n"
unless $paras[0] =~ m!copyright ([^\.]*)\.!i;
$shortdetails = $1;
my $out = "";
if ($mode eq "header") {
$out .= "/*\n";
$out .= " * licence.h - macro definitions for the PuTTY licence.\n";
$out .= " *\n";
$out .= " * Generated by @{[basename __FILE__]} from $infile.\n";
$out .= " * You should edit those files rather than editing this one.\n";
$out .= " */\n";
$out .= "\n";
$out .= "#define LICENCE_TEXT(parsep) \\\n";
for my $i (0..$#paras) {
my $lit = &stringlit($paras[$i]);
$out .= " parsep \\\n" if $i > 0;
$out .= " \"$lit\"";
$out .= " \\" if $i < $#paras;
$out .= "\n";
}
$out .= "\n";
$out .= sprintf "#define SHORT_COPYRIGHT_DETAILS \"%s\"\n",
&stringlit($shortdetails);
} elsif ($mode eq "licencedoc") {
# Write out doc/licence.but.
$out .= "\\# Generated by @{[basename __FILE__]} from $infile.\n";
$out .= "\\# You should edit those files rather than editing this one.\n\n";
$out .= "\\A{licence} PuTTY \\ii{Licence}\n\n";
for my $i (0..$#paras) {
my $para = &halibutescape($paras[$i]);
if ($i == 0) {
$para =~ s!copyright!\\i{copyright}!; # index term in paragraph 1
}
$out .= "$para\n\n";
}
} elsif ($mode eq "copyrightdoc") {
# Write out doc/copy.but, which defines a macro used in the manual
# preamble blurb.
$out .= "\\# Generated by @{[basename __FILE__]} from $infile.\n";
$out .= "\\# You should edit those files rather than editing this one.\n\n";
$out .= sprintf "\\define{shortcopyrightdetails} %s\n\n",
&halibutescape($shortdetails);
}
my $outfile;
my $opened = (defined $output) ?
(open $outfile, ">", $output) : (open $outfile, ">-");
$opened or die "$output: open: $!\n";
print $outfile $out;
close $outfile;
sub stringlit {
my ($lit) = @_;
$lit =~ s!\\!\\\\!g;
$lit =~ s!"!\\"!g;
return $lit;
}
sub halibutescape {
my ($text) = @_;
$text =~ s![\\{}]!\\$&!g; # Halibut escaping
$text =~ s!"([^"]*)"!\\q{$1}!g; # convert quoted strings to \q{}
return $text;
}
sub write {
my ($filename, $newcontents) = @_;
if (open my $fh, "<", $filename) {
my $oldcontents = "";
$oldcontents .= $_ while <$fh>;
close $fh;
return if $oldcontents eq $newcontents;
}
open my $fh, ">", $filename or die "$filename: open: $!\n";
print $fh $newcontents;
close $fh;
}