]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - licence.pl
first pass
[PuTTY.git] / licence.pl
1 #!/usr/bin/env perl -w
2
3 # This script generates licence.h (containing the PuTTY licence in the
4 # form of macros expanding to C string literals) from the LICENCE
5 # master file. It also regenerates the licence-related Halibut input
6 # files.
7
8 use File::Basename;
9
10 # Read the input file.
11 $infile = "LICENCE";
12 open my $in, $infile or die "$infile: open: $!\n";
13 my @lines = ();
14 while (<$in>) {
15     chomp;
16     push @lines, $_;
17 }
18 close $in;
19
20 # Format into paragraphs.
21 my @paras = ();
22 my $para = undef;
23 for my $line (@lines) {
24     if ($line eq "") {
25         $para = undef;
26     } elsif (!defined $para) {
27         push @paras, $line;
28         $para = \$paras[$#paras];
29     } else {
30         $$para .= " " . $line;
31     }
32 }
33
34 # Get the copyright years and short form of copyright holder.
35 die "bad format of first paragraph\n"
36     unless $paras[0] =~ m!copyright ([^\.]*)\.!i;
37 $shortdetails = $1;
38
39 # Write out licence.h.
40
41 $outfile = "licence.h";
42 open my $out, ">", $outfile or die "$outfile: open: $!\n";
43 select $out;
44
45 print "/*\n";
46 print " * $outfile - macro definitions for the PuTTY licence.\n";
47 print " *\n";
48 print " * Generated by @{[basename __FILE__]} from $infile.\n";
49 print " * You should edit those files rather than editing this one.\n";
50 print " */\n";
51 print "\n";
52
53 print "#define LICENCE_TEXT(parsep) \\\n";
54 for my $i (0..$#paras) {
55     my $lit = &stringlit($paras[$i]);
56     print "    parsep \\\n" if $i > 0;
57     print "    \"$lit\"";
58     print " \\" if $i < $#paras;
59     print "\n";
60 }
61 print "\n";
62
63 printf "#define SHORT_COPYRIGHT_DETAILS \"%s\"\n", &stringlit($shortdetails);
64
65 sub stringlit {
66     my ($lit) = @_;
67     $lit =~ s!\\!\\\\!g;
68     $lit =~ s!"!\\"!g;
69     return $lit;
70 }
71
72 close $out;
73
74 # Write out doc/licence.but.
75
76 $outfile = "doc/licence.but";
77 open $out, ">", $outfile or die "$outfile: open: $!\n";
78 select $out;
79
80 print "\\# Generated by @{[basename __FILE__]} from $infile.\n";
81 print "\\# You should edit those files rather than editing this one.\n\n";
82
83 print "\\A{licence} PuTTY \\ii{Licence}\n\n";
84
85 for my $i (0..$#paras) {
86     my $para = &halibutescape($paras[$i]);
87     if ($i == 0) {
88         $para =~ s!copyright!\\i{copyright}!; # index term in paragraph 1
89     }
90     print "$para\n\n";
91 }
92
93 close $out;
94
95 # And write out doc/copy.but, which defines a macro used in the manual
96 # preamble blurb.
97
98 $outfile = "doc/copy.but";
99 open $out, ">", $outfile or die "$outfile: open: $!\n";
100 select $out;
101
102 print "\\# Generated by @{[basename __FILE__]} from $infile.\n";
103 print "\\# You should edit those files rather than editing this one.\n\n";
104
105 printf "\\define{shortcopyrightdetails} %s\n\n",
106     &halibutescape($shortdetails);
107
108 close $out;
109
110 sub halibutescape {
111     my ($text) = @_;
112     $text =~ s![\\{}]!\\$&!g; # Halibut escaping
113     $text =~ s!"([^"]*)"!\\q{$1}!g; # convert quoted strings to \q{}
114     return $text;
115 }