licence.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env perl -w
  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 File::Basename;
  7. # Read the input file.
  8. $infile = "LICENCE";
  9. open my $in, $infile or die "$infile: open: $!\n";
  10. my @lines = ();
  11. while (<$in>) {
  12. chomp;
  13. push @lines, $_;
  14. }
  15. close $in;
  16. # Format into paragraphs.
  17. my @paras = ();
  18. my $para = undef;
  19. for my $line (@lines) {
  20. if ($line eq "") {
  21. $para = undef;
  22. } elsif (!defined $para) {
  23. push @paras, $line;
  24. $para = \$paras[$#paras];
  25. } else {
  26. $$para .= " " . $line;
  27. }
  28. }
  29. # Get the copyright years and short form of copyright holder.
  30. die "bad format of first paragraph\n"
  31. unless $paras[0] =~ m!copyright ([^\.]*)\.!i;
  32. $shortdetails = $1;
  33. # Write out licence.h.
  34. $outfile = "licence.h";
  35. open my $out, ">", $outfile or die "$outfile: open: $!\n";
  36. select $out;
  37. print "/*\n";
  38. print " * $outfile - macro definitions for the PuTTY licence.\n";
  39. print " *\n";
  40. print " * Generated by @{[basename __FILE__]} from $infile.\n";
  41. print " * You should edit those files rather than editing this one.\n";
  42. print " */\n";
  43. print "\n";
  44. print "#define LICENCE_TEXT(parsep) \\\n";
  45. for my $i (0..$#paras) {
  46. my $lit = &stringlit($paras[$i]);
  47. print " parsep \\\n" if $i > 0;
  48. print " \"$lit\"";
  49. print " \\" if $i < $#paras;
  50. print "\n";
  51. }
  52. print "\n";
  53. printf "#define SHORT_COPYRIGHT_DETAILS \"%s\"\n", &stringlit($shortdetails);
  54. sub stringlit {
  55. my ($lit) = @_;
  56. $lit =~ s!\\!\\\\!g;
  57. $lit =~ s!"!\\"!g;
  58. return $lit;
  59. }
  60. close $out;
  61. # Write out doc/licence.but.
  62. $outfile = "doc/licence.but";
  63. open $out, ">", $outfile or die "$outfile: open: $!\n";
  64. select $out;
  65. print "\\# Generated by @{[basename __FILE__]} from $infile.\n";
  66. print "\\# You should edit those files rather than editing this one.\n\n";
  67. print "\\A{licence} PuTTY \\ii{Licence}\n\n";
  68. for my $i (0..$#paras) {
  69. my $para = &halibutescape($paras[$i]);
  70. if ($i == 0) {
  71. $para =~ s!copyright!\\i{copyright}!; # index term in paragraph 1
  72. }
  73. print "$para\n\n";
  74. }
  75. close $out;
  76. # And write out doc/copy.but, which defines a macro used in the manual
  77. # preamble blurb.
  78. $outfile = "doc/copy.but";
  79. open $out, ">", $outfile or die "$outfile: open: $!\n";
  80. select $out;
  81. print "\\# Generated by @{[basename __FILE__]} from $infile.\n";
  82. print "\\# You should edit those files rather than editing this one.\n\n";
  83. printf "\\define{shortcopyrightdetails} %s\n\n",
  84. &halibutescape($shortdetails);
  85. close $out;
  86. sub halibutescape {
  87. my ($text) = @_;
  88. $text =~ s![\\{}]!\\$&!g; # Halibut escaping
  89. $text =~ s!"([^"]*)"!\\q{$1}!g; # convert quoted strings to \q{}
  90. return $text;
  91. }