get_maintainer.pl 34 KB

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