get_maintainer.pl 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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_maintainer.pl [OPTIONS] <patch>
  9. # perl scripts/get_maintainer.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.25';
  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_penguin_chiefs = 0;
  23. my $email_git = 0;
  24. my $email_git_all_signature_types = 0;
  25. my $email_git_blame = 0;
  26. my $email_git_fallback = 1;
  27. my $email_git_min_signatures = 1;
  28. my $email_git_max_maintainers = 5;
  29. my $email_git_min_percent = 5;
  30. my $email_git_since = "1-year-ago";
  31. my $email_hg_since = "-365";
  32. my $email_remove_duplicates = 1;
  33. my $output_multiline = 1;
  34. my $output_separator = ", ";
  35. my $output_roles = 0;
  36. my $output_rolestats = 0;
  37. my $scm = 0;
  38. my $web = 0;
  39. my $subsystem = 0;
  40. my $status = 0;
  41. my $keywords = 1;
  42. my $sections = 0;
  43. my $file_emails = 0;
  44. my $from_filename = 0;
  45. my $pattern_depth = 0;
  46. my $version = 0;
  47. my $help = 0;
  48. my $exit = 0;
  49. my @penguin_chief = ();
  50. push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
  51. #Andrew wants in on most everything - 2009/01/14
  52. #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
  53. my @penguin_chief_names = ();
  54. foreach my $chief (@penguin_chief) {
  55. if ($chief =~ m/^(.*):(.*)/) {
  56. my $chief_name = $1;
  57. my $chief_addr = $2;
  58. push(@penguin_chief_names, $chief_name);
  59. }
  60. }
  61. my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
  62. # Signature types of people who are either
  63. # a) responsible for the code in question, or
  64. # b) familiar enough with it to give relevant feedback
  65. my @signature_tags = ();
  66. push(@signature_tags, "Signed-off-by:");
  67. push(@signature_tags, "Reviewed-by:");
  68. push(@signature_tags, "Acked-by:");
  69. my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
  70. # rfc822 email address - preloaded methods go here.
  71. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  72. my $rfc822_char = '[\\000-\\377]';
  73. # VCS command support: class-like functions and strings
  74. my %VCS_cmds;
  75. my %VCS_cmds_git = (
  76. "execute_cmd" => \&git_execute_cmd,
  77. "available" => '(which("git") ne "") && (-d ".git")',
  78. "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
  79. "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
  80. "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
  81. "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
  82. "blame_file_cmd" => "git blame -l \$file",
  83. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  84. "blame_commit_pattern" => "^([0-9a-f]+) "
  85. );
  86. my %VCS_cmds_hg = (
  87. "execute_cmd" => \&hg_execute_cmd,
  88. "available" => '(which("hg") ne "") && (-d ".hg")',
  89. "find_signers_cmd" =>
  90. "hg log --date=\$email_hg_since" .
  91. " --template='commit {node}\\n{desc}\\n' -- \$file",
  92. "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
  93. "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
  94. "blame_range_cmd" => "", # not supported
  95. "blame_file_cmd" => "hg blame -c \$file",
  96. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  97. "blame_commit_pattern" => "^([0-9a-f]+):"
  98. );
  99. if (-f "${lk_path}.get_maintainer.conf") {
  100. my @conf_args;
  101. open(my $conffile, '<', "${lk_path}.get_maintainer.conf")
  102. or warn "$P: Can't open .get_maintainer.conf: $!\n";
  103. while (<$conffile>) {
  104. my $line = $_;
  105. $line =~ s/\s*\n?$//g;
  106. $line =~ s/^\s*//g;
  107. $line =~ s/\s+/ /g;
  108. next if ($line =~ m/^\s*#/);
  109. next if ($line =~ m/^\s*$/);
  110. my @words = split(" ", $line);
  111. foreach my $word (@words) {
  112. last if ($word =~ m/^#/);
  113. push (@conf_args, $word);
  114. }
  115. }
  116. close($conffile);
  117. unshift(@ARGV, @conf_args) if @conf_args;
  118. }
  119. if (!GetOptions(
  120. 'email!' => \$email,
  121. 'git!' => \$email_git,
  122. 'git-all-signature-types!' => \$email_git_all_signature_types,
  123. 'git-blame!' => \$email_git_blame,
  124. 'git-fallback!' => \$email_git_fallback,
  125. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  126. 'git-min-signatures=i' => \$email_git_min_signatures,
  127. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  128. 'git-min-percent=i' => \$email_git_min_percent,
  129. 'git-since=s' => \$email_git_since,
  130. 'hg-since=s' => \$email_hg_since,
  131. 'remove-duplicates!' => \$email_remove_duplicates,
  132. 'm!' => \$email_maintainer,
  133. 'n!' => \$email_usename,
  134. 'l!' => \$email_list,
  135. 's!' => \$email_subscriber_list,
  136. 'multiline!' => \$output_multiline,
  137. 'roles!' => \$output_roles,
  138. 'rolestats!' => \$output_rolestats,
  139. 'separator=s' => \$output_separator,
  140. 'subsystem!' => \$subsystem,
  141. 'status!' => \$status,
  142. 'scm!' => \$scm,
  143. 'web!' => \$web,
  144. 'pattern-depth=i' => \$pattern_depth,
  145. 'k|keywords!' => \$keywords,
  146. 'sections!' => \$sections,
  147. 'fe|file-emails!' => \$file_emails,
  148. 'f|file' => \$from_filename,
  149. 'v|version' => \$version,
  150. 'h|help|usage' => \$help,
  151. )) {
  152. die "$P: invalid argument - use --help if necessary\n";
  153. }
  154. if ($help != 0) {
  155. usage();
  156. exit 0;
  157. }
  158. if ($version != 0) {
  159. print("${P} ${V}\n");
  160. exit 0;
  161. }
  162. if (-t STDIN && !@ARGV) {
  163. # We're talking to a terminal, but have no command line arguments.
  164. die "$P: missing patchfile or -f file - use --help if necessary\n";
  165. }
  166. if ($output_separator ne ", ") {
  167. $output_multiline = 0;
  168. }
  169. if ($output_rolestats) {
  170. $output_roles = 1;
  171. }
  172. if ($sections) {
  173. $email = 0;
  174. $email_list = 0;
  175. $scm = 0;
  176. $status = 0;
  177. $subsystem = 0;
  178. $web = 0;
  179. $keywords = 0;
  180. } else {
  181. my $selections = $email + $scm + $status + $subsystem + $web;
  182. if ($selections == 0) {
  183. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  184. }
  185. }
  186. if ($email &&
  187. ($email_maintainer + $email_list + $email_subscriber_list +
  188. $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
  189. die "$P: Please select at least 1 email option\n";
  190. }
  191. if (!top_of_kernel_tree($lk_path)) {
  192. die "$P: The current directory does not appear to be "
  193. . "a linux kernel source tree.\n";
  194. }
  195. if ($email_git_all_signature_types) {
  196. $signaturePattern = "(.+?)[Bb][Yy]:";
  197. }
  198. ## Read MAINTAINERS for type/value pairs
  199. my @typevalue = ();
  200. my %keyword_hash;
  201. open (my $maint, '<', "${lk_path}MAINTAINERS")
  202. or die "$P: Can't open MAINTAINERS: $!\n";
  203. while (<$maint>) {
  204. my $line = $_;
  205. if ($line =~ m/^(\C):\s*(.*)/) {
  206. my $type = $1;
  207. my $value = $2;
  208. ##Filename pattern matching
  209. if ($type eq "F" || $type eq "X") {
  210. $value =~ s@\.@\\\.@g; ##Convert . to \.
  211. $value =~ s/\*/\.\*/g; ##Convert * to .*
  212. $value =~ s/\?/\./g; ##Convert ? to .
  213. ##if pattern is a directory and it lacks a trailing slash, add one
  214. if ((-d $value)) {
  215. $value =~ s@([^/])$@$1/@;
  216. }
  217. } elsif ($type eq "K") {
  218. $keyword_hash{@typevalue} = $value;
  219. }
  220. push(@typevalue, "$type:$value");
  221. } elsif (!/^(\s)*$/) {
  222. $line =~ s/\n$//g;
  223. push(@typevalue, $line);
  224. }
  225. }
  226. close($maint);
  227. my %mailmap;
  228. if ($email_remove_duplicates) {
  229. open(my $mailmap, '<', "${lk_path}.mailmap")
  230. or warn "$P: Can't open .mailmap: $!\n";
  231. while (<$mailmap>) {
  232. my $line = $_;
  233. next if ($line =~ m/^\s*#/);
  234. next if ($line =~ m/^\s*$/);
  235. my ($name, $address) = parse_email($line);
  236. $line = format_email($name, $address, $email_usename);
  237. next if ($line =~ m/^\s*$/);
  238. if (exists($mailmap{$name})) {
  239. my $obj = $mailmap{$name};
  240. push(@$obj, $address);
  241. } else {
  242. my @arr = ($address);
  243. $mailmap{$name} = \@arr;
  244. }
  245. }
  246. close($mailmap);
  247. }
  248. ## use the filenames on the command line or find the filenames in the patchfiles
  249. my @files = ();
  250. my @range = ();
  251. my @keyword_tvi = ();
  252. my @file_emails = ();
  253. if (!@ARGV) {
  254. push(@ARGV, "&STDIN");
  255. }
  256. foreach my $file (@ARGV) {
  257. if ($file ne "&STDIN") {
  258. ##if $file is a directory and it lacks a trailing slash, add one
  259. if ((-d $file)) {
  260. $file =~ s@([^/])$@$1/@;
  261. } elsif (!(-f $file)) {
  262. die "$P: file '${file}' not found\n";
  263. }
  264. }
  265. if ($from_filename) {
  266. push(@files, $file);
  267. if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
  268. open(my $f, '<', $file)
  269. or die "$P: Can't open $file: $!\n";
  270. my $text = do { local($/) ; <$f> };
  271. close($f);
  272. if ($keywords) {
  273. foreach my $line (keys %keyword_hash) {
  274. if ($text =~ m/$keyword_hash{$line}/x) {
  275. push(@keyword_tvi, $line);
  276. }
  277. }
  278. }
  279. if ($file_emails) {
  280. my @poss_addr = $text =~ m$[A-Za-zÀ-ÿ\"\' \,\.\+-]*\s*[\,]*\s*[\(\<\{]{0,1}[A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+\.[A-Za-z0-9]+[\)\>\}]{0,1}$g;
  281. push(@file_emails, clean_file_emails(@poss_addr));
  282. }
  283. }
  284. } else {
  285. my $file_cnt = @files;
  286. my $lastfile;
  287. open(my $patch, "< $file")
  288. or die "$P: Can't open $file: $!\n";
  289. while (<$patch>) {
  290. my $patch_line = $_;
  291. if (m/^\+\+\+\s+(\S+)/) {
  292. my $filename = $1;
  293. $filename =~ s@^[^/]*/@@;
  294. $filename =~ s@\n@@;
  295. $lastfile = $filename;
  296. push(@files, $filename);
  297. } elsif (m/^\@\@ -(\d+),(\d+)/) {
  298. if ($email_git_blame) {
  299. push(@range, "$lastfile:$1:$2");
  300. }
  301. } elsif ($keywords) {
  302. foreach my $line (keys %keyword_hash) {
  303. if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
  304. push(@keyword_tvi, $line);
  305. }
  306. }
  307. }
  308. }
  309. close($patch);
  310. if ($file_cnt == @files) {
  311. warn "$P: file '${file}' doesn't appear to be a patch. "
  312. . "Add -f to options?\n";
  313. }
  314. @files = sort_and_uniq(@files);
  315. }
  316. }
  317. @file_emails = uniq(@file_emails);
  318. my @email_to = ();
  319. my @list_to = ();
  320. my @scm = ();
  321. my @web = ();
  322. my @subsystem = ();
  323. my @status = ();
  324. # Find responsible parties
  325. foreach my $file (@files) {
  326. my %hash;
  327. my $exact_pattern_match = 0;
  328. my $tvi = find_first_section();
  329. while ($tvi < @typevalue) {
  330. my $start = find_starting_index($tvi);
  331. my $end = find_ending_index($tvi);
  332. my $exclude = 0;
  333. my $i;
  334. #Do not match excluded file patterns
  335. for ($i = $start; $i < $end; $i++) {
  336. my $line = $typevalue[$i];
  337. if ($line =~ m/^(\C):\s*(.*)/) {
  338. my $type = $1;
  339. my $value = $2;
  340. if ($type eq 'X') {
  341. if (file_match_pattern($file, $value)) {
  342. $exclude = 1;
  343. last;
  344. }
  345. }
  346. }
  347. }
  348. if (!$exclude) {
  349. for ($i = $start; $i < $end; $i++) {
  350. my $line = $typevalue[$i];
  351. if ($line =~ m/^(\C):\s*(.*)/) {
  352. my $type = $1;
  353. my $value = $2;
  354. if ($type eq 'F') {
  355. if (file_match_pattern($file, $value)) {
  356. my $value_pd = ($value =~ tr@/@@);
  357. my $file_pd = ($file =~ tr@/@@);
  358. $value_pd++ if (substr($value,-1,1) ne "/");
  359. $value_pd = -1 if ($value =~ /^\.\*/);
  360. $exact_pattern_match = 1 if ($value_pd >= $file_pd);
  361. if ($pattern_depth == 0 ||
  362. (($file_pd - $value_pd) < $pattern_depth)) {
  363. $hash{$tvi} = $value_pd;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. $tvi = $end + 1;
  371. }
  372. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  373. add_categories($line);
  374. if ($sections) {
  375. my $i;
  376. my $start = find_starting_index($line);
  377. my $end = find_ending_index($line);
  378. for ($i = $start; $i < $end; $i++) {
  379. my $line = $typevalue[$i];
  380. if ($line =~ /^[FX]:/) { ##Restore file patterns
  381. $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
  382. $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
  383. $line =~ s/\\\./\./g; ##Convert \. to .
  384. $line =~ s/\.\*/\*/g; ##Convert .* to *
  385. }
  386. $line =~ s/^([A-Z]):/$1:\t/g;
  387. print("$line\n");
  388. }
  389. print("\n");
  390. }
  391. }
  392. if ($email &&
  393. ($email_git || ($email_git_fallback && !$exact_pattern_match))) {
  394. vcs_file_signoffs($file);
  395. }
  396. if ($email && $email_git_blame) {
  397. vcs_file_blame($file);
  398. }
  399. }
  400. if ($keywords) {
  401. @keyword_tvi = sort_and_uniq(@keyword_tvi);
  402. foreach my $line (@keyword_tvi) {
  403. add_categories($line);
  404. }
  405. }
  406. if ($email) {
  407. foreach my $chief (@penguin_chief) {
  408. if ($chief =~ m/^(.*):(.*)/) {
  409. my $email_address;
  410. $email_address = format_email($1, $2, $email_usename);
  411. if ($email_git_penguin_chiefs) {
  412. push(@email_to, [$email_address, 'chief penguin']);
  413. } else {
  414. @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
  415. }
  416. }
  417. }
  418. foreach my $email (@file_emails) {
  419. my ($name, $address) = parse_email($email);
  420. my $tmp_email = format_email($name, $address, $email_usename);
  421. push_email_address($tmp_email, '');
  422. add_role($tmp_email, 'in file');
  423. }
  424. }
  425. if ($email || $email_list) {
  426. my @to = ();
  427. if ($email) {
  428. @to = (@to, @email_to);
  429. }
  430. if ($email_list) {
  431. @to = (@to, @list_to);
  432. }
  433. output(merge_email(@to));
  434. }
  435. if ($scm) {
  436. @scm = uniq(@scm);
  437. output(@scm);
  438. }
  439. if ($status) {
  440. @status = uniq(@status);
  441. output(@status);
  442. }
  443. if ($subsystem) {
  444. @subsystem = uniq(@subsystem);
  445. output(@subsystem);
  446. }
  447. if ($web) {
  448. @web = uniq(@web);
  449. output(@web);
  450. }
  451. exit($exit);
  452. sub file_match_pattern {
  453. my ($file, $pattern) = @_;
  454. if (substr($pattern, -1) eq "/") {
  455. if ($file =~ m@^$pattern@) {
  456. return 1;
  457. }
  458. } else {
  459. if ($file =~ m@^$pattern@) {
  460. my $s1 = ($file =~ tr@/@@);
  461. my $s2 = ($pattern =~ tr@/@@);
  462. if ($s1 == $s2) {
  463. return 1;
  464. }
  465. }
  466. }
  467. return 0;
  468. }
  469. sub usage {
  470. print <<EOT;
  471. usage: $P [options] patchfile
  472. $P [options] -f file|directory
  473. version: $V
  474. MAINTAINER field selection options:
  475. --email => print email address(es) if any
  476. --git => include recent git \*-by: signers
  477. --git-all-signature-types => include signers regardless of signature type
  478. or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
  479. --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
  480. --git-chief-penguins => include ${penguin_chiefs}
  481. --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
  482. --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
  483. --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
  484. --git-blame => use git blame to find modified commits for patch or file
  485. --git-since => git history to use (default: $email_git_since)
  486. --hg-since => hg history to use (default: $email_hg_since)
  487. --m => include maintainer(s) if any
  488. --n => include name 'Full Name <addr\@domain.tld>'
  489. --l => include list(s) if any
  490. --s => include subscriber only list(s) if any
  491. --remove-duplicates => minimize duplicate email names/addresses
  492. --roles => show roles (status:subsystem, git-signer, list, etc...)
  493. --rolestats => show roles and statistics (commits/total_commits, %)
  494. --file-emails => add email addresses found in -f file (default: 0 (off))
  495. --scm => print SCM tree(s) if any
  496. --status => print status if any
  497. --subsystem => print subsystem name if any
  498. --web => print website(s) if any
  499. Output type options:
  500. --separator [, ] => separator for multiple entries on 1 line
  501. using --separator also sets --nomultiline if --separator is not [, ]
  502. --multiline => print 1 entry per line
  503. Other options:
  504. --pattern-depth => Number of pattern directory traversals (default: 0 (all))
  505. --keywords => scan patch for keywords (default: 1 (on))
  506. --sections => print the entire subsystem sections with pattern matches
  507. --version => show version
  508. --help => show this help information
  509. Default options:
  510. [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
  511. Notes:
  512. Using "-f directory" may give unexpected results:
  513. Used with "--git", git signators for _all_ files in and below
  514. directory are examined as git recurses directories.
  515. Any specified X: (exclude) pattern matches are _not_ ignored.
  516. Used with "--nogit", directory is used as a pattern match,
  517. no individual file within the directory or subdirectory
  518. is matched.
  519. Used with "--git-blame", does not iterate all files in directory
  520. Using "--git-blame" is slow and may add old committers and authors
  521. that are no longer active maintainers to the output.
  522. Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
  523. other automated tools that expect only ["name"] <email address>
  524. may not work because of additional output after <email address>.
  525. Using "--rolestats" and "--git-blame" shows the #/total=% commits,
  526. not the percentage of the entire file authored. # of commits is
  527. not a good measure of amount of code authored. 1 major commit may
  528. contain a thousand lines, 5 trivial commits may modify a single line.
  529. If git is not installed, but mercurial (hg) is installed and an .hg
  530. repository exists, the following options apply to mercurial:
  531. --git,
  532. --git-min-signatures, --git-max-maintainers, --git-min-percent, and
  533. --git-blame
  534. Use --hg-since not --git-since to control date selection
  535. File ".get_maintainer.conf", if it exists in the linux kernel source root
  536. directory, can change whatever get_maintainer defaults are desired.
  537. Entries in this file can be any command line argument.
  538. This file is prepended to any additional command line arguments.
  539. Multiple lines and # comments are allowed.
  540. EOT
  541. }
  542. sub top_of_kernel_tree {
  543. my ($lk_path) = @_;
  544. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  545. $lk_path .= "/";
  546. }
  547. if ( (-f "${lk_path}COPYING")
  548. && (-f "${lk_path}CREDITS")
  549. && (-f "${lk_path}Kbuild")
  550. && (-f "${lk_path}MAINTAINERS")
  551. && (-f "${lk_path}Makefile")
  552. && (-f "${lk_path}README")
  553. && (-d "${lk_path}Documentation")
  554. && (-d "${lk_path}arch")
  555. && (-d "${lk_path}include")
  556. && (-d "${lk_path}drivers")
  557. && (-d "${lk_path}fs")
  558. && (-d "${lk_path}init")
  559. && (-d "${lk_path}ipc")
  560. && (-d "${lk_path}kernel")
  561. && (-d "${lk_path}lib")
  562. && (-d "${lk_path}scripts")) {
  563. return 1;
  564. }
  565. return 0;
  566. }
  567. sub parse_email {
  568. my ($formatted_email) = @_;
  569. my $name = "";
  570. my $address = "";
  571. if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
  572. $name = $1;
  573. $address = $2;
  574. } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
  575. $address = $1;
  576. } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
  577. $address = $1;
  578. }
  579. $name =~ s/^\s+|\s+$//g;
  580. $name =~ s/^\"|\"$//g;
  581. $address =~ s/^\s+|\s+$//g;
  582. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  583. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  584. $name = "\"$name\"";
  585. }
  586. return ($name, $address);
  587. }
  588. sub format_email {
  589. my ($name, $address, $usename) = @_;
  590. my $formatted_email;
  591. $name =~ s/^\s+|\s+$//g;
  592. $name =~ s/^\"|\"$//g;
  593. $address =~ s/^\s+|\s+$//g;
  594. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  595. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  596. $name = "\"$name\"";
  597. }
  598. if ($usename) {
  599. if ("$name" eq "") {
  600. $formatted_email = "$address";
  601. } else {
  602. $formatted_email = "$name <$address>";
  603. }
  604. } else {
  605. $formatted_email = $address;
  606. }
  607. return $formatted_email;
  608. }
  609. sub find_first_section {
  610. my $index = 0;
  611. while ($index < @typevalue) {
  612. my $tv = $typevalue[$index];
  613. if (($tv =~ m/^(\C):\s*(.*)/)) {
  614. last;
  615. }
  616. $index++;
  617. }
  618. return $index;
  619. }
  620. sub find_starting_index {
  621. my ($index) = @_;
  622. while ($index > 0) {
  623. my $tv = $typevalue[$index];
  624. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  625. last;
  626. }
  627. $index--;
  628. }
  629. return $index;
  630. }
  631. sub find_ending_index {
  632. my ($index) = @_;
  633. while ($index < @typevalue) {
  634. my $tv = $typevalue[$index];
  635. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  636. last;
  637. }
  638. $index++;
  639. }
  640. return $index;
  641. }
  642. sub get_maintainer_role {
  643. my ($index) = @_;
  644. my $i;
  645. my $start = find_starting_index($index);
  646. my $end = find_ending_index($index);
  647. my $role;
  648. my $subsystem = $typevalue[$start];
  649. if (length($subsystem) > 20) {
  650. $subsystem = substr($subsystem, 0, 17);
  651. $subsystem =~ s/\s*$//;
  652. $subsystem = $subsystem . "...";
  653. }
  654. for ($i = $start + 1; $i < $end; $i++) {
  655. my $tv = $typevalue[$i];
  656. if ($tv =~ m/^(\C):\s*(.*)/) {
  657. my $ptype = $1;
  658. my $pvalue = $2;
  659. if ($ptype eq "S") {
  660. $role = $pvalue;
  661. }
  662. }
  663. }
  664. $role = lc($role);
  665. if ($role eq "supported") {
  666. $role = "supporter";
  667. } elsif ($role eq "maintained") {
  668. $role = "maintainer";
  669. } elsif ($role eq "odd fixes") {
  670. $role = "odd fixer";
  671. } elsif ($role eq "orphan") {
  672. $role = "orphan minder";
  673. } elsif ($role eq "obsolete") {
  674. $role = "obsolete minder";
  675. } elsif ($role eq "buried alive in reporters") {
  676. $role = "chief penguin";
  677. }
  678. return $role . ":" . $subsystem;
  679. }
  680. sub get_list_role {
  681. my ($index) = @_;
  682. my $i;
  683. my $start = find_starting_index($index);
  684. my $end = find_ending_index($index);
  685. my $subsystem = $typevalue[$start];
  686. if (length($subsystem) > 20) {
  687. $subsystem = substr($subsystem, 0, 17);
  688. $subsystem =~ s/\s*$//;
  689. $subsystem = $subsystem . "...";
  690. }
  691. if ($subsystem eq "THE REST") {
  692. $subsystem = "";
  693. }
  694. return $subsystem;
  695. }
  696. sub add_categories {
  697. my ($index) = @_;
  698. my $i;
  699. my $start = find_starting_index($index);
  700. my $end = find_ending_index($index);
  701. push(@subsystem, $typevalue[$start]);
  702. for ($i = $start + 1; $i < $end; $i++) {
  703. my $tv = $typevalue[$i];
  704. if ($tv =~ m/^(\C):\s*(.*)/) {
  705. my $ptype = $1;
  706. my $pvalue = $2;
  707. if ($ptype eq "L") {
  708. my $list_address = $pvalue;
  709. my $list_additional = "";
  710. my $list_role = get_list_role($i);
  711. if ($list_role ne "") {
  712. $list_role = ":" . $list_role;
  713. }
  714. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  715. $list_address = $1;
  716. $list_additional = $2;
  717. }
  718. if ($list_additional =~ m/subscribers-only/) {
  719. if ($email_subscriber_list) {
  720. push(@list_to, [$list_address, "subscriber list${list_role}"]);
  721. }
  722. } else {
  723. if ($email_list) {
  724. push(@list_to, [$list_address, "open list${list_role}"]);
  725. }
  726. }
  727. } elsif ($ptype eq "M") {
  728. my ($name, $address) = parse_email($pvalue);
  729. if ($name eq "") {
  730. if ($i > 0) {
  731. my $tv = $typevalue[$i - 1];
  732. if ($tv =~ m/^(\C):\s*(.*)/) {
  733. if ($1 eq "P") {
  734. $name = $2;
  735. $pvalue = format_email($name, $address, $email_usename);
  736. }
  737. }
  738. }
  739. }
  740. if ($email_maintainer) {
  741. my $role = get_maintainer_role($i);
  742. push_email_addresses($pvalue, $role);
  743. }
  744. } elsif ($ptype eq "T") {
  745. push(@scm, $pvalue);
  746. } elsif ($ptype eq "W") {
  747. push(@web, $pvalue);
  748. } elsif ($ptype eq "S") {
  749. push(@status, $pvalue);
  750. }
  751. }
  752. }
  753. }
  754. my %email_hash_name;
  755. my %email_hash_address;
  756. sub email_inuse {
  757. my ($name, $address) = @_;
  758. return 1 if (($name eq "") && ($address eq ""));
  759. return 1 if (($name ne "") && exists($email_hash_name{$name}));
  760. return 1 if (($address ne "") && exists($email_hash_address{$address}));
  761. return 0;
  762. }
  763. sub push_email_address {
  764. my ($line, $role) = @_;
  765. my ($name, $address) = parse_email($line);
  766. if ($address eq "") {
  767. return 0;
  768. }
  769. if (!$email_remove_duplicates) {
  770. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  771. } elsif (!email_inuse($name, $address)) {
  772. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  773. $email_hash_name{$name}++;
  774. $email_hash_address{$address}++;
  775. }
  776. return 1;
  777. }
  778. sub push_email_addresses {
  779. my ($address, $role) = @_;
  780. my @address_list = ();
  781. if (rfc822_valid($address)) {
  782. push_email_address($address, $role);
  783. } elsif (@address_list = rfc822_validlist($address)) {
  784. my $array_count = shift(@address_list);
  785. while (my $entry = shift(@address_list)) {
  786. push_email_address($entry, $role);
  787. }
  788. } else {
  789. if (!push_email_address($address, $role)) {
  790. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  791. }
  792. }
  793. }
  794. sub add_role {
  795. my ($line, $role) = @_;
  796. my ($name, $address) = parse_email($line);
  797. my $email = format_email($name, $address, $email_usename);
  798. foreach my $entry (@email_to) {
  799. if ($email_remove_duplicates) {
  800. my ($entry_name, $entry_address) = parse_email($entry->[0]);
  801. if (($name eq $entry_name || $address eq $entry_address)
  802. && ($role eq "" || !($entry->[1] =~ m/$role/))
  803. ) {
  804. if ($entry->[1] eq "") {
  805. $entry->[1] = "$role";
  806. } else {
  807. $entry->[1] = "$entry->[1],$role";
  808. }
  809. }
  810. } else {
  811. if ($email eq $entry->[0]
  812. && ($role eq "" || !($entry->[1] =~ m/$role/))
  813. ) {
  814. if ($entry->[1] eq "") {
  815. $entry->[1] = "$role";
  816. } else {
  817. $entry->[1] = "$entry->[1],$role";
  818. }
  819. }
  820. }
  821. }
  822. }
  823. sub which {
  824. my ($bin) = @_;
  825. foreach my $path (split(/:/, $ENV{PATH})) {
  826. if (-e "$path/$bin") {
  827. return "$path/$bin";
  828. }
  829. }
  830. return "";
  831. }
  832. sub mailmap {
  833. my (@lines) = @_;
  834. my %hash;
  835. foreach my $line (@lines) {
  836. my ($name, $address) = parse_email($line);
  837. if (!exists($hash{$name})) {
  838. $hash{$name} = $address;
  839. } elsif ($address ne $hash{$name}) {
  840. $address = $hash{$name};
  841. $line = format_email($name, $address, $email_usename);
  842. }
  843. if (exists($mailmap{$name})) {
  844. my $obj = $mailmap{$name};
  845. foreach my $map_address (@$obj) {
  846. if (($map_address eq $address) &&
  847. ($map_address ne $hash{$name})) {
  848. $line = format_email($name, $hash{$name}, $email_usename);
  849. }
  850. }
  851. }
  852. }
  853. return @lines;
  854. }
  855. sub git_execute_cmd {
  856. my ($cmd) = @_;
  857. my @lines = ();
  858. my $output = `$cmd`;
  859. $output =~ s/^\s*//gm;
  860. @lines = split("\n", $output);
  861. return @lines;
  862. }
  863. sub hg_execute_cmd {
  864. my ($cmd) = @_;
  865. my @lines = ();
  866. my $output = `$cmd`;
  867. @lines = split("\n", $output);
  868. return @lines;
  869. }
  870. sub vcs_find_signers {
  871. my ($cmd) = @_;
  872. my @lines = ();
  873. my $commits;
  874. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  875. my $pattern = $VCS_cmds{"commit_pattern"};
  876. $commits = grep(/$pattern/, @lines); # of commits
  877. @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
  878. if (!$email_git_penguin_chiefs) {
  879. @lines = grep(!/${penguin_chiefs}/i, @lines);
  880. }
  881. return (0, @lines) if !@lines;
  882. # cut -f2- -d":"
  883. s/.*:\s*(.+)\s*/$1/ for (@lines);
  884. ## Reformat email addresses (with names) to avoid badly written signatures
  885. foreach my $line (@lines) {
  886. my ($name, $address) = parse_email($line);
  887. $line = format_email($name, $address, 1);
  888. }
  889. return ($commits, @lines);
  890. }
  891. sub vcs_find_author {
  892. my ($cmd) = @_;
  893. my @lines = ();
  894. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  895. if (!$email_git_penguin_chiefs) {
  896. @lines = grep(!/${penguin_chiefs}/i, @lines);
  897. }
  898. return @lines if !@lines;
  899. ## Reformat email addresses (with names) to avoid badly written signatures
  900. foreach my $line (@lines) {
  901. my ($name, $address) = parse_email($line);
  902. $line = format_email($name, $address, 1);
  903. }
  904. return @lines;
  905. }
  906. sub vcs_save_commits {
  907. my ($cmd) = @_;
  908. my @lines = ();
  909. my @commits = ();
  910. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  911. foreach my $line (@lines) {
  912. if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
  913. push(@commits, $1);
  914. }
  915. }
  916. return @commits;
  917. }
  918. sub vcs_blame {
  919. my ($file) = @_;
  920. my $cmd;
  921. my @commits = ();
  922. return @commits if (!(-f $file));
  923. if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
  924. my @all_commits = ();
  925. $cmd = $VCS_cmds{"blame_file_cmd"};
  926. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  927. @all_commits = vcs_save_commits($cmd);
  928. foreach my $file_range_diff (@range) {
  929. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  930. my $diff_file = $1;
  931. my $diff_start = $2;
  932. my $diff_length = $3;
  933. next if ("$file" ne "$diff_file");
  934. for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
  935. push(@commits, $all_commits[$i]);
  936. }
  937. }
  938. } elsif (@range) {
  939. foreach my $file_range_diff (@range) {
  940. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  941. my $diff_file = $1;
  942. my $diff_start = $2;
  943. my $diff_length = $3;
  944. next if ("$file" ne "$diff_file");
  945. $cmd = $VCS_cmds{"blame_range_cmd"};
  946. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  947. push(@commits, vcs_save_commits($cmd));
  948. }
  949. } else {
  950. $cmd = $VCS_cmds{"blame_file_cmd"};
  951. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  952. @commits = vcs_save_commits($cmd);
  953. }
  954. foreach my $commit (@commits) {
  955. $commit =~ s/^\^//g;
  956. }
  957. return @commits;
  958. }
  959. my $printed_novcs = 0;
  960. sub vcs_exists {
  961. %VCS_cmds = %VCS_cmds_git;
  962. return 1 if eval $VCS_cmds{"available"};
  963. %VCS_cmds = %VCS_cmds_hg;
  964. return 1 if eval $VCS_cmds{"available"};
  965. %VCS_cmds = ();
  966. if (!$printed_novcs) {
  967. warn("$P: No supported VCS found. Add --nogit to options?\n");
  968. warn("Using a git repository produces better results.\n");
  969. warn("Try Linus Torvalds' latest git repository using:\n");
  970. warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
  971. $printed_novcs = 1;
  972. }
  973. return 0;
  974. }
  975. sub vcs_assign {
  976. my ($role, $divisor, @lines) = @_;
  977. my %hash;
  978. my $count = 0;
  979. return if (@lines <= 0);
  980. if ($divisor <= 0) {
  981. warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
  982. $divisor = 1;
  983. }
  984. if ($email_remove_duplicates) {
  985. @lines = mailmap(@lines);
  986. }
  987. return if (@lines <= 0);
  988. @lines = sort(@lines);
  989. # uniq -c
  990. $hash{$_}++ for @lines;
  991. # sort -rn
  992. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  993. my $sign_offs = $hash{$line};
  994. my $percent = $sign_offs * 100 / $divisor;
  995. $percent = 100 if ($percent > 100);
  996. $count++;
  997. last if ($sign_offs < $email_git_min_signatures ||
  998. $count > $email_git_max_maintainers ||
  999. $percent < $email_git_min_percent);
  1000. push_email_address($line, '');
  1001. if ($output_rolestats) {
  1002. my $fmt_percent = sprintf("%.0f", $percent);
  1003. add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
  1004. } else {
  1005. add_role($line, $role);
  1006. }
  1007. }
  1008. }
  1009. sub vcs_file_signoffs {
  1010. my ($file) = @_;
  1011. my @signers = ();
  1012. my $commits;
  1013. return if (!vcs_exists());
  1014. my $cmd = $VCS_cmds{"find_signers_cmd"};
  1015. $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
  1016. ($commits, @signers) = vcs_find_signers($cmd);
  1017. vcs_assign("commit_signer", $commits, @signers);
  1018. }
  1019. sub vcs_file_blame {
  1020. my ($file) = @_;
  1021. my @signers = ();
  1022. my @all_commits = ();
  1023. my @commits = ();
  1024. my $total_commits;
  1025. my $total_lines;
  1026. return if (!vcs_exists());
  1027. @all_commits = vcs_blame($file);
  1028. @commits = uniq(@all_commits);
  1029. $total_commits = @commits;
  1030. $total_lines = @all_commits;
  1031. foreach my $commit (@commits) {
  1032. my $commit_count;
  1033. my @commit_signers = ();
  1034. my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
  1035. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  1036. ($commit_count, @commit_signers) = vcs_find_signers($cmd);
  1037. push(@signers, @commit_signers);
  1038. }
  1039. if ($from_filename) {
  1040. if ($output_rolestats) {
  1041. my @blame_signers;
  1042. foreach my $commit (@commits) {
  1043. my $i;
  1044. my $cmd = $VCS_cmds{"find_commit_author_cmd"};
  1045. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  1046. my @author = vcs_find_author($cmd);
  1047. next if !@author;
  1048. my $count = grep(/$commit/, @all_commits);
  1049. for ($i = 0; $i < $count ; $i++) {
  1050. push(@blame_signers, $author[0]);
  1051. }
  1052. }
  1053. if (@blame_signers) {
  1054. vcs_assign("authored lines", $total_lines, @blame_signers);
  1055. }
  1056. }
  1057. vcs_assign("commits", $total_commits, @signers);
  1058. } else {
  1059. vcs_assign("modified commits", $total_commits, @signers);
  1060. }
  1061. }
  1062. sub uniq {
  1063. my (@parms) = @_;
  1064. my %saw;
  1065. @parms = grep(!$saw{$_}++, @parms);
  1066. return @parms;
  1067. }
  1068. sub sort_and_uniq {
  1069. my (@parms) = @_;
  1070. my %saw;
  1071. @parms = sort @parms;
  1072. @parms = grep(!$saw{$_}++, @parms);
  1073. return @parms;
  1074. }
  1075. sub clean_file_emails {
  1076. my (@file_emails) = @_;
  1077. my @fmt_emails = ();
  1078. foreach my $email (@file_emails) {
  1079. $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
  1080. my ($name, $address) = parse_email($email);
  1081. if ($name eq '"[,\.]"') {
  1082. $name = "";
  1083. }
  1084. my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
  1085. if (@nw > 2) {
  1086. my $first = $nw[@nw - 3];
  1087. my $middle = $nw[@nw - 2];
  1088. my $last = $nw[@nw - 1];
  1089. if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
  1090. (length($first) == 2 && substr($first, -1) eq ".")) ||
  1091. (length($middle) == 1 ||
  1092. (length($middle) == 2 && substr($middle, -1) eq "."))) {
  1093. $name = "$first $middle $last";
  1094. } else {
  1095. $name = "$middle $last";
  1096. }
  1097. }
  1098. if (substr($name, -1) =~ /[,\.]/) {
  1099. $name = substr($name, 0, length($name) - 1);
  1100. } elsif (substr($name, -2) =~ /[,\.]"/) {
  1101. $name = substr($name, 0, length($name) - 2) . '"';
  1102. }
  1103. if (substr($name, 0, 1) =~ /[,\.]/) {
  1104. $name = substr($name, 1, length($name) - 1);
  1105. } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
  1106. $name = '"' . substr($name, 2, length($name) - 2);
  1107. }
  1108. my $fmt_email = format_email($name, $address, $email_usename);
  1109. push(@fmt_emails, $fmt_email);
  1110. }
  1111. return @fmt_emails;
  1112. }
  1113. sub merge_email {
  1114. my @lines;
  1115. my %saw;
  1116. for (@_) {
  1117. my ($address, $role) = @$_;
  1118. if (!$saw{$address}) {
  1119. if ($output_roles) {
  1120. push(@lines, "$address ($role)");
  1121. } else {
  1122. push(@lines, $address);
  1123. }
  1124. $saw{$address} = 1;
  1125. }
  1126. }
  1127. return @lines;
  1128. }
  1129. sub output {
  1130. my (@parms) = @_;
  1131. if ($output_multiline) {
  1132. foreach my $line (@parms) {
  1133. print("${line}\n");
  1134. }
  1135. } else {
  1136. print(join($output_separator, @parms));
  1137. print("\n");
  1138. }
  1139. }
  1140. my $rfc822re;
  1141. sub make_rfc822re {
  1142. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  1143. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  1144. # This regexp will only work on addresses which have had comments stripped
  1145. # and replaced with rfc822_lwsp.
  1146. my $specials = '()<>@,;:\\\\".\\[\\]';
  1147. my $controls = '\\000-\\037\\177';
  1148. my $dtext = "[^\\[\\]\\r\\\\]";
  1149. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  1150. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  1151. # Use zero-width assertion to spot the limit of an atom. A simple
  1152. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  1153. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  1154. my $word = "(?:$atom|$quoted_string)";
  1155. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  1156. my $sub_domain = "(?:$atom|$domain_literal)";
  1157. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  1158. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  1159. my $phrase = "$word*";
  1160. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  1161. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  1162. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  1163. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  1164. my $address = "(?:$mailbox|$group)";
  1165. return "$rfc822_lwsp*$address";
  1166. }
  1167. sub rfc822_strip_comments {
  1168. my $s = shift;
  1169. # Recursively remove comments, and replace with a single space. The simpler
  1170. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  1171. # chars in atoms, for example.
  1172. while ($s =~ s/^((?:[^"\\]|\\.)*
  1173. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  1174. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  1175. return $s;
  1176. }
  1177. # valid: returns true if the parameter is an RFC822 valid address
  1178. #
  1179. sub rfc822_valid {
  1180. my $s = rfc822_strip_comments(shift);
  1181. if (!$rfc822re) {
  1182. $rfc822re = make_rfc822re();
  1183. }
  1184. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  1185. }
  1186. # validlist: In scalar context, returns true if the parameter is an RFC822
  1187. # valid list of addresses.
  1188. #
  1189. # In list context, returns an empty list on failure (an invalid
  1190. # address was found); otherwise a list whose first element is the
  1191. # number of addresses found and whose remaining elements are the
  1192. # addresses. This is needed to disambiguate failure (invalid)
  1193. # from success with no addresses found, because an empty string is
  1194. # a valid list.
  1195. sub rfc822_validlist {
  1196. my $s = rfc822_strip_comments(shift);
  1197. if (!$rfc822re) {
  1198. $rfc822re = make_rfc822re();
  1199. }
  1200. # * null list items are valid according to the RFC
  1201. # * the '1' business is to aid in distinguishing failure from no results
  1202. my @r;
  1203. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  1204. $s =~ m/^$rfc822_char*$/) {
  1205. while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  1206. push(@r, $1);
  1207. }
  1208. return wantarray ? (scalar(@r), @r) : 1;
  1209. }
  1210. return wantarray ? () : 0;
  1211. }