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