get_maintainer.pl 36 KB

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