install-info.pl 14 KB

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