X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?a=blobdiff_plain;f=cp-update;h=d5b2ab24f80e7413ce0b39af4296e185fa9a9c4d;hb=b8722272394af7853b62e502d3c909e289872277;hp=6491c7837f4e0f2c566d11c9ba53fe40f204af8e;hpb=b6374f775ed04f3c9767d19a364ad0994a94ac4f;p=carnet-tools-cn.git diff --git a/cp-update b/cp-update index 6491c78..d5b2ab2 100755 --- a/cp-update +++ b/cp-update @@ -69,7 +69,6 @@ $UsageLong = " -i | --insert-after x Insert after this/matching line. -f | --insert-before x Insert before this/matching line. - -R | --regexp-match Use regexp mode for line matching. - Manipulating block marks: @@ -120,6 +119,7 @@ use constant DO_INSERT => 1; # Operation defaults $InsertRemove = DO_INSERT; $Placement = APPEND_AT_END; +$RegexpMatch = 1; $Comment = '#'; $CommentEnd = ''; $MatchLine = ''; @@ -166,7 +166,7 @@ while (@ARGV) { $Placement = INSERT_ON_TOP; } elsif (/^-R$/ || /^--regexp(?:-match|-mode)?$/) { - $RegexpMatch = 1; + $RegexpMatch = 1; # it's the default } elsif (/^-h$/ || /^--help$/) { die $UsageLong; @@ -299,8 +299,8 @@ sub add() { } ++ $lineNo; } - if (! $added) { - warn "$ProgramName: Inserting lines at the end implicitly!\n"; + if ($MatchLine and ! $added) { + warn "$ProgramName: Inserting lines at the end implicitly! No '$MatchLine'\n"; push(@Lines, $StdinContent); } } @@ -311,6 +311,8 @@ sub del() { my ($mytrailer, $mybegin, $myend) = ($Trailer, $MarkBegin, $MarkEnd); + my ($bm_found, $em_found); # begin/end mark found indicator + # Make the strings regexp-friendly by quoting non-word chars. $mybegin =~ s/\W/\\$&/g; $myend =~ s/\W/\\$&/g; @@ -320,9 +322,19 @@ sub del() { foreach (@Lines) { push (@filtered, $_) unless (/^$mybegin(?:$mytrailer)?$/o .. /^$myend(?:$mytrailer)?$/o); + + # for safety check: + $bm_found = 1 if (/^$mybegin(?:$mytrailer)?$/o); + $em_found = 1 if (/^$myend(?:$mytrailer)?$/o); + } + if ($bm_found and $em_found) { + DEBUG and print STDERR "Deleted ". (@Lines - @filtered) ." out of ".scalar(@Lines)." lines\n"; + @Lines = @filtered; + } + elsif ($bm_found and ! $em_found) { + # safety exit + die "$ProgramName: no end-mark after begin-mark!\n"; } - DEBUG and print STDERR "Deleted ". (@Lines - @filtered) ." out of ".scalar(@Lines)." lines\n"; - @Lines = @filtered; return scalar(@Lines); # whatever }