install-info.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #!/usr/bin/perl --
  2. use Text::Wrap;
  3. # fixme: --dirfile option
  4. # fixme: sort entries
  5. # fixme: send to FSF ?
  6. $version= '0.93.42.2'; # This line modified by Makefile
  7. sub version {
  8. $file = $_[0];
  9. print $file <<END;
  10. Debian install-info $version. Copyright (C) 1994,1995
  11. Ian Jackson. This is free software; see the GNU General Public Licence
  12. version 2 or later for copying conditions. There is NO warranty.
  13. END
  14. }
  15. sub usage {
  16. $file = $_[0];
  17. print $file <<END;
  18. usage: install-info [--version] [--help] [--debug] [--maxwidth=nnn]
  19. [--section regexp title] [--infodir=xxx] [--align=nnn]
  20. [--calign=nnn] [--quiet] [--menuentry=xxx] [--info-dir=xxx]
  21. [--keep-old] [--description=xxx] [--test]
  22. [--remove | --remove-exactly ] [--dir-file]
  23. [--]
  24. filename
  25. END
  26. }
  27. $infodir='/usr/share/info';
  28. $maxwidth=79;
  29. $Text::Wrap::columns=$maxwidth;
  30. $backup='/var/backups/infodir.bak';
  31. $default='/usr/share/base-files/info.dir';
  32. $menuentry="";
  33. $description="";
  34. $sectionre="";
  35. $sectiontitle="";
  36. $infoentry="";
  37. $quiet=0;
  38. $nowrite=0;
  39. $keepold=0;
  40. $debug=0;
  41. $remove=0;
  42. my $remove_exactly;
  43. $0 =~ m|[^/]+$|; $name= $&;
  44. while ($ARGV[0] =~ m/^--/) {
  45. $_= shift(@ARGV);
  46. last if $_ eq '--';
  47. if ($_ eq '--version') {
  48. &version(STDOUT); exit 0;
  49. } elsif ($_ eq '--quiet') {
  50. $quiet=1;
  51. } elsif ($_ eq '--test') {
  52. $nowrite=1;
  53. } elsif ($_ eq '--keep-old') {
  54. $keepold=1;
  55. } elsif ($_ eq '--remove') {
  56. $remove=1;
  57. } elsif ($_ eq '--remove-exactly') {
  58. $remove=1;
  59. $remove_exactly=1;
  60. } elsif ($_ eq '--help') {
  61. &usage(STDOUT); exit 0;
  62. } elsif ($_ eq '--debug') {
  63. open(DEBUG,">&STDERR") || die "Could not open stderr for output! $!\n";
  64. $debug=1;
  65. } elsif ($_ eq '--section') {
  66. if (@ARGV < 2) {
  67. print STDERR "$name: --section needs two more args\n";
  68. &usage(STDERR); exit 1;
  69. }
  70. $sectionre= shift(@ARGV);
  71. $sectiontitle= shift(@ARGV);
  72. } elsif (m/^--(c?align|maxwidth)=([0-9]+)$/) {
  73. warn( "$name: $1 deprecated(ignored)\n" );
  74. } elsif (m/^--infodir=/) {
  75. $infodir=$';
  76. } elsif (m/^--info-file=/) {
  77. $filename=$';
  78. } elsif (m/^--menuentry=/) {
  79. $menuentry=$';
  80. } elsif (m/^--info-dir=/) {
  81. $infodir=$';
  82. } elsif (m/^--description=/) {
  83. $description=$';
  84. } elsif (m/^--dir-file=/) { # for compatibility with GNU install-info
  85. $infodir=$';
  86. } else {
  87. print STDERR "$name: unknown option \`$_'\n"; &usage(STDERR); exit 1;
  88. }
  89. }
  90. if (!@ARGV) { &version(STDERR); print STDERR "\n"; &usage(STDERR); exit 1; }
  91. if ( !$filename ) {
  92. $filename= shift(@ARGV);
  93. $name = "$name($filename)";
  94. }
  95. if (@ARGV) { print STDERR "$name: too many arguments\n"; &usage(STDERR); exit 1; }
  96. if ($remove) {
  97. print STDERR "$name: --section ignored with --remove\n" if length($sectiontitle);
  98. print STDERR "$name: --description ignored with --remove\n" if length($description);
  99. }
  100. print STDERR "$name: test mode - dir file will not be updated\n"
  101. if $nowrite && !$quiet;
  102. umask(umask(0777) & ~0444);
  103. if($remove_exactly) {
  104. $remove_exactly = $filename;
  105. }
  106. $filename =~ m|[^/]+$|; $basename= $&; $basename =~ s/(\.info)?(\.gz)?$//;
  107. # The location of the info files from the dir entry, i.e. (emacs-20/emacs).
  108. my $fileinentry;
  109. &dprint("infodir='$infodir' filename='$filename' maxwidth='$maxwidth'\nmenuentry='$menuentry' basename='$basename'\ndescription='$description' remove=$remove");
  110. if (!$remove) {
  111. if (!-f $filename && -f "$filename.gz" || $filename =~ s/\.gz$//) {
  112. $filename= "gzip -cd <$filename.gz |"; $pipeit= 1;
  113. } else {
  114. $filename= "< $filename";
  115. }
  116. if (!length($description)) {
  117. open(IF,"$filename") || die "$name: read $filename: $!\n";
  118. $asread='';
  119. while(<IF>) {
  120. m/^START-INFO-DIR-ENTRY$/ && last;
  121. m/^INFO-DIR-SECTION (.+)$/ && do {
  122. $sectiontitle = $1 unless ($sectiontitle);
  123. $sectionre = '^'.quotemeta($1) unless ($sectionre);
  124. }
  125. }
  126. while(<IF>) { last if m/^END-INFO-DIR-ENTRY$/; $asread.= $_; }
  127. if ($pipeit) {
  128. while (<IF>) {};
  129. }
  130. close(IF); &checkpipe;
  131. if ($asread =~ m/(\*\s*[^:]+:\s*\(([^\)]+)\).*\. *.*\n){2}/) {
  132. $infoentry= $asread;
  133. $multiline= 1;
  134. $fileinentry = $2;
  135. &dprint("multiline '$asread'");
  136. } elsif ($asread =~ m/^\*\s*([^:]+):(\s*\(([^\)]+)\)\.|:)\s*/) {
  137. $menuentry= $1;
  138. $description= $';
  139. $fileinentry = $3;
  140. &dprint("infile menuentry '$menuentry' description '$description'");
  141. } elsif (length($asread)) {
  142. print STDERR <<END;
  143. $name: warning, ignoring confusing INFO-DIR-ENTRY in file.
  144. END
  145. }
  146. }
  147. if (length($infoentry)) {
  148. $infoentry =~ m/\n/;
  149. print "$`\n" unless $quiet;
  150. $infoentry =~ m/^\*\s*([^:]+):\s*\(([^\)]+)\)/ ||
  151. die "$name: Invalid info entry\n"; # internal error
  152. $sortby= $1;
  153. $fileinentry= $2;
  154. } else {
  155. if (!length($description)) {
  156. open(IF,"$filename") || die "$name: read $filename: $!\n";
  157. $asread='';
  158. while(<IF>) {
  159. if (m/^\s*[Tt]his file documents/) {
  160. $asread=$';
  161. last;
  162. }
  163. }
  164. if (length($asread)) {
  165. while(<IF>) { last if m/^\s*$/; $asread.= $_; }
  166. $description= $asread;
  167. }
  168. if ($pipeit) {
  169. while (<IF>) {};
  170. }
  171. close(IF); &checkpipe;
  172. }
  173. if (!length($description)) {
  174. print STDERR "
  175. No \`START-INFO-DIR-ENTRY' and no \`This file documents'.
  176. $name: unable to determine description for \`dir' entry - giving up
  177. ";
  178. exit 1;
  179. }
  180. $description =~ s/^\s*(.)//; $_=$1; y/a-z/A-Z/;
  181. $description= $_ . $description;
  182. if (!length($menuentry)) {
  183. $menuentry= $basename; $menuentry =~ s/\Winfo$//;
  184. $menuentry =~ s/^.//; $_=$&; y/a-z/A-Z/;
  185. $menuentry= $_ . $menuentry;
  186. }
  187. &dprint("menuentry='$menuentry' description='$description'");
  188. if($fileinentry) {
  189. $cprefix= sprintf("* %s: (%s).", $menuentry, $fileinentry);
  190. } else {
  191. $cprefix= sprintf("* %s: (%s).", $menuentry, $basename);
  192. }
  193. $align--; $calign--;
  194. $lprefix= length($cprefix);
  195. if ($lprefix < $align) {
  196. $cprefix .= ' ' x ($align - $lprefix);
  197. $lprefix= $align;
  198. }
  199. $prefix= "\n". (' 'x $calign);
  200. $cwidth= $maxwidth+1;
  201. for $_ (split(/\s+/,$description)) {
  202. $l= length($_);
  203. $cwidth++; $cwidth += $l;
  204. if ($cwidth > $maxwidth) {
  205. $infoentry .= $cprefix;
  206. $cwidth= $lprefix+1+$l;
  207. $cprefix= $prefix; $lprefix= $calign;
  208. }
  209. $infoentry.= ' '; $infoentry .= $_;
  210. }
  211. $infoentry.= "\n";
  212. print $infoentry unless $quiet;
  213. $sortby= $menuentry; $sortby =~ y/A-Z/a-z/;
  214. }
  215. }
  216. if (!$nowrite && ( ! -e "$infodir/dir" || ! -s "$infodir/dir" )) {
  217. if (-r $backup) {
  218. print STDERR "$name: no file $infodir/dir, retrieving backup file $backup.\n";
  219. if (system ("cp $backup $infodir/dir")) {
  220. print STDERR "$name: copying $backup to $infodir/dir failed, giving up: $!\n";
  221. exit 1;
  222. }
  223. } else {
  224. if (-r $default) {
  225. print STDERR "$name: no backup file $backup available, retrieving default file.\n";
  226. if (system("cp $default $infodir/dir")) {
  227. print STDERR "$name: copying $default to $infodir/dir failed, giving up: $!\n";
  228. exit 1;
  229. }
  230. } else {
  231. print STDERR "$name: no backup file $backup available.\n";
  232. print STDERR "$name: no default file $default available, giving up.\n";
  233. exit 1;
  234. }
  235. }
  236. }
  237. if (!$nowrite && !link("$infodir/dir","$infodir/dir.lock")) {
  238. print STDERR "$name: failed to lock dir for editing! $!\n".
  239. ($! =~ m/exists/i ? "try deleting $infodir/dir.lock ?\n" : '');
  240. }
  241. open(OLD,"$infodir/dir") || &ulquit("open $infodir/dir: $!");
  242. @work= <OLD>;
  243. eof(OLD) || &ulquit("read $infodir/dir: $!");
  244. close(OLD) || &ulquit("close $infodir/dir after read: $!");
  245. while (($#work >= 0) && ($work[$#work] !~ m/\S/)) { $#work--; }
  246. while (@work) {
  247. $_= shift(@work);
  248. push(@head,$_);
  249. last if (m/^\*\s*Menu:/i);
  250. }
  251. if (!$remove) {
  252. my $target_entry;
  253. if($fileinentry) {
  254. $target_entry = $fileinentry;
  255. } else {
  256. $target_entry = $basename;
  257. }
  258. for ($i=0; $i<=$#work; $i++) {
  259. next unless $work[$i] =~ m/^\*\s*[^:]+:\s*\(([^\)]+)\).*\.\s/;
  260. last if $1 eq $target_entry || $1 eq "$target_entry.info";
  261. }
  262. for ($j=$i; $j<=$#work+1; $j++) {
  263. next if $work[$j] =~ m/^\s+\S/;
  264. last unless $work[$j] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  265. last unless $1 eq $target_entry || $1 eq "$target_entry.info";
  266. }
  267. if ($i < $j) {
  268. if ($keepold) {
  269. print "$name: existing entry for \`$target_entry' not replaced\n" unless $quiet;
  270. $nowrite=1;
  271. } else {
  272. print "$name: replacing existing dir entry for \`$target_entry'\n" unless $quiet;
  273. }
  274. $mss= $i;
  275. @work= (@work[0..$i-1], @work[$j..$#work]);
  276. } elsif (length($sectionre)) {
  277. $mss= -1;
  278. for ($i=0; $i<=$#work; $i++) {
  279. $_= $work[$i];
  280. next if m/^\*/;
  281. next unless m/$sectionre/io;
  282. $mss= $i+1; last;
  283. }
  284. if ($mss < 0) {
  285. print "$name: creating new section \`$sectiontitle'\n" unless $quiet;
  286. for ($i= $#work; $i>=0 && $work[$i] =~ m/\S/; $i--) { }
  287. if ($i <= 0) { # We ran off the top, make this section and Misc.
  288. print "$name: no sections yet, creating Miscellaneous section too.\n"
  289. unless $quiet;
  290. @work= ("\n", "$sectiontitle\n", "\n", "Miscellaneous:\n", @work);
  291. $mss= 1;
  292. } else {
  293. @work= (@work[0..$i], "$sectiontitle\n", "\n", @work[$i+1..$#work]);
  294. $mss= $i+1;
  295. }
  296. }
  297. while ($mss <= $#work) {
  298. $work[$mss] =~ m/\S/ || last;
  299. $work[$mss] =~ m/^\* *([^:]+):/ || ($mss++, next);
  300. last if $multiline;
  301. $_=$1; y/A-Z/a-z/;
  302. last if $_ gt $sortby;
  303. $mss++;
  304. }
  305. } else {
  306. print "$name: no section specified for new entry, placing at end\n"
  307. unless $quiet;
  308. $mss= $#work+1;
  309. }
  310. @work= (@work[0..$mss-1], map("$_\n",split(/\n/,$infoentry)), @work[$mss..$#work]);
  311. } else {
  312. my $target_entry;
  313. if($remove_exactly) {
  314. $target_entry = $remove_exactly;
  315. } else {
  316. $target_entry = $basename;
  317. }
  318. for ($i=0; $i<=$#work; $i++) {
  319. next unless $work[$i] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  320. $tme= $1; $tfile= $2; $match= $&;
  321. next unless $tfile eq $target_entry;
  322. last if !length($menuentry);
  323. $tme =~ y/A-Z/a-z/;
  324. last if $tme eq $menuentry;
  325. }
  326. for ($j=$i; $j<=$#work+1; $j++) {
  327. next if $work[$j] =~ m/^\s+\S/;
  328. last unless $work[$j] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  329. $tme= $1; $tfile= $2;
  330. last unless $tfile eq $target_entry;
  331. next if !length($menuentry);
  332. $tme =~ y/A-Z/a-z/;
  333. last unless $tme eq $menuentry;
  334. }
  335. if ($i < $j) {
  336. &dprint("i=$i \$work[\$i]='$work[$i]' j=$j \$work[\$j]='$work[$j]'");
  337. print "$name: deleting entry \`$match ...'\n" unless $quiet;
  338. $_= $work[$i-1];
  339. unless (m/^\s/ || m/^\*/ || m/^$/ ||
  340. $j > $#work || $work[$j] !~ m/^\s*$/) {
  341. s/:?\s+$//;
  342. if ($keepold) {
  343. print "$name: empty section \`$_' not removed\n" unless $quiet;
  344. } else {
  345. $i--; $j++;
  346. print "$name: deleting empty section \`$_'\n" unless $quiet;
  347. }
  348. }
  349. @work= (@work[0..$i-1], @work[$j..$#work]);
  350. } else {
  351. print "$name: no entry for file \`$target_entry'".
  352. (length($menuentry) ? " and menu entry \`$menuentry'": '').
  353. ".\n"
  354. unless $quiet;
  355. }
  356. }
  357. $length = 0;
  358. $j = -1;
  359. for ($i=0; $i<=$#work; $i++) {
  360. $_ = $work[$i];
  361. chomp;
  362. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ) {
  363. $length = length($1) if ( length($1) > $length );
  364. $work[++$j] = $_;
  365. } elsif ( m/^[ \t]+(.*)/ ) {
  366. $work[$j] = "$work[$j] $1";
  367. } else {
  368. $work[++$j] = $_;
  369. }
  370. }
  371. @work = @work[0..$j];
  372. my $descalign=40;
  373. @newwork = ();
  374. foreach ( @work ) {
  375. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ||
  376. m/^([ \t]+)(.*)/ ) {
  377. if (length $1 >= $descalign) {
  378. push @newwork, $1;
  379. $_=(" " x $descalign) . $2;
  380. }
  381. else {
  382. $_ = $1 . (" " x ($descalign - length $1)) . $2;
  383. }
  384. push @newwork, split( "\n", wrap('', " " x $descalign, $_ ) );
  385. } else {
  386. push @newwork, $_;
  387. }
  388. }
  389. if (!$nowrite) {
  390. open(NEW,"> $infodir/dir.new") || &ulquit("create $infodir/dir.new: $!");
  391. print(NEW @head,join("\n",@newwork)) || &ulquit("write $infodir/dir.new: $!");
  392. close(NEW) || &ulquit("close $infodir/dir.new: $!");
  393. unlink("$infodir/dir.old");
  394. link("$infodir/dir","$infodir/dir.old") ||
  395. &ulquit("cannot backup old $infodir/dir, giving up: $!");
  396. rename("$infodir/dir.new","$infodir/dir") ||
  397. &ulquit("install new $infodir/dir: $!");
  398. unlink("$infodir/dir.lock") || die "$name: unlock $infodir/dir: $!\n";
  399. system ("cp $infodir/dir $backup") && warn "$name: couldn't backup $infodir/dir in $backup: $!\n";
  400. }
  401. sub ulquit {
  402. unlink("$infodir/dir.lock") ||
  403. warn "$name: warning - unable to unlock $infodir/dir: $!\n";
  404. die "$name: $_[0]\n";
  405. }
  406. sub checkpipe {
  407. return if !$pipeit || !$? || $?==0x8D00 || $?==0x0D;
  408. die "$name: read $filename: $?\n";
  409. }
  410. sub dprint {
  411. print DEBUG "dbg: $_[0]\n" if ($debug);
  412. }
  413. exit 0;