install-info.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/usr/bin/perl --
  2. # fixme: --dirfile option
  3. # fixme: sort entries
  4. # fixme: send to FSF ?
  5. $version= '0.93.42.2'; # This line modified by Makefile
  6. sub version {
  7. print STDERR <<END;
  8. Debian GNU/Linux install-info $version. Copyright (C) 1994,1995
  9. Ian Jackson. This is free software; see the GNU General Public Licence
  10. version 2 or later for copying conditions. There is NO warranty.
  11. END
  12. }
  13. sub usage {
  14. print STDERR <<END;
  15. usage: install-info [--version] [--help] [--debug] [--maxwidth=nnn]
  16. [--section regexp title] [--infodir=xxx] [--align=nnn]
  17. [--calign=nnn] [--quiet] [--menuentry=xxx] [--info-dir=xxx]
  18. [--keep-old] [--description=xxx] [--test] [--remove] [--]
  19. filename
  20. END
  21. }
  22. $infodir='/usr/info';
  23. $maxwidth=79;
  24. $align=27;
  25. $calign=29;
  26. undef $menuentry;
  27. undef $quiet;
  28. undef $nowrite;
  29. undef $keepold;
  30. undef $description;
  31. undef $sectionre;
  32. undef $sectiontitle;
  33. $0 =~ m|[^/]+$|; $name= $&;
  34. while ($ARGV[0] =~ m/^--/) {
  35. $_= shift(@ARGV);
  36. last if $_ eq '--';
  37. if ($_ eq '--version') {
  38. &version; exit 0;
  39. } elsif ($_ eq '--quiet') {
  40. $quiet=1;
  41. } elsif ($_ eq '--test') {
  42. $nowrite=1;
  43. } elsif ($_ eq '--keep-old') {
  44. $keepold=1;
  45. } elsif ($_ eq '--remove') {
  46. $remove=1;
  47. } elsif ($_ eq '--help') {
  48. &usage; exit 0;
  49. } elsif ($_ eq '--debug') {
  50. open(DEBUG,">&STDERR") || exit 1;
  51. } elsif ($_ eq '--section') {
  52. if (@ARGV < 2) {
  53. print STDERR "$name: --section needs two more args\n";
  54. &usage; exit 1;
  55. }
  56. $sectionre= shift(@ARGV);
  57. $sectiontitle= shift(@ARGV);
  58. } elsif (m/^--maxwidth=([0-9]+)$/) {
  59. $maxwidth= $1;
  60. } elsif (m/^--align=([0-9]+)$/) {
  61. $align= $1;
  62. } elsif (m/^--calign=([0-9]+)$/) {
  63. $calign= $1;
  64. } elsif (m/^--infodir=/) {
  65. $infodir=$';
  66. } elsif (m/^--menuentry=/) {
  67. $menuentry=$';
  68. } elsif (m/^--info-dir=/) {
  69. $infodir=$';
  70. } elsif (m/^--description=/) {
  71. $description=$';
  72. } else {
  73. print STDERR "$name: unknown option \`$_'\n"; &usage; exit 1;
  74. }
  75. }
  76. if (!@ARGV) { &version; print STDERR "\n"; &usage; exit 1; }
  77. $filename= shift(@ARGV);
  78. if (@ARGV) { print STDERR "$name: too many arguments\n"; &usage; exit 1; }
  79. if ($remove) {
  80. print STDERR "$name: --section ignored with --remove\n" if length($sectiontitle);
  81. print STDERR "$name: --description ignored with --remove\n" if length($description);
  82. }
  83. print STDERR "$name: test mode - dir file will not be updated\n"
  84. if $nowrite && !$quiet;
  85. umask(umask(0777) & ~0444);
  86. $filename =~ m|[^/]+$|; $basename= $&; $basename =~ s/(\.info)?(\.gz)?$//;
  87. print DEBUG <<END;
  88. infodir=\`$infodir' filename=\`$filename' maxwidth=\`$maxwidth'
  89. menuentry=\`$menuentry' basename=\`$basename'
  90. description=\`$description' remove=$remove
  91. END
  92. if (!$remove) {
  93. if (!-f $filename && -f "$filename.gz" || $filename =~ s/\.gz$//) {
  94. $filename= "gzip -d <$filename.gz |"; $pipeit= 1;
  95. } else {
  96. $filename= "< $filename";
  97. }
  98. if (!length($description)) {
  99. open(IF,"$filename") || die "$name: read $filename: $!\n";
  100. $asread='';
  101. while(<IF>) { last if m/^START-INFO-DIR-ENTRY$/; }
  102. while(<IF>) { last if m/^END-INFO-DIR-ENTRY$/; $asread.= $_; }
  103. close(IF); &checkpipe;
  104. if ($asread =~ m/(\* *[^:]+: *\([^\)]+\).*\. *.*\n){2,}/) {
  105. $infoentry= $asread; $multiline= 1;
  106. print DEBUG <<END;
  107. multiline \`$asread'
  108. END
  109. } elsif ($asread =~ m/^\* *([^:]+):( *\([^\)]+\)\.|:)\s*/) {
  110. $menuentry= $1; $description= $';
  111. print DEBUG <<END;
  112. infile menuentry \`$menuentry' description \`$description'
  113. END
  114. } elsif (length($asread)) {
  115. print STDERR <<END;
  116. $name: warning, ignoring confusing INFO-DIR-ENTRY in file.
  117. END
  118. }
  119. }
  120. if (length($infoentry)) {
  121. $infoentry =~ m/\n/;
  122. print "$`\n" unless $quiet;
  123. $infoentry =~ m/^\* *([^:]+): *\(([^\)]+)\)/ || die; # internal error
  124. $sortby= $1; $fileinentry= $2;
  125. } else {
  126. if (!length($description)) {
  127. open(IF,"$filename") || die "$name: read $filename: $!\n";
  128. $asread='';
  129. while(<IF>) {
  130. if (m/^\s*[Tt]his file documents/) {
  131. $asread=$';
  132. last;
  133. }
  134. }
  135. if (length($asread)) {
  136. while(<IF>) { last if m/^\s*$/; $asread.= $_; }
  137. $description= $asread;
  138. }
  139. close(IF); &checkpipe;
  140. }
  141. if (!length($description)) {
  142. print STDERR <<END;
  143. No \`START-INFO-DIR-ENTRY' and no \`This file documents'.
  144. $name: unable to determine description for \`dir' entry - giving up
  145. END
  146. exit 1;
  147. }
  148. $description =~ s/^\s*(.)//; $_=$1; y/a-z/A-Z/;
  149. $description= $_ . $description;
  150. if (!length($menuentry)) {
  151. $menuentry= $basename; $menuentry =~ s/\Winfo$//;
  152. $menuentry =~ s/^.//; $_=$&; y/a-z/A-Z/;
  153. $menuentry= $_ . $menuentry;
  154. }
  155. print DEBUG <<END;
  156. menuentry=\`$menuentry' description=\`$description'
  157. END
  158. $cprefix= sprintf("* %s: (%s).", $menuentry, $basename);
  159. $align--; $calign--;
  160. $lprefix= length($cprefix);
  161. if ($lprefix < $align) {
  162. $cprefix .= ' ' x ($align - $lprefix);
  163. $lprefix= $align;
  164. }
  165. $prefix= "\n". (' 'x $calign);
  166. $cwidth= $maxwidth+1;
  167. for $_ (split(/\s+/,$description)) {
  168. $l= length($_);
  169. $cwidth++; $cwidth += $l;
  170. if ($cwidth > $maxwidth) {
  171. $infoentry .= $cprefix;
  172. $cwidth= $lprefix+1+$l;
  173. $cprefix= $prefix; $lprefix= $calign;
  174. }
  175. $infoentry.= ' '; $infoentry .= $_;
  176. }
  177. $infoentry.= "\n";
  178. print $infoentry unless $quiet;
  179. $sortby= $menuentry; $sortby =~ y/A-Z/a-z/;
  180. }
  181. }
  182. if (!$nowrite && !link("$infodir/dir","$infodir/dir.lock")) {
  183. die "$name: failed to lock dir for editing! $!\n".
  184. ($! =~ m/exists/i ? "try deleting $infodir/dir.lock ?\n" : '');
  185. }
  186. open(OLD,"$infodir/dir") || &ulquit("$name: open $infodir/dir: $!\n");
  187. @work= <OLD>;
  188. eof(OLD) || &ulquit("$name: read $infodir/dir: $!\n");
  189. close(OLD) || &ulquit("$name: close $infodir/dir after read: $!\n");
  190. while ($work[$#work] !~ m/\S/) { $#work--; }
  191. do {
  192. last if !@work;
  193. $_= shift(@work);
  194. push(@head,$_);
  195. } until (m/^\*\s*Menu:/i);
  196. if (!$remove) {
  197. for ($i=0; $i<=$#work; $i++) {
  198. next unless $work[$i] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  199. last if $1 eq $basename || $1 eq "$basename.info";
  200. }
  201. for ($j=$i; $j<=$#work+1; $j++) {
  202. next if $work[$j] =~ m/^\s+\S/;
  203. last unless $work[$j] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  204. last unless $1 eq $basename || $1 eq "$basename.info";
  205. }
  206. if ($i < $j) {
  207. if ($keepold) {
  208. print "$name: existing entry for \`$basename' not replaced\n" unless $quiet;
  209. $nowrite=1;
  210. } else {
  211. print "$name: replacing existing dir entry for \`$basename'\n" unless $quiet;
  212. }
  213. $mss= $i;
  214. @work= (@work[0..$i-1], @work[$j..$#work]);
  215. } elsif (length($sectionre)) {
  216. $mss= -1;
  217. for ($i=0; $i<=$#work; $i++) {
  218. $_= $work[$i];
  219. next if m/^\*/;
  220. next unless m/$sectionre/io;
  221. $mss= $i+1; last;
  222. }
  223. if ($mss < 0) {
  224. print "$name: creating new section \`$sectiontitle'\n" unless $quiet;
  225. for ($i= $#work; $i>=0 && $work[$i] =~ m/\S/; $i--) { }
  226. if ($i <= 0) { # We ran off the top, make this section and Misc.
  227. print "$name: no sections yet, creating Miscellaneous section too.\n"
  228. unless $quiet;
  229. @work= ("\n", "$sectiontitle\n", "\n", "Miscellaneous:\n", @work);
  230. $mss= 1;
  231. } else {
  232. @work= (@work[0..$i], "$sectiontitle\n", "\n", @work[$i+1..$#work]);
  233. $mss= $i+1;
  234. }
  235. }
  236. while ($mss <= $#work) {
  237. $work[$mss] =~ m/\S/ || last;
  238. $work[$mss] =~ m/^\* *([^:]+):/ || ($mss++, next);
  239. last if $multiline;
  240. $_=$1; y/A-Z/a-z/;
  241. last if $_ gt $sortby;
  242. $mss++;
  243. }
  244. } else {
  245. print "$name: no section specified for new entry, placing at end\n"
  246. unless $quiet;
  247. $mss= $#work+1;
  248. }
  249. @work= (@work[0..$mss-1], $infoentry, @work[$mss..$#work]);
  250. } else {
  251. for ($i=0; $i<=$#work; $i++) {
  252. next unless $work[$i] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  253. $tme= $1; $tfile= $2; $match= $&;
  254. next unless $tfile eq $basename;
  255. last if !length($menuentry);
  256. $tme =~ y/A-Z/a-z/;
  257. last if $tme eq $menuentry;
  258. }
  259. for ($j=$i; $j<=$#work+1; $j++) {
  260. next if $work[$j] =~ m/^\s+\S/;
  261. last unless $work[$j] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  262. $tme= $1; $tfile= $2;
  263. last unless $tfile eq $basename;
  264. next if !length($menuentry);
  265. $tme =~ y/A-Z/a-z/;
  266. last unless $tme eq $menuentry;
  267. }
  268. print DEBUG <<END;
  269. i=$i \$work[\$i]=\`$work[$i]' j=$j \$work[\$j]=\`$work[$j]'
  270. END
  271. if ($i < $j) {
  272. print "$name: deleting entry \`$match ...'\n" unless $quiet;
  273. $_= $work[$i-1];
  274. unless (m/^\s/ || m/^\*/ || m/^$/ ||
  275. $j > $#work || $work[$j] !~ m/^\s*$/) {
  276. s/:?\s+$//;
  277. if ($keepold) {
  278. print "$name: empty section \`$_' not removed\n" unless $quiet;
  279. } else {
  280. $i--; $j++;
  281. print "$name: deleting empty section \`$_'\n" unless $quiet;
  282. }
  283. }
  284. @work= (@work[0..$i-1], @work[$j..$#work]);
  285. } else {
  286. print "$name: no entry for file \`$basename'".
  287. (length($menuentry) ? " and menu entry \`$menuentry'": '').
  288. ".\n"
  289. unless $quiet;
  290. }
  291. }
  292. if (!$nowrite) {
  293. open(NEW,"> $infodir/dir.new") || &ulquit("$name: create $infodir/dir.new: $!\n");
  294. print(NEW @head,@work) || &ulquit("$name: write $infodir/dir.new: $!\n");
  295. close(NEW) || &ulquit("$name: close $infodir/dir.new: $!\n");
  296. unlink("$infodir/dir.old");
  297. link("$infodir/dir","$infodir/dir.old") ||
  298. &ulquit("$name: cannot backup old $infodir/dir, giving up: $!\n");
  299. rename("$infodir/dir.new","$infodir/dir") ||
  300. &ulquit("$name: install new $infodir/dir: $!\n");
  301. unlink("$infodir/dir.lock") || die "$name: unlock $infodir/dir: $!\n";
  302. }
  303. sub ulquit {
  304. unlink("$infodir/dir.lock") ||
  305. warn "$name: warning - unable to unlock $infodir/dir: $!\n";
  306. die $_[0];
  307. }
  308. sub checkpipe {
  309. return if !$pipeit || !$? || $?==0x8D00;
  310. die "$name: read $filename: $?\n";
  311. }
  312. exit 0;