| 1 | # $Id: Cfe.pm,v 1.49 2002/06/03 10:22:08 jh Exp $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Copyright 2001 Jörgen Hägg | ||||||||||||||||||||||||||||
| 4 | # You may distribute under the terms of the GNU General Public License. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Config::Cfe - File configuration module | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | use Config::Cfe; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | This module contains functions that ease updating | ||||||||||||||||||||||||||||
| 17 | small text files, usually konfiguration files. | ||||||||||||||||||||||||||||
| 18 | Will complain if the file is more than 100000 lines (configurable). | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | It is inspired from cfengine, but does only the editing, cfengine | ||||||||||||||||||||||||||||
| 21 | can do much more. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 is_sunos is_sunos4 is_sunos5 | ||||||||||||||||||||||||||||
| 25 | is_freebsd is_linux is_debian | ||||||||||||||||||||||||||||
| 26 | is_i386 is_i486 is_i586 is_i686 | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Boolean test functions | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package Config::Cfe; | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT $debug $verbose $par); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ($VERSION) = '$Revision: 1.49 $ ' =~ /\$Revision:\s+([^\s]+)/; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 40 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 41 | 1 | @EXPORT = qw(read_file write_file abort_file new_file | |||||||||||||||||||||||||||
| 42 | par_os_type par_os_rev par_mach_type | ||||||||||||||||||||||||||||
| 43 | locate incr goto_top goto_end | ||||||||||||||||||||||||||||
| 44 | append insert append_file delete_all delete_to delete_n delete_file | ||||||||||||||||||||||||||||
| 45 | find_str eval_to eval_all eval_n add_list | ||||||||||||||||||||||||||||
| 46 | eval_where replace_all | ||||||||||||||||||||||||||||
| 47 | exec_where | ||||||||||||||||||||||||||||
| 48 | set_comment | ||||||||||||||||||||||||||||
| 49 | comment_where comment_to comment_n uncomment_where uncomment_to | ||||||||||||||||||||||||||||
| 50 | uncomment_n set_debug | ||||||||||||||||||||||||||||
| 51 | insert_sect append_sect start_sect end_sect delete_sect find_sect | ||||||||||||||||||||||||||||
| 52 | uncomment_sect comment_sect get_sect | ||||||||||||||||||||||||||||
| 53 | is_sunos4 is_sunos5 is_freebsd is_sunos is_linux | ||||||||||||||||||||||||||||
| 54 | is_i386 is_i486 is_i586 is_i686 | ||||||||||||||||||||||||||||
| 55 | is_debian | ||||||||||||||||||||||||||||
| 56 | curr_line last_line list_lines get_line | ||||||||||||||||||||||||||||
| 57 | cfe_version | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | my (@verb_save); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 predefined functions | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | A few predefined boolean functions are available | ||||||||||||||||||||||||||||
| 65 | for easy architecture checking: | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Function True if this is | ||||||||||||||||||||||||||||
| 68 | _________________________________________ | ||||||||||||||||||||||||||||
| 69 | is_debian a Debian GNU/Linux system, returns the actual version | ||||||||||||||||||||||||||||
| 70 | is_linux a Linux system | ||||||||||||||||||||||||||||
| 71 | is_i386 a 386-based system | ||||||||||||||||||||||||||||
| 72 | is_i486 a 486-based system | ||||||||||||||||||||||||||||
| 73 | is_i586 a 586-based system | ||||||||||||||||||||||||||||
| 74 | is_i686 a 686-based system | ||||||||||||||||||||||||||||
| 75 | is_sunos a SunOS system | ||||||||||||||||||||||||||||
| 76 | is_sunos4 a SunOS 4 system | ||||||||||||||||||||||||||||
| 77 | is_sunos5 a SunOS 5 system | ||||||||||||||||||||||||||||
| 78 | is_freebsd a FreeBSD system | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 predefined parameter functions | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | These functions returns some interesting values about the OS. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Function Returns | ||||||||||||||||||||||||||||
| 85 | _________________________________ | ||||||||||||||||||||||||||||
| 86 | par_os_type OS type from 'uname' | ||||||||||||||||||||||||||||
| 87 | par_os_rev OS revision from 'uname', modified to integer | ||||||||||||||||||||||||||||
| 88 | par_mach_type OS architecture from 'uname' | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | ###################################################################### | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 cfe_version [minimal version] | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Returns the current version of the B<cfe>-module. | ||||||||||||||||||||||||||||
| 97 | If called with a version number, cfe will die if B<cfe> is less than | ||||||||||||||||||||||||||||
| 98 | this version. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub cfe_version { | ||||||||||||||||||||||||||
|
Subroutine
cfe_version: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($min_vers) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $rev = '$Revision: 1.49 $'; | |||||||||||||||||||||||||||
| 106 | 0 | my ($v) = $rev =~ /:\s+([\d\.]+)/; | |||||||||||||||||||||||||||
| 107 | 0 | my $ndv = sprintf("%.3f", $v); | |||||||||||||||||||||||||||
| 108 | 0 | $ndv =~ s/\.//; | |||||||||||||||||||||||||||
| 109 | 0 | my $min_ndv = sprintf("%.3f", $min_vers); | |||||||||||||||||||||||||||
| 110 | 0 | $min_ndv =~ s/\.//; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($min_vers > 0 && $ndv < $min_ndv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$min_vers > 0 and $ndv < $min_ndv
MC/DC0%0/2
$min_vers > 0 and $ndv < $min_ndv
$min_vers > 0
$ndv < $min_ndv
Truth table0%0/3
A and B
A= $min_vers > 0B= $ndv < $min_ndv
| |||||||||||||||||||||||||||||
| 113 | 0 | croak sprintf("This program requires Config::Cfe ". | |||||||||||||||||||||||||||
| 114 | "version %.3f or greater, abort\n", $min_vers); | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | $ndv; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ###################################################################### | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 set_debug [debug [verbose]] | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | If B<debug> is non-zero then B<write_file> will write | ||||||||||||||||||||||||||||
| 124 | the new file but B<not> rename the file from I<file.new> to I<file>. | ||||||||||||||||||||||||||||
| 125 | B<verbose> greater than 0 will print out debug info. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Example: | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | use Getopt::Std; | ||||||||||||||||||||||||||||
| 130 | getopt('dv'); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | $host = hostname; | ||||||||||||||||||||||||||||
| 133 | set_debug($opt_d, $opt_v); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub set_debug { | ||||||||||||||||||||||||||
|
Subroutine
set_debug: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 0 | $debug = $_[0]; | |||||||||||||||||||||||||||
| 139 | 0 | $verbose = $_[1]; | |||||||||||||||||||||||||||
| 140 | 0 | for my $i (@verb_save) { | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | print $i if $verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 0 | @verb_save = (); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 new_file filename | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Start editing a new file, will overwrite any existing file. | ||||||||||||||||||||||||||||
| 149 | A new file will get file mode 0666 & umask. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub new_file { | ||||||||||||||||||||||||||
|
Subroutine
new_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # &lock($file); | ||||||||||||||||||||||||||||
| 157 | 0 | $par->{'file_name'} = $file; | |||||||||||||||||||||||||||
| 158 | 0 | $par->{'edit'} = $par->{'cur_line'} = 0; | |||||||||||||||||||||||||||
| 159 | 0 | $par->{'lines'} = []; | |||||||||||||||||||||||||||
| 160 | 0 | &verbose("new_file", $file); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 read_file filename [,1] | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Start editing an existing file. | ||||||||||||||||||||||||||||
| 166 | If I<filename> doesn't exist, B<read_file> will | ||||||||||||||||||||||||||||
| 167 | either complain or, if optional second argument is true, create | ||||||||||||||||||||||||||||
| 168 | the file. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | ▶ | sub read_file { | ||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=7 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 173 | 1 | my ($file, $exist_flag) = @_; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # &lock($file); | ||||||||||||||||||||||||||||
| 176 | 1 | ▶ | if ($exist_flag && !-e $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$exist_flag and not -e $file
MC/DC0%0/2
$exist_flag and not -e $file
$exist_flag
not -e $file
Truth table33%1/3
A and not(B)
A= $exist_flagB= not -e $file
| |||||||||||||||||||||||||||||
| 177 | 0 | &new_file($file); | |||||||||||||||||||||||||||
| 178 | 0 | $par->{'mode'} = 0666 & umask; | |||||||||||||||||||||||||||
| 179 | 0 | $par->{'uid'} = $>; | |||||||||||||||||||||||||||
| 180 | 0 | $par->{'gid'} = $); | |||||||||||||||||||||||||||
| 181 | &verbose('read_file', sprintf("mode=0%o, uid=%d, gid=%d", | ||||||||||||||||||||||||||||
| 182 | 0 | $par->{'mode'}, $par->{'uid'}, $par->{'gid'})); | |||||||||||||||||||||||||||
| 183 | 0 | return; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1 | &get_filemode($file); | |||||||||||||||||||||||||||
| 187 | 1 | $par->{'file_name'} = $file; | |||||||||||||||||||||||||||
| 188 | 1 | $par->{'edit'} = $par->{'cur_line'} = 0; | |||||||||||||||||||||||||||
| 189 | 1 | my $lines = $par->{'lines'} = []; | |||||||||||||||||||||||||||
| 190 | 1 | ▶ | unless (-e $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | &verbose("read_file", "$file does not exist"); | |||||||||||||||||||||||||||
| 192 | 0 | return; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 1 | ▶ | open(FILE, $file) || croak "open $file:$!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 1 | ▶ | while(<FILE>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 14 | chop; | |||||||||||||||||||||||||||
| 197 | 14 | push(@$lines, $_); | |||||||||||||||||||||||||||
| 198 | 14 | ▶ | croak "too many lines" if @$lines >= $par->{'max_lines'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 1 | close(FILE); | |||||||||||||||||||||||||||
| 201 | 1 | &verbose("read_file", "$file, ".(@$lines*1)." lines"); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 write_file [\%par] | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Write the current file if it has been changed. | ||||||||||||||||||||||||||||
| 207 | Control of the file can be done thru the hash parameter B<par>. | ||||||||||||||||||||||||||||
| 208 | The old file will be renamed to B<.>I<filename>B<.cfe>. | ||||||||||||||||||||||||||||
| 209 | Will not rename the final file (I<filename.new>) to I<filename> | ||||||||||||||||||||||||||||
| 210 | if debug is active. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Accepted keys for B<par>: | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | key name default | ||||||||||||||||||||||||||||
| 215 | -------------------------------------- | ||||||||||||||||||||||||||||
| 216 | mode filemode 0666 & umask. | ||||||||||||||||||||||||||||
| 217 | uid numeric userid 0 | ||||||||||||||||||||||||||||
| 218 | gid numeric groupid 0 | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1 | ▶ | sub write_file { | ||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=11 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | 1 | my (%tpar) = @_; | |||||||||||||||||||||||||||
| 224 | 1 | my ($tmp, $fsize, $file); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 1 | ▶ | return unless $par->{'edit'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 1 | ▶ | $tpar{'mode'} = $par->{'mode'} unless $tpar{'mode'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 1 | ▶ | $tpar{'uid'} = $par->{'uid'} unless $tpar{'uid'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 1 | ▶ | $tpar{'gid'} = $par->{'gid'} unless $tpar{'gid'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1 | ▶ | croak "No filename!" unless $file = $par->{'file_name'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 1 | ($tmp = $file) =~ s/$/.new/; | |||||||||||||||||||||||||||
| 234 | 1 | ▶ | open(FILEO, ">$tmp") || croak "can't create $tmp:$!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 1 | chmod $tpar{'mode'}, $tmp; | |||||||||||||||||||||||||||
| 236 | 1 | chown $tpar{'uid'}, $tpar{'gid'}, $tmp; | |||||||||||||||||||||||||||
| 237 | 1 | for my $line (@{$par->{'lines'}}) { | |||||||||||||||||||||||||||
| 238 | 16 | print FILEO "$line\n"; | |||||||||||||||||||||||||||
| 239 | 16 | $fsize += length($line)+1; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 1 | close(FILEO); | |||||||||||||||||||||||||||
| 242 | 1 | ▶ | &abort_file("incorrect filesize, abort") if -s $tmp != $fsize; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 1 | ▶ | unless ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 1 | rename($file, ".$file.cfe"); | |||||||||||||||||||||||||||
| 245 | 1 | ▶ | rename($tmp, $file) || &abort_file("rename $tmp to $file:$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 1 | $par->{'lines'} = []; | |||||||||||||||||||||||||||
| 248 | &verbose('write_file', "wrote $file, ". | ||||||||||||||||||||||||||||
| 249 | 1 | (@{$par->{'lines'}}*1)." lines, $fsize bytes"); | |||||||||||||||||||||||||||
| 250 | # &unlock($file); | ||||||||||||||||||||||||||||
| 251 | 1 | &reset_par; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =head1 abort_file | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Quit editing the current file. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =cut | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 0 | ▶ | sub abort_file { | ||||||||||||||||||||||||||
|
Subroutine
abort_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 261 | 0 | my ($txt) = @_; | |||||||||||||||||||||||||||
| 262 | 0 | my ($tmp, $fsize, $file); | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | croak "No filename!" unless $file = $par->{'file_name'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 0 | ($tmp = $file) =~ s/$/.new/; | |||||||||||||||||||||||||||
| 266 | 0 | ▶ | unlink($tmp) || croak "unlink $tmp:$!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | ▶ | croak $txt if $txt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | # &unlock($file); | ||||||||||||||||||||||||||||
| 269 | 0 | &reset_par; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =head1 append_file filename | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | Appends B<filename> at the current line. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =cut | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | ▶ | sub append_file { | ||||||||||||||||||||||||||
|
Subroutine
append_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 279 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 280 | 0 | my (@tmp); | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | &verbose("append_file", "**** $file"); | |||||||||||||||||||||||||||
| 283 | 0 | ▶ | open(FILE, $file) || croak "open $file:$!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | while(<FILE>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 0 | chop; | |||||||||||||||||||||||||||
| 286 | 0 | push(@tmp, $_); | |||||||||||||||||||||||||||
| 287 | 0 | &verbose("append_file", $_); | |||||||||||||||||||||||||||
| 288 | croak "too many lines" | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | if @{$par->{'lines'}} >= $par->{'max_lines'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | 0 | close(FILE); | |||||||||||||||||||||||||||
| 292 | 0 | &append(@tmp); | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =head1 list_lines [from_line [to_line]] | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | This is a debug function, it will list all current lines to stdout. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 4 | ▶ | sub list_lines { | ||||||||||||||||||||||||||
|
Subroutine
list_lines: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 302 | 4 | my ($from, $to) = @_; | |||||||||||||||||||||||||||
| 303 | 4 | my ($i); | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 4 | my $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 306 | 4 | ▶ | $from = 0 if $from eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 4 | ▶ | $to = @$lines if $to eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 4 | ▶ | for ($i = $from; $i < $to; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 65 | printf STDOUT "%3d: %s\n", $i, $lines->[$i]; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | ###################################################################### | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =head1 get_line from_line [to_line] | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | B<get_line> will return a pointer to an array with the | ||||||||||||||||||||||||||||
| 318 | lines from I<from_line> up to, not including I<to_line>, | ||||||||||||||||||||||||||||
| 319 | or the specified line if I<to_line> isn't specified. | ||||||||||||||||||||||||||||
| 320 | The array is a copy of the real lines, any changes to this | ||||||||||||||||||||||||||||
| 321 | array will be lost. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =cut | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 0 | ▶ | sub get_line { | ||||||||||||||||||||||||||
|
Subroutine
get_line: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 326 | 0 | my ($from, $to) = @_; | |||||||||||||||||||||||||||
| 327 | 0 | my $ret = []; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 0 | ▶ | croak "no start line defined" if !defined $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | my $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 331 | 0 | ▶ | return $lines->[$from] if !defined $to; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 0 | my $i; | |||||||||||||||||||||||||||
| 334 | 0 | ▶ | for ($i = $from; $i < $to; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 0 | push(@$ret, $lines->[$i]); | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | 0 | return $ret; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | ###################################################################### | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head1 locate regexp | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | Search for I<regexp>, returns true at the first occurance. | ||||||||||||||||||||||||||||
| 344 | B<locate> always starts at the beginning. | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =cut | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | ▶ | sub locate { | ||||||||||||||||||||||||||
|
Subroutine
locate: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 349 | 0 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 350 | 0 | my ($n, $x); | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 0 | $n = $par->{'cur_line'} = &find_row(0, $regexp); | |||||||||||||||||||||||||||
| 353 | 0 | ▶ | $x = '*found*: '.$par->{'lines'}->[$n] if $n >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 0 | &verbose("locate", "'$regexp': $x"); | |||||||||||||||||||||||||||
| 355 | 0 | return $n >= 0; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =head1 incr [[-]n] | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | Increments the line pointer, negative values allowed. | ||||||||||||||||||||||||||||
| 361 | Returns the new line number. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =cut | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | sub incr { | ||||||||||||||||||||||||||
|
Subroutine
incr: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 366 | 0 | my ($n) = @_; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | ▶ | $n = 1 unless $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | carp("outside file"), return | ||||||||||||||||||||||||||||
| 370 | 0 | ▶ | if $par->{'cur_line'}+$n > @{$par->{'lines'}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 0 | $par->{'cur_line'} += $n; | |||||||||||||||||||||||||||
| 372 | 0 | &verbose("incr", "$n, cur_line: ".$par->{'cur_line'}); | |||||||||||||||||||||||||||
| 373 | 0 | $par->{'cur_line'}; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =head1 goto_top | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | Set the first line to be the current line. | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =cut | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 1 | ▶ | sub goto_top { | ||||||||||||||||||||||||||
|
Subroutine
goto_top: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 383 | 1 | $par->{'cur_line'} = 0; | |||||||||||||||||||||||||||
| 384 | 1 | &verbose("goto_top", "beginning"); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =head1 goto_end | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | Set the last line to be the current line. | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =cut | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub goto_end { | ||||||||||||||||||||||||||
|
Subroutine
goto_end: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 394 | 0 | $par->{'cur_line'} = @{$par->{'lines'}}-1; | |||||||||||||||||||||||||||
| 395 | 0 | &verbose("goto_end", "eof"); | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =head1 curr_line [N] | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | Returns the current line number. | ||||||||||||||||||||||||||||
| 401 | Sets the current line number if arg B<N> exist; | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =cut | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub curr_line { | ||||||||||||||||||||||||||
|
Subroutine
curr_line: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 406 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 0 | $par->{'cur_line'} = shift; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | 0 | return $par->{'cur_line'}; | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =head1 last_line | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Returns the last line number. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =cut | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 0 | ▶ | sub last_line { | ||||||||||||||||||||||||||
|
Subroutine
last_line: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 419 | 0 | return @{$par->{'lines'}}*1-1; | |||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | ###################################################################### | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =head1 append line1 [line2 [...]] | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Appends one or more lines after the current line. | ||||||||||||||||||||||||||||
| 426 | The line pointer will point to the last appended line. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =cut | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 3 | ▶ | sub append { | ||||||||||||||||||||||||||
|
Subroutine
append: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 431 | 3 | my @new = &split_cr(@_); | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 3 | ▶ | &verbose("append", '**** start') if @new > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 3 | splice(@{$par->{'lines'}}, $par->{'cur_line'}+1, 0, @new); | |||||||||||||||||||||||||||
| 435 | 3 | $par->{'cur_line'} += @new; | |||||||||||||||||||||||||||
| 436 | 3 | &verbose("append", @new, "**** end"); | |||||||||||||||||||||||||||
| 437 | 3 | ++$par->{'edit'}; | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =head1 insert line1 [line2 [...]] | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | Inserts one or more lines before the current line. | ||||||||||||||||||||||||||||
| 443 | The line pointer will point to the first inserted line. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =cut | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 1 | ▶ | sub insert { | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 448 | 1 | my @new = &split_cr(@_); | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 1 | ▶ | &verbose("insert", '**** start') if @new > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | 1 | splice(@{$par->{'lines'}}, $par->{'cur_line'}, 0, @new); | |||||||||||||||||||||||||||
| 452 | 1 | &verbose("insert", @new, "**** end, cur_line: ".$par->{'cur_line'}); | |||||||||||||||||||||||||||
| 453 | 1 | ++$par->{'edit'}; | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | ###################################################################### | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =head1 add_list prefix listpointer [separator [newlinestring [suffix [length]]]] | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | Append a list after the current line. | ||||||||||||||||||||||||||||
| 461 | The result is one or more lines starting with B<prefix> and | ||||||||||||||||||||||||||||
| 462 | the list joined by B<separator>. If the line is too long it will | ||||||||||||||||||||||||||||
| 463 | be broken into several separated by B<newlinestring>. | ||||||||||||||||||||||||||||
| 464 | Default for B<separator> is 'B<:>' and for B<newlinestring> 'B<\\\n>'. | ||||||||||||||||||||||||||||
| 465 | Default for B<length> is 75. | ||||||||||||||||||||||||||||
| 466 | Example: | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | @list = qw(/bin /usr/bin /usr/local/bin); | ||||||||||||||||||||||||||||
| 469 | add_list PATH= \@list; | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | will look like this: | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | PATH=/bin:/usr/bin:/usr/local/bin | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =cut | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 0 | ▶ | sub add_list { | ||||||||||||||||||||||||||
|
Subroutine
add_list: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 478 | 0 | my ($prefix, $list, $sep, $newl, $suffix, $max) = @_; | |||||||||||||||||||||||||||
| 479 | 0 | my ($str, $newstr, $new, $cnt, @new); | |||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 0 | ▶ | $max = 75 unless $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | 0 | ▶ | $newl = "\\\n" unless $newl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 483 | 0 | ▶ | $sep = ':' unless $sep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | 0 | $str = $prefix; | |||||||||||||||||||||||||||
| 485 | 0 | my $i; | |||||||||||||||||||||||||||
| 486 | 0 | ▶ | for ($i = 0; $i < @$list; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 487 | 0 | $new = $list->[$i]; | |||||||||||||||||||||||||||
| 488 | 0 | ▶ | $str .= $sep if $cnt++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 489 | 0 | ▶ | if (length($str)+length($new) > $max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | 0 | push(@new, $str); | |||||||||||||||||||||||||||
| 491 | 0 | $str = ''; | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | 0 | $str .= $new; | |||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | 0 | ▶ | push(@new, $str) if $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 496 | 0 | $newstr = join($newl, @new); | |||||||||||||||||||||||||||
| 497 | 0 | $newstr .= $suffix; | |||||||||||||||||||||||||||
| 498 | 0 | &verbose("add_list", $newstr); | |||||||||||||||||||||||||||
| 499 | 0 | &append($newstr); | |||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | ###################################################################### | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | =head1 delete_all regexp | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | Deletes all lines containing B</regexp/>. | ||||||||||||||||||||||||||||
| 507 | Returns the deleted lines. | ||||||||||||||||||||||||||||
| 508 | If B<regexp> is a pointer to a function or a anonymous function, it | ||||||||||||||||||||||||||||
| 509 | will be executed with current line number and a pointer to the | ||||||||||||||||||||||||||||
| 510 | current line as argument. The line will be deleted if the function | ||||||||||||||||||||||||||||
| 511 | returns true. | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | =cut | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | 1 | ▶ | sub delete_all { | ||||||||||||||||||||||||||
|
Subroutine
delete_all: called CC=5 SCAR=17.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 516 | 1 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 517 | 1 | my (@del, $lines, $new); | |||||||||||||||||||||||||||
| 518 | 1 | my $line = 0; | |||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | 1 | &verbose("delete_all", "**** $regexp"); | |||||||||||||||||||||||||||
| 521 | 1 | $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 522 | 1 | $new = []; | |||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 1 | ▶ | for (my $i = 0; $i < @$lines; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 19 | $_ = $lines->[$i]; | |||||||||||||||||||||||||||
| 526 | 19 | ▶ | if (ref($regexp) =~ /CODE/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | 0 | my $del = &$regexp($i, \$lines->[$i]); | |||||||||||||||||||||||||||
| 528 | 0 | ▶ | if ($del) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 529 | 0 | &verbose("deleted", "$i:$_"); | |||||||||||||||||||||||||||
| 530 | 0 | push(@del, $_); | |||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||
| 532 | else { | ||||||||||||||||||||||||||||
| 533 | 0 | push(@$new, $_); | |||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||
| 535 | 0 | next; | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | 19 | ▶ | unless (/$regexp/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 538 | 16 | push(@$new, $_); | |||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||
| 540 | else { | ||||||||||||||||||||||||||||
| 541 | 3 | &verbose("deleted", "$i:$_"); | |||||||||||||||||||||||||||
| 542 | 3 | push(@del, $_); | |||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||
| 545 | 1 | $par->{'lines'} = $new; | |||||||||||||||||||||||||||
| 546 | 1 | $par->{'edit'} += @del*1; | |||||||||||||||||||||||||||
| 547 | 1 | return @del; | |||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | =head1 delete_to regexp | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | Deletes all lines from the current to the line | ||||||||||||||||||||||||||||
| 553 | matching B<regexp>. | ||||||||||||||||||||||||||||
| 554 | Returns '' if regexp not found, otherwhise returns the | ||||||||||||||||||||||||||||
| 555 | deleted lines. | ||||||||||||||||||||||||||||
| 556 | Current line will be the line preceding the deleted area. | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | =cut | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | 0 | ▶ | sub delete_to { | ||||||||||||||||||||||||||
|
Subroutine
delete_to: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 561 | 0 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 562 | 0 | my (@d, $i, $line, $cur); | |||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | 0 | my $cur = $par->{'cur_line'}; | |||||||||||||||||||||||||||
| 565 | 0 | my $last = &find_row($cur, $regexp); | |||||||||||||||||||||||||||
| 566 | 0 | ▶ | return 0 unless $last >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 0 | my @d = splice(@{$par->{'lines'}}, $cur, $last-$cur+1); | |||||||||||||||||||||||||||
| 569 | 0 | $par->{'cur_line'}--; | |||||||||||||||||||||||||||
| 570 | 0 | &verbose("delete_to", "*** $regexp", @d); | |||||||||||||||||||||||||||
| 571 | 0 | $par->{'edit'} += @d*1; | |||||||||||||||||||||||||||
| 572 | 0 | return @d; | |||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | =head1 delete_n N | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | Deletes B<N> lines from the current. | ||||||||||||||||||||||||||||
| 578 | Deletes to the end of file if N equal C<end>. | ||||||||||||||||||||||||||||
| 579 | Returns the deleted lines; | ||||||||||||||||||||||||||||
| 580 | Current line will be the line preceding the deleted area. | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | =cut | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 0 | ▶ | sub delete_n { | ||||||||||||||||||||||||||
|
Subroutine
delete_n: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 585 | 0 | my ($n) = @_; | |||||||||||||||||||||||||||
| 586 | 0 | my (@d); | |||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | 0 | ▶ | $n = &last_line+1 if $n eq 'end'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 589 | 0 | @d = splice(@{$par->{'lines'}}, $par->{'cur_line'}, $n); | |||||||||||||||||||||||||||
| 590 | 0 | &verbose("delete_n", $n, @d); | |||||||||||||||||||||||||||
| 591 | 0 | $par->{'cur_line'}--; | |||||||||||||||||||||||||||
| 592 | 0 | return @d; | |||||||||||||||||||||||||||
| 593 | 0 | $par->{'edit'}++; | |||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | =head1 delete_file | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | Deletes all lines. | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | =cut | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | 0 | ▶ | sub delete_file { | ||||||||||||||||||||||||||
|
Subroutine
delete_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 603 | 0 | $par->{'lines'} = []; | |||||||||||||||||||||||||||
| 604 | 0 | $par->{'edit'}++; | |||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | ###################################################################### | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | =head1 find_str regexp | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | B<find_str> returns true if any line matching B<regexp> is found. | ||||||||||||||||||||||||||||
| 612 | Current line is not changed. | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | =cut | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | 0 | ▶ | sub find_str { | ||||||||||||||||||||||||||
|
Subroutine
find_str: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 617 | 0 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | 0 | return &find_row(0, $regexp) >= 0; | |||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | ###################################################################### | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =head1 eval_to regexp exp | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | B<eval_to> will evaluate B<exp> on each line up to the line | ||||||||||||||||||||||||||||
| 627 | matching B<regexp>. | ||||||||||||||||||||||||||||
| 628 | Returns true if any line was changed. | ||||||||||||||||||||||||||||
| 629 | B<exp> can be any perl expression like C<'s/bin/usr/'>. | ||||||||||||||||||||||||||||
| 630 | If B<exp> is a pointer to a function or a anonymous function, it | ||||||||||||||||||||||||||||
| 631 | will be executed with current line number and a pointer to the | ||||||||||||||||||||||||||||
| 632 | current line as argument. | ||||||||||||||||||||||||||||
| 633 | Changes to the current line will be preserved. | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | =cut | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | 0 | ▶ | sub eval_to { | ||||||||||||||||||||||||||
|
Subroutine
eval_to: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 638 | 0 | my ($regexp, $eval) = @_; | |||||||||||||||||||||||||||
| 639 | 0 | my ($edit, $i, $line, $cur, $last); | |||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | 0 | $cur = $par->{'cur_line'}; | |||||||||||||||||||||||||||
| 642 | 0 | $last = &find_row($cur, $regexp); | |||||||||||||||||||||||||||
| 643 | 0 | ▶ | carp "can't find $regexp" unless $last >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | 0 | my $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 646 | 0 | &verbose("eval_to", "$regexp, $eval"); | |||||||||||||||||||||||||||
| 647 | 0 | ▶ | for ($i = $cur; $i <= $last; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 648 | 0 | ▶ | if (eval "\$par->{'lines'}->[\$i] =~ $eval") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 649 | 0 | ▶ | if (ref($eval) =~ /CODE/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 650 | 0 | my $tline = $lines->[$i]; | |||||||||||||||||||||||||||
| 651 | 0 | &$eval($i, \$lines->[$i]); | |||||||||||||||||||||||||||
| 652 | 0 | ▶ | if ($tline ne $lines->[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 653 | 0 | &verbose("exec", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 654 | 0 | $edit++; | |||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||
| 656 | 0 | next; | |||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||
| 658 | 0 | $edit++; | |||||||||||||||||||||||||||
| 659 | 0 | &verbose("eval", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | 0 | $par->{'edit'} += $edit; | |||||||||||||||||||||||||||
| 663 | 0 | return $edit; | |||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | =head1 eval_all exp | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | B<eval_all> will evaluate B<exp> on each line in the file. | ||||||||||||||||||||||||||||
| 669 | Returns true if any line was changed. | ||||||||||||||||||||||||||||
| 670 | See B<eval_to>. | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | =cut | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | 0 | ▶ | sub eval_all { | ||||||||||||||||||||||||||
|
Subroutine
eval_all: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 675 | 0 | my ($eval) = @_; | |||||||||||||||||||||||||||
| 676 | 0 | my ($edit, $i, $line, $cur, $last); | |||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | 0 | my $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 679 | 0 | &verbose("eval_all", "**** $eval"); | |||||||||||||||||||||||||||
| 680 | 0 | ▶ | for ($i = 0; $i < @$lines; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 681 | 0 | ▶ | if (ref($eval) =~ /CODE/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 682 | 0 | my $tline = $lines->[$i]; | |||||||||||||||||||||||||||
| 683 | 0 | &$eval($i, \$lines->[$i]); | |||||||||||||||||||||||||||
| 684 | 0 | ▶ | if ($tline ne $lines->[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 685 | 0 | &verbose("exec", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 686 | 0 | $edit++; | |||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||
| 688 | 0 | next; | |||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||
| 690 | elsif (eval "\$lines->[\$i] =~ $eval") { | ||||||||||||||||||||||||||||
| 691 | 0 | &verbose("eval", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 692 | 0 | $edit++; | |||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||
| 695 | 0 | $par->{'edit'} += $edit; | |||||||||||||||||||||||||||
| 696 | 0 | return $edit; | |||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | =head1 eval_n N exp | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | B<eval_n> will evaluate B<exp> on B<N> lines from the current line. | ||||||||||||||||||||||||||||
| 702 | Returns true if any line was changed. | ||||||||||||||||||||||||||||
| 703 | A value of 0 will change the current line, 1 will change the current | ||||||||||||||||||||||||||||
| 704 | and the next line. | ||||||||||||||||||||||||||||
| 705 | See B<eval_to>. | ||||||||||||||||||||||||||||
| 706 | Evaluates to the end of file if N equal C<end>. | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | =cut | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | 0 | ▶ | sub eval_n { | ||||||||||||||||||||||||||
|
Subroutine
eval_n: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 711 | 0 | my ($n, $eval) = @_; | |||||||||||||||||||||||||||
| 712 | 0 | my ($edit, $i, $line, $cur, $last, $start); | |||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | 0 | ▶ | $n = &last_line if $n eq 'end'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 715 | 0 | my $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 716 | 0 | $start = $par->{'cur_line'}; | |||||||||||||||||||||||||||
| 717 | 0 | $last = $start+$n; | |||||||||||||||||||||||||||
| 718 | 0 | ▶ | $last = @$lines-1 if $last >= @$lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 719 | 0 | &verbose("eval_n", "**** $n,$eval: $start-$last"); | |||||||||||||||||||||||||||
| 720 | 0 | ▶ | for ($i = $start; $i <= $last; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 721 | 0 | ▶ | if (ref($eval) =~ /CODE/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 722 | 0 | my $tline = $lines->[$i]; | |||||||||||||||||||||||||||
| 723 | 0 | &$eval($i, \$lines->[$i]); | |||||||||||||||||||||||||||
| 724 | 0 | ▶ | if ($tline ne $lines->[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 725 | 0 | &verbose("exec", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 726 | 0 | $edit++; | |||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||
| 728 | 0 | next; | |||||||||||||||||||||||||||
| 729 | } | ||||||||||||||||||||||||||||
| 730 | 0 | ▶ | if (eval "\$lines->[\$i] =~ $eval") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 731 | 0 | &verbose("eval_n", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 732 | 0 | $edit++; | |||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||
| 735 | 0 | $par->{'edit'} += $edit; | |||||||||||||||||||||||||||
| 736 | 0 | return $edit; | |||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||
| 739 | =head1 eval_where regexp1 exp1 [regexp2 exp2 [...]] | ||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | B<eval_where> will evaluate B<exp> for each line | ||||||||||||||||||||||||||||
| 742 | matching B<regexp>. | ||||||||||||||||||||||||||||
| 743 | Returns true if any line was changed. | ||||||||||||||||||||||||||||
| 744 | See B<eval_to>. | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | =cut | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | 1 | ▶ | sub eval_where { | ||||||||||||||||||||||||||
|
Subroutine
eval_where: called CC=8 SCAR=21.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 749 | 1 | my ($i, $line, $cur, $last); | |||||||||||||||||||||||||||
| 750 | 1 | my ($eval, $regexp, $edit); | |||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | 1 | my $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 753 | 1 | &verbose("eval_where", '**** start'); | |||||||||||||||||||||||||||
| 754 | 1 | ▶ | while(($regexp = shift) && ($eval = shift)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$regexp = shift() and $eval = shift()
MC/DC50%1/2
$regexp = shift() and $eval = shift()
$regexp = shift()
$eval = shift()
Truth table66%2/3
A and B
A= $regexp = shift()B= $eval = shift()
| |||||||||||||||||||||||||||||
| 755 | 1 | &verbose("eval", "$regexp, $eval"); | |||||||||||||||||||||||||||
| 756 | 1 | ▶ | for ($i = 0; $i < @$lines; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 757 | 16 | ▶ | if ($lines->[$i] =~ /$regexp/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 758 | 3 | ▶ | if (ref($eval) =~ /CODE/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 759 | 3 | my $tline = $lines->[$i]; | |||||||||||||||||||||||||||
| 760 | 3 | &$eval($i, \$lines->[$i]); | |||||||||||||||||||||||||||
| 761 | 3 | ▶ | if ($tline ne $lines->[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 762 | 3 | &verbose("exec", | |||||||||||||||||||||||||||
| 763 | "$i: ".$lines->[$i]); | ||||||||||||||||||||||||||||
| 764 | 3 | $edit++; | |||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||
| 766 | 3 | next; | |||||||||||||||||||||||||||
| 767 | } | ||||||||||||||||||||||||||||
| 768 | 0 | ▶ | if (eval "\$lines->[\$i] =~ $eval") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 769 | 0 | &verbose("eval", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 770 | 0 | $edit++; | |||||||||||||||||||||||||||
| 771 | } | ||||||||||||||||||||||||||||
| 772 | } | ||||||||||||||||||||||||||||
| 773 | } | ||||||||||||||||||||||||||||
| 774 | } | ||||||||||||||||||||||||||||
| 775 | 1 | $par->{'edit'} += $edit; | |||||||||||||||||||||||||||
| 776 | 1 | return $edit; | |||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||
| 779 | ###################################################################### | ||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | =head1 replace_all from1 to1 [from2 to2 [...]] | ||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | B<replace_all> replaces all occurrences of the string | ||||||||||||||||||||||||||||
| 784 | B<from> with the string B<to>. | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | =cut | ||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||
| 788 | 0 | ▶ | sub replace_all { | ||||||||||||||||||||||||||
|
Subroutine
replace_all: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 789 | 0 | my ($from, $to, $i); | |||||||||||||||||||||||||||
| 790 | 0 | my ($edit, $lines); | |||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | 0 | &verbose("replace_all", '**** start'); | |||||||||||||||||||||||||||
| 793 | 0 | ▶ | while(($from = shift) && ($to = shift)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$from = shift() and $to = shift()
MC/DC0%0/2
$from = shift() and $to = shift()
$from = shift()
$to = shift()
Truth table0%0/3
A and B
A= $from = shift()B= $to = shift()
| |||||||||||||||||||||||||||||
| 794 | 0 | &verbose("replace_all", "$from -> $to"); | |||||||||||||||||||||||||||
| 795 | 0 | $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 796 | 0 | ▶ | for ($i = 0; $i < @$lines; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 797 | 0 | ▶ | if ($lines->[$i] =~ s/$from/$to/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 798 | 0 | &verbose("replace_all", "$i: ".$lines->[$i]); | |||||||||||||||||||||||||||
| 799 | 0 | $edit++; | |||||||||||||||||||||||||||
| 800 | } | ||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||
| 802 | } | ||||||||||||||||||||||||||||
| 803 | 0 | $par->{'edit'} += $edit; | |||||||||||||||||||||||||||
| 804 | 0 | return $edit; | |||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | ###################################################################### | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | =head1 set_comment [start_comment [end_comment]] | ||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | B<set_comment> sets the current comment strings. | ||||||||||||||||||||||||||||
| 812 | B<start_comment> defaults to B<'# '>, | ||||||||||||||||||||||||||||
| 813 | B<end_comment> defaults to B<''>. | ||||||||||||||||||||||||||||
| 814 | Using B<set_comment> whithout arguments will set the | ||||||||||||||||||||||||||||
| 815 | default comment strings again. | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | =cut | ||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||
| 819 | 3 | ▶ | sub set_comment { | ||||||||||||||||||||||||||
|
Subroutine
set_comment: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 820 | 3 | my ($cstart, $cend) = @_; | |||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | 3 | ▶ | $cstart = $par->{'cstart'} = '# ' unless $cstart; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 823 | 3 | ▶ | $cend = $par->{'cend'} = '' unless $cend; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 824 | 3 | &verbose("set_comment", "$par->{'cstart'}, $par->{'cend'}"); | |||||||||||||||||||||||||||
| 825 | 3 | my $x = ', do not change this line!'; | |||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||
| 827 | 3 | $par->{'begin_sect'} = $cstart.'CFE begin <%s>'.$x; | |||||||||||||||||||||||||||
| 828 | 3 | &verbose("set_comment", $par->{'begin_sect'}); | |||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||
| 830 | 3 | $par->{'end_sect'} = $cstart.'CFE end <%s>'.$x; | |||||||||||||||||||||||||||
| 831 | 3 | &verbose("set_comment", $par->{'end_sect'}); | |||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||
| 833 | 3 | $par->{'find_sect'} = '^'.quotemeta($cstart).'CFE %s <%s>'; | |||||||||||||||||||||||||||
| 834 | 3 | &verbose("set_comment", $par->{'find_sect'}); | |||||||||||||||||||||||||||
| 835 | } | ||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||
| 837 | =head1 comment_where regexp | ||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||
| 839 | B<comment_where> inserts a comment in the beginning of each | ||||||||||||||||||||||||||||
| 840 | line matching B<regexp>. | ||||||||||||||||||||||||||||
| 841 | See also B<set_comment>. | ||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||
| 843 | =cut | ||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||
| 845 | 0 | ▶ | sub comment_where { | ||||||||||||||||||||||||||
|
Subroutine
comment_where: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 846 | 0 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 847 | 0 | my ($cstart, $cend); | |||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||
| 849 | 0 | &verbose("comment_where", $regexp); | |||||||||||||||||||||||||||
| 850 | 0 | $cstart = $par->{'cstart'}; | |||||||||||||||||||||||||||
| 851 | 0 | $cend = $par->{'cend'}; | |||||||||||||||||||||||||||
| 852 | 0 | return &eval_where($regexp, "s/^(.*)\$/$cstart\$1$cend/"); | |||||||||||||||||||||||||||
| 853 | } | ||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||
| 855 | =head1 comment_to regexp | ||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||
| 857 | B<comment_to> inserts a comment in the beginning of each | ||||||||||||||||||||||||||||
| 858 | line from the current line up to the line | ||||||||||||||||||||||||||||
| 859 | matching B<regexp>. | ||||||||||||||||||||||||||||
| 860 | See also B<set_comment>. | ||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||
| 862 | =cut | ||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | 0 | ▶ | sub comment_to { | ||||||||||||||||||||||||||
|
Subroutine
comment_to: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 865 | 0 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 866 | 0 | my ($cstart, $cend); | |||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||
| 868 | 0 | &verbose("comment_to", $regexp); | |||||||||||||||||||||||||||
| 869 | 0 | $cstart = $par->{'cstart'}; | |||||||||||||||||||||||||||
| 870 | 0 | $cend = $par->{'cend'}; | |||||||||||||||||||||||||||
| 871 | 0 | return &eval_to($regexp, "s/^(.*)\$/$cstart\$1$cend/"); | |||||||||||||||||||||||||||
| 872 | } | ||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||
| 874 | =head1 comment_n N | ||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||
| 876 | B<comment_n> inserts a comment in the beginning of B<N> | ||||||||||||||||||||||||||||
| 877 | lines starting with the current line. | ||||||||||||||||||||||||||||
| 878 | See also B<set_comment>. | ||||||||||||||||||||||||||||
| 879 | Comments to the end of file if N equal C<end>. | ||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||
| 881 | =cut | ||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||
| 883 | 0 | ▶ | sub comment_n { | ||||||||||||||||||||||||||
|
Subroutine
comment_n: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 884 | 0 | my ($n) = @_; | |||||||||||||||||||||||||||
| 885 | 0 | my ($cstart, $cend); | |||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||
| 887 | 0 | &verbose("comment_n", $n); | |||||||||||||||||||||||||||
| 888 | 0 | ▶ | $n = last_line if $n eq 'end'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 889 | 0 | $cstart = $par->{'cstart'}; | |||||||||||||||||||||||||||
| 890 | 0 | $cend = $par->{'cend'}; | |||||||||||||||||||||||||||
| 891 | 0 | return &eval_n($n, "s/^(.*)\$/$cstart\$1$cend/"); | |||||||||||||||||||||||||||
| 892 | } | ||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||
| 894 | =head1 uncomment_where regexp | ||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||
| 896 | B<uncomment_where> removes the comment in the beginning of each | ||||||||||||||||||||||||||||
| 897 | line matching B<regexp>. | ||||||||||||||||||||||||||||
| 898 | See also B<set_comment>. | ||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||
| 900 | =cut | ||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||
| 902 | 0 | ▶ | sub uncomment_where { | ||||||||||||||||||||||||||
|
Subroutine
uncomment_where: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 903 | 0 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 904 | 0 | my ($cstart, $cend); | |||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | 0 | &verbose("uncomment_where", $regexp); | |||||||||||||||||||||||||||
| 907 | 0 | $cstart = $par->{'cstart'}; | |||||||||||||||||||||||||||
| 908 | 0 | $cend = $par->{'cend'}; | |||||||||||||||||||||||||||
| 909 | 0 | return &eval_where($regexp, "s/^$cstart(.*)$cend\$/\$1/"); | |||||||||||||||||||||||||||
| 910 | } | ||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||
| 912 | =head1 uncomment_to regexp | ||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||
| 914 | B<uncomment_to> removes the comment in the beginning of each | ||||||||||||||||||||||||||||
| 915 | line from the current line up to the line | ||||||||||||||||||||||||||||
| 916 | matching B<regexp>. | ||||||||||||||||||||||||||||
| 917 | See also B<set_comment>. | ||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||
| 919 | =cut | ||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||
| 921 | 0 | ▶ | sub uncomment_to { | ||||||||||||||||||||||||||
|
Subroutine
uncomment_to: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 922 | 0 | my ($regexp) = @_; | |||||||||||||||||||||||||||
| 923 | 0 | my ($cstart, $cend); | |||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||
| 925 | 0 | &verbose("uncomment_to", $regexp); | |||||||||||||||||||||||||||
| 926 | 0 | $cstart = $par->{'cstart'}; | |||||||||||||||||||||||||||
| 927 | 0 | $cend = $par->{'cend'}; | |||||||||||||||||||||||||||
| 928 | 0 | return &eval_to($regexp, "s/^$cstart(.*)$cend\$/\$1/"); | |||||||||||||||||||||||||||
| 929 | } | ||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||
| 931 | =head1 uncomment_n N | ||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||
| 933 | B<comment_n> inserts a comment in the beginning of B<N> | ||||||||||||||||||||||||||||
| 934 | lines starting with the current line. | ||||||||||||||||||||||||||||
| 935 | See also B<set_comment> and B<eval_n>. | ||||||||||||||||||||||||||||
| 936 | Uncomments to the end of file if N equal C<end>. | ||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||
| 938 | =cut | ||||||||||||||||||||||||||||
| 939 | |||||||||||||||||||||||||||||
| 940 | 0 | ▶ | sub uncomment_n { | ||||||||||||||||||||||||||
|
Subroutine
uncomment_n: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 941 | 0 | my ($n) = @_; | |||||||||||||||||||||||||||
| 942 | 0 | my ($cstart, $cend); | |||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||
| 944 | 0 | &verbose("uncomment_n", $n); | |||||||||||||||||||||||||||
| 945 | 0 | ▶ | $n = last_line if $n eq 'end'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 946 | 0 | $cstart = $par->{'cstart'}; | |||||||||||||||||||||||||||
| 947 | 0 | $cend = $par->{'cend'}; | |||||||||||||||||||||||||||
| 948 | 0 | return &eval_n($n, "s/^$cstart(.*)$cend\$/\$1/"); | |||||||||||||||||||||||||||
| 949 | } | ||||||||||||||||||||||||||||
| 950 | ###################################################################### | ||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||
| 952 | =head1 append_sect section | ||||||||||||||||||||||||||||
| 953 | |||||||||||||||||||||||||||||
| 954 | Start appending a new section by adding a B<CFE> tag named | ||||||||||||||||||||||||||||
| 955 | I<section>. | ||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||
| 957 | =cut | ||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||
| 959 | 1 | ▶ | sub append_sect { | ||||||||||||||||||||||||||
|
Subroutine
append_sect: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 960 | 1 | my ($section) = @_; | |||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||
| 962 | 1 | &verbose("append_sect", $section); | |||||||||||||||||||||||||||
| 963 | 1 | &append(sprintf($par->{'begin_sect'}, $section)); | |||||||||||||||||||||||||||
| 964 | 1 | $par->{'cur_sect'} = $section; | |||||||||||||||||||||||||||
| 965 | } | ||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||
| 967 | =head1 insert_sect section | ||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||
| 969 | Start inserting a new section | ||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||
| 971 | =cut | ||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||
| 973 | 0 | ▶ | sub insert_sect { | ||||||||||||||||||||||||||
|
Subroutine
insert_sect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 974 | 0 | my ($section) = @_; | |||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||
| 976 | 0 | &verbose("start_sect", $section); | |||||||||||||||||||||||||||
| 977 | 0 | &insert(sprintf($par->{'begin_sect'}, $section)); | |||||||||||||||||||||||||||
| 978 | 0 | $par->{'cur_sect'} = $section; | |||||||||||||||||||||||||||
| 979 | } | ||||||||||||||||||||||||||||
| 980 | 0 | ▶ | sub start_sect { | ||||||||||||||||||||||||||
|
Subroutine
start_sect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 981 | 0 | &insert_sect($_[0]); | |||||||||||||||||||||||||||
| 982 | } | ||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||
| 984 | =head1 end_sect | ||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||
| 986 | Appends a comment at current line that marks the end of the section. | ||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||
| 988 | =cut | ||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||
| 990 | 1 | ▶ | sub end_sect { | ||||||||||||||||||||||||||
|
Subroutine
end_sect: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 991 | 1 | ▶ | carp "No active section!" unless $par->{'cur_sect'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 992 | 1 | &verbose("end_sect", '***'); | |||||||||||||||||||||||||||
| 993 | 1 | &append(sprintf($par->{'end_sect'}, $par->{'cur_sect'})); | |||||||||||||||||||||||||||
| 994 | 1 | $par->{'cur_sect'} = ''; | |||||||||||||||||||||||||||
| 995 | } | ||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||
| 997 | =head1 delete_sect section | ||||||||||||||||||||||||||||
| 998 | |||||||||||||||||||||||||||||
| 999 | Deletes the section name B<section>. | ||||||||||||||||||||||||||||
| 1000 | Returns true if section actually was deleted. | ||||||||||||||||||||||||||||
| 1001 | Will set the current line. | ||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||
| 1003 | =cut | ||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||
| 1005 | 0 | ▶ | sub delete_sect { | ||||||||||||||||||||||||||
|
Subroutine
delete_sect: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1006 | 0 | my ($section) = @_; | |||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||
| 1008 | 0 | &verbose("delete_sect", $section); | |||||||||||||||||||||||||||
| 1009 | 0 | ▶ | return 0 unless &locate(sprintf($par->{'find_sect'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1010 | 'begin', $section)); | ||||||||||||||||||||||||||||
| 1011 | 0 | return &delete_to(sprintf($par->{'find_sect'}, 'end', $section)); | |||||||||||||||||||||||||||
| 1012 | } | ||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||
| 1014 | =head1 find_sect section | ||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||
| 1016 | Returns true if B<section> exist. | ||||||||||||||||||||||||||||
| 1017 | Will set the current line. | ||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||
| 1019 | =cut | ||||||||||||||||||||||||||||
| 1020 | |||||||||||||||||||||||||||||
| 1021 | 0 | ▶ | sub find_sect { | ||||||||||||||||||||||||||
|
Subroutine
find_sect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1022 | 0 | my ($section) = @_; | |||||||||||||||||||||||||||
| 1023 | |||||||||||||||||||||||||||||
| 1024 | 0 | &verbose("find_sect", $section); | |||||||||||||||||||||||||||
| 1025 | 0 | return &locate(sprintf($par->{'find_sect'}, 'begin', $section)); | |||||||||||||||||||||||||||
| 1026 | } | ||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||
| 1028 | =head1 comment_sect section | ||||||||||||||||||||||||||||
| 1029 | |||||||||||||||||||||||||||||
| 1030 | Comment out the section name B<section>. | ||||||||||||||||||||||||||||
| 1031 | Will set the current line. | ||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||
| 1033 | =cut | ||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||
| 1035 | 0 | ▶ | sub comment_sect { | ||||||||||||||||||||||||||
|
Subroutine
comment_sect: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1036 | 0 | my ($section) = @_; | |||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||
| 1038 | 0 | &verbose("comment_sect", $section); | |||||||||||||||||||||||||||
| 1039 | 0 | ▶ | return 0 unless &locate(sprintf($par->{'find_sect'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1040 | 'begin', $section)); | ||||||||||||||||||||||||||||
| 1041 | 0 | my $from = &incr(); | |||||||||||||||||||||||||||
| 1042 | my $to = &find_row($from, | ||||||||||||||||||||||||||||
| 1043 | 0 | sprintf($par->{'find_sect'}, 'end', $section)); | |||||||||||||||||||||||||||
| 1044 | 0 | ▶ | return unless $to >= $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1045 | 0 | return &comment_n($to-$from-1); | |||||||||||||||||||||||||||
| 1046 | } | ||||||||||||||||||||||||||||
| 1047 | |||||||||||||||||||||||||||||
| 1048 | =head1 uncomment_sect section | ||||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||
| 1050 | Enable the section name B<section> by removing the comment string | ||||||||||||||||||||||||||||
| 1051 | at the beginning of the lines. Will set the current line. | ||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||
| 1053 | =cut | ||||||||||||||||||||||||||||
| 1054 | |||||||||||||||||||||||||||||
| 1055 | 0 | ▶ | sub uncomment_sect { | ||||||||||||||||||||||||||
|
Subroutine
uncomment_sect: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1056 | 0 | my ($section) = @_; | |||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||
| 1058 | 0 | &verbose("uncomment_sect", $section); | |||||||||||||||||||||||||||
| 1059 | 0 | ▶ | return 0 unless &locate(sprintf($par->{'find_sect'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1060 | 'begin', $section)); | ||||||||||||||||||||||||||||
| 1061 | 0 | my $from = &incr(); | |||||||||||||||||||||||||||
| 1062 | my $to = &find_row($from, | ||||||||||||||||||||||||||||
| 1063 | 0 | sprintf($par->{'find_sect'}, 'end', $section)); | |||||||||||||||||||||||||||
| 1064 | 0 | ▶ | return unless $to >= $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1065 | 0 | return &uncomment_n($to-$from-1); | |||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||
| 1067 | } | ||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||
| 1069 | =head1 get_sect section | ||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||
| 1071 | Fetch the content of section named B<section>, return a pointer | ||||||||||||||||||||||||||||
| 1072 | to an anonymous array. | ||||||||||||||||||||||||||||
| 1073 | Will set the current line. | ||||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||
| 1075 | =cut | ||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||
| 1077 | 0 | ▶ | sub get_sect { | ||||||||||||||||||||||||||
|
Subroutine
get_sect: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1078 | 0 | my ($section) = @_; | |||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||
| 1080 | 0 | &verbose("get_sect", $section); | |||||||||||||||||||||||||||
| 1081 | 0 | ▶ | return 0 unless &locate(sprintf($par->{'find_sect'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1082 | 'begin', $section)); | ||||||||||||||||||||||||||||
| 1083 | 0 | my $from = &incr(); | |||||||||||||||||||||||||||
| 1084 | my $to = &find_row($from, | ||||||||||||||||||||||||||||
| 1085 | 0 | sprintf($par->{'find_sect'}, 'end', $section)); | |||||||||||||||||||||||||||
| 1086 | 0 | ▶ | return [] unless $to >= $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1087 | 0 | return &get_line($from, $to); | |||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||
| 1089 | } | ||||||||||||||||||||||||||||
| 1090 | ###################################################################### | ||||||||||||||||||||||||||||
| 1091 | # Internal subroutine | ||||||||||||||||||||||||||||
| 1092 | # start searching from line 'start' for 'regexp'; | ||||||||||||||||||||||||||||
| 1093 | # Returns the line number or -1 if not found. | ||||||||||||||||||||||||||||
| 1094 | 0 | ▶ | sub find_row { | ||||||||||||||||||||||||||
|
Subroutine
find_row: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1095 | 0 | my ($start, $regexp) = @_; | |||||||||||||||||||||||||||
| 1096 | 0 | my ($i, $line); | |||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||
| 1098 | 0 | my $lines = $par->{'lines'}; | |||||||||||||||||||||||||||
| 1099 | 0 | my $l = -1; | |||||||||||||||||||||||||||
| 1100 | 0 | ▶ | for ($i = $start; $i < @$lines; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1101 | 0 | ▶ | $l = $i, last if $lines->[$i] =~ /$regexp/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1102 | } | ||||||||||||||||||||||||||||
| 1103 | 0 | return $l; | |||||||||||||||||||||||||||
| 1104 | } | ||||||||||||||||||||||||||||
| 1105 | # splits any multilined string and returns an array of | ||||||||||||||||||||||||||||
| 1106 | # guaranteed single lines. | ||||||||||||||||||||||||||||
| 1107 | 4 | ▶ | sub split_cr { | ||||||||||||||||||||||||||
|
Subroutine
split_cr: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1108 | 4 | my ($i, @new); | |||||||||||||||||||||||||||
| 1109 | 4 | for my $i (@_) { | |||||||||||||||||||||||||||
| 1110 | 5 | ▶ | push(@new, ''), next if $i eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1111 | 4 | push(@new, split(/\n/, $i)); | |||||||||||||||||||||||||||
| 1112 | } | ||||||||||||||||||||||||||||
| 1113 | 4 | return @new; | |||||||||||||||||||||||||||
| 1114 | } | ||||||||||||||||||||||||||||
| 1115 | # Print out debug info if verbose is active. | ||||||||||||||||||||||||||||
| 1116 | # | ||||||||||||||||||||||||||||
| 1117 | 42 | ▶ | sub verbose { | ||||||||||||||||||||||||||
|
Subroutine
verbose: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1118 | 42 | my ($func, @txt) = @_; | |||||||||||||||||||||||||||
| 1119 | 42 | my ($l, $i); | |||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||
| 1121 | |||||||||||||||||||||||||||||
| 1122 | 42 | ▶ | return if $verbose == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1123 | 14 | $l = $par->{'cur_line'}; | |||||||||||||||||||||||||||
| 1124 | 14 | for my $i (@txt) { | |||||||||||||||||||||||||||
| 1125 | 14 | ▶ | push(@verb_save, "$func($l): $i\n"), next if $verbose < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1126 | 0 | print "$func($l): $i\n"; | |||||||||||||||||||||||||||
| 1127 | } | ||||||||||||||||||||||||||||
| 1128 | } | ||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||
| 1130 | 2 | ▶ | sub reset_par { | ||||||||||||||||||||||||||
|
Subroutine
reset_par: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1131 | 2 | $par->{'max_lines'} = 100000; | |||||||||||||||||||||||||||
| 1132 | 2 | $par->{'file_name'} = ''; | |||||||||||||||||||||||||||
| 1133 | 2 | $par->{'num_lines'} = 0; | |||||||||||||||||||||||||||
| 1134 | 2 | $par->{'lines'} = []; | |||||||||||||||||||||||||||
| 1135 | 2 | $par->{'cur_line'} = 0; | |||||||||||||||||||||||||||
| 1136 | 2 | $par->{'edit'} = 0; | |||||||||||||||||||||||||||
| 1137 | 2 | $par->{'mode'} = 0666 & umask; | |||||||||||||||||||||||||||
| 1138 | 2 | $par->{'uid'} = 0; | |||||||||||||||||||||||||||
| 1139 | 2 | $par->{'gid'} = 0; | |||||||||||||||||||||||||||
| 1140 | 2 | &set_comment; | |||||||||||||||||||||||||||
| 1141 | } | ||||||||||||||||||||||||||||
| 1142 | 1 | ▶ | sub get_filemode { | ||||||||||||||||||||||||||
|
Subroutine
get_filemode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1143 | 1 | my ($f) = @_; | |||||||||||||||||||||||||||
| 1144 | |||||||||||||||||||||||||||||
| 1145 | 1 | ($par->{'mode'}, $par->{'uid'}, $par->{'gid'}) = (stat($f))[2,4,5]; | |||||||||||||||||||||||||||
| 1146 | 1 | $par->{'mode'} &= 0777; | |||||||||||||||||||||||||||
| 1147 | &verbose('get_filemode', | ||||||||||||||||||||||||||||
| 1148 | sprintf("mode=0%o, uid=%d, gid=%d", $par->{'mode'}, | ||||||||||||||||||||||||||||
| 1149 | 1 | $par->{'uid'}, $par->{'gid'})); | |||||||||||||||||||||||||||
| 1150 | } | ||||||||||||||||||||||||||||
| 1151 | |||||||||||||||||||||||||||||
| 1152 | 1 | ▶ | sub BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=14 SCAR=33.6
| |||||||||||||||||||||||||||||
| 1153 | 1 | my (%type, $uname, $r, $ostype, $version, $mach_type); | |||||||||||||||||||||||||||
| 1154 | |||||||||||||||||||||||||||||
| 1155 | 1 | $par = {}; | |||||||||||||||||||||||||||
| 1156 | 1 | &reset_par; | |||||||||||||||||||||||||||
| 1157 | 1 | chop($uname = `uname -srm`); | |||||||||||||||||||||||||||
| 1158 | 1 | ($ostype, $version, $mach_type) = split(/\s+/, $uname); | |||||||||||||||||||||||||||
| 1159 | 1 | ($r = $version) =~ tr/0-9//cd; | |||||||||||||||||||||||||||
| 1160 | 1 | ▶ | $r .= '0' if length($r) < 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1161 | 1 | $r *= 1.0; | |||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||
| 1163 | 1 | ($par->{'os_type'} = $ostype) =~ tr/A-Z/a-z/; | |||||||||||||||||||||||||||
| 1164 | 1 | $par->{'os_rev'} = $r; | |||||||||||||||||||||||||||
| 1165 | 1 | ($par->{'mach_type'} = $mach_type) =~ tr/A-Z/a-z/; | |||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||
| 1167 | 1 | ▶ | $type{'sunos4'} = $ostype eq 'SunOS' && $version < 5 ? $r : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ostype eq "SunOS" and $version < 5
MC/DC0%0/2
$ostype eq "SunOS" and $version < 5
$ostype eq "SunOS"
$version < 5
Truth table33%1/3
A and B
A= $ostype eq "SunOS"B= $version < 5
| |||||||||||||||||||||||||||||
| 1168 | 1 | ▶ | $type{'sunos5'} = $ostype eq 'SunOS' && $version > 5 ? $r : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ostype eq "SunOS" and $version > 5
MC/DC0%0/2
$ostype eq "SunOS" and $version > 5
$ostype eq "SunOS"
$version > 5
Truth table33%1/3
A and B
A= $ostype eq "SunOS"B= $version > 5
| |||||||||||||||||||||||||||||
| 1169 | 1 | ▶ | $type{'freebsd'} = $ostype eq 'FreeBSD' ? $r : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1170 | 1 | ▶ | $type{'sunos'} = $ostype eq 'SunOS' ? $r : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1171 | 1 | ▶ | $type{'linux'} = $ostype eq 'Linux' ? $r : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1172 | |||||||||||||||||||||||||||||
| 1173 | 1 | ▶ | $type{$1} = $mach_type =~ /^(i[3-9]86)/ ? $r : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||
| 1175 | 1 | $verbose = -1; | |||||||||||||||||||||||||||
| 1176 | |||||||||||||||||||||||||||||
| 1177 | 1 | ▶ | if (-f '/etc/debian_version') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1178 | 1 | my $deb; | |||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||
| 1180 | 1 | open(IN, '/etc/debian_version'); | |||||||||||||||||||||||||||
| 1181 | 1 | chop($deb = <IN>); | |||||||||||||||||||||||||||
| 1182 | 1 | close(IN); | |||||||||||||||||||||||||||
| 1183 | 1 | $deb =~ s/\.//; | |||||||||||||||||||||||||||
| 1184 | 1 | ▶ | $deb .= '0' if length($deb) < 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1185 | 1 | $type{'debian'} = $deb; | |||||||||||||||||||||||||||
| 1186 | } | ||||||||||||||||||||||||||||
| 1187 | 1 | for my $i (keys %type) { | |||||||||||||||||||||||||||
| 1188 | 7 | &verbose('init', "sub is_$i\{return $type{$i};}"); | |||||||||||||||||||||||||||
| 1189 | 0 | ▶ | eval "sub is_$i\{ &verbose('is_$i','$type{$i}');". | ||||||||||||||||||||||||||
|
Subroutine
is_: not called CC=1 SCAR=6.0
Subroutine
is_debian: not called CC=1 SCAR=6.0
Subroutine
is_freebsd: not called CC=1 SCAR=6.0
Subroutine
is_linux: not called CC=1 SCAR=6.0
Subroutine
is_sunos: not called CC=1 SCAR=6.0
Subroutine
is_sunos4: not called CC=1 SCAR=6.0
Subroutine
is_sunos5: not called CC=1 SCAR=6.0
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1190 | "return \"$type{$i}\";}"; | ||||||||||||||||||||||||||||
| 1191 | } | ||||||||||||||||||||||||||||
| 1192 | 1 | for my $i (qw(os_type os_rev mach_type)) { | |||||||||||||||||||||||||||
| 1193 | 3 | &verbose('init', "sub par_$i { return \"$par->{$i}\"; }"); | |||||||||||||||||||||||||||
| 1194 | 0 | ▶ | eval "sub par_$i { ". | ||||||||||||||||||||||||||
|
Subroutine
par_mach_type: not called CC=1 SCAR=4.9
Subroutine
par_os_rev: not called CC=1 SCAR=4.9
Subroutine
par_os_type: not called CC=1 SCAR=4.9
Pod
undocumented
Pod
undocumented
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1195 | "&verbose('par_$i','$par->{$i}');". | ||||||||||||||||||||||||||||
| 1196 | "return \"$par->{$i}\"; }"; | ||||||||||||||||||||||||||||
| 1197 | } | ||||||||||||||||||||||||||||
| 1198 | 1 | &set_comment; | |||||||||||||||||||||||||||
| 1199 | 1 | $verbose = 0; | |||||||||||||||||||||||||||
| 1200 | } | ||||||||||||||||||||||||||||