install-info.pl 13 KB

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