install-info.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. || &quit(sprintf(_g("could not open stderr for output! %s"), $!));
  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: option --%s is 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") || &quit(sprintf(_g("unable to read %s: %s"), $filename, $!));
  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. &quit(_g("invalid info entry")); # internal error
  171. $sortby= $1;
  172. $fileinentry= $2;
  173. } else {
  174. if (!length($description)) {
  175. open(IF,"$filename") || &quit(_g("unable to read %s: %s"), $filename, $!);
  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("unable to open %s: %s"), $dirfile, $!));
  268. @work= <OLD>;
  269. eof(OLD) || &ulquit(sprintf(_g("unable to read %s: %s"), $dirfile, $!));
  270. close(OLD) || &ulquit(sprintf(_g("unable to close %s after read: %s"),
  271. $dirfile, $!));
  272. while (($#work >= 0) && ($work[$#work] !~ m/\S/)) { $#work--; }
  273. while (@work) {
  274. $_= shift(@work);
  275. push(@head,$_);
  276. last if (m/^\*\s*Menu:/i);
  277. }
  278. if (!$remove) {
  279. my $target_entry;
  280. if($fileinentry) {
  281. $target_entry = $fileinentry;
  282. } else {
  283. $target_entry = $basename;
  284. }
  285. for ($i=0; $i<=$#work; $i++) {
  286. next unless $work[$i] =~ m/^\*\s*[^:]+:\s*\(([^\)]+)\).*\.\s/;
  287. last if $1 eq $target_entry || $1 eq "$target_entry.info";
  288. }
  289. for ($j=$i; $j<=$#work+1; $j++) {
  290. next if $work[$j] =~ m/^\s+\S/;
  291. last unless $work[$j] =~ m/^\* *[^:]+: *\(([^\)]+)\).*\.\s/;
  292. last unless $1 eq $target_entry || $1 eq "$target_entry.info";
  293. }
  294. if ($i < $j) {
  295. if ($keepold) {
  296. printf(_g("%s: existing entry for \`%s' not replaced")."\n", $name, $target_entry) unless $quiet;
  297. $nowrite=1;
  298. } else {
  299. printf(_g("%s: replacing existing dir entry for \`%s'")."\n", $name, $target_entry) unless $quiet;
  300. }
  301. $mss= $i;
  302. @work= (@work[0..$i-1], @work[$j..$#work]);
  303. } elsif (length($sectionre)) {
  304. $mss= -1;
  305. for ($i=0; $i<=$#work; $i++) {
  306. $_= $work[$i];
  307. next if m/^\*/;
  308. next unless m/$sectionre/io;
  309. $mss= $i+1; last;
  310. }
  311. if ($mss < 0) {
  312. printf(_g("%s: creating new section \`%s'")."\n", $name, $sectiontitle) unless $quiet;
  313. for ($i= $#work; $i>=0 && $work[$i] =~ m/\S/; $i--) { }
  314. if ($i <= 0) { # We ran off the top, make this section and Misc.
  315. printf(_g("%s: no sections yet, creating Miscellaneous section too.")."\n", $name)
  316. unless $quiet;
  317. @work= ("\n", "$sectiontitle\n", "\n", "Miscellaneous:\n", @work);
  318. $mss= 1;
  319. } else {
  320. @work= (@work[0..$i], "$sectiontitle\n", "\n", @work[$i+1..$#work]);
  321. $mss= $i+1;
  322. }
  323. }
  324. while ($mss <= $#work) {
  325. $work[$mss] =~ m/\S/ || last;
  326. $work[$mss] =~ m/^\* *([^:]+):/ || ($mss++, next);
  327. last if $multiline;
  328. $_=$1; y/A-Z/a-z/;
  329. last if $_ gt $sortby;
  330. $mss++;
  331. }
  332. } else {
  333. printf(_g("%s: no section specified for new entry, placing at end")."\n", $name)
  334. unless $quiet;
  335. $mss= $#work+1;
  336. }
  337. @work= (@work[0..$mss-1], map("$_\n",split(/\n/,$infoentry)), @work[$mss..$#work]);
  338. } else {
  339. my $target_entry;
  340. if($remove_exactly) {
  341. $target_entry = $remove_exactly;
  342. } else {
  343. $target_entry = $basename;
  344. }
  345. for ($i=0; $i<=$#work; $i++) {
  346. next unless $work[$i] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  347. $tme= $1; $tfile= $2; $match= $&;
  348. next unless $tfile eq $target_entry;
  349. last if !length($menuentry);
  350. $tme =~ y/A-Z/a-z/;
  351. last if $tme eq $menuentry;
  352. }
  353. for ($j=$i; $j<=$#work+1; $j++) {
  354. next if $work[$j] =~ m/^\s+\S/;
  355. last unless $work[$j] =~ m/^\* *([^:]+): *\((\w[^\)]*)\)/;
  356. $tme= $1; $tfile= $2;
  357. last unless $tfile eq $target_entry;
  358. next if !length($menuentry);
  359. $tme =~ y/A-Z/a-z/;
  360. last unless $tme eq $menuentry;
  361. }
  362. if ($i < $j) {
  363. &dprint("i=$i \$work[\$i]='$work[$i]' j=$j \$work[\$j]='$work[$j]'");
  364. printf(_g("%s: deleting entry \`%s ...'")."\n", $name, $match) unless $quiet;
  365. $_= $work[$i-1];
  366. unless (m/^\s/ || m/^\*/ || m/^$/ ||
  367. $j > $#work || $work[$j] !~ m/^\s*$/) {
  368. s/:?\s+$//;
  369. if ($keepold) {
  370. printf(_g("%s: empty section \`%s' not removed")."\n", $name, $_) unless $quiet;
  371. } else {
  372. $i--; $j++;
  373. printf(_g("%s: deleting empty section \`%s'")."\n", $name, $_) unless $quiet;
  374. }
  375. }
  376. @work= (@work[0..$i-1], @work[$j..$#work]);
  377. } else {
  378. unless ($quiet) {
  379. if (length($menuentry)) {
  380. printf _g("%s: no entry for file \`%s' and menu entry \`%s'")."\n", $name, $target_entry, $menuentry;
  381. } else {
  382. printf _g("%s: no entry for file \`%s'")."\n", $name, $target_entry;
  383. }
  384. }
  385. }
  386. }
  387. $length = 0;
  388. $j = -1;
  389. for ($i=0; $i<=$#work; $i++) {
  390. $_ = $work[$i];
  391. chomp;
  392. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ) {
  393. $length = length($1) if ( length($1) > $length );
  394. $work[++$j] = $_;
  395. } elsif ( m/^[ \t]+(.*)/ ) {
  396. $work[$j] = "$work[$j] $1";
  397. } else {
  398. $work[++$j] = $_;
  399. }
  400. }
  401. @work = @work[0..$j];
  402. my $descalign=40;
  403. @newwork = ();
  404. foreach ( @work ) {
  405. if ( m/^(\* *[^:]+: *\(\w[^\)]*\)[^.]*\.)[ \t]*(.*)/ ||
  406. m/^([ \t]+)(.*)/ ) {
  407. if (length $1 >= $descalign) {
  408. push @newwork, $1;
  409. $_=(" " x $descalign) . $2;
  410. }
  411. else {
  412. $_ = $1 . (" " x ($descalign - length $1)) . $2;
  413. }
  414. push @newwork, split( "\n", wrap('', " " x $descalign, $_ ) );
  415. } else {
  416. push @newwork, $_;
  417. }
  418. }
  419. if (!$nowrite) {
  420. open(NEW,"> $dirfile.new") || &ulquit(sprintf(_g("unable to create %s: %s"),
  421. "$dirfile.new", $!));
  422. print(NEW @head,join("\n",@newwork)) ||
  423. &ulquit(sprintf(_g("unable to write %s: %s"), "$dirfile.new", $!));
  424. close(NEW) || &ulquit(sprintf(_g("unable to close %s: %s"), "$dirfile.new", $!));
  425. unlink("$dirfile.old");
  426. link($dirfile, "$dirfile.old") ||
  427. &ulquit(sprintf(_g("unable to backup old %s, giving up: %s"),
  428. $dirfile, $!));
  429. rename("$dirfile.new", $dirfile) ||
  430. &ulquit(sprintf(_g("unable to install new %s: %s"), $dirfile, $!));
  431. unlink("$dirfile.lock") ||
  432. &quit(sprintf(_g("unable to unlock %s: %s"), $dirfile, $!));
  433. system ('cp', $dirfile, $backup) &&
  434. warn sprintf(_g("%s: could not backup %s in %s: %s"), $name, $dirfile, $backup, $!)."\n";
  435. }
  436. sub quit
  437. {
  438. die "$name: $@\n";
  439. }
  440. sub ulquit {
  441. unlink("$dirfile.lock") ||
  442. warn sprintf(_g("%s: warning - unable to unlock %s: %s"),
  443. $name, $dirfile, $!)."\n";
  444. &quit($_[0]);
  445. }
  446. sub checkpipe {
  447. return if !$pipeit || !$? || $?==0x8D00 || $?==0x0D;
  448. &quit(sprintf(_g("unable to read %s: %d"), $filename, $?));
  449. }
  450. sub dprint {
  451. printf(DEBUG _g("dbg: %s")."\n", $_[0]) if ($debug);
  452. }
  453. exit 0;