#!/usr/bin/perl -w # File: wrap # Author: Samuel Inverso # URL: http://www.saminverso.com # Contributors: # # Description: takes any number of file names on the command line # and outputs those files to standard output with headers # and footers around them. # # Options: # -n output line numbers # # License: # Use this code freely, it is distributed without warranty express or # implied, use your common sense. # # # Revisions: # March 1, 2003 Initial Revision # # Todo: # Check if seek is successful in calcIndentLength # use Getopt::Long; $firstFile = 1; # if the current file is the first file $numLinesp = ''; # if the lines should be numbered $indentNumLinesp=''; # if the lines numbers should be right justified $indentLen = 0; $lineCount = 0; # what line we are on of the current file # Set any options specified and remove the from ARGV &setOptions(); # setOptions removes options from ARGV, so only file names should # be left, make sure there is at least one file name if( $#ARGV < 0 ) { &printUsage; } # process each file foreach $file( @ARGV ) { open( FILE, $file ) || ((print STDERR "Can't open file $file\n\n") && next) ; if( $indentNumLinesp ) { $indentLen = &calcIndentLength; } &beginDocument(FILE, $file ); while( ) { ++$lineCount; &beginLine($_); &line($_); &endLine($_); } &endDocument(FILE, $file ); close FILE; } # params file, fileName sub beginDocument { $lineCount = 0; # need to separate files with a blank line if( $firstFile ) { $firstFile = 0; } else { print "\n"; } print "="x5 . " File \"$_[1]\":\n"; } # param line sub beginLine { if( $numLinesp || $indentNumLinesp) { printf( "%${indentLen}s ", $lineCount ); } } # param line sub line { print $_[0]; } # param line sub endLine { } #param file, fileName sub endDocument { print "="x5 . " End of file \"$_[1]\"\n"; } # Needs work to support multiple options sub setOptions { GetOptions( 'numLinesp' => \$numLinesp, 'indentNumLinesp' => \$indentNumLinesp ); } sub printUsage { print "Usage: wrap [ -ni ] ... \n"; print " -n number lines\n"; print " -i number lines right justified by largest line number\n"; print " if i is specifid n is implied\n"; } sub calcIndentLength { $count++ while ; #TODO handle the problem of not being able to seek back to the # begining of the file seek(FILE, 0, 0 ); return length($count); }