install-info.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #!/usr/bin/perl --
  2. use Text::Wrap;
  3. use English;
  4. my $dpkglibdir = "."; # This line modified by Makefile
  5. push (@INC, $dpkglibdir);
  6. require 'dpkg-gettext.pl';
  7. textdomain("dpkg");
  8. ($0) = $0 =~ m:.*/(.+):;
  9. # fixme: sort entries
  10. # fixme: send to FSF ?
  11. $version= '0.93.42.2'; # This line modified by Makefile
  12. sub version {
  13. printf _g("Debian %s version %s.\n"), $0, $version;
  14. printf _g("
  15. Copyright (C) 1994,1995 Ian Jackson.");
  16. printf _g("
  17. This is free software; see the GNU General Public Licence version 2 or
  18. later for copying conditions. There is NO warranty.
  19. ");
  20. }
  21. sub usage {
  22. $file = $_[0];
  23. printf $file _g(
  24. "Usage: %s [<options> ...] [--] <filename>
  25. Options:
  26. --section <regexp> <title>
  27. put the new entry in the <regex> matched section
  28. or create a new one with <title> if non-existent.
  29. --menuentry=<text> set the menu entry.
  30. --description=<text> set the description to be used in the menu entry.
  31. --info-file=<path> specify info file to install in the directory.
  32. --dir-file=<path> specify file name of info directory file.
  33. --infodir=<directory> same as '--dir-file=<directory>/dir'.
  34. --info-dir=<directory> likewise.
  35. --keep-old do not replace entries nor remove empty ones.
  36. --remove remove the entry specified by <filename> basename.
  37. --remove-exactly remove the exact <filename> entry.
  38. --test enables test mode (no actions taken).
  39. --debug enables debug mode (show more information).
  40. --quiet do not show output messages.
  41. --help show this help message.
  42. --version show the version.
  43. "), $0;
  44. }
  45. $dirfile = '/usr/share/info/dir';
  46. $maxwidth=79;
  47. $Text::Wrap::columns=$maxwidth;
  48. $backup='/var/backups/infodir.bak';
  49. $default='/usr/share/base-files/info.dir';
  50. $menuentry="";
  51. $description="";
  52. $sectionre="";
  53. $sectiontitle="";
  54. $infoentry="";
  55. $quiet=0;
  56. $nowrite=0;
  57. $keepold=0;
  58. $debug=0;
  59. $remove=0;
  60. my $remove_exactly;
  61. $0 =~ m|[^/]+$|; $name= $&;
  62. while ($ARGV[0] =~ m/^--/) {
  63. $_= shift(@ARGV);
  64. last if $_ eq '--';
  65. if ($_ eq '--version') {
  66. &version(STDOUT); exit 0;
  67. } elsif ($_ eq '--quiet') {
  68. $quiet=1;
  69. } elsif ($_ eq '--test') {
  70. $nowrite=1;
  71. } elsif ($_ eq '--keep-old') {
  72. $keepold=1;
  73. } elsif ($_ eq '--remove') {
  74. $remove=1;
  75. } elsif ($_ eq '--remove-exactly') {
  76. $remove=1;
  77. $remove_exactly=1;
  78. } elsif ($_ eq '--help') {
  79. &usage(STDOUT); exit 0;
  80. } elsif ($_ eq '--version') {
  81. &version; exit 0;
  82. } elsif ($_ eq '--debug') {
  83. open(DEBUG,">&STDERR")
  84. || die sprintf(_g("Could not open stderr for output! %s"), $!)."\n";
  85. $debug=1;
  86. } elsif ($_ eq '--section') {
  87. if (@ARGV < 2) {
  88. printf STDERR _g("%s: --section needs two more args")."\n", $name;
  89. &usage(STDERR); exit 1;
  90. }
  91. $sectionre= shift(@ARGV);
  92. $sectiontitle= shift(@ARGV);
  93. } elsif (m/^--(c?align|maxwidth)=([0-9]+)$/) {
  94. warn( sprintf(_g("%s: %s deprecated(ignored)"), $name, $1)."\n" );
  95. } elsif (m/^--info-?dir=/) {
  96. $dirfile = $POSTMATCH . '/dir';
  97. } elsif (m/^--info-file=/) {
  98. $filename=$POSTMATCH;
  99. } elsif (m/^--menuentry=/) {
  100. $menuentry=$POSTMATCH;
  101. } elsif (m/^--description=/) {
  102. $description=$POSTMATCH;
  103. } elsif (m/^--dir-file=/) { # for compatibility with GNU install-info
  104. $dirfile = $POSTMATCH;
  105. } else {
  106. printf STDERR _g("%s: unknown option \`%s'")."\n", $name, $_;
  107. &usage(STDERR); exit 1;
  108. }
  109. }
  110. if (!@ARGV) { &usage(STDERR); exit 1; }
  111. if ( !$filename ) {
  112. $filename= shift(@ARGV);
  113. $name = "$name($filename)";
  114. }
  115. if (@ARGV) { printf STDERR _g("%s: too many arguments")."\n", $name; &usage(STDERR); exit 1; }
  116. if ($remove) {
  117. printf(STDERR _g("%s: --section ignored with --remove")."\n", $name) if length($sectiontitle);
  118. printf(STDERR _g("%s: --description ignored with --remove")."\n", $name) if length($description);
  119. }
  120. printf(STDERR _g("%s: test mode - dir file will not be updated")."\n", $name)
  121. if $nowrite && !$quiet;
  122. umask(umask(0777) & ~0444);
  123. if($remove_exactly) {
  124. $remove_exactly = $filename;
  125. }
  126. $filename =~ m|[^/]+$|; $basename= $&; $basename =~ s/(\.info)?(\.gz)?$//;
  127. # The location of the info files from the dir entry, i.e. (emacs-20/emacs).
  128. my $fileinentry;
  129. &dprint("dirfile='$dirfile' filename='$filename' maxwidth='$maxwidth'");
  130. &dprint("menuentry='$menuentry' basename='$basename'");
  131. &dprint("description='$description' remove=$remove");
  132. if (!$remove) {
  133. if (!-f $filename && -f "$filename.gz" || $filename =~ s/\.gz$//) {
  134. $filename= "gzip -cd <$filename.gz |"; $pipeit= 1;
  135. } else {
  136. $filename= "< $filename";
  137. }
  138. if (!length($description)) {
  139. open(IF,"$filename") || die sprintf(_g("%s: read %s: %s"), $name, $filename, $!)."\n";
  140. $asread='';
  141. while(<IF>) {
  142. m/^START-INFO-DIR-ENTRY$/ && last;
  143. m/^INFO-DIR-SECTION (.+)$/ && do {
  144. $sectiontitle = $1 unless ($sectiontitle);
  145. $sectionre = '^'.quotemeta($1) unless ($sectionre);
  146. }
  147. }
  148. while(<IF>) { last if m/^END-INFO-DIR-ENTRY$/; $asread.= $_; }
  149. if ($pipeit) {
  150. while (<IF>) {};
  151. }
  152. close(IF); &checkpipe;
  153. if ($asread =~ m/(\*\s*[^:]+:\s*\(([^\)]+)\).*\. *.*\n){2}/) {
  154. $infoentry= $asread;
  155. $multiline= 1;
  156. $fileinentry = $2;
  157. &dprint("multiline '$asread'");
  158. } elsif ($asread =~ m/^\*\s*([^:]+):(\s*\(([^\)]+)\)\.|:)\s*/) {
  159. $menuentry= $1;
  160. $description= $POSTMATCH;
  161. $fileinentry = $3;
  162. &dprint("infile menuentry '$menuentry' description '$description'");
  163. } elsif (length($asread)) {
  164. printf STDERR _g("%s: warning, ignoring confusing INFO-DIR-ENTRY in file.")."\n", $name;
  165. }
  166. }
  167. if (length($infoentry)) {
  168. $infoentry =~ m/\n/;
  169. print "$`\n" unless $quiet;
  170. $infoentry =~ m/^\*\s*([^:]+):\s*\(([^\)]+)\)/ ||
  171. die sprintf(_g("%s: Invalid info entry"), $name)."\n"; # internal error
  172. $sortby= $1;
  173. $fileinentry= $2;
  174. } else {
  175. if (!length($description)) {
  176. open(IF,"$filename") || die sprintf(_g("%s: read %s: %s"), $name, $filename, $!)."\n";
  177. $asread='';
  178. while(<IF>) {
  179. if (m/^\s*[Tt]his file documents/) {
  180. $asread=$POSTMATCH;
  181. last;
  182. }
  183. }
  184. if (length($asread)) {
  185. while(<IF>) { last if m/^\s*$/; $asread.= $_; }
  186. $description= $asread;
  187. }
  188. if ($pipeit) {
  189. while (<IF>) {};
  190. }
  191. close(IF); &checkpipe;
  192. }
  193. if (!length($description)) {
  194. printf STDERR _g("
  195. No \`START-INFO-DIR-ENTRY' and no \`This file documents'.
  196. %s: unable to determine description for \`dir' entry - giving up
  197. "), $name;
  198. exit 1;
  199. }
  200. $description =~ s/^\s*(.)//; $_=$1; y/a-z/A-Z/;
  201. $description= $_ . $description;
  202. if (!length($menuentry)) {
  203. $menuentry= $basename; $menuentry =~ s/\Winfo$//;
  204. $menuentry =~ s/^.//; $_=$&; y/a-z/A-Z/;
  205. $menuentry= $_ . $menuentry;
  206. }
  207. &dprint("menuentry='$menuentry' description='$description'");
  208. if($fileinentry) {
  209. $cprefix= sprintf("* %s: (%s).", $menuentry, $fileinentry);
  210. } else {
  211. $cprefix= sprintf("* %s: (%s).", $menuentry, $basename);
  212. }
  213. $align--; $calign--;
  214. $lprefix= length($cprefix);
  215. if ($lprefix < $align) {
  216. $cprefix .= ' ' x ($align - $lprefix);
  217. $lprefix= $align;
  218. }
  219. $prefix= "\n". (' 'x $calign);
  220. $cwidth= $maxwidth+1;
  221. for $_ (split(/\s+/,$description)) {
  222. $l= length($_);
  223. $cwidth++; $cwidth += $l;
  224. if ($cwidth > $maxwidth) {
  225. $infoentry .= $cprefix;
  226. $cwidth= $lprefix+1+$l;
  227. $cprefix= $prefix; $lprefix= $calign;
  228. }
  229. $infoentry.= ' '; $infoentry .= $_;
  230. }
  231. $infoentry.= "\n";
  232. print $infoentry unless $quiet;
  233. $sortby= $menuentry; $sortby =~ y/A-Z/a-z/;
  234. }
  235. }
  236. if (!$nowrite && ( ! -e $dirfile || ! -s _ )) {
  237. if (-r $backup) {
  238. printf( STDERR _g("%s: no file %s, retrieving backup file %s.")."\n",
  239. $name, $dirfile, "$backup" );
  240. if (system ('cp', $backup, $dirfile)) {
  241. printf( STDERR _g("%s: copying %s to %s failed, giving up: %s")."\n",
  242. $name, $backup, $dirfile, $! );
  243. exit 1;
  244. }
  245. } else {
  246. if (-r $default) {
  247. printf( STDERR _g("%s: no backup file %s available, retrieving default file.")."\n",
  248. $name, $backup );
  249. if (system('cp', $default, $dirfile)) {
  250. printf( STDERR _g("%s: copying %s to %s failed, giving up: %s")."\n",
  251. $name, $default, $dirfile, $! );
  252. exit 1;
  253. }
  254. } else {
  255. printf STDERR _g("%s: no backup file %s available.")."\n", $name, $backup;
  256. printf STDERR _g("%s: no default file %s available, giving up.")."\n", $name, $default;
  257. exit 1;
  258. }
  259. }
  260. }
  261. if (!$nowrite && !link($dirfile, "$dirfile.lock")) {
  262. printf( STDERR _g("%s: failed to lock dir for editing! %s")."\n",
  263. $name, $! );
  264. printf( STDERR _g("try deleting %s?")."\n", "$dirfile.lock")
  265. if $!{EEXIST};
  266. exit 1;
  267. }
  268. open(OLD,$dirfile) || &ulquit(sprintf(_g("open %s: %s"), $dirfile, $!));
  269. @work= <OLD>;
  270. eof(OLD) || &ulquit(sprintf(_g("read %s: %s"), $dirfile, $!));
  271. close(OLD) || &ulquit(sprintf(_g("close %s after read: %s"), $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("create %s: %s"), "$dirfile.new", $!));
  421. print(NEW @head,join("\n",@newwork)) ||
  422. &ulquit(sprintf(_g("write %s: %s"), "$dirfile.new", $!));
  423. close(NEW) || &ulquit(sprintf(_g("close %s: %s"), "$dirfile.new", $!));
  424. unlink("$dirfile.old");
  425. link($dirfile, "$dirfile.old") ||
  426. &ulquit(sprintf(_g("cannot backup old %s, giving up: %s"), $dirfile, $!));
  427. rename("$dirfile.new", $dirfile) ||
  428. &ulquit(sprintf(_g("install new %s: %s"), $dirfile, $!));
  429. unlink("$dirfile.lock") ||
  430. die sprintf(_g("%s: unlock %s: %s"), $name, $dirfile, $!)."\n";
  431. system ('cp', $dirfile, $backup) &&
  432. warn sprintf(_g("%s: couldn't backup %s in %s: %s"), $name, $dirfile, $backup, $!)."\n";
  433. }
  434. sub ulquit {
  435. unlink("$dirfile.lock") ||
  436. warn sprintf(_g("%s: warning - unable to unlock %s: %s"),
  437. $name, $dirfile, $!)."\n";
  438. die "$name: $_[0]\n";
  439. }
  440. sub checkpipe {
  441. return if !$pipeit || !$? || $?==0x8D00 || $?==0x0D;
  442. die sprintf(_g("%s: read %s: %d"), $name, $filename, $?)."\n";
  443. }
  444. sub dprint {
  445. printf(DEBUG _g("dbg: %s")."\n", $_[0]) if ($debug);
  446. }
  447. exit 0;