-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewline.pl
executable file
·109 lines (90 loc) · 2.74 KB
/
newline.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl
#
# newline.pl -- check C sources for violation of section 2.1.1.2 of the ANSI C 1989 standard.
# Also in Section 5.1.1.2 of the ISO C 1999 standard
#
# Optionally fixes files and checks them into subversion.
#
use strict;
use Carp;
use Getopt::Long;
use Digest::MD5;
use SVN::Client;
use SVN::Core;
use SVN::Wc;
use Encode;
my %option = ();
GetOptions(\%option, 'update', 'commit');
my $ctx;
if (defined $option{commit} && $ARGV[0]) {
my %svnClientOpts = ();
$svnClientOpts{auth} = [SVN::Client::get_simple_provider(),
SVN::Client::get_simple_prompt_provider(\&simple_prompt,2),
SVN::Client::get_username_provider()];
$ctx = new SVN::Client(%svnClientOpts);
$ctx->log_msg(\&log_comments);
}
foreach (@ARGV) {
my $source = $_;
unless ((defined $option{update}) ? (-w "$source") : (-r "$source")) {
carp "$0: cannot operate on `$source'";
next;
}
my $srcfile = ((defined $option{update}) ? "+" : "") . "<$source";
unless (open(FH, $srcfile)) {
carp "$0: could not read `$source'";
next;
}
my $fsize;
(undef , undef, undef, undef, undef, undef , undef, $fsize) = stat FH;
# empty files need not end with a newline
next if ($fsize == 0);
unless (seek(FH, -1,2)) {
carp "$0: could not seek to the end of `$source'";
close(FH);
next;
}
my $b;
unless (read(FH, $b, 1)) {
carp "$0: could not read at EOF `$source'";
close(FH);
next;
}
unless ($b eq "\n") {
if (defined $option{update}) {
unless (seek(FH, 0,2)) {
carp "$0: could not seek to EOF `$source'";
close(FH);
next;
}
print FH "\n";
close(FH);
if (defined $option{commit}) {
my ($commit_val) = $ctx->commit(Encode::encode('utf8', $source), 0);
if (!defined $commit_val || $commit_val->revision() == $SVN::Core::INVALID_REVNUM) {
croak "$0: svn commit failed `$source'";
}
}
}
print "$source\n";
}
}
# callback for authentication
sub simple_prompt {
my ($cred,$realm,$default_username,$may_save,$pool) = @_;
print "Enter authentication info for realm: $realm\n";
print "Username: ";
my $username = <>;
chomp($username);
$cred->username($username);
print "Password: ";
my $password = <>;
chomp($password);
$cred->password($password);
}
# callback for comments
sub log_comments {
my ($msg,$tmpFile,$commit_ary,$pool) = @_;
$$msg = Encode::encode('utf8', "fixed violation of section 2.1.1.2 of the ANSI C 1989 standard");
}
# end of newline.pl