install-info.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/usr/bin/perl --
  2. # fixme: --dirfile option
  3. # fixme: sort entries
  4. # fixme: send to FSF ?
  5. $version= '0.93.42.2'; # This line modified by Makefile
  6. sub version {
  7. print STDERR <<END;
  8. Debian GNU/Linux install-info $version. Copyright (C) 1994,1995
  9. Ian Jackson. This is free software; see the GNU General Public Licence
  10. version 2 or later for copying conditions. There is NO warranty.
  11. END
  12. }
  13. sub usage {
  14. print STDERR <<END;
  15. usage: install-info [--version] [--help] [--debug] [--maxwidth=nnn]
  16. [--section regexp title] [--infodir=xxx] [--align=nnn]
  17. [--calign=nnn] [--quiet] [--menuentry=xxx] [--keep-old]
  18. [--description=xxx] [--test] [--remove] [--] filename
  19. END
  20. }
  21. $infodir='/usr/info';
  22. $maxwidth=79;
  23. $align=27;
  24. $calign=29;
  25. undef $menuentry;
  26. undef $quiet;
  27. undef $nowrite;
  28. undef $keepold;
  29. undef $description;
  30. undef $sectionre;
  31. undef $sectiontitle;
  32. $0 =~ m|[^/]+$|; $name= $&;
  33. while ($ARGV[0] =~ m/^--/) {
  34. $_= shift(@ARGV);
  35. last if $eq eq '--';
  36. if ($_ eq '--version') {
  37. &version; exit 0;
  38. } elsif ($_ eq '--quiet') {
  39. $quiet=1;
  40. } elsif ($_ eq '--test') {
  41. $nowrite=1;
  42. } elsif ($_ eq '--keep-old') {
  43. $keepold=1;
  44. } elsif ($_ eq '--remove') {
  45. $remove=1;
  46. } elsif ($_ eq '--help') {
  47. &usage; exit 0;
  48. } elsif ($_ eq '--debug') {
  49. open(DEBUG,">&STDERR") || exit 1;
  50. } elsif ($_ eq '--section') {
  51. if (@ARGV < 2) {
  52. print STDERR "$name: --section needs two more args\n";
  53. &usage; exit 1;
  54. }
  55. $sectionre= shift(@ARGV);
  56. $sectiontitle= shift(@ARGV);
  57. } elsif (m/^--maxwidth=([0-9]+)$/) {
  58. $maxwidth= $1;
  59. } elsif (m/^--align=([0-9]+)$/) {
  60. $align= $1;
  61. } elsif (m/^--calign=([0-9]+)$/) {
  62. $calign= $1;
  63. } elsif (m/^--infodir=/) {
  64. $infodir=$';
  65. } elsif (m/^--menuentry=/) {
  66. $menuentry=$';
  67. } elsif (m/^--description=/) {
  68. $description=$';
  69. } else {
  70. print STDERR "$name: unknown option \`$_'\n"; &usage; exit 1;
  71. }
  72. }
  73. if (!@ARGV) { &version; print STDERR "\n"; &usage; exit 1; }
  74. $filename= shift(@ARGV);
  75. if (@ARGV) { print STDERR "$name: too many arguments\n"; &usage; exit 1; }
  76. if ($remove) {
  77. print STDERR "$name: --section ignored with --remove\n" if length($sectiontitle);
  78. print STDERR "$name: --description ignored with --remove\n" if length($description);
  79. }
  80. print STDERR "$name: test mode - dir file will not be updated\n"
  81. if $nowrite && !$quiet;
  82. umask(umask(0777) & ~0444);
  83. $filename =~ m|[^/]+$|; $basename= $&; $basename =~ s/(\.info)?(\.gz)?$//;
  84. print DEBUG <<END;
  85. infodir=\`$infodir' filename=\`$filename' maxwidth=\`$maxwidth'
  86. menuentry=\`$menuentry' basename=\`$basename'
  87. description=\`$description' remove=$remove
  88. END
  89. if (!$remove) {
  90. if (!-f $filename && -f "$filename.gz" || $filename =~ s/\.gz$//) {
  91. $filename= "gzip -d <$filename.gz |"; $pipeit= 1;
  92. } else {
  93. $filename= "< $filename";
  94. }
  95. if (!length($description)) {
  96. open(IF,"$filename") || die "$name: read $filename: $!\n";
  97. $asread='';
  98. while(<IF>) { last if m/^START-INFO-DIR-ENTRY$/; }
  99. while(<IF>) { last if m/^END-INFO-DIR-ENTRY$/; $asread.= $_; }
  100. close(IF); &checkpipe;
  101. if ($asread =~ m/(\* *[^:]+: *\([^\)]+\).*\. *.*\n){2,}/) {
  102. $infoentry= $asread; $multiline= 1;
  103. print DEBUG <<END;
  104. multiline \`$asread'
  105. END
  106. } elsif ($asread =~ m/^\* *([^:]+):( *\([^\)]+\)\.|:)\s*/) {
  107. $menuentry= $1; $description= $';
  108. print DEBUG <<END;
  109. infile menuentry \`$menuentry' description \`$description'
  110. END
  111. } elsif (length($asread)) {
  112. print STDERR <<END;
  113. $name: warning, ignoring confusing INFO-DIR-ENTRY in file.
  114. END
  115. }
  116. }
  117. if (length($infoentry)) {
  118. $infoentry =~ m/\n/;
  119. print "$`\n" unless $quiet;
  120. $infoentry =~ m/^\* *([^:]+): *\(([^\)]+)\)/ || die; # internal error
  121. $sortby= $1; $fileinentry= $2;
  122. } else {
  123. if (!length($description)) {
  124. open(IF,"$filename") || die "$name: read $filename: $!\n";
  125. $asread='';
  126. while(<IF>) {
  127. if (m/^\s*[Tt]his file documents/) {
  128. $asread=$';
  129. last;
  130. }
  131. }
  132. if (length($asread)) {
  133. while(<IF>) { last if m/^\s*$/; $asread.= $_; }
  134. $description= $asread;
  135. }
  136. close(IF); &checkpipe;
  137. }
  138. if (!length($description)) {
  139. print STDERR <<END;
  140. No \`START-INFO-DIR-ENTRY' and no \`This file documents'.
  141. $name: unable to determine description for \`dir' entry - giving up
  142. END
  143. exit 1;
  144. }
  145. $description =~ s/^\s*(.)//; $_=$1; y/a-z/A-Z/;
  146. $description= $_ . $description;
  147. if (!length($menuentry)) {
  148. $menuentry= $basename; $menuentry =~ s/\Winfo$//;
  149. $menuentry =~ s/^.//; $_=$&; y/a-z/A-Z/;
  150. $menuentry= $_ . $menuentry;
  151. }
  152. print DEBUG <<END;
  153. menuentry=\`$menuentry' description=\`$description'
  154. END
  155. $cprefix= sprintf("* %s: (%s).", $menuentry, $basename);
  156. $align--; $calign--;
  157. $lprefix= length($cprefix);
  158. if ($lprefix < $align) {
  159. $cprefix .= ' ' x ($align - $lprefix);
  160. $lprefix= $align;
  161. }
  162. $prefix= "\n". (' 'x $calign);
  163. $cwidth= $maxwidth+1;
  164. for $_ (split(/\s+/,$description)) {
  165. $l= length($_);
  166. $cwidth++; $cwidth += $l;
  167. if ($cwidth > $maxwidth) {
  168. $infoentry .= $cprefix;
  169. $cwidth= $lprefix+1+$l;
  170. $cprefix= $prefix; $lprefix= $calign;
  171. }
  172. $infoentry.= ' '; $infoentry .= $_;
  173. }
  174. $infoentry.= "\n";
  175. print $infoentry unless $quiet;
  176. $sortby= $menuentry; $sortby =~ y/A-Z/a-z/;
  177. }
  178. }
  179. if (!link("$infodir/dir","$infodir/dir.lock")) {
  180. die "$name: failed to lock dir for editing! $!\n".
  181. ($! =~ m/exists/i ? "try deleting $infodir/dir.lock ?\n" : '');
  182. }
  183. open(OLD,"$infodir/dir") || &ulquit("$name: open $infodir/dir: $!\n");
  184. @work= <OLD>;
  185. eof(OLD) || &ulquit("$name: read $infodir/dir: $!\n");
  186. close(OLD) || &ulquit("$name: close $infodir/dir after read: $!\n");
  187. while ($work[$#work] !~ m/\S/) { $#work--; }
  188. do {
  189. last if !@work;
  190. $_= shift(@work);
  191. push(@head,$_);
  192. } until (m/^\*\s*Menu:/i);
  193. if (!$remove) {
  194. for ($i=0; $i<=$#work; $i++) {
  195. next unless $work[$i] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  196. last if $1 eq $basename || $1 eq "$basename.info";
  197. }
  198. for ($j=$i; $j<=$#work+1; $j++) {
  199. next if $work[$j] =~ m/^\s+\S/;
  200. last unless $work[$j] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  201. last unless $1 eq $basename || $1 eq "$basename.info";
  202. }
  203. if ($i < $j) {
  204. if ($keepold) {
  205. print "$name: existing entry for \`$basename' not replaced\n" unless $quiet;
  206. $nowrite=1;
  207. } else {
  208. print "$name: replacing existing dir entry for \`$basename'\n" unless $quiet;
  209. }
  210. $mss= $i;
  211. @work= (@work[0..$i-1], @work[$j..$#work]);
  212. } elsif (length($sectionre)) {
  213. $mss= -1;
  214. for ($i=0; $i<=$#work; $i++) {
  215. $_= $work[$i];
  216. next if m/^\*/;
  217. next unless m/$sectionre/io;
  218. $mss= $i+1; last;
  219. }
  220. if ($mss < 0) {
  221. print "$name: creating new section \`$sectiontitle'\n" unless $quiet;
  222. for ($i= $#work; $i>=0 && $work[$i] =~ m/\S/; $i--) { }
  223. if ($i <= 0) { # We ran off the top, make this section and Misc.
  224. print "$name: no sections yet, creating Miscellaneous section too.\n"
  225. unless $quiet;
  226. @work= ("\n", "$sectiontitle\n", "\n", "Miscellaneous:\n", @work);
  227. $mss= 1;
  228. } else {
  229. @work= (@work[0..$i], "$sectiontitle\n", "\n", @work[$i+1..$#work]);
  230. $mss= $i+1;
  231. }
  232. }
  233. while ($mss <= $#work) {
  234. $work[$mss] =~ m/\S/ || last;
  235. $work[$mss] =~ m/^\* *([^:]+):/ || ($mss++, next);
  236. last if $multiline;
  237. $_=$1; y/A-Z/a-z/;
  238. last if $_ gt $sortby;
  239. $mss++;
  240. }
  241. } else {
  242. print "$name: no section specified for new entry, placing at end\n"
  243. unless $quiet;
  244. $mss= $#work+1;
  245. }
  246. @work= (@work[0..$mss-1], $infoentry, @work[$mss..$#work]);
  247. } else {
  248. for ($i=0; $i<=$#work; $i++) {
  249. next unless $work[$i] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  250. $tme= $1; $tfile= $2; $match= $&;
  251. next unless $tfile eq $basename;
  252. last if !length($menuentry);
  253. $tme =~ y/A-Z/a-z/;
  254. last if $tme eq $menuentry;
  255. }
  256. for ($j=$i; $j<=$#work+1; $j++) {
  257. next if $work[$j] =~ m/^\s+\S/;
  258. last unless $work[$j] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  259. $tme= $1; $tfile= $2;
  260. last unless $tfile eq $basename;
  261. next if !length($menuentry);
  262. $tme =~ y/A-Z/a-z/;
  263. last unless $tme eq $menuentry;
  264. }
  265. print DEBUG <<END;
  266. i=$i \$work[\$i]=\`$work[$i]' j=$j \$work[\$j]=\`$work[$j]'
  267. END
  268. if ($i < $j) {
  269. print "$name: deleting entry \`$match ...'\n" unless $quiet;
  270. $_= $work[$i-1];
  271. unless (m/^\s/ || m/^\*/ || m/^$/ ||
  272. $j > $#work || $work[$j] !~ m/^\s*$/) {
  273. s/:?\s+$//;
  274. if ($keepold) {
  275. print "$name: empty section \`$_' not removed\n" unless $quiet;
  276. } else {
  277. $i--; $j++;
  278. print "$name: deleting empty section \`$_'\n" unless $quiet;
  279. }
  280. }
  281. @work= (@work[0..$i-1], @work[$j..$#work]);
  282. } else {
  283. print "$name: no entry for file \`$basename'".
  284. (length($menuentry) ? " and menu entry \`$menuentry'": '').
  285. ".\n"
  286. unless $quiet;
  287. }
  288. }
  289. if (!$nowrite) {
  290. open(NEW,"> $infodir/dir.new") || &ulquit("$name: create $infodir/dir.new: $!\n");
  291. print(NEW @head,@work) || &ulquit("$name: write $infodir/dir.new: $!\n");
  292. close(NEW) || &ulquit("$name: close $infodir/dir.new: $!\n");
  293. unlink("$infodir/dir.old");
  294. link("$infodir/dir","$infodir/dir.old") ||
  295. &ulquit("$name: cannot backup old $infodir/dir, giving up: $!\n");
  296. rename("$infodir/dir.new","$infodir/dir") ||
  297. &ulquit("$name: install new $infodir/dir: $!\n");
  298. }
  299. unlink("$infodir/dir.lock") || die "$name: unlock $infodir/dir: $!\n";
  300. sub ulquit {
  301. unlink("$infodir/dir.lock") ||
  302. warn "$name: warning - unable to unlock $infodir/dir: $!\n";
  303. die $_[0];
  304. }
  305. sub checkpipe {
  306. return if !$pipeit || !$? || $?==0x8D00;
  307. die "$name: read $filename: $?\n";
  308. }
  309. exit 0;