install-info.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #!/usr/bin/perl --
  2. use Text::Wrap;
  3. my $dpkglibdir = "."; # This line modified by Makefile
  4. push (@INC, $dpkglibdir);
  5. require 'dpkg-gettext.pl';
  6. textdomain("dpkg");
  7. ($0) = $0 =~ m:.*/(.+):;
  8. # fixme: sort entries
  9. # fixme: send to FSF ?
  10. $version= '0.93.42.2'; # This line modified by Makefile
  11. sub version {
  12. printf _g("Debian %s version %s.\n"), $0, $version;
  13. printf _g("
  14. Copyright (C) 1994,1995 Ian Jackson.");
  15. printf _g("
  16. This is free software; see the GNU General Public Licence version 2 or
  17. later for copying conditions. There is NO warranty.
  18. ");
  19. }
  20. sub usage {
  21. $file = $_[0];
  22. printf $file _g(
  23. "Usage: %s [<options> ...] [--] <filename>
  24. Options:
  25. --section <regexp> <title>
  26. put the new entry in the <regex> matched section
  27. or create a new one with <title> if non-existent.
  28. --menuentry=<text> set the menu entry.
  29. --description=<text> set the description to be used in the menu entry.
  30. --info-file=<path> specify info file to install in the directory.
  31. --dir-file=<path> specify file name of info directory file.
  32. --infodir=<directory> same as '--dir-file=<directory>/dir'.
  33. --info-dir=<directory> likewise.
  34. --keep-old do not replace entries nor remove empty ones.
  35. --remove remove the entry specified by <filename> basename.
  36. --remove-exactly remove the exact <filename> entry.
  37. --test enables test mode (no actions taken).
  38. --debug enables debug mode (show more information).
  39. --quiet do not show output messages.
  40. --help show this help message.
  41. --version show the version.
  42. "), $0;
  43. }
  44. $dirfile = '/usr/share/info/dir';
  45. $maxwidth=79;
  46. $Text::Wrap::columns=$maxwidth;
  47. $backup='/var/backups/infodir.bak';
  48. $default='/usr/share/base-files/info.dir';
  49. $menuentry="";
  50. $description="";
  51. $sectionre="";
  52. $sectiontitle="";
  53. $infoentry="";
  54. $quiet=0;
  55. $nowrite=0;
  56. $keepold=0;
  57. $debug=0;
  58. $remove=0;
  59. my $remove_exactly;
  60. $0 =~ m|[^/]+$|; $name= $&;
  61. while ($ARGV[0] =~ m/^--/) {
  62. $_= shift(@ARGV);
  63. last if $_ eq '--';
  64. if ($_ eq '--version') {
  65. &version(STDOUT); exit 0;
  66. } elsif ($_ eq '--quiet') {
  67. $quiet=1;
  68. } elsif ($_ eq '--test') {
  69. $nowrite=1;
  70. } elsif ($_ eq '--keep-old') {
  71. $keepold=1;
  72. } elsif ($_ eq '--remove') {
  73. $remove=1;
  74. } elsif ($_ eq '--remove-exactly') {
  75. $remove=1;
  76. $remove_exactly=1;
  77. } elsif ($_ eq '--help') {
  78. &usage(STDOUT); exit 0;
  79. } elsif ($_ eq '--version') {
  80. &version; exit 0;
  81. } elsif ($_ eq '--debug') {
  82. open(DEBUG,">&STDERR")
  83. || die sprintf(_g("Could not open stderr for output! %s"), $!)."\n";
  84. $debug=1;
  85. } elsif ($_ eq '--section') {
  86. if (@ARGV < 2) {
  87. printf STDERR _g("%s: --section needs two more args")."\n", $name;
  88. &usage(STDERR); exit 1;
  89. }
  90. $sectionre= shift(@ARGV);
  91. $sectiontitle= shift(@ARGV);
  92. } elsif (m/^--(c?align|maxwidth)=([0-9]+)$/) {
  93. warn( sprintf(_g("%s: %s deprecated(ignored)"), $name, $1)."\n" );
  94. } elsif (m/^--info-?dir=/) {
  95. $dirfile = $' . '/dir';
  96. } elsif (m/^--info-file=/) {
  97. $filename = $';
  98. } elsif (m/^--menuentry=/) {
  99. $menuentry = $';
  100. } elsif (m/^--description=/) {
  101. $description = $';
  102. } elsif (m/^--dir-file=/) { # for compatibility with GNU install-info
  103. $dirfile = $';
  104. } else {
  105. printf STDERR _g("%s: unknown option \`%s'")."\n", $name, $_;
  106. &usage(STDERR); exit 1;
  107. }
  108. }
  109. if (!@ARGV) { &usage(STDERR); exit 1; }
  110. if ( !$filename ) {
  111. $filename= shift(@ARGV);
  112. $name = "$name($filename)";
  113. }
  114. if (@ARGV) { printf STDERR _g("%s: too many arguments")."\n", $name; &usage(STDERR); exit 1; }
  115. if ($remove) {
  116. printf(STDERR _g("%s: --section ignored with --remove")."\n", $name) if length($sectiontitle);
  117. printf(STDERR _g("%s: --description ignored with --remove")."\n", $name) if length($description);
  118. }
  119. printf(STDERR _g("%s: test mode - dir file will not be updated")."\n", $name)
  120. if $nowrite && !$quiet;
  121. umask(umask(0777) & ~0444);
  122. if($remove_exactly) {
  123. $remove_exactly = $filename;
  124. }
  125. $filename =~ m|[^/]+$|; $basename= $&; $basename =~ s/(\.info)?(\.gz)?$//;
  126. # The location of the info files from the dir entry, i.e. (emacs-20/emacs).
  127. my $fileinentry;
  128. &dprint("dirfile='$dirfile' filename='$filename' maxwidth='$maxwidth'");
  129. &dprint("menuentry='$menuentry' basename='$basename'");
  130. &dprint("description='$description' remove=$remove");
  131. if (!$remove) {
  132. if (!-f $filename && -f "$filename.gz" || $filename =~ s/\.gz$//) {
  133. $filename= "gzip -cd <$filename.gz |"; $pipeit= 1;
  134. } else {
  135. $filename= "< $filename";
  136. }
  137. if (!length($description)) {
  138. open(IF,"$filename") || die sprintf(_g("%s: read %s: %s"), $name, $filename, $!)."\n";
  139. $asread='';
  140. while(<IF>) {
  141. m/^START-INFO-DIR-ENTRY$/ && last;
  142. m/^INFO-DIR-SECTION (.+)$/ && do {
  143. $sectiontitle = $1 unless ($sectiontitle);
  144. $sectionre = '^'.quotemeta($1) unless ($sectionre);
  145. }
  146. }
  147. while(<IF>) { last if m/^END-INFO-DIR-ENTRY$/; $asread.= $_; }
  148. if ($pipeit) {
  149. while (<IF>) {};
  150. }
  151. close(IF); &checkpipe;
  152. if ($asread =~ m/(\*\s*[^:]+:\s*\(([^\)]+)\).*\. *.*\n){2}/) {
  153. $infoentry= $asread;
  154. $multiline= 1;
  155. $fileinentry = $2;
  156. &dprint("multiline '$asread'");
  157. } elsif ($asread =~ m/^\*\s*([^:]+):(\s*\(([^\)]+)\)\.|:)\s*/) {
  158. $menuentry= $1;
  159. $description = $';
  160. $fileinentry = $3;
  161. &dprint("infile menuentry '$menuentry' description '$description'");
  162. } elsif (length($asread)) {
  163. printf STDERR _g("%s: warning, ignoring confusing INFO-DIR-ENTRY in file.")."\n", $name;
  164. }
  165. }
  166. if (length($infoentry)) {
  167. $infoentry =~ m/\n/;
  168. print "$`\n" unless $quiet;
  169. $infoentry =~ m/^\*\s*([^:]+):\s*\(([^\)]+)\)/ ||
  170. die sprintf(_g("%s: Invalid info entry"), $name)."\n"; # internal error
  171. $sortby= $1;
  172. $fileinentry= $2;
  173. } else {
  174. if (!length($description)) {
  175. open(IF,"$filename") || die sprintf(_g("%s: read %s: %s"), $name, $filename, $!)."\n";
  176. $asread='';
  177. while(<IF>) {
  178. if (m/^\s*[Tt]his file documents/) {
  179. $asread = $';
  180. last;
  181. }
  182. }
  183. if (length($asread)) {
  184. while(<IF>) { last if m/^\s*$/; $asread.= $_; }
  185. $description= $asread;
  186. }
  187. if ($pipeit) {
  188. while (<IF>) {};
  189. }
  190. close(IF); &checkpipe;
  191. }
  192. if (!length($description)) {
  193. printf STDERR _g("
  194. No \`START-INFO-DIR-ENTRY' and no \`This file documents'.
  195. %s: unable to determine description for \`dir' entry - giving up
  196. "), $name;
  197. exit 1;
  198. }
  199. $description =~ s/^\s*(.)//; $_=$1; y/a-z/A-Z/;
  200. $description= $_ . $description;
  201. if (!length($menuentry)) {
  202. $menuentry= $basename; $menuentry =~ s/\Winfo$//;
  203. $menuentry =~ s/^.//; $_=$&; y/a-z/A-Z/;
  204. $menuentry= $_ . $menuentry;
  205. }
  206. &dprint("menuentry='$menuentry' description='$description'");
  207. if($fileinentry) {
  208. $cprefix= sprintf("* %s: (%s).", $menuentry, $fileinentry);
  209. } else {
  210. $cprefix= sprintf("* %s: (%s).", $menuentry, $basename);
  211. }
  212. $align--; $calign--;
  213. $lprefix= length($cprefix);
  214. if ($lprefix < $align) {
  215. $cprefix .= ' ' x ($align - $lprefix);
  216. $lprefix= $align;
  217. }
  218. $prefix= "\n". (' 'x $calign);
  219. $cwidth= $maxwidth+1;
  220. for $_ (split(/\s+/,$description)) {
  221. $l= length($_);
  222. $cwidth++; $cwidth += $l;
  223. if ($cwidth > $maxwidth) {
  224. $infoentry .= $cprefix;
  225. $cwidth= $lprefix+1+$l;
  226. $cprefix= $prefix; $lprefix= $calign;
  227. }
  228. $infoentry.= ' '; $infoentry .= $_;
  229. }
  230. $infoentry.= "\n";
  231. print $infoentry unless $quiet;
  232. $sortby= $menuentry; $sortby =~ y/A-Z/a-z/;
  233. }
  234. }
  235. if (!$nowrite && ( ! -e $dirfile || ! -s _ )) {
  236. if (-r $backup) {
  237. printf( STDERR _g("%s: no file %s, retrieving backup file %s.")."\n",
  238. $name, $dirfile, "$backup" );
  239. if (system ('cp', $backup, $dirfile)) {
  240. printf( STDERR _g("%s: copying %s to %s failed, giving up: %s")."\n",
  241. $name, $backup, $dirfile, $! );
  242. exit 1;
  243. }
  244. } else {
  245. if (-r $default) {
  246. printf( STDERR _g("%s: no backup file %s available, retrieving default file.")."\n",
  247. $name, $backup );
  248. if (system('cp', $default, $dirfile)) {
  249. printf( STDERR _g("%s: copying %s to %s failed, giving up: %s")."\n",
  250. $name, $default, $dirfile, $! );
  251. exit 1;
  252. }
  253. } else {
  254. printf STDERR _g("%s: no backup file %s available.")."\n", $name, $backup;
  255. printf STDERR _g("%s: no default file %s available, giving up.")."\n", $name, $default;
  256. exit 1;
  257. }
  258. }
  259. }
  260. if (!$nowrite && !link($dirfile, "$dirfile.lock")) {
  261. printf( STDERR _g("%s: failed to lock dir for editing! %s")."\n",
  262. $name, $! );
  263. printf( STDERR _g("try deleting %s?")."\n", "$dirfile.lock")
  264. if $!{EEXIST};
  265. exit 1;
  266. }
  267. open(OLD,$dirfile) || &ulquit(sprintf(_g("open %s: %s"), $dirfile, $!));
  268. @work= <OLD>;
  269. eof(OLD) || &ulquit(sprintf(_g("read %s: %s"), $dirfile, $!));
  270. close(OLD) || &ulquit(sprintf(_g("close %s after read: %s"), $dirfile, $!));
  271. while (($#work >= 0) && ($work[$#work] !~ m/\S/)) { $#work--; }
  272. while (@work) {
  273. $_= shift(@work);
  274. push(@head,$_);
  275. last if (m/^\*\s*Menu:/i);
  276. }
  277. if (!$remove) {
  278. my $target_entry;
  279. if($fileinentry) {
  280. $target_entry = $fileinentry;
  281. } else {
  282. $target_entry = $basename;
  283. }
  284. for ($i=0; $i<=$#work; $i++) {
  285. next unless $work[$i] =~ m/^\*\s*[^:]+:\s*\(([^\)]+)\).*\.\s/;
  286. last if $1 eq $target_entry || $1 eq "$target_entry.info";
  287. }
  288. for ($j=$i; $j<=$#work+1; $j++) {
  289. next if $work[$j] =~ m/^\s+\S/;
  290. last unless $work[$j] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  291. last unless $1 eq $target_entry || $1 eq "$target_entry.info";
  292. }
  293. if ($i < $j) {
  294. if ($keepold) {
  295. printf(_g("%s: existing entry for \`%s' not replaced")."\n", $name, $target_entry) unless $quiet;
  296. $nowrite=1;
  297. } else {
  298. printf(_g("%s: replacing existing dir entry for \`%s'")."\n", $name, $target_entry) unless $quiet;
  299. }
  300. $mss= $i;
  301. @work= (@work[0..$i-1], @work[$j..$#work]);
  302. } elsif (length($sectionre)) {
  303. $mss= -1;
  304. for ($i=0; $i<=$#work; $i++) {
  305. $_= $work[$i];
  306. next if m/^\*/;
  307. next unless m/$sectionre/io;
  308. $mss= $i+1; last;
  309. }
  310. if ($mss < 0) {
  311. printf(_g("%s: creating new section \`%s'")."\n", $name, $sectiontitle) unless $quiet;
  312. for ($i= $#work; $i>=0 && $work[$i] =~ m/\S/; $i--) { }
  313. if ($i <= 0) { # We ran off the top, make this section and Misc.
  314. printf(_g("%s: no sections yet, creating Miscellaneous section too.")."\n", $name)
  315. unless $quiet;
  316. @work= ("\n", "$sectiontitle\n", "\n", "Miscellaneous:\n", @work);
  317. $mss= 1;
  318. } else {
  319. @work= (@work[0..$i], "$sectiontitle\n", "\n", @work[$i+1..$#work]);
  320. $mss= $i+1;
  321. }
  322. }
  323. while ($mss <= $#work) {
  324. $work[$mss] =~ m/\S/ || last;
  325. $work[$mss] =~ m/^\* *([^:]+):/ || ($mss++, next);
  326. last if $multiline;
  327. $_=$1; y/A-Z/a-z/;
  328. last if $_ gt $sortby;
  329. $mss++;
  330. }
  331. } else {
  332. printf(_g("%s: no section specified for new entry, placing at end")."\n", $name)
  333. unless $quiet;
  334. $mss= $#work+1;
  335. }
  336. @work= (@work[0..$mss-1], map("$_\n",split(/\n/,$infoentry)), @work[$mss..$#work]);
  337. } else {
  338. my $target_entry;
  339. if($remove_exactly) {
  340. $target_entry = $remove_exactly;
  341. } else {
  342. $target_entry = $basename;
  343. }
  344. for ($i=0; $i<=$#work; $i++) {
  345. next unless $work[$i] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  346. $tme= $1; $tfile= $2; $match= $&;
  347. next unless $tfile eq $target_entry;
  348. last if !length($menuentry);
  349. $tme =~ y/A-Z/a-z/;
  350. last if $tme eq $menuentry;
  351. }
  352. for ($j=$i; $j<=$#work+1; $j++) {
  353. next if $work[$j] =~ m/^\s+\S/;
  354. last unless $work[$j] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  355. $tme= $1; $tfile= $2;
  356. last unless $tfile eq $target_entry;
  357. next if !length($menuentry);
  358. $tme =~ y/A-Z/a-z/;
  359. last unless $tme eq $menuentry;
  360. }
  361. if ($i < $j) {
  362. &dprint("i=$i \$work[\$i]='$work[$i]' j=$j \$work[\$j]='$work[$j]'");
  363. printf(_g("%s: deleting entry \`%s ...'")."\n", $name, $match) unless $quiet;
  364. $_= $work[$i-1];
  365. unless (m/^\s/ || m/^\*/ || m/^$/ ||
  366. $j > $#work || $work[$j] !~ m/^\s*$/) {
  367. s/:?\s+$//;
  368. if ($keepold) {
  369. printf(_g("%s: empty section \`%s' not removed")."\n", $name, $_) unless $quiet;
  370. } else {
  371. $i--; $j++;
  372. printf(_g("%s: deleting empty section \`%s'")."\n", $name, $_) unless $quiet;
  373. }
  374. }
  375. @work= (@work[0..$i-1], @work[$j..$#work]);
  376. } else {
  377. unless ($quiet) {
  378. if (length($menuentry)) {
  379. printf _g("%s: no entry for file \`%s' and menu entry \`%s'")."\n", $name, $target_entry, $menuentry;
  380. } else {
  381. printf _g("%s: no entry for file \`%s'")."\n", $name, $target_entry;
  382. }
  383. }
  384. }
  385. }
  386. $length = 0;
  387. $j = -1;
  388. for ($i=0; $i<=$#work; $i++) {
  389. $_ = $work[$i];
  390. chomp;
  391. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ) {
  392. $length = length($1) if ( length($1) > $length );
  393. $work[++$j] = $_;
  394. } elsif ( m/^[ \t]+(.*)/ ) {
  395. $work[$j] = "$work[$j] $1";
  396. } else {
  397. $work[++$j] = $_;
  398. }
  399. }
  400. @work = @work[0..$j];
  401. my $descalign=40;
  402. @newwork = ();
  403. foreach ( @work ) {
  404. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ||
  405. m/^([ \t]+)(.*)/ ) {
  406. if (length $1 >= $descalign) {
  407. push @newwork, $1;
  408. $_=(" " x $descalign) . $2;
  409. }
  410. else {
  411. $_ = $1 . (" " x ($descalign - length $1)) . $2;
  412. }
  413. push @newwork, split( "\n", wrap('', " " x $descalign, $_ ) );
  414. } else {
  415. push @newwork, $_;
  416. }
  417. }
  418. if (!$nowrite) {
  419. open(NEW,"> $dirfile.new") || &ulquit(sprintf(_g("create %s: %s"), "$dirfile.new", $!));
  420. print(NEW @head,join("\n",@newwork)) ||
  421. &ulquit(sprintf(_g("write %s: %s"), "$dirfile.new", $!));
  422. close(NEW) || &ulquit(sprintf(_g("close %s: %s"), "$dirfile.new", $!));
  423. unlink("$dirfile.old");
  424. link($dirfile, "$dirfile.old") ||
  425. &ulquit(sprintf(_g("cannot backup old %s, giving up: %s"), $dirfile, $!));
  426. rename("$dirfile.new", $dirfile) ||
  427. &ulquit(sprintf(_g("install new %s: %s"), $dirfile, $!));
  428. unlink("$dirfile.lock") ||
  429. die sprintf(_g("%s: unlock %s: %s"), $name, $dirfile, $!)."\n";
  430. system ('cp', $dirfile, $backup) &&
  431. warn sprintf(_g("%s: couldn't backup %s in %s: %s"), $name, $dirfile, $backup, $!)."\n";
  432. }
  433. sub ulquit {
  434. unlink("$dirfile.lock") ||
  435. warn sprintf(_g("%s: warning - unable to unlock %s: %s"),
  436. $name, $dirfile, $!)."\n";
  437. die "$name: $_[0]\n";
  438. }
  439. sub checkpipe {
  440. return if !$pipeit || !$? || $?==0x8D00 || $?==0x0D;
  441. die sprintf(_g("%s: read %s: %d"), $name, $filename, $?)."\n";
  442. }
  443. sub dprint {
  444. printf(DEBUG _g("dbg: %s")."\n", $_[0]) if ($debug);
  445. }
  446. exit 0;