#!/usr/bonsaitools/bin/perl # $Id: search,v 1.9 2003-07-28 21:24:42 leaf%mozilla.org Exp $ # search -- Freetext search # # Arne Georg Gleditsch # Per Kristian Gjermshus # # # 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. ###################################################################### use lib 'lib/'; use LXR::Common; use LXR::Config; $maxhits = 1000; sub search { print("

\n", "Free-text search through the source code, including comments.\n", "
By default, this form treats all characters as literals. \n", "
Search strings can have a maximum of 29 characters.\n", "
Read the documentation", " for help with glimpse's regular expression syntax,\n"); print("\n\n
"); print("
\n"); foreach ($Conf->allvariables) { if ($Conf->variable($_) ne $Conf->vardefault($_)) { print("variable($_), "\">\n"); } } $s = $searchtext; $s =~ s/"/\"/g; print("Search for:
\n", "
\n", "
\n", "{'regexp'} eq 'on') { print (" CHECKED"); print (" value=\"on\">Regular Expression Search\n"); } else { print (">Regular Expression Search\n"); } print ("
\n"); $| = 1; print(''); if ($searchtext ne "") { print("


\n"); unless (open(GLIMPSE, "-|")) { open(STDERR, ">&STDOUT"); $!=''; if ($Conf->{'regexp'} eq 'off') { $searchtext =~ s/([;,#><\-\$.^*[^|()\!])/\\$1/g; } exec($Conf->glimpsebin,"-i","-H".$Conf->dbdir,'-y','-n','-e',$searchtext); print("Glimpse subprocess died unexpectedly: $!\n"); exit; } $numlines = 0; while () { $numlines++; push(@glimpselines,$_); if ($numlines > $maxhits) { last; } } close(GLIMPSE); $retval = $? >> 8; # The manpage for glimpse says that it returns 2 on syntax errors or # inaccessible files. It seems this is not the case. # We will have to work around it for the time being. if ($retval == 0) { if (@glimpselines == 0) { print("No matching files
\n"); } elsif (@glimpselines[0] =~ /pattern too long/) { print ("Pattern too long. Use a maximum 29 characters.\n"); } else { if ($numlines > $maxhits) { print(" Too many hits, displaying first $maxhits
\n"); } $searchtext =~ s/&/&/g; $searchtext =~ s//>/g; print("

$searchtext

\n"); $sourceroot = $Conf->sourceroot; foreach $glimpseline (@glimpselines) { $glimpseline =~ s/$sourceroot//; ($file, $line, $text) = $glimpseline =~ /(.*?):\s*(\d+)\s*:(.*)/; $text =~ s/&/&/g; $text =~ s//>/g; print(&fileref("$file, line $line", "$file", $line), " -- $text
\n"); } } } elsif ($retval == 1) { $glimpsebin = $Conf->glimpsebin; $glimpseresponse = join("
",@glimpselines); $glimpseresponse =~ s/$glimpsebin/Reason/; $glimpseresponse =~ s/glimpse: error in searching index//; print("Search failed
\n$glimpseresponse"); } else { print("Unexpected returnvalue $retval from Glimpse\n"); } } } ($Conf, $HTTP, $Path) = &glimpse_init; $searchtext = $HTTP->{'param'}->{'string'}; $regexp = $HTTP->{'param'}->{'regexp'}; $searchtext =~ tr/+/ /; $searchtext =~ s/%(\w\w)/chr(hex $1)/ge; &makeheader('search'); &search; &makefooter('search');