Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
13 KiB

  1. #############################################################################
  2. # Pod/Find.pm -- finds files containing POD documentation
  3. #
  4. # Author: Marek Rouchal <[email protected]>
  5. #
  6. # Copyright (C) 1999-2000 by Marek Rouchal (and borrowing code
  7. # from Nick Ing-Simmon's PodToHtml). All rights reserved.
  8. # This file is part of "PodParser". Pod::Find is free software;
  9. # you can redistribute it and/or modify it under the same terms
  10. # as Perl itself.
  11. #############################################################################
  12. package Pod::Find;
  13. use vars qw($VERSION);
  14. $VERSION = 0.21; ## Current version of this package
  15. require 5.005; ## requires this Perl version or later
  16. use Carp;
  17. #############################################################################
  18. =head1 NAME
  19. Pod::Find - find POD documents in directory trees
  20. =head1 SYNOPSIS
  21. use Pod::Find qw(pod_find simplify_name);
  22. my %pods = pod_find({ -verbose => 1, -inc => 1 });
  23. foreach(keys %pods) {
  24. print "found library POD `$pods{$_}' in $_\n";
  25. }
  26. print "podname=",simplify_name('a/b/c/mymodule.pod'),"\n";
  27. $location = pod_where( { -inc => 1 }, "Pod::Find" );
  28. =head1 DESCRIPTION
  29. B<Pod::Find> provides a set of functions to locate POD files. Note that
  30. no function is exported by default to avoid pollution of your namespace,
  31. so be sure to specify them in the B<use> statement if you need them:
  32. use Pod::Find qw(pod_find);
  33. =cut
  34. use strict;
  35. #use diagnostics;
  36. use Exporter;
  37. use File::Spec;
  38. use File::Find;
  39. use Cwd;
  40. use vars qw(@ISA @EXPORT_OK $VERSION);
  41. @ISA = qw(Exporter);
  42. @EXPORT_OK = qw(&pod_find &simplify_name &pod_where &contains_pod);
  43. # package global variables
  44. my $SIMPLIFY_RX;
  45. =head2 C<pod_find( { %opts } , @directories )>
  46. The function B<pod_find> searches for POD documents in a given set of
  47. files and/or directories. It returns a hash with the file names as keys
  48. and the POD name as value. The POD name is derived from the file name
  49. and its position in the directory tree.
  50. E.g. when searching in F<$HOME/perl5lib>, the file
  51. F<$HOME/perl5lib/MyModule.pm> would get the POD name I<MyModule>,
  52. whereas F<$HOME/perl5lib/Myclass/Subclass.pm> would be
  53. I<Myclass::Subclass>. The name information can be used for POD
  54. translators.
  55. Only text files containing at least one valid POD command are found.
  56. A warning is printed if more than one POD file with the same POD name
  57. is found, e.g. F<CPAN.pm> in different directories. This usually
  58. indicates duplicate occurrences of modules in the I<@INC> search path.
  59. B<OPTIONS> The first argument for B<pod_find> may be a hash reference
  60. with options. The rest are either directories that are searched
  61. recursively or files. The POD names of files are the plain basenames
  62. with any Perl-like extension (.pm, .pl, .pod) stripped.
  63. =over 4
  64. =item C<-verbose =E<gt> 1>
  65. Print progress information while scanning.
  66. =item C<-perl =E<gt> 1>
  67. Apply Perl-specific heuristics to find the correct PODs. This includes
  68. stripping Perl-like extensions, omitting subdirectories that are numeric
  69. but do I<not> match the current Perl interpreter's version id, suppressing
  70. F<site_perl> as a module hierarchy name etc.
  71. =item C<-script =E<gt> 1>
  72. Search for PODs in the current Perl interpreter's installation
  73. B<scriptdir>. This is taken from the local L<Config|Config> module.
  74. =item C<-inc =E<gt> 1>
  75. Search for PODs in the current Perl interpreter's I<@INC> paths. This
  76. automatically considers paths specified in the C<PERL5LIB> environment
  77. as this is prepended to I<@INC> by the Perl interpreter itself.
  78. =back
  79. =cut
  80. # return a hash of the POD files found
  81. # first argument may be a hashref (options),
  82. # rest is a list of directories to search recursively
  83. sub pod_find
  84. {
  85. my %opts;
  86. if(ref $_[0]) {
  87. %opts = %{shift()};
  88. }
  89. $opts{-verbose} ||= 0;
  90. $opts{-perl} ||= 0;
  91. my (@search) = @_;
  92. if($opts{-script}) {
  93. require Config;
  94. push(@search, $Config::Config{scriptdir});
  95. $opts{-perl} = 1;
  96. }
  97. if($opts{-inc}) {
  98. push(@search, grep($_ ne '.',@INC));
  99. $opts{-perl} = 1;
  100. }
  101. if($opts{-perl}) {
  102. require Config;
  103. # this code simplifies the POD name for Perl modules:
  104. # * remove "site_perl"
  105. # * remove e.g. "i586-linux" (from 'archname')
  106. # * remove e.g. 5.00503
  107. # * remove pod/ if followed by *.pod (e.g. in pod/perlfunc.pod)
  108. $SIMPLIFY_RX =
  109. qq!^(?i:site(_perl)?/|\Q$Config::Config{archname}\E/|\\d+\\.\\d+([_.]?\\d+)?/|pod/(?=.*?\\.pod\\z))*!;
  110. }
  111. my %dirs_visited;
  112. my %pods;
  113. my %names;
  114. my $pwd = cwd();
  115. foreach my $try (@search) {
  116. unless(File::Spec->file_name_is_absolute($try)) {
  117. # make path absolute
  118. $try = File::Spec->catfile($pwd,$try);
  119. }
  120. # simplify path
  121. # on VMS canonpath will vmsify:[the.path], but File::Find::find
  122. # wants /unixy/paths
  123. $try = File::Spec->canonpath($try) if ($^O ne 'VMS');
  124. my $name;
  125. if(-f $try) {
  126. if($name = _check_and_extract_name($try, $opts{-verbose})) {
  127. _check_for_duplicates($try, $name, \%names, \%pods);
  128. }
  129. next;
  130. }
  131. my $root_rx = qq!^\Q$try\E/!;
  132. File::Find::find( sub {
  133. my $item = $File::Find::name;
  134. if(-d) {
  135. if($dirs_visited{$item}) {
  136. warn "Directory '$item' already seen, skipping.\n"
  137. if($opts{-verbose});
  138. $File::Find::prune = 1;
  139. return;
  140. }
  141. else {
  142. $dirs_visited{$item} = 1;
  143. }
  144. if($opts{-perl} && /^(\d+\.[\d_]+)\z/s && eval "$1" != $]) {
  145. $File::Find::prune = 1;
  146. warn "Perl $] version mismatch on $_, skipping.\n"
  147. if($opts{-verbose});
  148. }
  149. return;
  150. }
  151. if($name = _check_and_extract_name($item, $opts{-verbose}, $root_rx)) {
  152. _check_for_duplicates($item, $name, \%names, \%pods);
  153. }
  154. }, $try); # end of File::Find::find
  155. }
  156. chdir $pwd;
  157. %pods;
  158. }
  159. sub _check_for_duplicates {
  160. my ($file, $name, $names_ref, $pods_ref) = @_;
  161. if($$names_ref{$name}) {
  162. warn "Duplicate POD found (shadowing?): $name ($file)\n";
  163. warn " Already seen in ",
  164. join(' ', grep($$pods_ref{$_} eq $name, keys %$pods_ref)),"\n";
  165. }
  166. else {
  167. $$names_ref{$name} = 1;
  168. }
  169. $$pods_ref{$file} = $name;
  170. }
  171. sub _check_and_extract_name {
  172. my ($file, $verbose, $root_rx) = @_;
  173. # check extension or executable flag
  174. # this involves testing the .bat extension on Win32!
  175. unless(-f $file && -T _ && ($file =~ /\.(pod|pm|plx?)\z/i || -x _ )) {
  176. return undef;
  177. }
  178. return undef unless contains_pod($file,$verbose);
  179. # strip non-significant path components
  180. # TODO what happens on e.g. Win32?
  181. my $name = $file;
  182. if(defined $root_rx) {
  183. $name =~ s!$root_rx!!s;
  184. $name =~ s!$SIMPLIFY_RX!!os if(defined $SIMPLIFY_RX);
  185. }
  186. else {
  187. $name =~ s:^.*/::s;
  188. }
  189. _simplify($name);
  190. $name =~ s!/+!::!g; #/
  191. $name;
  192. }
  193. =head2 C<simplify_name( $str )>
  194. The function B<simplify_name> is equivalent to B<basename>, but also
  195. strips Perl-like extensions (.pm, .pl, .pod) and extensions like
  196. F<.bat>, F<.cmd> on Win32 and OS/2, or F<.com> on VMS, respectively.
  197. =cut
  198. # basic simplification of the POD name:
  199. # basename & strip extension
  200. sub simplify_name {
  201. my ($str) = @_;
  202. # remove all path components
  203. $str =~ s:^.*/::s;
  204. _simplify($str);
  205. $str;
  206. }
  207. # internal sub only
  208. sub _simplify {
  209. # strip Perl's own extensions
  210. $_[0] =~ s/\.(pod|pm|plx?)\z//i;
  211. # strip meaningless extensions on Win32 and OS/2
  212. $_[0] =~ s/\.(bat|exe|cmd)\z//i if($^O =~ /mswin|os2/i);
  213. # strip meaningless extensions on VMS
  214. $_[0] =~ s/\.(com)\z//i if($^O eq 'VMS');
  215. }
  216. # contribution from Tim Jenness <[email protected]>
  217. =head2 C<pod_where( { %opts }, $pod )>
  218. Returns the location of a pod document given a search directory
  219. and a module (e.g. C<File::Find>) or script (e.g. C<perldoc>) name.
  220. Options:
  221. =over 4
  222. =item C<-inc =E<gt> 1>
  223. Search @INC for the pod and also the C<scriptdir> defined in the
  224. L<Config|Config> module.
  225. =item C<-dirs =E<gt> [ $dir1, $dir2, ... ]>
  226. Reference to an array of search directories. These are searched in order
  227. before looking in C<@INC> (if B<-inc>). Current directory is used if
  228. none are specified.
  229. =item C<-verbose =E<gt> 1>
  230. List directories as they are searched
  231. =back
  232. Returns the full path of the first occurence to the file.
  233. Package names (eg 'A::B') are automatically converted to directory
  234. names in the selected directory. (eg on unix 'A::B' is converted to
  235. 'A/B'). Additionally, '.pm', '.pl' and '.pod' are appended to the
  236. search automatically if required.
  237. A subdirectory F<pod/> is also checked if it exists in any of the given
  238. search directories. This ensures that e.g. L<perlfunc|perlfunc> is
  239. found.
  240. It is assumed that if a module name is supplied, that that name
  241. matches the file name. Pods are not opened to check for the 'NAME'
  242. entry.
  243. A check is made to make sure that the file that is found does
  244. contain some pod documentation.
  245. =cut
  246. sub pod_where {
  247. # default options
  248. my %options = (
  249. '-inc' => 0,
  250. '-verbose' => 0,
  251. '-dirs' => [ '.' ],
  252. );
  253. # Check for an options hash as first argument
  254. if (defined $_[0] && ref($_[0]) eq 'HASH') {
  255. my $opt = shift;
  256. # Merge default options with supplied options
  257. %options = (%options, %$opt);
  258. }
  259. # Check usage
  260. carp 'Usage: pod_where({options}, $pod)' unless (scalar(@_));
  261. # Read argument
  262. my $pod = shift;
  263. # Split on :: and then join the name together using File::Spec
  264. my @parts = split (/::/, $pod);
  265. # Get full directory list
  266. my @search_dirs = @{ $options{'-dirs'} };
  267. if ($options{'-inc'}) {
  268. require Config;
  269. # Add @INC
  270. push (@search_dirs, @INC) if $options{'-inc'};
  271. # Add location of pod documentation for perl man pages (eg perlfunc)
  272. # This is a pod directory in the private install tree
  273. #my $perlpoddir = File::Spec->catdir($Config::Config{'installprivlib'},
  274. # 'pod');
  275. #push (@search_dirs, $perlpoddir)
  276. # if -d $perlpoddir;
  277. # Add location of binaries such as pod2text
  278. push (@search_dirs, $Config::Config{'scriptdir'})
  279. if -d $Config::Config{'scriptdir'};
  280. }
  281. # Loop over directories
  282. Dir: foreach my $dir ( @search_dirs ) {
  283. # Don't bother if cant find the directory
  284. if (-d $dir) {
  285. warn "Looking in directory $dir\n"
  286. if $options{'-verbose'};
  287. # Now concatenate this directory with the pod we are searching for
  288. my $fullname = File::Spec->catfile($dir, @parts);
  289. warn "Filename is now $fullname\n"
  290. if $options{'-verbose'};
  291. # Loop over possible extensions
  292. foreach my $ext ('', '.pod', '.pm', '.pl') {
  293. my $fullext = $fullname . $ext;
  294. if (-f $fullext &&
  295. contains_pod($fullext, $options{'-verbose'}) ) {
  296. warn "FOUND: $fullext\n" if $options{'-verbose'};
  297. return $fullext;
  298. }
  299. }
  300. } else {
  301. warn "Directory $dir does not exist\n"
  302. if $options{'-verbose'};
  303. next Dir;
  304. }
  305. if(-d File::Spec->catdir($dir,'pod')) {
  306. $dir = File::Spec->catdir($dir,'pod');
  307. redo Dir;
  308. }
  309. }
  310. # No match;
  311. return undef;
  312. }
  313. =head2 C<contains_pod( $file , $verbose )>
  314. Returns true if the supplied filename (not POD module) contains some pod
  315. information.
  316. =cut
  317. sub contains_pod {
  318. my $file = shift;
  319. my $verbose = 0;
  320. $verbose = shift if @_;
  321. # check for one line of POD
  322. unless(open(POD,"<$file")) {
  323. warn "Error: $file is unreadable: $!\n";
  324. return undef;
  325. }
  326. local $/ = undef;
  327. my $pod = <POD>;
  328. close(POD) || die "Error closing $file: $!\n";
  329. unless($pod =~ /\n=(head\d|pod|over|item)\b/s) {
  330. warn "No POD in $file, skipping.\n"
  331. if($verbose);
  332. return 0;
  333. }
  334. return 1;
  335. }
  336. =head1 AUTHOR
  337. Marek Rouchal E<lt>marek@saftsack.fs.uni-bayreuth.deE<gt>,
  338. heavily borrowing code from Nick Ing-Simmons' PodToHtml.
  339. Tim Jenness E<lt>t.jenness@jach.hawaii.eduE<gt> provided
  340. C<pod_where> and C<contains_pod>.
  341. =head1 SEE ALSO
  342. L<Pod::Parser>, L<Pod::Checker>, L<perldoc>
  343. =cut
  344. 1;