find 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/perl
  2. # $Id: find,v 1.9 2006/12/07 04:59:38 reed%reedloden.com Exp $
  3. # find -- Find files
  4. #
  5. # Arne Georg Gleditsch <argggh@ifi.uio.no>
  6. # Per Kristian Gjermshus <pergj@ifi.uio.no>
  7. #
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. ######################################################################
  23. use lib 'lib';
  24. use LXR::Common;
  25. use LXR::Config;
  26. my $hint;
  27. my $lineno;
  28. my @args;
  29. sub find {
  30. print '
  31. <p class=desc>
  32. Search for files (by name) using <a
  33. href="search-help.html">regular expressions</a>.
  34. </p>
  35. ';
  36. if ($Conf->{'treename'} ne '') {
  37. print &bigexpandtemplate('<script src="$dotdoturl/script.js"></script>');
  38. }
  39. print '<form name=find id=find method=get action="find" class="beforecontent">
  40. ';
  41. my @extras = qw(rev mark);
  42. foreach $extra (@extras) {
  43. if (defined $HTTP->{'param'}->{$extra} &&
  44. $HTTP->{'param'}->{$extra} =~ /^([-0-9a-f,.]+)$/i) {
  45. print qq{<input id="$extra" name="$extra" value="$1" type="hidden">
  46. };
  47. push @args, ("$extra=$1");
  48. }
  49. }
  50. foreach ($Conf->allvariables) {
  51. if ($Conf->variable($_) ne $Conf->vardefault($_)) {
  52. print '<input type=hidden name="' . $_ . '" '.
  53. 'value="' . $Conf->variable($_) . '">
  54. ';
  55. }
  56. }
  57. $searchtext = cleanquery $searchtext;
  58. $lineno = $HTTP->{'param'}->{'line'};
  59. $lineno =~ s/\D+//g;
  60. $hint = clean_hint $hint;
  61. print qq{
  62. <b><label for="string">Find file:</label></b>
  63. <input type=text id="string" name="string"
  64. value="$searchtext" size=50>};
  65. if ($Conf->{'treename'} ne '') {
  66. print ' <label for="tree">in</label>
  67. <select name=tree id=tree onchange="changetarget()">';
  68. my @treelist = @{$Conf->{'trees'}};
  69. foreach my $othertree (@treelist) {
  70. my $default=$othertree eq $Conf->{'treename'} ? ' selected=1' : '';
  71. print "
  72. <option$default value='$othertree'>$othertree</option>";
  73. }
  74. print '
  75. </select>
  76. ';
  77. }
  78. print qq{<input type=submit value="search"><br>
  79. <b><label for="hint" title="each matching path is favored,
  80. only files with the most matches will be shown">Directory hints</label></b>:
  81. <input id="hint" name="hint" value="$hint">
  82. </form>
  83. };
  84. print "<br>";
  85. if ($searchtext ne "") {
  86. my $filename = $Conf->dbdir."/.glimpse_filenames";
  87. unless (open(FILELLISTING, $filename)) {
  88. &warning("Could not open $filename", 'searchfile');
  89. return;
  90. }
  91. print "<p><hr>\n";
  92. $searchtext =~ s/\+/\\+/g;
  93. if ($searchtext =~ /^(\s*)(.*?)(\s*)$/ &&
  94. (($1 ne '') || ($3 ne ''))) {
  95. my $find = cleanquery $2;
  96. print qq%<p><i>Your search included <u>spaces</u></i>,
  97. if this was not your <b>intent</b>,
  98. <i>you can always search <a href="find?string=$find">without them</a>.</i></p>%;
  99. }
  100. print qq%<p><i>If you can't find what you're looking for, you can always
  101. <a href="search?string=$searchtext&regexp=on">search</a> for it.</i></p>%;
  102. $sourceroot = $Conf->sourceroot;
  103. $file = <FILELLISTING>;
  104. if ($file !~ /^\d+$/) {
  105. &warning("glimpse file format doesn't match expectations.", 'glimpsedb');
  106. return;
  107. }
  108. my $highscore = 0;
  109. my @matches = ();
  110. my @hints = ();
  111. if ($hint ne '') {
  112. $hint =~ s/\./\\./g;
  113. $hint =~ s/\|/\\b\|\\b/g;
  114. $hint = "\\b$hint\\b";
  115. @hints = sort {length $b <=> length $a} (split /\|/, $hint);
  116. }
  117. while ($file = <FILELLISTING>) {
  118. $file =~ s/^$sourceroot//;
  119. if ($file =~ /$searchtext/i) {
  120. my $filepath='';
  121. $filename = $file;
  122. my $score = 0;
  123. for $hint (@hints) {
  124. ++$score if ($filename =~ s/$hint//);
  125. }
  126. ($file, $filename) = split m|/(?!.*/)|, $file;
  127. print "<span class='s$score'>";
  128. if (length $file) {
  129. foreach my $filepart (split m|/|, $file) {
  130. $filepath .= "$filepart/";
  131. print &fileref($filepart ? $filepart : '/', "$filepath").
  132. ($filepart && '/');
  133. }
  134. } else {
  135. $filepath = '/';
  136. print &fileref('/', "/");
  137. }
  138. $filepath.=$filename;
  139. push @args, "force=1" if ($filename =~ /\.html?$/);
  140. push @args, $markstring if $markstring ne '';
  141. print &fileref("$filename", "$filepath", "$lineno", @args) .
  142. '<br>
  143. ';
  144. print "</span>";
  145. if ($score > $highscore) {
  146. my @classes = ();
  147. for (; $highscore < $score; ++$highscore) {
  148. push @classes, ".s$highscore";
  149. }
  150. local $, = ", ";
  151. print "<style>";
  152. print @classes;
  153. print "{ display:none }</style>";
  154. }
  155. }
  156. }
  157. }
  158. }
  159. ($Conf, $HTTP, $Path, $head) = &init;
  160. my $searchtext2 = $HTTP->{'param'}->{'text'};
  161. $searchtext = $HTTP->{'param'}->{'string'};
  162. my $tree = $HTTP->{'param'}->{'tree'};
  163. $hint = $HTTP->{'param'}->{'hint'} || '';
  164. my $verb = 'find';
  165. my $refresh;
  166. my $extra;
  167. if ($searchtext2 ne '') {
  168. if (defined $HTTP->{'param'}->{'i'} || $HTTP->{'param'}->{'kind'} eq 'ident') {
  169. $verb = 'ident';
  170. $searchtext2 =~ s/\+//g;
  171. $searchtext2 =~ s/\s+//g;
  172. $extra = 'i=' . url_quote($searchtext2);
  173. $extra .= '&filter=' . url_quote($searchtext) if $searchtext;
  174. } else {
  175. $verb = 'search';
  176. $extra = 'string=' . url_quote($searchtext2);
  177. $extra .= '&find=' . url_quote($searchtext) if $searchtext;
  178. $extra .= '&regexp=1' if $HTTP->{'param'}->{'kind'} eq 'regexp';
  179. }
  180. }
  181. if ($verb ne 'find' || ($tree && ($tree ne $Conf->{'treename'}))) {
  182. my @treelist = @{$Conf->{'trees'}};
  183. my $foundtree;
  184. foreach my $othertree (@treelist) {
  185. next unless $othertree eq $tree;
  186. $foundtree = $othertree;
  187. last;
  188. }
  189. $foundtree ||= $Conf->{'treename'} if $verb ne 'find';
  190. if ($foundtree) {
  191. my @tail = ();
  192. if ($extra) {
  193. push @tail, $extra;
  194. } else {
  195. push @tail, "string=" . url_quote($searchtext) if $searchtext ne '';
  196. }
  197. push @tail, "hint=" . url_quote($hint) if $hint ne '';
  198. my $tail = $#tail >= 0 ? '?' . join "&", @tail : '';
  199. $refresh .= "Refresh: 0; url=../$foundtree/$verb$tail
  200. ";
  201. }
  202. }
  203. print "$head$refresh
  204. ";
  205. exit if $refresh ne '';
  206. &makeheader('find');
  207. &find;
  208. &makefooter('find');
  209. 1;