2
0

update-copyright 9.3 KB

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