install-info.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. $infodir=~s/\/dir$//;
  87. } else {
  88. print STDERR "$name: unknown option \`$_'\n"; &usage(STDERR); exit 1;
  89. }
  90. }
  91. if (!@ARGV) { &version(STDERR); print STDERR "\n"; &usage(STDERR); exit 1; }
  92. if ( !$filename ) {
  93. $filename= shift(@ARGV);
  94. $name = "$name($filename)";
  95. }
  96. if (@ARGV) { print STDERR "$name: too many arguments\n"; &usage(STDERR); exit 1; }
  97. if ($remove) {
  98. print STDERR "$name: --section ignored with --remove\n" if length($sectiontitle);
  99. print STDERR "$name: --description ignored with --remove\n" if length($description);
  100. }
  101. print STDERR "$name: test mode - dir file will not be updated\n"
  102. if $nowrite && !$quiet;
  103. umask(umask(0777) & ~0444);
  104. if($remove_exactly) {
  105. $remove_exactly = $filename;
  106. }
  107. $filename =~ m|[^/]+$|; $basename= $&; $basename =~ s/(\.info)?(\.gz)?$//;
  108. # The location of the info files from the dir entry, i.e. (emacs-20/emacs).
  109. my $fileinentry;
  110. &dprint("infodir='$infodir' filename='$filename' maxwidth='$maxwidth'\nmenuentry='$menuentry' basename='$basename'\ndescription='$description' remove=$remove");
  111. if (!$remove) {
  112. if (!-f $filename && -f "$filename.gz" || $filename =~ s/\.gz$//) {
  113. $filename= "gzip -cd <$filename.gz |"; $pipeit= 1;
  114. } else {
  115. $filename= "< $filename";
  116. }
  117. if (!length($description)) {
  118. open(IF,"$filename") || die "$name: read $filename: $!\n";
  119. $asread='';
  120. while(<IF>) {
  121. m/^START-INFO-DIR-ENTRY$/ && last;
  122. m/^INFO-DIR-SECTION (.+)$/ && do {
  123. $sectiontitle = $1 unless ($sectiontitle);
  124. $sectionre = '^'.quotemeta($1) unless ($sectionre);
  125. }
  126. }
  127. while(<IF>) { last if m/^END-INFO-DIR-ENTRY$/; $asread.= $_; }
  128. if ($pipeit) {
  129. while (<IF>) {};
  130. }
  131. close(IF); &checkpipe;
  132. if ($asread =~ m/(\*\s*[^:]+:\s*\(([^\)]+)\).*\. *.*\n){2}/) {
  133. $infoentry= $asread;
  134. $multiline= 1;
  135. $fileinentry = $2;
  136. &dprint("multiline '$asread'");
  137. } elsif ($asread =~ m/^\*\s*([^:]+):(\s*\(([^\)]+)\)\.|:)\s*/) {
  138. $menuentry= $1;
  139. $description= $';
  140. $fileinentry = $3;
  141. &dprint("infile menuentry '$menuentry' description '$description'");
  142. } elsif (length($asread)) {
  143. print STDERR <<END;
  144. $name: warning, ignoring confusing INFO-DIR-ENTRY in file.
  145. END
  146. }
  147. }
  148. if (length($infoentry)) {
  149. $infoentry =~ m/\n/;
  150. print "$`\n" unless $quiet;
  151. $infoentry =~ m/^\*\s*([^:]+):\s*\(([^\)]+)\)/ ||
  152. die "$name: Invalid info entry\n"; # internal error
  153. $sortby= $1;
  154. $fileinentry= $2;
  155. } else {
  156. if (!length($description)) {
  157. open(IF,"$filename") || die "$name: read $filename: $!\n";
  158. $asread='';
  159. while(<IF>) {
  160. if (m/^\s*[Tt]his file documents/) {
  161. $asread=$';
  162. last;
  163. }
  164. }
  165. if (length($asread)) {
  166. while(<IF>) { last if m/^\s*$/; $asread.= $_; }
  167. $description= $asread;
  168. }
  169. if ($pipeit) {
  170. while (<IF>) {};
  171. }
  172. close(IF); &checkpipe;
  173. }
  174. if (!length($description)) {
  175. print STDERR "
  176. No \`START-INFO-DIR-ENTRY' and no \`This file documents'.
  177. $name: unable to determine description for \`dir' entry - giving up
  178. ";
  179. exit 1;
  180. }
  181. $description =~ s/^\s*(.)//; $_=$1; y/a-z/A-Z/;
  182. $description= $_ . $description;
  183. if (!length($menuentry)) {
  184. $menuentry= $basename; $menuentry =~ s/\Winfo$//;
  185. $menuentry =~ s/^.//; $_=$&; y/a-z/A-Z/;
  186. $menuentry= $_ . $menuentry;
  187. }
  188. &dprint("menuentry='$menuentry' description='$description'");
  189. if($fileinentry) {
  190. $cprefix= sprintf("* %s: (%s).", $menuentry, $fileinentry);
  191. } else {
  192. $cprefix= sprintf("* %s: (%s).", $menuentry, $basename);
  193. }
  194. $align--; $calign--;
  195. $lprefix= length($cprefix);
  196. if ($lprefix < $align) {
  197. $cprefix .= ' ' x ($align - $lprefix);
  198. $lprefix= $align;
  199. }
  200. $prefix= "\n". (' 'x $calign);
  201. $cwidth= $maxwidth+1;
  202. for $_ (split(/\s+/,$description)) {
  203. $l= length($_);
  204. $cwidth++; $cwidth += $l;
  205. if ($cwidth > $maxwidth) {
  206. $infoentry .= $cprefix;
  207. $cwidth= $lprefix+1+$l;
  208. $cprefix= $prefix; $lprefix= $calign;
  209. }
  210. $infoentry.= ' '; $infoentry .= $_;
  211. }
  212. $infoentry.= "\n";
  213. print $infoentry unless $quiet;
  214. $sortby= $menuentry; $sortby =~ y/A-Z/a-z/;
  215. }
  216. }
  217. if (!$nowrite && ( ! -e "$infodir/dir" || ! -s "$infodir/dir" )) {
  218. if (-r $backup) {
  219. print STDERR "$name: no file $infodir/dir, retrieving backup file $backup.\n";
  220. if (system ("cp $backup $infodir/dir")) {
  221. print STDERR "$name: copying $backup to $infodir/dir failed, giving up: $!\n";
  222. exit 1;
  223. }
  224. } else {
  225. if (-r $default) {
  226. print STDERR "$name: no backup file $backup available, retrieving default file.\n";
  227. if (system("cp $default $infodir/dir")) {
  228. print STDERR "$name: copying $default to $infodir/dir failed, giving up: $!\n";
  229. exit 1;
  230. }
  231. } else {
  232. print STDERR "$name: no backup file $backup available.\n";
  233. print STDERR "$name: no default file $default available, giving up.\n";
  234. exit 1;
  235. }
  236. }
  237. }
  238. if (!$nowrite && !link("$infodir/dir","$infodir/dir.lock")) {
  239. print STDERR "$name: failed to lock dir for editing! $!\n".
  240. ($! =~ m/exists/i ? "try deleting $infodir/dir.lock ?\n" : '');
  241. }
  242. open(OLD,"$infodir/dir") || &ulquit("open $infodir/dir: $!");
  243. @work= <OLD>;
  244. eof(OLD) || &ulquit("read $infodir/dir: $!");
  245. close(OLD) || &ulquit("close $infodir/dir after read: $!");
  246. while (($#work >= 0) && ($work[$#work] !~ m/\S/)) { $#work--; }
  247. while (@work) {
  248. $_= shift(@work);
  249. push(@head,$_);
  250. last if (m/^\*\s*Menu:/i);
  251. }
  252. if (!$remove) {
  253. my $target_entry;
  254. if($fileinentry) {
  255. $target_entry = $fileinentry;
  256. } else {
  257. $target_entry = $basename;
  258. }
  259. for ($i=0; $i<=$#work; $i++) {
  260. next unless $work[$i] =~ m/^\*\s*[^:]+:\s*\(([^\)]+)\).*\.\s/;
  261. last if $1 eq $target_entry || $1 eq "$target_entry.info";
  262. }
  263. for ($j=$i; $j<=$#work+1; $j++) {
  264. next if $work[$j] =~ m/^\s+\S/;
  265. last unless $work[$j] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  266. last unless $1 eq $target_entry || $1 eq "$target_entry.info";
  267. }
  268. if ($i < $j) {
  269. if ($keepold) {
  270. print "$name: existing entry for \`$target_entry' not replaced\n" unless $quiet;
  271. $nowrite=1;
  272. } else {
  273. print "$name: replacing existing dir entry for \`$target_entry'\n" unless $quiet;
  274. }
  275. $mss= $i;
  276. @work= (@work[0..$i-1], @work[$j..$#work]);
  277. } elsif (length($sectionre)) {
  278. $mss= -1;
  279. for ($i=0; $i<=$#work; $i++) {
  280. $_= $work[$i];
  281. next if m/^\*/;
  282. next unless m/$sectionre/io;
  283. $mss= $i+1; last;
  284. }
  285. if ($mss < 0) {
  286. print "$name: creating new section \`$sectiontitle'\n" unless $quiet;
  287. for ($i= $#work; $i>=0 && $work[$i] =~ m/\S/; $i--) { }
  288. if ($i <= 0) { # We ran off the top, make this section and Misc.
  289. print "$name: no sections yet, creating Miscellaneous section too.\n"
  290. unless $quiet;
  291. @work= ("\n", "$sectiontitle\n", "\n", "Miscellaneous:\n", @work);
  292. $mss= 1;
  293. } else {
  294. @work= (@work[0..$i], "$sectiontitle\n", "\n", @work[$i+1..$#work]);
  295. $mss= $i+1;
  296. }
  297. }
  298. while ($mss <= $#work) {
  299. $work[$mss] =~ m/\S/ || last;
  300. $work[$mss] =~ m/^\* *([^:]+):/ || ($mss++, next);
  301. last if $multiline;
  302. $_=$1; y/A-Z/a-z/;
  303. last if $_ gt $sortby;
  304. $mss++;
  305. }
  306. } else {
  307. print "$name: no section specified for new entry, placing at end\n"
  308. unless $quiet;
  309. $mss= $#work+1;
  310. }
  311. @work= (@work[0..$mss-1], map("$_\n",split(/\n/,$infoentry)), @work[$mss..$#work]);
  312. } else {
  313. my $target_entry;
  314. if($remove_exactly) {
  315. $target_entry = $remove_exactly;
  316. } else {
  317. $target_entry = $basename;
  318. }
  319. for ($i=0; $i<=$#work; $i++) {
  320. next unless $work[$i] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  321. $tme= $1; $tfile= $2; $match= $&;
  322. next unless $tfile eq $target_entry;
  323. last if !length($menuentry);
  324. $tme =~ y/A-Z/a-z/;
  325. last if $tme eq $menuentry;
  326. }
  327. for ($j=$i; $j<=$#work+1; $j++) {
  328. next if $work[$j] =~ m/^\s+\S/;
  329. last unless $work[$j] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  330. $tme= $1; $tfile= $2;
  331. last unless $tfile eq $target_entry;
  332. next if !length($menuentry);
  333. $tme =~ y/A-Z/a-z/;
  334. last unless $tme eq $menuentry;
  335. }
  336. if ($i < $j) {
  337. &dprint("i=$i \$work[\$i]='$work[$i]' j=$j \$work[\$j]='$work[$j]'");
  338. print "$name: deleting entry \`$match ...'\n" unless $quiet;
  339. $_= $work[$i-1];
  340. unless (m/^\s/ || m/^\*/ || m/^$/ ||
  341. $j > $#work || $work[$j] !~ m/^\s*$/) {
  342. s/:?\s+$//;
  343. if ($keepold) {
  344. print "$name: empty section \`$_' not removed\n" unless $quiet;
  345. } else {
  346. $i--; $j++;
  347. print "$name: deleting empty section \`$_'\n" unless $quiet;
  348. }
  349. }
  350. @work= (@work[0..$i-1], @work[$j..$#work]);
  351. } else {
  352. print "$name: no entry for file \`$target_entry'".
  353. (length($menuentry) ? " and menu entry \`$menuentry'": '').
  354. ".\n"
  355. unless $quiet;
  356. }
  357. }
  358. $length = 0;
  359. $j = -1;
  360. for ($i=0; $i<=$#work; $i++) {
  361. $_ = $work[$i];
  362. chomp;
  363. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ) {
  364. $length = length($1) if ( length($1) > $length );
  365. $work[++$j] = $_;
  366. } elsif ( m/^[ \t]+(.*)/ ) {
  367. $work[$j] = "$work[$j] $1";
  368. } else {
  369. $work[++$j] = $_;
  370. }
  371. }
  372. @work = @work[0..$j];
  373. my $descalign=40;
  374. @newwork = ();
  375. foreach ( @work ) {
  376. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ||
  377. m/^([ \t]+)(.*)/ ) {
  378. if (length $1 >= $descalign) {
  379. push @newwork, $1;
  380. $_=(" " x $descalign) . $2;
  381. }
  382. else {
  383. $_ = $1 . (" " x ($descalign - length $1)) . $2;
  384. }
  385. push @newwork, split( "\n", wrap('', " " x $descalign, $_ ) );
  386. } else {
  387. push @newwork, $_;
  388. }
  389. }
  390. if (!$nowrite) {
  391. open(NEW,"> $infodir/dir.new") || &ulquit("create $infodir/dir.new: $!");
  392. print(NEW @head,join("\n",@newwork)) || &ulquit("write $infodir/dir.new: $!");
  393. close(NEW) || &ulquit("close $infodir/dir.new: $!");
  394. unlink("$infodir/dir.old");
  395. link("$infodir/dir","$infodir/dir.old") ||
  396. &ulquit("cannot backup old $infodir/dir, giving up: $!");
  397. rename("$infodir/dir.new","$infodir/dir") ||
  398. &ulquit("install new $infodir/dir: $!");
  399. unlink("$infodir/dir.lock") || die "$name: unlock $infodir/dir: $!\n";
  400. system ("cp $infodir/dir $backup") && warn "$name: couldn't backup $infodir/dir in $backup: $!\n";
  401. }
  402. sub ulquit {
  403. unlink("$infodir/dir.lock") ||
  404. warn "$name: warning - unable to unlock $infodir/dir: $!\n";
  405. die "$name: $_[0]\n";
  406. }
  407. sub checkpipe {
  408. return if !$pipeit || !$? || $?==0x8D00 || $?==0x0D;
  409. die "$name: read $filename: $?\n";
  410. }
  411. sub dprint {
  412. print DEBUG "dbg: $_[0]\n" if ($debug);
  413. }
  414. exit 0;