#! /usr/bin/env perl
=comment
Pretty-printer for Perforce protections table and client specs.
Can be installed as a server trigger like this:
pretty_printer form-out protect "/path/to/printer.pl %quote%%clientprog%%quote% %quote%%formfile%%quote%"
pretty_printer form-out client "/path/to/printer.pl %quote%%clientprog%%quote% %quote%%formfile%%quote%"
$Id: //guest/jason_gibson/misc/spec_printer.pl#4 $
$DateTime: 2012/08/24 09:41:35 $
$Author: jason_gibson $
$Change: 8164 $
=cut
=comment
Copyright (c) 2012, Perforce Software, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
=comment
User contributed content on the Perforce Public Depot is not supported
by Perforce, although it may be supported by its author. This applies
to all contributions even those submitted by Perforce employees.
=cut
use strict;
use warnings;
my ( $clientProg, $file ) = @ARGV;
die "USAGE: $0 \%clientprog\% \%formfile\%\n"
if @ARGV != 2 or ! -e $file or ! -w $file;
# Various GUIs don't work well with the reformatted spec.
exit if $clientProg ne 'p4';
open FH, "+<$file" or die "$0: Couldn't open form '$file': $!\n";
my ( $output, $type, $offset, @form ) = ( '', '', 0, <FH> );
foreach ( @form ) {
$type = /^Protections:$/ ? 'PROT' : /^View:$/ ? 'CLIENT' : '';
last if $type ne '';
$offset ++ ;
}
die "$0: Unknown form type!\n" if $type eq '';
my $comments = join '', @form[ 0 .. $offset ];
my @data = @form[ $offset + 1 ... $#form ];
if( $type eq 'CLIENT' ) {
my ( $space, $plusminus, $quote_len ) = ( 0, 0, 0 );
foreach ( @_ = @data ) {
s! "*//.*,*!!;
s/^\s*//;
$space = length if length > $space;
my $quote = /^"[+-]/ ? 2 : /^[+-]/ ? 1 : /^"/ ? 1 : 0;
$quote_len = $quote if $quote > $quote_len;
}
foreach ( @data ) {
$output .= $_ and next if /^\s+$/;
s/^\s*//;
my $quote = /^"[+-]/ ? 2 : /^[+-]/ ? 1 : /^"/ ? 1 : 0;
@_ = split '( "*//)';
my $ql = $quote_len - $quote;
my $sp1 = $quote_len ? ' ' x $ql : '';
my $sp2 = ' ' x ( $space - (/ "+\/\// ? $ql : $ql - 1 ) - length $_[ 0 ] );
$output .= "\t" . $sp1 . $_[ 0 ] . $sp2 . join '', @_[ 1 .. @_ - 1 ];
}
}
else {
my ( $has_part, @spaces ) = ( 0, 0, 0, 0, 0 );
foreach ( @data ) {
next if /^$/;
$has_part = 1 if /^\s*=/;
@_ = split ' ';
for( my $i = 0; $i < 4; $i++ ) {
my $l = length $_[ $i ];
$spaces[ $i ] = $l if $l > $spaces[ $i ];
}
}
foreach ( @data ) {
$output .= $_ and next if /^$/;
my $is_part = 1 if /^\s*=/;
my $line = $_;
@_ = split ' ';
$output .= "\t";
for( my $i = 0; $i < 4; $i++ ) {
my $sp1 = 1 + $spaces[ $i ] - length $_[ $i ];
$output .= ' ' x $sp1 . $_[ $i ] ;
}
$_ = substr $line, 2 + length( join ' ', @_[ 0 .. 3 ] );
my $quote = /^"[+-]/ ? 2 : /^[+-]/ ? 1 : /^"/ ? 1 : 0;
$output .= ' ' . ' ' x (2 - $quote). $_;
}
}
truncate FH, 0;
seek FH, 0, 0;
print FH $comments . $output;
close FH;