blob: 4e11c271e61366cc9afe7ad5d2f5d6788b763036 [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 Perches3c7385b2009-12-14 18:00:46 -080016my $V = '0.22';
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;
26my $email_git = 1;
27my $email_git_penguin_chiefs = 0;
28my $email_git_min_signatures = 1;
29my $email_git_max_maintainers = 5;
Joe Perchesafa81ee2009-07-29 15:04:28 -070030my $email_git_min_percent = 5;
Joe Perchescb7301c2009-04-07 20:40:12 -070031my $email_git_since = "1-year-ago";
Joe Perchesf5492662009-09-21 17:04:13 -070032my $email_git_blame = 0;
Joe Perches11ecf532009-09-21 17:04:22 -070033my $email_remove_duplicates = 1;
Joe Perchescb7301c2009-04-07 20:40:12 -070034my $output_multiline = 1;
35my $output_separator = ", ";
Joe Perches3c7385b2009-12-14 18:00:46 -080036my $output_roles = 0;
37my $output_rolestats = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070038my $scm = 0;
39my $web = 0;
40my $subsystem = 0;
41my $status = 0;
Joe Perchesdcf36a92009-10-26 16:49:47 -070042my $keywords = 1;
Joe Perches4a7fdb52009-04-10 12:28:57 -070043my $from_filename = 0;
Joe Perches3fb55652009-09-21 17:04:17 -070044my $pattern_depth = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070045my $version = 0;
46my $help = 0;
47
48my $exit = 0;
49
50my @penguin_chief = ();
51push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
52#Andrew wants in on most everything - 2009/01/14
53#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
54
55my @penguin_chief_names = ();
56foreach my $chief (@penguin_chief) {
57 if ($chief =~ m/^(.*):(.*)/) {
58 my $chief_name = $1;
59 my $chief_addr = $2;
60 push(@penguin_chief_names, $chief_name);
61 }
62}
63my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
64
Joe Perches5f2441e2009-06-16 15:34:02 -070065# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070066my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070067my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070068
Joe Perchescb7301c2009-04-07 20:40:12 -070069if (!GetOptions(
70 'email!' => \$email,
71 'git!' => \$email_git,
72 'git-chief-penguins!' => \$email_git_penguin_chiefs,
73 'git-min-signatures=i' => \$email_git_min_signatures,
74 'git-max-maintainers=i' => \$email_git_max_maintainers,
Joe Perchesafa81ee2009-07-29 15:04:28 -070075 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -070076 'git-since=s' => \$email_git_since,
Joe Perchesf5492662009-09-21 17:04:13 -070077 'git-blame!' => \$email_git_blame,
Joe Perches11ecf532009-09-21 17:04:22 -070078 'remove-duplicates!' => \$email_remove_duplicates,
Joe Perchescb7301c2009-04-07 20:40:12 -070079 'm!' => \$email_maintainer,
80 'n!' => \$email_usename,
81 'l!' => \$email_list,
82 's!' => \$email_subscriber_list,
83 'multiline!' => \$output_multiline,
Joe Perches3c7385b2009-12-14 18:00:46 -080084 'roles!' => \$output_roles,
85 'rolestats!' => \$output_rolestats,
Joe Perchescb7301c2009-04-07 20:40:12 -070086 'separator=s' => \$output_separator,
87 'subsystem!' => \$subsystem,
88 'status!' => \$status,
89 'scm!' => \$scm,
90 'web!' => \$web,
Joe Perches3fb55652009-09-21 17:04:17 -070091 'pattern-depth=i' => \$pattern_depth,
Joe Perchesdcf36a92009-10-26 16:49:47 -070092 'k|keywords!' => \$keywords,
Joe Perches4a7fdb52009-04-10 12:28:57 -070093 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -070094 'v|version' => \$version,
95 'h|help' => \$help,
96 )) {
Joe Perches3c7385b2009-12-14 18:00:46 -080097 die "$P: invalid argument - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -070098}
99
100if ($help != 0) {
101 usage();
102 exit 0;
103}
104
105if ($version != 0) {
106 print("${P} ${V}\n");
107 exit 0;
108}
109
Joe Perchescb7301c2009-04-07 20:40:12 -0700110if ($#ARGV < 0) {
111 usage();
112 die "$P: argument missing: patchfile or -f file please\n";
113}
114
Joe Perches42498312009-09-21 17:04:21 -0700115if ($output_separator ne ", ") {
116 $output_multiline = 0;
117}
118
Joe Perches3c7385b2009-12-14 18:00:46 -0800119if ($output_rolestats) {
120 $output_roles = 1;
121}
122
Joe Perchescb7301c2009-04-07 20:40:12 -0700123my $selections = $email + $scm + $status + $subsystem + $web;
124if ($selections == 0) {
125 usage();
126 die "$P: Missing required option: email, scm, status, subsystem or web\n";
127}
128
Joe Perchesf5492662009-09-21 17:04:13 -0700129if ($email &&
130 ($email_maintainer + $email_list + $email_subscriber_list +
131 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700132 usage();
133 die "$P: Please select at least 1 email option\n";
134}
135
136if (!top_of_kernel_tree($lk_path)) {
137 die "$P: The current directory does not appear to be "
138 . "a linux kernel source tree.\n";
139}
140
141## Read MAINTAINERS for type/value pairs
142
143my @typevalue = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700144my %keyword_hash;
145
Joe Perchescb7301c2009-04-07 20:40:12 -0700146open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
147while (<MAINT>) {
148 my $line = $_;
149
150 if ($line =~ m/^(\C):\s*(.*)/) {
151 my $type = $1;
152 my $value = $2;
153
154 ##Filename pattern matching
155 if ($type eq "F" || $type eq "X") {
156 $value =~ s@\.@\\\.@g; ##Convert . to \.
157 $value =~ s/\*/\.\*/g; ##Convert * to .*
158 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700159 ##if pattern is a directory and it lacks a trailing slash, add one
160 if ((-d $value)) {
161 $value =~ s@([^/])$@$1/@;
162 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700163 } elsif ($type eq "K") {
164 $keyword_hash{@typevalue} = $value;
Joe Perchescb7301c2009-04-07 20:40:12 -0700165 }
166 push(@typevalue, "$type:$value");
167 } elsif (!/^(\s)*$/) {
168 $line =~ s/\n$//g;
169 push(@typevalue, $line);
170 }
171}
172close(MAINT);
173
Joe Perches8cbb3a72009-09-21 17:04:21 -0700174my %mailmap;
175
Joe Perches11ecf532009-09-21 17:04:22 -0700176if ($email_remove_duplicates) {
177 open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
178 while (<MAILMAP>) {
179 my $line = $_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700180
Joe Perches11ecf532009-09-21 17:04:22 -0700181 next if ($line =~ m/^\s*#/);
182 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700183
Joe Perches11ecf532009-09-21 17:04:22 -0700184 my ($name, $address) = parse_email($line);
185 $line = format_email($name, $address);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700186
Joe Perches11ecf532009-09-21 17:04:22 -0700187 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700188
Joe Perches11ecf532009-09-21 17:04:22 -0700189 if (exists($mailmap{$name})) {
190 my $obj = $mailmap{$name};
191 push(@$obj, $address);
192 } else {
193 my @arr = ($address);
194 $mailmap{$name} = \@arr;
195 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700196 }
Joe Perches11ecf532009-09-21 17:04:22 -0700197 close(MAILMAP);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700198}
199
Joe Perches4a7fdb52009-04-10 12:28:57 -0700200## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700201
202my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700203my @range = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700204my @keyword_tvi = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700205
Joe Perches4a7fdb52009-04-10 12:28:57 -0700206foreach my $file (@ARGV) {
Joe Perches870020f2009-07-29 15:04:28 -0700207 ##if $file is a directory and it lacks a trailing slash, add one
208 if ((-d $file)) {
209 $file =~ s@([^/])$@$1/@;
210 } elsif (!(-f $file)) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700211 die "$P: file '${file}' not found\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700212 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700213 if ($from_filename) {
214 push(@files, $file);
Joe Perchesdcf36a92009-10-26 16:49:47 -0700215 if (-f $file && $keywords) {
216 open(FILE, "<$file") or die "$P: Can't open ${file}\n";
217 while (<FILE>) {
218 my $patch_line = $_;
219 foreach my $line (keys %keyword_hash) {
220 if ($patch_line =~ m/^.*$keyword_hash{$line}/x) {
221 push(@keyword_tvi, $line);
222 }
223 }
224 }
225 close(FILE);
226 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700227 } else {
228 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700229 my $lastfile;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700230 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
231 while (<PATCH>) {
Joe Perchesdcf36a92009-10-26 16:49:47 -0700232 my $patch_line = $_;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700233 if (m/^\+\+\+\s+(\S+)/) {
234 my $filename = $1;
235 $filename =~ s@^[^/]*/@@;
236 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700237 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700238 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700239 } elsif (m/^\@\@ -(\d+),(\d+)/) {
240 if ($email_git_blame) {
241 push(@range, "$lastfile:$1:$2");
242 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700243 } elsif ($keywords) {
244 foreach my $line (keys %keyword_hash) {
245 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
246 push(@keyword_tvi, $line);
247 }
248 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700249 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700250 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700251 close(PATCH);
252 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700253 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700254 . "Add -f to options?\n";
255 }
256 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700257 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700258}
259
260my @email_to = ();
Joe Perches290603c12009-06-16 15:33:58 -0700261my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700262my @scm = ();
263my @web = ();
264my @subsystem = ();
265my @status = ();
266
267# Find responsible parties
268
269foreach my $file (@files) {
270
271#Do not match excluded file patterns
272
273 my $exclude = 0;
274 foreach my $line (@typevalue) {
Joe Perches290603c12009-06-16 15:33:58 -0700275 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700276 my $type = $1;
277 my $value = $2;
278 if ($type eq 'X') {
279 if (file_match_pattern($file, $value)) {
280 $exclude = 1;
Joe Perches1d606b42009-09-21 17:04:14 -0700281 last;
Joe Perchescb7301c2009-04-07 20:40:12 -0700282 }
283 }
284 }
285 }
286
287 if (!$exclude) {
288 my $tvi = 0;
Joe Perches1d606b42009-09-21 17:04:14 -0700289 my %hash;
Joe Perchescb7301c2009-04-07 20:40:12 -0700290 foreach my $line (@typevalue) {
Joe Perches290603c12009-06-16 15:33:58 -0700291 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700292 my $type = $1;
293 my $value = $2;
294 if ($type eq 'F') {
295 if (file_match_pattern($file, $value)) {
Joe Perches3fb55652009-09-21 17:04:17 -0700296 my $value_pd = ($value =~ tr@/@@);
297 my $file_pd = ($file =~ tr@/@@);
298 $value_pd++ if (substr($value,-1,1) ne "/");
299 if ($pattern_depth == 0 ||
300 (($file_pd - $value_pd) < $pattern_depth)) {
301 $hash{$tvi} = $value_pd;
302 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700303 }
304 }
305 }
306 $tvi++;
307 }
Joe Perches1d606b42009-09-21 17:04:14 -0700308 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
309 add_categories($line);
310 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700311 }
312
Joe Perches4a7fdb52009-04-10 12:28:57 -0700313 if ($email && $email_git) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700314 recent_git_signoffs($file);
315 }
316
Joe Perchesf5492662009-09-21 17:04:13 -0700317 if ($email && $email_git_blame) {
318 git_assign_blame($file);
319 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700320}
321
Joe Perchesdcf36a92009-10-26 16:49:47 -0700322if ($keywords) {
323 @keyword_tvi = sort_and_uniq(@keyword_tvi);
324 foreach my $line (@keyword_tvi) {
325 add_categories($line);
326 }
327}
328
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700329if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700330 foreach my $chief (@penguin_chief) {
331 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700332 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700333
334 $email_address = format_email($1, $2);
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700335 if ($email_git_penguin_chiefs) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800336 push(@email_to, [$email_address, 'chief penguin']);
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700337 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800338 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700339 }
340 }
341 }
342}
343
Joe Perches290603c12009-06-16 15:33:58 -0700344if ($email || $email_list) {
345 my @to = ();
346 if ($email) {
347 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700348 }
Joe Perches290603c12009-06-16 15:33:58 -0700349 if ($email_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700350 @to = (@to, @list_to);
Joe Perches290603c12009-06-16 15:33:58 -0700351 }
Joe Perches3c7385b2009-12-14 18:00:46 -0800352 output(merge_email(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700353}
354
355if ($scm) {
Joe Perchesb7816552009-09-21 17:04:24 -0700356 @scm = uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700357 output(@scm);
358}
359
360if ($status) {
Joe Perchesb7816552009-09-21 17:04:24 -0700361 @status = uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700362 output(@status);
363}
364
365if ($subsystem) {
Joe Perchesb7816552009-09-21 17:04:24 -0700366 @subsystem = uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700367 output(@subsystem);
368}
369
370if ($web) {
Joe Perchesb7816552009-09-21 17:04:24 -0700371 @web = uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700372 output(@web);
373}
374
375exit($exit);
376
377sub file_match_pattern {
378 my ($file, $pattern) = @_;
379 if (substr($pattern, -1) eq "/") {
380 if ($file =~ m@^$pattern@) {
381 return 1;
382 }
383 } else {
384 if ($file =~ m@^$pattern@) {
385 my $s1 = ($file =~ tr@/@@);
386 my $s2 = ($pattern =~ tr@/@@);
387 if ($s1 == $s2) {
388 return 1;
389 }
390 }
391 }
392 return 0;
393}
394
395sub usage {
396 print <<EOT;
397usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700398 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700399version: $V
400
401MAINTAINER field selection options:
402 --email => print email address(es) if any
403 --git => include recent git \*-by: signers
404 --git-chief-penguins => include ${penguin_chiefs}
405 --git-min-signatures => number of signatures required (default: 1)
406 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700407 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchescb7301c2009-04-07 20:40:12 -0700408 --git-since => git history to use (default: 1-year-ago)
Joe Perchesf5492662009-09-21 17:04:13 -0700409 --git-blame => use git blame to find modified commits for patch or file
Joe Perchescb7301c2009-04-07 20:40:12 -0700410 --m => include maintainer(s) if any
411 --n => include name 'Full Name <addr\@domain.tld>'
412 --l => include list(s) if any
413 --s => include subscriber only list(s) if any
Joe Perches11ecf532009-09-21 17:04:22 -0700414 --remove-duplicates => minimize duplicate email names/addresses
Joe Perches3c7385b2009-12-14 18:00:46 -0800415 --roles => show roles (status:subsystem, git-signer, list, etc...)
416 --rolestats => show roles and statistics (commits/total_commits, %)
Joe Perchescb7301c2009-04-07 20:40:12 -0700417 --scm => print SCM tree(s) if any
418 --status => print status if any
419 --subsystem => print subsystem name if any
420 --web => print website(s) if any
421
422Output type options:
423 --separator [, ] => separator for multiple entries on 1 line
Joe Perches42498312009-09-21 17:04:21 -0700424 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700425 --multiline => print 1 entry per line
426
Joe Perchescb7301c2009-04-07 20:40:12 -0700427Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700428 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesdcf36a92009-10-26 16:49:47 -0700429 --keywords => scan patch for keywords (default: 1 (on))
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700430 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700431 --help => show this help information
432
Joe Perches3fb55652009-09-21 17:04:17 -0700433Default options:
Joe Perches11ecf532009-09-21 17:04:22 -0700434 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
Joe Perches3fb55652009-09-21 17:04:17 -0700435
Joe Perches870020f2009-07-29 15:04:28 -0700436Notes:
437 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700438 Used with "--git", git signators for _all_ files in and below
439 directory are examined as git recurses directories.
440 Any specified X: (exclude) pattern matches are _not_ ignored.
441 Used with "--nogit", directory is used as a pattern match,
442 no individual file within the directory or subdirectory
443 is matched.
444 Used with "--git-blame", does not iterate all files in directory
445 Using "--git-blame" is slow and may add old committers and authors
446 that are no longer active maintainers to the output.
Joe Perches3c7385b2009-12-14 18:00:46 -0800447 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
448 other automated tools that expect only ["name"] <email address>
449 may not work because of additional output after <email address>.
450 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
451 not the percentage of the entire file authored. # of commits is
452 not a good measure of amount of code authored. 1 major commit may
453 contain a thousand lines, 5 trivial commits may modify a single line.
Joe Perchescb7301c2009-04-07 20:40:12 -0700454EOT
455}
456
457sub top_of_kernel_tree {
458 my ($lk_path) = @_;
459
460 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
461 $lk_path .= "/";
462 }
463 if ( (-f "${lk_path}COPYING")
464 && (-f "${lk_path}CREDITS")
465 && (-f "${lk_path}Kbuild")
466 && (-f "${lk_path}MAINTAINERS")
467 && (-f "${lk_path}Makefile")
468 && (-f "${lk_path}README")
469 && (-d "${lk_path}Documentation")
470 && (-d "${lk_path}arch")
471 && (-d "${lk_path}include")
472 && (-d "${lk_path}drivers")
473 && (-d "${lk_path}fs")
474 && (-d "${lk_path}init")
475 && (-d "${lk_path}ipc")
476 && (-d "${lk_path}kernel")
477 && (-d "${lk_path}lib")
478 && (-d "${lk_path}scripts")) {
479 return 1;
480 }
481 return 0;
482}
483
Joe Perches0e70e832009-09-21 17:04:20 -0700484sub parse_email {
485 my ($formatted_email) = @_;
486
487 my $name = "";
488 my $address = "";
489
Joe Perches11ecf532009-09-21 17:04:22 -0700490 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700491 $name = $1;
492 $address = $2;
Joe Perches11ecf532009-09-21 17:04:22 -0700493 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700494 $address = $1;
Joe Perchesb7816552009-09-21 17:04:24 -0700495 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700496 $address = $1;
497 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700498
499 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700500 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700501 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700502
503 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
504 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700505 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700506 }
Joe Perches0e70e832009-09-21 17:04:20 -0700507
508 return ($name, $address);
509}
510
511sub format_email {
512 my ($name, $address) = @_;
513
514 my $formatted_email;
515
516 $name =~ s/^\s+|\s+$//g;
517 $name =~ s/^\"|\"$//g;
518 $address =~ s/^\s+|\s+$//g;
519
520 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
521 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
522 $name = "\"$name\"";
523 }
524
525 if ($email_usename) {
526 if ("$name" eq "") {
527 $formatted_email = "$address";
528 } else {
529 $formatted_email = "$name <${address}>";
530 }
531 } else {
532 $formatted_email = $address;
533 }
534
Joe Perchescb7301c2009-04-07 20:40:12 -0700535 return $formatted_email;
536}
537
Joe Perchesb7816552009-09-21 17:04:24 -0700538sub find_starting_index {
Joe Perchesb7816552009-09-21 17:04:24 -0700539 my ($index) = @_;
540
541 while ($index > 0) {
542 my $tv = $typevalue[$index];
543 if (!($tv =~ m/^(\C):\s*(.*)/)) {
544 last;
545 }
546 $index--;
547 }
548
549 return $index;
550}
551
552sub find_ending_index {
553 my ($index) = @_;
554
555 while ($index < @typevalue) {
556 my $tv = $typevalue[$index];
557 if (!($tv =~ m/^(\C):\s*(.*)/)) {
558 last;
559 }
560 $index++;
561 }
562
563 return $index;
564}
565
Joe Perches3c7385b2009-12-14 18:00:46 -0800566sub get_maintainer_role {
567 my ($index) = @_;
568
569 my $i;
570 my $start = find_starting_index($index);
571 my $end = find_ending_index($index);
572
573 my $role;
574 my $subsystem = $typevalue[$start];
575 if (length($subsystem) > 20) {
576 $subsystem = substr($subsystem, 0, 17);
577 $subsystem =~ s/\s*$//;
578 $subsystem = $subsystem . "...";
579 }
580
581 for ($i = $start + 1; $i < $end; $i++) {
582 my $tv = $typevalue[$i];
583 if ($tv =~ m/^(\C):\s*(.*)/) {
584 my $ptype = $1;
585 my $pvalue = $2;
586 if ($ptype eq "S") {
587 $role = $pvalue;
588 }
589 }
590 }
591
592 $role = lc($role);
593 if ($role eq "supported") {
594 $role = "supporter";
595 } elsif ($role eq "maintained") {
596 $role = "maintainer";
597 } elsif ($role eq "odd fixes") {
598 $role = "odd fixer";
599 } elsif ($role eq "orphan") {
600 $role = "orphan minder";
601 } elsif ($role eq "obsolete") {
602 $role = "obsolete minder";
603 } elsif ($role eq "buried alive in reporters") {
604 $role = "chief penguin";
605 }
606
607 return $role . ":" . $subsystem;
608}
609
610sub get_list_role {
611 my ($index) = @_;
612
613 my $i;
614 my $start = find_starting_index($index);
615 my $end = find_ending_index($index);
616
617 my $subsystem = $typevalue[$start];
618 if (length($subsystem) > 20) {
619 $subsystem = substr($subsystem, 0, 17);
620 $subsystem =~ s/\s*$//;
621 $subsystem = $subsystem . "...";
622 }
623
624 if ($subsystem eq "THE REST") {
625 $subsystem = "";
626 }
627
628 return $subsystem;
629}
630
Joe Perchescb7301c2009-04-07 20:40:12 -0700631sub add_categories {
632 my ($index) = @_;
633
Joe Perchesb7816552009-09-21 17:04:24 -0700634 my $i;
635 my $start = find_starting_index($index);
636 my $end = find_ending_index($index);
637
638 push(@subsystem, $typevalue[$start]);
639
640 for ($i = $start + 1; $i < $end; $i++) {
641 my $tv = $typevalue[$i];
Joe Perches290603c12009-06-16 15:33:58 -0700642 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700643 my $ptype = $1;
644 my $pvalue = $2;
645 if ($ptype eq "L") {
Joe Perches290603c12009-06-16 15:33:58 -0700646 my $list_address = $pvalue;
647 my $list_additional = "";
Joe Perches3c7385b2009-12-14 18:00:46 -0800648 my $list_role = get_list_role($i);
649
650 if ($list_role ne "") {
651 $list_role = ":" . $list_role;
652 }
Joe Perches290603c12009-06-16 15:33:58 -0700653 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
654 $list_address = $1;
655 $list_additional = $2;
656 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700657 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700658 if ($email_subscriber_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800659 push(@list_to, [$list_address, "subscriber list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700660 }
661 } else {
662 if ($email_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800663 push(@list_to, [$list_address, "open list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700664 }
665 }
666 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700667 my ($name, $address) = parse_email($pvalue);
668 if ($name eq "") {
Joe Perchesb7816552009-09-21 17:04:24 -0700669 if ($i > 0) {
670 my $tv = $typevalue[$i - 1];
Joe Perches0e70e832009-09-21 17:04:20 -0700671 if ($tv =~ m/^(\C):\s*(.*)/) {
672 if ($1 eq "P") {
673 $name = $2;
Joe Perchesb7816552009-09-21 17:04:24 -0700674 $pvalue = format_email($name, $address);
Joe Perches5f2441e2009-06-16 15:34:02 -0700675 }
676 }
677 }
678 }
Joe Perches0e70e832009-09-21 17:04:20 -0700679 if ($email_maintainer) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800680 my $role = get_maintainer_role($i);
681 push_email_addresses($pvalue, $role);
Joe Perchescb7301c2009-04-07 20:40:12 -0700682 }
683 } elsif ($ptype eq "T") {
684 push(@scm, $pvalue);
685 } elsif ($ptype eq "W") {
686 push(@web, $pvalue);
687 } elsif ($ptype eq "S") {
688 push(@status, $pvalue);
689 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700690 }
691 }
692}
693
Joe Perches11ecf532009-09-21 17:04:22 -0700694my %email_hash_name;
695my %email_hash_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700696
Joe Perches11ecf532009-09-21 17:04:22 -0700697sub email_inuse {
698 my ($name, $address) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700699
Joe Perches11ecf532009-09-21 17:04:22 -0700700 return 1 if (($name eq "") && ($address eq ""));
701 return 1 if (($name ne "") && exists($email_hash_name{$name}));
702 return 1 if (($address ne "") && exists($email_hash_address{$address}));
703
Joe Perches0e70e832009-09-21 17:04:20 -0700704 return 0;
705}
706
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700707sub push_email_address {
Joe Perches3c7385b2009-12-14 18:00:46 -0800708 my ($line, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700709
Joe Perches0e70e832009-09-21 17:04:20 -0700710 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700711
Joe Perchesb7816552009-09-21 17:04:24 -0700712 if ($address eq "") {
713 return 0;
714 }
715
Joe Perches11ecf532009-09-21 17:04:22 -0700716 if (!$email_remove_duplicates) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800717 push(@email_to, [format_email($name, $address), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700718 } elsif (!email_inuse($name, $address)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800719 push(@email_to, [format_email($name, $address), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700720 $email_hash_name{$name}++;
721 $email_hash_address{$address}++;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700722 }
Joe Perchesb7816552009-09-21 17:04:24 -0700723
724 return 1;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700725}
726
727sub push_email_addresses {
Joe Perches3c7385b2009-12-14 18:00:46 -0800728 my ($address, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700729
730 my @address_list = ();
731
Joe Perches5f2441e2009-06-16 15:34:02 -0700732 if (rfc822_valid($address)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800733 push_email_address($address, $role);
Joe Perches5f2441e2009-06-16 15:34:02 -0700734 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700735 my $array_count = shift(@address_list);
736 while (my $entry = shift(@address_list)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800737 push_email_address($entry, $role);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700738 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700739 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800740 if (!push_email_address($address, $role)) {
Joe Perchesb7816552009-09-21 17:04:24 -0700741 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
742 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700743 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700744}
745
Joe Perches3c7385b2009-12-14 18:00:46 -0800746sub add_role {
747 my ($line, $role) = @_;
748
749 my ($name, $address) = parse_email($line);
750 my $email = format_email($name, $address);
751
752 foreach my $entry (@email_to) {
753 if ($email_remove_duplicates) {
754 my ($entry_name, $entry_address) = parse_email($entry->[0]);
755 if ($name eq $entry_name || $address eq $entry_address) {
756 if ($entry->[1] eq "") {
757 $entry->[1] = "$role";
758 } else {
759 $entry->[1] = "$entry->[1],$role";
760 }
761 }
762 } else {
763 if ($email eq $entry->[0]) {
764 if ($entry->[1] eq "") {
765 $entry->[1] = "$role";
766 } else {
767 $entry->[1] = "$entry->[1],$role";
768 }
769 }
770 }
771 }
772}
773
Joe Perchescb7301c2009-04-07 20:40:12 -0700774sub which {
775 my ($bin) = @_;
776
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700777 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700778 if (-e "$path/$bin") {
779 return "$path/$bin";
780 }
781 }
782
783 return "";
784}
785
Joe Perches8cbb3a72009-09-21 17:04:21 -0700786sub mailmap {
787 my @lines = @_;
788 my %hash;
789
790 foreach my $line (@lines) {
791 my ($name, $address) = parse_email($line);
792 if (!exists($hash{$name})) {
793 $hash{$name} = $address;
Joe Perches11ecf532009-09-21 17:04:22 -0700794 } elsif ($address ne $hash{$name}) {
795 $address = $hash{$name};
796 $line = format_email($name, $address);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700797 }
798 if (exists($mailmap{$name})) {
799 my $obj = $mailmap{$name};
800 foreach my $map_address (@$obj) {
801 if (($map_address eq $address) &&
802 ($map_address ne $hash{$name})) {
803 $line = format_email($name, $hash{$name});
804 }
805 }
806 }
807 }
808
809 return @lines;
810}
811
Joe Perchescb7301c2009-04-07 20:40:12 -0700812sub recent_git_signoffs {
813 my ($file) = @_;
814
815 my $sign_offs = "";
816 my $cmd = "";
817 my $output = "";
818 my $count = 0;
819 my @lines = ();
Joe Perches0e70e832009-09-21 17:04:20 -0700820 my %hash;
Joe Perchesafa81ee2009-07-29 15:04:28 -0700821 my $total_sign_offs;
Joe Perchescb7301c2009-04-07 20:40:12 -0700822
823 if (which("git") eq "") {
Joe Perchesde2fc492009-06-16 15:34:01 -0700824 warn("$P: git not found. Add --nogit to options?\n");
825 return;
826 }
827 if (!(-d ".git")) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700828 warn("$P: .git directory not found. Use a git repository for better results.\n");
829 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
Joe Perchesde2fc492009-06-16 15:34:01 -0700830 return;
Joe Perchescb7301c2009-04-07 20:40:12 -0700831 }
832
833 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perchescb7301c2009-04-07 20:40:12 -0700834
835 $output = `${cmd}`;
836 $output =~ s/^\s*//gm;
837
838 @lines = split("\n", $output);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700839
Joe Perches0e70e832009-09-21 17:04:20 -0700840 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
841 if (!$email_git_penguin_chiefs) {
842 @lines = grep(!/${penguin_chiefs}/i, @lines);
843 }
844 # cut -f2- -d":"
845 s/.*:\s*(.+)\s*/$1/ for (@lines);
846
Joe Perches8cbb3a72009-09-21 17:04:21 -0700847 $total_sign_offs = @lines;
Joe Perches3c7385b2009-12-14 18:00:46 -0800848 foreach my $line (@lines) {
849 my ($name, $address) = parse_email($line);
850 $line = format_email($name, $address);
851 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700852
Joe Perches11ecf532009-09-21 17:04:22 -0700853 if ($email_remove_duplicates) {
854 @lines = mailmap(@lines);
855 }
Joe Perches0e70e832009-09-21 17:04:20 -0700856
Joe Perches0e70e832009-09-21 17:04:20 -0700857 @lines = sort(@lines);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700858
Joe Perches11ecf532009-09-21 17:04:22 -0700859 # uniq -c
860 $hash{$_}++ for @lines;
861
862 # sort -rn
863 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
864 my $sign_offs = $hash{$line};
Joe Perches3c7385b2009-12-14 18:00:46 -0800865 my $role;
866
Joe Perches11ecf532009-09-21 17:04:22 -0700867 $count++;
868 last if ($sign_offs < $email_git_min_signatures ||
869 $count > $email_git_max_maintainers ||
870 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
Joe Perches3c7385b2009-12-14 18:00:46 -0800871 push_email_address($line, '');
872 $role = "git-signer";
873 if ($output_rolestats) {
874 my $percent = sprintf("%.0f", $sign_offs * 100 / $total_sign_offs);
875 $role = "$role:$sign_offs/$total_sign_offs=$percent%";
876 }
877 add_role($line, $role);
Joe Perchesf5492662009-09-21 17:04:13 -0700878 }
879}
880
881sub save_commits {
882 my ($cmd, @commits) = @_;
883 my $output;
884 my @lines = ();
885
886 $output = `${cmd}`;
887
888 @lines = split("\n", $output);
889 foreach my $line (@lines) {
890 if ($line =~ m/^(\w+) /) {
891 push (@commits, $1);
Joe Perchescb7301c2009-04-07 20:40:12 -0700892 }
893 }
Joe Perchesf5492662009-09-21 17:04:13 -0700894 return @commits;
895}
896
897sub git_assign_blame {
898 my ($file) = @_;
899
900 my @lines = ();
901 my @commits = ();
902 my $cmd;
903 my $output;
904 my %hash;
905 my $total_sign_offs;
906 my $count;
907
908 if (@range) {
909 foreach my $file_range_diff (@range) {
910 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
911 my $diff_file = $1;
912 my $diff_start = $2;
913 my $diff_length = $3;
914 next if (!("$file" eq "$diff_file"));
Joe Perches8cbb3a72009-09-21 17:04:21 -0700915 $cmd = "git blame -l -L $diff_start,+$diff_length $file";
Joe Perchesf5492662009-09-21 17:04:13 -0700916 @commits = save_commits($cmd, @commits);
917 }
918 } else {
919 if (-f $file) {
Joe Perches8cbb3a72009-09-21 17:04:21 -0700920 $cmd = "git blame -l $file";
Joe Perchesf5492662009-09-21 17:04:13 -0700921 @commits = save_commits($cmd, @commits);
922 }
923 }
924
925 $total_sign_offs = 0;
926 @commits = uniq(@commits);
927 foreach my $commit (@commits) {
928 $cmd = "git log -1 ${commit}";
Joe Perchesf5492662009-09-21 17:04:13 -0700929
930 $output = `${cmd}`;
931 $output =~ s/^\s*//gm;
932 @lines = split("\n", $output);
Joe Perches0e70e832009-09-21 17:04:20 -0700933
934 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
935 if (!$email_git_penguin_chiefs) {
936 @lines = grep(!/${penguin_chiefs}/i, @lines);
937 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700938
Joe Perches0e70e832009-09-21 17:04:20 -0700939 # cut -f2- -d":"
940 s/.*:\s*(.+)\s*/$1/ for (@lines);
941
Joe Perchesf5492662009-09-21 17:04:13 -0700942 $total_sign_offs += @lines;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700943
Joe Perches11ecf532009-09-21 17:04:22 -0700944 if ($email_remove_duplicates) {
945 @lines = mailmap(@lines);
946 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700947
948 $hash{$_}++ for @lines;
Joe Perchesf5492662009-09-21 17:04:13 -0700949 }
950
951 $count = 0;
952 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
953 my $sign_offs = $hash{$line};
Joe Perches3c7385b2009-12-14 18:00:46 -0800954 my $role;
955
Joe Perchesf5492662009-09-21 17:04:13 -0700956 $count++;
957 last if ($sign_offs < $email_git_min_signatures ||
958 $count > $email_git_max_maintainers ||
959 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
Joe Perches3c7385b2009-12-14 18:00:46 -0800960 push_email_address($line, '');
961 if ($from_filename) {
962 $role = "commits";
963 } else {
964 $role = "modified commits";
965 }
966 if ($output_rolestats) {
967 my $percent = sprintf("%.0f", $sign_offs * 100 / $total_sign_offs);
968 $role = "$role:$sign_offs/$total_sign_offs=$percent%";
969 }
970 add_role($line, $role);
Joe Perchesf5492662009-09-21 17:04:13 -0700971 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700972}
973
974sub uniq {
975 my @parms = @_;
976
977 my %saw;
978 @parms = grep(!$saw{$_}++, @parms);
979 return @parms;
980}
981
982sub sort_and_uniq {
983 my @parms = @_;
984
985 my %saw;
986 @parms = sort @parms;
987 @parms = grep(!$saw{$_}++, @parms);
988 return @parms;
989}
990
Joe Perches3c7385b2009-12-14 18:00:46 -0800991sub merge_email {
992 my @lines;
993 my %saw;
994
995 for (@_) {
996 my ($address, $role) = @$_;
997 if (!$saw{$address}) {
998 if ($output_roles) {
999 push @lines, "$address ($role)";
1000 } else {
1001 push @lines, $address;
1002 }
1003 $saw{$address} = 1;
1004 }
1005 }
1006
1007 return @lines;
1008}
1009
Joe Perchescb7301c2009-04-07 20:40:12 -07001010sub output {
1011 my @parms = @_;
1012
1013 if ($output_multiline) {
1014 foreach my $line (@parms) {
1015 print("${line}\n");
1016 }
1017 } else {
1018 print(join($output_separator, @parms));
1019 print("\n");
1020 }
1021}
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001022
1023my $rfc822re;
1024
1025sub make_rfc822re {
1026# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1027# comment. We must allow for rfc822_lwsp (or comments) after each of these.
1028# This regexp will only work on addresses which have had comments stripped
1029# and replaced with rfc822_lwsp.
1030
1031 my $specials = '()<>@,;:\\\\".\\[\\]';
1032 my $controls = '\\000-\\037\\177';
1033
1034 my $dtext = "[^\\[\\]\\r\\\\]";
1035 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1036
1037 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1038
1039# Use zero-width assertion to spot the limit of an atom. A simple
1040# $rfc822_lwsp* causes the regexp engine to hang occasionally.
1041 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1042 my $word = "(?:$atom|$quoted_string)";
1043 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1044
1045 my $sub_domain = "(?:$atom|$domain_literal)";
1046 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1047
1048 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1049
1050 my $phrase = "$word*";
1051 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1052 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1053 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1054
1055 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1056 my $address = "(?:$mailbox|$group)";
1057
1058 return "$rfc822_lwsp*$address";
1059}
1060
1061sub rfc822_strip_comments {
1062 my $s = shift;
1063# Recursively remove comments, and replace with a single space. The simpler
1064# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1065# chars in atoms, for example.
1066
1067 while ($s =~ s/^((?:[^"\\]|\\.)*
1068 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1069 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1070 return $s;
1071}
1072
1073# valid: returns true if the parameter is an RFC822 valid address
1074#
1075sub rfc822_valid ($) {
1076 my $s = rfc822_strip_comments(shift);
1077
1078 if (!$rfc822re) {
1079 $rfc822re = make_rfc822re();
1080 }
1081
1082 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1083}
1084
1085# validlist: In scalar context, returns true if the parameter is an RFC822
1086# valid list of addresses.
1087#
1088# In list context, returns an empty list on failure (an invalid
1089# address was found); otherwise a list whose first element is the
1090# number of addresses found and whose remaining elements are the
1091# addresses. This is needed to disambiguate failure (invalid)
1092# from success with no addresses found, because an empty string is
1093# a valid list.
1094
1095sub rfc822_validlist ($) {
1096 my $s = rfc822_strip_comments(shift);
1097
1098 if (!$rfc822re) {
1099 $rfc822re = make_rfc822re();
1100 }
1101 # * null list items are valid according to the RFC
1102 # * the '1' business is to aid in distinguishing failure from no results
1103
1104 my @r;
1105 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1106 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -07001107 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001108 push @r, $1;
1109 }
1110 return wantarray ? (scalar(@r), @r) : 1;
1111 }
1112 else {
1113 return wantarray ? () : 0;
1114 }
1115}