install-info.pl 15 KB

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