1#!/usr/bin/perl -w 2use strict; 3use warnings; 4use 5.010; 5use File::Find; 6use IO::File; 7use Getopt::Long; 8use Pod::Usage; 9 10my %limits = ( 11 c90 => { 12 'logical-source-line-length' => 509, 13 }, 14 c99 => { 15 'logical-source-line-length' => 4095, 16 }, 17); 18 19my %opt = ( 20 std => 'c99', 21); 22 23GetOptions(\%opt, qw( logical-source-line-length=i std=s )) 24 && @ARGV && exists $limits{$opt{std}} 25 or pod2usage(2); 26 27for my $k (keys %{$limits{$opt{std}}}) { 28 $opt{$k} //= $limits{$opt{std}}{$k}; 29} 30 31{ 32 my $num = 1; 33 34 sub report 35 { 36 my $msg = shift; 37 my $info = join '', @_; 38 39 if ($info) { 40 $info =~ s/\R+$//; 41 $info =~ s/^/ #|\t/mg; 42 $info = "\n$info\n\n"; 43 } 44 45 warn sprintf "[%d] %s(%d): %s\n%s", 46 $num++, $File::Find::name, $., $msg, $info; 47 } 48} 49 50find(sub { 51 /\.([ch]|xs)$/ or return; 52 53 my $fh = IO::File->new($_, 'r') or die "$_: $!\n"; 54 my $ll = ''; 55 56 while (defined(my $line = <$fh>)) { 57 report("trailing whitespace after backslash", $line) 58 if $line =~ /\\[[:blank:]]+$/; 59 60 $ll .= $line; 61 62 unless ($ll =~ /\\$/) { 63 if (length $ll > $opt{'logical-source-line-length'}) { 64 report(sprintf("logical source line too long (%d > %d)", 65 length $ll, $opt{'logical-source-line-length'}), $ll); 66 } 67 $ll = ''; 68 } 69 } 70}, @ARGV); 71 72__END__ 73 74=head1 NAME 75 76checkansi.pl - Check source code for ANSI-C violations 77 78=head1 SYNOPSIS 79 80checkansi.pl [B<--std>=c90|c99] 81[B<--logical-source-line-length>=I<num>] 82<path> ... 83 84=head1 DESCRIPTION 85 86B<checkansi.pl> searches 87 88=head1 OPTIONS 89 90=over 4 91 92=item B<--std>=c90|c99 93 94Choose the ANSI/ISO standard against which shall be checked. 95Defaults to C<c99>. 96 97=item B<--logical-source-line-length>=I<number> 98 99Maximum length of a logical source line. Overrides the default 100given by the chosen standard. 101 102=back 103 104=head1 COPYRIGHT 105 106Copyright 2007 by Marcus Holland-Moritz <mhx@cpan.org>. 107 108This program is free software; you may redistribute it 109and/or modify it under the same terms as Perl itself. 110 111=cut 112