123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- #!/usr/bin/env perl
- #***************************************************************************
- # _ _ ____ _
- # Project ___| | | | _ \| |
- # / __| | | | |_) | |
- # | (__| |_| | _ <| |___
- # \___|\___/|_| \_\_____|
- #
- # Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
- #
- # This software is licensed as described in the file COPYING, which
- # you should have received as part of this distribution. The terms
- # are also available at https://curl.haxx.se/docs/copyright.html.
- #
- # You may opt to use, copy, modify, merge, publish, distribute and/or sell
- # copies of the Software, and permit persons to whom the Software is
- # furnished to do so, under the terms of the COPYING file.
- #
- # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
- # KIND, either express or implied.
- #
- ###########################################################################
- # Update man pages.
- use strict;
- use warnings;
- use Tie::File;
- # Data from the command line.
- my $curlver = $ARGV[0];
- my $curldate = $ARGV[1];
- # Directories and extensions.
- my @dirlist = ("docs/", "docs/libcurl/", "docs/libcurl/opts/", "tests/");
- my @extlist = (".1", ".3");
- my @excludelist = ("mk-ca-bundle.1", "template.3");
- # Subroutines
- sub printargs{
- # Print arguments and exit.
- print "usage: updatemanpages.pl <version> <date>\n";
- exit;
- }
- sub getthline{
- # Process file looking for .TH section.
- my $filename = shift;
- my $file_handle;
- my $file_line;
- # Open the file.
- open($file_handle, $filename);
- # Look for the .TH section, process it into an array,
- # modify it and write to file.
- tie(my @file_data, 'Tie::File', $filename);
- foreach my $file_data_line(@file_data) {
- if($file_data_line =~ /^.TH/) {
- $file_line = $file_data_line;
- last;
- }
- }
- # Close the file.
- close($file_handle);
- return $file_line;
- }
- sub extractth{
- # Extract .TH section as an array.
- my $input = shift;
- # Split the line into an array.
- my @tharray;
- my $inputsize = length($input);
- my $inputcurrent = "";
- my $quotemode = 0;
- for(my $inputseek = 0; $inputseek < $inputsize; $inputseek++) {
- if(substr($input, $inputseek, 1) eq " " && $quotemode eq 0) {
- push(@tharray, $inputcurrent);
- $inputcurrent = "";
- next;
- }
- $inputcurrent = $inputcurrent . substr($input, $inputseek, 1);
- if(substr($input, $inputseek, 1) eq "\"") {
- if($quotemode eq 0) {
- $quotemode = 1;
- }
- else {
- $quotemode = 0;
- }
- }
- }
- if($inputcurrent ne "") {
- push(@tharray, $inputcurrent);
- }
- return @tharray;
- }
- sub getdate{
- # Get the date from the .TH section.
- my $filename = shift;
- my $thline;
- my @tharray;
- my $date = "";
- $thline = getthline($filename);
- # Return nothing if there is no .TH section found.
- if(!$thline || $thline eq "") {
- return "";
- }
- @tharray = extractth($thline);
- # Remove the quotes at the start and end.
- $date = substr($tharray[3], 1, -1);
- return $date;
- }
- sub processth{
- # Process .TH section.
- my $input = shift;
- my $date = shift;
- # Split the line into an array.
- my @tharray = extractth($input);
- # Alter the date.
- my $itemdate = "\"";
- $itemdate .= $date;
- $itemdate .= "\"";
- $tharray[3] = $itemdate;
- # Alter the item version.
- my $itemver = $tharray[4];
- my $itemname = "";
- for(my $itemnameseek = 1;
- $itemnameseek < length($itemver);
- $itemnameseek++) {
- if(substr($itemver, $itemnameseek, 1) eq " " ||
- substr($itemver, $itemnameseek, 1) eq "\"") {
- last;
- }
- $itemname .= substr($itemver, $itemnameseek, 1);
- }
- $itemver = "\"";
- $itemver .= $itemname;
- $itemver .= " ";
- $itemver .= $curlver;
- $itemver .= "\"";
- $tharray[4] = $itemver;
- my $thoutput = "";
- foreach my $thvalue (@tharray) {
- $thoutput .= $thvalue;
- $thoutput .= " ";
- }
- $thoutput =~ s/\s+$//;
- $thoutput .= "\n";
- # Return updated string.
- return $thoutput;
- }
- sub processfile{
- # Process file looking for .TH section.
- my $filename = shift;
- my $date = shift;
- my $file_handle;
- my $file_dist_handle;
- my $filename_dist;
- # Open a handle for the original file and a second file handle
- # for the dist file.
- $filename_dist = $filename . ".dist";
- open($file_handle, $filename);
- open($file_dist_handle, ">" . $filename_dist);
- # Look for the .TH section, process it into an array,
- # modify it and write to file.
- tie(my @file_data, 'Tie::File', $filename);
- foreach my $file_data_line (@file_data) {
- if($file_data_line =~ /^.TH/) {
- my $file_dist_line = processth($file_data_line, $date);
- print $file_dist_handle $file_dist_line . "\n";
- }
- else {
- print $file_dist_handle $file_data_line . "\n";
- }
- }
- # Close the file.
- close($file_handle);
- close($file_dist_handle);
- }
- # Check that $curlver is set, otherwise print arguments and exit.
- if(!$curlver) {
- printargs();
- }
- # check to see that the git command works, it requires git 2.6 something
- my $gitcheck = `git log -1 --date="format:%B %d, %Y" $dirlist[0] 2>/dev/null`;
- if(length($gitcheck) < 1) {
- print "git version too old or $dirlist[0] is a bad argument\n";
- exit;
- }
- # Look in each directory.
- my $dir_handle;
- foreach my $dirname (@dirlist) {
- foreach my $extname (@extlist) {
- # Go through the directory looking for files ending with
- # the current extension.
- opendir($dir_handle, $dirname);
- my @filelist = grep(/.$extname$/i, readdir($dir_handle));
- foreach my $file (@filelist) {
- # Skip if file is in exclude list.
- if(grep(/^$file$/, @excludelist)) {
- next;
- }
- # Load the file and get the date.
- my $filedate;
- # Check if dist version exists and load date from that
- # file if it does.
- if(-e ($dirname . $file . ".dist")) {
- $filedate = getdate(($dirname . $file . ".dist"));
- }
- else {
- $filedate = getdate(($dirname . $file));
- }
- # Skip if value is empty.
- if(!$filedate || $filedate eq "") {
- next;
- }
- # Check the man page in the git repository.
- my $repodata = `LC_TIME=C git log -1 --date="format:%B %d, %Y" \\
- --since="$filedate" $dirname$file | grep ^Date:`;
- # If there is output then update the man page
- # with the new date/version.
- # Process the file if there is output.
- if($repodata) {
- my $thisdate;
- if(!$curldate) {
- if($repodata =~ /^Date: +(.*)/) {
- $thisdate = $1;
- }
- else {
- print STDERR "Warning: " . ($dirname . $file) . ": found no " .
- "date\n";
- }
- }
- else {
- $thisdate = $curldate;
- }
- processfile(($dirname . $file), $thisdate);
- print $dirname . $file . " page updated to $thisdate\n";
- }
- }
- closedir($dir_handle);
- }
- }
- __END__
- =pod
- =head1 updatemanpages.pl
- Updates the man pages with the version number and optional date. If the date
- isn't provided, the last modified date from git is used.
- =head2 USAGE
- updatemanpages.pl version [date]
- =head3 version
- Specifies version (required)
- =head3 date
- Specifies date (optional)
- =head2 SETTINGS
- =head3 @dirlist
- Specifies the list of directories to look for files in.
- =head3 @extlist
- Specifies the list of files with extensions to process.
- =head3 @excludelist
- Specifies the list of files to not process.
- =head2 NOTES
- This script is used during maketgz.
- =cut
|