cleanup-info.pl 4.8 KB

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