]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/get_maintainer.pl
scripts/get_maintainer.pl: optionally ignore non-maintainer signatures
[mirror_ubuntu-bionic-kernel.git] / scripts / get_maintainer.pl
CommitLineData
cb7301c7
JP
1#!/usr/bin/perl -w
2# (c) 2007, Joe Perches <joe@perches.com>
3# created from checkpatch.pl
4#
5# Print selected MAINTAINERS information for
6# the files modified in a patch or for a file
7#
3bd7bf5f
RK
8# usage: perl scripts/get_maintainer.pl [OPTIONS] <patch>
9# perl scripts/get_maintainer.pl [OPTIONS] -f <file>
cb7301c7
JP
10#
11# Licensed under the terms of the GNU GPL License version 2
12
13use strict;
14
15my $P = $0;
60db31ac 16my $V = '0.23';
cb7301c7
JP
17
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $lk_path = "./";
21my $email = 1;
22my $email_usename = 1;
23my $email_maintainer = 1;
24my $email_list = 1;
25my $email_subscriber_list = 0;
cb7301c7 26my $email_git_penguin_chiefs = 0;
60db31ac 27my $email_git = 1;
e4d26b02 28my $email_git_all_signature_types = 1;
60db31ac 29my $email_git_blame = 0;
cb7301c7
JP
30my $email_git_min_signatures = 1;
31my $email_git_max_maintainers = 5;
afa81ee1 32my $email_git_min_percent = 5;
cb7301c7 33my $email_git_since = "1-year-ago";
60db31ac 34my $email_hg_since = "-365";
11ecf53c 35my $email_remove_duplicates = 1;
cb7301c7
JP
36my $output_multiline = 1;
37my $output_separator = ", ";
3c7385b8
JP
38my $output_roles = 0;
39my $output_rolestats = 0;
cb7301c7
JP
40my $scm = 0;
41my $web = 0;
42my $subsystem = 0;
43my $status = 0;
dcf36a92 44my $keywords = 1;
4b76c9da 45my $sections = 0;
03372dbb 46my $file_emails = 0;
4a7fdb5f 47my $from_filename = 0;
3fb55652 48my $pattern_depth = 0;
cb7301c7
JP
49my $version = 0;
50my $help = 0;
51
52my $exit = 0;
53
54my @penguin_chief = ();
e4d26b02 55push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
cb7301c7 56#Andrew wants in on most everything - 2009/01/14
e4d26b02 57#push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
cb7301c7
JP
58
59my @penguin_chief_names = ();
60foreach my $chief (@penguin_chief) {
61 if ($chief =~ m/^(.*):(.*)/) {
62 my $chief_name = $1;
63 my $chief_addr = $2;
64 push(@penguin_chief_names, $chief_name);
65 }
66}
e4d26b02
JP
67my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
68
69# Signature types of people who are either
70# a) responsible for the code in question, or
71# b) familiar enough with it to give relevant feedback
72my @signature_tags = ();
73push(@signature_tags, "Signed-off-by:");
74push(@signature_tags, "Reviewed-by:");
75push(@signature_tags, "Acked-by:");
76my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
cb7301c7 77
5f2441e9 78# rfc822 email address - preloaded methods go here.
1b5e1cf6 79my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
df4cc036 80my $rfc822_char = '[\\000-\\377]';
1b5e1cf6 81
60db31ac
JP
82# VCS command support: class-like functions and strings
83
84my %VCS_cmds;
85
86my %VCS_cmds_git = (
87 "execute_cmd" => \&git_execute_cmd,
88 "available" => '(which("git") ne "") && (-d ".git")',
99cf6116
RK
89 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
90 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
60db31ac
JP
91 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
92 "blame_file_cmd" => "git blame -l \$file",
93 "commit_pattern" => "^commit [0-9a-f]{40,40}",
94 "blame_commit_pattern" => "^([0-9a-f]+) "
95);
96
97my %VCS_cmds_hg = (
98 "execute_cmd" => \&hg_execute_cmd,
99 "available" => '(which("hg") ne "") && (-d ".hg")',
100 "find_signers_cmd" =>
101 "hg log --date=\$email_hg_since" .
102 " --template='commit {node}\\n{desc}\\n' -- \$file",
103 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
104 "blame_range_cmd" => "", # not supported
105 "blame_file_cmd" => "hg blame -c \$file",
106 "commit_pattern" => "^commit [0-9a-f]{40,40}",
107 "blame_commit_pattern" => "^([0-9a-f]+):"
108);
109
cb7301c7
JP
110if (!GetOptions(
111 'email!' => \$email,
112 'git!' => \$email_git,
e4d26b02 113 'git-all-signature-types!' => \$email_git_all_signature_types,
60db31ac 114 'git-blame!' => \$email_git_blame,
cb7301c7
JP
115 'git-chief-penguins!' => \$email_git_penguin_chiefs,
116 'git-min-signatures=i' => \$email_git_min_signatures,
117 'git-max-maintainers=i' => \$email_git_max_maintainers,
afa81ee1 118 'git-min-percent=i' => \$email_git_min_percent,
cb7301c7 119 'git-since=s' => \$email_git_since,
60db31ac 120 'hg-since=s' => \$email_hg_since,
11ecf53c 121 'remove-duplicates!' => \$email_remove_duplicates,
cb7301c7
JP
122 'm!' => \$email_maintainer,
123 'n!' => \$email_usename,
124 'l!' => \$email_list,
125 's!' => \$email_subscriber_list,
126 'multiline!' => \$output_multiline,
3c7385b8
JP
127 'roles!' => \$output_roles,
128 'rolestats!' => \$output_rolestats,
cb7301c7
JP
129 'separator=s' => \$output_separator,
130 'subsystem!' => \$subsystem,
131 'status!' => \$status,
132 'scm!' => \$scm,
133 'web!' => \$web,
3fb55652 134 'pattern-depth=i' => \$pattern_depth,
dcf36a92 135 'k|keywords!' => \$keywords,
4b76c9da 136 'sections!' => \$sections,
03372dbb 137 'fe|file-emails!' => \$file_emails,
4a7fdb5f 138 'f|file' => \$from_filename,
cb7301c7 139 'v|version' => \$version,
64f77f31 140 'h|help|usage' => \$help,
cb7301c7 141 )) {
3c7385b8 142 die "$P: invalid argument - use --help if necessary\n";
cb7301c7
JP
143}
144
145if ($help != 0) {
146 usage();
147 exit 0;
148}
149
150if ($version != 0) {
151 print("${P} ${V}\n");
152 exit 0;
153}
154
64f77f31
JP
155if (-t STDIN && !@ARGV) {
156 # We're talking to a terminal, but have no command line arguments.
157 die "$P: missing patchfile or -f file - use --help if necessary\n";
cb7301c7
JP
158}
159
42498316
JP
160if ($output_separator ne ", ") {
161 $output_multiline = 0;
162}
163
3c7385b8
JP
164if ($output_rolestats) {
165 $output_roles = 1;
166}
167
4b76c9da
JP
168if ($sections) {
169 $email = 0;
170 $email_list = 0;
171 $scm = 0;
172 $status = 0;
173 $subsystem = 0;
174 $web = 0;
175 $keywords = 0;
176} else {
177 my $selections = $email + $scm + $status + $subsystem + $web;
178 if ($selections == 0) {
4b76c9da
JP
179 die "$P: Missing required option: email, scm, status, subsystem or web\n";
180 }
cb7301c7
JP
181}
182
f5492666
JP
183if ($email &&
184 ($email_maintainer + $email_list + $email_subscriber_list +
185 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
cb7301c7
JP
186 die "$P: Please select at least 1 email option\n";
187}
188
189if (!top_of_kernel_tree($lk_path)) {
190 die "$P: The current directory does not appear to be "
191 . "a linux kernel source tree.\n";
192}
193
e4d26b02
JP
194if ($email_git_all_signature_types) {
195 $signaturePattern = "(.+?)[Bb][Yy]:";
196}
197
cb7301c7
JP
198## Read MAINTAINERS for type/value pairs
199
200my @typevalue = ();
dcf36a92
JP
201my %keyword_hash;
202
22dd5b0c
SH
203open (my $maint, '<', "${lk_path}MAINTAINERS")
204 or die "$P: Can't open MAINTAINERS: $!\n";
205while (<$maint>) {
cb7301c7
JP
206 my $line = $_;
207
208 if ($line =~ m/^(\C):\s*(.*)/) {
209 my $type = $1;
210 my $value = $2;
211
212 ##Filename pattern matching
213 if ($type eq "F" || $type eq "X") {
214 $value =~ s@\.@\\\.@g; ##Convert . to \.
215 $value =~ s/\*/\.\*/g; ##Convert * to .*
216 $value =~ s/\?/\./g; ##Convert ? to .
870020f9
JP
217 ##if pattern is a directory and it lacks a trailing slash, add one
218 if ((-d $value)) {
219 $value =~ s@([^/])$@$1/@;
220 }
dcf36a92
JP
221 } elsif ($type eq "K") {
222 $keyword_hash{@typevalue} = $value;
cb7301c7
JP
223 }
224 push(@typevalue, "$type:$value");
225 } elsif (!/^(\s)*$/) {
226 $line =~ s/\n$//g;
227 push(@typevalue, $line);
228 }
229}
22dd5b0c 230close($maint);
cb7301c7 231
8cbb3a77
JP
232my %mailmap;
233
11ecf53c 234if ($email_remove_duplicates) {
22dd5b0c
SH
235 open(my $mailmap, '<', "${lk_path}.mailmap")
236 or warn "$P: Can't open .mailmap: $!\n";
237 while (<$mailmap>) {
11ecf53c 238 my $line = $_;
8cbb3a77 239
11ecf53c
JP
240 next if ($line =~ m/^\s*#/);
241 next if ($line =~ m/^\s*$/);
8cbb3a77 242
11ecf53c 243 my ($name, $address) = parse_email($line);
a8af2430 244 $line = format_email($name, $address, $email_usename);
8cbb3a77 245
11ecf53c 246 next if ($line =~ m/^\s*$/);
8cbb3a77 247
11ecf53c
JP
248 if (exists($mailmap{$name})) {
249 my $obj = $mailmap{$name};
250 push(@$obj, $address);
251 } else {
252 my @arr = ($address);
253 $mailmap{$name} = \@arr;
254 }
8cbb3a77 255 }
22dd5b0c 256 close($mailmap);
8cbb3a77
JP
257}
258
4a7fdb5f 259## use the filenames on the command line or find the filenames in the patchfiles
cb7301c7
JP
260
261my @files = ();
f5492666 262my @range = ();
dcf36a92 263my @keyword_tvi = ();
03372dbb 264my @file_emails = ();
cb7301c7 265
64f77f31
JP
266if (!@ARGV) {
267 push(@ARGV, "&STDIN");
268}
269
4a7fdb5f 270foreach my $file (@ARGV) {
64f77f31
JP
271 if ($file ne "&STDIN") {
272 ##if $file is a directory and it lacks a trailing slash, add one
273 if ((-d $file)) {
274 $file =~ s@([^/])$@$1/@;
275 } elsif (!(-f $file)) {
276 die "$P: file '${file}' not found\n";
277 }
cb7301c7 278 }
4a7fdb5f
JP
279 if ($from_filename) {
280 push(@files, $file);
03372dbb 281 if (-f $file && ($keywords || $file_emails)) {
22dd5b0c
SH
282 open(my $f, '<', $file)
283 or die "$P: Can't open $file: $!\n";
284 my $text = do { local($/) ; <$f> };
285 close($f);
03372dbb
JP
286 if ($keywords) {
287 foreach my $line (keys %keyword_hash) {
288 if ($text =~ m/$keyword_hash{$line}/x) {
289 push(@keyword_tvi, $line);
290 }
dcf36a92
JP
291 }
292 }
03372dbb
JP
293 if ($file_emails) {
294 my @poss_addr = $text =~ m$[A-Za-zÀ-ÿ\"\' \,\.\+-]*\s*[\,]*\s*[\(\<\{]{0,1}[A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+\.[A-Za-z0-9]+[\)\>\}]{0,1}$g;
295 push(@file_emails, clean_file_emails(@poss_addr));
296 }
dcf36a92 297 }
4a7fdb5f
JP
298 } else {
299 my $file_cnt = @files;
f5492666 300 my $lastfile;
22dd5b0c 301
3a4df13d 302 open(my $patch, "< $file")
22dd5b0c
SH
303 or die "$P: Can't open $file: $!\n";
304 while (<$patch>) {
dcf36a92 305 my $patch_line = $_;
4a7fdb5f
JP
306 if (m/^\+\+\+\s+(\S+)/) {
307 my $filename = $1;
308 $filename =~ s@^[^/]*/@@;
309 $filename =~ s@\n@@;
f5492666 310 $lastfile = $filename;
4a7fdb5f 311 push(@files, $filename);
f5492666
JP
312 } elsif (m/^\@\@ -(\d+),(\d+)/) {
313 if ($email_git_blame) {
314 push(@range, "$lastfile:$1:$2");
315 }
dcf36a92
JP
316 } elsif ($keywords) {
317 foreach my $line (keys %keyword_hash) {
318 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
319 push(@keyword_tvi, $line);
320 }
321 }
4a7fdb5f 322 }
cb7301c7 323 }
22dd5b0c
SH
324 close($patch);
325
4a7fdb5f 326 if ($file_cnt == @files) {
7f29fd27 327 warn "$P: file '${file}' doesn't appear to be a patch. "
4a7fdb5f
JP
328 . "Add -f to options?\n";
329 }
330 @files = sort_and_uniq(@files);
cb7301c7 331 }
cb7301c7
JP
332}
333
03372dbb
JP
334@file_emails = uniq(@file_emails);
335
cb7301c7 336my @email_to = ();
290603c1 337my @list_to = ();
cb7301c7
JP
338my @scm = ();
339my @web = ();
340my @subsystem = ();
341my @status = ();
342
343# Find responsible parties
344
345foreach my $file (@files) {
346
272a8979
JP
347 my %hash;
348 my $tvi = find_first_section();
349 while ($tvi < @typevalue) {
350 my $start = find_starting_index($tvi);
351 my $end = find_ending_index($tvi);
352 my $exclude = 0;
353 my $i;
354
355 #Do not match excluded file patterns
356
357 for ($i = $start; $i < $end; $i++) {
358 my $line = $typevalue[$i];
290603c1 359 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
360 my $type = $1;
361 my $value = $2;
272a8979 362 if ($type eq 'X') {
cb7301c7 363 if (file_match_pattern($file, $value)) {
272a8979 364 $exclude = 1;
3c840c18 365 last;
cb7301c7
JP
366 }
367 }
368 }
cb7301c7 369 }
272a8979
JP
370
371 if (!$exclude) {
372 for ($i = $start; $i < $end; $i++) {
373 my $line = $typevalue[$i];
374 if ($line =~ m/^(\C):\s*(.*)/) {
375 my $type = $1;
376 my $value = $2;
377 if ($type eq 'F') {
378 if (file_match_pattern($file, $value)) {
379 my $value_pd = ($value =~ tr@/@@);
380 my $file_pd = ($file =~ tr@/@@);
381 $value_pd++ if (substr($value,-1,1) ne "/");
382 if ($pattern_depth == 0 ||
383 (($file_pd - $value_pd) < $pattern_depth)) {
384 $hash{$tvi} = $value_pd;
385 }
386 }
387 }
388 }
389 }
1d606b4e 390 }
272a8979 391
3c840c18 392 $tvi = $end + 1;
272a8979
JP
393 }
394
395 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
396 add_categories($line);
4b76c9da
JP
397 if ($sections) {
398 my $i;
399 my $start = find_starting_index($line);
400 my $end = find_ending_index($line);
401 for ($i = $start; $i < $end; $i++) {
402 my $line = $typevalue[$i];
403 if ($line =~ /^[FX]:/) { ##Restore file patterns
404 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
405 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
406 $line =~ s/\\\./\./g; ##Convert \. to .
407 $line =~ s/\.\*/\*/g; ##Convert .* to *
408 }
f11e9a15 409 $line =~ s/^([A-Z]):/$1:\t/g;
4b76c9da
JP
410 print("$line\n");
411 }
f11e9a15 412 print("\n");
4b76c9da 413 }
cb7301c7
JP
414 }
415
4a7fdb5f 416 if ($email && $email_git) {
60db31ac 417 vcs_file_signoffs($file);
cb7301c7
JP
418 }
419
f5492666 420 if ($email && $email_git_blame) {
60db31ac 421 vcs_file_blame($file);
f5492666 422 }
cb7301c7
JP
423}
424
dcf36a92
JP
425if ($keywords) {
426 @keyword_tvi = sort_and_uniq(@keyword_tvi);
427 foreach my $line (@keyword_tvi) {
428 add_categories($line);
429 }
430}
431
f5f5078d 432if ($email) {
cb7301c7
JP
433 foreach my $chief (@penguin_chief) {
434 if ($chief =~ m/^(.*):(.*)/) {
f5f5078d 435 my $email_address;
0e70e83d 436
a8af2430 437 $email_address = format_email($1, $2, $email_usename);
f5f5078d 438 if ($email_git_penguin_chiefs) {
3c7385b8 439 push(@email_to, [$email_address, 'chief penguin']);
f5f5078d 440 } else {
3c7385b8 441 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
cb7301c7
JP
442 }
443 }
444 }
03372dbb
JP
445
446 foreach my $email (@file_emails) {
447 my ($name, $address) = parse_email($email);
448
449 my $tmp_email = format_email($name, $address, $email_usename);
450 push_email_address($tmp_email, '');
451 add_role($tmp_email, 'in file');
452 }
cb7301c7
JP
453}
454
290603c1
JP
455if ($email || $email_list) {
456 my @to = ();
457 if ($email) {
458 @to = (@to, @email_to);
cb7301c7 459 }
290603c1 460 if ($email_list) {
290603c1 461 @to = (@to, @list_to);
290603c1 462 }
3c7385b8 463 output(merge_email(@to));
cb7301c7
JP
464}
465
466if ($scm) {
b781655a 467 @scm = uniq(@scm);
cb7301c7
JP
468 output(@scm);
469}
470
471if ($status) {
b781655a 472 @status = uniq(@status);
cb7301c7
JP
473 output(@status);
474}
475
476if ($subsystem) {
b781655a 477 @subsystem = uniq(@subsystem);
cb7301c7
JP
478 output(@subsystem);
479}
480
481if ($web) {
b781655a 482 @web = uniq(@web);
cb7301c7
JP
483 output(@web);
484}
485
486exit($exit);
487
488sub file_match_pattern {
489 my ($file, $pattern) = @_;
490 if (substr($pattern, -1) eq "/") {
491 if ($file =~ m@^$pattern@) {
492 return 1;
493 }
494 } else {
495 if ($file =~ m@^$pattern@) {
496 my $s1 = ($file =~ tr@/@@);
497 my $s2 = ($pattern =~ tr@/@@);
498 if ($s1 == $s2) {
499 return 1;
500 }
501 }
502 }
503 return 0;
504}
505
506sub usage {
507 print <<EOT;
508usage: $P [options] patchfile
870020f9 509 $P [options] -f file|directory
cb7301c7
JP
510version: $V
511
512MAINTAINER field selection options:
513 --email => print email address(es) if any
514 --git => include recent git \*-by: signers
e4d26b02
JP
515 --git-all-signature-types => include signers regardless of signature type
516 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
cb7301c7 517 --git-chief-penguins => include ${penguin_chiefs}
e4d26b02
JP
518 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
519 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
520 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
f5492666 521 --git-blame => use git blame to find modified commits for patch or file
e4d26b02
JP
522 --git-since => git history to use (default: $email_git_since)
523 --hg-since => hg history to use (default: $email_hg_since)
cb7301c7
JP
524 --m => include maintainer(s) if any
525 --n => include name 'Full Name <addr\@domain.tld>'
526 --l => include list(s) if any
527 --s => include subscriber only list(s) if any
11ecf53c 528 --remove-duplicates => minimize duplicate email names/addresses
3c7385b8
JP
529 --roles => show roles (status:subsystem, git-signer, list, etc...)
530 --rolestats => show roles and statistics (commits/total_commits, %)
03372dbb 531 --file-emails => add email addresses found in -f file (default: 0 (off))
cb7301c7
JP
532 --scm => print SCM tree(s) if any
533 --status => print status if any
534 --subsystem => print subsystem name if any
535 --web => print website(s) if any
536
537Output type options:
538 --separator [, ] => separator for multiple entries on 1 line
42498316 539 using --separator also sets --nomultiline if --separator is not [, ]
cb7301c7
JP
540 --multiline => print 1 entry per line
541
cb7301c7 542Other options:
3fb55652 543 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
dcf36a92 544 --keywords => scan patch for keywords (default: 1 (on))
4b76c9da 545 --sections => print the entire subsystem sections with pattern matches
f5f5078d 546 --version => show version
cb7301c7
JP
547 --help => show this help information
548
3fb55652 549Default options:
11ecf53c 550 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
3fb55652 551
870020f9
JP
552Notes:
553 Using "-f directory" may give unexpected results:
f5492666
JP
554 Used with "--git", git signators for _all_ files in and below
555 directory are examined as git recurses directories.
556 Any specified X: (exclude) pattern matches are _not_ ignored.
557 Used with "--nogit", directory is used as a pattern match,
60db31ac
JP
558 no individual file within the directory or subdirectory
559 is matched.
f5492666
JP
560 Used with "--git-blame", does not iterate all files in directory
561 Using "--git-blame" is slow and may add old committers and authors
562 that are no longer active maintainers to the output.
3c7385b8
JP
563 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
564 other automated tools that expect only ["name"] <email address>
565 may not work because of additional output after <email address>.
566 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
567 not the percentage of the entire file authored. # of commits is
568 not a good measure of amount of code authored. 1 major commit may
569 contain a thousand lines, 5 trivial commits may modify a single line.
60db31ac
JP
570 If git is not installed, but mercurial (hg) is installed and an .hg
571 repository exists, the following options apply to mercurial:
572 --git,
573 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
574 --git-blame
575 Use --hg-since not --git-since to control date selection
cb7301c7
JP
576EOT
577}
578
579sub top_of_kernel_tree {
580 my ($lk_path) = @_;
581
582 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
583 $lk_path .= "/";
584 }
585 if ( (-f "${lk_path}COPYING")
586 && (-f "${lk_path}CREDITS")
587 && (-f "${lk_path}Kbuild")
588 && (-f "${lk_path}MAINTAINERS")
589 && (-f "${lk_path}Makefile")
590 && (-f "${lk_path}README")
591 && (-d "${lk_path}Documentation")
592 && (-d "${lk_path}arch")
593 && (-d "${lk_path}include")
594 && (-d "${lk_path}drivers")
595 && (-d "${lk_path}fs")
596 && (-d "${lk_path}init")
597 && (-d "${lk_path}ipc")
598 && (-d "${lk_path}kernel")
599 && (-d "${lk_path}lib")
600 && (-d "${lk_path}scripts")) {
601 return 1;
602 }
603 return 0;
604}
605
0e70e83d
JP
606sub parse_email {
607 my ($formatted_email) = @_;
608
609 my $name = "";
610 my $address = "";
611
11ecf53c 612 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
0e70e83d
JP
613 $name = $1;
614 $address = $2;
11ecf53c 615 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
0e70e83d 616 $address = $1;
b781655a 617 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
0e70e83d
JP
618 $address = $1;
619 }
cb7301c7
JP
620
621 $name =~ s/^\s+|\s+$//g;
d789504a 622 $name =~ s/^\"|\"$//g;
0e70e83d 623 $address =~ s/^\s+|\s+$//g;
cb7301c7 624
a63ceb4c 625 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
0e70e83d
JP
626 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
627 $name = "\"$name\"";
628 }
629
630 return ($name, $address);
631}
632
633sub format_email {
a8af2430 634 my ($name, $address, $usename) = @_;
0e70e83d
JP
635
636 my $formatted_email;
637
638 $name =~ s/^\s+|\s+$//g;
639 $name =~ s/^\"|\"$//g;
640 $address =~ s/^\s+|\s+$//g;
cb7301c7 641
a63ceb4c 642 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
cb7301c7 643 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
0e70e83d
JP
644 $name = "\"$name\"";
645 }
646
a8af2430 647 if ($usename) {
0e70e83d
JP
648 if ("$name" eq "") {
649 $formatted_email = "$address";
650 } else {
a8af2430 651 $formatted_email = "$name <$address>";
0e70e83d 652 }
cb7301c7 653 } else {
0e70e83d 654 $formatted_email = $address;
cb7301c7 655 }
0e70e83d 656
cb7301c7
JP
657 return $formatted_email;
658}
659
272a8979
JP
660sub find_first_section {
661 my $index = 0;
662
663 while ($index < @typevalue) {
664 my $tv = $typevalue[$index];
665 if (($tv =~ m/^(\C):\s*(.*)/)) {
666 last;
667 }
668 $index++;
669 }
670
671 return $index;
672}
673
b781655a 674sub find_starting_index {
b781655a
JP
675 my ($index) = @_;
676
677 while ($index > 0) {
678 my $tv = $typevalue[$index];
679 if (!($tv =~ m/^(\C):\s*(.*)/)) {
680 last;
681 }
682 $index--;
683 }
684
685 return $index;
686}
687
688sub find_ending_index {
cb7301c7
JP
689 my ($index) = @_;
690
b781655a 691 while ($index < @typevalue) {
cb7301c7 692 my $tv = $typevalue[$index];
b781655a
JP
693 if (!($tv =~ m/^(\C):\s*(.*)/)) {
694 last;
695 }
696 $index++;
697 }
698
699 return $index;
700}
701
3c7385b8
JP
702sub get_maintainer_role {
703 my ($index) = @_;
704
705 my $i;
706 my $start = find_starting_index($index);
707 my $end = find_ending_index($index);
708
709 my $role;
710 my $subsystem = $typevalue[$start];
711 if (length($subsystem) > 20) {
712 $subsystem = substr($subsystem, 0, 17);
713 $subsystem =~ s/\s*$//;
714 $subsystem = $subsystem . "...";
715 }
716
717 for ($i = $start + 1; $i < $end; $i++) {
718 my $tv = $typevalue[$i];
719 if ($tv =~ m/^(\C):\s*(.*)/) {
720 my $ptype = $1;
721 my $pvalue = $2;
722 if ($ptype eq "S") {
723 $role = $pvalue;
724 }
725 }
726 }
727
728 $role = lc($role);
729 if ($role eq "supported") {
730 $role = "supporter";
731 } elsif ($role eq "maintained") {
732 $role = "maintainer";
733 } elsif ($role eq "odd fixes") {
734 $role = "odd fixer";
735 } elsif ($role eq "orphan") {
736 $role = "orphan minder";
737 } elsif ($role eq "obsolete") {
738 $role = "obsolete minder";
739 } elsif ($role eq "buried alive in reporters") {
740 $role = "chief penguin";
741 }
742
743 return $role . ":" . $subsystem;
744}
745
746sub get_list_role {
747 my ($index) = @_;
748
749 my $i;
750 my $start = find_starting_index($index);
751 my $end = find_ending_index($index);
752
753 my $subsystem = $typevalue[$start];
754 if (length($subsystem) > 20) {
755 $subsystem = substr($subsystem, 0, 17);
756 $subsystem =~ s/\s*$//;
757 $subsystem = $subsystem . "...";
758 }
759
760 if ($subsystem eq "THE REST") {
761 $subsystem = "";
762 }
763
764 return $subsystem;
765}
766
b781655a
JP
767sub add_categories {
768 my ($index) = @_;
769
770 my $i;
771 my $start = find_starting_index($index);
772 my $end = find_ending_index($index);
773
774 push(@subsystem, $typevalue[$start]);
775
776 for ($i = $start + 1; $i < $end; $i++) {
777 my $tv = $typevalue[$i];
290603c1 778 if ($tv =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
779 my $ptype = $1;
780 my $pvalue = $2;
781 if ($ptype eq "L") {
290603c1
JP
782 my $list_address = $pvalue;
783 my $list_additional = "";
3c7385b8
JP
784 my $list_role = get_list_role($i);
785
786 if ($list_role ne "") {
787 $list_role = ":" . $list_role;
788 }
290603c1
JP
789 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
790 $list_address = $1;
791 $list_additional = $2;
792 }
bdf7c685 793 if ($list_additional =~ m/subscribers-only/) {
cb7301c7 794 if ($email_subscriber_list) {
3c7385b8 795 push(@list_to, [$list_address, "subscriber list${list_role}"]);
cb7301c7
JP
796 }
797 } else {
798 if ($email_list) {
3c7385b8 799 push(@list_to, [$list_address, "open list${list_role}"]);
cb7301c7
JP
800 }
801 }
802 } elsif ($ptype eq "M") {
0e70e83d
JP
803 my ($name, $address) = parse_email($pvalue);
804 if ($name eq "") {
b781655a
JP
805 if ($i > 0) {
806 my $tv = $typevalue[$i - 1];
0e70e83d
JP
807 if ($tv =~ m/^(\C):\s*(.*)/) {
808 if ($1 eq "P") {
809 $name = $2;
a8af2430 810 $pvalue = format_email($name, $address, $email_usename);
5f2441e9
JP
811 }
812 }
813 }
814 }
0e70e83d 815 if ($email_maintainer) {
3c7385b8
JP
816 my $role = get_maintainer_role($i);
817 push_email_addresses($pvalue, $role);
cb7301c7
JP
818 }
819 } elsif ($ptype eq "T") {
820 push(@scm, $pvalue);
821 } elsif ($ptype eq "W") {
822 push(@web, $pvalue);
823 } elsif ($ptype eq "S") {
824 push(@status, $pvalue);
825 }
cb7301c7
JP
826 }
827 }
828}
829
11ecf53c
JP
830my %email_hash_name;
831my %email_hash_address;
0e70e83d 832
11ecf53c
JP
833sub email_inuse {
834 my ($name, $address) = @_;
835
836 return 1 if (($name eq "") && ($address eq ""));
837 return 1 if (($name ne "") && exists($email_hash_name{$name}));
838 return 1 if (($address ne "") && exists($email_hash_address{$address}));
0e70e83d 839
0e70e83d
JP
840 return 0;
841}
842
1b5e1cf6 843sub push_email_address {
3c7385b8 844 my ($line, $role) = @_;
1b5e1cf6 845
0e70e83d 846 my ($name, $address) = parse_email($line);
1b5e1cf6 847
b781655a
JP
848 if ($address eq "") {
849 return 0;
850 }
851
11ecf53c 852 if (!$email_remove_duplicates) {
a8af2430 853 push(@email_to, [format_email($name, $address, $email_usename), $role]);
11ecf53c 854 } elsif (!email_inuse($name, $address)) {
a8af2430 855 push(@email_to, [format_email($name, $address, $email_usename), $role]);
11ecf53c
JP
856 $email_hash_name{$name}++;
857 $email_hash_address{$address}++;
1b5e1cf6 858 }
b781655a
JP
859
860 return 1;
1b5e1cf6
JP
861}
862
863sub push_email_addresses {
3c7385b8 864 my ($address, $role) = @_;
1b5e1cf6
JP
865
866 my @address_list = ();
867
5f2441e9 868 if (rfc822_valid($address)) {
3c7385b8 869 push_email_address($address, $role);
5f2441e9 870 } elsif (@address_list = rfc822_validlist($address)) {
1b5e1cf6
JP
871 my $array_count = shift(@address_list);
872 while (my $entry = shift(@address_list)) {
3c7385b8 873 push_email_address($entry, $role);
1b5e1cf6 874 }
5f2441e9 875 } else {
3c7385b8 876 if (!push_email_address($address, $role)) {
b781655a
JP
877 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
878 }
1b5e1cf6 879 }
1b5e1cf6
JP
880}
881
3c7385b8
JP
882sub add_role {
883 my ($line, $role) = @_;
884
885 my ($name, $address) = parse_email($line);
a8af2430 886 my $email = format_email($name, $address, $email_usename);
3c7385b8
JP
887
888 foreach my $entry (@email_to) {
889 if ($email_remove_duplicates) {
890 my ($entry_name, $entry_address) = parse_email($entry->[0]);
03372dbb
JP
891 if (($name eq $entry_name || $address eq $entry_address)
892 && ($role eq "" || !($entry->[1] =~ m/$role/))
893 ) {
3c7385b8
JP
894 if ($entry->[1] eq "") {
895 $entry->[1] = "$role";
896 } else {
897 $entry->[1] = "$entry->[1],$role";
898 }
899 }
900 } else {
03372dbb
JP
901 if ($email eq $entry->[0]
902 && ($role eq "" || !($entry->[1] =~ m/$role/))
903 ) {
3c7385b8
JP
904 if ($entry->[1] eq "") {
905 $entry->[1] = "$role";
906 } else {
907 $entry->[1] = "$entry->[1],$role";
908 }
909 }
910 }
911 }
912}
913
cb7301c7
JP
914sub which {
915 my ($bin) = @_;
916
f5f5078d 917 foreach my $path (split(/:/, $ENV{PATH})) {
cb7301c7
JP
918 if (-e "$path/$bin") {
919 return "$path/$bin";
920 }
921 }
922
923 return "";
924}
925
8cbb3a77 926sub mailmap {
a8af2430 927 my (@lines) = @_;
8cbb3a77
JP
928 my %hash;
929
930 foreach my $line (@lines) {
931 my ($name, $address) = parse_email($line);
932 if (!exists($hash{$name})) {
933 $hash{$name} = $address;
11ecf53c
JP
934 } elsif ($address ne $hash{$name}) {
935 $address = $hash{$name};
a8af2430 936 $line = format_email($name, $address, $email_usename);
8cbb3a77
JP
937 }
938 if (exists($mailmap{$name})) {
939 my $obj = $mailmap{$name};
940 foreach my $map_address (@$obj) {
941 if (($map_address eq $address) &&
942 ($map_address ne $hash{$name})) {
a8af2430 943 $line = format_email($name, $hash{$name}, $email_usename);
8cbb3a77
JP
944 }
945 }
946 }
947 }
948
949 return @lines;
950}
951
60db31ac
JP
952sub git_execute_cmd {
953 my ($cmd) = @_;
954 my @lines = ();
cb7301c7 955
60db31ac
JP
956 my $output = `$cmd`;
957 $output =~ s/^\s*//gm;
958 @lines = split("\n", $output);
959
960 return @lines;
a8af2430
JP
961}
962
60db31ac 963sub hg_execute_cmd {
a8af2430 964 my ($cmd) = @_;
60db31ac
JP
965 my @lines = ();
966
967 my $output = `$cmd`;
968 @lines = split("\n", $output);
a8af2430 969
60db31ac
JP
970 return @lines;
971}
972
973sub vcs_find_signers {
974 my ($cmd) = @_;
a8af2430
JP
975 my @lines = ();
976 my $commits;
977
60db31ac 978 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
cb7301c7 979
60db31ac 980 my $pattern = $VCS_cmds{"commit_pattern"};
cb7301c7 981
60db31ac 982 $commits = grep(/$pattern/, @lines); # of commits
afa81ee1 983
e4d26b02 984 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
0e70e83d
JP
985 if (!$email_git_penguin_chiefs) {
986 @lines = grep(!/${penguin_chiefs}/i, @lines);
987 }
988 # cut -f2- -d":"
989 s/.*:\s*(.+)\s*/$1/ for (@lines);
990
a8af2430
JP
991## Reformat email addresses (with names) to avoid badly written signatures
992
3c7385b8
JP
993 foreach my $line (@lines) {
994 my ($name, $address) = parse_email($line);
a8af2430
JP
995 $line = format_email($name, $address, 1);
996 }
997
998 return ($commits, @lines);
999}
1000
60db31ac
JP
1001sub vcs_save_commits {
1002 my ($cmd) = @_;
1003 my @lines = ();
1004 my @commits = ();
1005
1006 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1007
1008 foreach my $line (@lines) {
1009 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1010 push(@commits, $1);
1011 }
1012 }
1013
1014 return @commits;
1015}
1016
1017sub vcs_blame {
1018 my ($file) = @_;
1019 my $cmd;
1020 my @commits = ();
1021
1022 return @commits if (!(-f $file));
1023
1024 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1025 my @all_commits = ();
1026
1027 $cmd = $VCS_cmds{"blame_file_cmd"};
1028 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1029 @all_commits = vcs_save_commits($cmd);
1030
1031 foreach my $file_range_diff (@range) {
1032 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1033 my $diff_file = $1;
1034 my $diff_start = $2;
1035 my $diff_length = $3;
1036 next if ("$file" ne "$diff_file");
1037 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1038 push(@commits, $all_commits[$i]);
1039 }
1040 }
1041 } elsif (@range) {
1042 foreach my $file_range_diff (@range) {
1043 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1044 my $diff_file = $1;
1045 my $diff_start = $2;
1046 my $diff_length = $3;
1047 next if ("$file" ne "$diff_file");
1048 $cmd = $VCS_cmds{"blame_range_cmd"};
1049 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1050 push(@commits, vcs_save_commits($cmd));
1051 }
1052 } else {
1053 $cmd = $VCS_cmds{"blame_file_cmd"};
1054 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1055 @commits = vcs_save_commits($cmd);
1056 }
1057
1058 return @commits;
1059}
1060
1061my $printed_novcs = 0;
1062sub vcs_exists {
1063 %VCS_cmds = %VCS_cmds_git;
1064 return 1 if eval $VCS_cmds{"available"};
1065 %VCS_cmds = %VCS_cmds_hg;
1066 return 1 if eval $VCS_cmds{"available"};
1067 %VCS_cmds = ();
1068 if (!$printed_novcs) {
1069 warn("$P: No supported VCS found. Add --nogit to options?\n");
1070 warn("Using a git repository produces better results.\n");
1071 warn("Try Linus Torvalds' latest git repository using:\n");
1072 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1073 $printed_novcs = 1;
1074 }
1075 return 0;
1076}
1077
1078sub vcs_assign {
a8af2430
JP
1079 my ($role, $divisor, @lines) = @_;
1080
1081 my %hash;
1082 my $count = 0;
1083
a8af2430
JP
1084 return if (@lines <= 0);
1085
1086 if ($divisor <= 0) {
60db31ac 1087 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
a8af2430 1088 $divisor = 1;
3c7385b8 1089 }
8cbb3a77 1090
11ecf53c
JP
1091 if ($email_remove_duplicates) {
1092 @lines = mailmap(@lines);
1093 }
0e70e83d 1094
0e70e83d 1095 @lines = sort(@lines);
11ecf53c 1096
0e70e83d 1097 # uniq -c
11ecf53c
JP
1098 $hash{$_}++ for @lines;
1099
0e70e83d 1100 # sort -rn
0e70e83d 1101 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
11ecf53c 1102 my $sign_offs = $hash{$line};
a8af2430 1103 my $percent = $sign_offs * 100 / $divisor;
3c7385b8 1104
a8af2430 1105 $percent = 100 if ($percent > 100);
11ecf53c
JP
1106 $count++;
1107 last if ($sign_offs < $email_git_min_signatures ||
1108 $count > $email_git_max_maintainers ||
a8af2430 1109 $percent < $email_git_min_percent);
3c7385b8 1110 push_email_address($line, '');
3c7385b8 1111 if ($output_rolestats) {
a8af2430
JP
1112 my $fmt_percent = sprintf("%.0f", $percent);
1113 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1114 } else {
1115 add_role($line, $role);
3c7385b8 1116 }
f5492666
JP
1117 }
1118}
1119
60db31ac 1120sub vcs_file_signoffs {
a8af2430
JP
1121 my ($file) = @_;
1122
1123 my @signers = ();
60db31ac 1124 my $commits;
f5492666 1125
60db31ac 1126 return if (!vcs_exists());
a8af2430 1127
60db31ac
JP
1128 my $cmd = $VCS_cmds{"find_signers_cmd"};
1129 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
f5492666 1130
60db31ac
JP
1131 ($commits, @signers) = vcs_find_signers($cmd);
1132 vcs_assign("commit_signer", $commits, @signers);
f5492666
JP
1133}
1134
60db31ac 1135sub vcs_file_blame {
f5492666
JP
1136 my ($file) = @_;
1137
a8af2430 1138 my @signers = ();
60db31ac 1139 my @commits = ();
a8af2430 1140 my $total_commits;
f5492666 1141
60db31ac 1142 return if (!vcs_exists());
f5492666 1143
60db31ac 1144 @commits = vcs_blame($file);
f5492666 1145 @commits = uniq(@commits);
a8af2430 1146 $total_commits = @commits;
8cbb3a77 1147
a8af2430
JP
1148 foreach my $commit (@commits) {
1149 my $commit_count;
1150 my @commit_signers = ();
8cbb3a77 1151
60db31ac
JP
1152 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1153 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1154
1155 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1156 push(@signers, @commit_signers);
f5492666
JP
1157 }
1158
a8af2430 1159 if ($from_filename) {
60db31ac 1160 vcs_assign("commits", $total_commits, @signers);
a8af2430 1161 } else {
60db31ac 1162 vcs_assign("modified commits", $total_commits, @signers);
cb7301c7 1163 }
cb7301c7
JP
1164}
1165
1166sub uniq {
a8af2430 1167 my (@parms) = @_;
cb7301c7
JP
1168
1169 my %saw;
1170 @parms = grep(!$saw{$_}++, @parms);
1171 return @parms;
1172}
1173
1174sub sort_and_uniq {
a8af2430 1175 my (@parms) = @_;
cb7301c7
JP
1176
1177 my %saw;
1178 @parms = sort @parms;
1179 @parms = grep(!$saw{$_}++, @parms);
1180 return @parms;
1181}
1182
03372dbb
JP
1183sub clean_file_emails {
1184 my (@file_emails) = @_;
1185 my @fmt_emails = ();
1186
1187 foreach my $email (@file_emails) {
1188 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1189 my ($name, $address) = parse_email($email);
1190 if ($name eq '"[,\.]"') {
1191 $name = "";
1192 }
1193
1194 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1195 if (@nw > 2) {
1196 my $first = $nw[@nw - 3];
1197 my $middle = $nw[@nw - 2];
1198 my $last = $nw[@nw - 1];
1199
1200 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1201 (length($first) == 2 && substr($first, -1) eq ".")) ||
1202 (length($middle) == 1 ||
1203 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1204 $name = "$first $middle $last";
1205 } else {
1206 $name = "$middle $last";
1207 }
1208 }
1209
1210 if (substr($name, -1) =~ /[,\.]/) {
1211 $name = substr($name, 0, length($name) - 1);
1212 } elsif (substr($name, -2) =~ /[,\.]"/) {
1213 $name = substr($name, 0, length($name) - 2) . '"';
1214 }
1215
1216 if (substr($name, 0, 1) =~ /[,\.]/) {
1217 $name = substr($name, 1, length($name) - 1);
1218 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1219 $name = '"' . substr($name, 2, length($name) - 2);
1220 }
1221
1222 my $fmt_email = format_email($name, $address, $email_usename);
1223 push(@fmt_emails, $fmt_email);
1224 }
1225 return @fmt_emails;
1226}
1227
3c7385b8
JP
1228sub merge_email {
1229 my @lines;
1230 my %saw;
1231
1232 for (@_) {
1233 my ($address, $role) = @$_;
1234 if (!$saw{$address}) {
1235 if ($output_roles) {
60db31ac 1236 push(@lines, "$address ($role)");
3c7385b8 1237 } else {
60db31ac 1238 push(@lines, $address);
3c7385b8
JP
1239 }
1240 $saw{$address} = 1;
1241 }
1242 }
1243
1244 return @lines;
1245}
1246
cb7301c7 1247sub output {
a8af2430 1248 my (@parms) = @_;
cb7301c7
JP
1249
1250 if ($output_multiline) {
1251 foreach my $line (@parms) {
1252 print("${line}\n");
1253 }
1254 } else {
1255 print(join($output_separator, @parms));
1256 print("\n");
1257 }
1258}
1b5e1cf6
JP
1259
1260my $rfc822re;
1261
1262sub make_rfc822re {
1263# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1264# comment. We must allow for rfc822_lwsp (or comments) after each of these.
1265# This regexp will only work on addresses which have had comments stripped
1266# and replaced with rfc822_lwsp.
1267
1268 my $specials = '()<>@,;:\\\\".\\[\\]';
1269 my $controls = '\\000-\\037\\177';
1270
1271 my $dtext = "[^\\[\\]\\r\\\\]";
1272 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1273
1274 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1275
1276# Use zero-width assertion to spot the limit of an atom. A simple
1277# $rfc822_lwsp* causes the regexp engine to hang occasionally.
1278 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1279 my $word = "(?:$atom|$quoted_string)";
1280 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1281
1282 my $sub_domain = "(?:$atom|$domain_literal)";
1283 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1284
1285 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1286
1287 my $phrase = "$word*";
1288 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1289 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1290 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1291
1292 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1293 my $address = "(?:$mailbox|$group)";
1294
1295 return "$rfc822_lwsp*$address";
1296}
1297
1298sub rfc822_strip_comments {
1299 my $s = shift;
1300# Recursively remove comments, and replace with a single space. The simpler
1301# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1302# chars in atoms, for example.
1303
1304 while ($s =~ s/^((?:[^"\\]|\\.)*
1305 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1306 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1307 return $s;
1308}
1309
1310# valid: returns true if the parameter is an RFC822 valid address
1311#
22dd5b0c 1312sub rfc822_valid {
1b5e1cf6
JP
1313 my $s = rfc822_strip_comments(shift);
1314
1315 if (!$rfc822re) {
1316 $rfc822re = make_rfc822re();
1317 }
1318
1319 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1320}
1321
1322# validlist: In scalar context, returns true if the parameter is an RFC822
1323# valid list of addresses.
1324#
1325# In list context, returns an empty list on failure (an invalid
1326# address was found); otherwise a list whose first element is the
1327# number of addresses found and whose remaining elements are the
1328# addresses. This is needed to disambiguate failure (invalid)
1329# from success with no addresses found, because an empty string is
1330# a valid list.
1331
22dd5b0c 1332sub rfc822_validlist {
1b5e1cf6
JP
1333 my $s = rfc822_strip_comments(shift);
1334
1335 if (!$rfc822re) {
1336 $rfc822re = make_rfc822re();
1337 }
1338 # * null list items are valid according to the RFC
1339 # * the '1' business is to aid in distinguishing failure from no results
1340
1341 my @r;
1342 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1343 $s =~ m/^$rfc822_char*$/) {
5f2441e9 1344 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
60db31ac 1345 push(@r, $1);
1b5e1cf6
JP
1346 }
1347 return wantarray ? (scalar(@r), @r) : 1;
1348 }
60db31ac 1349 return wantarray ? () : 0;
1b5e1cf6 1350}