]> git.donarmstrong.com Git - debhelper.git/blob - dh_installman
r1894: * dh_installman: die with an error if a man page read for so lines fails
[debhelper.git] / dh_installman
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_installman - install man pages into package build directories
6
7 =cut
8
9 use strict;
10 use File::Find;
11 use Debian::Debhelper::Dh_Lib;
12
13 =head1 SYNOPSIS
14
15 B<dh_installman> [S<I<debhelper options>>] [S<I<manpage ...>>]
16
17 =head1 DESCRIPTION
18
19 dh_installman is a debhelper program that handles installing
20 man pages into the correct locations in package build directories. You tell
21 it what man pages go in your packages, and it figures out where to
22 install them based on the section field in their .TH line. If you have a
23 properly formatted .TH line, your man page will be installed into the right
24 directory, with the right name (this includes proper handling of pages
25 with a subsection, like "3perl", which are placed in man3, and given an
26 extension of ".3perl"). If your .TH line is incorrect or missing, the program
27 may guess wrong based on the file extension.
28
29 It also supports translated man pages, by looking for extensions
30 like .ll.8 and .ll_LL.8, or by use of the --language switch.
31
32 If dh_installman seems to install a man page into the wrong section or with
33 the wrong extension, this is because the man page has the wrong section
34 listed in its .TH line. Edit the man page and correct the section, and
35 dh_installman will follow suit.  See to L<man(7)> for details about the .TH
36 section. If dh_installman seems to install a man page into a directory
37 like /usr/share/man/pl/man1/, that is because your program has a
38 name like "foo.pl", and dh_installman assumes that means it is translated
39 into Polish. Use --language=C to avoid this.
40
41 Any man page filenames specified as parameters will be installed into the
42 first package dh_installman is told to act on. By default, this is the
43 first binary package in debian/control, but if you use -p, -i, or -a flags,
44 it will be the first package specified by those flags.
45
46 Files named debian/package.manpages can list other man pages to be
47 installed.
48
49 After the man page installation step, dh_installman will check to see if
50 any of the man pages in the temporary directories of any of the packages it
51 is acting on contain ".so" links. If so, it changes them to symlinks.
52
53 =head1 OPTIONS
54
55 =over 4
56
57 =item B<-A>, B<--all>
58
59 Install all files specified by command line parameters in ALL packages
60 acted on.
61
62 =item B<--language>=ll
63
64 Use this to specify that the man pages being acted on are written in the
65 specified language.
66
67 =item I<manpage ...>
68
69 Install these man pages into the first package acted on. (Or in all
70 packages if -A is specified).
71
72 =back
73
74 =head1 NOTES
75
76 An older version of this program, L<dh_installmanpages(1)>, is still used
77 by some packages, and so is still included in debhelper.
78 It is, however, deprecated, due to its counterintuitive and inconsistent
79 interface. Use this program instead.
80
81 =cut
82
83 init();
84
85 my @sofiles;
86 my @sodests;
87
88 foreach my $package (@{$dh{DOPACKAGES}}) {
89         next if is_udeb($package);
90
91         my $tmp=tmpdir($package);
92         my $file=pkgfile($package,"manpages");
93         my @manpages;
94
95         if ($file) {
96                 @manpages=filearray($file, ".");
97         }
98
99         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
100                 push @manpages, @ARGV;
101         }
102
103         foreach my $page (@manpages) {
104                 my $basename=basename($page);
105
106                 # Support compressed pages.
107                 my $gz='';
108                 if ($basename=~m/(.*)(\.gz)/) {
109                         $basename=$1;
110                         $gz=$2;
111                 }
112
113                 my $section;
114                 # See if there is a .TH entry in the man page. If so,
115                 # we'll pull the section field from that.
116                 if ($gz) {
117                         open (IN, "zcat $page|") or die "$page: $!";
118                 }
119                 else {
120                         open (IN, $page) or die "$page: $!";
121                 }
122                 while (<IN>) {
123                         if (/^\.TH\s+\S+\s+"?(\d+[^"\s]*)"?/) {
124                                 $section=$1;
125                                 last;
126                         }
127                 }
128                 # Failing that, we can try to get it from the filename.
129                 if (! $section) {
130                         ($section)=$basename=~m/.*\.([1-9]\S*)/;
131                 }
132
133                 # Now get the numeric component of the section.
134                 my ($realsection)=$section=~m/^(\d)/ if defined $section;
135                 
136                 # If there is no numeric section, bail.
137                 if (! $realsection) {
138                         error("Could not determine section for $page");
139                 }
140                 
141                 # Get the man page's name -- everything up to the last dot.
142                 my ($instname)=$basename=~m/^(.*)\./;
143         
144                 my $destdir="$tmp/usr/share/man/man$realsection/";
145                 my $langcode;
146                 if (! defined $dh{LANGUAGE} || ! exists $dh{LANGUAGE}) {
147                         # Translated man pages are typically specified by adding the
148                         # language code to the filename, so detect that and
149                         # redirect to appropriate directory, stripping the code.
150                         ($langcode)=$basename=~m/.*\.([a-z][a-z](?:_[A-Z][A-Z])?)\.(?:[1-9]|man)/;
151                         if (defined $langcode && $langcode ne '') {
152                                 # Strip the language code from the instname.
153                                 $instname=~s/\.$langcode$//;
154                         }
155                 }
156                 elsif ($dh{LANGUAGE} ne 'C') {
157                         $langcode=$dh{LANGUAGE};
158                         ($instname)=$basename=~m/(.*?)\./;
159                 }
160                 
161                 if (defined $langcode && $langcode ne '') {
162                         $destdir="$tmp/usr/share/man/$langcode/man$realsection/";
163                 }
164                 $destdir=~tr:/:/:s; # just for looks
165
166                 if (! -e "$destdir/$instname.$section" && 
167                     ! -l "$destdir/$instname.$section") {
168                         if (! -d $destdir) {
169                                 doit "install","-d",$destdir;
170                         }
171                         doit "install","-p","-m644",$page,
172                                 "$destdir$instname.$section$gz";
173                 }
174                 
175         }
176
177         # Now the .so conversion.
178         @sofiles=@sodests=();
179         foreach my $dir (qw{usr/share/man usr/X11R6/man}) {
180                 if (-e "$tmp/$dir") {
181                         find(\&find_so_man, "$tmp/$dir");
182                 }
183         }
184         foreach my $sofile (@sofiles) {
185                 my $sodest=shift(@sodests);
186                 doit "rm","-f",$sofile;
187                 doit "ln","-sf",$sodest,$sofile;
188         }
189 }
190
191 # Check if a file is a .so man page, for use by File::Find.
192 sub find_so_man {
193         # The -s test is becuase a .so file tends to be small. We don't want
194         # to open every man page. 1024 is arbitrary.
195         if (! -f $_ || -s $_ > 1024 || -s == 0) {
196                 return;
197         }
198
199         # Test first line of file for the .so thing.
200         if (/\.gz$/) {
201                 open (SOTEST, "zcat $_|") or die "$_: $!";
202         }
203         else {
204                 open (SOTEST,$_) || die "$_: $!";
205         }
206         my $l=<SOTEST>;
207         close SOTEST;
208
209         if (! defined $l) {
210                 error("failed to read $_");
211         }
212         
213         if ($l=~m/\.so\s+(.*)\s*/) {
214                 my $solink=$1;
215                 # This test is here to prevent links like ... man8/../man8/foo.8
216                 if (basename($File::Find::dir) eq
217                     dirname($solink)) {
218                         $solink=basename($solink);
219                 }
220                 else {
221                         $solink="../$solink";
222                 }
223         
224                 push @sofiles,"$File::Find::dir/$_";
225                 push @sodests,$solink;
226         }
227 }
228
229 =head1 SEE ALSO
230
231 L<debhelper(7)>
232
233 This program is a part of debhelper.
234
235 =head1 AUTHOR
236
237 Joey Hess <joeyh@debian.org>
238
239 =cut