Debian Codegraph @

bin/ped

raw
1 #!/usr/bin/perl -w
2
3 $doc = <<'';
4 # perl editor : edit files in place
5 #
6 # ped edits files via perl command(s) in place and saves a backup file.
7 #
8 # Files are loaded and edited in one chunk, not line by line.
9 # Symbolic and hard links are handled correctly, in opposition to
10 # "sed -i" or "perl -p -i -e" which breaks links.
11 # A file is assigend to $_, which you can modify with any Perl commands.
12 # ped can also edit STDIN in one chunk (multiline edit).
13 #
14 # Hint: use s/^.../.../gm to modify each line begin.
15
16 # 2016-09-11 keep hard and soft links
17 # 2016-09-14 added option -q
18
19 use Getopt::Std;
20
21 $0 =~ s:.*/::;
22 local $/;
23
24 $usage =
25 "usage: $0 [-n] [-v] [-q] 'perl command(s)' file(s)\n".
26 "options: -n no backup\n".
27 " -v verbose mode\n".
28 " -q quiet mode (no warnings)\n".
29 "example: $0 's/ /_/g' *.txt\n".
30 "\n".
31 "usage: ... | $0 'perl command(s)'\n".
32 "example: ifconfig | $0 's/(.+?)\\n\\n/{\$1\\n}\\n/sg'\n";
33
34 $opt_h = $opt_n = $opt_v = $opt_q = 0;
35 getopts('hnvq') or die $usage;
36
37 if ($opt_h) {
38 print $doc,"\n",$usage;
39 exit;
40 }
41
42 $opt_q = 0 if $opt_v;
43
44 $cmd = shift or die $usage;
45
46 if ($vv = pathsearch('vv') and slurp($vv) !~ /visual versioning/) {
47 $vv = '';
48 }
49
50 $STDOUT = '';
51 tie *STDOUT => "Buffer",\$STDOUT;
52
53 if (@ARGV) {
54 foreach $file (@ARGV) {
55 unless (-e $file) {
56 $status = 2;
57 warn "$0: $file does not exist\n";
58 next;
59 }
60 unless (-f $file) {
61 $status = 3;
62 warn "$0: $file is not a regular file\n";
63 next;
64 }
65 unless (-w $file) {
66 $status = 4;
67 warn "$0: no write permission for $file\n";
68 next;
69 }
70 $file = realfilename($file) if -l $file;
71 @stat = stat $file;
72 $fid = $stat[0].':'.$stat[1];
73 if ($fhl = $file{$fid}) {
74 if (not $opt_q and $file ne $fhl) {
75 warn "$0: ignoring hard link $file of $fhl\n";
76 }
77 next;
78 }
79 $file{$fid} = $file;
80 unless (open $file,'+>>',$file) {
81 $status = $?;
82 warn "$0: cannot write $file - $!\n";
83 next;
84 }
85 seek $file,0,0;
86 $_ = $__ = <$file>;
87 eval $cmd;
88 $_ .= $STDOUT;
89 if ($@) {
90 $status = 2;
91 warn "$0: $@";
92 } elsif ($_ eq $__) {
93 warn "$0: $file not modified\n" if $opt_v;
94 } else {
95 unless ($opt_n) {
96 if ($vv) {
97 if ($opt_v) {
98 print STDERR "$0: creating backup file: ";
99 system qw'vv -vs',$file
100 } else {
101 system qw'vv -sq',$file
102 }
103 } else {
104 warn "$0: creating backup file $file~\n" if $opt_v;
105 system qw'rsync -aA',$file,"$file~";
106 }
107 }
108 truncate $file,0;
109 seek $file,0,0;
110 print {$file} $_;
111 $status ||= 0;
112 warn "$0: $file modified\n" if $opt_v;
113 }
114 close $file;
115 }
116 if (defined $status) { exit $status } else { exit 99 }
117 } else {
118 $_ = <STDIN>;
119 eval $cmd;
120 die "$0: $@" if $@;
121 $^W = 0;
122 untie *STDOUT;
123 $_ .= $STDOUT;
124 print;
125 exit;
126 }
127
128
129 # resolve symlinks
130 sub realfilename {
131 my $file = shift;
132
133 if (-e $file) {
134 if (-l $file) {
135 my $link = readlink($file);
136 if ($link !~ /^\// and $file =~ m:(.*/).:) {
137 $link = $1 . $link;
138 }
139 return realfilename($link);
140 } else {
141 return $file;
142 }
143 } else {
144 return undef;
145 }
146 }
147
148
149 sub pathsearch {
150 my $prg = shift;
151
152 foreach my $dir (split(':',$ENV{PATH})) {
153 return "$dir/$prg" if -x "$dir/$prg";
154 }
155 }
156
157
158 sub slurp {
159 my $file = shift;
160 local $_;
161 local $/;
162
163 if (open $file,$file) {
164 $_ = <$file>;
165 close $file;
166 }
167
168 return $_;
169 }
170
171
172 # tie STDOUT to buffer variable (redefining print and printf)
173 package Buffer;
174
175 sub TIEHANDLE {
176 my ($class,$buffer) = @_;
177 bless $buffer,$class;
178 }
179
180 sub PRINT {
181 my $buffer = shift;
182 $$buffer .= $_ foreach @_;
183 }
184
185 sub PRINTF {
186 my $buffer = shift;
187 my $fmt = shift @_;
188 $$buffer .= sprintf($fmt,@_);
189 }
190
191 1;