install-info.pl 11 KB

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