updatemanpages.pl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #!/usr/bin/perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.haxx.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. ###########################################################################
  23. # Update man pages.
  24. use strict;
  25. use warnings;
  26. use Tie::File;
  27. # Data from the command line.
  28. my $curlver = $ARGV[0];
  29. my $curldate = $ARGV[1];
  30. # Directories and extensions.
  31. my @dirlist = ("docs/", "docs/libcurl/", "docs/libcurl/opts/", "tests/");
  32. my @extlist = (".1", ".3");
  33. my @excludelist = ("mk-ca-bundle.1", "template.3");
  34. # Subroutines
  35. sub printargs{
  36. # Print arguments and exit.
  37. print "usage: updatemanpages.pl <version> <date>\n";
  38. exit;
  39. }
  40. sub getthline{
  41. # Process file looking for .TH section.
  42. my $filename = shift;
  43. my $file_handle;
  44. my $file_line;
  45. # Open the file.
  46. open($file_handle, $filename);
  47. # Look for the .TH section, process it into an array,
  48. # modify it and write to file.
  49. tie(my @file_data, 'Tie::File', $filename);
  50. foreach my $file_data_line(@file_data) {
  51. if($file_data_line =~ /^.TH/) {
  52. $file_line = $file_data_line;
  53. last;
  54. }
  55. }
  56. # Close the file.
  57. close($file_handle);
  58. return $file_line;
  59. }
  60. sub extractth{
  61. # Extract .TH section as an array.
  62. my $input = shift;
  63. # Split the line into an array.
  64. my @tharray;
  65. my $inputsize = length($input);
  66. my $inputcurrent = "";
  67. my $quotemode = 0;
  68. for(my $inputseek = 0; $inputseek < $inputsize; $inputseek++) {
  69. if(substr($input, $inputseek, 1) eq " " && $quotemode eq 0) {
  70. push(@tharray, $inputcurrent);
  71. $inputcurrent = "";
  72. next;
  73. }
  74. $inputcurrent = $inputcurrent . substr($input, $inputseek, 1);
  75. if(substr($input, $inputseek, 1) eq "\"") {
  76. if($quotemode eq 0) {
  77. $quotemode = 1;
  78. }
  79. else {
  80. $quotemode = 0;
  81. }
  82. }
  83. }
  84. if($inputcurrent ne "") {
  85. push(@tharray, $inputcurrent);
  86. }
  87. return @tharray;
  88. }
  89. sub getdate{
  90. # Get the date from the .TH section.
  91. my $filename = shift;
  92. my $thline;
  93. my @tharray;
  94. my $date = "";
  95. $thline = getthline($filename);
  96. # Return nothing if there is no .TH section found.
  97. if(!$thline || $thline eq "") {
  98. return "";
  99. }
  100. @tharray = extractth($thline);
  101. # Remove the quotes at the start and end.
  102. $date = substr($tharray[3], 1, -1);
  103. return $date;
  104. }
  105. sub processth{
  106. # Process .TH section.
  107. my $input = shift;
  108. my $date = shift;
  109. # Split the line into an array.
  110. my @tharray = extractth($input);
  111. # Alter the date.
  112. my $itemdate = "\"";
  113. $itemdate .= $date;
  114. $itemdate .= "\"";
  115. $tharray[3] = $itemdate;
  116. # Alter the item version.
  117. my $itemver = $tharray[4];
  118. my $itemname = "";
  119. for(my $itemnameseek = 1;
  120. $itemnameseek < length($itemver);
  121. $itemnameseek++) {
  122. if(substr($itemver, $itemnameseek, 1) eq " " ||
  123. substr($itemver, $itemnameseek, 1) eq "\"") {
  124. last;
  125. }
  126. $itemname .= substr($itemver, $itemnameseek, 1);
  127. }
  128. $itemver = "\"";
  129. $itemver .= $itemname;
  130. $itemver .= " ";
  131. $itemver .= $curlver;
  132. $itemver .= "\"";
  133. $tharray[4] = $itemver;
  134. my $thoutput = "";
  135. foreach my $thvalue (@tharray) {
  136. $thoutput .= $thvalue;
  137. $thoutput .= " ";
  138. }
  139. $thoutput =~ s/\s+$//;
  140. $thoutput .= "\n";
  141. # Return updated string.
  142. return $thoutput;
  143. }
  144. sub processfile{
  145. # Process file looking for .TH section.
  146. my $filename = shift;
  147. my $date = shift;
  148. my $file_handle;
  149. my $file_dist_handle;
  150. my $filename_dist;
  151. # Open a handle for the original file and a second file handle
  152. # for the dist file.
  153. $filename_dist = $filename . ".dist";
  154. open($file_handle, $filename);
  155. open($file_dist_handle, ">" . $filename_dist);
  156. # Look for the .TH section, process it into an array,
  157. # modify it and write to file.
  158. tie(my @file_data, 'Tie::File', $filename);
  159. foreach my $file_data_line (@file_data) {
  160. if($file_data_line =~ /^.TH/) {
  161. my $file_dist_line = processth($file_data_line, $date);
  162. print $file_dist_handle $file_dist_line . "\n";
  163. }
  164. else {
  165. print $file_dist_handle $file_data_line . "\n";
  166. }
  167. }
  168. # Close the file.
  169. close($file_handle);
  170. close($file_dist_handle);
  171. }
  172. # Check that $curlver is set, otherwise print arguments and exit.
  173. if(!$curlver) {
  174. printargs();
  175. }
  176. # Look in each directory.
  177. my $dir_handle;
  178. foreach my $dirname (@dirlist) {
  179. foreach my $extname (@extlist) {
  180. # Go through the directory looking for files ending with
  181. # the current extension.
  182. opendir($dir_handle, $dirname);
  183. my @filelist = grep(/.$extname$/i, readdir($dir_handle));
  184. foreach my $file (@filelist) {
  185. # Skip if file is in exclude list.
  186. if(grep(/^$file$/, @excludelist)) {
  187. next;
  188. }
  189. # Load the file and get the date.
  190. my $filedate;
  191. # Check if dist version exists and load date from that
  192. # file if it does.
  193. if(-e ($dirname . $file . ".dist")) {
  194. $filedate = getdate(($dirname . $file . ".dist"));
  195. }
  196. else {
  197. $filedate = getdate(($dirname . $file));
  198. }
  199. # Skip if value is empty.
  200. if(!$filedate || $filedate eq "") {
  201. next;
  202. }
  203. # Check the man page in the git repository.
  204. my $repodata = `LC_TIME=C git log -1 --date="format:%B %d, %Y" \\
  205. --since="$filedate" $dirname$file | grep ^Date:`;
  206. # If there is output then update the man page
  207. # with the new date/version.
  208. # Process the file if there is output.
  209. if($repodata) {
  210. my $thisdate;
  211. if(!$curldate) {
  212. if($repodata =~ /^Date: +(.*)/) {
  213. $thisdate = $1;
  214. }
  215. else {
  216. print STDERR "Warning: " . ($dirname . $file) . ": found no " .
  217. "date\n";
  218. }
  219. }
  220. else {
  221. $thisdate = $curldate;
  222. }
  223. processfile(($dirname . $file), $thisdate);
  224. print $dirname . $file . " page updated to $thisdate\n";
  225. }
  226. }
  227. closedir($dir_handle);
  228. }
  229. }
  230. __END__
  231. =pod
  232. =head1 updatemanpages.pl
  233. Updates the man pages with the version number and optional date. If the date
  234. isn't provided, the last modified date from git is used.
  235. =head2 USAGE
  236. updatemanpages.pl version [date]
  237. =head3 version
  238. Specifies version (required)
  239. =head3 date
  240. Specifies date (optional)
  241. =head2 SETTINGS
  242. =head3 @dirlist
  243. Specifies the list of directories to look for files in.
  244. =head3 @extlist
  245. Specifies the list of files with extensions to process.
  246. =head3 @excludelist
  247. Specifies the list of files to not process.
  248. =head2 NOTES
  249. This script is used during maketgz.
  250. =cut