| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
|
| 5 |
my $USAGE = <<END; |
| 6 |
srcredact - a program for redacting the sources |
| 7 |
|
| 8 |
Usage: |
| 9 |
|
| 10 |
srcredact [OPTIONS] -e audience [full_file] |
| 11 |
|
| 12 |
srcredact [OPTIONS] -u audience full_file [redacted_file] |
| 13 |
|
| 14 |
srcredact -l [full_file] |
| 15 |
|
| 16 |
srcredact -h|-v |
| 17 |
|
| 18 |
|
| 19 |
srcredact is the program to extract ``redacted versions'' of the master |
| 20 |
file (option -e) or to incorporate the changes in the redacted versions |
| 21 |
into the master file (``unextract'', option -u). |
| 22 |
|
| 23 |
The master file consists of chunks intended for different audiences. |
| 24 |
Each audience has a name, e.g. "classified", "unclssified", "expert" |
| 25 |
etc. Chunks are started and stopped by guard lines. Each guard line |
| 26 |
has the format (for the default TeX syntax) |
| 27 |
|
| 28 |
%<name1|name2|...> |
| 29 |
|
| 30 |
or |
| 31 |
|
| 32 |
%</name1|name2|...> |
| 33 |
|
| 34 |
In the first cases the text following the guard is included for the |
| 35 |
audiences "name1", "name2", .... In the second case it is excluded for |
| 36 |
these audiences. |
| 37 |
|
| 38 |
THere is a special audience "ALL": a wild card for all audiences. Thus |
| 39 |
the idiom |
| 40 |
|
| 41 |
%</ALL> |
| 42 |
%<classified> |
| 43 |
|
| 44 |
means that the chunk is excluded for all audiences but "classified" |
| 45 |
|
| 46 |
Exactly one of the options -e (extract) or -u (unextract) must be present. |
| 47 |
In the redact mode the non-option argument is the name of the full file. |
| 48 |
If it is absent, or is "-", standard input is used. In the unextract mode |
| 49 |
the first non-option argument |
| 50 |
|
| 51 |
OPTIONS |
| 52 |
-c list of comment patterns |
| 53 |
Use the given pattern for comment lines to search for guards instead |
| 54 |
of the default "TeX" pattern. The recognized patterns are: |
| 55 |
|
| 56 |
c |
| 57 |
/<guard>/ |
| 58 |
|
| 59 |
cpp |
| 60 |
//<guard> |
| 61 |
|
| 62 |
fortran |
| 63 |
C<guard> |
| 64 |
|
| 65 |
shell |
| 66 |
#<guard> |
| 67 |
|
| 68 |
TeX |
| 69 |
%<guard> |
| 70 |
|
| 71 |
The pattern names should be separated by commas, and the list may be |
| 72 |
enclosed in quotes to prevent shell expansion, e.g |
| 73 |
|
| 74 |
-c "TeX, c, shell" |
| 75 |
|
| 76 |
-d Debug mode on. |
| 77 |
|
| 78 |
-e audience |
| 79 |
Extract the contents for the current audience into the file file. |
| 80 |
The cuurent audience is guessed from the file name, if the latter |
| 81 |
has the structure base-audience.extension, e.g. |
| 82 |
"report-unclassified.tex". The key -a overrides this guess and |
| 83 |
should be used if the file name does not follow this pattern. The |
| 84 |
file name "-" means the standard output. |
| 85 |
|
| 86 |
-h Print help information and exit. |
| 87 |
|
| 88 |
-l List all audiences set in the file (one per line) and exit. |
| 89 |
|
| 90 |
-u audience |
| 91 |
Take a redacted file intended for the audience (the second |
| 92 |
non-option argument) and incorporate the changes in it into the full |
| 93 |
file (the first non-option argument). If the second argument is |
| 94 |
missing, standard input is used instead. As usual, "-" also means |
| 95 |
standard input. Note that only one of the two file arguments in this |
| 96 |
case can be standard input. |
| 97 |
|
| 98 |
-v Print version information and exit. |
| 99 |
|
| 100 |
-w on|off|1|0|true|false |
| 101 |
If "on", 1 or "true" (the deafult), implicitly wrap the full |
| 102 |
document into the guards |
| 103 |
|
| 104 |
%<ALL> |
| 105 |
... |
| 106 |
%</ALL> |
| 107 |
END |
| 108 |
|
| 109 |
|
| 110 |
my $COPYRIGHT= <<END; |
| 111 |
srcredact - a program for redacting the sources |
| 112 |
|
| 113 |
Copyright (C) 2015 Boris Veytsman. Version 1.0 |
| 114 |
|
| 115 |
This program is free software; you can redistribute it and/or modify |
| 116 |
it under the terms of the GNU General Public License as published by |
| 117 |
the Free Software Foundation; either version 2 of the License, or (at |
| 118 |
your option) any later version. |
| 119 |
|
| 120 |
This program is distributed in the hope that it will be useful, but |
| 121 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 122 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 123 |
General Public License for more details. |
| 124 |
|
| 125 |
You should have received a copy of the GNU General Public License |
| 126 |
along with this program; if not, write to the Free Software |
| 127 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 128 |
02110-1301, USA |
| 129 |
END |
| 130 |
|
| 131 |
|
| 132 |
###################################################### |
| 133 |
# Reading arguments |
| 134 |
###################################################### |
| 135 |
|
| 136 |
use Getopt::Std; |
| 137 |
use File::Temp qw/tempdir/; |
| 138 |
|
| 139 |
our %opts; |
| 140 |
getopts ('c:de:hlu:vw:', \%opts) or do { |
| 141 |
print STDERR $USAGE; |
| 142 |
exit(2); |
| 143 |
}; |
| 144 |
|
| 145 |
if ($opts{h}) { |
| 146 |
die ($USAGE); |
| 147 |
} |
| 148 |
|
| 149 |
if ($opts{v}){ |
| 150 |
die ($COPYRIGHT); |
| 151 |
} |
| 152 |
|
| 153 |
my $DEBUG=0; |
| 154 |
if ($opts{d}) { |
| 155 |
$DEBUG=1; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
my @commentPatterns; |
| 160 |
if ($opts{c}) { |
| 161 |
@commentPatterns=GetCommentPatterns($opts{c}); |
| 162 |
} else { |
| 163 |
@commentPatterns=GetCommentPatterns('TeX'); |
| 164 |
} |
| 165 |
|
| 166 |
# We want exactly one of the modes -l, -e, -u: |
| 167 |
my $audience; |
| 168 |
my $mode; |
| 169 |
my $modesFound=0; |
| 170 |
if ($opts{l}) { |
| 171 |
$mode = "list"; |
| 172 |
$modesFound++; |
| 173 |
} |
| 174 |
if ($opts{e}) { |
| 175 |
$mode='extract'; |
| 176 |
$audience = $opts{e}; |
| 177 |
$modesFound++; |
| 178 |
} |
| 179 |
if ($opts{u}) { |
| 180 |
$mode = "unextract"; |
| 181 |
$audience = $opts{u}; |
| 182 |
$modesFound++; |
| 183 |
} |
| 184 |
|
| 185 |
if ($modesFound != 1) { |
| 186 |
print STDERR |
| 187 |
"Exactly one of the options -l, -e, -u must be present.\n\n$USAGE"; |
| 188 |
exit (2); |
| 189 |
} |
| 190 |
|
| 191 |
my $wrap=1; |
| 192 |
if (exists $opts{w}) { |
| 193 |
if ($opts{w} eq '0' || $opts{w} =~ m/^off$/i || |
| 194 |
$opts{w} =~ m/^false$/i) { |
| 195 |
$wrap=0; |
| 196 |
} elsif ($opts{w} eq '1' || $opts{w} =~ m/^on$/i || |
| 197 |
$opts{w} =~ m/^true$/i) { |
| 198 |
$wrap=0; |
| 199 |
} else { |
| 200 |
print STDERR "Unknwon value for -w option: $opts{w}.\n" . |
| 201 |
"Must be 0|1|on|off|true|false\n"; |
| 202 |
exit (2); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if ($DEBUG) { |
| 207 |
print STDERR "Wrapping in <ALL> is $wrap\n"; |
| 208 |
} |
| 209 |
|
| 210 |
# Opening files |
| 211 |
my $fullFH; |
| 212 |
my $newFH; |
| 213 |
|
| 214 |
# In 'l' or 'e' mode we need one or zero non-option arguments |
| 215 |
if (($mode eq 'list') || ($mode eq 'extract')) { |
| 216 |
if (scalar(@ARGV) == 0) { |
| 217 |
if ($DEBUG) { |
| 218 |
print STDERR "Usign stdin for full file\n"; |
| 219 |
} |
| 220 |
$fullFH=*STDIN; |
| 221 |
} elsif (scalar(@ARGV) == 1) { |
| 222 |
my $full = shift @ARGV; |
| 223 |
if ($full eq '-') { |
| 224 |
if ($DEBUG) { |
| 225 |
print STDERR "Usign stdin for full file\n"; |
| 226 |
} |
| 227 |
$fullFH = *STDIN; |
| 228 |
} else { |
| 229 |
if ($DEBUG) { |
| 230 |
print STDERR "Usign $full for full file\n"; |
| 231 |
} |
| 232 |
open ($fullFH, "<", $full) or do { |
| 233 |
print STDERR "Cannot open file $full\n"; |
| 234 |
exit (2); |
| 235 |
}; |
| 236 |
} |
| 237 |
} else { |
| 238 |
print STDERR |
| 239 |
"Options -e and -l require one or zero non-option argument\n\n"; |
| 240 |
print STDERR $USAGE; |
| 241 |
exit(2); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
# In 'u' mode we need one or two arguments |
| 246 |
if ($mode eq 'unextract') { |
| 247 |
if ((scalar(@ARGV) < 1) || (scalar(@ARGV) > 2)) { |
| 248 |
print STDERR |
| 249 |
"Option -u requires one or two non-option argument\n\n"; |
| 250 |
print STDERR $USAGE; |
| 251 |
exit(2); |
| 252 |
} |
| 253 |
|
| 254 |
my $full = shift @ARGV; |
| 255 |
if ($full eq '-') { |
| 256 |
if ($DEBUG) { |
| 257 |
print STDERR "Usign stdin for full file\n"; |
| 258 |
} |
| 259 |
$fullFH = *STDIN; |
| 260 |
} else { |
| 261 |
if ($DEBUG) { |
| 262 |
print STDERR "Usign $full for full file\n"; |
| 263 |
} |
| 264 |
open ($fullFH, "<", $full) or do { |
| 265 |
print STDERR "Cannot open file $full\n"; |
| 266 |
exit (2); |
| 267 |
}; |
| 268 |
} |
| 269 |
|
| 270 |
if (scalar(@ARGV) == 0) { |
| 271 |
if ($DEBUG) { |
| 272 |
print STDERR "Usign stdin for redacted file\n"; |
| 273 |
} |
| 274 |
$newFH=*STDIN; |
| 275 |
} else { |
| 276 |
my $redacted = shift @ARGV; |
| 277 |
if ($redacted eq '-') { |
| 278 |
if ($DEBUG) { |
| 279 |
print STDERR "Usign stdin for redacted file\n"; |
| 280 |
} |
| 281 |
$newFH = *STDIN; |
| 282 |
} else { |
| 283 |
if ($DEBUG) { |
| 284 |
print STDERR "Usign $redacted for redacted file\n"; |
| 285 |
} |
| 286 |
open ($newFH, "<", $redacted) or do { |
| 287 |
print STDERR "Cannot open file $redacted\n"; |
| 288 |
exit (2); |
| 289 |
}; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
# And the real work |
| 297 |
if ($mode eq 'list') { |
| 298 |
if ($DEBUG) { |
| 299 |
print STDERR "List mode\n"; |
| 300 |
} |
| 301 |
my @audiences=ListAudiences($fullFH, \@commentPatterns); |
| 302 |
print join("\n", @audiences), "\n"; |
| 303 |
exit (0); |
| 304 |
} |
| 305 |
|
| 306 |
if ($mode eq 'extract') { |
| 307 |
if ($DEBUG) { |
| 308 |
print STDERR "Extract mode\n"; |
| 309 |
} |
| 310 |
ExtractText($fullFH, \*STDOUT, $audience, |
| 311 |
\@commentPatterns, $wrap); |
| 312 |
exit (0); |
| 313 |
} |
| 314 |
|
| 315 |
if ($mode eq 'unextract') { |
| 316 |
if ($DEBUG) { |
| 317 |
print STDERR "Unextract mode\n"; |
| 318 |
} |
| 319 |
|
| 320 |
my $exitCode = UnextractText($fullFH, $newFH, \*STDOUT, |
| 321 |
$audience, \@commentPatterns, $wrap); |
| 322 |
|
| 323 |
exit ($exitCode); |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
################################################## |
| 328 |
# Extracting comment patterns |
| 329 |
################################################## |
| 330 |
|
| 331 |
sub GetCommentPatterns { |
| 332 |
|
| 333 |
my %knownPatterns = ( 'c' => '^/\*<([^>]*)>\*/', |
| 334 |
'cpp' => '^\/\/<([^>]*)>', |
| 335 |
fortran => '^C<([^>]*)>', |
| 336 |
shell => '^#<([^>]*)>', |
| 337 |
tex => '^%<([^>]*)>' |
| 338 |
); |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
my $input = shift; |
| 343 |
if ($DEBUG) { |
| 344 |
print STDERR "Got pattern string $input\n"; |
| 345 |
} |
| 346 |
$input =~ s/^\s*//; |
| 347 |
$input =~ s/\s*$//; |
| 348 |
my @languages = split /[\s,]\s*/, $input; |
| 349 |
my @patterns; |
| 350 |
|
| 351 |
foreach my $lang (@languages) { |
| 352 |
$lang =~ s/^(.*)$/\L$1\E/; |
| 353 |
if ($DEBUG) { |
| 354 |
print STDERR "Processing language $lang... "; |
| 355 |
} |
| 356 |
if (exists $knownPatterns{$lang}) { |
| 357 |
push @patterns, $knownPatterns{$lang}; |
| 358 |
if ($DEBUG) { |
| 359 |
print STDERR "found pattern $knownPatterns{$lang}\n"; |
| 360 |
} |
| 361 |
} else { |
| 362 |
print STDERR |
| 363 |
"Unknown comment language $lang. The supported languages are ", |
| 364 |
join(", ", keys %knownPatterns), "\n"; |
| 365 |
exit(2); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return @patterns; |
| 370 |
} |
| 371 |
|
| 372 |
################################################## |
| 373 |
# List audiences |
| 374 |
################################################## |
| 375 |
|
| 376 |
sub ListAudiences { |
| 377 |
my $fh=shift; |
| 378 |
my $patterns=shift; |
| 379 |
my %foundAudiences = ('ALL' => 1); |
| 380 |
while (<$fh>) { |
| 381 |
my @result = FindAudiences ($_, $patterns); |
| 382 |
if (scalar @result) { |
| 383 |
shift @result; |
| 384 |
foreach my $audience (@result) { |
| 385 |
$foundAudiences{$audience} = 1; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
return (sort keys %foundAudiences); |
| 390 |
} |
| 391 |
|
| 392 |
################################################## |
| 393 |
# Check whether the line is a comment |
| 394 |
################################################## |
| 395 |
# Return 0 to delete, 1 to add, and the list of |
| 396 |
# audiences |
| 397 |
sub FindAudiences { |
| 398 |
my $line = shift; |
| 399 |
my $patterns=shift; |
| 400 |
foreach my $pattern (@{$patterns}) { |
| 401 |
if ($line =~ m/$pattern/) { |
| 402 |
if ($DEBUG) { |
| 403 |
print STDERR "Line $. is a guard: $line"; |
| 404 |
} |
| 405 |
my $guards = $1; |
| 406 |
my @result; |
| 407 |
if (substr($guards,0,1) eq '*') { |
| 408 |
push @result, 1; |
| 409 |
} elsif (substr($guards,0,1) eq '/') { |
| 410 |
push @result, 0; |
| 411 |
} else { |
| 412 |
die "Wrong guard line $line\n"; |
| 413 |
} |
| 414 |
@result = (@result, split(/\|/, substr($guards,1))); |
| 415 |
return @result; |
| 416 |
} |
| 417 |
} |
| 418 |
return (); |
| 419 |
} |
| 420 |
|
| 421 |
################################################## |
| 422 |
# Extracting text for the given audience |
| 423 |
################################################## |
| 424 |
sub ExtractText { |
| 425 |
my $fullFH=shift; |
| 426 |
my $outFH=shift; |
| 427 |
my $audience = shift; |
| 428 |
my $patterns = shift; |
| 429 |
my $state = shift; |
| 430 |
|
| 431 |
if ($DEBUG) { |
| 432 |
print STDERR "Initial state $state\n"; |
| 433 |
} |
| 434 |
|
| 435 |
while (<$fullFH>) { |
| 436 |
my @result = FindAudiences ($_, $patterns); |
| 437 |
if (scalar @result) { |
| 438 |
my $newstate = shift @result; |
| 439 |
foreach my $tryaudience (@result) { |
| 440 |
if (($tryaudience eq 'ALL') || |
| 441 |
($audience eq $tryaudience)) { |
| 442 |
$state = $newstate; |
| 443 |
if ($DEBUG) { |
| 444 |
print STDERR "Changing state to $state\n"; |
| 445 |
} |
| 446 |
last; |
| 447 |
} |
| 448 |
} |
| 449 |
} else { # Normal line |
| 450 |
if ($state) { |
| 451 |
print $outFH $_; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
################################################################ |
| 458 |
# Merge files |
| 459 |
################################################################ |
| 460 |
|
| 461 |
sub UnextractText { |
| 462 |
my $fullFH=shift; |
| 463 |
my $newFH=shift; |
| 464 |
my $outFH = shift; |
| 465 |
my $audience = shift; |
| 466 |
my $patterns = shift; |
| 467 |
my $state = shift; |
| 468 |
|
| 469 |
# Creating tempdir. We leave the tempdir in place in |
| 470 |
# the debug mode |
| 471 |
my $tmpdir = tempdir (CLEANUP => !$DEBUG); |
| 472 |
if ($DEBUG) { |
| 473 |
print STDERR "Creating directory $tmpdir\n"; |
| 474 |
} |
| 475 |
|
| 476 |
open (FULL, ">$tmpdir/full") or do { |
| 477 |
print STDERR "Cannot open temporary file $tmpdir/full\n"; |
| 478 |
return(2); |
| 479 |
}; |
| 480 |
|
| 481 |
print FULL <$fullFH>; |
| 482 |
close FULL; |
| 483 |
|
| 484 |
open (FULL, "<$tmpdir/full") or do { |
| 485 |
print STDERR "Cannot open temporary file $tmpdir/full\n"; |
| 486 |
return(2); |
| 487 |
}; |
| 488 |
|
| 489 |
open (EXTRACTED, ">$tmpdir/extracted") or do { |
| 490 |
print STDERR "Cannot open temporary file $tmpdir/extracted\n"; |
| 491 |
return(2); |
| 492 |
}; |
| 493 |
|
| 494 |
if ($DEBUG) { |
| 495 |
print STDERR "Extracting text\n"; |
| 496 |
} |
| 497 |
|
| 498 |
ExtractText(\*FULL, \*EXTRACTED, $audience, |
| 499 |
$patterns, $wrap); |
| 500 |
|
| 501 |
close FULL; |
| 502 |
close EXTRACTED; |
| 503 |
|
| 504 |
open (NEW, ">$tmpdir/new") or do { |
| 505 |
print STDERR "Cannot open temporary file $tmpdir/new\n"; |
| 506 |
return(2); |
| 507 |
}; |
| 508 |
|
| 509 |
print NEW <$newFH>; |
| 510 |
close NEW; |
| 511 |
|
| 512 |
|
| 513 |
if ($DEBUG) { |
| 514 |
print STDERR "Merging text\n"; |
| 515 |
} |
| 516 |
|
| 517 |
print $outFH `diff3 -m $tmpdir/full $tmpdir/extracted $tmpdir/new`; |
| 518 |
|
| 519 |
return ${^CHILD_ERROR_NATIVE}; |
| 520 |
|
| 521 |
} |