cleanup-info.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. ($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 $_[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("%s: can't open %s: %s"),
  95. $name, "$infodir/dir", $!)."\n";
  96. open OUT, ">$infodir/dir.new"
  97. or ulquit sprintf(_g("%s: can't create %s: %s"),
  98. $name, "$infodir/dir.new", $!)."\n";
  99. my (%sections, @section_list, $lastline);
  100. my $section="Miscellaneous"; # default section
  101. my $section_canonic="miscellaneous";
  102. my $waitfor = $unsafe ? '^\*.*:' : '^\*\s*Menu';
  103. while (<OLD>) { # dump the non entries part
  104. last if (/$waitfor/oi);
  105. if (defined $lastline) {
  106. print OUT $lastline
  107. or ulquit sprintf(_g("%s: error writing %s: %s"),
  108. $name, "$infodir/dir.new", $!)."\n";
  109. }
  110. $lastline = $_;
  111. };
  112. if (/^\*\s*Menu\s*:?/i) {
  113. print OUT $lastline if defined $lastline;
  114. print OUT $_;
  115. } else {
  116. print OUT "* Menu:\n";
  117. if (defined $lastline) {
  118. $lastline =~ s/\s*$//;
  119. if ($lastline =~ /^([^\*\s].*)/) { # there was a section title
  120. $section = $1;
  121. $lastline =~ s/\s*:$//;
  122. $section_canonic = lc $lastline;
  123. }
  124. }
  125. push @section_list, $section_canonic;
  126. s/\s*$//;
  127. $sections{$section_canonic} = "\n$section\n$_\n";
  128. }
  129. foreach (<OLD>) { # collect sections
  130. next if (/^\s*$/ or $unsafe and /^\*\s*Menu/oi);
  131. s/\s*$//;
  132. if (/^([^\*\s].*)/) { # change of section
  133. $section = $1;
  134. s/\s*:$//;
  135. $section_canonic = lc $_;
  136. } else { # add to section
  137. if (! exists $sections{$section_canonic}) { # create section header
  138. push @section_list, $section_canonic;
  139. $sections{$section_canonic} = "\n$section\n";
  140. }
  141. $sections{$section_canonic} .= "$_\n";
  142. }
  143. }
  144. eof OLD or ulquit sprintf(_g("%s: read %s: %s"),
  145. $name, "$infodir/dir", $!)."\n";
  146. close OLD or ulquit sprintf(_g("%s: close %s after read: %s"),
  147. $name, "$infodir/dir", $!)."\n";
  148. print OUT @sections{@section_list};
  149. close OUT or ulquit sprintf(_g("%s: error closing %s: %s"),
  150. $name, "$infodir/dir.new", $!)."\n";
  151. # install clean version
  152. unlink "$infodir/dir.old";
  153. link "$infodir/dir", "$infodir/dir.old"
  154. or ulquit sprintf(_g("%s: can't backup old %s, giving up: %s"),
  155. $name, "$infodir/dir", $!)."\n";
  156. rename "$infodir/dir.new", "$infodir/dir"
  157. or ulquit sprintf(_g("%s: failed to install %s; I'll leave it as %s: %s"),
  158. $name, "$infodir/dir", "$infodir/dir.new", $!)."\n";
  159. unlink "$infodir/dir.lock"
  160. or die sprintf(_g("%s: failed to unlock %s: %s"),
  161. $name, "$infodir/dir", $!)."\n";
  162. exit 0;