get_maintainer.pl 20 KB

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