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