| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# copyright-update -- Utility to update copyright information in files |
| 4 |
# |
| 5 |
# Copyright |
| 6 |
# |
| 7 |
# Copyright (C) 2000-2025 Jari Aalto |
| 8 |
# |
| 9 |
# License |
| 10 |
# |
| 11 |
# This program is free software; you can redistribute it and/or modify |
| 12 |
# it under the terms of the GNU General Public License as published by |
| 13 |
# the Free Software Foundation; either version 2 of the License, or |
| 14 |
# (at your option) any later version. |
| 15 |
# |
| 16 |
# This program is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
# GNU General Public License for more details. |
| 20 |
# |
| 21 |
# You should have received a copy of the GNU General Public License |
| 22 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
# |
| 24 |
# Documentation |
| 25 |
# |
| 26 |
# To read manual, start this program with option: --help |
| 27 |
|
| 28 |
# **************************************************************************** |
| 29 |
# |
| 30 |
# Standard perl modules |
| 31 |
# |
| 32 |
# **************************************************************************** |
| 33 |
|
| 34 |
use strict; |
| 35 |
|
| 36 |
use autouse 'Pod::Text' => qw(pod2text); |
| 37 |
use autouse 'Pod::Html' => qw(pod2html); |
| 38 |
|
| 39 |
use English qw(-no_match_vars); |
| 40 |
use Getopt::Long; |
| 41 |
use File::Basename; |
| 42 |
use File::Find; |
| 43 |
use charnames q(:full); |
| 44 |
use Fatal qw(open binmode); |
| 45 |
|
| 46 |
use Encode; |
| 47 |
use Encode::Guess; |
| 48 |
|
| 49 |
IMPORT: # This is just a syntactic sugar: actually no-op |
| 50 |
{ |
| 51 |
# Import following environment variables |
| 52 |
|
| 53 |
use Env; |
| 54 |
use vars qw |
| 55 |
( |
| 56 |
$NAME |
| 57 |
$EMAIL |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
# **************************************************************************** |
| 62 |
# |
| 63 |
# GLOBALS |
| 64 |
# |
| 65 |
# **************************************************************************** |
| 66 |
|
| 67 |
use vars qw($VERSION $URL); |
| 68 |
|
| 69 |
local $OUTPUT_AUTOFLUSH = 1; |
| 70 |
my $LIB = basename $PROGRAM_NAME; |
| 71 |
|
| 72 |
# This is for use of Makefile.PL and ExtUtils::MakeMaker |
| 73 |
# |
| 74 |
# The following variable is updated by custom Emacs setup whenever |
| 75 |
# this file is saved. |
| 76 |
|
| 77 |
my $VERSION = '2025.0404.0924'; |
| 78 |
|
| 79 |
my $DEFAULT_PATH_EXCLUDE = # Matches *only path component |
| 80 |
'(CVS|RCS|\.(bzr|svn|git|darcs|arch|mtn|hg))$' |
| 81 |
; |
| 82 |
|
| 83 |
my $DEFAULT_FILE_EXCLUDE = # Matches *only* file component |
| 84 |
'[#~]$' |
| 85 |
. '|\.[#]' |
| 86 |
. '|\.(s?o|l?a|bin|com|exe|class|elc)$' |
| 87 |
. '|\.(ods|odt|pdf|ppt|xls|rtf)$' |
| 88 |
. '|\.(xpm|jpg|png|gif|tiff|bmp)$' |
| 89 |
. '|(config.guess|config.sub|ltconfig|depcomp|missing|COPYING)$' |
| 90 |
; |
| 91 |
|
| 92 |
# **************************************************************************** |
| 93 |
# |
| 94 |
# DESCRIPTION |
| 95 |
# |
| 96 |
# Set global variables for the program |
| 97 |
# |
| 98 |
# INPUT PARAMETERS |
| 99 |
# |
| 100 |
# none |
| 101 |
# |
| 102 |
# RETURN VALUES |
| 103 |
# |
| 104 |
# none |
| 105 |
# |
| 106 |
# **************************************************************************** |
| 107 |
|
| 108 |
sub Initialize () |
| 109 |
{ |
| 110 |
use vars qw # Declare globals |
| 111 |
( |
| 112 |
$LICENSE |
| 113 |
$CONTACT |
| 114 |
$URL |
| 115 |
|
| 116 |
$PROGNAME |
| 117 |
); |
| 118 |
|
| 119 |
$LICENSE = "GPL-2.0-or-later"; |
| 120 |
$CONTACT = "Jari Aalto"; |
| 121 |
$URL = "https://github.com/jaalto/project--copyright-update"; |
| 122 |
|
| 123 |
$PROGNAME = $LIB; |
| 124 |
} |
| 125 |
|
| 126 |
# **************************************************************************** |
| 127 |
# |
| 128 |
# DESCRIPTION |
| 129 |
# |
| 130 |
# Help function and embedded POD documentation |
| 131 |
# |
| 132 |
# INPUT PARAMETERS |
| 133 |
# |
| 134 |
# none |
| 135 |
# |
| 136 |
# RETURN VALUES |
| 137 |
# |
| 138 |
# none |
| 139 |
# |
| 140 |
# **************************************************************************** |
| 141 |
|
| 142 |
# Perlcritic flags this |
| 143 |
# perldoc Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars |
| 144 |
# ... commented out |
| 145 |
|
| 146 |
# sub Help (;$$); |
| 147 |
|
| 148 |
sub Help |
| 149 |
{ |
| 150 |
my $id = "$LIB.Help"; |
| 151 |
my $type = shift; # optional arg, type |
| 152 |
my $msg = shift; # optional arg, why are we here... |
| 153 |
|
| 154 |
if ($type eq -html) |
| 155 |
{ |
| 156 |
pod2html $PROGRAM_NAME; |
| 157 |
} |
| 158 |
elsif ($type eq -man) |
| 159 |
{ |
| 160 |
eval {require Pod::Man; 1} |
| 161 |
or die "$id: Cannot generate Man: $EVAL_ERROR"; |
| 162 |
|
| 163 |
my %options; |
| 164 |
$options{center} = "User commands"; |
| 165 |
|
| 166 |
my $parser = Pod::Man->new(%options); |
| 167 |
$parser->parse_from_file ($PROGRAM_NAME); |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
system "perl -S pod2text $PROGRAM_NAME" or |
| 172 |
do { 1; } # Ignore error. Do nothing. |
| 173 |
} |
| 174 |
|
| 175 |
defined $msg and print $msg; |
| 176 |
exit 0; |
| 177 |
} |
| 178 |
|
| 179 |
# **************************************************************************** |
| 180 |
# |
| 181 |
# DESCRIPTION |
| 182 |
# |
| 183 |
# Display default excludes. |
| 184 |
# |
| 185 |
# INPUT PARAMETERS |
| 186 |
# |
| 187 |
# None |
| 188 |
# |
| 189 |
# RETURN VALUES |
| 190 |
# |
| 191 |
# None |
| 192 |
# |
| 193 |
# **************************************************************************** |
| 194 |
|
| 195 |
sub HelpExclude () |
| 196 |
{ |
| 197 |
my $id = "$LIB.HelpExclude"; |
| 198 |
|
| 199 |
print "Default path exclude regexp: '$DEFAULT_PATH_EXCLUDE'\n"; |
| 200 |
print "Default file exclude regexp: '$DEFAULT_FILE_EXCLUDE'\n"; |
| 201 |
} |
| 202 |
|
| 203 |
# **************************************************************************** |
| 204 |
# |
| 205 |
# DESCRIPTION |
| 206 |
# |
| 207 |
# Return current year YYYY. |
| 208 |
# |
| 209 |
# INPUT PARAMETERS |
| 210 |
# |
| 211 |
# None |
| 212 |
# |
| 213 |
# RETURN VALUES |
| 214 |
# |
| 215 |
# number YYYY |
| 216 |
# |
| 217 |
# **************************************************************************** |
| 218 |
|
| 219 |
sub Year () |
| 220 |
{ |
| 221 |
my $id = "$LIB.Year"; |
| 222 |
1900 + (localtime time)[5]; |
| 223 |
} |
| 224 |
|
| 225 |
# **************************************************************************** |
| 226 |
# |
| 227 |
# DESCRIPTION |
| 228 |
# |
| 229 |
# Read command line arguments and their parameters. |
| 230 |
# |
| 231 |
# INPUT PARAMETERS |
| 232 |
# |
| 233 |
# None |
| 234 |
# |
| 235 |
# RETURN VALUES |
| 236 |
# |
| 237 |
# Globally set options. |
| 238 |
# |
| 239 |
# **************************************************************************** |
| 240 |
|
| 241 |
sub HandleCommandLineArgs () |
| 242 |
{ |
| 243 |
my $id = "$LIB.HandleCommandLineArgs"; |
| 244 |
|
| 245 |
# Declare globals |
| 246 |
use vars qw |
| 247 |
( |
| 248 |
$TEST |
| 249 |
$VERB |
| 250 |
$DEBUG |
| 251 |
|
| 252 |
$YEAR |
| 253 |
$OPT_AUTOMATIC |
| 254 |
@OPT_FILE_REGEXP_EXCLUDE |
| 255 |
@OPT_FILE_REGEXP_INCLUDE |
| 256 |
$OPT_FSF_ADDRESS |
| 257 |
$OPT_LINE_REGEXP |
| 258 |
$OPT_NO_YEAR |
| 259 |
$OPT_RECURSIVE |
| 260 |
$OPT_REGEXP |
| 261 |
); |
| 262 |
|
| 263 |
Getopt::Long::config(qw |
| 264 |
( |
| 265 |
no_ignore_case |
| 266 |
no_ignore_case_always |
| 267 |
)); |
| 268 |
|
| 269 |
my ($help, $helpMan, $helpHtml, $version); # local variables to function |
| 270 |
my ($helpExclude, $code); |
| 271 |
|
| 272 |
$DEBUG = -1; |
| 273 |
|
| 274 |
GetOptions # Getopt::Long |
| 275 |
( |
| 276 |
"a|auto" => \$OPT_AUTOMATIC |
| 277 |
, "fsf-address" => \$OPT_FSF_ADDRESS |
| 278 |
, "c|code" => \$code |
| 279 |
, "D|debug:i" => \$DEBUG |
| 280 |
, "dry-run" => \$TEST |
| 281 |
, "help-exclude" => \$helpExclude |
| 282 |
, "help-html" => \$helpHtml |
| 283 |
, "help-man" => \$helpMan |
| 284 |
, "h|help" => \$help |
| 285 |
, "include=s" => \@OPT_FILE_REGEXP_INCLUDE |
| 286 |
, "line=s" => \$OPT_LINE_REGEXP |
| 287 |
, "r|recursive" => \$OPT_RECURSIVE |
| 288 |
, "R|regexp=s" => \$OPT_REGEXP |
| 289 |
, "test" => \$TEST |
| 290 |
, "v|verbose:i" => \$VERB |
| 291 |
, "V|version" => \$version |
| 292 |
, "year=i" => \$YEAR |
| 293 |
, "Y|no-year" => \$OPT_NO_YEAR |
| 294 |
, "x|exclude=s" => \@OPT_FILE_REGEXP_EXCLUDE |
| 295 |
); |
| 296 |
|
| 297 |
$version and die "$VERSION $CONTACT $LICENSE $URL\n"; |
| 298 |
$helpExclude and HelpExclude(); |
| 299 |
$help and Help(); |
| 300 |
$helpMan and Help(-man); |
| 301 |
$helpHtml and Help(-html); |
| 302 |
|
| 303 |
$DEBUG = 1 if $DEBUG == 0; |
| 304 |
$DEBUG = 0 if $DEBUG < 0; |
| 305 |
|
| 306 |
$YEAR = Year() unless defined $YEAR; |
| 307 |
|
| 308 |
unless ($YEAR =~ m,^\d{4}$,) |
| 309 |
{ |
| 310 |
die "$id: Option --year must be given with four digits [$YEAR]"; |
| 311 |
} |
| 312 |
|
| 313 |
if (defined $VERB and $VERB == 0) |
| 314 |
{ |
| 315 |
$VERB = 1; |
| 316 |
} |
| 317 |
|
| 318 |
if ($code) |
| 319 |
{ |
| 320 |
push @OPT_FILE_REGEXP_INCLUDE, |
| 321 |
'\.([Cch]|cc|hh|cpp|c\+\+|hpp|p[y]l|sh|rb)'; |
| 322 |
} |
| 323 |
|
| 324 |
$VERB = 1 if $TEST and $VERB == 0; |
| 325 |
$VERB = 5 if $DEBUG; |
| 326 |
|
| 327 |
if ( $OPT_LINE_REGEXP and $OPT_AUTOMATIC ) |
| 328 |
{ |
| 329 |
die "$id: option --auto cannot be used togethet with --line"; |
| 330 |
} |
| 331 |
|
| 332 |
if ($OPT_AUTOMATIC) |
| 333 |
{ |
| 334 |
if ($NAME) |
| 335 |
{ |
| 336 |
local $ARG = $NAME; |
| 337 |
|
| 338 |
if (/^([[:lower:]-]+) +([[:lower:]-]+)/i) |
| 339 |
{ |
| 340 |
$VERB > 1 and print "$id: Using NAME: $NAME\n"; |
| 341 |
$OPT_LINE_REGEXP = "${1}[ \\t]+$2"; |
| 342 |
} |
| 343 |
else |
| 344 |
{ |
| 345 |
$VERB and print "$id: WARN: NAME not recognized: '$NAME'\n"; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if (not $OPT_LINE_REGEXP and $EMAIL) |
| 350 |
{ |
| 351 |
local $ARG = $EMAIL; |
| 352 |
|
| 353 |
if ( /^([[:lower:]-]+)[.]([[:lower:]-]+)@/i ) |
| 354 |
{ |
| 355 |
$VERB > 1 and print "$id: Using EMAIL: $EMAIL\n"; |
| 356 |
$OPT_LINE_REGEXP = "${1}[ \\t]+$2"; |
| 357 |
} |
| 358 |
else |
| 359 |
{ |
| 360 |
$VERB and print "$id: WARN: EMAIL not recognized: '$EMAIL'\n"; |
| 361 |
} |
| 362 |
|
| 363 |
unless ($OPT_LINE_REGEXP) |
| 364 |
{ |
| 365 |
die "$id: information for option --auto missing"; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
$VERB > 1 and print "$id: automatic preset of --line '$OPT_LINE_REGEXP'\n"; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
# **************************************************************************** |
| 374 |
# |
| 375 |
# DESCRIPTION |
| 376 |
# |
| 377 |
# Change FSF Address |
| 378 |
# |
| 379 |
# INPUT PARAMETERS |
| 380 |
# |
| 381 |
# $content File content. |
| 382 |
# |
| 383 |
# RETURN VALUES |
| 384 |
# |
| 385 |
# $string File content or empty if no chnages. |
| 386 |
# |
| 387 |
# **************************************************************************** |
| 388 |
|
| 389 |
sub FsfAddress ($ ; $) |
| 390 |
{ |
| 391 |
my $id = "$LIB.FsfAddress"; |
| 392 |
local $ARG = shift; |
| 393 |
my $file = shift; |
| 394 |
|
| 395 |
my $X = $DEBUG ? "$id: " : ""; |
| 396 |
my $done; |
| 397 |
|
| 398 |
# Substitute operation in s{}{} |
| 399 |
|
| 400 |
my $re = qr{ |
| 401 |
^([^\r\n]*)You \s+ should \s+ have \s+ received |
| 402 |
.*? (write \s+ to | contact .*? Foundation) |
| 403 |
.*? USA[ \t.]* |
| 404 |
}x; |
| 405 |
|
| 406 |
s |
| 407 |
{ |
| 408 |
$re |
| 409 |
} |
| 410 |
{$1You should have received a copy of the GNU General Public License\n$1along with this program. If not, see <http://www.gnu.org/licenses/>.}smix |
| 411 |
and $done++; |
| 412 |
|
| 413 |
$ARG if $done; |
| 414 |
} |
| 415 |
|
| 416 |
# **************************************************************************** |
| 417 |
# |
| 418 |
# DESCRIPTION |
| 419 |
# |
| 420 |
# Handle Single file |
| 421 |
# |
| 422 |
# INPUT PARAMETERS |
| 423 |
# |
| 424 |
# %hash -file => [filename list] |
| 425 |
# -regexp => Regexp to match file content. |
| 426 |
# If regexp is not found in file, file is not |
| 427 |
# handled. |
| 428 |
# |
| 429 |
# RETURN VALUES |
| 430 |
# |
| 431 |
# none |
| 432 |
# |
| 433 |
# **************************************************************************** |
| 434 |
|
| 435 |
sub HandleFile (%) |
| 436 |
{ |
| 437 |
my $id = "$LIB.HandleFile"; |
| 438 |
my %arg = @ARG; |
| 439 |
|
| 440 |
my $X = $DEBUG ? "$id: " : ""; |
| 441 |
my @files = @{ $arg{-file} }; |
| 442 |
my $regexp = $arg{-regexp}; |
| 443 |
my $linere = $arg{-line}; |
| 444 |
|
| 445 |
unless (@files) |
| 446 |
{ |
| 447 |
warn "$id: -file argument is empty: ", $arg{-file}; |
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
$DEBUG and print "$id: -file [@files], ", |
| 452 |
"-regexp '$regexp' ", |
| 453 |
"-line '$linere'\n" |
| 454 |
; |
| 455 |
|
| 456 |
my $ffile; |
| 457 |
|
| 458 |
# "Creating Nested Functions" |
| 459 |
# https://www.perlmonks.org/?node_id=696592 |
| 460 |
|
| 461 |
my $Print; |
| 462 |
$Print = sub (@) |
| 463 |
{ |
| 464 |
print "${X}$ffile: ", @_, "\n"; |
| 465 |
}; |
| 466 |
|
| 467 |
local $ARG = ""; |
| 468 |
|
| 469 |
for my $file (@files) |
| 470 |
{ |
| 471 |
$ffile = $file; # For Print() |
| 472 |
|
| 473 |
$DEBUG and print "$id: $file: open\n"; |
| 474 |
|
| 475 |
# ..................................................... read ... |
| 476 |
|
| 477 |
{ |
| 478 |
my $FILE; |
| 479 |
|
| 480 |
unless (open my $FILE, "<", $file) |
| 481 |
{ |
| 482 |
$VERB and $Print->("ERROR: cannot open"); |
| 483 |
next; |
| 484 |
} |
| 485 |
else |
| 486 |
{ |
| 487 |
binmode $FILE; |
| 488 |
local $INPUT_RECORD_SEPARATOR = undef; |
| 489 |
$ARG = <$FILE>; |
| 490 |
close $FILE or warn "Close $file error $ERRNO"; |
| 491 |
|
| 492 |
unless (/\w/) |
| 493 |
{ |
| 494 |
$VERB and $Print->("WARN: empty file"); |
| 495 |
return; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
if ($regexp) |
| 501 |
{ |
| 502 |
unless (/$regexp/o) |
| 503 |
{ |
| 504 |
$VERB and $Print->("WARN: failed regexp check: $regexp"); |
| 505 |
next; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
my $done; |
| 510 |
my $msg = $TEST ? "Would change" : "Changed"; |
| 511 |
|
| 512 |
if ($OPT_FSF_ADDRESS and (my $fsf = FsfAddress $ARG, $file)) |
| 513 |
{ |
| 514 |
$Print->("$msg FSF address to URL"); |
| 515 |
$ARG = $fsf; |
| 516 |
$done++; |
| 517 |
} |
| 518 |
|
| 519 |
# Perl Unicode also would accpt x{a9}/, but \N{} is more readable |
| 520 |
# http://en.wikipedia.org/wiki/Copyright_symbol |
| 521 |
|
| 522 |
my $ch = encode("UTF-8", "\N{COPYRIGHT SIGN}"); |
| 523 |
my $sign = '(?:' . $ch . '|\([Cc]\))'; |
| 524 |
my $yyyy = qr/\d{4}/; |
| 525 |
my $copy = qr/(?:(?i)Copyright):?[ \t]+/ . $sign . qr/[ \t]+/ . $yyyy; |
| 526 |
|
| 527 |
# In manual pages the hyphen is quoted as: |
| 528 |
# Copyright (C) YYYY\-YYYY |
| 529 |
|
| 530 |
my $repeat = qr/\\?-/; |
| 531 |
|
| 532 |
# If we find the regexp, then check if YEAR is different |
| 533 |
# and finally do substitution. |
| 534 |
# |
| 535 |
# If everything went ok, replace file. |
| 536 |
|
| 537 |
my $y; |
| 538 |
|
| 539 |
if (/$copy$repeat($yyyy)/oi) |
| 540 |
{ |
| 541 |
$y = $1; |
| 542 |
} |
| 543 |
else |
| 544 |
{ |
| 545 |
$VERB > 1 and $Print->("No Copyright line"); |
| 546 |
$DEBUG > 2 and $Print->("$id: Match regexp: $copy$repeat($yyyy)\n"); |
| 547 |
} |
| 548 |
|
| 549 |
$y = "" if $OPT_NO_YEAR; |
| 550 |
|
| 551 |
if ($y and $y eq $YEAR) |
| 552 |
{ |
| 553 |
$VERB > 2 and $Print->("Copyright is already $YEAR"); |
| 554 |
$y = ""; |
| 555 |
} |
| 556 |
|
| 557 |
unless ($OPT_NO_YEAR) |
| 558 |
{ |
| 559 |
my $i = 0; |
| 560 |
|
| 561 |
if ($linere) |
| 562 |
{ |
| 563 |
if ($DEBUG > 1) |
| 564 |
{ |
| 565 |
warn "s/(?:$linere).*\\K($copy$repeat)($yyyy)/\${1}$YEAR/gmi\n"; |
| 566 |
warn "s/($copy$repeat)$yyyy(.*$linere)/\${1}$YEAR\${2}/gmi\n"; |
| 567 |
} |
| 568 |
|
| 569 |
s/(?:$linere).*\K($copy$repeat)$yyyy/$1$YEAR/gmi and $i++; |
| 570 |
s/($copy$repeat)$yyyy(.*$linere)/$1$YEAR$2/gmi and $i++; |
| 571 |
|
| 572 |
} |
| 573 |
else |
| 574 |
{ |
| 575 |
s/($copy$repeat)$yyyy/$1$YEAR/gmi and $i++; |
| 576 |
} |
| 577 |
|
| 578 |
$done += $i; |
| 579 |
$DEBUG and $i and print "$id: matches --line '$linere'\n"; |
| 580 |
|
| 581 |
$y = "" unless $i; |
| 582 |
} |
| 583 |
|
| 584 |
$VERB and $y and $Print->("$msg $y => $YEAR"); |
| 585 |
|
| 586 |
$TEST and next; |
| 587 |
not $done and next; |
| 588 |
|
| 589 |
my $FILE; |
| 590 |
|
| 591 |
unless (open $FILE, ">", $file) |
| 592 |
{ |
| 593 |
$Print->("ERROR: Cannot open for write"); |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
$VERB > 2 and $Print->("wrote"); |
| 598 |
|
| 599 |
binmode $FILE; |
| 600 |
print $FILE $ARG; |
| 601 |
close $FILE or warn "Close $file error $ERRNO"; |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
# **************************************************************************** |
| 607 |
# |
| 608 |
# DESCRIPTION |
| 609 |
# |
| 610 |
# Check if FILE matches exclude regexps. |
| 611 |
# |
| 612 |
# INPUT PARAMETERS |
| 613 |
# |
| 614 |
# $ Filename |
| 615 |
# |
| 616 |
# RETURN VALUES |
| 617 |
# |
| 618 |
# true File in exclude list |
| 619 |
# false File NOT in exclude list |
| 620 |
# |
| 621 |
# **************************************************************************** |
| 622 |
|
| 623 |
sub IsExclude ($) |
| 624 |
{ |
| 625 |
my $id = "$LIB.IsExclude"; |
| 626 |
local $ARG = shift; |
| 627 |
|
| 628 |
scalar @OPT_FILE_REGEXP_EXCLUDE or return 0; |
| 629 |
|
| 630 |
for my $re (@OPT_FILE_REGEXP_EXCLUDE) |
| 631 |
{ |
| 632 |
|
| 633 |
if (/$re/) |
| 634 |
{ |
| 635 |
$VERB > 2 and print "$id: '$re' matches: $ARG\n"; |
| 636 |
return 1 |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
# **************************************************************************** |
| 644 |
# |
| 645 |
# DESCRIPTION |
| 646 |
# |
| 647 |
# Check if FILE matches include regexps. |
| 648 |
# |
| 649 |
# INPUT PARAMETERS |
| 650 |
# |
| 651 |
# $ Filename |
| 652 |
# |
| 653 |
# RETURN VALUES |
| 654 |
# |
| 655 |
# true File in include list |
| 656 |
# false File NOT in include list |
| 657 |
# |
| 658 |
# **************************************************************************** |
| 659 |
|
| 660 |
sub IsInclude ($) |
| 661 |
{ |
| 662 |
my $id = "$LIB.IsInclude"; |
| 663 |
local $ARG = shift; |
| 664 |
|
| 665 |
scalar @OPT_FILE_REGEXP_INCLUDE or return 1; |
| 666 |
|
| 667 |
for my $re (@OPT_FILE_REGEXP_INCLUDE) |
| 668 |
{ |
| 669 |
|
| 670 |
if (/$re/) |
| 671 |
{ |
| 672 |
$VERB > 2 and print "$id: '$re' matches: $ARG\n"; |
| 673 |
return 1 |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
# **************************************************************************** |
| 681 |
# |
| 682 |
# DESCRIPTION |
| 683 |
# |
| 684 |
# Recursively find out all files and change their content. |
| 685 |
# |
| 686 |
# INPUT PARAMETERS |
| 687 |
# |
| 688 |
# None. This function is called from File::FInd.pm library |
| 689 |
# |
| 690 |
# RETURN VALUES |
| 691 |
# |
| 692 |
# None. |
| 693 |
# |
| 694 |
# **************************************************************************** |
| 695 |
|
| 696 |
sub wanted () |
| 697 |
{ |
| 698 |
my $id = "$LIB.wanted"; |
| 699 |
|
| 700 |
my $dir = $File::Find::dir; |
| 701 |
my $file = $File::Find::name; # complete path |
| 702 |
|
| 703 |
if ($dir =~ m,$DEFAULT_PATH_EXCLUDE,o) |
| 704 |
{ |
| 705 |
$File::Find::prune = 1; |
| 706 |
$DEBUG and print "$id: DEfault path exclude: $dir\n"; |
| 707 |
return; |
| 708 |
} |
| 709 |
|
| 710 |
if ($file =~ m,$DEFAULT_FILE_EXCLUDE,o) |
| 711 |
{ |
| 712 |
$DEBUG and print "$id: Default file exclude: $file\n"; |
| 713 |
return; |
| 714 |
} |
| 715 |
|
| 716 |
if (-f) |
| 717 |
{ |
| 718 |
if ($VERB > 3) |
| 719 |
{ |
| 720 |
print "$id: $file\n"; |
| 721 |
} |
| 722 |
|
| 723 |
unless (-T) |
| 724 |
{ |
| 725 |
$DEBUG and |
| 726 |
print "$id: Exclude binary file (internal test): $file\n"; |
| 727 |
} |
| 728 |
|
| 729 |
IsInclude $file or return; |
| 730 |
IsExclude $file and return; |
| 731 |
|
| 732 |
HandleFile -file => [$file], |
| 733 |
-line => $OPT_LINE_REGEXP, |
| 734 |
-regexp => $OPT_REGEXP; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
# **************************************************************************** |
| 739 |
# |
| 740 |
# DESCRIPTION |
| 741 |
# |
| 742 |
# Expand files in list. Win32 support |
| 743 |
# |
| 744 |
# INPUT PARAMETERS |
| 745 |
# |
| 746 |
# @ list of file glob patterns. |
| 747 |
# |
| 748 |
# RETURN VALUES |
| 749 |
# |
| 750 |
# @ list of filenames |
| 751 |
# |
| 752 |
# **************************************************************************** |
| 753 |
|
| 754 |
sub FileGlobs (@) |
| 755 |
{ |
| 756 |
my $id = "$LIB.FileGlobs"; |
| 757 |
my @list = @ARG; |
| 758 |
not @list and die "$id: No files to expand. Argument list is empty."; |
| 759 |
|
| 760 |
my @files; |
| 761 |
|
| 762 |
for my $glob (@list) |
| 763 |
{ |
| 764 |
# Win32 can't expand "*". We must do it here. |
| 765 |
# Grep only FILES, not directories. |
| 766 |
|
| 767 |
push @files, grep {-f} glob $glob; |
| 768 |
} |
| 769 |
|
| 770 |
$DEBUG and print "$id: RETURN [@files]\n"; |
| 771 |
@files; |
| 772 |
} |
| 773 |
|
| 774 |
# **************************************************************************** |
| 775 |
# |
| 776 |
# DESCRIPTION |
| 777 |
# |
| 778 |
# Main function |
| 779 |
# |
| 780 |
# INPUT PARAMETERS |
| 781 |
# |
| 782 |
# None |
| 783 |
# |
| 784 |
# RETURN VALUES |
| 785 |
# |
| 786 |
# None |
| 787 |
# |
| 788 |
# **************************************************************************** |
| 789 |
|
| 790 |
sub Main () |
| 791 |
{ |
| 792 |
my $id = "$LIB.Main"; |
| 793 |
|
| 794 |
grep /-D|--debug/, @ARGV and print "$id: ARGV [@ARGV]\n"; |
| 795 |
|
| 796 |
Initialize(); |
| 797 |
HandleCommandLineArgs(); |
| 798 |
|
| 799 |
unless (scalar @ARGV) |
| 800 |
{ |
| 801 |
die "What files to change? See --help."; |
| 802 |
} |
| 803 |
|
| 804 |
$DEBUG and print "$id: ARGV files or paths [@ARGV]\n"; |
| 805 |
|
| 806 |
# .......................................... expand command line ... |
| 807 |
|
| 808 |
|
| 809 |
if ($OPT_RECURSIVE) |
| 810 |
{ |
| 811 |
find({wanted => \&wanted, no_chdir => 1}, @ARGV); |
| 812 |
} |
| 813 |
else |
| 814 |
{ |
| 815 |
my @files = FileGlobs @ARGV; |
| 816 |
|
| 817 |
unless (@files) |
| 818 |
{ |
| 819 |
$VERB and warn "[WARN] No files matching glob(s): @ARGV\n"; |
| 820 |
return; |
| 821 |
} |
| 822 |
|
| 823 |
HandleFile -file => [@files], |
| 824 |
-line => $OPT_LINE_REGEXP, |
| 825 |
-regexp => $OPT_REGEXP; |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
Main(); |
| 830 |
|
| 831 |
__END__ |
| 832 |
|
| 833 |
=pod |
| 834 |
|
| 835 |
=head1 NAME |
| 836 |
|
| 837 |
copyright-update - Update Copyright information in files |
| 838 |
|
| 839 |
=head1 USAGE |
| 840 |
|
| 841 |
copyright-update [options] FILE [FILE ...] |
| 842 |
copyright-update [options] --recursive PATH [PATH ...] |
| 843 |
|
| 844 |
=head1 DESCRIPTION |
| 845 |
|
| 846 |
Update the copyright information in a set of files, possibly |
| 847 |
recursively, matching content criteria. The update affects the copyright |
| 848 |
year, GPL address information, etc. |
| 849 |
|
| 850 |
The line must contain the word "Copyright," a space, three characters '(C)' |
| 851 |
(or a U+00A9 UTF copyright sign), a space, and the range of |
| 852 |
years. A varying amount of spaces and tabs are permitted, but there must |
| 853 |
be no spaces around the dash character in YEAR-YEAR. Examples: |
| 854 |
|
| 855 |
A whitespace, or multiple, required |
| 856 |
| | No space between years |
| 857 |
| | | |
| 858 |
Copyright (C) YYYY-YYYY |
| 859 |
Copyright: (C) YYYY-YYYY |
| 860 |
| |
| 861 |
A colon is optional |
| 862 |
|
| 863 |
By default, certain files and paths are always ignored, such as version |
| 864 |
control directories, backup files, object files, and binary files |
| 865 |
according to Perl's C<-T> file test. Run the program with the option |
| 866 |
B<--help-exclude> to see the excluded path list. |
| 867 |
|
| 868 |
=head1 REQUIRED ARGUMENTS |
| 869 |
|
| 870 |
Either FILE or PATH with C<--recursive> option. |
| 871 |
|
| 872 |
=head1 OPTIONS |
| 873 |
|
| 874 |
=head2 Run options |
| 875 |
|
| 876 |
=over 4 |
| 877 |
|
| 878 |
=item B<-a, --auto> |
| 879 |
|
| 880 |
In automatic mode, the author's name is read from the environment variable |
| 881 |
NAME, and only lines matching 'Copyright.*$NAME' are affected. If NAME |
| 882 |
is not set, the information is read from EMAIL. |
| 883 |
|
| 884 |
If neither NAME nor EMAIL exists in the environment, or if their values |
| 885 |
are not in a recognizable format (see section ENVIRONMENT), this |
| 886 |
option does nothing. |
| 887 |
|
| 888 |
This option effectively tries to preset a value for the B<--line> option. |
| 889 |
|
| 890 |
=item B<-c, --code> |
| 891 |
|
| 892 |
Preset B<--include> to match common code files: .c, .h, .cc, .hh, .cpp, |
| 893 |
.c++, .hpp, .C, .pl, .py, .sh, and .rb. |
| 894 |
|
| 895 |
=item B<-D, --debug LEVEL> |
| 896 |
|
| 897 |
Turn on debugging. LEVEL can be in the range of 0-10. |
| 898 |
|
| 899 |
=item B<-f, --fsf-address> |
| 900 |
|
| 901 |
Change license paragraphs that refer to the FSF address. The FSF's current |
| 902 |
recommendation is to redirect inquiries about licenses to their URL. |
| 903 |
|
| 904 |
Effectively replace the paragraphs: |
| 905 |
|
| 906 |
You should have received a copy of the GNU General Public License |
| 907 |
along with this program; if not, write to the Free Software |
| 908 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 909 |
|
| 910 |
You should have received a copy of the GNU General Public License |
| 911 |
along with this package; if not, write to the Free Software |
| 912 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 913 |
|
| 914 |
With this: |
| 915 |
|
| 916 |
You should have received a copy of the GNU General Public License |
| 917 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 918 |
|
| 919 |
=item B<-i, --include REGEXP> |
| 920 |
|
| 921 |
Include files matching regexp. The match is done against whole path. |
| 922 |
|
| 923 |
If this option is not supplied, every file is automatically included. |
| 924 |
The matches can be further filtered by using option B<--exclude>. |
| 925 |
|
| 926 |
This option can be used multiple times. See also option B<--code>. |
| 927 |
|
| 928 |
=item B<-l, --line REGEXP> |
| 929 |
|
| 930 |
Change only lines which match REGEXP. The match is case-insensitive. |
| 931 |
|
| 932 |
=item B<-r, --recursive> |
| 933 |
|
| 934 |
Recursively search all directories given in command line. |
| 935 |
|
| 936 |
=item B<-R, --regexp REGEXP> |
| 937 |
|
| 938 |
Change only files whose content matches REGEXP. The file is read |
| 939 |
in as a one big string so that it's possible to match using Perl |
| 940 |
regular expressions across the whole file. An example: |
| 941 |
C<'(?smi)This.*multi.*line.*match'>. See C<perlre(1)> for more |
| 942 |
information about C<'smi'> and other modifiers. |
| 943 |
|
| 944 |
This option can be used as a preliminary I<Content criteria>, to |
| 945 |
select files before B<--line> option finds the correct Copyright |
| 946 |
lines. |
| 947 |
|
| 948 |
=item B<-t, --test, --dry-run> |
| 949 |
|
| 950 |
Run in test mode. Show what would happen. No files are changed. |
| 951 |
|
| 952 |
=item B<-v, --verbose LEVEL> |
| 953 |
|
| 954 |
Print informational messages. Increase numeric LEVEL for more |
| 955 |
verbosity. |
| 956 |
|
| 957 |
=item B<-x, --exclude REGEXP> |
| 958 |
|
| 959 |
Ignore files matching regexp. The match is done against whole path. |
| 960 |
The option can be used multiple times. |
| 961 |
|
| 962 |
This option is applied after possible B<--include> matches. |
| 963 |
|
| 964 |
=item B<-y, --year YEAR> |
| 965 |
|
| 966 |
Update files using YEAR. Value must be four digits. |
| 967 |
|
| 968 |
The default operation is to use currect year, so this option is not |
| 969 |
necessary in normal use. Use case: you plan to take a long holiday |
| 970 |
before year's end and want to update the next year's value beforehand. |
| 971 |
|
| 972 |
=item B<-Y, --no-year> |
| 973 |
|
| 974 |
Disable updating year. |
| 975 |
|
| 976 |
=back |
| 977 |
|
| 978 |
=head2 Miscellaneus options |
| 979 |
|
| 980 |
=over 4 |
| 981 |
|
| 982 |
=item B<-h, --help> |
| 983 |
|
| 984 |
Display help. |
| 985 |
|
| 986 |
=item B<--help-exclude> |
| 987 |
|
| 988 |
Display default values used to exclude paths and files. |
| 989 |
|
| 990 |
=item B<--help-html> |
| 991 |
|
| 992 |
Display help in HTML format. |
| 993 |
|
| 994 |
=item B<--help-man> |
| 995 |
|
| 996 |
Display help in manual page C<man(1)> format. |
| 997 |
|
| 998 |
=item B<-V, --version> |
| 999 |
|
| 1000 |
Display contact and version information. |
| 1001 |
|
| 1002 |
=back |
| 1003 |
|
| 1004 |
=head1 EXAMPLES |
| 1005 |
|
| 1006 |
The primary use is to update files to reflect current year; option |
| 1007 |
B<--year> is implicit: |
| 1008 |
|
| 1009 |
copyright-update * |
| 1010 |
|
| 1011 |
To test what would happen: |
| 1012 |
|
| 1013 |
copyright-update --verbose 1 --test * |
| 1014 |
|
| 1015 |
Update only C-code file: |
| 1016 |
|
| 1017 |
copyright-update --verbose 1 --include '\.[ch]$' --recursive . |
| 1018 |
|
| 1019 |
It is possible to restrict updating files recursively to only those |
| 1020 |
files whose content match regexp. Here, the lines affected are those |
| 1021 |
that match B<--line> regular expression. |
| 1022 |
|
| 1023 |
copyright-update \ |
| 1024 |
--regexp '(?i)Author:.*John.*Doe' \ |
| 1025 |
--line '\bFoo\b' \ |
| 1026 |
--ignore '\.(bak|bup|[~#]])$' \ |
| 1027 |
--verbose 1 \ |
| 1028 |
--year 2013 \ |
| 1029 |
--test \ |
| 1030 |
--recursive \ |
| 1031 |
. |
| 1032 |
|
| 1033 |
=head1 TROUBLESHOOTING |
| 1034 |
|
| 1035 |
See option B<--debug>. |
| 1036 |
|
| 1037 |
=head1 ENVIRONMENT |
| 1038 |
|
| 1039 |
=over 4 |
| 1040 |
|
| 1041 |
=item EMAIL |
| 1042 |
|
| 1043 |
In the form "[email protected]"; that is, without the |
| 1044 |
angles <>. If set, the value is used in option B<--auto> only if |
| 1045 |
environment variable NAME is not set. |
| 1046 |
|
| 1047 |
In order to find first and lastname, the localpart in email address |
| 1048 |
must match case insensitive regexp C<'^[a-z-]+\.[a-z-]+@'>: |
| 1049 |
|
| 1050 |
[email protected] Not used, not in form First.Last |
| 1051 |
+------ +---------- |
| 1052 |
|
| 1053 |
[email protected] Not used, likewise |
| 1054 |
+---------------- +---------- |
| 1055 |
| | |
| 1056 |
Localpart Domain part |
| 1057 |
|
| 1058 |
=item NAME |
| 1059 |
|
| 1060 |
In the form "Firstname Lastname". If set, the value is used in option |
| 1061 |
B<--auto>. |
| 1062 |
|
| 1063 |
=back |
| 1064 |
|
| 1065 |
=head1 DIAGNOSTICS |
| 1066 |
|
| 1067 |
See option C<--debug LEVEL> |
| 1068 |
|
| 1069 |
=head1 EXIT STATUS |
| 1070 |
|
| 1071 |
Value 0 on normal exit and non-zero on error. |
| 1072 |
|
| 1073 |
=head1 CONFIGURATION |
| 1074 |
|
| 1075 |
None. |
| 1076 |
|
| 1077 |
=head1 DEPENDENCIES |
| 1078 |
|
| 1079 |
None. Uses only standard Perl modules. |
| 1080 |
|
| 1081 |
=head1 INCOMPATIBILITIES |
| 1082 |
|
| 1083 |
None. |
| 1084 |
|
| 1085 |
=head1 FILES |
| 1086 |
|
| 1087 |
None. |
| 1088 |
|
| 1089 |
=head1 SEE ALSO |
| 1090 |
|
| 1091 |
licensecheck(1) |
| 1092 |
|
| 1093 |
=head1 BUGS AND LIMITATIONS |
| 1094 |
|
| 1095 |
The Copyright stanzas searched with option B<--fsf-address> must be |
| 1096 |
exactly like in the FSF recommendation in order for them to be |
| 1097 |
noticed. See STANDARDS. |
| 1098 |
|
| 1099 |
=head1 STANDARDS |
| 1100 |
|
| 1101 |
C<How to use GNU licenses for your own software> |
| 1102 |
http://www.gnu.org/copyleft/gpl-howto.html |
| 1103 |
|
| 1104 |
C<Information for maintainers of GNU software (6.5 Copyright Notices)> |
| 1105 |
http://www.gnu.org/prep/maintain/html_node/Copyright-Notices.html |
| 1106 |
|
| 1107 |
C<Information for maintainers of GNU software (6.6 License Notices)> |
| 1108 |
http://www.gnu.org/prep/maintain/html_node/License-Notices.html#License-Notices |
| 1109 |
|
| 1110 |
=head1 AVAILABILITY |
| 1111 |
|
| 1112 |
Homepage is at https://github.com/jaalto/project--copyright-update |
| 1113 |
|
| 1114 |
=head1 AUTHOR |
| 1115 |
|
| 1116 |
Jari Aalto |
| 1117 |
|
| 1118 |
=head1 LICENSE AND COPYRIGHT |
| 1119 |
|
| 1120 |
Copyright (C) 2000-2025 Jari Aalto |
| 1121 |
|
| 1122 |
This program is free software; you can redistribute and/or modify |
| 1123 |
program under the terms of GNU General Public license either version 2 |
| 1124 |
of the License, or (at your option) any later version. |
| 1125 |
For more information, see see <http://www.gnu.org/licenses/>. |
| 1126 |
|
| 1127 |
=cut |