cleanup-info.pl 4.6 KB

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