#!/usr/bin/perl -w # vim: set foldmethod=marker: # --------------- # AddCVSHeader.pl # --------------- # $Source: /dat/f711b/astro_dat/.CVSROOT/d3807/verilog/rtl/CMB/tools/AddCVSHeader.pl,v $ # $Revision: 1.6 $ # $Author: shimizu $ # $Date: 2003/06/06 06:34:12 $ # $Locker: $ # $Log: AddCVSHeader.pl,v $ # Revision 1.6 2003/06/06 06:34:12 shimizu # bug fix: second comment type can not auto detect. # # Revision 1.5 2003/06/05 06:55:08 shimizu # bug fix. # # Revision 1.4 2003/06/05 06:48:36 shimizu # correct usage spell miss. Data => Date. # # Revision 1.3 2003/06/05 06:47:43 shimizu # mod usage. # # Revision 1.2 2003/06/05 06:40:55 shimizu # version up add_cvs_header tool. # # Revision 1.1 2003/06/05 05:15:01 shimizu # change $(CORE_DIR) to coregen. # # $State: Exp $ # # -------------------------------------- # AddCVSHeader - CVS Header Adding Tool # Author: 2003.2.25 Kazuhide Shimizu ( rocketpon@yahoo.co.jp ) # -------------------------------------- # Copyright 2003, Kazuhide Shimizu, All Rights Reserved. # this tool is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # use strict; use FileHandle; my $ach = new AddCVSHeader; $ach->getOpt( \@ARGV ); $ach->execAll; # ------------ # AddCVSHeader {{{1 # ------------ package AddCVSHeader; sub new { my $self = {}; my $distinct = { c => 'c', cpp => 'c', C => 'c', CPP => 'c', v => 'c', Makefile => 's', mk => 's', csh => 's', sh => 's', bashrc => 's', cshrc => 's', pl => 's', pm => 's', vhd => 'v' }; $self->{distinct} = $distinct; bless $self; } # get options {{{2 sub getOpt { my ( $self, $arr ) = @_; # scan my @infile = (); while( defined( $arr->[0] ) ) { if( $arr->[0] =~ m/^-/ ) { my $opt = $arr->[0]; my $fname = 'getOpt_'.substr( $arr->[0], 1 ); $self->usage( 'Error: invalid option "'.$opt.'".' ) if !defined( $AddCVSHeader::{$fname} ); $self->$fname( $arr ); } else { push( @infile, $arr->[0] ); } shift( @$arr ); } # check input file $self->usage if !defined( $infile[0] ); $self->{infile} = \@infile; # default template $self->getOpt_a if !defined( $self->{template} ); } sub getOpt_l { my ( $self, $arr ) = @_; $self->{template} = q{CSTART COMMENT FILENAME COMMENT SEPARATOR COMMENT Log: CEND }; } sub getOpt_a { my ( $self, $arr ) = @_; $self->{template} = q{CSTART COMMENT FILENAME COMMENT SEPARATOR COMMENT Source: COMMENT Revision: COMMENT Author: COMMENT Date: COMMENT Locker: COMMENT Log: COMMENT State: CEND }; } sub getOpt_t { my ( $self, $arr ) = @_; shift( @$arr ); $self->{manual_comment_type} = 1; $self->{comment_type} = $arr->[0]; } sub getOpt_f { my ( $self, $arr ) = @_; $self->{force} = 1; } sub getOpt_p { my ( $self, $arr ) = @_; print( "\n" ); print( "--- AUTO DETECTED SUFFIX TABLE ------\n" ); print( "c => c language type, s => shell type\n" ); print( "-------------------------------------\n" ); foreach my $key ( keys( %{$self->{distinct}} ) ) { print( $key."\t=>\t".$self->{distinct}->{$key}."\n" ); } print( "\n" ); exit 0; } # exec all {{{2 sub execAll { my ( $self ) = @_; foreach my $infile ( @{$self->{infile}} ) { $self->{comment_type} = undef if !defined( $self->{manual_comment_type} ); print( "processing $infile ...\n" ); $self->read( $infile ) or next; $self->write( $infile ); } } # read {{{2 sub read { my ( $self, $name ) = @_; my $infile = new FileHandle( "<$name" ) or $self->usage( 'Error: '.$! ); my @data = $infile->getlines; $infile->close; if( !defined( $self->{force} ) ) { return undef if scalar( grep( /\$Log:/, @data ) ); } my @vim = (); while( defined( $data[0] ) ) { last if $data[0] !~ /vim:/ and $data[0] !~ /^#!/; push( @vim, shift( @data ) ); } $self->{vim} = \@vim; $self->{data} = \@data; return 1; } # write {{{2 sub write { my ( $self, $name ) = @_; my $header = $self->{template}; my $fname = substr( $name, rindex( $name, '/', ) + 1 ); my $suffix = substr( $fname, rindex( $fname, '.' ) + 1 ); ### auto distinct ### if( !defined( $self->{comment_type} ) ) { $self->{comment_type} = $self->{distinct}->{$suffix}; } ### exchange comment. ### if( !defined( $self->{comment_type} ) ) { $self->usage( 'Error: suffix "'.$suffix.'". comment type is not auto detected.' ); } elsif( $self->{comment_type} eq 's' ) { $header =~ s/CSTART/# SEPARATOR/; $header =~ s/COMMENT/#/g; $header =~ s/CEND/#/; } elsif( $self->{comment_type} eq 'c' ) { $header =~ s/CSTART/\/\*/; $header =~ s/COMMENT/ \*/g; $header =~ s/CEND/ \*\//; } elsif( $self->{comment_type} eq 'v' ) { $header =~ s/CSTART/-- SEPARATOR/; $header =~ s/COMMENT/--/g; $header =~ s/CEND/--/; } else { $self->usage( 'Error: invalid comment type "'.$self->{comment_type}.'".' ); } ### exchange dollar ### $header =~ s//\$/g; $header =~ s/FILENAME/$fname/; ### exchange separator ### my $sep = ''; for( my $i = 0; $i < length( $fname ); $i++ ) { $sep .= '-'; } $header =~ s/SEPARATOR/$sep/g; ### print out ### my $ofile = new FileHandle( ">$name" ) or $self->usage( 'Error: '.$! ); $ofile->print( @{$self->{vim}} ); $ofile->print( $header ); $ofile->print( @{$self->{data}} ); $ofile->close; } # usage {{{2 sub usage { my ( $self, $msg ) = @_; print( "*\n" ); print( "* AddCVSHeader version 0.1\n" ); print( '* $Date: 2003/06/06 06:34:12 $'."\n" ); print( '* kshimizu ( shimizu@mosk.semicon.sony.co.jp )'."\n" ); print( "*\n" ); print( $msg."\n" ) if defined( $msg ); print( q{ usage: perl AddCVSHeader.pl [-a | -l ] [-t type] [-f] [-p] infile [infiles...] option: -a: add all header. -l: add Log: header only. -t type: comment type. ( c => c lang, s => shell, v => vhdl ). default is auto detection. -f: force. -p: print supported suffix and comment type. } ); exit 1; } __END__ =head1 NAME AddCVSHeader - The CVS header adding tool. =head1 SYNOPSIS usage: perl AddCVSHeader.pl [-a | -l ] [-t type] [-f] [-p] infile [infiles...] option: -a: add all header. -l: add Log: header only. -t type: comment type. ( c => c lang, s => shell, v => vhdl ). default is auto detection. -f: force. -p: print supported suffix and comment type. =head1 DESCRIPTION =head1 AUTHOR Kazuhide Shimizu ( rocketpon@yahoo.co.jp ) =head1 COPYRIGHT AND LICENSE Copyright 2003, Kazuhide Shimizu, All Rights Reserved. This tool is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AVAILABILITY http://www.geocities.co.jp/SiliconValley/1253/