#!/usr/bin/perl
# $Id: ident,v 1.11 2001/10/16 20:38:37 pergj Exp $

# ident --	Look up identifiers
#
#	Arne Georg Gleditsch <argggh@ifi.uio.no>
#	Per Kristian Gjermshus <pergj@ifi.uio.no>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

######################################################################

$CVSID = '$Id: ident,v 1.11 2001/10/16 20:38:37 pergj Exp $ ';

use strict;
use lib do { $0 =~ m{(.*)/} ? "$1/lib" : "lib" };

use LXR::Common qw(:html);
use Local;


sub varinputs {
    my $ret = '';
    foreach ($config->allvariables) {
	if ($config->variable($_) ne $config->vardefault($_)) {
	    $ret .= "<input type=hidden name='$_' value='" . 
	      $config->variable($_) . "'>\n";
	}
    }
    return $ret;
}

sub refexpand {
    my $templ = shift;
    my $ret = '';

    my @refs = $index->getindex($identifier, $release);

    my $def;
    foreach my $def (@refs) {
	my ($file, $line, $type, $rel) = @$def;
	
	$rel &&= "(member of ".idref($rel, "search-member", $rel).")";
	$ret .= expandtemplate($templ,
			       (file => sub { $file },
				line => sub { $line },
				type => sub { $type_names{$type} },
				rel  => sub { $rel },
				fileref  => sub {
				    fileref("$file, line $line", 
					    "search-decl",
					    $file, $line);
				}
			       ));
	
# 	print("<span class=search-li1> $type_names{$type} in ".
# 	      fileref("$file, line $line", "search-decl",
# 		      $file, $line).
# 	      " $rel</span>\n");
    }
    
    return $ret;
}

sub usesexpand {
    my $templ = shift;
    my $ret = '';

    my @uses = $index->getreference($identifier, $release);
    foreach my $ref (sort { $$a[0] cmp $$b[0] } @uses) {
	my ($file, $line) = @$ref;
	$ret .= expandtemplate($templ,
			       (
				file => sub { $file },
				line => sub { $line },
				fileref => sub {
				    fileref("$file, line $line", "search-ref",
					    $file, $line);
				}
			       ));
    }
    
    return $ret;
}

sub printident {
    my $dir = shift;
    my $templ;
    
    #$templ = "<ul>\n\$files{\n<li>\$iconlink \$namelink\n}</ul>\n";
    if ($config->htmlident) {
	unless (open(TEMPL, $config->htmlident)) {
	    warning("Template ".$config->htmlident." does not exist.");
	} else {
	    local($/) = undef;
	    $templ = <TEMPL>;
	    close(TEMPL);
	}
    }
    
    # print the description of the current directory
    #dirdesc($dir);
    
    # print the listing itself
    print(expandtemplate($templ,
			 (test => sub { "testik" },
			  variables => \&varinputs,
			  identifier => sub { return $identifier },
			  refs => sub { refexpand(@_) },
			  uses => sub { usesexpand(@_) },
			 )
			));
}

sub ident_ { 
    print("Type the full name of an identifier to summarize ",
	  "(a function name, variable name, typedef, etc).\n",
	  "<p>Matches are case-sensitive.\n");                    

    print("<form method=get action=\"ident\">\n");

    foreach ($config->allvariables) {
	if ($config->variable($_) ne $config->vardefault($_)) {
	    print("<input type=hidden name=\"",$_, "\" ",
		  "value=\"", $config->variable($_), "\">\n");
	}
    }
    
    print("<b>Identifier: </b><input type=text name=\"i\" ",
	  "value=\"",$identifier,"\" size=15>\n",
	  "<input type=submit value=\"Find\">\n",
          "</form>\n");     

    
    if ($identifier) {
	my @refs = $index->getindex($identifier, $release);

	print("<span class=search-ident>$identifier</span>\n");

	if (@refs) {
	    my $def;

	    # print("<ul>\n"); CSS _PH_
	    print "<span class=search-ul>\n";
	    while ($def = shift(@refs)) {
		my ($file, $line, $type, $rel) = @$def;

		$rel &&= "(member of ".idref($rel, "search-member", $rel).")";
		
		print("<span class=search-li1> $type_names{$type} in ".
		      fileref("$file, line $line", "search-decl",
			      $file, $line).
		      " $rel</span>\n");
	    }
	    print "</span>\n";
	    # print("</ul>\n");

	    my @uses = $index->getreference($identifier, $release);

	    if (@uses) {
		@uses = sort { $$a[0] cmp $$b[0] } @uses;

		my $ref;
		while ($ref = shift(@uses)) {
		    my ($file, $line) = @$ref;

		    print("<span class=search-li2> ".
			  fileref("$file, line $line", "search-ref",
				  $file, $line).
			  "</span>\n");
		}
	    }
	}
	else {
	    print("<br><b>Not used</b>");
	}
    }
}


httpinit;

makeheader('ident');
printident;
makefooter('ident');
httpclean;

