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