install-info.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. $cwidth= $maxwidth+1;
  190. for $_ (split(/\s+/,$description)) {
  191. $l= length($_);
  192. $cwidth++; $cwidth += $l;
  193. $infoentry.= ' '; $infoentry .= $_;
  194. }
  195. $infoentry.= "\n";
  196. print $infoentry unless $quiet;
  197. $sortby= $menuentry; $sortby =~ y/A-Z/a-z/;
  198. }
  199. }
  200. if (!$nowrite && ( ! -e "$infodir/dir" || ! -s "$infodir/dir" )) {
  201. if (-r $backup) {
  202. print STDERR "$name: no file $infodir/dir, retrieving backup file $backup.\n";
  203. if (system ("cp $backup $infodir/dir")) {
  204. print STDERR "$name: copying $backup to $infodir/dir failed, giving up: $!\n";
  205. exit 1;
  206. }
  207. } else {
  208. print STDERR "$name: no backup file $backup available, giving up.\n";
  209. exit 1;
  210. }
  211. }
  212. if (!$nowrite && !link("$infodir/dir","$infodir/dir.lock")) {
  213. print STDERR "$name: failed to lock dir for editing! $!\n".
  214. ($! =~ m/exists/i ? "try deleting $infodir/dir.lock ?\n" : '');
  215. }
  216. open(OLD,"$infodir/dir") || &ulquit("open $infodir/dir: $!");
  217. @work= <OLD>;
  218. eof(OLD) || &ulquit("read $infodir/dir: $!");
  219. close(OLD) || &ulquit("close $infodir/dir after read: $!");
  220. while ($work[$#work] !~ m/\S/) { $#work--; }
  221. while (@work) {
  222. $_= shift(@work);
  223. push(@head,$_);
  224. last if (m/^\*\s*Menu:/i);
  225. }
  226. if (!$remove) {
  227. my $target_entry;
  228. if($fileinentry) {
  229. $target_entry = $fileinentry;
  230. } else {
  231. $target_entry = $basename;
  232. }
  233. for ($i=0; $i<=$#work; $i++) {
  234. next unless $work[$i] =~ m/^\*\s*[^:]+:\s*\(([^\)]+)\).*\.\s/;
  235. last if $1 eq $target_entry || $1 eq "$target_entry.info";
  236. }
  237. for ($j=$i; $j<=$#work+1; $j++) {
  238. next if $work[$j] =~ m/^\s+\S/;
  239. last unless $work[$j] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  240. last unless $1 eq $target_entry || $1 eq "$target_entry.info";
  241. }
  242. if ($i < $j) {
  243. if ($keepold) {
  244. print "$name: existing entry for \`$target_entry' not replaced\n" unless $quiet;
  245. $nowrite=1;
  246. } else {
  247. print "$name: replacing existing dir entry for \`$target_entry'\n" unless $quiet;
  248. }
  249. $mss= $i;
  250. @work= (@work[0..$i-1], @work[$j..$#work]);
  251. } elsif (length($sectionre)) {
  252. $mss= -1;
  253. for ($i=0; $i<=$#work; $i++) {
  254. $_= $work[$i];
  255. next if m/^\*/;
  256. next unless m/$sectionre/io;
  257. $mss= $i+1; last;
  258. }
  259. if ($mss < 0) {
  260. print "$name: creating new section \`$sectiontitle'\n" unless $quiet;
  261. for ($i= $#work; $i>=0 && $work[$i] =~ m/\S/; $i--) { }
  262. if ($i <= 0) { # We ran off the top, make this section and Misc.
  263. print "$name: no sections yet, creating Miscellaneous section too.\n"
  264. unless $quiet;
  265. @work= ("\n", "$sectiontitle\n", "\n", "Miscellaneous:\n", @work);
  266. $mss= 1;
  267. } else {
  268. @work= (@work[0..$i], "$sectiontitle\n", "\n", @work[$i+1..$#work]);
  269. $mss= $i+1;
  270. }
  271. }
  272. while ($mss <= $#work) {
  273. $work[$mss] =~ m/\S/ || last;
  274. $work[$mss] =~ m/^\* *([^:]+):/ || ($mss++, next);
  275. last if $multiline;
  276. $_=$1; y/A-Z/a-z/;
  277. last if $_ gt $sortby;
  278. $mss++;
  279. }
  280. } else {
  281. print "$name: no section specified for new entry, placing at end\n"
  282. unless $quiet;
  283. $mss= $#work+1;
  284. }
  285. @work= (@work[0..$mss-1], $infoentry, @work[$mss..$#work]);
  286. } else {
  287. my $target_entry;
  288. if($remove_exactly) {
  289. $target_entry = $remove_exactly;
  290. } else {
  291. $target_entry = $basename;
  292. }
  293. for ($i=0; $i<=$#work; $i++) {
  294. next unless $work[$i] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  295. $tme= $1; $tfile= $2; $match= $&;
  296. next unless $tfile eq $target_entry;
  297. last if !length($menuentry);
  298. $tme =~ y/A-Z/a-z/;
  299. last if $tme eq $menuentry;
  300. }
  301. for ($j=$i; $j<=$#work+1; $j++) {
  302. next if $work[$j] =~ m/^\s+\S/;
  303. last unless $work[$j] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  304. $tme= $1; $tfile= $2;
  305. last unless $tfile eq $target_entry;
  306. next if !length($menuentry);
  307. $tme =~ y/A-Z/a-z/;
  308. last unless $tme eq $menuentry;
  309. }
  310. if ($i < $j) {
  311. &dprint("i=$i \$work[\$i]='$work[$i]' j=$j \$work[\$j]='$work[$j]'");
  312. print "$name: deleting entry \`$match ...'\n" unless $quiet;
  313. $_= $work[$i-1];
  314. unless (m/^\s/ || m/^\*/ || m/^$/ ||
  315. $j > $#work || $work[$j] !~ m/^\s*$/) {
  316. s/:?\s+$//;
  317. if ($keepold) {
  318. print "$name: empty section \`$_' not removed\n" unless $quiet;
  319. } else {
  320. $i--; $j++;
  321. print "$name: deleting empty section \`$_'\n" unless $quiet;
  322. }
  323. }
  324. @work= (@work[0..$i-1], @work[$j..$#work]);
  325. } else {
  326. print "$name: no entry for file \`$target_entry'".
  327. (length($menuentry) ? " and menu entry \`$menuentry'": '').
  328. ".\n"
  329. unless $quiet;
  330. }
  331. }
  332. $length = 0;
  333. $j = -1;
  334. for ($i=0; $i<=$#work; $i++) {
  335. $_ = $work[$i];
  336. chomp;
  337. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ) {
  338. $length = length($1) if ( length($1) > $length );
  339. $work[++$j] = $_;
  340. } elsif ( m/^[ \t]+(.*)/ ) {
  341. $work[$j] = "$work[$j] $1";
  342. } else {
  343. $work[++$j] = $_;
  344. }
  345. }
  346. @work = @work[0..$j];
  347. @newwork = ();
  348. foreach ( @work ) {
  349. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ||
  350. m/^([ \t]+)(.*)/ ) {
  351. $_ = $1 . ( " " x ( $length - length($1) + 1 ) ) . $2;
  352. push @newwork, split( "\n", wrap('', " " x ( $length + 1 ), $_ ) );
  353. } else {
  354. push @newwork, $_;
  355. }
  356. }
  357. if (!$nowrite) {
  358. open(NEW,"> $infodir/dir.new") || &ulquit("create $infodir/dir.new: $!");
  359. print(NEW @head,join("\n",@newwork)) || &ulquit("write $infodir/dir.new: $!");
  360. close(NEW) || &ulquit("close $infodir/dir.new: $!");
  361. unlink("$infodir/dir.old");
  362. link("$infodir/dir","$infodir/dir.old") ||
  363. &ulquit("cannot backup old $infodir/dir, giving up: $!");
  364. rename("$infodir/dir.new","$infodir/dir") ||
  365. &ulquit("install new $infodir/dir: $!");
  366. unlink("$infodir/dir.lock") || die "$name: unlock $infodir/dir: $!\n";
  367. system ("cp $infodir/dir $backup") && warn "$name: couldn't backup $infodir/dir in $backup: $!\n";
  368. }
  369. sub ulquit {
  370. unlink("$infodir/dir.lock") ||
  371. warn "$name: warning - unable to unlock $infodir/dir: $!\n";
  372. die "$name: $_[0]\n";
  373. }
  374. sub checkpipe {
  375. return if !$pipeit || !$? || $?==0x8D00 || $?==0x0D;
  376. die "$name: read $filename: $?\n";
  377. }
  378. sub dprint {
  379. print DEBUG "dbg: $_[0]\n" if ($debug);
  380. }
  381. exit 0;