blob: e5a400c53bf01bd50b77d20f8d6e46aa864be2d0 [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 Perchese3e9d112010-10-26 14:22:53 -070027my $email_git = 0;
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 Perchese3e9d112010-10-26 14:22:53 -070030my $email_git_fallback = 1;
Joe Perchescb7301c2009-04-07 20:40:12 -070031my $email_git_min_signatures = 1;
32my $email_git_max_maintainers = 5;
Joe Perchesafa81ee2009-07-29 15:04:28 -070033my $email_git_min_percent = 5;
Joe Perchescb7301c2009-04-07 20:40:12 -070034my $email_git_since = "1-year-ago";
Joe Perches60db31a2009-12-14 18:00:50 -080035my $email_hg_since = "-365";
Joe Perches11ecf532009-09-21 17:04:22 -070036my $email_remove_duplicates = 1;
Joe Perchescb7301c2009-04-07 20:40:12 -070037my $output_multiline = 1;
38my $output_separator = ", ";
Joe Perches3c7385b2009-12-14 18:00:46 -080039my $output_roles = 0;
40my $output_rolestats = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070041my $scm = 0;
42my $web = 0;
43my $subsystem = 0;
44my $status = 0;
Joe Perchesdcf36a92009-10-26 16:49:47 -070045my $keywords = 1;
Joe Perches4b76c9d2010-03-05 13:43:03 -080046my $sections = 0;
Joe Perches03372db2010-03-05 13:43:00 -080047my $file_emails = 0;
Joe Perches4a7fdb52009-04-10 12:28:57 -070048my $from_filename = 0;
Joe Perches3fb55652009-09-21 17:04:17 -070049my $pattern_depth = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070050my $version = 0;
51my $help = 0;
52
53my $exit = 0;
54
55my @penguin_chief = ();
Joe Perchese4d26b02010-05-24 14:33:17 -070056push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
Joe Perchescb7301c2009-04-07 20:40:12 -070057#Andrew wants in on most everything - 2009/01/14
Joe Perchese4d26b02010-05-24 14:33:17 -070058#push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
Joe Perchescb7301c2009-04-07 20:40:12 -070059
60my @penguin_chief_names = ();
61foreach my $chief (@penguin_chief) {
62 if ($chief =~ m/^(.*):(.*)/) {
63 my $chief_name = $1;
64 my $chief_addr = $2;
65 push(@penguin_chief_names, $chief_name);
66 }
67}
Joe Perchese4d26b02010-05-24 14:33:17 -070068my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
69
70# Signature types of people who are either
71# a) responsible for the code in question, or
72# b) familiar enough with it to give relevant feedback
73my @signature_tags = ();
74push(@signature_tags, "Signed-off-by:");
75push(@signature_tags, "Reviewed-by:");
76push(@signature_tags, "Acked-by:");
77my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
Joe Perchescb7301c2009-04-07 20:40:12 -070078
Joe Perches5f2441e2009-06-16 15:34:02 -070079# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070080my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070081my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070082
Joe Perches60db31a2009-12-14 18:00:50 -080083# VCS command support: class-like functions and strings
84
85my %VCS_cmds;
86
87my %VCS_cmds_git = (
88 "execute_cmd" => \&git_execute_cmd,
89 "available" => '(which("git") ne "") && (-d ".git")',
Richard Kennedy99cf6112010-02-02 13:44:07 -080090 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
91 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
Joe Perches63ab52d2010-10-26 14:22:51 -070092 "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
Joe Perches60db31a2009-12-14 18:00:50 -080093 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
94 "blame_file_cmd" => "git blame -l \$file",
95 "commit_pattern" => "^commit [0-9a-f]{40,40}",
96 "blame_commit_pattern" => "^([0-9a-f]+) "
97);
98
99my %VCS_cmds_hg = (
100 "execute_cmd" => \&hg_execute_cmd,
101 "available" => '(which("hg") ne "") && (-d ".hg")',
102 "find_signers_cmd" =>
103 "hg log --date=\$email_hg_since" .
104 " --template='commit {node}\\n{desc}\\n' -- \$file",
105 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
Joe Perches63ab52d2010-10-26 14:22:51 -0700106 "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
Joe Perches60db31a2009-12-14 18:00:50 -0800107 "blame_range_cmd" => "", # not supported
108 "blame_file_cmd" => "hg blame -c \$file",
109 "commit_pattern" => "^commit [0-9a-f]{40,40}",
110 "blame_commit_pattern" => "^([0-9a-f]+):"
111);
112
Joe Perchesbcde44e2010-10-26 14:22:53 -0700113my $conf = which_conf(".get_maintainer.conf");
114if (-f $conf) {
Joe Perches368669d2010-05-24 14:33:19 -0700115 my @conf_args;
Joe Perchesbcde44e2010-10-26 14:22:53 -0700116 open(my $conffile, '<', "$conf")
117 or warn "$P: Can't find a readable .get_maintainer.conf file $!\n";
118
Joe Perches368669d2010-05-24 14:33:19 -0700119 while (<$conffile>) {
120 my $line = $_;
121
122 $line =~ s/\s*\n?$//g;
123 $line =~ s/^\s*//g;
124 $line =~ s/\s+/ /g;
125
126 next if ($line =~ m/^\s*#/);
127 next if ($line =~ m/^\s*$/);
128
129 my @words = split(" ", $line);
130 foreach my $word (@words) {
131 last if ($word =~ m/^#/);
132 push (@conf_args, $word);
133 }
134 }
135 close($conffile);
136 unshift(@ARGV, @conf_args) if @conf_args;
137}
138
Joe Perchescb7301c2009-04-07 20:40:12 -0700139if (!GetOptions(
140 'email!' => \$email,
141 'git!' => \$email_git,
Joe Perchese4d26b02010-05-24 14:33:17 -0700142 'git-all-signature-types!' => \$email_git_all_signature_types,
Joe Perches60db31a2009-12-14 18:00:50 -0800143 'git-blame!' => \$email_git_blame,
Joe Perchese3e9d112010-10-26 14:22:53 -0700144 'git-fallback!' => \$email_git_fallback,
Joe Perchescb7301c2009-04-07 20:40:12 -0700145 'git-chief-penguins!' => \$email_git_penguin_chiefs,
146 'git-min-signatures=i' => \$email_git_min_signatures,
147 'git-max-maintainers=i' => \$email_git_max_maintainers,
Joe Perchesafa81ee2009-07-29 15:04:28 -0700148 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -0700149 'git-since=s' => \$email_git_since,
Joe Perches60db31a2009-12-14 18:00:50 -0800150 'hg-since=s' => \$email_hg_since,
Joe Perches11ecf532009-09-21 17:04:22 -0700151 'remove-duplicates!' => \$email_remove_duplicates,
Joe Perchescb7301c2009-04-07 20:40:12 -0700152 'm!' => \$email_maintainer,
153 'n!' => \$email_usename,
154 'l!' => \$email_list,
155 's!' => \$email_subscriber_list,
156 'multiline!' => \$output_multiline,
Joe Perches3c7385b2009-12-14 18:00:46 -0800157 'roles!' => \$output_roles,
158 'rolestats!' => \$output_rolestats,
Joe Perchescb7301c2009-04-07 20:40:12 -0700159 'separator=s' => \$output_separator,
160 'subsystem!' => \$subsystem,
161 'status!' => \$status,
162 'scm!' => \$scm,
163 'web!' => \$web,
Joe Perches3fb55652009-09-21 17:04:17 -0700164 'pattern-depth=i' => \$pattern_depth,
Joe Perchesdcf36a92009-10-26 16:49:47 -0700165 'k|keywords!' => \$keywords,
Joe Perches4b76c9d2010-03-05 13:43:03 -0800166 'sections!' => \$sections,
Joe Perches03372db2010-03-05 13:43:00 -0800167 'fe|file-emails!' => \$file_emails,
Joe Perches4a7fdb52009-04-10 12:28:57 -0700168 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -0700169 'v|version' => \$version,
Joe Perches64f77f32010-03-05 13:43:04 -0800170 'h|help|usage' => \$help,
Joe Perchescb7301c2009-04-07 20:40:12 -0700171 )) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800172 die "$P: invalid argument - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700173}
174
175if ($help != 0) {
176 usage();
177 exit 0;
178}
179
180if ($version != 0) {
181 print("${P} ${V}\n");
182 exit 0;
183}
184
Joe Perches64f77f32010-03-05 13:43:04 -0800185if (-t STDIN && !@ARGV) {
186 # We're talking to a terminal, but have no command line arguments.
187 die "$P: missing patchfile or -f file - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700188}
189
Joe Perches42498312009-09-21 17:04:21 -0700190if ($output_separator ne ", ") {
191 $output_multiline = 0;
192}
193
Joe Perches3c7385b2009-12-14 18:00:46 -0800194if ($output_rolestats) {
195 $output_roles = 1;
196}
197
Joe Perches4b76c9d2010-03-05 13:43:03 -0800198if ($sections) {
199 $email = 0;
200 $email_list = 0;
201 $scm = 0;
202 $status = 0;
203 $subsystem = 0;
204 $web = 0;
205 $keywords = 0;
206} else {
207 my $selections = $email + $scm + $status + $subsystem + $web;
208 if ($selections == 0) {
Joe Perches4b76c9d2010-03-05 13:43:03 -0800209 die "$P: Missing required option: email, scm, status, subsystem or web\n";
210 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700211}
212
Joe Perchesf5492662009-09-21 17:04:13 -0700213if ($email &&
214 ($email_maintainer + $email_list + $email_subscriber_list +
215 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700216 die "$P: Please select at least 1 email option\n";
217}
218
219if (!top_of_kernel_tree($lk_path)) {
220 die "$P: The current directory does not appear to be "
221 . "a linux kernel source tree.\n";
222}
223
Joe Perchese4d26b02010-05-24 14:33:17 -0700224if ($email_git_all_signature_types) {
225 $signaturePattern = "(.+?)[Bb][Yy]:";
226}
227
Joe Perchescb7301c2009-04-07 20:40:12 -0700228## Read MAINTAINERS for type/value pairs
229
230my @typevalue = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700231my %keyword_hash;
232
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800233open (my $maint, '<', "${lk_path}MAINTAINERS")
234 or die "$P: Can't open MAINTAINERS: $!\n";
235while (<$maint>) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700236 my $line = $_;
237
238 if ($line =~ m/^(\C):\s*(.*)/) {
239 my $type = $1;
240 my $value = $2;
241
242 ##Filename pattern matching
243 if ($type eq "F" || $type eq "X") {
244 $value =~ s@\.@\\\.@g; ##Convert . to \.
245 $value =~ s/\*/\.\*/g; ##Convert * to .*
246 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700247 ##if pattern is a directory and it lacks a trailing slash, add one
248 if ((-d $value)) {
249 $value =~ s@([^/])$@$1/@;
250 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700251 } elsif ($type eq "K") {
252 $keyword_hash{@typevalue} = $value;
Joe Perchescb7301c2009-04-07 20:40:12 -0700253 }
254 push(@typevalue, "$type:$value");
255 } elsif (!/^(\s)*$/) {
256 $line =~ s/\n$//g;
257 push(@typevalue, $line);
258 }
259}
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800260close($maint);
Joe Perchescb7301c2009-04-07 20:40:12 -0700261
Joe Perches8cbb3a72009-09-21 17:04:21 -0700262my %mailmap;
263
Joe Perches11ecf532009-09-21 17:04:22 -0700264if ($email_remove_duplicates) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800265 open(my $mailmap, '<', "${lk_path}.mailmap")
266 or warn "$P: Can't open .mailmap: $!\n";
267 while (<$mailmap>) {
Joe Perches11ecf532009-09-21 17:04:22 -0700268 my $line = $_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700269
Joe Perches11ecf532009-09-21 17:04:22 -0700270 next if ($line =~ m/^\s*#/);
271 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700272
Joe Perches11ecf532009-09-21 17:04:22 -0700273 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800274 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700275
Joe Perches11ecf532009-09-21 17:04:22 -0700276 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700277
Joe Perches11ecf532009-09-21 17:04:22 -0700278 if (exists($mailmap{$name})) {
279 my $obj = $mailmap{$name};
280 push(@$obj, $address);
281 } else {
282 my @arr = ($address);
283 $mailmap{$name} = \@arr;
284 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700285 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800286 close($mailmap);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700287}
288
Joe Perches4a7fdb52009-04-10 12:28:57 -0700289## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700290
291my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700292my @range = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700293my @keyword_tvi = ();
Joe Perches03372db2010-03-05 13:43:00 -0800294my @file_emails = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700295
Joe Perches64f77f32010-03-05 13:43:04 -0800296if (!@ARGV) {
297 push(@ARGV, "&STDIN");
298}
299
Joe Perches4a7fdb52009-04-10 12:28:57 -0700300foreach my $file (@ARGV) {
Joe Perches64f77f32010-03-05 13:43:04 -0800301 if ($file ne "&STDIN") {
302 ##if $file is a directory and it lacks a trailing slash, add one
303 if ((-d $file)) {
304 $file =~ s@([^/])$@$1/@;
305 } elsif (!(-f $file)) {
306 die "$P: file '${file}' not found\n";
307 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700308 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700309 if ($from_filename) {
310 push(@files, $file);
Joe Perchesfab9ed12010-10-26 14:22:52 -0700311 if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800312 open(my $f, '<', $file)
313 or die "$P: Can't open $file: $!\n";
314 my $text = do { local($/) ; <$f> };
315 close($f);
Joe Perches03372db2010-03-05 13:43:00 -0800316 if ($keywords) {
317 foreach my $line (keys %keyword_hash) {
318 if ($text =~ m/$keyword_hash{$line}/x) {
319 push(@keyword_tvi, $line);
320 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700321 }
322 }
Joe Perches03372db2010-03-05 13:43:00 -0800323 if ($file_emails) {
324 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;
325 push(@file_emails, clean_file_emails(@poss_addr));
326 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700327 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700328 } else {
329 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700330 my $lastfile;
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800331
Wolfram Sang3a4df132010-03-23 13:35:18 -0700332 open(my $patch, "< $file")
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800333 or die "$P: Can't open $file: $!\n";
334 while (<$patch>) {
Joe Perchesdcf36a92009-10-26 16:49:47 -0700335 my $patch_line = $_;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700336 if (m/^\+\+\+\s+(\S+)/) {
337 my $filename = $1;
338 $filename =~ s@^[^/]*/@@;
339 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700340 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700341 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700342 } elsif (m/^\@\@ -(\d+),(\d+)/) {
343 if ($email_git_blame) {
344 push(@range, "$lastfile:$1:$2");
345 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700346 } elsif ($keywords) {
347 foreach my $line (keys %keyword_hash) {
348 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
349 push(@keyword_tvi, $line);
350 }
351 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700352 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700353 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800354 close($patch);
355
Joe Perches4a7fdb52009-04-10 12:28:57 -0700356 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700357 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700358 . "Add -f to options?\n";
359 }
360 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700361 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700362}
363
Joe Perches03372db2010-03-05 13:43:00 -0800364@file_emails = uniq(@file_emails);
365
Joe Perchescb7301c2009-04-07 20:40:12 -0700366my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700367my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700368my @scm = ();
369my @web = ();
370my @subsystem = ();
371my @status = ();
372
373# Find responsible parties
374
375foreach my $file (@files) {
376
Joe Perches272a8972010-01-08 14:42:48 -0800377 my %hash;
Joe Perchese3e9d112010-10-26 14:22:53 -0700378 my $exact_pattern_match = 0;
Joe Perches272a8972010-01-08 14:42:48 -0800379 my $tvi = find_first_section();
380 while ($tvi < @typevalue) {
381 my $start = find_starting_index($tvi);
382 my $end = find_ending_index($tvi);
383 my $exclude = 0;
384 my $i;
Joe Perchescb7301c2009-04-07 20:40:12 -0700385
Joe Perches272a8972010-01-08 14:42:48 -0800386 #Do not match excluded file patterns
Joe Perchescb7301c2009-04-07 20:40:12 -0700387
Joe Perches272a8972010-01-08 14:42:48 -0800388 for ($i = $start; $i < $end; $i++) {
389 my $line = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700390 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700391 my $type = $1;
392 my $value = $2;
Joe Perches272a8972010-01-08 14:42:48 -0800393 if ($type eq 'X') {
Joe Perchescb7301c2009-04-07 20:40:12 -0700394 if (file_match_pattern($file, $value)) {
Joe Perches272a8972010-01-08 14:42:48 -0800395 $exclude = 1;
Joe Perches3c840c12010-03-05 13:43:07 -0800396 last;
Joe Perches272a8972010-01-08 14:42:48 -0800397 }
398 }
399 }
400 }
401
402 if (!$exclude) {
403 for ($i = $start; $i < $end; $i++) {
404 my $line = $typevalue[$i];
405 if ($line =~ m/^(\C):\s*(.*)/) {
406 my $type = $1;
407 my $value = $2;
408 if ($type eq 'F') {
409 if (file_match_pattern($file, $value)) {
410 my $value_pd = ($value =~ tr@/@@);
411 my $file_pd = ($file =~ tr@/@@);
412 $value_pd++ if (substr($value,-1,1) ne "/");
Joe Perchese3e9d112010-10-26 14:22:53 -0700413 $value_pd = -1 if ($value =~ /^\.\*/);
414 $exact_pattern_match = 1 if ($value_pd >= $file_pd);
Joe Perches272a8972010-01-08 14:42:48 -0800415 if ($pattern_depth == 0 ||
416 (($file_pd - $value_pd) < $pattern_depth)) {
417 $hash{$tvi} = $value_pd;
418 }
Joe Perches3fb55652009-09-21 17:04:17 -0700419 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700420 }
421 }
422 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700423 }
Joe Perches272a8972010-01-08 14:42:48 -0800424
Joe Perches3c840c12010-03-05 13:43:07 -0800425 $tvi = $end + 1;
Joe Perches272a8972010-01-08 14:42:48 -0800426 }
427
428 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
429 add_categories($line);
Joe Perches6ffd9482010-10-26 14:22:51 -0700430 if ($sections) {
431 my $i;
432 my $start = find_starting_index($line);
433 my $end = find_ending_index($line);
434 for ($i = $start; $i < $end; $i++) {
435 my $line = $typevalue[$i];
436 if ($line =~ /^[FX]:/) { ##Restore file patterns
437 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
438 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
439 $line =~ s/\\\./\./g; ##Convert \. to .
440 $line =~ s/\.\*/\*/g; ##Convert .* to *
Joe Perches4b76c9d2010-03-05 13:43:03 -0800441 }
Joe Perches6ffd9482010-10-26 14:22:51 -0700442 $line =~ s/^([A-Z]):/$1:\t/g;
443 print("$line\n");
Joe Perches4b76c9d2010-03-05 13:43:03 -0800444 }
Joe Perches6ffd9482010-10-26 14:22:51 -0700445 print("\n");
446 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700447 }
448
Joe Perchese3e9d112010-10-26 14:22:53 -0700449 if ($email &&
450 ($email_git || ($email_git_fallback && !$exact_pattern_match))) {
Joe Perches60db31a2009-12-14 18:00:50 -0800451 vcs_file_signoffs($file);
Joe Perchescb7301c2009-04-07 20:40:12 -0700452 }
453
Joe Perchesf5492662009-09-21 17:04:13 -0700454 if ($email && $email_git_blame) {
Joe Perches60db31a2009-12-14 18:00:50 -0800455 vcs_file_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -0700456 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700457}
458
Joe Perchesdcf36a92009-10-26 16:49:47 -0700459if ($keywords) {
460 @keyword_tvi = sort_and_uniq(@keyword_tvi);
461 foreach my $line (@keyword_tvi) {
462 add_categories($line);
463 }
464}
465
Joe Perchesf5f50782009-06-16 15:34:00 -0700466if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700467 foreach my $chief (@penguin_chief) {
468 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700469 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700470
Joe Perchesa8af2432009-12-14 18:00:49 -0800471 $email_address = format_email($1, $2, $email_usename);
Joe Perchesf5f50782009-06-16 15:34:00 -0700472 if ($email_git_penguin_chiefs) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800473 push(@email_to, [$email_address, 'chief penguin']);
Joe Perchesf5f50782009-06-16 15:34:00 -0700474 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800475 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700476 }
477 }
478 }
Joe Perches03372db2010-03-05 13:43:00 -0800479
480 foreach my $email (@file_emails) {
481 my ($name, $address) = parse_email($email);
482
483 my $tmp_email = format_email($name, $address, $email_usename);
484 push_email_address($tmp_email, '');
485 add_role($tmp_email, 'in file');
486 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700487}
488
Joe Perches290603c2009-06-16 15:33:58 -0700489if ($email || $email_list) {
490 my @to = ();
491 if ($email) {
492 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700493 }
Joe Perches290603c2009-06-16 15:33:58 -0700494 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700495 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700496 }
Joe Perches3c7385b2009-12-14 18:00:46 -0800497 output(merge_email(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700498}
499
500if ($scm) {
Joe Perchesb7816552009-09-21 17:04:24 -0700501 @scm = uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700502 output(@scm);
503}
504
505if ($status) {
Joe Perchesb7816552009-09-21 17:04:24 -0700506 @status = uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700507 output(@status);
508}
509
510if ($subsystem) {
Joe Perchesb7816552009-09-21 17:04:24 -0700511 @subsystem = uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700512 output(@subsystem);
513}
514
515if ($web) {
Joe Perchesb7816552009-09-21 17:04:24 -0700516 @web = uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700517 output(@web);
518}
519
520exit($exit);
521
522sub file_match_pattern {
523 my ($file, $pattern) = @_;
524 if (substr($pattern, -1) eq "/") {
525 if ($file =~ m@^$pattern@) {
526 return 1;
527 }
528 } else {
529 if ($file =~ m@^$pattern@) {
530 my $s1 = ($file =~ tr@/@@);
531 my $s2 = ($pattern =~ tr@/@@);
532 if ($s1 == $s2) {
533 return 1;
534 }
535 }
536 }
537 return 0;
538}
539
540sub usage {
541 print <<EOT;
542usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700543 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700544version: $V
545
546MAINTAINER field selection options:
547 --email => print email address(es) if any
548 --git => include recent git \*-by: signers
Joe Perchese4d26b02010-05-24 14:33:17 -0700549 --git-all-signature-types => include signers regardless of signature type
550 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
Joe Perchese3e9d112010-10-26 14:22:53 -0700551 --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
Joe Perchescb7301c2009-04-07 20:40:12 -0700552 --git-chief-penguins => include ${penguin_chiefs}
Joe Perchese4d26b02010-05-24 14:33:17 -0700553 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
554 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
555 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
Joe Perchesf5492662009-09-21 17:04:13 -0700556 --git-blame => use git blame to find modified commits for patch or file
Joe Perchese4d26b02010-05-24 14:33:17 -0700557 --git-since => git history to use (default: $email_git_since)
558 --hg-since => hg history to use (default: $email_hg_since)
Joe Perchescb7301c2009-04-07 20:40:12 -0700559 --m => include maintainer(s) if any
560 --n => include name 'Full Name <addr\@domain.tld>'
561 --l => include list(s) if any
562 --s => include subscriber only list(s) if any
Joe Perches11ecf532009-09-21 17:04:22 -0700563 --remove-duplicates => minimize duplicate email names/addresses
Joe Perches3c7385b2009-12-14 18:00:46 -0800564 --roles => show roles (status:subsystem, git-signer, list, etc...)
565 --rolestats => show roles and statistics (commits/total_commits, %)
Joe Perches03372db2010-03-05 13:43:00 -0800566 --file-emails => add email addresses found in -f file (default: 0 (off))
Joe Perchescb7301c2009-04-07 20:40:12 -0700567 --scm => print SCM tree(s) if any
568 --status => print status if any
569 --subsystem => print subsystem name if any
570 --web => print website(s) if any
571
572Output type options:
573 --separator [, ] => separator for multiple entries on 1 line
Joe Perches42498312009-09-21 17:04:21 -0700574 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700575 --multiline => print 1 entry per line
576
Joe Perchescb7301c2009-04-07 20:40:12 -0700577Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700578 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesdcf36a92009-10-26 16:49:47 -0700579 --keywords => scan patch for keywords (default: 1 (on))
Joe Perches4b76c9d2010-03-05 13:43:03 -0800580 --sections => print the entire subsystem sections with pattern matches
Joe Perchesf5f50782009-06-16 15:34:00 -0700581 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700582 --help => show this help information
583
Joe Perches3fb55652009-09-21 17:04:17 -0700584Default options:
Joe Perches11ecf532009-09-21 17:04:22 -0700585 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
Joe Perches3fb55652009-09-21 17:04:17 -0700586
Joe Perches870020f2009-07-29 15:04:28 -0700587Notes:
588 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700589 Used with "--git", git signators for _all_ files in and below
590 directory are examined as git recurses directories.
591 Any specified X: (exclude) pattern matches are _not_ ignored.
592 Used with "--nogit", directory is used as a pattern match,
Joe Perches60db31a2009-12-14 18:00:50 -0800593 no individual file within the directory or subdirectory
594 is matched.
Joe Perchesf5492662009-09-21 17:04:13 -0700595 Used with "--git-blame", does not iterate all files in directory
596 Using "--git-blame" is slow and may add old committers and authors
597 that are no longer active maintainers to the output.
Joe Perches3c7385b2009-12-14 18:00:46 -0800598 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
599 other automated tools that expect only ["name"] <email address>
600 may not work because of additional output after <email address>.
601 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
602 not the percentage of the entire file authored. # of commits is
603 not a good measure of amount of code authored. 1 major commit may
604 contain a thousand lines, 5 trivial commits may modify a single line.
Joe Perches60db31a2009-12-14 18:00:50 -0800605 If git is not installed, but mercurial (hg) is installed and an .hg
606 repository exists, the following options apply to mercurial:
607 --git,
608 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
609 --git-blame
610 Use --hg-since not --git-since to control date selection
Joe Perches368669d2010-05-24 14:33:19 -0700611 File ".get_maintainer.conf", if it exists in the linux kernel source root
612 directory, can change whatever get_maintainer defaults are desired.
613 Entries in this file can be any command line argument.
614 This file is prepended to any additional command line arguments.
615 Multiple lines and # comments are allowed.
Joe Perchescb7301c2009-04-07 20:40:12 -0700616EOT
617}
618
619sub top_of_kernel_tree {
620 my ($lk_path) = @_;
621
622 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
623 $lk_path .= "/";
624 }
625 if ( (-f "${lk_path}COPYING")
626 && (-f "${lk_path}CREDITS")
627 && (-f "${lk_path}Kbuild")
628 && (-f "${lk_path}MAINTAINERS")
629 && (-f "${lk_path}Makefile")
630 && (-f "${lk_path}README")
631 && (-d "${lk_path}Documentation")
632 && (-d "${lk_path}arch")
633 && (-d "${lk_path}include")
634 && (-d "${lk_path}drivers")
635 && (-d "${lk_path}fs")
636 && (-d "${lk_path}init")
637 && (-d "${lk_path}ipc")
638 && (-d "${lk_path}kernel")
639 && (-d "${lk_path}lib")
640 && (-d "${lk_path}scripts")) {
641 return 1;
642 }
643 return 0;
644}
645
Joe Perches0e70e832009-09-21 17:04:20 -0700646sub parse_email {
647 my ($formatted_email) = @_;
648
649 my $name = "";
650 my $address = "";
651
Joe Perches11ecf532009-09-21 17:04:22 -0700652 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700653 $name = $1;
654 $address = $2;
Joe Perches11ecf532009-09-21 17:04:22 -0700655 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700656 $address = $1;
Joe Perchesb7816552009-09-21 17:04:24 -0700657 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700658 $address = $1;
659 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700660
661 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700662 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700663 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700664
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800665 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perchescb7301c2009-04-07 20:40:12 -0700666 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700667 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700668 }
Joe Perches0e70e832009-09-21 17:04:20 -0700669
670 return ($name, $address);
671}
672
673sub format_email {
Joe Perchesa8af2432009-12-14 18:00:49 -0800674 my ($name, $address, $usename) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700675
676 my $formatted_email;
677
678 $name =~ s/^\s+|\s+$//g;
679 $name =~ s/^\"|\"$//g;
680 $address =~ s/^\s+|\s+$//g;
681
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800682 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perches0e70e832009-09-21 17:04:20 -0700683 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
684 $name = "\"$name\"";
685 }
686
Joe Perchesa8af2432009-12-14 18:00:49 -0800687 if ($usename) {
Joe Perches0e70e832009-09-21 17:04:20 -0700688 if ("$name" eq "") {
689 $formatted_email = "$address";
690 } else {
Joe Perchesa8af2432009-12-14 18:00:49 -0800691 $formatted_email = "$name <$address>";
Joe Perches0e70e832009-09-21 17:04:20 -0700692 }
693 } else {
694 $formatted_email = $address;
695 }
696
Joe Perchescb7301c2009-04-07 20:40:12 -0700697 return $formatted_email;
698}
699
Joe Perches272a8972010-01-08 14:42:48 -0800700sub find_first_section {
701 my $index = 0;
702
703 while ($index < @typevalue) {
704 my $tv = $typevalue[$index];
705 if (($tv =~ m/^(\C):\s*(.*)/)) {
706 last;
707 }
708 $index++;
709 }
710
711 return $index;
712}
713
Joe Perchesb7816552009-09-21 17:04:24 -0700714sub find_starting_index {
Joe Perchesb7816552009-09-21 17:04:24 -0700715 my ($index) = @_;
716
717 while ($index > 0) {
718 my $tv = $typevalue[$index];
719 if (!($tv =~ m/^(\C):\s*(.*)/)) {
720 last;
721 }
722 $index--;
723 }
724
725 return $index;
726}
727
728sub find_ending_index {
729 my ($index) = @_;
730
731 while ($index < @typevalue) {
732 my $tv = $typevalue[$index];
733 if (!($tv =~ m/^(\C):\s*(.*)/)) {
734 last;
735 }
736 $index++;
737 }
738
739 return $index;
740}
741
Joe Perches3c7385b2009-12-14 18:00:46 -0800742sub get_maintainer_role {
743 my ($index) = @_;
744
745 my $i;
746 my $start = find_starting_index($index);
747 my $end = find_ending_index($index);
748
749 my $role;
750 my $subsystem = $typevalue[$start];
751 if (length($subsystem) > 20) {
752 $subsystem = substr($subsystem, 0, 17);
753 $subsystem =~ s/\s*$//;
754 $subsystem = $subsystem . "...";
755 }
756
757 for ($i = $start + 1; $i < $end; $i++) {
758 my $tv = $typevalue[$i];
759 if ($tv =~ m/^(\C):\s*(.*)/) {
760 my $ptype = $1;
761 my $pvalue = $2;
762 if ($ptype eq "S") {
763 $role = $pvalue;
764 }
765 }
766 }
767
768 $role = lc($role);
769 if ($role eq "supported") {
770 $role = "supporter";
771 } elsif ($role eq "maintained") {
772 $role = "maintainer";
773 } elsif ($role eq "odd fixes") {
774 $role = "odd fixer";
775 } elsif ($role eq "orphan") {
776 $role = "orphan minder";
777 } elsif ($role eq "obsolete") {
778 $role = "obsolete minder";
779 } elsif ($role eq "buried alive in reporters") {
780 $role = "chief penguin";
781 }
782
783 return $role . ":" . $subsystem;
784}
785
786sub get_list_role {
787 my ($index) = @_;
788
789 my $i;
790 my $start = find_starting_index($index);
791 my $end = find_ending_index($index);
792
793 my $subsystem = $typevalue[$start];
794 if (length($subsystem) > 20) {
795 $subsystem = substr($subsystem, 0, 17);
796 $subsystem =~ s/\s*$//;
797 $subsystem = $subsystem . "...";
798 }
799
800 if ($subsystem eq "THE REST") {
801 $subsystem = "";
802 }
803
804 return $subsystem;
805}
806
Joe Perchescb7301c2009-04-07 20:40:12 -0700807sub add_categories {
808 my ($index) = @_;
809
Joe Perchesb7816552009-09-21 17:04:24 -0700810 my $i;
811 my $start = find_starting_index($index);
812 my $end = find_ending_index($index);
813
814 push(@subsystem, $typevalue[$start]);
815
816 for ($i = $start + 1; $i < $end; $i++) {
817 my $tv = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700818 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700819 my $ptype = $1;
820 my $pvalue = $2;
821 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700822 my $list_address = $pvalue;
823 my $list_additional = "";
Joe Perches3c7385b2009-12-14 18:00:46 -0800824 my $list_role = get_list_role($i);
825
826 if ($list_role ne "") {
827 $list_role = ":" . $list_role;
828 }
Joe Perches290603c2009-06-16 15:33:58 -0700829 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
830 $list_address = $1;
831 $list_additional = $2;
832 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700833 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700834 if ($email_subscriber_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800835 push(@list_to, [$list_address, "subscriber list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700836 }
837 } else {
838 if ($email_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800839 push(@list_to, [$list_address, "open list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700840 }
841 }
842 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700843 my ($name, $address) = parse_email($pvalue);
844 if ($name eq "") {
Joe Perchesb7816552009-09-21 17:04:24 -0700845 if ($i > 0) {
846 my $tv = $typevalue[$i - 1];
Joe Perches0e70e832009-09-21 17:04:20 -0700847 if ($tv =~ m/^(\C):\s*(.*)/) {
848 if ($1 eq "P") {
849 $name = $2;
Joe Perchesa8af2432009-12-14 18:00:49 -0800850 $pvalue = format_email($name, $address, $email_usename);
Joe Perches5f2441e2009-06-16 15:34:02 -0700851 }
852 }
853 }
854 }
Joe Perches0e70e832009-09-21 17:04:20 -0700855 if ($email_maintainer) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800856 my $role = get_maintainer_role($i);
857 push_email_addresses($pvalue, $role);
Joe Perchescb7301c2009-04-07 20:40:12 -0700858 }
859 } elsif ($ptype eq "T") {
860 push(@scm, $pvalue);
861 } elsif ($ptype eq "W") {
862 push(@web, $pvalue);
863 } elsif ($ptype eq "S") {
864 push(@status, $pvalue);
865 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700866 }
867 }
868}
869
Joe Perches11ecf532009-09-21 17:04:22 -0700870my %email_hash_name;
871my %email_hash_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700872
Joe Perches11ecf532009-09-21 17:04:22 -0700873sub email_inuse {
874 my ($name, $address) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700875
Joe Perches11ecf532009-09-21 17:04:22 -0700876 return 1 if (($name eq "") && ($address eq ""));
877 return 1 if (($name ne "") && exists($email_hash_name{$name}));
878 return 1 if (($address ne "") && exists($email_hash_address{$address}));
879
Joe Perches0e70e832009-09-21 17:04:20 -0700880 return 0;
881}
882
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700883sub push_email_address {
Joe Perches3c7385b2009-12-14 18:00:46 -0800884 my ($line, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700885
Joe Perches0e70e832009-09-21 17:04:20 -0700886 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700887
Joe Perchesb7816552009-09-21 17:04:24 -0700888 if ($address eq "") {
889 return 0;
890 }
891
Joe Perches11ecf532009-09-21 17:04:22 -0700892 if (!$email_remove_duplicates) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800893 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700894 } elsif (!email_inuse($name, $address)) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800895 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700896 $email_hash_name{$name}++;
897 $email_hash_address{$address}++;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700898 }
Joe Perchesb7816552009-09-21 17:04:24 -0700899
900 return 1;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700901}
902
903sub push_email_addresses {
Joe Perches3c7385b2009-12-14 18:00:46 -0800904 my ($address, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700905
906 my @address_list = ();
907
Joe Perches5f2441e2009-06-16 15:34:02 -0700908 if (rfc822_valid($address)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800909 push_email_address($address, $role);
Joe Perches5f2441e2009-06-16 15:34:02 -0700910 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700911 my $array_count = shift(@address_list);
912 while (my $entry = shift(@address_list)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800913 push_email_address($entry, $role);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700914 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700915 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800916 if (!push_email_address($address, $role)) {
Joe Perchesb7816552009-09-21 17:04:24 -0700917 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
918 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700919 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700920}
921
Joe Perches3c7385b2009-12-14 18:00:46 -0800922sub add_role {
923 my ($line, $role) = @_;
924
925 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800926 my $email = format_email($name, $address, $email_usename);
Joe Perches3c7385b2009-12-14 18:00:46 -0800927
928 foreach my $entry (@email_to) {
929 if ($email_remove_duplicates) {
930 my ($entry_name, $entry_address) = parse_email($entry->[0]);
Joe Perches03372db2010-03-05 13:43:00 -0800931 if (($name eq $entry_name || $address eq $entry_address)
932 && ($role eq "" || !($entry->[1] =~ m/$role/))
933 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800934 if ($entry->[1] eq "") {
935 $entry->[1] = "$role";
936 } else {
937 $entry->[1] = "$entry->[1],$role";
938 }
939 }
940 } else {
Joe Perches03372db2010-03-05 13:43:00 -0800941 if ($email eq $entry->[0]
942 && ($role eq "" || !($entry->[1] =~ m/$role/))
943 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800944 if ($entry->[1] eq "") {
945 $entry->[1] = "$role";
946 } else {
947 $entry->[1] = "$entry->[1],$role";
948 }
949 }
950 }
951 }
952}
953
Joe Perchescb7301c2009-04-07 20:40:12 -0700954sub which {
955 my ($bin) = @_;
956
Joe Perchesf5f50782009-06-16 15:34:00 -0700957 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700958 if (-e "$path/$bin") {
959 return "$path/$bin";
960 }
961 }
962
963 return "";
964}
965
Joe Perchesbcde44e2010-10-26 14:22:53 -0700966sub which_conf {
967 my ($conf) = @_;
968
969 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
970 if (-e "$path/$conf") {
971 return "$path/$conf";
972 }
973 }
974
975 return "";
976}
977
Joe Perches8cbb3a72009-09-21 17:04:21 -0700978sub mailmap {
Joe Perchesa8af2432009-12-14 18:00:49 -0800979 my (@lines) = @_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700980 my %hash;
981
982 foreach my $line (@lines) {
983 my ($name, $address) = parse_email($line);
984 if (!exists($hash{$name})) {
985 $hash{$name} = $address;
Joe Perches11ecf532009-09-21 17:04:22 -0700986 } elsif ($address ne $hash{$name}) {
987 $address = $hash{$name};
Joe Perchesa8af2432009-12-14 18:00:49 -0800988 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700989 }
990 if (exists($mailmap{$name})) {
991 my $obj = $mailmap{$name};
992 foreach my $map_address (@$obj) {
993 if (($map_address eq $address) &&
994 ($map_address ne $hash{$name})) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800995 $line = format_email($name, $hash{$name}, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700996 }
997 }
998 }
999 }
1000
1001 return @lines;
1002}
1003
Joe Perches60db31a2009-12-14 18:00:50 -08001004sub git_execute_cmd {
1005 my ($cmd) = @_;
1006 my @lines = ();
Joe Perchescb7301c2009-04-07 20:40:12 -07001007
Joe Perches60db31a2009-12-14 18:00:50 -08001008 my $output = `$cmd`;
1009 $output =~ s/^\s*//gm;
1010 @lines = split("\n", $output);
1011
1012 return @lines;
Joe Perchesa8af2432009-12-14 18:00:49 -08001013}
1014
Joe Perches60db31a2009-12-14 18:00:50 -08001015sub hg_execute_cmd {
Joe Perchesa8af2432009-12-14 18:00:49 -08001016 my ($cmd) = @_;
Joe Perches60db31a2009-12-14 18:00:50 -08001017 my @lines = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001018
Joe Perches60db31a2009-12-14 18:00:50 -08001019 my $output = `$cmd`;
1020 @lines = split("\n", $output);
1021
1022 return @lines;
1023}
1024
1025sub vcs_find_signers {
1026 my ($cmd) = @_;
Joe Perchesa8af2432009-12-14 18:00:49 -08001027 my @lines = ();
1028 my $commits;
1029
Joe Perches60db31a2009-12-14 18:00:50 -08001030 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
Joe Perchescb7301c2009-04-07 20:40:12 -07001031
Joe Perches60db31a2009-12-14 18:00:50 -08001032 my $pattern = $VCS_cmds{"commit_pattern"};
Joe Perchescb7301c2009-04-07 20:40:12 -07001033
Joe Perches60db31a2009-12-14 18:00:50 -08001034 $commits = grep(/$pattern/, @lines); # of commits
Joe Perchesafa81ee2009-07-29 15:04:28 -07001035
Joe Perchese4d26b02010-05-24 14:33:17 -07001036 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
Joe Perches0e70e832009-09-21 17:04:20 -07001037 if (!$email_git_penguin_chiefs) {
1038 @lines = grep(!/${penguin_chiefs}/i, @lines);
1039 }
Joe Perches63ab52d2010-10-26 14:22:51 -07001040
1041 return (0, @lines) if !@lines;
1042
Joe Perches0e70e832009-09-21 17:04:20 -07001043 # cut -f2- -d":"
1044 s/.*:\s*(.+)\s*/$1/ for (@lines);
1045
Joe Perchesa8af2432009-12-14 18:00:49 -08001046## Reformat email addresses (with names) to avoid badly written signatures
1047
Joe Perches3c7385b2009-12-14 18:00:46 -08001048 foreach my $line (@lines) {
1049 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -08001050 $line = format_email($name, $address, 1);
1051 }
1052
1053 return ($commits, @lines);
1054}
1055
Joe Perches63ab52d2010-10-26 14:22:51 -07001056sub vcs_find_author {
1057 my ($cmd) = @_;
1058 my @lines = ();
1059
1060 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1061
1062 if (!$email_git_penguin_chiefs) {
1063 @lines = grep(!/${penguin_chiefs}/i, @lines);
1064 }
1065
1066 return @lines if !@lines;
1067
1068## Reformat email addresses (with names) to avoid badly written signatures
1069
1070 foreach my $line (@lines) {
1071 my ($name, $address) = parse_email($line);
1072 $line = format_email($name, $address, 1);
1073 }
1074
1075 return @lines;
1076}
1077
Joe Perches60db31a2009-12-14 18:00:50 -08001078sub vcs_save_commits {
1079 my ($cmd) = @_;
1080 my @lines = ();
1081 my @commits = ();
1082
1083 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1084
1085 foreach my $line (@lines) {
1086 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1087 push(@commits, $1);
1088 }
1089 }
1090
1091 return @commits;
1092}
1093
1094sub vcs_blame {
1095 my ($file) = @_;
1096 my $cmd;
1097 my @commits = ();
1098
1099 return @commits if (!(-f $file));
1100
1101 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1102 my @all_commits = ();
1103
1104 $cmd = $VCS_cmds{"blame_file_cmd"};
1105 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1106 @all_commits = vcs_save_commits($cmd);
1107
1108 foreach my $file_range_diff (@range) {
1109 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1110 my $diff_file = $1;
1111 my $diff_start = $2;
1112 my $diff_length = $3;
1113 next if ("$file" ne "$diff_file");
1114 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1115 push(@commits, $all_commits[$i]);
1116 }
1117 }
1118 } elsif (@range) {
1119 foreach my $file_range_diff (@range) {
1120 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1121 my $diff_file = $1;
1122 my $diff_start = $2;
1123 my $diff_length = $3;
1124 next if ("$file" ne "$diff_file");
1125 $cmd = $VCS_cmds{"blame_range_cmd"};
1126 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1127 push(@commits, vcs_save_commits($cmd));
1128 }
1129 } else {
1130 $cmd = $VCS_cmds{"blame_file_cmd"};
1131 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1132 @commits = vcs_save_commits($cmd);
1133 }
1134
Joe Perches63ab52d2010-10-26 14:22:51 -07001135 foreach my $commit (@commits) {
1136 $commit =~ s/^\^//g;
1137 }
1138
Joe Perches60db31a2009-12-14 18:00:50 -08001139 return @commits;
1140}
1141
1142my $printed_novcs = 0;
1143sub vcs_exists {
1144 %VCS_cmds = %VCS_cmds_git;
1145 return 1 if eval $VCS_cmds{"available"};
1146 %VCS_cmds = %VCS_cmds_hg;
1147 return 1 if eval $VCS_cmds{"available"};
1148 %VCS_cmds = ();
1149 if (!$printed_novcs) {
1150 warn("$P: No supported VCS found. Add --nogit to options?\n");
1151 warn("Using a git repository produces better results.\n");
1152 warn("Try Linus Torvalds' latest git repository using:\n");
1153 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1154 $printed_novcs = 1;
1155 }
1156 return 0;
1157}
1158
1159sub vcs_assign {
Joe Perchesa8af2432009-12-14 18:00:49 -08001160 my ($role, $divisor, @lines) = @_;
1161
1162 my %hash;
1163 my $count = 0;
1164
Joe Perchesa8af2432009-12-14 18:00:49 -08001165 return if (@lines <= 0);
1166
1167 if ($divisor <= 0) {
Joe Perches60db31a2009-12-14 18:00:50 -08001168 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
Joe Perchesa8af2432009-12-14 18:00:49 -08001169 $divisor = 1;
Joe Perches3c7385b2009-12-14 18:00:46 -08001170 }
Joe Perches8cbb3a72009-09-21 17:04:21 -07001171
Joe Perches11ecf532009-09-21 17:04:22 -07001172 if ($email_remove_duplicates) {
1173 @lines = mailmap(@lines);
1174 }
Joe Perches0e70e832009-09-21 17:04:20 -07001175
Joe Perches63ab52d2010-10-26 14:22:51 -07001176 return if (@lines <= 0);
1177
Joe Perches0e70e832009-09-21 17:04:20 -07001178 @lines = sort(@lines);
Joe Perchesafa81ee2009-07-29 15:04:28 -07001179
Joe Perches11ecf532009-09-21 17:04:22 -07001180 # uniq -c
1181 $hash{$_}++ for @lines;
1182
1183 # sort -rn
1184 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1185 my $sign_offs = $hash{$line};
Joe Perchesa8af2432009-12-14 18:00:49 -08001186 my $percent = $sign_offs * 100 / $divisor;
Joe Perches3c7385b2009-12-14 18:00:46 -08001187
Joe Perchesa8af2432009-12-14 18:00:49 -08001188 $percent = 100 if ($percent > 100);
Joe Perches11ecf532009-09-21 17:04:22 -07001189 $count++;
1190 last if ($sign_offs < $email_git_min_signatures ||
1191 $count > $email_git_max_maintainers ||
Joe Perchesa8af2432009-12-14 18:00:49 -08001192 $percent < $email_git_min_percent);
Joe Perches3c7385b2009-12-14 18:00:46 -08001193 push_email_address($line, '');
Joe Perches3c7385b2009-12-14 18:00:46 -08001194 if ($output_rolestats) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001195 my $fmt_percent = sprintf("%.0f", $percent);
1196 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1197 } else {
1198 add_role($line, $role);
Joe Perches3c7385b2009-12-14 18:00:46 -08001199 }
Joe Perchesf5492662009-09-21 17:04:13 -07001200 }
1201}
1202
Joe Perches60db31a2009-12-14 18:00:50 -08001203sub vcs_file_signoffs {
Joe Perchesa8af2432009-12-14 18:00:49 -08001204 my ($file) = @_;
1205
1206 my @signers = ();
Joe Perches60db31a2009-12-14 18:00:50 -08001207 my $commits;
Joe Perchesa8af2432009-12-14 18:00:49 -08001208
Joe Perches60db31a2009-12-14 18:00:50 -08001209 return if (!vcs_exists());
Joe Perchesa8af2432009-12-14 18:00:49 -08001210
Joe Perches60db31a2009-12-14 18:00:50 -08001211 my $cmd = $VCS_cmds{"find_signers_cmd"};
1212 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1213
1214 ($commits, @signers) = vcs_find_signers($cmd);
1215 vcs_assign("commit_signer", $commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001216}
1217
Joe Perches60db31a2009-12-14 18:00:50 -08001218sub vcs_file_blame {
Joe Perchesf5492662009-09-21 17:04:13 -07001219 my ($file) = @_;
1220
Joe Perches60db31a2009-12-14 18:00:50 -08001221 my @signers = ();
Joe Perches63ab52d2010-10-26 14:22:51 -07001222 my @all_commits = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001223 my @commits = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001224 my $total_commits;
Joe Perches63ab52d2010-10-26 14:22:51 -07001225 my $total_lines;
Joe Perchesf5492662009-09-21 17:04:13 -07001226
Joe Perches60db31a2009-12-14 18:00:50 -08001227 return if (!vcs_exists());
Joe Perchesf5492662009-09-21 17:04:13 -07001228
Joe Perches63ab52d2010-10-26 14:22:51 -07001229 @all_commits = vcs_blame($file);
1230 @commits = uniq(@all_commits);
Joe Perchesa8af2432009-12-14 18:00:49 -08001231 $total_commits = @commits;
Joe Perches63ab52d2010-10-26 14:22:51 -07001232 $total_lines = @all_commits;
Joe Perchesa8af2432009-12-14 18:00:49 -08001233
Joe Perchesf5492662009-09-21 17:04:13 -07001234 foreach my $commit (@commits) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001235 my $commit_count;
1236 my @commit_signers = ();
Joe Perchesf5492662009-09-21 17:04:13 -07001237
Joe Perches60db31a2009-12-14 18:00:50 -08001238 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1239 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1240
1241 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
Joe Perches63ab52d2010-10-26 14:22:51 -07001242
Joe Perches60db31a2009-12-14 18:00:50 -08001243 push(@signers, @commit_signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001244 }
1245
Joe Perchesa8af2432009-12-14 18:00:49 -08001246 if ($from_filename) {
Joe Perches63ab52d2010-10-26 14:22:51 -07001247 if ($output_rolestats) {
1248 my @blame_signers;
1249 foreach my $commit (@commits) {
1250 my $i;
1251 my $cmd = $VCS_cmds{"find_commit_author_cmd"};
1252 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1253 my @author = vcs_find_author($cmd);
1254 next if !@author;
1255 my $count = grep(/$commit/, @all_commits);
1256 for ($i = 0; $i < $count ; $i++) {
1257 push(@blame_signers, $author[0]);
1258 }
1259 }
1260 if (@blame_signers) {
1261 vcs_assign("authored lines", $total_lines, @blame_signers);
1262 }
1263 }
Joe Perches60db31a2009-12-14 18:00:50 -08001264 vcs_assign("commits", $total_commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001265 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001266 vcs_assign("modified commits", $total_commits, @signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001267 }
Joe Perchescb7301c2009-04-07 20:40:12 -07001268}
1269
1270sub uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001271 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001272
1273 my %saw;
1274 @parms = grep(!$saw{$_}++, @parms);
1275 return @parms;
1276}
1277
1278sub sort_and_uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001279 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001280
1281 my %saw;
1282 @parms = sort @parms;
1283 @parms = grep(!$saw{$_}++, @parms);
1284 return @parms;
1285}
1286
Joe Perches03372db2010-03-05 13:43:00 -08001287sub clean_file_emails {
1288 my (@file_emails) = @_;
1289 my @fmt_emails = ();
1290
1291 foreach my $email (@file_emails) {
1292 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1293 my ($name, $address) = parse_email($email);
1294 if ($name eq '"[,\.]"') {
1295 $name = "";
1296 }
1297
1298 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1299 if (@nw > 2) {
1300 my $first = $nw[@nw - 3];
1301 my $middle = $nw[@nw - 2];
1302 my $last = $nw[@nw - 1];
1303
1304 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1305 (length($first) == 2 && substr($first, -1) eq ".")) ||
1306 (length($middle) == 1 ||
1307 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1308 $name = "$first $middle $last";
1309 } else {
1310 $name = "$middle $last";
1311 }
1312 }
1313
1314 if (substr($name, -1) =~ /[,\.]/) {
1315 $name = substr($name, 0, length($name) - 1);
1316 } elsif (substr($name, -2) =~ /[,\.]"/) {
1317 $name = substr($name, 0, length($name) - 2) . '"';
1318 }
1319
1320 if (substr($name, 0, 1) =~ /[,\.]/) {
1321 $name = substr($name, 1, length($name) - 1);
1322 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1323 $name = '"' . substr($name, 2, length($name) - 2);
1324 }
1325
1326 my $fmt_email = format_email($name, $address, $email_usename);
1327 push(@fmt_emails, $fmt_email);
1328 }
1329 return @fmt_emails;
1330}
1331
Joe Perches3c7385b2009-12-14 18:00:46 -08001332sub merge_email {
1333 my @lines;
1334 my %saw;
1335
1336 for (@_) {
1337 my ($address, $role) = @$_;
1338 if (!$saw{$address}) {
1339 if ($output_roles) {
Joe Perches60db31a2009-12-14 18:00:50 -08001340 push(@lines, "$address ($role)");
Joe Perches3c7385b2009-12-14 18:00:46 -08001341 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001342 push(@lines, $address);
Joe Perches3c7385b2009-12-14 18:00:46 -08001343 }
1344 $saw{$address} = 1;
1345 }
1346 }
1347
1348 return @lines;
1349}
1350
Joe Perchescb7301c2009-04-07 20:40:12 -07001351sub output {
Joe Perchesa8af2432009-12-14 18:00:49 -08001352 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001353
1354 if ($output_multiline) {
1355 foreach my $line (@parms) {
1356 print("${line}\n");
1357 }
1358 } else {
1359 print(join($output_separator, @parms));
1360 print("\n");
1361 }
1362}
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001363
1364my $rfc822re;
1365
1366sub make_rfc822re {
1367# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1368# comment. We must allow for rfc822_lwsp (or comments) after each of these.
1369# This regexp will only work on addresses which have had comments stripped
1370# and replaced with rfc822_lwsp.
1371
1372 my $specials = '()<>@,;:\\\\".\\[\\]';
1373 my $controls = '\\000-\\037\\177';
1374
1375 my $dtext = "[^\\[\\]\\r\\\\]";
1376 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1377
1378 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1379
1380# Use zero-width assertion to spot the limit of an atom. A simple
1381# $rfc822_lwsp* causes the regexp engine to hang occasionally.
1382 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1383 my $word = "(?:$atom|$quoted_string)";
1384 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1385
1386 my $sub_domain = "(?:$atom|$domain_literal)";
1387 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1388
1389 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1390
1391 my $phrase = "$word*";
1392 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1393 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1394 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1395
1396 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1397 my $address = "(?:$mailbox|$group)";
1398
1399 return "$rfc822_lwsp*$address";
1400}
1401
1402sub rfc822_strip_comments {
1403 my $s = shift;
1404# Recursively remove comments, and replace with a single space. The simpler
1405# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1406# chars in atoms, for example.
1407
1408 while ($s =~ s/^((?:[^"\\]|\\.)*
1409 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1410 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1411 return $s;
1412}
1413
1414# valid: returns true if the parameter is an RFC822 valid address
1415#
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001416sub rfc822_valid {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001417 my $s = rfc822_strip_comments(shift);
1418
1419 if (!$rfc822re) {
1420 $rfc822re = make_rfc822re();
1421 }
1422
1423 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1424}
1425
1426# validlist: In scalar context, returns true if the parameter is an RFC822
1427# valid list of addresses.
1428#
1429# In list context, returns an empty list on failure (an invalid
1430# address was found); otherwise a list whose first element is the
1431# number of addresses found and whose remaining elements are the
1432# addresses. This is needed to disambiguate failure (invalid)
1433# from success with no addresses found, because an empty string is
1434# a valid list.
1435
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001436sub rfc822_validlist {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001437 my $s = rfc822_strip_comments(shift);
1438
1439 if (!$rfc822re) {
1440 $rfc822re = make_rfc822re();
1441 }
1442 # * null list items are valid according to the RFC
1443 # * the '1' business is to aid in distinguishing failure from no results
1444
1445 my @r;
1446 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1447 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -07001448 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches60db31a2009-12-14 18:00:50 -08001449 push(@r, $1);
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001450 }
1451 return wantarray ? (scalar(@r), @r) : 1;
1452 }
Joe Perches60db31a2009-12-14 18:00:50 -08001453 return wantarray ? () : 0;
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001454}