3 # created from checkpatch.pl
5 # Print selected MAINTAINERS information for
6 # the files modified in a patch or for a file
8 # usage: perl scripts/get_maintainer.pl [OPTIONS] <patch>
9 # perl scripts/get_maintainer.pl [OPTIONS] -f <file>
11 # Licensed under the terms of the GNU GPL License version 2
18 use Getopt::Long qw(:config no_auto_abbrev);
22 my $email_usename = 1;
23 my $email_maintainer = 1;
24 my $email_reviewer = 1;
26 my $email_subscriber_list = 0;
28 my $email_git_all_signature_types = 0;
29 my $email_git_blame = 0;
30 my $email_git_blame_signatures = 1;
31 my $email_git_fallback = 1;
32 my $email_git_min_signatures = 1;
33 my $email_git_max_maintainers = 5;
34 my $email_git_min_percent = 5;
35 my $email_git_since = "1-year-ago";
36 my $email_hg_since = "-365";
38 my $email_remove_duplicates = 1;
39 my $email_use_mailmap = 1;
40 my $output_multiline = 1;
41 my $output_separator = ", ";
43 my $output_rolestats = 1;
51 my $from_filename = 0;
52 my $pattern_depth = 0;
60 my %commit_author_hash;
61 my %commit_signer_hash;
63 # Signature types of people who are either
64 # a) responsible for the code in question, or
65 # b) familiar enough with it to give relevant feedback
66 my @signature_tags = ();
67 push(@signature_tags, "Signed-off-by:");
68 push(@signature_tags, "Reviewed-by:");
69 push(@signature_tags, "Acked-by:");
71 my $signature_pattern = "\(" . join("|", @signature_tags) . "\)";
73 # rfc822 email address - preloaded methods go here.
74 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
75 my $rfc822_char = '[\\000-\\377]';
77 # VCS command support: class-like functions and strings
82 "execute_cmd" => \&git_execute_cmd,
83 "available" => '(which("git") ne "") && (-d ".git")',
85 "git log --no-color --follow --since=\$email_git_since " .
86 '--format="GitCommit: %H%n' .
87 'GitAuthor: %an <%ae>%n' .
92 "find_commit_signers_cmd" =>
93 "git log --no-color " .
94 '--format="GitCommit: %H%n' .
95 'GitAuthor: %an <%ae>%n' .
100 "find_commit_author_cmd" =>
101 "git log --no-color " .
102 '--format="GitCommit: %H%n' .
103 'GitAuthor: %an <%ae>%n' .
105 'GitSubject: %s%n"' .
107 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
108 "blame_file_cmd" => "git blame -l \$file",
109 "commit_pattern" => "^GitCommit: ([0-9a-f]{40,40})",
110 "blame_commit_pattern" => "^([0-9a-f]+) ",
111 "author_pattern" => "^GitAuthor: (.*)",
112 "subject_pattern" => "^GitSubject: (.*)",
116 "execute_cmd" => \&hg_execute_cmd,
117 "available" => '(which("hg") ne "") && (-d ".hg")',
118 "find_signers_cmd" =>
119 "hg log --date=\$email_hg_since " .
120 "--template='HgCommit: {node}\\n" .
121 "HgAuthor: {author}\\n" .
122 "HgSubject: {desc}\\n'" .
124 "find_commit_signers_cmd" =>
126 "--template='HgSubject: {desc}\\n'" .
128 "find_commit_author_cmd" =>
130 "--template='HgCommit: {node}\\n" .
131 "HgAuthor: {author}\\n" .
132 "HgSubject: {desc|firstline}\\n'" .
134 "blame_range_cmd" => "", # not supported
135 "blame_file_cmd" => "hg blame -n \$file",
136 "commit_pattern" => "^HgCommit: ([0-9a-f]{40,40})",
137 "blame_commit_pattern" => "^([ 0-9a-f]+):",
138 "author_pattern" => "^HgAuthor: (.*)",
139 "subject_pattern" => "^HgSubject: (.*)",
142 my $conf = which_conf(".get_maintainer.conf");
145 open(my $conffile, '<', "$conf")
146 or warn "$P: Can't find a readable .get_maintainer.conf file $!\n";
148 while (<$conffile>) {
151 $line =~ s/\s*\n?$//g;
155 next if ($line =~ m/^\s*#/);
156 next if ($line =~ m/^\s*$/);
158 my @words = split(" ", $line);
159 foreach my $word (@words) {
160 last if ($word =~ m/^#/);
161 push (@conf_args, $word);
165 unshift(@ARGV, @conf_args) if @conf_args;
170 'git!' => \$email_git,
171 'git-all-signature-types!' => \$email_git_all_signature_types,
172 'git-blame!' => \$email_git_blame,
173 'git-blame-signatures!' => \$email_git_blame_signatures,
174 'git-fallback!' => \$email_git_fallback,
175 'git-min-signatures=i' => \$email_git_min_signatures,
176 'git-max-maintainers=i' => \$email_git_max_maintainers,
177 'git-min-percent=i' => \$email_git_min_percent,
178 'git-since=s' => \$email_git_since,
179 'hg-since=s' => \$email_hg_since,
180 'i|interactive!' => \$interactive,
181 'remove-duplicates!' => \$email_remove_duplicates,
182 'mailmap!' => \$email_use_mailmap,
183 'm!' => \$email_maintainer,
184 'r!' => \$email_reviewer,
185 'n!' => \$email_usename,
186 'l!' => \$email_list,
187 's!' => \$email_subscriber_list,
188 'multiline!' => \$output_multiline,
189 'roles!' => \$output_roles,
190 'rolestats!' => \$output_rolestats,
191 'separator=s' => \$output_separator,
192 'subsystem!' => \$subsystem,
193 'status!' => \$status,
196 'pattern-depth=i' => \$pattern_depth,
197 'k|keywords!' => \$keywords,
198 'sections!' => \$sections,
199 'fe|file-emails!' => \$file_emails,
200 'f|file' => \$from_filename,
201 'v|version' => \$version,
202 'h|help|usage' => \$help,
204 die "$P: invalid argument - use --help if necessary\n";
213 print("${P} ${V}\n");
217 if (-t STDIN && !@ARGV) {
218 # We're talking to a terminal, but have no command line arguments.
219 die "$P: missing patchfile or -f file - use --help if necessary\n";
222 $output_multiline = 0 if ($output_separator ne ", ");
223 $output_rolestats = 1 if ($interactive);
224 $output_roles = 1 if ($output_rolestats);
236 my $selections = $email + $scm + $status + $subsystem + $web;
237 if ($selections == 0) {
238 die "$P: Missing required option: email, scm, status, subsystem or web\n";
243 ($email_maintainer + $email_reviewer +
244 $email_list + $email_subscriber_list +
245 $email_git + $email_git_blame) == 0) {
246 die "$P: Please select at least 1 email option\n";
249 if (!top_of_tree($lk_path)) {
250 die "$P: The current directory does not appear to be "
251 . "a QEMU source tree.\n";
254 ## Read MAINTAINERS for type/value pairs
259 open (my $maint, '<', "${lk_path}MAINTAINERS")
260 or die "$P: Can't open MAINTAINERS: $!\n";
264 if ($line =~ m/^(.):\s*(.*)/) {
268 ##Filename pattern matching
269 if ($type eq "F" || $type eq "X") {
270 $value =~ s@\.@\\\.@g; ##Convert . to \.
271 $value =~ s/\*/\.\*/g; ##Convert * to .*
272 $value =~ s/\?/\./g; ##Convert ? to .
273 ##if pattern is a directory and it lacks a trailing slash, add one
275 $value =~ s@([^/])$@$1/@;
277 } elsif ($type eq "K") {
278 $keyword_hash{@typevalue} = $value;
280 push(@typevalue, "$type:$value");
281 } elsif (!/^(\s)*$/) {
283 push(@typevalue, $line);
290 # Read mail address map
303 return if (!$email_use_mailmap || !(-f "${lk_path}.mailmap"));
305 open(my $mailmap_file, '<', "${lk_path}.mailmap")
306 or warn "$P: Can't open .mailmap: $!\n";
308 while (<$mailmap_file>) {
309 s/#.*$//; #strip comments
310 s/^\s+|\s+$//g; #trim
312 next if (/^\s*$/); #skip empty lines
313 #entries have one of the following formats:
316 # name1 <mail1> <mail2>
317 # name1 <mail1> name2 <mail2>
318 # (see man git-shortlog)
320 if (/^([^<]+)<([^>]+)>$/) {
324 $real_name =~ s/\s+$//;
325 ($real_name, $address) = parse_email("$real_name <$address>");
326 $mailmap->{names}->{$address} = $real_name;
328 } elsif (/^<([^>]+)>\s*<([^>]+)>$/) {
329 my $real_address = $1;
330 my $wrong_address = $2;
332 $mailmap->{addresses}->{$wrong_address} = $real_address;
334 } elsif (/^(.+)<([^>]+)>\s*<([^>]+)>$/) {
336 my $real_address = $2;
337 my $wrong_address = $3;
339 $real_name =~ s/\s+$//;
340 ($real_name, $real_address) =
341 parse_email("$real_name <$real_address>");
342 $mailmap->{names}->{$wrong_address} = $real_name;
343 $mailmap->{addresses}->{$wrong_address} = $real_address;
345 } elsif (/^(.+)<([^>]+)>\s*(.+)\s*<([^>]+)>$/) {
347 my $real_address = $2;
349 my $wrong_address = $4;
351 $real_name =~ s/\s+$//;
352 ($real_name, $real_address) =
353 parse_email("$real_name <$real_address>");
355 $wrong_name =~ s/\s+$//;
356 ($wrong_name, $wrong_address) =
357 parse_email("$wrong_name <$wrong_address>");
359 my $wrong_email = format_email($wrong_name, $wrong_address, 1);
360 $mailmap->{names}->{$wrong_email} = $real_name;
361 $mailmap->{addresses}->{$wrong_email} = $real_address;
364 close($mailmap_file);
367 ## use the filenames on the command line or find the filenames in the patchfiles
371 my @keyword_tvi = ();
372 my @file_emails = ();
375 push(@ARGV, "&STDIN");
378 foreach my $file (@ARGV) {
379 if ($file ne "&STDIN") {
380 ##if $file is a directory and it lacks a trailing slash, add one
382 $file =~ s@([^/])$@$1/@;
383 } elsif (!(-f $file)) {
384 die "$P: file '${file}' not found\n";
387 if ($from_filename) {
389 if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
390 open(my $f, '<', $file)
391 or die "$P: Can't open $file: $!\n";
392 my $text = do { local($/) ; <$f> };
395 foreach my $line (keys %keyword_hash) {
396 if ($text =~ m/$keyword_hash{$line}/x) {
397 push(@keyword_tvi, $line);
402 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;
403 push(@file_emails, clean_file_emails(@poss_addr));
407 my $file_cnt = @files;
410 open(my $patch, "< $file")
411 or die "$P: Can't open $file: $!\n";
413 # We can check arbitrary information before the patch
414 # like the commit message, mail headers, etc...
415 # This allows us to match arbitrary keywords against any part
416 # of a git format-patch generated file (subject tags, etc...)
418 my $patch_prefix = ""; #Parsing the intro
422 if (m/^\+\+\+\s+(\S+)/) {
424 $filename =~ s@^[^/]*/@@;
426 $lastfile = $filename;
427 push(@files, $filename);
428 $patch_prefix = "^[+-].*"; #Now parsing the actual patch
429 } elsif (m/^\@\@ -(\d+),(\d+)/) {
430 if ($email_git_blame) {
431 push(@range, "$lastfile:$1:$2");
433 } elsif ($keywords) {
434 foreach my $line (keys %keyword_hash) {
435 if ($patch_line =~ m/${patch_prefix}$keyword_hash{$line}/x) {
436 push(@keyword_tvi, $line);
443 if ($file_cnt == @files) {
444 warn "$P: file '${file}' doesn't appear to be a patch. "
445 . "Add -f to options?\n";
447 @files = sort_and_uniq(@files);
451 @file_emails = uniq(@file_emails);
454 my %email_hash_address;
462 my %deduplicate_name_hash = ();
463 my %deduplicate_address_hash = ();
465 my @maintainers = get_maintainers();
468 @maintainers = merge_email(@maintainers);
469 output(@maintainers);
478 @status = uniq(@status);
483 @subsystem = uniq(@subsystem);
494 sub range_is_maintained {
495 my ($start, $end) = @_;
497 for (my $i = $start; $i < $end; $i++) {
498 my $line = $typevalue[$i];
499 if ($line =~ m/^(.):\s*(.*)/) {
503 if ($value =~ /(maintain|support)/i) {
512 sub range_has_maintainer {
513 my ($start, $end) = @_;
515 for (my $i = $start; $i < $end; $i++) {
516 my $line = $typevalue[$i];
517 if ($line =~ m/^(.):\s*(.*)/) {
528 sub get_maintainers {
529 %email_hash_name = ();
530 %email_hash_address = ();
531 %commit_author_hash = ();
532 %commit_signer_hash = ();
540 %deduplicate_name_hash = ();
541 %deduplicate_address_hash = ();
542 if ($email_git_all_signature_types) {
543 $signature_pattern = "(.+?)[Bb][Yy]:";
545 $signature_pattern = "\(" . join("|", @signature_tags) . "\)";
548 # Find responsible parties
550 my %exact_pattern_match_hash = ();
552 foreach my $file (@files) {
555 my $tvi = find_first_section();
556 while ($tvi < @typevalue) {
557 my $start = find_starting_index($tvi);
558 my $end = find_ending_index($tvi);
562 #Do not match excluded file patterns
564 for ($i = $start; $i < $end; $i++) {
565 my $line = $typevalue[$i];
566 if ($line =~ m/^(.):\s*(.*)/) {
570 if (file_match_pattern($file, $value)) {
579 for ($i = $start; $i < $end; $i++) {
580 my $line = $typevalue[$i];
581 if ($line =~ m/^(.):\s*(.*)/) {
585 if (file_match_pattern($file, $value)) {
586 my $value_pd = ($value =~ tr@/@@);
587 my $file_pd = ($file =~ tr@/@@);
588 $value_pd++ if (substr($value,-1,1) ne "/");
589 $value_pd = -1 if ($value =~ /^\.\*/);
590 if ($value_pd >= $file_pd &&
591 range_is_maintained($start, $end) &&
592 range_has_maintainer($start, $end)) {
593 $exact_pattern_match_hash{$file} = 1;
595 if ($pattern_depth == 0 ||
596 (($file_pd - $value_pd) < $pattern_depth)) {
597 $hash{$tvi} = $value_pd;
607 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
608 add_categories($line);
611 my $start = find_starting_index($line);
612 my $end = find_ending_index($line);
613 for ($i = $start; $i < $end; $i++) {
614 my $line = $typevalue[$i];
615 if ($line =~ /^[FX]:/) { ##Restore file patterns
616 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
617 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
618 $line =~ s/\\\./\./g; ##Convert \. to .
619 $line =~ s/\.\*/\*/g; ##Convert .* to *
621 $line =~ s/^([A-Z]):/$1:\t/g;
630 @keyword_tvi = sort_and_uniq(@keyword_tvi);
631 foreach my $line (@keyword_tvi) {
632 add_categories($line);
636 foreach my $email (@email_to, @list_to) {
637 $email->[0] = deduplicate_email($email->[0]);
641 if (! $interactive) {
642 $email_git_fallback = 0 if @email_to > 0 || $email_git || $email_git_blame;
643 if ($email_git_fallback) {
644 print STDERR "get_maintainer.pl: No maintainers found, printing recent contributors.\n";
645 print STDERR "get_maintainer.pl: Do not blindly cc: them on patches! Use common sense.\n";
650 foreach my $file (@files) {
651 if ($email_git || ($email_git_fallback &&
652 !$exact_pattern_match_hash{$file})) {
653 vcs_file_signoffs($file);
655 if ($email_git_blame) {
656 vcs_file_blame($file);
660 foreach my $email (@file_emails) {
661 my ($name, $address) = parse_email($email);
663 my $tmp_email = format_email($name, $address, $email_usename);
664 push_email_address($tmp_email, '');
665 add_role($tmp_email, 'in file');
670 if ($email || $email_list) {
672 @to = (@to, @email_to);
675 @to = (@to, @list_to);
680 @to = interactive_get_maintainers(\@to);
686 sub file_match_pattern {
687 my ($file, $pattern) = @_;
688 if (substr($pattern, -1) eq "/") {
689 if ($file =~ m@^$pattern@) {
693 if ($file =~ m@^$pattern@) {
694 my $s1 = ($file =~ tr@/@@);
695 my $s2 = ($pattern =~ tr@/@@);
706 usage: $P [options] patchfile
707 $P [options] -f file|directory
710 MAINTAINER field selection options:
711 --email => print email address(es) if any
712 --git => include recent git \*-by: signers
713 --git-all-signature-types => include signers regardless of signature type
714 or use only ${signature_pattern} signers (default: $email_git_all_signature_types)
715 --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
716 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
717 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
718 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
719 --git-blame => use git blame to find modified commits for patch or file
720 --git-since => git history to use (default: $email_git_since)
721 --hg-since => hg history to use (default: $email_hg_since)
722 --interactive => display a menu (mostly useful if used with the --git option)
723 --m => include maintainer(s) if any
724 --r => include reviewer(s) if any
725 --n => include name 'Full Name <addr\@domain.tld>'
726 --l => include list(s) if any
727 --s => include subscriber only list(s) if any
728 --remove-duplicates => minimize duplicate email names/addresses
729 --roles => show roles (status:subsystem, git-signer, list, etc...)
730 --rolestats => show roles and statistics (commits/total_commits, %)
731 --file-emails => add email addresses found in -f file (default: 0 (off))
732 --scm => print SCM tree(s) if any
733 --status => print status if any
734 --subsystem => print subsystem name if any
735 --web => print website(s) if any
738 --separator [, ] => separator for multiple entries on 1 line
739 using --separator also sets --nomultiline if --separator is not [, ]
740 --multiline => print 1 entry per line
743 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
744 --keywords => scan patch for keywords (default: $keywords)
745 --sections => print all of the subsystem sections with pattern matches
746 --mailmap => use .mailmap file (default: $email_use_mailmap)
747 --version => show version
748 --help => show this help information
751 [--email --nogit --git-fallback --m --r --n --l --multiline --pattern-depth=0
752 --remove-duplicates --rolestats]
755 Using "-f directory" may give unexpected results:
756 Used with "--git", git signators for _all_ files in and below
757 directory are examined as git recurses directories.
758 Any specified X: (exclude) pattern matches are _not_ ignored.
759 Used with "--nogit", directory is used as a pattern match,
760 no individual file within the directory or subdirectory
762 Used with "--git-blame", does not iterate all files in directory
763 Using "--git-blame" is slow and may add old committers and authors
764 that are no longer active maintainers to the output.
765 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
766 other automated tools that expect only ["name"] <email address>
767 may not work because of additional output after <email address>.
768 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
769 not the percentage of the entire file authored. # of commits is
770 not a good measure of amount of code authored. 1 major commit may
771 contain a thousand lines, 5 trivial commits may modify a single line.
772 If git is not installed, but mercurial (hg) is installed and an .hg
773 repository exists, the following options apply to mercurial:
775 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
777 Use --hg-since not --git-since to control date selection
778 File ".get_maintainer.conf", if it exists in the QEMU source root
779 directory, can change whatever get_maintainer defaults are desired.
780 Entries in this file can be any command line argument.
781 This file is prepended to any additional command line arguments.
782 Multiple lines and # comments are allowed.
789 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
792 if ( (-f "${lk_path}COPYING")
793 && (-f "${lk_path}MAINTAINERS")
794 && (-f "${lk_path}Makefile")
795 && (-d "${lk_path}docs")
796 && (-f "${lk_path}VERSION")
797 && (-f "${lk_path}vl.c")) {
804 my ($formatted_email) = @_;
809 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
812 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
814 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
818 $name =~ s/^\s+|\s+$//g;
819 $name =~ s/^\"|\"$//g;
820 $address =~ s/^\s+|\s+$//g;
822 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
823 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
827 return ($name, $address);
831 my ($name, $address, $usename) = @_;
835 $name =~ s/^\s+|\s+$//g;
836 $name =~ s/^\"|\"$//g;
837 $address =~ s/^\s+|\s+$//g;
839 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
840 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
846 $formatted_email = "$address";
848 $formatted_email = "$name <$address>";
851 $formatted_email = $address;
854 return $formatted_email;
857 sub find_first_section {
860 while ($index < @typevalue) {
861 my $tv = $typevalue[$index];
862 if (($tv =~ m/^(.):\s*(.*)/)) {
871 sub find_starting_index {
875 my $tv = $typevalue[$index];
876 if (!($tv =~ m/^(.):\s*(.*)/)) {
885 sub find_ending_index {
888 while ($index < @typevalue) {
889 my $tv = $typevalue[$index];
890 if (!($tv =~ m/^(.):\s*(.*)/)) {
899 sub get_subsystem_name {
902 my $start = find_starting_index($index);
904 my $subsystem = $typevalue[$start];
905 if (length($subsystem) > 20) {
906 $subsystem = substr($subsystem, 0, 17);
907 $subsystem =~ s/\s*$//;
908 $subsystem = $subsystem . "...";
913 sub get_maintainer_role {
917 my $start = find_starting_index($index);
918 my $end = find_ending_index($index);
920 my $role = "unknown";
921 my $subsystem = get_subsystem_name($index);
923 for ($i = $start + 1; $i < $end; $i++) {
924 my $tv = $typevalue[$i];
925 if ($tv =~ m/^(.):\s*(.*)/) {
935 if ($role eq "supported") {
937 } elsif ($role eq "maintained") {
938 $role = "maintainer";
939 } elsif ($role eq "odd fixes") {
941 } elsif ($role eq "orphan") {
942 $role = "orphan minder";
943 } elsif ($role eq "obsolete") {
944 $role = "obsolete minder";
945 } elsif ($role eq "buried alive in reporters") {
946 $role = "chief penguin";
949 return $role . ":" . $subsystem;
955 my $subsystem = get_subsystem_name($index);
957 if ($subsystem eq "THE REST") {
968 my $start = find_starting_index($index);
969 my $end = find_ending_index($index);
971 push(@subsystem, $typevalue[$start]);
973 for ($i = $start + 1; $i < $end; $i++) {
974 my $tv = $typevalue[$i];
975 if ($tv =~ m/^(.):\s*(.*)/) {
979 my $list_address = $pvalue;
980 my $list_additional = "";
981 my $list_role = get_list_role($i);
983 if ($list_role ne "") {
984 $list_role = ":" . $list_role;
986 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
988 $list_additional = $2;
990 if ($list_additional =~ m/subscribers-only/) {
991 if ($email_subscriber_list) {
992 if (!$hash_list_to{lc($list_address)}) {
993 $hash_list_to{lc($list_address)} = 1;
994 push(@list_to, [$list_address,
995 "subscriber list${list_role}"]);
1000 if (!$hash_list_to{lc($list_address)}) {
1001 $hash_list_to{lc($list_address)} = 1;
1002 if ($list_additional =~ m/moderated/) {
1003 push(@list_to, [$list_address,
1004 "moderated list${list_role}"]);
1006 push(@list_to, [$list_address,
1007 "open list${list_role}"]);
1012 } elsif ($ptype eq "M") {
1013 my ($name, $address) = parse_email($pvalue);
1016 my $tv = $typevalue[$i - 1];
1017 if ($tv =~ m/^(.):\s*(.*)/) {
1020 $pvalue = format_email($name, $address, $email_usename);
1025 if ($email_maintainer) {
1026 my $role = get_maintainer_role($i);
1027 push_email_addresses($pvalue, $role);
1029 } elsif ($ptype eq "R") {
1030 my ($name, $address) = parse_email($pvalue);
1033 my $tv = $typevalue[$i - 1];
1034 if ($tv =~ m/^(.):\s*(.*)/) {
1037 $pvalue = format_email($name, $address, $email_usename);
1042 if ($email_reviewer) {
1043 my $subsystem = get_subsystem_name($i);
1044 push_email_addresses($pvalue, "reviewer:$subsystem");
1046 } elsif ($ptype eq "T") {
1047 push(@scm, $pvalue);
1048 } elsif ($ptype eq "W") {
1049 push(@web, $pvalue);
1050 } elsif ($ptype eq "S") {
1051 push(@status, $pvalue);
1058 my ($name, $address) = @_;
1060 return 1 if (($name eq "") && ($address eq ""));
1061 return 1 if (($name ne "") && exists($email_hash_name{lc($name)}));
1062 return 1 if (($address ne "") && exists($email_hash_address{lc($address)}));
1067 sub push_email_address {
1068 my ($line, $role) = @_;
1070 my ($name, $address) = parse_email($line);
1072 if ($address eq "") {
1076 if (!$email_remove_duplicates) {
1077 push(@email_to, [format_email($name, $address, $email_usename), $role]);
1078 } elsif (!email_inuse($name, $address)) {
1079 push(@email_to, [format_email($name, $address, $email_usename), $role]);
1080 $email_hash_name{lc($name)}++ if ($name ne "");
1081 $email_hash_address{lc($address)}++;
1087 sub push_email_addresses {
1088 my ($address, $role) = @_;
1090 my @address_list = ();
1092 if (rfc822_valid($address)) {
1093 push_email_address($address, $role);
1094 } elsif (@address_list = rfc822_validlist($address)) {
1095 my $array_count = shift(@address_list);
1096 while (my $entry = shift(@address_list)) {
1097 push_email_address($entry, $role);
1100 if (!push_email_address($address, $role)) {
1101 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
1107 my ($line, $role) = @_;
1109 my ($name, $address) = parse_email($line);
1110 my $email = format_email($name, $address, $email_usename);
1112 foreach my $entry (@email_to) {
1113 if ($email_remove_duplicates) {
1114 my ($entry_name, $entry_address) = parse_email($entry->[0]);
1115 if (($name eq $entry_name || $address eq $entry_address)
1116 && ($role eq "" || !($entry->[1] =~ m/$role/))
1118 if ($entry->[1] eq "") {
1119 $entry->[1] = "$role";
1121 $entry->[1] = "$entry->[1],$role";
1125 if ($email eq $entry->[0]
1126 && ($role eq "" || !($entry->[1] =~ m/$role/))
1128 if ($entry->[1] eq "") {
1129 $entry->[1] = "$role";
1131 $entry->[1] = "$entry->[1],$role";
1141 foreach my $path (split(/:/, $ENV{PATH})) {
1142 if (-e "$path/$bin") {
1143 return "$path/$bin";
1153 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1154 if (-e "$path/$conf") {
1155 return "$path/$conf";
1165 my ($name, $address) = parse_email($line);
1166 my $email = format_email($name, $address, 1);
1167 my $real_name = $name;
1168 my $real_address = $address;
1170 if (exists $mailmap->{names}->{$email} ||
1171 exists $mailmap->{addresses}->{$email}) {
1172 if (exists $mailmap->{names}->{$email}) {
1173 $real_name = $mailmap->{names}->{$email};
1175 if (exists $mailmap->{addresses}->{$email}) {
1176 $real_address = $mailmap->{addresses}->{$email};
1179 if (exists $mailmap->{names}->{$address}) {
1180 $real_name = $mailmap->{names}->{$address};
1182 if (exists $mailmap->{addresses}->{$address}) {
1183 $real_address = $mailmap->{addresses}->{$address};
1186 return format_email($real_name, $real_address, 1);
1190 my (@addresses) = @_;
1192 my @mapped_emails = ();
1193 foreach my $line (@addresses) {
1194 push(@mapped_emails, mailmap_email($line));
1196 merge_by_realname(@mapped_emails) if ($email_use_mailmap);
1197 return @mapped_emails;
1200 sub merge_by_realname {
1204 foreach my $email (@emails) {
1205 my ($name, $address) = parse_email($email);
1206 if (exists $address_map{$name}) {
1207 $address = $address_map{$name};
1208 $email = format_email($name, $address, 1);
1210 $address_map{$name} = $address;
1215 sub git_execute_cmd {
1219 my $output = `$cmd`;
1220 $output =~ s/^\s*//gm;
1221 @lines = split("\n", $output);
1226 sub hg_execute_cmd {
1230 my $output = `$cmd`;
1231 @lines = split("\n", $output);
1236 sub extract_formatted_signatures {
1237 my (@signature_lines) = @_;
1239 my @type = @signature_lines;
1241 s/\s*(.*):.*/$1/ for (@type);
1244 s/\s*.*:\s*(.+)\s*/$1/ for (@signature_lines);
1246 ## Reformat email addresses (with names) to avoid badly written signatures
1248 foreach my $signer (@signature_lines) {
1249 $signer = deduplicate_email($signer);
1252 return (\@type, \@signature_lines);
1255 sub vcs_find_signers {
1259 my @signatures = ();
1261 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1263 my $pattern = $VCS_cmds{"commit_pattern"};
1265 $commits = grep(/$pattern/, @lines); # of commits
1267 @signatures = grep(/^[ \t]*${signature_pattern}.*\@.*$/, @lines);
1269 return (0, @signatures) if !@signatures;
1271 save_commits_by_author(@lines) if ($interactive);
1272 save_commits_by_signer(@lines) if ($interactive);
1274 my ($types_ref, $signers_ref) = extract_formatted_signatures(@signatures);
1276 return ($commits, @$signers_ref);
1279 sub vcs_find_author {
1283 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1285 return @lines if !@lines;
1288 foreach my $line (@lines) {
1289 if ($line =~ m/$VCS_cmds{"author_pattern"}/) {
1291 my ($name, $address) = parse_email($author);
1292 $author = format_email($name, $address, 1);
1293 push(@authors, $author);
1297 save_commits_by_author(@lines) if ($interactive);
1298 save_commits_by_signer(@lines) if ($interactive);
1303 sub vcs_save_commits {
1308 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1310 foreach my $line (@lines) {
1311 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1324 return @commits if (!(-f $file));
1326 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1327 my @all_commits = ();
1329 $cmd = $VCS_cmds{"blame_file_cmd"};
1330 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1331 @all_commits = vcs_save_commits($cmd);
1333 foreach my $file_range_diff (@range) {
1334 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1336 my $diff_start = $2;
1337 my $diff_length = $3;
1338 next if ("$file" ne "$diff_file");
1339 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1340 push(@commits, $all_commits[$i]);
1344 foreach my $file_range_diff (@range) {
1345 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1347 my $diff_start = $2;
1348 my $diff_length = $3;
1349 next if ("$file" ne "$diff_file");
1350 $cmd = $VCS_cmds{"blame_range_cmd"};
1351 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1352 push(@commits, vcs_save_commits($cmd));
1355 $cmd = $VCS_cmds{"blame_file_cmd"};
1356 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1357 @commits = vcs_save_commits($cmd);
1360 foreach my $commit (@commits) {
1361 $commit =~ s/^\^//g;
1367 my $printed_novcs = 0;
1369 %VCS_cmds = %VCS_cmds_git;
1370 return 1 if eval $VCS_cmds{"available"};
1371 %VCS_cmds = %VCS_cmds_hg;
1372 return 2 if eval $VCS_cmds{"available"};
1374 if (!$printed_novcs) {
1375 warn("$P: No supported VCS found. Add --nogit to options?\n");
1376 warn("Using a git repository produces better results.\n");
1377 warn("Try latest git repository using:\n");
1378 warn("git clone git://git.qemu-project.org/qemu.git\n");
1386 return $vcs_used == 1;
1390 return $vcs_used == 2;
1393 sub interactive_get_maintainers {
1394 my ($list_ref) = @_;
1395 my @list = @$list_ref;
1404 foreach my $entry (@list) {
1405 $maintained = 1 if ($entry->[1] =~ /^(maintainer|supporter)/i);
1406 $selected{$count} = 1;
1407 $authored{$count} = 0;
1408 $signed{$count} = 0;
1414 my $print_options = 0;
1419 printf STDERR "\n%1s %2s %-65s",
1420 "*", "#", "email/list and role:stats";
1422 ($email_git_fallback && !$maintained) ||
1424 print STDERR "auth sign";
1427 foreach my $entry (@list) {
1428 my $email = $entry->[0];
1429 my $role = $entry->[1];
1431 $sel = "*" if ($selected{$count});
1432 my $commit_author = $commit_author_hash{$email};
1433 my $commit_signer = $commit_signer_hash{$email};
1436 $authored++ for (@{$commit_author});
1437 $signed++ for (@{$commit_signer});
1438 printf STDERR "%1s %2d %-65s", $sel, $count + 1, $email;
1439 printf STDERR "%4d %4d", $authored, $signed
1440 if ($authored > 0 || $signed > 0);
1441 printf STDERR "\n %s\n", $role;
1442 if ($authored{$count}) {
1443 my $commit_author = $commit_author_hash{$email};
1444 foreach my $ref (@{$commit_author}) {
1445 print STDERR " Author: @{$ref}[1]\n";
1448 if ($signed{$count}) {
1449 my $commit_signer = $commit_signer_hash{$email};
1450 foreach my $ref (@{$commit_signer}) {
1451 print STDERR " @{$ref}[2]: @{$ref}[1]\n";
1458 my $date_ref = \$email_git_since;
1459 $date_ref = \$email_hg_since if (vcs_is_hg());
1460 if ($print_options) {
1465 Version Control options:
1466 g use git history [$email_git]
1467 gf use git-fallback [$email_git_fallback]
1468 b use git blame [$email_git_blame]
1469 bs use blame signatures [$email_git_blame_signatures]
1470 c# minimum commits [$email_git_min_signatures]
1471 %# min percent [$email_git_min_percent]
1472 d# history to use [$$date_ref]
1473 x# max maintainers [$email_git_max_maintainers]
1474 t all signature types [$email_git_all_signature_types]
1475 m use .mailmap [$email_use_mailmap]
1482 tm toggle maintainers
1483 tg toggle git entries
1484 tl toggle open list entries
1485 ts toggle subscriber list entries
1486 f emails in file [$file_emails]
1487 k keywords in file [$keywords]
1488 r remove duplicates [$email_remove_duplicates]
1489 p# pattern match depth [$pattern_depth]
1493 "\n#(toggle), A#(author), S#(signed) *(all), ^(none), O(options), Y(approve): ";
1495 my $input = <STDIN>;
1500 my @wish = split(/[, ]+/, $input);
1501 foreach my $nr (@wish) {
1503 my $sel = substr($nr, 0, 1);
1504 my $str = substr($nr, 1);
1506 $val = $1 if $str =~ /^(\d+)$/;
1511 $output_rolestats = 0;
1514 } elsif ($nr =~ /^\d+$/ && $nr > 0 && $nr <= $count) {
1515 $selected{$nr - 1} = !$selected{$nr - 1};
1516 } elsif ($sel eq "*" || $sel eq '^') {
1518 $toggle = 1 if ($sel eq '*');
1519 for (my $i = 0; $i < $count; $i++) {
1520 $selected{$i} = $toggle;
1522 } elsif ($sel eq "0") {
1523 for (my $i = 0; $i < $count; $i++) {
1524 $selected{$i} = !$selected{$i};
1526 } elsif ($sel eq "t") {
1527 if (lc($str) eq "m") {
1528 for (my $i = 0; $i < $count; $i++) {
1529 $selected{$i} = !$selected{$i}
1530 if ($list[$i]->[1] =~ /^(maintainer|supporter)/i);
1532 } elsif (lc($str) eq "g") {
1533 for (my $i = 0; $i < $count; $i++) {
1534 $selected{$i} = !$selected{$i}
1535 if ($list[$i]->[1] =~ /^(author|commit|signer)/i);
1537 } elsif (lc($str) eq "l") {
1538 for (my $i = 0; $i < $count; $i++) {
1539 $selected{$i} = !$selected{$i}
1540 if ($list[$i]->[1] =~ /^(open list)/i);
1542 } elsif (lc($str) eq "s") {
1543 for (my $i = 0; $i < $count; $i++) {
1544 $selected{$i} = !$selected{$i}
1545 if ($list[$i]->[1] =~ /^(subscriber list)/i);
1548 } elsif ($sel eq "a") {
1549 if ($val > 0 && $val <= $count) {
1550 $authored{$val - 1} = !$authored{$val - 1};
1551 } elsif ($str eq '*' || $str eq '^') {
1553 $toggle = 1 if ($str eq '*');
1554 for (my $i = 0; $i < $count; $i++) {
1555 $authored{$i} = $toggle;
1558 } elsif ($sel eq "s") {
1559 if ($val > 0 && $val <= $count) {
1560 $signed{$val - 1} = !$signed{$val - 1};
1561 } elsif ($str eq '*' || $str eq '^') {
1563 $toggle = 1 if ($str eq '*');
1564 for (my $i = 0; $i < $count; $i++) {
1565 $signed{$i} = $toggle;
1568 } elsif ($sel eq "o") {
1571 } elsif ($sel eq "g") {
1573 bool_invert(\$email_git_fallback);
1575 bool_invert(\$email_git);
1578 } elsif ($sel eq "b") {
1580 bool_invert(\$email_git_blame_signatures);
1582 bool_invert(\$email_git_blame);
1585 } elsif ($sel eq "c") {
1587 $email_git_min_signatures = $val;
1590 } elsif ($sel eq "x") {
1592 $email_git_max_maintainers = $val;
1595 } elsif ($sel eq "%") {
1596 if ($str ne "" && $val >= 0) {
1597 $email_git_min_percent = $val;
1600 } elsif ($sel eq "d") {
1602 $email_git_since = $str;
1603 } elsif (vcs_is_hg()) {
1604 $email_hg_since = $str;
1607 } elsif ($sel eq "t") {
1608 bool_invert(\$email_git_all_signature_types);
1610 } elsif ($sel eq "f") {
1611 bool_invert(\$file_emails);
1613 } elsif ($sel eq "r") {
1614 bool_invert(\$email_remove_duplicates);
1616 } elsif ($sel eq "m") {
1617 bool_invert(\$email_use_mailmap);
1620 } elsif ($sel eq "k") {
1621 bool_invert(\$keywords);
1623 } elsif ($sel eq "p") {
1624 if ($str ne "" && $val >= 0) {
1625 $pattern_depth = $val;
1628 } elsif ($sel eq "h" || $sel eq "?") {
1631 Interactive mode allows you to select the various maintainers, submitters,
1632 commit signers and mailing lists that could be CC'd on a patch.
1634 Any *'d entry is selected.
1636 If you have git or hg installed, you can choose to summarize the commit
1637 history of files in the patch. Also, each line of the current file can
1638 be matched to its commit author and that commits signers with blame.
1640 Various knobs exist to control the length of time for active commit
1641 tracking, the maximum number of commit authors and signers to add,
1644 Enter selections at the prompt until you are satisfied that the selected
1645 maintainers are appropriate. You may enter multiple selections separated
1646 by either commas or spaces.
1650 print STDERR "invalid option: '$nr'\n";
1655 print STDERR "git-blame can be very slow, please have patience..."
1656 if ($email_git_blame);
1657 goto &get_maintainers;
1661 #drop not selected entries
1663 my @new_emailto = ();
1664 foreach my $entry (@list) {
1665 if ($selected{$count}) {
1666 push(@new_emailto, $list[$count]);
1670 return @new_emailto;
1674 my ($bool_ref) = @_;
1683 sub deduplicate_email {
1687 my ($name, $address) = parse_email($email);
1688 $email = format_email($name, $address, 1);
1689 $email = mailmap_email($email);
1691 return $email if (!$email_remove_duplicates);
1693 ($name, $address) = parse_email($email);
1695 if ($name ne "" && $deduplicate_name_hash{lc($name)}) {
1696 $name = $deduplicate_name_hash{lc($name)}->[0];
1697 $address = $deduplicate_name_hash{lc($name)}->[1];
1699 } elsif ($deduplicate_address_hash{lc($address)}) {
1700 $name = $deduplicate_address_hash{lc($address)}->[0];
1701 $address = $deduplicate_address_hash{lc($address)}->[1];
1705 $deduplicate_name_hash{lc($name)} = [ $name, $address ];
1706 $deduplicate_address_hash{lc($address)} = [ $name, $address ];
1708 $email = format_email($name, $address, 1);
1709 $email = mailmap_email($email);
1713 sub save_commits_by_author {
1720 foreach my $line (@lines) {
1721 if ($line =~ m/$VCS_cmds{"author_pattern"}/) {
1723 $author = deduplicate_email($author);
1724 push(@authors, $author);
1726 push(@commits, $1) if ($line =~ m/$VCS_cmds{"commit_pattern"}/);
1727 push(@subjects, $1) if ($line =~ m/$VCS_cmds{"subject_pattern"}/);
1730 for (my $i = 0; $i < @authors; $i++) {
1732 foreach my $ref(@{$commit_author_hash{$authors[$i]}}) {
1733 if (@{$ref}[0] eq $commits[$i] &&
1734 @{$ref}[1] eq $subjects[$i]) {
1740 push(@{$commit_author_hash{$authors[$i]}},
1741 [ ($commits[$i], $subjects[$i]) ]);
1746 sub save_commits_by_signer {
1752 foreach my $line (@lines) {
1753 $commit = $1 if ($line =~ m/$VCS_cmds{"commit_pattern"}/);
1754 $subject = $1 if ($line =~ m/$VCS_cmds{"subject_pattern"}/);
1755 if ($line =~ /^[ \t]*${signature_pattern}.*\@.*$/) {
1756 my @signatures = ($line);
1757 my ($types_ref, $signers_ref) = extract_formatted_signatures(@signatures);
1758 my @types = @$types_ref;
1759 my @signers = @$signers_ref;
1761 my $type = $types[0];
1762 my $signer = $signers[0];
1764 $signer = deduplicate_email($signer);
1767 foreach my $ref(@{$commit_signer_hash{$signer}}) {
1768 if (@{$ref}[0] eq $commit &&
1769 @{$ref}[1] eq $subject &&
1770 @{$ref}[2] eq $type) {
1776 push(@{$commit_signer_hash{$signer}},
1777 [ ($commit, $subject, $type) ]);
1784 my ($role, $divisor, @lines) = @_;
1789 return if (@lines <= 0);
1791 if ($divisor <= 0) {
1792 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1796 @lines = mailmap(@lines);
1798 return if (@lines <= 0);
1800 @lines = sort(@lines);
1803 $hash{$_}++ for @lines;
1806 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1807 my $sign_offs = $hash{$line};
1808 my $percent = $sign_offs * 100 / $divisor;
1810 $percent = 100 if ($percent > 100);
1812 last if ($sign_offs < $email_git_min_signatures ||
1813 $count > $email_git_max_maintainers ||
1814 $percent < $email_git_min_percent);
1815 push_email_address($line, '');
1816 if ($output_rolestats) {
1817 my $fmt_percent = sprintf("%.0f", $percent);
1818 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1820 add_role($line, $role);
1825 sub vcs_file_signoffs {
1831 $vcs_used = vcs_exists();
1832 return if (!$vcs_used);
1834 my $cmd = $VCS_cmds{"find_signers_cmd"};
1835 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1837 ($commits, @signers) = vcs_find_signers($cmd);
1839 foreach my $signer (@signers) {
1840 $signer = deduplicate_email($signer);
1843 vcs_assign("commit_signer", $commits, @signers);
1846 sub vcs_file_blame {
1850 my @all_commits = ();
1855 $vcs_used = vcs_exists();
1856 return if (!$vcs_used);
1858 @all_commits = vcs_blame($file);
1859 @commits = uniq(@all_commits);
1860 $total_commits = @commits;
1861 $total_lines = @all_commits;
1863 if ($email_git_blame_signatures) {
1866 my @commit_signers = ();
1867 my $commit = join(" -r ", @commits);
1870 $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1871 $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
1873 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1875 push(@signers, @commit_signers);
1877 foreach my $commit (@commits) {
1879 my @commit_signers = ();
1882 $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1883 $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
1885 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1887 push(@signers, @commit_signers);
1892 if ($from_filename) {
1893 if ($output_rolestats) {
1895 if (vcs_is_hg()) {{ # Double brace for last exit
1897 my @commit_signers = ();
1898 @commits = uniq(@commits);
1899 @commits = sort(@commits);
1900 my $commit = join(" -r ", @commits);
1903 $cmd = $VCS_cmds{"find_commit_author_cmd"};
1904 $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
1908 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1913 foreach my $line (@lines) {
1914 if ($line =~ m/$VCS_cmds{"author_pattern"}/) {
1916 $author = deduplicate_email($author);
1917 push(@authors, $author);
1921 save_commits_by_author(@lines) if ($interactive);
1922 save_commits_by_signer(@lines) if ($interactive);
1924 push(@signers, @authors);
1927 foreach my $commit (@commits) {
1929 my $cmd = $VCS_cmds{"find_commit_author_cmd"};
1930 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1931 my @author = vcs_find_author($cmd);
1934 my $formatted_author = deduplicate_email($author[0]);
1936 my $count = grep(/$commit/, @all_commits);
1937 for ($i = 0; $i < $count ; $i++) {
1938 push(@blame_signers, $formatted_author);
1942 if (@blame_signers) {
1943 vcs_assign("authored lines", $total_lines, @blame_signers);
1946 foreach my $signer (@signers) {
1947 $signer = deduplicate_email($signer);
1949 vcs_assign("commits", $total_commits, @signers);
1951 foreach my $signer (@signers) {
1952 $signer = deduplicate_email($signer);
1954 vcs_assign("modified commits", $total_commits, @signers);
1962 @parms = grep(!$saw{$_}++, @parms);
1970 @parms = sort @parms;
1971 @parms = grep(!$saw{$_}++, @parms);
1975 sub clean_file_emails {
1976 my (@file_emails) = @_;
1977 my @fmt_emails = ();
1979 foreach my $email (@file_emails) {
1980 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1981 my ($name, $address) = parse_email($email);
1982 if ($name eq '"[,\.]"') {
1986 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1988 my $first = $nw[@nw - 3];
1989 my $middle = $nw[@nw - 2];
1990 my $last = $nw[@nw - 1];
1992 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1993 (length($first) == 2 && substr($first, -1) eq ".")) ||
1994 (length($middle) == 1 ||
1995 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1996 $name = "$first $middle $last";
1998 $name = "$middle $last";
2002 if (substr($name, -1) =~ /[,\.]/) {
2003 $name = substr($name, 0, length($name) - 1);
2004 } elsif (substr($name, -2) =~ /[,\.]"/) {
2005 $name = substr($name, 0, length($name) - 2) . '"';
2008 if (substr($name, 0, 1) =~ /[,\.]/) {
2009 $name = substr($name, 1, length($name) - 1);
2010 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
2011 $name = '"' . substr($name, 2, length($name) - 2);
2014 my $fmt_email = format_email($name, $address, $email_usename);
2015 push(@fmt_emails, $fmt_email);
2025 my ($address, $role) = @$_;
2026 if (!$saw{$address}) {
2027 if ($output_roles) {
2028 push(@lines, "$address ($role)");
2030 push(@lines, $address);
2042 if ($output_multiline) {
2043 foreach my $line (@parms) {
2047 print(join($output_separator, @parms));
2055 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
2056 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
2057 # This regexp will only work on addresses which have had comments stripped
2058 # and replaced with rfc822_lwsp.
2060 my $specials = '()<>@,;:\\\\".\\[\\]';
2061 my $controls = '\\000-\\037\\177';
2063 my $dtext = "[^\\[\\]\\r\\\\]";
2064 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
2066 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
2068 # Use zero-width assertion to spot the limit of an atom. A simple
2069 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
2070 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
2071 my $word = "(?:$atom|$quoted_string)";
2072 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
2074 my $sub_domain = "(?:$atom|$domain_literal)";
2075 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
2077 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
2079 my $phrase = "$word*";
2080 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
2081 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
2082 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
2084 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
2085 my $address = "(?:$mailbox|$group)";
2087 return "$rfc822_lwsp*$address";
2090 sub rfc822_strip_comments {
2092 # Recursively remove comments, and replace with a single space. The simpler
2093 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
2094 # chars in atoms, for example.
2096 while ($s =~ s/^((?:[^"\\]|\\.)*
2097 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
2098 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
2102 # valid: returns true if the parameter is an RFC822 valid address
2105 my $s = rfc822_strip_comments(shift);
2108 $rfc822re = make_rfc822re();
2111 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
2114 # validlist: In scalar context, returns true if the parameter is an RFC822
2115 # valid list of addresses.
2117 # In list context, returns an empty list on failure (an invalid
2118 # address was found); otherwise a list whose first element is the
2119 # number of addresses found and whose remaining elements are the
2120 # addresses. This is needed to disambiguate failure (invalid)
2121 # from success with no addresses found, because an empty string is
2124 sub rfc822_validlist {
2125 my $s = rfc822_strip_comments(shift);
2128 $rfc822re = make_rfc822re();
2130 # * null list items are valid according to the RFC
2131 # * the '1' business is to aid in distinguishing failure from no results
2134 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
2135 $s =~ m/^$rfc822_char*$/) {
2136 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
2139 return wantarray ? (scalar(@r), @r) : 1;
2141 return wantarray ? () : 0;