scripts/get_maintainer.pl: using --separator implies --nomultiline
[safe/jmp/linux-2.6] / scripts / get_maintainer.pl
1 #!/usr/bin/perl -w
2 # (c) 2007, Joe Perches <joe@perches.com>
3 #           created from checkpatch.pl
4 #
5 # Print selected MAINTAINERS information for
6 # the files modified in a patch or for a file
7 #
8 # usage: perl scripts/get_maintainers.pl [OPTIONS] <patch>
9 #        perl scripts/get_maintainers.pl [OPTIONS] -f <file>
10 #
11 # Licensed under the terms of the GNU GPL License version 2
12
13 use strict;
14
15 my $P = $0;
16 my $V = '0.20';
17
18 use Getopt::Long qw(:config no_auto_abbrev);
19
20 my $lk_path = "./";
21 my $email = 1;
22 my $email_usename = 1;
23 my $email_maintainer = 1;
24 my $email_list = 1;
25 my $email_subscriber_list = 0;
26 my $email_git = 1;
27 my $email_git_penguin_chiefs = 0;
28 my $email_git_min_signatures = 1;
29 my $email_git_max_maintainers = 5;
30 my $email_git_min_percent = 5;
31 my $email_git_since = "1-year-ago";
32 my $email_git_blame = 0;
33 my $output_multiline = 1;
34 my $output_separator = ", ";
35 my $scm = 0;
36 my $web = 0;
37 my $subsystem = 0;
38 my $status = 0;
39 my $from_filename = 0;
40 my $pattern_depth = 0;
41 my $version = 0;
42 my $help = 0;
43
44 my $exit = 0;
45
46 my @penguin_chief = ();
47 push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
48 #Andrew wants in on most everything - 2009/01/14
49 #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
50
51 my @penguin_chief_names = ();
52 foreach my $chief (@penguin_chief) {
53     if ($chief =~ m/^(.*):(.*)/) {
54         my $chief_name = $1;
55         my $chief_addr = $2;
56         push(@penguin_chief_names, $chief_name);
57     }
58 }
59 my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
60
61 # rfc822 email address - preloaded methods go here.
62 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
63 my $rfc822_char = '[\\000-\\377]';
64
65 if (!GetOptions(
66                 'email!' => \$email,
67                 'git!' => \$email_git,
68                 'git-chief-penguins!' => \$email_git_penguin_chiefs,
69                 'git-min-signatures=i' => \$email_git_min_signatures,
70                 'git-max-maintainers=i' => \$email_git_max_maintainers,
71                 'git-min-percent=i' => \$email_git_min_percent,
72                 'git-since=s' => \$email_git_since,
73                 'git-blame!' => \$email_git_blame,
74                 'm!' => \$email_maintainer,
75                 'n!' => \$email_usename,
76                 'l!' => \$email_list,
77                 's!' => \$email_subscriber_list,
78                 'multiline!' => \$output_multiline,
79                 'separator=s' => \$output_separator,
80                 'subsystem!' => \$subsystem,
81                 'status!' => \$status,
82                 'scm!' => \$scm,
83                 'web!' => \$web,
84                 'pattern-depth=i' => \$pattern_depth,
85                 'f|file' => \$from_filename,
86                 'v|version' => \$version,
87                 'h|help' => \$help,
88                 )) {
89     usage();
90     die "$P: invalid argument\n";
91 }
92
93 if ($help != 0) {
94     usage();
95     exit 0;
96 }
97
98 if ($version != 0) {
99     print("${P} ${V}\n");
100     exit 0;
101 }
102
103 if ($#ARGV < 0) {
104     usage();
105     die "$P: argument missing: patchfile or -f file please\n";
106 }
107
108 if ($output_separator ne ", ") {
109     $output_multiline = 0;
110 }
111
112 my $selections = $email + $scm + $status + $subsystem + $web;
113 if ($selections == 0) {
114     usage();
115     die "$P:  Missing required option: email, scm, status, subsystem or web\n";
116 }
117
118 if ($email &&
119     ($email_maintainer + $email_list + $email_subscriber_list +
120      $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
121     usage();
122     die "$P: Please select at least 1 email option\n";
123 }
124
125 if (!top_of_kernel_tree($lk_path)) {
126     die "$P: The current directory does not appear to be "
127         . "a linux kernel source tree.\n";
128 }
129
130 ## Read MAINTAINERS for type/value pairs
131
132 my @typevalue = ();
133 open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
134 while (<MAINT>) {
135     my $line = $_;
136
137     if ($line =~ m/^(\C):\s*(.*)/) {
138         my $type = $1;
139         my $value = $2;
140
141         ##Filename pattern matching
142         if ($type eq "F" || $type eq "X") {
143             $value =~ s@\.@\\\.@g;       ##Convert . to \.
144             $value =~ s/\*/\.\*/g;       ##Convert * to .*
145             $value =~ s/\?/\./g;         ##Convert ? to .
146             ##if pattern is a directory and it lacks a trailing slash, add one
147             if ((-d $value)) {
148                 $value =~ s@([^/])$@$1/@;
149             }
150         }
151         push(@typevalue, "$type:$value");
152     } elsif (!/^(\s)*$/) {
153         $line =~ s/\n$//g;
154         push(@typevalue, $line);
155     }
156 }
157 close(MAINT);
158
159 my %mailmap;
160
161 open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
162 while (<MAILMAP>) {
163     my $line = $_;
164
165     next if ($line =~ m/^\s*#/);
166     next if ($line =~ m/^\s*$/);
167
168     my ($name, $address) = parse_email($line);
169     $line = format_email($name, $address);
170
171     next if ($line =~ m/^\s*$/);
172
173     if (exists($mailmap{$name})) {
174         my $obj = $mailmap{$name};
175         push(@$obj, $address);
176     } else {
177         my @arr = ($address);
178         $mailmap{$name} = \@arr;
179     }
180 }
181 close(MAILMAP);
182
183 foreach my $name (sort {$mailmap{$a} <=> $mailmap{$b}} keys %mailmap) {
184     my $obj = $mailmap{$name};
185     foreach my $address (@$obj) {
186     }
187 }
188
189 ## use the filenames on the command line or find the filenames in the patchfiles
190
191 my @files = ();
192 my @range = ();
193
194 foreach my $file (@ARGV) {
195     ##if $file is a directory and it lacks a trailing slash, add one
196     if ((-d $file)) {
197         $file =~ s@([^/])$@$1/@;
198     } elsif (!(-f $file)) {
199         die "$P: file '${file}' not found\n";
200     }
201     if ($from_filename) {
202         push(@files, $file);
203     } else {
204         my $file_cnt = @files;
205         my $lastfile;
206         open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
207         while (<PATCH>) {
208             if (m/^\+\+\+\s+(\S+)/) {
209                 my $filename = $1;
210                 $filename =~ s@^[^/]*/@@;
211                 $filename =~ s@\n@@;
212                 $lastfile = $filename;
213                 push(@files, $filename);
214             } elsif (m/^\@\@ -(\d+),(\d+)/) {
215                 if ($email_git_blame) {
216                     push(@range, "$lastfile:$1:$2");
217                 }
218             }
219         }
220         close(PATCH);
221         if ($file_cnt == @files) {
222             warn "$P: file '${file}' doesn't appear to be a patch.  "
223                 . "Add -f to options?\n";
224         }
225         @files = sort_and_uniq(@files);
226     }
227 }
228
229 my @email_to = ();
230 my @list_to = ();
231 my @scm = ();
232 my @web = ();
233 my @subsystem = ();
234 my @status = ();
235
236 # Find responsible parties
237
238 foreach my $file (@files) {
239
240 #Do not match excluded file patterns
241
242     my $exclude = 0;
243     foreach my $line (@typevalue) {
244         if ($line =~ m/^(\C):\s*(.*)/) {
245             my $type = $1;
246             my $value = $2;
247             if ($type eq 'X') {
248                 if (file_match_pattern($file, $value)) {
249                     $exclude = 1;
250                     last;
251                 }
252             }
253         }
254     }
255
256     if (!$exclude) {
257         my $tvi = 0;
258         my %hash;
259         foreach my $line (@typevalue) {
260             if ($line =~ m/^(\C):\s*(.*)/) {
261                 my $type = $1;
262                 my $value = $2;
263                 if ($type eq 'F') {
264                     if (file_match_pattern($file, $value)) {
265                         my $value_pd = ($value =~ tr@/@@);
266                         my $file_pd = ($file  =~ tr@/@@);
267                         $value_pd++ if (substr($value,-1,1) ne "/");
268                         if ($pattern_depth == 0 ||
269                             (($file_pd - $value_pd) < $pattern_depth)) {
270                             $hash{$tvi} = $value_pd;
271                         }
272                     }
273                 }
274             }
275             $tvi++;
276         }
277         foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
278             add_categories($line);
279         }
280     }
281
282     if ($email && $email_git) {
283         recent_git_signoffs($file);
284     }
285
286     if ($email && $email_git_blame) {
287         git_assign_blame($file);
288     }
289 }
290
291 if ($email) {
292     foreach my $chief (@penguin_chief) {
293         if ($chief =~ m/^(.*):(.*)/) {
294             my $email_address;
295
296             $email_address = format_email($1, $2);
297             if ($email_git_penguin_chiefs) {
298                 push(@email_to, $email_address);
299             } else {
300                 @email_to = grep(!/${email_address}/, @email_to);
301             }
302         }
303     }
304 }
305
306 if ($email || $email_list) {
307     my @to = ();
308     if ($email) {
309         @to = (@to, @email_to);
310     }
311     if ($email_list) {
312         @to = (@to, @list_to);
313     }
314     output(uniq(@to));
315 }
316
317 if ($scm) {
318     @scm = sort_and_uniq(@scm);
319     output(@scm);
320 }
321
322 if ($status) {
323     @status = sort_and_uniq(@status);
324     output(@status);
325 }
326
327 if ($subsystem) {
328     @subsystem = sort_and_uniq(@subsystem);
329     output(@subsystem);
330 }
331
332 if ($web) {
333     @web = sort_and_uniq(@web);
334     output(@web);
335 }
336
337 exit($exit);
338
339 sub file_match_pattern {
340     my ($file, $pattern) = @_;
341     if (substr($pattern, -1) eq "/") {
342         if ($file =~ m@^$pattern@) {
343             return 1;
344         }
345     } else {
346         if ($file =~ m@^$pattern@) {
347             my $s1 = ($file =~ tr@/@@);
348             my $s2 = ($pattern =~ tr@/@@);
349             if ($s1 == $s2) {
350                 return 1;
351             }
352         }
353     }
354     return 0;
355 }
356
357 sub usage {
358     print <<EOT;
359 usage: $P [options] patchfile
360        $P [options] -f file|directory
361 version: $V
362
363 MAINTAINER field selection options:
364   --email => print email address(es) if any
365     --git => include recent git \*-by: signers
366     --git-chief-penguins => include ${penguin_chiefs}
367     --git-min-signatures => number of signatures required (default: 1)
368     --git-max-maintainers => maximum maintainers to add (default: 5)
369     --git-min-percent => minimum percentage of commits required (default: 5)
370     --git-since => git history to use (default: 1-year-ago)
371     --git-blame => use git blame to find modified commits for patch or file
372     --m => include maintainer(s) if any
373     --n => include name 'Full Name <addr\@domain.tld>'
374     --l => include list(s) if any
375     --s => include subscriber only list(s) if any
376   --scm => print SCM tree(s) if any
377   --status => print status if any
378   --subsystem => print subsystem name if any
379   --web => print website(s) if any
380
381 Output type options:
382   --separator [, ] => separator for multiple entries on 1 line
383     using --separator also sets --nomultiline if --separator is not [, ]
384   --multiline => print 1 entry per line
385
386 Other options:
387   --pattern-depth => Number of pattern directory traversals (default: 0 (all))
388   --version => show version
389   --help => show this help information
390
391 Default options:
392   [--email --git --m --n --l --multiline --pattern-depth=0]
393
394 Notes:
395   Using "-f directory" may give unexpected results:
396       Used with "--git", git signators for _all_ files in and below
397           directory are examined as git recurses directories.
398           Any specified X: (exclude) pattern matches are _not_ ignored.
399       Used with "--nogit", directory is used as a pattern match,
400          no individual file within the directory or subdirectory
401          is matched.
402       Used with "--git-blame", does not iterate all files in directory
403   Using "--git-blame" is slow and may add old committers and authors
404       that are no longer active maintainers to the output.
405 EOT
406 }
407
408 sub top_of_kernel_tree {
409         my ($lk_path) = @_;
410
411         if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
412             $lk_path .= "/";
413         }
414         if (   (-f "${lk_path}COPYING")
415             && (-f "${lk_path}CREDITS")
416             && (-f "${lk_path}Kbuild")
417             && (-f "${lk_path}MAINTAINERS")
418             && (-f "${lk_path}Makefile")
419             && (-f "${lk_path}README")
420             && (-d "${lk_path}Documentation")
421             && (-d "${lk_path}arch")
422             && (-d "${lk_path}include")
423             && (-d "${lk_path}drivers")
424             && (-d "${lk_path}fs")
425             && (-d "${lk_path}init")
426             && (-d "${lk_path}ipc")
427             && (-d "${lk_path}kernel")
428             && (-d "${lk_path}lib")
429             && (-d "${lk_path}scripts")) {
430                 return 1;
431         }
432         return 0;
433 }
434
435 sub parse_email {
436     my ($formatted_email) = @_;
437
438     my $name = "";
439     my $address = "";
440
441     if ($formatted_email =~ /^([^<]+)<(.*\@.*)>.*$/) {
442         $name = $1;
443         $address = $2;
444     } elsif ($formatted_email =~ /^\s*<(.*\@.*)>.*$/) {
445         $address = $1;
446     } elsif ($formatted_email =~ /^\s*(.*\@.*)$/) {
447         $address = $1;
448     }
449
450     $name =~ s/^\s+|\s+$//g;
451     $name =~ s/^\"|\"$//g;
452     $address =~ s/^\s+|\s+$//g;
453
454     if ($name =~ /[^a-z0-9 \.\-]/i) {    ##has "must quote" chars
455         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
456         $name = "\"$name\"";
457     }
458
459     return ($name, $address);
460 }
461
462 sub format_email {
463     my ($name, $address) = @_;
464
465     my $formatted_email;
466
467     $name =~ s/^\s+|\s+$//g;
468     $name =~ s/^\"|\"$//g;
469     $address =~ s/^\s+|\s+$//g;
470
471     if ($name =~ /[^a-z0-9 \.\-]/i) {    ##has "must quote" chars
472         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
473         $name = "\"$name\"";
474     }
475
476     if ($email_usename) {
477         if ("$name" eq "") {
478             $formatted_email = "$address";
479         } else {
480             $formatted_email = "$name <${address}>";
481         }
482     } else {
483         $formatted_email = $address;
484     }
485
486     return $formatted_email;
487 }
488
489 sub add_categories {
490     my ($index) = @_;
491
492     $index = $index - 1;
493     while ($index >= 0) {
494         my $tv = $typevalue[$index];
495         if ($tv =~ m/^(\C):\s*(.*)/) {
496             my $ptype = $1;
497             my $pvalue = $2;
498             if ($ptype eq "L") {
499                 my $list_address = $pvalue;
500                 my $list_additional = "";
501                 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
502                     $list_address = $1;
503                     $list_additional = $2;
504                 }
505                 if ($list_additional =~ m/subscribers-only/) {
506                     if ($email_subscriber_list) {
507                         push(@list_to, $list_address);
508                     }
509                 } else {
510                     if ($email_list) {
511                         push(@list_to, $list_address);
512                     }
513                 }
514             } elsif ($ptype eq "M") {
515                 my ($name, $address) = parse_email($pvalue);
516                 if ($name eq "") {
517                     if ($index >= 0) {
518                         my $tv = $typevalue[$index - 1];
519                         if ($tv =~ m/^(\C):\s*(.*)/) {
520                             if ($1 eq "P") {
521                                 $name = $2;
522                             }
523                         }
524                     }
525                 }
526                 if ($email_maintainer) {
527                     push_email_addresses($pvalue);
528                 }
529             } elsif ($ptype eq "T") {
530                 push(@scm, $pvalue);
531             } elsif ($ptype eq "W") {
532                 push(@web, $pvalue);
533             } elsif ($ptype eq "S") {
534                 push(@status, $pvalue);
535             }
536
537             $index--;
538         } else {
539             push(@subsystem,$tv);
540             $index = -1;
541         }
542     }
543 }
544
545 sub email_address_inuse {
546     my ($test_address) = @_;
547
548     foreach my $line (@email_to) {
549         my ($name, $address) = parse_email($line);
550
551         return 1 if ($address eq $test_address);
552     }
553     return 0;
554 }
555
556 sub push_email_address {
557     my ($line) = @_;
558
559     my ($name, $address) = parse_email($line);
560
561     if (!email_address_inuse($address)) {
562         push(@email_to, format_email($name, $address));
563     }
564 }
565
566 sub push_email_addresses {
567     my ($address) = @_;
568
569     my @address_list = ();
570
571     if (rfc822_valid($address)) {
572         push_email_address($address);
573     } elsif (@address_list = rfc822_validlist($address)) {
574         my $array_count = shift(@address_list);
575         while (my $entry = shift(@address_list)) {
576             push_email_address($entry);
577         }
578     } else {
579         warn("Invalid MAINTAINERS address: '" . $address . "'\n");
580     }
581 }
582
583 sub which {
584     my ($bin) = @_;
585
586     foreach my $path (split(/:/, $ENV{PATH})) {
587         if (-e "$path/$bin") {
588             return "$path/$bin";
589         }
590     }
591
592     return "";
593 }
594
595 sub mailmap {
596     my @lines = @_;
597     my %hash;
598
599     foreach my $line (@lines) {
600         my ($name, $address) = parse_email($line);
601         if (!exists($hash{$name})) {
602             $hash{$name} = $address;
603         }
604         if (exists($mailmap{$name})) {
605             my $obj = $mailmap{$name};
606             foreach my $map_address (@$obj) {
607                 if (($map_address eq $address) &&
608                     ($map_address ne $hash{$name})) {
609                     $line = format_email($name, $hash{$name});
610                 }
611             }
612         }
613     }
614
615     return @lines;
616 }
617
618 sub recent_git_signoffs {
619     my ($file) = @_;
620
621     my $sign_offs = "";
622     my $cmd = "";
623     my $output = "";
624     my $count = 0;
625     my @lines = ();
626     my %hash;
627     my $total_sign_offs;
628
629     if (which("git") eq "") {
630         warn("$P: git not found.  Add --nogit to options?\n");
631         return;
632     }
633     if (!(-d ".git")) {
634         warn("$P: .git directory not found.  Use a git repository for better results.\n");
635         warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
636         return;
637     }
638
639     $cmd = "git log --since=${email_git_since} -- ${file}";
640
641     $output = `${cmd}`;
642     $output =~ s/^\s*//gm;
643
644     @lines = split("\n", $output);
645
646     @lines = grep(/^[-_         a-z]+by:.*\@.*$/i, @lines);
647     if (!$email_git_penguin_chiefs) {
648         @lines = grep(!/${penguin_chiefs}/i, @lines);
649     }
650     # cut -f2- -d":"
651     s/.*:\s*(.+)\s*/$1/ for (@lines);
652
653     $total_sign_offs = @lines;
654
655     @lines = mailmap(@lines);
656
657     @lines = sort(@lines);
658     # uniq -c
659     foreach my $line (@lines) {
660         $hash{$line}++;
661     }
662     # sort -rn
663     @lines = ();
664     foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
665         push(@lines,"$hash{$line}       $line");
666     }
667
668     foreach my $line (@lines) {
669         if ($line =~ m/([0-9]+)\s+(.*)/) {
670             my $sign_offs = $1;
671             $line = $2;
672             $count++;
673             if ($sign_offs < $email_git_min_signatures ||
674                 $count > $email_git_max_maintainers ||
675                 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
676                 last;
677             }
678             push_email_address($line);
679         }
680     }
681 }
682
683 sub save_commits {
684     my ($cmd, @commits) = @_;
685     my $output;
686     my @lines = ();
687
688     $output = `${cmd}`;
689
690     @lines = split("\n", $output);
691     foreach my $line (@lines) {
692         if ($line =~ m/^(\w+) /) {
693             push (@commits, $1);
694         }
695     }
696     return @commits;
697 }
698
699 sub git_assign_blame {
700     my ($file) = @_;
701
702     my @lines = ();
703     my @commits = ();
704     my $cmd;
705     my $output;
706     my %hash;
707     my $total_sign_offs;
708     my $count;
709
710     if (@range) {
711         foreach my $file_range_diff (@range) {
712             next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
713             my $diff_file = $1;
714             my $diff_start = $2;
715             my $diff_length = $3;
716             next if (!("$file" eq "$diff_file"));
717             $cmd = "git blame -l -L $diff_start,+$diff_length $file";
718             @commits = save_commits($cmd, @commits);
719         }
720     } else {
721         if (-f $file) {
722             $cmd = "git blame -l $file";
723             @commits = save_commits($cmd, @commits);
724         }
725     }
726
727     $total_sign_offs = 0;
728     @commits = uniq(@commits);
729     foreach my $commit (@commits) {
730         $cmd = "git log -1 ${commit}";
731
732         $output = `${cmd}`;
733         $output =~ s/^\s*//gm;
734         @lines = split("\n", $output);
735
736         @lines = grep(/^[-_     a-z]+by:.*\@.*$/i, @lines);
737         if (!$email_git_penguin_chiefs) {
738             @lines = grep(!/${penguin_chiefs}/i, @lines);
739         }
740
741         # cut -f2- -d":"
742         s/.*:\s*(.+)\s*/$1/ for (@lines);
743
744         $total_sign_offs += @lines;
745
746         @lines = mailmap(@lines);
747
748         $hash{$_}++ for @lines;
749     }
750
751     $count = 0;
752     foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
753         my $sign_offs = $hash{$line};
754         $count++;
755         last if ($sign_offs < $email_git_min_signatures ||
756                  $count > $email_git_max_maintainers ||
757                  $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
758         push_email_address($line);
759     }
760 }
761
762 sub uniq {
763     my @parms = @_;
764
765     my %saw;
766     @parms = grep(!$saw{$_}++, @parms);
767     return @parms;
768 }
769
770 sub sort_and_uniq {
771     my @parms = @_;
772
773     my %saw;
774     @parms = sort @parms;
775     @parms = grep(!$saw{$_}++, @parms);
776     return @parms;
777 }
778
779 sub output {
780     my @parms = @_;
781
782     if ($output_multiline) {
783         foreach my $line (@parms) {
784             print("${line}\n");
785         }
786     } else {
787         print(join($output_separator, @parms));
788         print("\n");
789     }
790 }
791
792 my $rfc822re;
793
794 sub make_rfc822re {
795 #   Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
796 #   comment.  We must allow for rfc822_lwsp (or comments) after each of these.
797 #   This regexp will only work on addresses which have had comments stripped
798 #   and replaced with rfc822_lwsp.
799
800     my $specials = '()<>@,;:\\\\".\\[\\]';
801     my $controls = '\\000-\\037\\177';
802
803     my $dtext = "[^\\[\\]\\r\\\\]";
804     my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
805
806     my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
807
808 #   Use zero-width assertion to spot the limit of an atom.  A simple
809 #   $rfc822_lwsp* causes the regexp engine to hang occasionally.
810     my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
811     my $word = "(?:$atom|$quoted_string)";
812     my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
813
814     my $sub_domain = "(?:$atom|$domain_literal)";
815     my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
816
817     my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
818
819     my $phrase = "$word*";
820     my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
821     my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
822     my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
823
824     my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
825     my $address = "(?:$mailbox|$group)";
826
827     return "$rfc822_lwsp*$address";
828 }
829
830 sub rfc822_strip_comments {
831     my $s = shift;
832 #   Recursively remove comments, and replace with a single space.  The simpler
833 #   regexps in the Email Addressing FAQ are imperfect - they will miss escaped
834 #   chars in atoms, for example.
835
836     while ($s =~ s/^((?:[^"\\]|\\.)*
837                     (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
838                     \((?:[^()\\]|\\.)*\)/$1 /osx) {}
839     return $s;
840 }
841
842 #   valid: returns true if the parameter is an RFC822 valid address
843 #
844 sub rfc822_valid ($) {
845     my $s = rfc822_strip_comments(shift);
846
847     if (!$rfc822re) {
848         $rfc822re = make_rfc822re();
849     }
850
851     return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
852 }
853
854 #   validlist: In scalar context, returns true if the parameter is an RFC822
855 #              valid list of addresses.
856 #
857 #              In list context, returns an empty list on failure (an invalid
858 #              address was found); otherwise a list whose first element is the
859 #              number of addresses found and whose remaining elements are the
860 #              addresses.  This is needed to disambiguate failure (invalid)
861 #              from success with no addresses found, because an empty string is
862 #              a valid list.
863
864 sub rfc822_validlist ($) {
865     my $s = rfc822_strip_comments(shift);
866
867     if (!$rfc822re) {
868         $rfc822re = make_rfc822re();
869     }
870     # * null list items are valid according to the RFC
871     # * the '1' business is to aid in distinguishing failure from no results
872
873     my @r;
874     if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
875         $s =~ m/^$rfc822_char*$/) {
876         while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
877             push @r, $1;
878         }
879         return wantarray ? (scalar(@r), @r) : 1;
880     }
881     else {
882         return wantarray ? () : 0;
883     }
884 }