install-info.pl 13 KB

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