2
0

process_docs.pl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #! /usr/bin/env perl
  2. # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use strict;
  9. use warnings;
  10. use File::Spec::Functions;
  11. use File::Basename;
  12. use File::Copy;
  13. use File::Path;
  14. use FindBin;
  15. use lib "$FindBin::Bin/perl";
  16. use OpenSSL::Glob;
  17. use Getopt::Long;
  18. use Pod::Usage;
  19. use lib '.';
  20. use configdata;
  21. # We know we are in the 'util' directory and that our perl modules are
  22. # in util/perl
  23. use lib catdir(dirname($0), "perl");
  24. use OpenSSL::Util::Pod;
  25. my %options = ();
  26. GetOptions(\%options,
  27. 'sourcedir=s@', # Source directories
  28. 'section=i@', # Subdirectories to look through,
  29. # with associated section numbers
  30. 'destdir=s', # Destination directory
  31. #'in=s@', # Explicit files to process (ignores sourcedir)
  32. 'type=s', # The result type, 'man' or 'html'
  33. 'suffix:s', # Suffix to add to the extension.
  34. # Only used with type=man
  35. 'remove', # To remove files rather than writing them
  36. 'dry-run|n', # Only output file names on STDOUT
  37. 'debug|D+',
  38. );
  39. unless ($options{section}) {
  40. $options{section} = [ 1, 3, 5, 7 ];
  41. }
  42. unless ($options{sourcedir}) {
  43. $options{sourcedir} = [ catdir($config{sourcedir}, "doc"),
  44. catdir($config{builddir}, "doc") ];
  45. }
  46. pod2usage(1) unless ( defined $options{section}
  47. && defined $options{sourcedir}
  48. && defined $options{destdir}
  49. && defined $options{type}
  50. && ($options{type} eq 'man'
  51. || $options{type} eq 'html') );
  52. pod2usage(1) if ( $options{type} eq 'html'
  53. && defined $options{suffix} );
  54. if ($options{debug}) {
  55. print STDERR "DEBUG: options:\n";
  56. foreach (sort @{$options{sourcedir}}) {
  57. print STDERR "DEBUG: --sourcedir = $_\n";
  58. }
  59. print STDERR "DEBUG: --destdir = $options{destdir}\n"
  60. if defined $options{destdir};
  61. print STDERR "DEBUG: --type = $options{type}\n"
  62. if defined $options{type};
  63. print STDERR "DEBUG: --suffix = $options{suffix}\n"
  64. if defined $options{suffix};
  65. foreach (sort @{$options{section}}) {
  66. print STDERR "DEBUG: --section = $_\n";
  67. }
  68. print STDERR "DEBUG: --remove = $options{remove}\n"
  69. if defined $options{remove};
  70. print STDERR "DEBUG: --debug = $options{debug}\n"
  71. if defined $options{debug};
  72. print STDERR "DEBUG: --dry-run = $options{\"dry-run\"}\n"
  73. if defined $options{"dry-run"};
  74. }
  75. my $symlink_exists = eval { symlink("",""); 1 };
  76. foreach my $section (sort @{$options{section}}) {
  77. my $subdir = "man$section";
  78. foreach my $sourcedir (@{$options{sourcedir}}) {
  79. my $podsourcedir = catfile($sourcedir, $subdir);
  80. my $podglob = catfile($podsourcedir, "*.pod");
  81. foreach my $podfile (glob $podglob) {
  82. my $podname = basename($podfile, ".pod");
  83. my $podpath = catfile($podfile);
  84. my %podinfo = extract_pod_info($podpath,
  85. { debug => $options{debug},
  86. section => $section });
  87. my @podfiles = grep { $_ ne $podname } @{$podinfo{names}};
  88. my $updir = updir();
  89. my $name = uc $podname;
  90. my $suffix =
  91. { man => ".$podinfo{section}".($options{suffix} // ""),
  92. html => ".html" } -> {$options{type}};
  93. my $generate =
  94. { man => <<"_____",
  95. pod2man --name=$name --section=$podinfo{section} --center=OpenSSL --release=$config{version} "$podpath"
  96. _____
  97. html => <<"_____",
  98. pod2html "--podroot=$sourcedir" --htmldir=$updir --podpath=man1:man3:man5:man7 "--infile=$podpath" "--title=$podname" --quiet
  99. _____
  100. } -> {$options{type}};
  101. my $output_dir = catdir($options{destdir}, "man$podinfo{section}");
  102. my $output_file = $podname . $suffix;
  103. my $output_path = catfile($output_dir, $output_file);
  104. if (! $options{remove}) {
  105. my @output;
  106. print STDERR "DEBUG: Processing, using \"$generate\"\n"
  107. if $options{debug};
  108. unless ($options{"dry-run"}) {
  109. @output = `$generate`;
  110. map { s|href="http://man\.he\.net/(man\d/[^"]+)(?:\.html)?"|href="../$1.html"|g; } @output
  111. if $options{type} eq "html";
  112. if ($options{type} eq "man") {
  113. # Because some *roff parsers are more strict than
  114. # others, multiple lines in the NAME section must
  115. # be merged into one.
  116. my $in_name = 0;
  117. my $name_line = "";
  118. my @newoutput = ();
  119. foreach (@output) {
  120. if ($in_name) {
  121. if (/^\.SH "/) {
  122. $in_name = 0;
  123. push @newoutput, $name_line."\n";
  124. } else {
  125. chomp (my $x = $_);
  126. $name_line .= " " if $name_line;
  127. $name_line .= $x;
  128. next;
  129. }
  130. }
  131. if (/^\.SH +"NAME" *$/) {
  132. $in_name = 1;
  133. }
  134. push @newoutput, $_;
  135. }
  136. @output = @newoutput;
  137. }
  138. }
  139. print STDERR "DEBUG: Done processing\n" if $options{debug};
  140. if (! -d $output_dir) {
  141. print STDERR "DEBUG: Creating directory $output_dir\n"
  142. if $options{debug};
  143. unless ($options{"dry-run"}) {
  144. mkpath $output_dir
  145. or die "Trying to create directory $output_dir: $!\n";
  146. }
  147. }
  148. print STDERR "DEBUG: Writing $output_path\n" if $options{debug};
  149. unless ($options{"dry-run"}) {
  150. open my $output_fh, '>', $output_path
  151. or die "Trying to write to $output_path: $!\n";
  152. foreach (@output) {
  153. print $output_fh $_;
  154. }
  155. close $output_fh;
  156. }
  157. print STDERR "DEBUG: Done writing $output_path\n" if $options{debug};
  158. } else {
  159. print STDERR "DEBUG: Removing $output_path\n" if $options{debug};
  160. unless ($options{"dry-run"}) {
  161. while (unlink $output_path) {}
  162. }
  163. }
  164. print "$output_path\n";
  165. foreach (@podfiles) {
  166. my $link_file = $_ . $suffix;
  167. my $link_path = catfile($output_dir, $link_file);
  168. if (! $options{remove}) {
  169. if ($symlink_exists) {
  170. print STDERR "DEBUG: Linking $link_path -> $output_file\n"
  171. if $options{debug};
  172. unless ($options{"dry-run"}) {
  173. symlink $output_file, $link_path;
  174. }
  175. } else {
  176. print STDERR "DEBUG: Copying $output_path to link_path\n"
  177. if $options{debug};
  178. unless ($options{"dry-run"}) {
  179. copy $output_path, $link_path;
  180. }
  181. }
  182. } else {
  183. print STDERR "DEBUG: Removing $link_path\n" if $options{debug};
  184. unless ($options{"dry-run"}) {
  185. while (unlink $link_path) {}
  186. }
  187. }
  188. print "$link_path -> $output_path\n";
  189. }
  190. }
  191. }
  192. }
  193. __END__
  194. =pod
  195. =head1 NAME
  196. process_docs.pl - A script to process OpenSSL docs
  197. =head1 SYNOPSIS
  198. B<process_docs.pl>
  199. [B<--sourcedir>=I<dir>]
  200. B<--destdir>=I<dir>
  201. B<--type>=B<man>|B<html>
  202. [B<--suffix>=I<suffix>]
  203. [B<--remove>]
  204. [B<--dry-run>|B<-n>]
  205. [B<--debug>|B<-D>]
  206. =head1 DESCRIPTION
  207. This script looks for .pod files in the subdirectories 'apps', 'crypto'
  208. and 'ssl' under the given source directory.
  209. The OpenSSL configuration data file F<configdata.pm> I<must> reside in
  210. the current directory, I<or> perl must have the directory it resides in
  211. in its inclusion array. For the latter variant, a call like this would
  212. work:
  213. perl -I../foo util/process_docs.pl {options ...}
  214. =head1 OPTIONS
  215. =over 4
  216. =item B<--sourcedir>=I<dir>
  217. Top directory where the source files are found.
  218. =item B<--destdir>=I<dir>
  219. Top directory where the resulting files should end up
  220. =item B<--type>=B<man>|B<html>
  221. Type of output to produce. Currently supported are man pages and HTML files.
  222. =item B<--suffix>=I<suffix>
  223. A suffix added to the extension. Only valid with B<--type>=B<man>
  224. =item B<--remove>
  225. Instead of writing the files, remove them.
  226. =item B<--dry-run>|B<-n>
  227. Do not perform any file writing, directory creation or file removal.
  228. =item B<--debug>|B<-D>
  229. Print extra debugging output.
  230. =back
  231. =head1 COPYRIGHT
  232. Copyright 2013-2018 The OpenSSL Project Authors. All Rights Reserved.
  233. Licensed under the Apache License 2.0 (the "License"). You may not use
  234. this file except in compliance with the License. You can obtain a copy
  235. in the file LICENSE in the source distribution or at
  236. https://www.openssl.org/source/license.html
  237. =cut