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