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