update-copyright 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #!/bin/sh
  2. #! -*-perl-*-
  3. # Update an FSF copyright year list to include the current year.
  4. # Copyright (C) 2009-2021 Free Software Foundation, Inc.
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. #
  19. # Written by Jim Meyering and Joel E. Denny
  20. # This script updates an FSF copyright year list to include the current year.
  21. # Usage: update-copyright [FILE...]
  22. #
  23. # The arguments to this script should be names of files that contain
  24. # copyright statements to be updated. The copyright holder's name
  25. # defaults to "Free Software Foundation, Inc." but may be changed to
  26. # any other name by using the "UPDATE_COPYRIGHT_HOLDER" environment
  27. # variable.
  28. #
  29. # For example, you might wish to use the update-copyright target rule
  30. # in maint.mk from gnulib's maintainer-makefile module.
  31. #
  32. # Iff a copyright statement is recognized in a file and the final
  33. # year is not the current year, then the statement is updated for the
  34. # new year and it is reformatted to:
  35. #
  36. # 1. Fit within 72 columns.
  37. # 2. Convert 2-digit years to 4-digit years by prepending "19".
  38. # 3. Expand copyright year intervals. (See "Environment variables"
  39. # below.)
  40. #
  41. # A warning is printed for every file for which no copyright
  42. # statement is recognized.
  43. #
  44. # Each file's copyright statement must be formatted correctly in
  45. # order to be recognized. For example, each of these is fine:
  46. #
  47. # Copyright @copyright{} 1990-2005, 2007-2009 Free Software
  48. # Foundation, Inc.
  49. #
  50. # # Copyright (C) 1990-2005, 2007-2009 Free Software
  51. # # Foundation, Inc.
  52. #
  53. # /*
  54. # * Copyright &copy; 90,2005,2007-2009
  55. # * Free Software Foundation, Inc.
  56. # */
  57. #
  58. # However, the following format is not recognized because the line
  59. # prefix changes after the first line:
  60. #
  61. # ## Copyright (C) 1990-2005, 2007-2009 Free Software
  62. # # Foundation, Inc.
  63. #
  64. # However, any correctly formatted copyright statement following
  65. # a non-matching copyright statements would be recognized.
  66. #
  67. # The exact conditions that a file's copyright statement must meet
  68. # to be recognized are:
  69. #
  70. # 1. It is the first copyright statement that meets all of the
  71. # following conditions. Subsequent copyright statements are
  72. # ignored.
  73. # 2. Its format is "Copyright (C)", then a list of copyright years,
  74. # and then the name of the copyright holder.
  75. # 3. The "(C)" takes one of the following forms or is omitted
  76. # entirely:
  77. #
  78. # A. (C)
  79. # B. (c)
  80. # C. @copyright{}
  81. # D. &copy;
  82. # E. ©
  83. #
  84. # 4. The "Copyright" appears at the beginning of a line, except that it
  85. # may be prefixed by any sequence (e.g., a comment) of no more than
  86. # 5 characters -- including white space.
  87. # 5. Iff such a prefix is present, the same prefix appears at the
  88. # beginning of each remaining line within the FSF copyright
  89. # statement. There is one exception in order to support C-style
  90. # comments: if the first line's prefix contains nothing but
  91. # whitespace surrounding a "/*", then the prefix for all subsequent
  92. # lines is the same as the first line's prefix except with each of
  93. # "/" and possibly "*" replaced by a " ". The replacement of "*"
  94. # by " " is consistent throughout all subsequent lines.
  95. # 6. Blank lines, even if preceded by the prefix, do not appear
  96. # within the FSF copyright statement.
  97. # 7. Each copyright year is 2 or 4 digits, and years are separated by
  98. # commas, "-", or "--". Whitespace may appear after commas.
  99. #
  100. # Environment variables:
  101. #
  102. # 1. If UPDATE_COPYRIGHT_FORCE=1, a recognized FSF copyright statement
  103. # is reformatted even if it does not need updating for the new
  104. # year. If unset or set to 0, only updated FSF copyright
  105. # statements are reformatted.
  106. # 2. If UPDATE_COPYRIGHT_USE_INTERVALS=1, every series of consecutive
  107. # copyright years (such as 90, 1991, 1992-2007, 2008) in a
  108. # reformatted FSF copyright statement is collapsed to a single
  109. # interval (such as 1990-2008). If unset or set to 0, all existing
  110. # copyright year intervals in a reformatted FSF copyright statement
  111. # are expanded instead.
  112. # If UPDATE_COPYRIGHT_USE_INTERVALS=2, convert a sequence with gaps
  113. # to the minimal containing range. For example, convert
  114. # 2000, 2004-2007, 2009 to 2000-2009.
  115. # 3. For testing purposes, you can set the assumed current year in
  116. # UPDATE_COPYRIGHT_YEAR.
  117. # 4. The default maximum line length for a copyright line is 72.
  118. # Set UPDATE_COPYRIGHT_MAX_LINE_LENGTH to use a different length.
  119. # 5. Set UPDATE_COPYRIGHT_HOLDER if the copyright holder is other
  120. # than "Free Software Foundation, Inc.".
  121. # This is a prologue that allows to run a perl script as an executable
  122. # on systems that are compliant to a POSIX version before POSIX:2017.
  123. # On such systems, the usual invocation of an executable through execlp()
  124. # or execvp() fails with ENOEXEC if it is a script that does not start
  125. # with a #! line. The script interpreter mentioned in the #! line has
  126. # to be /bin/sh, because on GuixSD systems that is the only program that
  127. # has a fixed file name. The second line is essential for perl and is
  128. # also useful for editing this file in Emacs. The next two lines below
  129. # are valid code in both sh and perl. When executed by sh, they re-execute
  130. # the script through the perl program found in $PATH. The '-x' option
  131. # is essential as well; without it, perl would re-execute the script
  132. # through /bin/sh. When executed by perl, the next two lines are a no-op.
  133. eval 'exec perl -wSx -0777 -pi "$0" "$@"'
  134. if 0;
  135. my $VERSION = '2020-04-04.15:07'; # UTC
  136. # The definition above must lie within the first 8 lines in order
  137. # for the Emacs time-stamp write hook (at end) to update it.
  138. # If you change this file with Emacs, please let the write hook
  139. # do its job. Otherwise, update this string manually.
  140. use strict;
  141. use warnings;
  142. my $copyright_re = 'Copyright';
  143. my $circle_c_re = '(?:\([cC]\)|@copyright\{}|\\\\\(co|&copy;|©)';
  144. my $holder = $ENV{UPDATE_COPYRIGHT_HOLDER};
  145. $holder ||= 'Free Software Foundation, Inc.';
  146. my $prefix_max = 5;
  147. my $margin = $ENV{UPDATE_COPYRIGHT_MAX_LINE_LENGTH};
  148. !$margin || $margin !~ m/^\d+$/
  149. and $margin = 72;
  150. my $tab_width = 8;
  151. my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
  152. if (!$this_year || $this_year !~ m/^\d{4}$/)
  153. {
  154. my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
  155. $this_year = $year + 1900;
  156. }
  157. # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
  158. my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
  159. my $leading;
  160. my $prefix;
  161. my $ws_re;
  162. my $stmt_re;
  163. while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
  164. {
  165. $leading = "$1$2";
  166. $prefix = $2;
  167. if ($prefix =~ /^(\s*\/)\*(\s*)$/)
  168. {
  169. $prefix =~ s,/, ,;
  170. my $prefix_ws = $prefix;
  171. $prefix_ws =~ s/\*/ /; # Only whitespace.
  172. if (/\G(?:[^*\n]|\*[^\/\n])*\*?\n$prefix_ws/)
  173. {
  174. $prefix = $prefix_ws;
  175. }
  176. }
  177. $ws_re = '[ \t\r\f]'; # \s without \n
  178. $ws_re =
  179. "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
  180. my $holder_re = $holder;
  181. $holder_re =~ s/\s/$ws_re/g;
  182. my $stmt_remainder_re =
  183. "(?:$ws_re$circle_c_re)?"
  184. . "$ws_re(?:(?:\\d\\d)?\\d\\d(?:,$ws_re?|--?))*"
  185. . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
  186. if (/\G$stmt_remainder_re/)
  187. {
  188. $stmt_re =
  189. quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
  190. last;
  191. }
  192. }
  193. if (defined $stmt_re)
  194. {
  195. /$stmt_re/ or die; # Should never die.
  196. my $stmt = $1;
  197. my $final_year_orig = $2;
  198. # Handle two-digit year numbers like "98" and "99".
  199. my $final_year = $final_year_orig;
  200. $final_year <= 99
  201. and $final_year += 1900;
  202. if ($final_year != $this_year)
  203. {
  204. # Update the year.
  205. $stmt =~ s/\b$final_year_orig\b/$final_year, $this_year/;
  206. }
  207. if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'})
  208. {
  209. # Normalize all whitespace including newline-prefix sequences.
  210. $stmt =~ s/$ws_re/ /g;
  211. # Put spaces after commas.
  212. $stmt =~ s/, ?/, /g;
  213. # Convert 2-digit to 4-digit years.
  214. $stmt =~ s/(\b\d\d\b)/19$1/g;
  215. # Make the use of intervals consistent.
  216. if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS})
  217. {
  218. $stmt =~ s/(\d{4})--?(\d{4})/join(', ', $1..$2)/eg;
  219. }
  220. else
  221. {
  222. my $ndash = $ARGV =~ /\.tex(i(nfo)?)?$/ ? "--" : "-";
  223. $stmt =~
  224. s/
  225. (\d{4})
  226. (?:
  227. (,\ |--?)
  228. ((??{
  229. if ($2 ne ', ') { '\d{4}'; }
  230. elsif (!$3) { $1 + 1; }
  231. else { $3 + 1; }
  232. }))
  233. )+
  234. /$1$ndash$3/gx;
  235. # When it's 2, emit a single range encompassing all year numbers.
  236. $ENV{UPDATE_COPYRIGHT_USE_INTERVALS} == 2
  237. and $stmt =~ s/\b(\d{4})\b.*\b(\d{4})\b/$1$ndash$2/;
  238. }
  239. # Format within margin.
  240. my $stmt_wrapped;
  241. my $text_margin = $margin - length($prefix);
  242. if ($prefix =~ /^(\t+)/)
  243. {
  244. $text_margin -= length($1) * ($tab_width - 1);
  245. }
  246. while (length $stmt)
  247. {
  248. if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
  249. || ($stmt =~ s/^([\S]+)(?: |$)//))
  250. {
  251. my $line = $1;
  252. $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
  253. $stmt_wrapped .= $line;
  254. }
  255. else
  256. {
  257. # Should be unreachable, but we don't want an infinite
  258. # loop if it can be reached.
  259. die;
  260. }
  261. }
  262. # Replace the old copyright statement.
  263. s/$stmt_re/$stmt_wrapped/;
  264. }
  265. }
  266. else
  267. {
  268. print STDERR "$ARGV: warning: copyright statement not found\n";
  269. }
  270. # Hey Emacs!
  271. # Local variables:
  272. # coding: utf-8
  273. # mode: perl
  274. # indent-tabs-mode: nil
  275. # eval: (add-hook 'before-save-hook 'time-stamp)
  276. # time-stamp-line-limit: 200
  277. # time-stamp-start: "my $VERSION = '"
  278. # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
  279. # time-stamp-time-zone: "UTC0"
  280. # time-stamp-end: "'; # UTC"
  281. # End: