cleanup-info.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/perl --
  2. #
  3. # Clean up the mess that bogus install-info may have done :
  4. #
  5. # - gather all sections with the same heading into a single one.
  6. # Tries to be smart about cases and trailing colon/spaces.
  7. #
  8. # Other clean ups :
  9. #
  10. # - remove empty sections,
  11. # - squeeze blank lines (in entries part only).
  12. #
  13. # Order of sections is preserved (the first encountered section
  14. # counts).
  15. #
  16. # Order of entries within a section is preserved.
  17. #
  18. # BUGS:
  19. #
  20. # Probably many : I just recently learned Perl for this program
  21. # using the man pages. Hopefully this is a short enough program to
  22. # debug.
  23. # don't put that in for production.
  24. # use strict;
  25. my $dpkglibdir = "."; # This line modified by Makefile
  26. push(@INC,$dpkglibdir);
  27. require 'dpkg-gettext.pl';
  28. textdomain("dpkg");
  29. my $version = '1.1.6'; # This line modified by Makefile
  30. sub version {
  31. printf STDERR _g(<<END), $version;
  32. Debian cleanup-info %s. Copyright (C)1996 Kim-Minh Kaplan.
  33. This is free software; see the GNU General Public Licence
  34. version 2 or later for copying conditions. There is NO warranty.
  35. END
  36. }
  37. sub usage {
  38. print STDERR _g(<<'EOF');
  39. usage: cleanup-info [--version] [--help] [--unsafe] [--] [<dirname>]
  40. Warning: the ``--unsafe'' option may garble an otherwise correct file
  41. EOF
  42. }
  43. my $infodir = '/usr/info';
  44. my $unsafe = 0;
  45. $0 =~ m|[^/]+$|;
  46. my $name= $&;
  47. sub ulquit {
  48. unlink "$infodir/dir.lock"
  49. or warn sprintf(_g("%s: warning - unable to unlock %s: %s"),
  50. $name, "$infodir/dir", $!)."\n";
  51. die $_[0];
  52. }
  53. while (scalar @ARGV > 0 && $ARGV[0] =~ /^--/) {
  54. $_ = shift;
  55. /^--$/ and last;
  56. /^--version$/ and do {
  57. version;
  58. exit 0;
  59. };
  60. /^--help$/ and do {
  61. usage;
  62. exit 0;
  63. };
  64. /^--unsafe$/ and do {
  65. $unsafe=1;
  66. next;
  67. };
  68. printf STDERR _g("%s: unknown option \`%s'")."\n", $name, $_;
  69. usage;
  70. exit 1;
  71. }
  72. if (scalar @ARGV > 0) {
  73. $infodir = shift;
  74. if (scalar @ARGV > 0) {
  75. printf STDERR _g("%s: too many arguments")."\n", $name;
  76. usage;
  77. exit 1;
  78. }
  79. }
  80. if (!link "$infodir/dir", "$infodir/dir.lock") {
  81. die sprintf(_g("%s: failed to lock dir for editing! %s"),
  82. $name, $!)."\n".
  83. ($! =~ /exist/i ? sprintf(_g("try deleting %s"),
  84. "$infodir/dir.lock")."\n" : '');
  85. }
  86. open OLD, "$infodir/dir"
  87. or ulquit sprintf(_g("%s: can't open %s: %s"),
  88. $name, "$infodir/dir", $!)."\n";
  89. open OUT, ">$infodir/dir.new"
  90. or ulquit sprintf(_g("%s: can't create %s: %s"),
  91. $name, "$infodir/dir.new", $!)."\n";
  92. my (%sections, @section_list, $lastline);
  93. my $section="Miscellaneous"; # default section
  94. my $section_canonic="miscellaneous";
  95. my $waitfor = $unsafe ? '^\*.*:' : '^\*\s*Menu';
  96. while (<OLD>) { # dump the non entries part
  97. last if (/$waitfor/oi);
  98. if (defined $lastline) {
  99. print OUT $lastline
  100. or ulquit sprintf(_g("%s: error writing %s: %s"),
  101. $name, "$infodir/dir.new", $!)."\n";
  102. }
  103. $lastline = $_;
  104. };
  105. if (/^\*\s*Menu\s*:?/i) {
  106. print OUT $lastline if defined $lastline;
  107. print OUT $_;
  108. } else {
  109. print OUT "* Menu:\n";
  110. if (defined $lastline) {
  111. $lastline =~ s/\s*$//;
  112. if ($lastline =~ /^([^\*\s].*)/) { # there was a section title
  113. $section = $1;
  114. $lastline =~ s/\s*:$//;
  115. $section_canonic = lc $lastline;
  116. }
  117. }
  118. push @section_list, $section_canonic;
  119. s/\s*$//;
  120. $sections{$section_canonic} = "\n$section\n$_\n";
  121. }
  122. foreach (<OLD>) { # collect sections
  123. next if (/^\s*$/ or $unsafe and /^\*\s*Menu/oi);
  124. s/\s*$//;
  125. if (/^([^\*\s].*)/) { # change of section
  126. $section = $1;
  127. s/\s*:$//;
  128. $section_canonic = lc $_;
  129. } else { # add to section
  130. if (! exists $sections{$section_canonic}) { # create section header
  131. push @section_list, $section_canonic;
  132. $sections{$section_canonic} = "\n$section\n";
  133. }
  134. $sections{$section_canonic} .= "$_\n";
  135. }
  136. }
  137. eof OLD or ulquit sprintf(_g("%s: read %s: %s"),
  138. $name, "$infodir/dir", $!)."\n";
  139. close OLD or ulquit sprintf(_g("%s: close %s after read: %s"),
  140. $name, "$infodir/dir", $!)."\n";
  141. print OUT @sections{@section_list};
  142. close OUT or ulquit sprintf(_g("%s: error closing %s: %s"),
  143. $name, "$infodir/dir.new", $!)."\n";
  144. # install clean version
  145. unlink "$infodir/dir.old";
  146. link "$infodir/dir", "$infodir/dir.old"
  147. or ulquit sprintf(_g("%s: can't backup old %s, giving up: %s"),
  148. $name, "$infodir/dir", $!)."\n";
  149. rename "$infodir/dir.new", "$infodir/dir"
  150. or ulquit sprintf(_g("%s: failed to install %s; I'll leave it as %s: %s"),
  151. $name, "$infodir/dir", "$infodir/dir.new", $!)."\n";
  152. unlink "$infodir/dir.lock"
  153. or die sprintf(_g("%s: failed to unlock %s: %s"),
  154. $name, "$infodir/dir", $!)."\n";
  155. exit 0;