#!/usr/bin/perl -w # ruler: display lines with a column grid. use Getopt::Long; use Pod::Usage; use File::Basename; use Carp; use strict; $ENV{'PATH'} = join ":", qw(/bin /usr/bin /usr/local/bin /opt/sfw/bin); my $myname = basename($0); $myname =~ s/\.\w*$//; # strip any extension my %options; my @getopt_args = ( 'c=i', # number of columns 'h|?', # print usage 'i=i', # print grid after this many lines 'm', # print manpage 'n', # number the lines 'u', # print UUID 'v', # print version 'w', # print source location ); Getopt::Long::config("noignorecase", "bundling"); usage() unless GetOptions(\%options, @getopt_args); manpage() if $options{'m'}; myuuid() if $options{'u'}; version() if $options{'v'}; where() if $options{'w'}; usage() if $options{'h'}; my $cols = $options{'c'} || 80; my $interval = $options{'i'} || 10; # if we're numbering the lines, we lose 8 character spaces, # so reduce the column count. $cols -= 8 if $options{'n'}; # make the gridline; good for values less than 1000 columns. # extra space on the end is because we count from 1, but perl # counts from zero... my $grid = ('.' x $cols) . ' '; my $k; my $num; $k = 5; while ($k <= $cols) { substr($grid, $k - 1, 1) = '*'; $k += 5; } $k = 10; while ($k <= $cols) { $num = sprintf("%3.3d", $k); substr($grid, $k - 1, 1) = substr($num, 1, 1); $k += 10; } chop($grid); # real work starts here... if ($options{'n'}) { print "\t$grid\n"; while (<>) { print "$.\t$_"; print "\t$grid\n" if $. % $interval == 0; } } else { print "$grid\n"; while (<>) { print "$_"; print "$grid\n" if $. % $interval == 0; } } exit(0); #--------------------------------------------------------------------- # Print a usage message from the comments and exit. sub usage { my ($emsg) = @_; use Pod::Usage qw(pod2usage); warn "$emsg\n" if defined $emsg; pod2usage(-verbose => 99, -sections => "NAME|SYNOPSIS|OPTIONS"); } sub manpage { use Pod::Man(); my $parser = Pod::Man->new(); open(STDOUT, "| groff -T ascii -man | gcat -s | less") || die "groff\n"; $parser->parse_from_file($0); close STDOUT || die "$myname: can't close stdout: $!\n"; $? = 1 if $? == 255; # from die exit($?); } #--------------------------------------------------------------------- # Print the UUID, current version, or source location. sub myuuid { my $UUID = sprintf("%s", q$UUID: cb7a1d47-db5d-3f86-a811-93d28b15d73a $ =~ /UUID: (.*) /); print "$UUID\n"; exit(0); } sub version { my $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/); my $DATE = sprintf("%s", q$Date: 2008/03/24 02:26:19 $ =~ /Date: (.*) /); print "$myname $VERSION $DATE\n"; exit(0); } sub where { my $SOURCE = sprintf("%s", print "$SOURCE\n"; exit(0); } #--------------------------------------------------------------------- __END__ =head1 NAME ruler - display lines with a column grid =head1 SYNOPSIS ruler [-c columns] [-i interval] [-hmnuvw] [file ...] =head1 OPTIONS =over 4 =item B<-c> columns Make the grid B long. Defaults to 80. =item B<-i> interval Print the grid every B lines. Defaults to 10. =item B<-h> Print a brief help message and exit. =item B<-m> Print the manual page and exit. =item B<-n> Number the lines. =item B<-u> Print the script UUID and exit. =item B<-v> Print the version and exit. =item B<-w> Print the source location and exit. =back =head1 DESCRIPTION B will read the given input file(s) and print a grid showing column locations every few lines. =head1 AUTHOR Karl Vogel Sumaria Systems, Inc. =cut