#!/usr/bin/perl
#
# gdb list command implemented in perl with addr2line for radare
#
# author: pancake <@youterm.com>
#

my $inrad = defined($ENV{FILE});
my @path = split(/:/,$ENV{LISTPATH});

sub resolve_file {
	my $f = shift;

	for my $p (@path) {
		return "$p/$f" if ( -e "$p/$f");
	}

	my @lin = split(/\n/, `find | grep '$f\$'`);
	for my $l (@lin) {
		print "Found in $l\n";
		return $l;
	}
	print "Not found \n";
	return $f;
}

my $arg = shift;
if ($arg) {
	$arg=~/(.*):(\d*)/;
	my $file=$1;
	my $line=$2;
	if ($line == 0) {
		$file = $arg;
		$line = shift;
		if ($line == 0) {
			if ($inrad) {
				exit(0);
			} else {
				die "No line specified\n";
			}
		}
	}
	$file = resolve_file($file)
		unless(-f "$file");
#print "$file $line\n";
	@lines = split(/\n/,`nl -ba "$file"`);

	for my $myline (@lines) {
		$myline=~/([^\t]*)/;
		my $numline = int($1);
		if (($numline > ($line-5)) && ($numline < ($line+5))) {
			$en = ""; $ch = "  ";
			if ($numline == $line) {
				if ($ENV{COLOR} eq "1") {
					$ch = "\e[47m\e[30m *";
					$en = "\e[0m";
				} else {
					$ch = "*>";
				}
			}
			print " $ch $myline$en\n";
		}
		
	}
} else {
	die("Usage: rsc list <file>:<line>\n".
	    "LISTPATH is used to find sources in a colon separated string\n")
		if ($inrad);
}
