licence.pl 3.7 KB

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