| 1 | # | ||
| 2 | # $Id: Strip.pm,v 0.1 2001/03/31 10:04:36 ram Exp $ | ||
| 3 | # | ||
| 4 | # Copyright (c) 2000-2001, Christophe Dehaudt & Raphael Manfredi | ||
| 5 | # | ||
| 6 | # You may redistribute only under the terms of the Artistic License, | ||
| 7 | # as specified in the README file that comes with the distribution. | ||
| 8 | # | ||
| 9 | # HISTORY | ||
| 10 | # $Log: Strip.pm,v $ | ||
| 11 | # Revision 0.1 2001/03/31 10:04:36 ram | ||
| 12 | # Baseline for first Alpha release. | ||
| 13 | # | ||
| 14 | # $EndLog$ | ||
| 15 | # | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | |||
| 19 | package Carp::Datum::Strip; | ||
| 20 | |||
| 21 | require Exporter; | ||
| 22 | |||
| 23 | use vars qw(@ISA @EXPORT_OK); | ||
| 24 | @ISA = qw(Exporter); | ||
| 25 | |||
| 26 | @EXPORT_OK = qw(datum_strip); | ||
| 27 | |||
| 28 | use Log::Agent; | ||
| 29 | |||
| 30 | # | ||
| 31 | # datum_strip | ||
| 32 | # | ||
| 33 | # Strip all Datum assertions in file and flow control tracing. | ||
| 34 | # Also turn Datum off by stripping the "use" line. | ||
| 35 | # | ||
| 36 | # Let all DTRACE statements pass through. | ||
| 37 | # | ||
| 38 | # Arguments: | ||
| 39 | # file old file path, to strip | ||
| 40 | # fnew new file, stripped | ||
| 41 | # ext when defined, renames fnew as file upon success and file with ext | ||
| 42 | # | ||
| 43 | # Returns 1 if OK, undef otherwise. | ||
| 44 | # | ||
| 45 | sub datum_strip { | ||
| 46 | my ($file, $fnew, $ext) = @_; | ||
| 47 | |||
| 48 | local *OLD; | ||
| 49 | local *NEW; | ||
| 50 | |||
| 51 | if ($file eq '-') { | ||
| 52 | logdie "can't dup stdin: $!" unless open(OLD, '<&STDIN'); | ||
| 53 | } else { | ||
| 54 | unless (open(OLD, $file)) { | ||
| 55 | logerr "can't open $file: $!"; | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | if ($fnew eq '-') { | ||
| 61 | logdie "can't dup stdout: $!" unless open(NEW, '>&STDOUT'); | ||
| 62 | } else { | ||
| 63 | unless (open(NEW, ">$fnew")) { | ||
| 64 | logerr "can't create $fnew: $!"; | ||
| 65 | close OLD; | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | eval { strip(\*OLD, \*NEW) }; | ||
| 71 | if (chop $@) { | ||
| 72 | logerr "can't write to $fnew: $@"; | ||
| 73 | close NEW; | ||
| 74 | close OLD; | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | if ($file ne '-' && $fnew ne '-') { | ||
| 79 | my $mode = (stat(OLD))[2] & 07777; | ||
| 80 | chmod $mode, $fnew or logwarn "can't propagate mode %04o on $fnew: $!"; | ||
| 81 | } | ||
| 82 | |||
| 83 | unless (close NEW) { | ||
| 84 | logerr "can't flush $fnew: $!"; | ||
| 85 | close OLD; | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | close OLD; | ||
| 90 | return 1 if $file eq '-' || $fnew eq '-'; | ||
| 91 | return 1 unless defined $ext; | ||
| 92 | |||
| 93 | unless (rename($file, "$file$ext")) { | ||
| 94 | logwarn "can't rename $file as $file$ext: $!"; | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | unless (rename($fnew, $file)) { | ||
| 99 | logwarn "can't rename $fnew as $file: $!"; | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | return 1; # OK | ||
| 104 | } | ||
| 105 | |||
| 106 | # | ||
| 107 | # strip | ||
| 108 | # | ||
| 109 | # Lexical stripping of assertions, and return tracing routines. | ||
| 110 | # We don't have the pretention of handling all the possible cases. | ||
| 111 | # That would be foolish, because we'd have to write a Perl parser! | ||
| 112 | # | ||
| 113 | # Therefore, unless the conventions documented in the Carp::Datum manpage | ||
| 114 | # are strictly followed, stripping will be incorret. | ||
| 115 | # | ||
| 116 | # Note: we don't remove DTRACE, they will be remapped to Log::Agent calls | ||
| 117 | # dynamically. We can't do that statically because the syntax is not | ||
| 118 | # compatible. | ||
| 119 | # | ||
| 120 | sub strip { | ||
| 121 | my ($old, $new) = @_; | ||
| 122 | |||
| 123 | local $_; | ||
| 124 | my $last_was_nl = 0; | ||
| 125 | |||
| 126 | while (<$old>) { | ||
| 127 | next if s/^(\s*use Carp::Datum).*;/$1;/; # Turns it off | ||
| 128 | next if s/^(\s*)(?:DVOID|DVAL|DARY)\b/$1/; | ||
| 129 | next if s/^(\s*return)\s+DVOID\b/$1/; | ||
| 130 | next if s/^(\s*return\s+)(?:(?:DVAL|DARY)\s*)/$1/; | ||
| 131 | |||
| 132 | if (s/^(\s*)(?:DFEATURE|DREQUIRE|DENSURE|DASSERT)\b//) { | ||
| 133 | my $indent = $1; | ||
| 134 | $_ = skip_to_sc($old, $_); | ||
| 135 | s/^\s+//; | ||
| 136 | $_ = /^\s*$/ ? '' : ($indent . $_); # Keep leading indent | ||
| 137 | next; | ||
| 138 | } | ||
| 139 | } continue { | ||
| 140 | my $is_nl = /^\s*$/; | ||
| 141 | unless ($last_was_nl && $is_nl) { | ||
| 142 | print $new $_ or CORE::die "$!\n"; | ||
| 143 | } | ||
| 144 | $last_was_nl = $is_nl; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | # | ||
| 149 | # skip_to_sc | ||
| 150 | # | ||
| 151 | # Strip to next ';' outside any string. | ||
| 152 | # We don't handle regexps, here docs, nor syntactic sugar for quotes. | ||
| 153 | # | ||
| 154 | # Returns anything after the final ';'. | ||
| 155 | # | ||
| 156 | sub skip_to_sc { | ||
| 157 | my ($fd, $str) = @_; | ||
| 158 | my $str_end = ''; | ||
| 159 | for (;;) { | ||
| 160 | if ($str =~ /^\s*$/) { | ||
| 161 | $str = <$fd>; | ||
| 162 | return '' unless defined $str; # EOF | ||
| 163 | } | ||
| 164 | |||
| 165 | if ($str_end) { # Within string | ||
| 166 | $str =~ s/\\(?:\\\\)*['"`]//g; # Remove escaped quotes | ||
| 167 | $str_end = '' if $str =~ s/.*$str_end//; | ||
| 168 | if ($str_end) { # Still not reached the end | ||
| 169 | $str = ''; | ||
| 170 | next; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | $str =~ s/^[^'"`;]*//; | ||
| 175 | return substr($str, 1) if substr($str, 0, 1) eq ";"; | ||
| 176 | next if $str =~ /^\s*$/; | ||
| 177 | if ($str =~ s/^(['"`])//) { # Found a string | ||
| 178 | $str_end = $1; | ||
| 179 | next; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | =head1 NAME | ||
| 187 | |||
| 188 | Carp::Datum::Strip - strips most Carp::Datum calls lexically | ||
| 189 | |||
| 190 | =head1 SYNOPSIS | ||
| 191 | |||
| 192 | use Carp::Datum::Strip qw(datum_strip); | ||
| 193 | |||
| 194 | datum_strip("-", "-"); | ||
| 195 | datum_strip($file, "$file.new", ".bak"); | ||
| 196 | |||
| 197 | =head1 DESCRIPTION | ||
| 198 | |||
| 199 | This module exports a single routine, datum_strip(), whose purpose is | ||
| 200 | to remove calls to C<Carp::Datum> routines lexically. | ||
| 201 | |||
| 202 | Because stripping is done lexically, there are some restrictions about | ||
| 203 | what is actually supported. Unless the conventions documented in | ||
| 204 | L<Carp::Datum> are followed, stripping will be incorrect. | ||
| 205 | |||
| 206 | The general guidelines are: | ||
| 207 | |||
| 208 | =over 4 | ||
| 209 | |||
| 210 | =item * | ||
| 211 | |||
| 212 | Do not use here documents or generalized quotes (qq) within | ||
| 213 | assertion expression or tags. Write assertions using '' or "", | ||
| 214 | as appropriate. | ||
| 215 | |||
| 216 | =item * | ||
| 217 | |||
| 218 | Assertions can be safely put on several lines, but must end with a semi-colon, | ||
| 219 | outside any string. | ||
| 220 | |||
| 221 | =back | ||
| 222 | |||
| 223 | There are two calls that will never be stripped: VERIFY() and DTRACE(). | ||
| 224 | The VERIFY() is meant to be preserved (or C<DREQUIRE> would have been used). | ||
| 225 | C<DTRACE>, when called, will be remapped dynamically to some | ||
| 226 | C<Log::Agent> routine, depending on the trace level. See L<Carp::Datum> | ||
| 227 | for details. | ||
| 228 | |||
| 229 | =head1 INTERFACE | ||
| 230 | |||
| 231 | The interface of the datum_strip() routine is: | ||
| 232 | |||
| 233 | =over 4 | ||
| 234 | |||
| 235 | =item C<datum_strip> I<old_file>, I<new_file>, [I<ext>] | ||
| 236 | |||
| 237 | The I<old_file> specifies the old file path, the one to be stripped. | ||
| 238 | The stripped version will be written to I<new_file>. | ||
| 239 | |||
| 240 | If the optional third argument I<ext> is given (e.g. ".bak"), | ||
| 241 | then I<old_file> will be renamed with the supplied extension, and I<new_file> | ||
| 242 | will be renamed I<old_file>. Renaming only occurs if stripping was successful | ||
| 243 | (i.e. the new file was correctly written to disk). | ||
| 244 | |||
| 245 | The lowest nine "rwx" mode bits from I<old_file> are preserved when | ||
| 246 | creating I<new_file>. | ||
| 247 | |||
| 248 | Both I<old_file> and I<new_file> can be set to "-", in which case STDIN | ||
| 249 | and STDOUT are used, respectively, and no renaming can occur, nor any | ||
| 250 | mode bit propagation. | ||
| 251 | |||
| 252 | Returns true on success, C<undef> on error. | ||
| 253 | |||
| 254 | =back | ||
| 255 | |||
| 256 | =head1 AUTHORS | ||
| 257 | |||
| 258 | Christophe Dehaudt and Raphael Manfredi are the original authors. | ||
| 259 | |||
| 260 | Send bug reports, hints, tips, suggestions to Dave Hoover at <squirrel@cpan.org>. | ||
| 261 | |||
| 262 | =head1 SEE ALSO | ||
| 263 | |||
| 264 | Carp::Datum(3). | ||
| 265 | |||
| 266 | =cut | ||
| 267 |