cleanup-info.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 $version = '1.1.6'; # This line modified by Makefile
  26. sub version {
  27. print STDERR <<END;
  28. Debian GNU/Linux cleanup-info $version. Copyright (C)1996 Kim-Minh Kaplan.
  29. This is free software; see the GNU General Public Licence
  30. version 2 or later for copying conditions. There is NO warranty.
  31. END
  32. }
  33. sub usage {
  34. print STDERR <<'EOF';
  35. usage: cleanup-info [--version] [--help] [--unsafe] [--] [<dirname>]
  36. Warning: the ``--unsafe'' option may garble an otherwise correct file
  37. EOF
  38. }
  39. my $infodir = '/usr/info';
  40. my $unsafe = 0;
  41. $0 =~ m|[^/]+$|;
  42. my $name= $&;
  43. sub ulquit {
  44. unlink "$infodir/dir.lock"
  45. or warn "$name: warning - unable to unlock $infodir/dir: $!\n";
  46. die $_[0];
  47. }
  48. while (scalar @ARGV > 0 && $ARGV[0] =~ /^--/) {
  49. $_ = shift;
  50. /^--$/ and last;
  51. /^--version$/ and do {
  52. version;
  53. exit 0;
  54. };
  55. /^--help$/ and do {
  56. usage;
  57. exit 0;
  58. };
  59. /^--unsafe$/ and do {
  60. $unsafe=1;
  61. next;
  62. };
  63. print STDERR "$name: unknown option \`$_'\n";
  64. usage;
  65. exit 1;
  66. }
  67. if (scalar @ARGV > 0) {
  68. $infodir = shift;
  69. if (scalar @ARGV > 0) {
  70. print STDERR "$name: too many arguments\n";
  71. usage;
  72. exit 1;
  73. }
  74. }
  75. if (!link "$infodir/dir", "$infodir/dir.lock") {
  76. die "$name: failed to lock dir for editing! $!\n".
  77. ($! =~ /exist/i ? "try deleting $infodir/dir.lock\n" : '');
  78. }
  79. open OLD, "$infodir/dir" or ulquit "$name: can't open $infodir/dir: $!\n";
  80. open OUT, ">$infodir/dir.new"
  81. or ulquit "$name can't create $infodir/dir.new: $!\n";
  82. my (%sections, @section_list, $lastline);
  83. my $section="Miscellaneous"; # default section
  84. my $section_canonic="miscellaneous";
  85. my $waitfor = $unsafe ? '^\*.*:' : '^\*\s*Menu';
  86. while (<OLD>) { # dump the non entries part
  87. last if (/$waitfor/oi);
  88. if (defined $lastline) {
  89. print OUT $lastline
  90. or ulquit "$name: error writing $infodir/dir.new: $!\n";
  91. }
  92. $lastline = $_;
  93. };
  94. if (/^\*\s*Menu\s*:?/i) {
  95. print OUT $lastline if defined $lastline;
  96. print OUT $_;
  97. } else {
  98. print OUT "* Menu:\n";
  99. if (defined $lastline) {
  100. $lastline =~ s/\s*$//;
  101. if ($lastline =~ /^([^\*\s].*)/) { # there was a section title
  102. $section = $1;
  103. $lastline =~ s/\s*:$//;
  104. $section_canonic = lc $lastline;
  105. }
  106. }
  107. push @section_list, $section_canonic;
  108. s/\s*$//;
  109. $sections{$section_canonic} = "\n$section\n$_\n";
  110. }
  111. foreach (<OLD>) { # collect sections
  112. next if (/^\s*$/ or $unsafe and /^\*\s*Menu/oi);
  113. s/\s*$//;
  114. if (/^([^\*\s].*)/) { # change of section
  115. $section = $1;
  116. s/\s*:$//;
  117. $section_canonic = lc $_;
  118. } else { # add to section
  119. if (! exists $sections{$section_canonic}) { # create section header
  120. push @section_list, $section_canonic;
  121. $sections{$section_canonic} = "\n$section\n";
  122. }
  123. $sections{$section_canonic} .= "$_\n";
  124. }
  125. }
  126. eof OLD or ulquit "$name: read $infodir/dir: $!\n";
  127. close OLD or ulquit "$name: close $infodir/dir after read: $!\n";
  128. print OUT @sections{@section_list};
  129. close OUT or ulquit "$name: error closing $infodir/dir.new: $!\n";
  130. # install clean version
  131. unlink "$infodir/dir.old";
  132. link "$infodir/dir", "$infodir/dir.old"
  133. or ulquit "$name: can't backup old $infodir/dir, giving up: $!\n";
  134. rename "$infodir/dir.new", "$infodir/dir"
  135. or ulquit "$name: failed to install $infodir/dir; I'll leave it as $infodir/dir.new: $!\n";
  136. unlink "$infodir/dir.lock"
  137. or die "$name: failed to unlock $infodir/dir: $!\n";
  138. exit 0;