blob: 77f4f2e4cd81183d769c14e1778fb31a1c99cf98 [file] [log] [blame]
Joe Perchescb7301c2009-04-07 20:40:12 -07001#!/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#
Roel Kluin3bd7bf52009-11-11 14:26:13 -08008# usage: perl scripts/get_maintainer.pl [OPTIONS] <patch>
9# perl scripts/get_maintainer.pl [OPTIONS] -f <file>
Joe Perchescb7301c2009-04-07 20:40:12 -070010#
11# Licensed under the terms of the GNU GPL License version 2
12
13use strict;
14
15my $P = $0;
Joe Perches63ab52d2010-10-26 14:22:51 -070016my $V = '0.25';
Joe Perchescb7301c2009-04-07 20:40:12 -070017
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;
Joe Perchescb7301c2009-04-07 20:40:12 -070026my $email_git_penguin_chiefs = 0;
Joe Perches60db31a2009-12-14 18:00:50 -080027my $email_git = 1;
Florian Mickler0fa05592010-05-24 14:33:20 -070028my $email_git_all_signature_types = 0;
Joe Perches60db31a2009-12-14 18:00:50 -080029my $email_git_blame = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070030my $email_git_min_signatures = 1;
31my $email_git_max_maintainers = 5;
Joe Perchesafa81ee2009-07-29 15:04:28 -070032my $email_git_min_percent = 5;
Joe Perchescb7301c2009-04-07 20:40:12 -070033my $email_git_since = "1-year-ago";
Joe Perches60db31a2009-12-14 18:00:50 -080034my $email_hg_since = "-365";
Joe Perches11ecf532009-09-21 17:04:22 -070035my $email_remove_duplicates = 1;
Joe Perchescb7301c2009-04-07 20:40:12 -070036my $output_multiline = 1;
37my $output_separator = ", ";
Joe Perches3c7385b2009-12-14 18:00:46 -080038my $output_roles = 0;
39my $output_rolestats = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070040my $scm = 0;
41my $web = 0;
42my $subsystem = 0;
43my $status = 0;
Joe Perchesdcf36a92009-10-26 16:49:47 -070044my $keywords = 1;
Joe Perches4b76c9d2010-03-05 13:43:03 -080045my $sections = 0;
Joe Perches03372db2010-03-05 13:43:00 -080046my $file_emails = 0;
Joe Perches4a7fdb52009-04-10 12:28:57 -070047my $from_filename = 0;
Joe Perches3fb55652009-09-21 17:04:17 -070048my $pattern_depth = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070049my $version = 0;
50my $help = 0;
51
52my $exit = 0;
53
54my @penguin_chief = ();
Joe Perchese4d26b02010-05-24 14:33:17 -070055push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
Joe Perchescb7301c2009-04-07 20:40:12 -070056#Andrew wants in on most everything - 2009/01/14
Joe Perchese4d26b02010-05-24 14:33:17 -070057#push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
Joe Perchescb7301c2009-04-07 20:40:12 -070058
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}
Joe Perchese4d26b02010-05-24 14:33:17 -070067my $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) . "\)";
Joe Perchescb7301c2009-04-07 20:40:12 -070077
Joe Perches5f2441e2009-06-16 15:34:02 -070078# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070079my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070080my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070081
Joe Perches60db31a2009-12-14 18:00:50 -080082# 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")',
Richard Kennedy99cf6112010-02-02 13:44:07 -080089 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
90 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
Joe Perches63ab52d2010-10-26 14:22:51 -070091 "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
Joe Perches60db31a2009-12-14 18:00:50 -080092 "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",
Joe Perches63ab52d2010-10-26 14:22:51 -0700105 "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
Joe Perches60db31a2009-12-14 18:00:50 -0800106 "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
Joe Perches368669d2010-05-24 14:33:19 -0700112if (-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
Joe Perchescb7301c2009-04-07 20:40:12 -0700136if (!GetOptions(
137 'email!' => \$email,
138 'git!' => \$email_git,
Joe Perchese4d26b02010-05-24 14:33:17 -0700139 'git-all-signature-types!' => \$email_git_all_signature_types,
Joe Perches60db31a2009-12-14 18:00:50 -0800140 'git-blame!' => \$email_git_blame,
Joe Perchescb7301c2009-04-07 20:40:12 -0700141 '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,
Joe Perchesafa81ee2009-07-29 15:04:28 -0700144 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -0700145 'git-since=s' => \$email_git_since,
Joe Perches60db31a2009-12-14 18:00:50 -0800146 'hg-since=s' => \$email_hg_since,
Joe Perches11ecf532009-09-21 17:04:22 -0700147 'remove-duplicates!' => \$email_remove_duplicates,
Joe Perchescb7301c2009-04-07 20:40:12 -0700148 'm!' => \$email_maintainer,
149 'n!' => \$email_usename,
150 'l!' => \$email_list,
151 's!' => \$email_subscriber_list,
152 'multiline!' => \$output_multiline,
Joe Perches3c7385b2009-12-14 18:00:46 -0800153 'roles!' => \$output_roles,
154 'rolestats!' => \$output_rolestats,
Joe Perchescb7301c2009-04-07 20:40:12 -0700155 'separator=s' => \$output_separator,
156 'subsystem!' => \$subsystem,
157 'status!' => \$status,
158 'scm!' => \$scm,
159 'web!' => \$web,
Joe Perches3fb55652009-09-21 17:04:17 -0700160 'pattern-depth=i' => \$pattern_depth,
Joe Perchesdcf36a92009-10-26 16:49:47 -0700161 'k|keywords!' => \$keywords,
Joe Perches4b76c9d2010-03-05 13:43:03 -0800162 'sections!' => \$sections,
Joe Perches03372db2010-03-05 13:43:00 -0800163 'fe|file-emails!' => \$file_emails,
Joe Perches4a7fdb52009-04-10 12:28:57 -0700164 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -0700165 'v|version' => \$version,
Joe Perches64f77f32010-03-05 13:43:04 -0800166 'h|help|usage' => \$help,
Joe Perchescb7301c2009-04-07 20:40:12 -0700167 )) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800168 die "$P: invalid argument - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700169}
170
171if ($help != 0) {
172 usage();
173 exit 0;
174}
175
176if ($version != 0) {
177 print("${P} ${V}\n");
178 exit 0;
179}
180
Joe Perches64f77f32010-03-05 13:43:04 -0800181if (-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";
Joe Perchescb7301c2009-04-07 20:40:12 -0700184}
185
Joe Perches42498312009-09-21 17:04:21 -0700186if ($output_separator ne ", ") {
187 $output_multiline = 0;
188}
189
Joe Perches3c7385b2009-12-14 18:00:46 -0800190if ($output_rolestats) {
191 $output_roles = 1;
192}
193
Joe Perches4b76c9d2010-03-05 13:43:03 -0800194if ($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) {
Joe Perches4b76c9d2010-03-05 13:43:03 -0800205 die "$P: Missing required option: email, scm, status, subsystem or web\n";
206 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700207}
208
Joe Perchesf5492662009-09-21 17:04:13 -0700209if ($email &&
210 ($email_maintainer + $email_list + $email_subscriber_list +
211 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700212 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
Joe Perchese4d26b02010-05-24 14:33:17 -0700220if ($email_git_all_signature_types) {
221 $signaturePattern = "(.+?)[Bb][Yy]:";
222}
223
Joe Perchescb7301c2009-04-07 20:40:12 -0700224## Read MAINTAINERS for type/value pairs
225
226my @typevalue = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700227my %keyword_hash;
228
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800229open (my $maint, '<', "${lk_path}MAINTAINERS")
230 or die "$P: Can't open MAINTAINERS: $!\n";
231while (<$maint>) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700232 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 .
Joe Perches870020f2009-07-29 15:04:28 -0700243 ##if pattern is a directory and it lacks a trailing slash, add one
244 if ((-d $value)) {
245 $value =~ s@([^/])$@$1/@;
246 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700247 } elsif ($type eq "K") {
248 $keyword_hash{@typevalue} = $value;
Joe Perchescb7301c2009-04-07 20:40:12 -0700249 }
250 push(@typevalue, "$type:$value");
251 } elsif (!/^(\s)*$/) {
252 $line =~ s/\n$//g;
253 push(@typevalue, $line);
254 }
255}
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800256close($maint);
Joe Perchescb7301c2009-04-07 20:40:12 -0700257
Joe Perches8cbb3a72009-09-21 17:04:21 -0700258my %mailmap;
259
Joe Perches11ecf532009-09-21 17:04:22 -0700260if ($email_remove_duplicates) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800261 open(my $mailmap, '<', "${lk_path}.mailmap")
262 or warn "$P: Can't open .mailmap: $!\n";
263 while (<$mailmap>) {
Joe Perches11ecf532009-09-21 17:04:22 -0700264 my $line = $_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700265
Joe Perches11ecf532009-09-21 17:04:22 -0700266 next if ($line =~ m/^\s*#/);
267 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700268
Joe Perches11ecf532009-09-21 17:04:22 -0700269 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800270 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700271
Joe Perches11ecf532009-09-21 17:04:22 -0700272 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700273
Joe Perches11ecf532009-09-21 17:04:22 -0700274 if (exists($mailmap{$name})) {
275 my $obj = $mailmap{$name};
276 push(@$obj, $address);
277 } else {
278 my @arr = ($address);
279 $mailmap{$name} = \@arr;
280 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700281 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800282 close($mailmap);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700283}
284
Joe Perches4a7fdb52009-04-10 12:28:57 -0700285## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700286
287my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700288my @range = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700289my @keyword_tvi = ();
Joe Perches03372db2010-03-05 13:43:00 -0800290my @file_emails = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700291
Joe Perches64f77f32010-03-05 13:43:04 -0800292if (!@ARGV) {
293 push(@ARGV, "&STDIN");
294}
295
Joe Perches4a7fdb52009-04-10 12:28:57 -0700296foreach my $file (@ARGV) {
Joe Perches64f77f32010-03-05 13:43:04 -0800297 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 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700304 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700305 if ($from_filename) {
306 push(@files, $file);
Joe Perchesfab9ed12010-10-26 14:22:52 -0700307 if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800308 open(my $f, '<', $file)
309 or die "$P: Can't open $file: $!\n";
310 my $text = do { local($/) ; <$f> };
311 close($f);
Joe Perches03372db2010-03-05 13:43:00 -0800312 if ($keywords) {
313 foreach my $line (keys %keyword_hash) {
314 if ($text =~ m/$keyword_hash{$line}/x) {
315 push(@keyword_tvi, $line);
316 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700317 }
318 }
Joe Perches03372db2010-03-05 13:43:00 -0800319 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 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700323 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700324 } else {
325 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700326 my $lastfile;
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800327
Wolfram Sang3a4df132010-03-23 13:35:18 -0700328 open(my $patch, "< $file")
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800329 or die "$P: Can't open $file: $!\n";
330 while (<$patch>) {
Joe Perchesdcf36a92009-10-26 16:49:47 -0700331 my $patch_line = $_;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700332 if (m/^\+\+\+\s+(\S+)/) {
333 my $filename = $1;
334 $filename =~ s@^[^/]*/@@;
335 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700336 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700337 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700338 } elsif (m/^\@\@ -(\d+),(\d+)/) {
339 if ($email_git_blame) {
340 push(@range, "$lastfile:$1:$2");
341 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700342 } 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 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700348 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700349 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800350 close($patch);
351
Joe Perches4a7fdb52009-04-10 12:28:57 -0700352 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700353 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700354 . "Add -f to options?\n";
355 }
356 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700357 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700358}
359
Joe Perches03372db2010-03-05 13:43:00 -0800360@file_emails = uniq(@file_emails);
361
Joe Perchescb7301c2009-04-07 20:40:12 -0700362my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700363my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700364my @scm = ();
365my @web = ();
366my @subsystem = ();
367my @status = ();
368
369# Find responsible parties
370
371foreach my $file (@files) {
372
Joe Perches272a8972010-01-08 14:42:48 -0800373 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;
Joe Perchescb7301c2009-04-07 20:40:12 -0700380
Joe Perches272a8972010-01-08 14:42:48 -0800381 #Do not match excluded file patterns
Joe Perchescb7301c2009-04-07 20:40:12 -0700382
Joe Perches272a8972010-01-08 14:42:48 -0800383 for ($i = $start; $i < $end; $i++) {
384 my $line = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700385 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700386 my $type = $1;
387 my $value = $2;
Joe Perches272a8972010-01-08 14:42:48 -0800388 if ($type eq 'X') {
Joe Perchescb7301c2009-04-07 20:40:12 -0700389 if (file_match_pattern($file, $value)) {
Joe Perches272a8972010-01-08 14:42:48 -0800390 $exclude = 1;
Joe Perches3c840c12010-03-05 13:43:07 -0800391 last;
Joe Perches272a8972010-01-08 14:42:48 -0800392 }
393 }
394 }
395 }
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 }
Joe Perches3fb55652009-09-21 17:04:17 -0700412 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700413 }
414 }
415 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700416 }
Joe Perches272a8972010-01-08 14:42:48 -0800417
Joe Perches3c840c12010-03-05 13:43:07 -0800418 $tvi = $end + 1;
Joe Perches272a8972010-01-08 14:42:48 -0800419 }
420
421 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
422 add_categories($line);
Joe Perches6ffd9482010-10-26 14:22:51 -0700423 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 *
Joe Perches4b76c9d2010-03-05 13:43:03 -0800434 }
Joe Perches6ffd9482010-10-26 14:22:51 -0700435 $line =~ s/^([A-Z]):/$1:\t/g;
436 print("$line\n");
Joe Perches4b76c9d2010-03-05 13:43:03 -0800437 }
Joe Perches6ffd9482010-10-26 14:22:51 -0700438 print("\n");
439 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700440 }
441
Joe Perches4a7fdb52009-04-10 12:28:57 -0700442 if ($email && $email_git) {
Joe Perches60db31a2009-12-14 18:00:50 -0800443 vcs_file_signoffs($file);
Joe Perchescb7301c2009-04-07 20:40:12 -0700444 }
445
Joe Perchesf5492662009-09-21 17:04:13 -0700446 if ($email && $email_git_blame) {
Joe Perches60db31a2009-12-14 18:00:50 -0800447 vcs_file_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -0700448 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700449}
450
Joe Perchesdcf36a92009-10-26 16:49:47 -0700451if ($keywords) {
452 @keyword_tvi = sort_and_uniq(@keyword_tvi);
453 foreach my $line (@keyword_tvi) {
454 add_categories($line);
455 }
456}
457
Joe Perchesf5f50782009-06-16 15:34:00 -0700458if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700459 foreach my $chief (@penguin_chief) {
460 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700461 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700462
Joe Perchesa8af2432009-12-14 18:00:49 -0800463 $email_address = format_email($1, $2, $email_usename);
Joe Perchesf5f50782009-06-16 15:34:00 -0700464 if ($email_git_penguin_chiefs) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800465 push(@email_to, [$email_address, 'chief penguin']);
Joe Perchesf5f50782009-06-16 15:34:00 -0700466 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800467 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700468 }
469 }
470 }
Joe Perches03372db2010-03-05 13:43:00 -0800471
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 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700479}
480
Joe Perches290603c2009-06-16 15:33:58 -0700481if ($email || $email_list) {
482 my @to = ();
483 if ($email) {
484 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700485 }
Joe Perches290603c2009-06-16 15:33:58 -0700486 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700487 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700488 }
Joe Perches3c7385b2009-12-14 18:00:46 -0800489 output(merge_email(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700490}
491
492if ($scm) {
Joe Perchesb7816552009-09-21 17:04:24 -0700493 @scm = uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700494 output(@scm);
495}
496
497if ($status) {
Joe Perchesb7816552009-09-21 17:04:24 -0700498 @status = uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700499 output(@status);
500}
501
502if ($subsystem) {
Joe Perchesb7816552009-09-21 17:04:24 -0700503 @subsystem = uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700504 output(@subsystem);
505}
506
507if ($web) {
Joe Perchesb7816552009-09-21 17:04:24 -0700508 @web = uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700509 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
Joe Perches870020f2009-07-29 15:04:28 -0700535 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700536version: $V
537
538MAINTAINER field selection options:
539 --email => print email address(es) if any
540 --git => include recent git \*-by: signers
Joe Perchese4d26b02010-05-24 14:33:17 -0700541 --git-all-signature-types => include signers regardless of signature type
542 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
Joe Perchescb7301c2009-04-07 20:40:12 -0700543 --git-chief-penguins => include ${penguin_chiefs}
Joe Perchese4d26b02010-05-24 14:33:17 -0700544 --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)
Joe Perchesf5492662009-09-21 17:04:13 -0700547 --git-blame => use git blame to find modified commits for patch or file
Joe Perchese4d26b02010-05-24 14:33:17 -0700548 --git-since => git history to use (default: $email_git_since)
549 --hg-since => hg history to use (default: $email_hg_since)
Joe Perchescb7301c2009-04-07 20:40:12 -0700550 --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
Joe Perches11ecf532009-09-21 17:04:22 -0700554 --remove-duplicates => minimize duplicate email names/addresses
Joe Perches3c7385b2009-12-14 18:00:46 -0800555 --roles => show roles (status:subsystem, git-signer, list, etc...)
556 --rolestats => show roles and statistics (commits/total_commits, %)
Joe Perches03372db2010-03-05 13:43:00 -0800557 --file-emails => add email addresses found in -f file (default: 0 (off))
Joe Perchescb7301c2009-04-07 20:40:12 -0700558 --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
Joe Perches42498312009-09-21 17:04:21 -0700565 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700566 --multiline => print 1 entry per line
567
Joe Perchescb7301c2009-04-07 20:40:12 -0700568Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700569 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesdcf36a92009-10-26 16:49:47 -0700570 --keywords => scan patch for keywords (default: 1 (on))
Joe Perches4b76c9d2010-03-05 13:43:03 -0800571 --sections => print the entire subsystem sections with pattern matches
Joe Perchesf5f50782009-06-16 15:34:00 -0700572 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700573 --help => show this help information
574
Joe Perches3fb55652009-09-21 17:04:17 -0700575Default options:
Joe Perches11ecf532009-09-21 17:04:22 -0700576 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
Joe Perches3fb55652009-09-21 17:04:17 -0700577
Joe Perches870020f2009-07-29 15:04:28 -0700578Notes:
579 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700580 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,
Joe Perches60db31a2009-12-14 18:00:50 -0800584 no individual file within the directory or subdirectory
585 is matched.
Joe Perchesf5492662009-09-21 17:04:13 -0700586 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.
Joe Perches3c7385b2009-12-14 18:00:46 -0800589 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.
Joe Perches60db31a2009-12-14 18:00:50 -0800596 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
Joe Perches368669d2010-05-24 14:33:19 -0700602 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.
Joe Perchescb7301c2009-04-07 20:40:12 -0700607EOT
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
Joe Perches0e70e832009-09-21 17:04:20 -0700637sub parse_email {
638 my ($formatted_email) = @_;
639
640 my $name = "";
641 my $address = "";
642
Joe Perches11ecf532009-09-21 17:04:22 -0700643 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700644 $name = $1;
645 $address = $2;
Joe Perches11ecf532009-09-21 17:04:22 -0700646 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700647 $address = $1;
Joe Perchesb7816552009-09-21 17:04:24 -0700648 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700649 $address = $1;
650 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700651
652 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700653 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700654 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700655
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800656 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perchescb7301c2009-04-07 20:40:12 -0700657 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700658 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700659 }
Joe Perches0e70e832009-09-21 17:04:20 -0700660
661 return ($name, $address);
662}
663
664sub format_email {
Joe Perchesa8af2432009-12-14 18:00:49 -0800665 my ($name, $address, $usename) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700666
667 my $formatted_email;
668
669 $name =~ s/^\s+|\s+$//g;
670 $name =~ s/^\"|\"$//g;
671 $address =~ s/^\s+|\s+$//g;
672
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800673 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perches0e70e832009-09-21 17:04:20 -0700674 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
675 $name = "\"$name\"";
676 }
677
Joe Perchesa8af2432009-12-14 18:00:49 -0800678 if ($usename) {
Joe Perches0e70e832009-09-21 17:04:20 -0700679 if ("$name" eq "") {
680 $formatted_email = "$address";
681 } else {
Joe Perchesa8af2432009-12-14 18:00:49 -0800682 $formatted_email = "$name <$address>";
Joe Perches0e70e832009-09-21 17:04:20 -0700683 }
684 } else {
685 $formatted_email = $address;
686 }
687
Joe Perchescb7301c2009-04-07 20:40:12 -0700688 return $formatted_email;
689}
690
Joe Perches272a8972010-01-08 14:42:48 -0800691sub 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
Joe Perchesb7816552009-09-21 17:04:24 -0700705sub find_starting_index {
Joe Perchesb7816552009-09-21 17:04:24 -0700706 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 {
720 my ($index) = @_;
721
722 while ($index < @typevalue) {
723 my $tv = $typevalue[$index];
724 if (!($tv =~ m/^(\C):\s*(.*)/)) {
725 last;
726 }
727 $index++;
728 }
729
730 return $index;
731}
732
Joe Perches3c7385b2009-12-14 18:00:46 -0800733sub 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
Joe Perchescb7301c2009-04-07 20:40:12 -0700798sub add_categories {
799 my ($index) = @_;
800
Joe Perchesb7816552009-09-21 17:04:24 -0700801 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];
Joe Perches290603c2009-06-16 15:33:58 -0700809 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700810 my $ptype = $1;
811 my $pvalue = $2;
812 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700813 my $list_address = $pvalue;
814 my $list_additional = "";
Joe Perches3c7385b2009-12-14 18:00:46 -0800815 my $list_role = get_list_role($i);
816
817 if ($list_role ne "") {
818 $list_role = ":" . $list_role;
819 }
Joe Perches290603c2009-06-16 15:33:58 -0700820 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
821 $list_address = $1;
822 $list_additional = $2;
823 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700824 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700825 if ($email_subscriber_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800826 push(@list_to, [$list_address, "subscriber list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700827 }
828 } else {
829 if ($email_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800830 push(@list_to, [$list_address, "open list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700831 }
832 }
833 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700834 my ($name, $address) = parse_email($pvalue);
835 if ($name eq "") {
Joe Perchesb7816552009-09-21 17:04:24 -0700836 if ($i > 0) {
837 my $tv = $typevalue[$i - 1];
Joe Perches0e70e832009-09-21 17:04:20 -0700838 if ($tv =~ m/^(\C):\s*(.*)/) {
839 if ($1 eq "P") {
840 $name = $2;
Joe Perchesa8af2432009-12-14 18:00:49 -0800841 $pvalue = format_email($name, $address, $email_usename);
Joe Perches5f2441e2009-06-16 15:34:02 -0700842 }
843 }
844 }
845 }
Joe Perches0e70e832009-09-21 17:04:20 -0700846 if ($email_maintainer) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800847 my $role = get_maintainer_role($i);
848 push_email_addresses($pvalue, $role);
Joe Perchescb7301c2009-04-07 20:40:12 -0700849 }
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 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700857 }
858 }
859}
860
Joe Perches11ecf532009-09-21 17:04:22 -0700861my %email_hash_name;
862my %email_hash_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700863
Joe Perches11ecf532009-09-21 17:04:22 -0700864sub email_inuse {
865 my ($name, $address) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700866
Joe Perches11ecf532009-09-21 17:04:22 -0700867 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}));
870
Joe Perches0e70e832009-09-21 17:04:20 -0700871 return 0;
872}
873
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700874sub push_email_address {
Joe Perches3c7385b2009-12-14 18:00:46 -0800875 my ($line, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700876
Joe Perches0e70e832009-09-21 17:04:20 -0700877 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700878
Joe Perchesb7816552009-09-21 17:04:24 -0700879 if ($address eq "") {
880 return 0;
881 }
882
Joe Perches11ecf532009-09-21 17:04:22 -0700883 if (!$email_remove_duplicates) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800884 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700885 } elsif (!email_inuse($name, $address)) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800886 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700887 $email_hash_name{$name}++;
888 $email_hash_address{$address}++;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700889 }
Joe Perchesb7816552009-09-21 17:04:24 -0700890
891 return 1;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700892}
893
894sub push_email_addresses {
Joe Perches3c7385b2009-12-14 18:00:46 -0800895 my ($address, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700896
897 my @address_list = ();
898
Joe Perches5f2441e2009-06-16 15:34:02 -0700899 if (rfc822_valid($address)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800900 push_email_address($address, $role);
Joe Perches5f2441e2009-06-16 15:34:02 -0700901 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700902 my $array_count = shift(@address_list);
903 while (my $entry = shift(@address_list)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800904 push_email_address($entry, $role);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700905 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700906 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800907 if (!push_email_address($address, $role)) {
Joe Perchesb7816552009-09-21 17:04:24 -0700908 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
909 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700910 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700911}
912
Joe Perches3c7385b2009-12-14 18:00:46 -0800913sub add_role {
914 my ($line, $role) = @_;
915
916 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800917 my $email = format_email($name, $address, $email_usename);
Joe Perches3c7385b2009-12-14 18:00:46 -0800918
919 foreach my $entry (@email_to) {
920 if ($email_remove_duplicates) {
921 my ($entry_name, $entry_address) = parse_email($entry->[0]);
Joe Perches03372db2010-03-05 13:43:00 -0800922 if (($name eq $entry_name || $address eq $entry_address)
923 && ($role eq "" || !($entry->[1] =~ m/$role/))
924 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800925 if ($entry->[1] eq "") {
926 $entry->[1] = "$role";
927 } else {
928 $entry->[1] = "$entry->[1],$role";
929 }
930 }
931 } else {
Joe Perches03372db2010-03-05 13:43:00 -0800932 if ($email eq $entry->[0]
933 && ($role eq "" || !($entry->[1] =~ m/$role/))
934 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800935 if ($entry->[1] eq "") {
936 $entry->[1] = "$role";
937 } else {
938 $entry->[1] = "$entry->[1],$role";
939 }
940 }
941 }
942 }
943}
944
Joe Perchescb7301c2009-04-07 20:40:12 -0700945sub which {
946 my ($bin) = @_;
947
Joe Perchesf5f50782009-06-16 15:34:00 -0700948 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700949 if (-e "$path/$bin") {
950 return "$path/$bin";
951 }
952 }
953
954 return "";
955}
956
Joe Perches8cbb3a72009-09-21 17:04:21 -0700957sub mailmap {
Joe Perchesa8af2432009-12-14 18:00:49 -0800958 my (@lines) = @_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700959 my %hash;
960
961 foreach my $line (@lines) {
962 my ($name, $address) = parse_email($line);
963 if (!exists($hash{$name})) {
964 $hash{$name} = $address;
Joe Perches11ecf532009-09-21 17:04:22 -0700965 } elsif ($address ne $hash{$name}) {
966 $address = $hash{$name};
Joe Perchesa8af2432009-12-14 18:00:49 -0800967 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700968 }
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})) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800974 $line = format_email($name, $hash{$name}, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700975 }
976 }
977 }
978 }
979
980 return @lines;
981}
982
Joe Perches60db31a2009-12-14 18:00:50 -0800983sub git_execute_cmd {
984 my ($cmd) = @_;
985 my @lines = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700986
Joe Perches60db31a2009-12-14 18:00:50 -0800987 my $output = `$cmd`;
988 $output =~ s/^\s*//gm;
989 @lines = split("\n", $output);
990
991 return @lines;
Joe Perchesa8af2432009-12-14 18:00:49 -0800992}
993
Joe Perches60db31a2009-12-14 18:00:50 -0800994sub hg_execute_cmd {
Joe Perchesa8af2432009-12-14 18:00:49 -0800995 my ($cmd) = @_;
Joe Perches60db31a2009-12-14 18:00:50 -0800996 my @lines = ();
Joe Perchesa8af2432009-12-14 18:00:49 -0800997
Joe Perches60db31a2009-12-14 18:00:50 -0800998 my $output = `$cmd`;
999 @lines = split("\n", $output);
1000
1001 return @lines;
1002}
1003
1004sub vcs_find_signers {
1005 my ($cmd) = @_;
Joe Perchesa8af2432009-12-14 18:00:49 -08001006 my @lines = ();
1007 my $commits;
1008
Joe Perches60db31a2009-12-14 18:00:50 -08001009 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
Joe Perchescb7301c2009-04-07 20:40:12 -07001010
Joe Perches60db31a2009-12-14 18:00:50 -08001011 my $pattern = $VCS_cmds{"commit_pattern"};
Joe Perchescb7301c2009-04-07 20:40:12 -07001012
Joe Perches60db31a2009-12-14 18:00:50 -08001013 $commits = grep(/$pattern/, @lines); # of commits
Joe Perchesafa81ee2009-07-29 15:04:28 -07001014
Joe Perchese4d26b02010-05-24 14:33:17 -07001015 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
Joe Perches0e70e832009-09-21 17:04:20 -07001016 if (!$email_git_penguin_chiefs) {
1017 @lines = grep(!/${penguin_chiefs}/i, @lines);
1018 }
Joe Perches63ab52d2010-10-26 14:22:51 -07001019
1020 return (0, @lines) if !@lines;
1021
Joe Perches0e70e832009-09-21 17:04:20 -07001022 # cut -f2- -d":"
1023 s/.*:\s*(.+)\s*/$1/ for (@lines);
1024
Joe Perchesa8af2432009-12-14 18:00:49 -08001025## Reformat email addresses (with names) to avoid badly written signatures
1026
Joe Perches3c7385b2009-12-14 18:00:46 -08001027 foreach my $line (@lines) {
1028 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -08001029 $line = format_email($name, $address, 1);
1030 }
1031
1032 return ($commits, @lines);
1033}
1034
Joe Perches63ab52d2010-10-26 14:22:51 -07001035sub 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
Joe Perches60db31a2009-12-14 18:00:50 -08001057sub 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
Joe Perches63ab52d2010-10-26 14:22:51 -07001114 foreach my $commit (@commits) {
1115 $commit =~ s/^\^//g;
1116 }
1117
Joe Perches60db31a2009-12-14 18:00:50 -08001118 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 {
Joe Perchesa8af2432009-12-14 18:00:49 -08001139 my ($role, $divisor, @lines) = @_;
1140
1141 my %hash;
1142 my $count = 0;
1143
Joe Perchesa8af2432009-12-14 18:00:49 -08001144 return if (@lines <= 0);
1145
1146 if ($divisor <= 0) {
Joe Perches60db31a2009-12-14 18:00:50 -08001147 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
Joe Perchesa8af2432009-12-14 18:00:49 -08001148 $divisor = 1;
Joe Perches3c7385b2009-12-14 18:00:46 -08001149 }
Joe Perches8cbb3a72009-09-21 17:04:21 -07001150
Joe Perches11ecf532009-09-21 17:04:22 -07001151 if ($email_remove_duplicates) {
1152 @lines = mailmap(@lines);
1153 }
Joe Perches0e70e832009-09-21 17:04:20 -07001154
Joe Perches63ab52d2010-10-26 14:22:51 -07001155 return if (@lines <= 0);
1156
Joe Perches0e70e832009-09-21 17:04:20 -07001157 @lines = sort(@lines);
Joe Perchesafa81ee2009-07-29 15:04:28 -07001158
Joe Perches11ecf532009-09-21 17:04:22 -07001159 # uniq -c
1160 $hash{$_}++ for @lines;
1161
1162 # sort -rn
1163 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1164 my $sign_offs = $hash{$line};
Joe Perchesa8af2432009-12-14 18:00:49 -08001165 my $percent = $sign_offs * 100 / $divisor;
Joe Perches3c7385b2009-12-14 18:00:46 -08001166
Joe Perchesa8af2432009-12-14 18:00:49 -08001167 $percent = 100 if ($percent > 100);
Joe Perches11ecf532009-09-21 17:04:22 -07001168 $count++;
1169 last if ($sign_offs < $email_git_min_signatures ||
1170 $count > $email_git_max_maintainers ||
Joe Perchesa8af2432009-12-14 18:00:49 -08001171 $percent < $email_git_min_percent);
Joe Perches3c7385b2009-12-14 18:00:46 -08001172 push_email_address($line, '');
Joe Perches3c7385b2009-12-14 18:00:46 -08001173 if ($output_rolestats) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001174 my $fmt_percent = sprintf("%.0f", $percent);
1175 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1176 } else {
1177 add_role($line, $role);
Joe Perches3c7385b2009-12-14 18:00:46 -08001178 }
Joe Perchesf5492662009-09-21 17:04:13 -07001179 }
1180}
1181
Joe Perches60db31a2009-12-14 18:00:50 -08001182sub vcs_file_signoffs {
Joe Perchesa8af2432009-12-14 18:00:49 -08001183 my ($file) = @_;
1184
1185 my @signers = ();
Joe Perches60db31a2009-12-14 18:00:50 -08001186 my $commits;
Joe Perchesa8af2432009-12-14 18:00:49 -08001187
Joe Perches60db31a2009-12-14 18:00:50 -08001188 return if (!vcs_exists());
Joe Perchesa8af2432009-12-14 18:00:49 -08001189
Joe Perches60db31a2009-12-14 18:00:50 -08001190 my $cmd = $VCS_cmds{"find_signers_cmd"};
1191 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1192
1193 ($commits, @signers) = vcs_find_signers($cmd);
1194 vcs_assign("commit_signer", $commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001195}
1196
Joe Perches60db31a2009-12-14 18:00:50 -08001197sub vcs_file_blame {
Joe Perchesf5492662009-09-21 17:04:13 -07001198 my ($file) = @_;
1199
Joe Perches60db31a2009-12-14 18:00:50 -08001200 my @signers = ();
Joe Perches63ab52d2010-10-26 14:22:51 -07001201 my @all_commits = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001202 my @commits = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001203 my $total_commits;
Joe Perches63ab52d2010-10-26 14:22:51 -07001204 my $total_lines;
Joe Perchesf5492662009-09-21 17:04:13 -07001205
Joe Perches60db31a2009-12-14 18:00:50 -08001206 return if (!vcs_exists());
Joe Perchesf5492662009-09-21 17:04:13 -07001207
Joe Perches63ab52d2010-10-26 14:22:51 -07001208 @all_commits = vcs_blame($file);
1209 @commits = uniq(@all_commits);
Joe Perchesa8af2432009-12-14 18:00:49 -08001210 $total_commits = @commits;
Joe Perches63ab52d2010-10-26 14:22:51 -07001211 $total_lines = @all_commits;
Joe Perchesa8af2432009-12-14 18:00:49 -08001212
Joe Perchesf5492662009-09-21 17:04:13 -07001213 foreach my $commit (@commits) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001214 my $commit_count;
1215 my @commit_signers = ();
Joe Perchesf5492662009-09-21 17:04:13 -07001216
Joe Perches60db31a2009-12-14 18:00:50 -08001217 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);
Joe Perches63ab52d2010-10-26 14:22:51 -07001221
Joe Perches60db31a2009-12-14 18:00:50 -08001222 push(@signers, @commit_signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001223 }
1224
Joe Perchesa8af2432009-12-14 18:00:49 -08001225 if ($from_filename) {
Joe Perches63ab52d2010-10-26 14:22:51 -07001226 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 }
Joe Perches60db31a2009-12-14 18:00:50 -08001243 vcs_assign("commits", $total_commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001244 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001245 vcs_assign("modified commits", $total_commits, @signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001246 }
Joe Perchescb7301c2009-04-07 20:40:12 -07001247}
1248
1249sub uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001250 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001251
1252 my %saw;
1253 @parms = grep(!$saw{$_}++, @parms);
1254 return @parms;
1255}
1256
1257sub sort_and_uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001258 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001259
1260 my %saw;
1261 @parms = sort @parms;
1262 @parms = grep(!$saw{$_}++, @parms);
1263 return @parms;
1264}
1265
Joe Perches03372db2010-03-05 13:43:00 -08001266sub 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
Joe Perches3c7385b2009-12-14 18:00:46 -08001311sub merge_email {
1312 my @lines;
1313 my %saw;
1314
1315 for (@_) {
1316 my ($address, $role) = @$_;
1317 if (!$saw{$address}) {
1318 if ($output_roles) {
Joe Perches60db31a2009-12-14 18:00:50 -08001319 push(@lines, "$address ($role)");
Joe Perches3c7385b2009-12-14 18:00:46 -08001320 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001321 push(@lines, $address);
Joe Perches3c7385b2009-12-14 18:00:46 -08001322 }
1323 $saw{$address} = 1;
1324 }
1325 }
1326
1327 return @lines;
1328}
1329
Joe Perchescb7301c2009-04-07 20:40:12 -07001330sub output {
Joe Perchesa8af2432009-12-14 18:00:49 -08001331 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001332
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}
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001342
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#
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001395sub rfc822_valid {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001396 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
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001415sub rfc822_validlist {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001416 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*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -07001427 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches60db31a2009-12-14 18:00:50 -08001428 push(@r, $1);
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001429 }
1430 return wantarray ? (scalar(@r), @r) : 1;
1431 }
Joe Perches60db31a2009-12-14 18:00:50 -08001432 return wantarray ? () : 0;
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001433}