#!/usr/bin/perl
#   number [-options] [lo [inc [hi]]] [file]...
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#
# This  reads the input and adds line numbers.  Several styles are recognized: #
# The "-c" style adds numbers as C comments. The "-l" and "-r" options add the #
# line  numbers  at  the left and right, respectively.  If the lo, inc, and hi #
# args are present, they are the low and high numbers  to  generate,  and  the #
# increment;  the  defaults are lo=0, inc=1, hi=MAXINT, where MAXINT is a very #
# large number.                                                                #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#

for (@ARGV) {
	if (/^-(.*)/) {
		&opts($1);
	} elsif (/^\d+$/) {
		if (!defined $lo) {
			$lo = $_;
		} elsif (!defined $inc) {
			$inc = $_;
		} elsif (!defined $hi) {
			$hi = $_;
		}
	} else {
		@files = (@files , $_);
	}
}
@ARGV = @files;
$lo = 0 if !defined $lo;
$inc = 1 if !defined $inc;
$hi = 1000000000 if !defined $hi;

# Now read in the data and output it with line numbers.
for (<>) {
	chop;
	if ($cc) {
		$num = '/* ' . $lo . ' */';
	} else {
		$num = $lo;
	}
	if ($left) {
		print $num, "\t", $_, "\n";
	} else {
		print  $_, "\t", $num, "\n";
	}
	exit 0 if (($lo += $inc) > $hi);
}


sub opts {
	while (s/.//) {
		if (/c/) {
			$cc = 1;
		} elsif (/l/) {
			$left = 1;
		} elsif (/r/) {
			$right = 1;
		}
	}
}
