| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
# Copyright 2025 The NASM Authors - All Rights Reserved |
| 4 |
|
| 5 |
# |
| 6 |
# Convert ultra-verbose in-file copyright statements to SPDX tags; |
| 7 |
# strip trailing whitespace while we are at it... |
| 8 |
# |
| 9 |
use strict; |
| 10 |
use integer; |
| 11 |
|
| 12 |
# Strip excessive comment barriers for the purpose of matching |
| 13 |
sub stripcom($) { |
| 14 |
my($l) = @_; |
| 15 |
|
| 16 |
$l =~ s/^(\S+) -+( \*)?$/$1/; |
| 17 |
$l =~ s/^ \* -+( \*\/)$/$1/; # Tail comment in C block |
| 18 |
return $1; |
| 19 |
} |
| 20 |
|
| 21 |
foreach my $file (@ARGV) { |
| 22 |
open(my $in, '<', $file) or die "$0: $file: $!\n"; |
| 23 |
|
| 24 |
my @lines = (); |
| 25 |
my $copy; |
| 26 |
my $ctail; |
| 27 |
my $cpfx; |
| 28 |
my $modified = 0; |
| 29 |
|
| 30 |
while (defined(my $ll = <$in>)) { |
| 31 |
(my $l = $ll) =~ s/\s+$//; |
| 32 |
$modified = 1 if ($l."\n" ne $ll); |
| 33 |
|
| 34 |
if ($l =~ /^(\s*\S+)\s+(Copyright [1-2].*)?$/) { |
| 35 |
$copy = $2; |
| 36 |
$ctail = $cpfx = $1; |
| 37 |
$ctail = ' */' if ($cpfx eq ' *'); # C-style block comment |
| 38 |
} elsif (defined($ctail) && (stripcom($l) eq $ctail)) { |
| 39 |
# End of copyright comment. Walk backwards through the |
| 40 |
# lines looking for the beginning. |
| 41 |
while (scalar(@lines) && $lines[-1] =~ /^\Q$cpfx\E(\s.*)?/) { |
| 42 |
pop(@lines); |
| 43 |
} |
| 44 |
|
| 45 |
# Now the first line left is the start of the comment body; |
| 46 |
# for line comments the entire comment is gone, but for a |
| 47 |
# C-style block comment the comment start marker remains. |
| 48 |
|
| 49 |
my $npfx = $cpfx.' '; |
| 50 |
$npfx = '# ' if ($npfx eq '## '); # Change ## to # |
| 51 |
my $nsuf = ''; |
| 52 |
|
| 53 |
# Change C block comments to inline comments |
| 54 |
if ($ctail eq ' */') { |
| 55 |
pop(@lines) if ($lines[-1] =~ /^\/\*/); |
| 56 |
$npfx = '/* '; |
| 57 |
$nsuf = ' */'; |
| 58 |
} |
| 59 |
my @hdr = (); |
| 60 |
# Shebang and emacs mode lines should be left at the top |
| 61 |
while (scalar(@lines) && $lines[0] =~ /^(?:\#\!|.*\-\*\-)/) { |
| 62 |
push(@hdr, shift(@lines)); |
| 63 |
} |
| 64 |
push(@hdr, $npfx.'SPDX-License-Identifier: BSD-2-Clause'.$nsuf); |
| 65 |
push(@hdr, $npfx.$copy.$nsuf); |
| 66 |
unshift(@lines, @hdr); |
| 67 |
undef $ctail; |
| 68 |
$modified = 1; |
| 69 |
next; |
| 70 |
} |
| 71 |
|
| 72 |
push(@lines, $l) if (defined($l)); |
| 73 |
} |
| 74 |
close($in); |
| 75 |
|
| 76 |
while (scalar(@lines) && $lines[-1] =~ /^\s*$/) { |
| 77 |
pop(@lines); |
| 78 |
$modified = 1; |
| 79 |
} |
| 80 |
next unless ($modified); |
| 81 |
|
| 82 |
open(my $out, '>', $file) or die "$0: $file: $!\n"; |
| 83 |
print $out map { "$_\n" } @lines; |
| 84 |
close($out); |
| 85 |
} |
| 86 |
exit 0; |