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