/[wait]/trunk/script/sman
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/script/sman

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (hide annotations)
Mon May 24 13:44:01 2004 UTC (19 years, 11 months ago) by dpavlin
File size: 12337 byte(s)
move cvs-head to trunk

1 ulpfr 19 #!perl -w
2     # -*- Mode: Perl -*-
3     # $Basename: sman $
4     # $Revision: 1.14 $
5     # Author : Ulrich Pfeifer
6     # Created On : Fri Aug 30 15:52:25 1996
7     # Last Modified By: Ulrich Pfeifer
8     # Last Modified On: Mon May 8 11:03:46 2000
9     # Language : CPerl
10     #
11     # (C) Copyright 1996-2000, Ulrich Pfeifer
12     #
13 ulpfr 10
14     use strict;
15    
16     use Term::ReadLine;
17     use Getopt::Long;
18 laperla 65 use Fcntl qw(O_RDONLY);
19 ulpfr 10 use Config;
20    
21 dpavlin 86 use '/data/wait/lib';
22    
23 ulpfr 10 require WAIT::Config;
24     require WAIT::Database;
25     require WAIT::Query::Base;
26     require WAIT::Query::Wais;
27    
28    
29     $SIG{PIPE} = 'IGNORE';
30     my %OPT = (database => 'DB',
31     dir => $WAIT::Config->{WAIT_home} || '/tmp',
32     table => 'man',
33     pager => $WAIT::Config->{'pager'} || 'more',
34     filter => 0,
35     max => 15,
36     );
37    
38     GetOptions(\%OPT,
39     'database=s',
40     'dir=s',
41     'table=s',
42     'filter=i',
43     'max=i',
44 laperla 65 'pager:s') || die "
45     Usage: $0
46     [--database database]
47     [--dir dir ]
48     [--table table ]
49     [--filter integer ]
50     [--max integer ]
51     [--pager pager ]
52     ";
53 ulpfr 10
54     my $db = WAIT::Database->open(name => $OPT{database},
55     mode => O_RDONLY,
56     directory => $OPT{dir})
57     or die "Could not open database $OPT{database}: $@";
58    
59     my $tb = $db->table(name => $OPT{table})
60     or die "Could not open table $OPT{table}: $@";
61    
62     # not used: my $layout = $tb->layout; # a WAIT::Parse::Nroff object
63    
64     my $term = new Term::ReadLine 'Simple Query Interface';
65    
66     require WAIT::Format::Term;
67     my $format;
68     if ($Config::Config{'archname'} eq 'i586-linux') {
69     # for color xterm
70     $format = new WAIT::Format::Term query_s => "", query_e => "";
71     } else {
72     $format = new WAIT::Format::Term;
73     }
74    
75     my $pager = ($OPT{pager}) ? \&pager : \&less;
76     my $OUT = $term->OUT;
77    
78     my $st = 1;
79     print $OUT "Enter 'h' for help.\n";
80    
81     # sman is basically offering three services: find the hits and show
82     # them (a query), show metadata for a hit (a view), show a hot (display)
83    
84     my($query, @did);
85    
86     while (defined ($_ = &myreadline("$st> "))) {
87     chomp; $st++;
88    
89     my(%hits, $query_text);
90     if (/^$/) {
91     next;
92     } elsif (/^m (\d+)$/) {
93     $OPT{max} = $1;
94     } elsif (/^f\s*(\d+)?$/) {
95     $OPT{filter} = $1;
96     next;
97     } elsif (/^t$/i) {
98     if ($pager eq \&less) {
99     $pager = \&pager;
100     } else {
101     $pager = \&less;
102     }
103     next;
104     } elsif (/^(\d+)$/) {
105     if (defined $did[$1]) {
106     display($did[$1]); # <----------- display (full doc)
107     next;
108     }
109     } elsif (/^d\s*(\d+)/) {
110     if (defined $did[$1]) {
111     view($did[$1]); # <----------- view (metadata from WAIT)
112     next;
113     }
114     } elsif (/^q$/i) {
115     last;
116     } elsif (/^l$/i) {
117     # fall through
118     } elsif (/^[h?]$/i) {
119     help();
120     next;
121     } elsif (/^hh$/i) {
122     extended_help();
123     next;
124     } else { # <----------- A query (Display a list)
125     $query_text = $_;
126     eval {$query = WAIT::Query::Wais::query($tb, $_)};
127     if ($@ ne '') {
128     print $OUT "$_ => $query\n\$\@='$@'\n";
129     } elsif (ref($query)) {
130 ulpfr 19 %hits = $query->execute(top => $OPT{max}, picky => 1);
131 ulpfr 10 # the hash %hits has as keys document numbers and as values
132     # quality figures. The doc numbers are not what we have as docid
133     # to find the item in the access class, they are WAIT's private
134     # numbers.
135     } else {
136     next;
137     }
138     }
139    
140     next unless %hits;
141     my $no = 1; # numbering the hits for the result table that is
142     # presented to the user
143    
144     @did = (); # store the internal numbers (keys of %hits). The user
145     # will use $no in sman's interface to select a hit.
146    
147     # the following loop uses the values of %hits to sort the results
148     # according to the quality and cut after a number of rows. After
149     # that %hits isn't needed anymore.
150     print "Query: $query_text\n";
151     for my $did (sort {$hits{$b} <=> $hits{$a}} keys %hits) {
152    
153     my %tattr = $tb->fetch($did);
154     # the hash %tattr contains several attributes of the item we are
155     # referring to, namely the attributes that we named in the "attr"
156     # argument of the create_table statement in smakewhatis
157    
158     printf $OUT "%2d %6.3f %s\n", $no, $hits{$did},
159     substr($tattr{headline} ||'',0,68);
160     $did[$no] = $did;
161     last if $no++ >= $OPT{max};
162    
163     }
164    
165     } continue {
166 ulpfr 19 # we don't do this since Andreas Koenig does not think of it as feature
167 ulpfr 10 # $term->SetHistory(grep length($_)>4, $term->GetHistory)
168     }
169 ulpfr 19 warn "Thank you for using sman\n";
170 ulpfr 10
171 ulpfr 19 $tb->close;
172     $db->close;
173    
174 ulpfr 10 sub myreadline {
175     if (@ARGV) {
176     return shift @ARGV;
177     } else {
178     $term->readline(@_);
179     }
180     }
181     sub help {
182     my $idb = "\n\t'". join(q[', '], $tb->fields()) . "'";
183     print $OUT qq[Available commands:
184    
185     <num> Show the document <num>
186     d <num> Show the db entry of document <num>
187     f <num> Display only <num> lines context
188     h,? Display this help message
189     hh Display query examples
190     m <num> Set maxhits to <num>
191     t Toggle display mode (term/less)
192     q Exit from $0
193     l redisplay last ranking
194     Other input is tried as wais query.
195     The following fields are known: $idb
196     ] ;
197     }
198    
199     sub extended_help {
200     print q{
201     Here are some query examples:
202    
203     information retrieval free text query
204     information or retrieval same as above
205     des=information retrieval `information' must be in the description
206     des=(information retrieval) one of them in description
207     des=(information or retrieval) same as above
208     des=(information and retrieval) both of them in description
209     des=(information not retrieval) `information' in description and
210     `retrieval' not in description
211     des=(information system*) wild-card search
212     au=ilia author names may be misspelled
213    
214     You can build arbitary boolean combination of the above examples.
215     Field names may be abbreviated.
216     }
217     }
218    
219     sub view {
220     my $did = shift;
221     my %tattr = $tb->fetch($did);
222     for (keys %tattr) {
223     print $OUT "$_ $tattr{$_}\n";
224     }
225     }
226    
227     sub display {
228     my $did = shift;
229    
230     return unless defined $query and defined $did;
231    
232     print $OUT "Wais display document $did\n";
233     my %tattr = $tb->fetch($did);
234     my $tdid = $tattr{docid};
235     # WHAT DOES HE DO HERE? ULI???
236     # Re: some indexing scripts did use pathnames relative to the table directory
237     # especially the cpanwait script does this. uli
238 dpavlin 86 if ($tdid !~ m(^/)) {
239     $tdid = $tb->dir . '/' . $tdid;
240     }
241 ulpfr 10
242     # The main task of all that follows from here is highlighting. WAIT
243     # is designed to make it possible to show the user why a certain
244     # document was chosen by the indexer.
245    
246     my $buf = $tb->fetch_extern($tdid);
247     # This $buf can be an object that can have enough information to do
248     # highlighting without WAIT's help. If you prefer to implement your
249     # own highlighting, you can do so now with e.g. print
250     # $buf->highlight(query => $query)
251    
252     # All you need to know to implement highlighting is how a
253     # WAIT::Query::Base object looks like (left as an exercise for the
254     # reader).
255    
256     # The impatient reader may want to implement something without
257     # highlighting, in which case he does not need any info about the
258     # query object and can rightaway run e.g.
259     # print $buf->as_string
260    
261     # Thus the impatient reader does not necessarily need the following
262     # heavy wizardry. Just to give you an idea what's going on: every
263     # word in the text must be compared to every word in the query if it
264     # is worth highlighting, and which part of the word is worth
265     # highlighting. This must be done differently for every field in the
266     # table and for every index defined for that field. Try to run a
267     # query with 100 words and you'll be amazed to see it really works.
268     # Or maybe it doesn't. You should be aware that the hilighting code
269     # is to be regarded as alpha. It is certainly the least tested part
270     # of WAIT so far.
271    
272     if ($buf) {
273     my @txt = $query->hilight($buf);
274     # In this operation the following things melt into one piece:
275     # $query: The query entered by the user (Class isa WAIT::Query::Base)
276     # $tb: The table we queried (Class WAIT::Table)
277     # $buf: The document to display (User defined class or string)
278     # The steps taken are:
279     # 1.) $query calls "hilight" on $tb and passes
280     # filtered and raw search terms ($query->{Plain} and $query->{Raw}).
281     # 2.) $tb asks the layout object to tag the object which results
282     # in an array with alternating elements of tags (anon HASHes) and
283     # strings.
284     # 3.) $tb adds some markup on its own: {qt=>1} or some such
285    
286     # The result of that process can optionally be sent through a
287     # filter, just to impress your friends with yet more heavy
288     # wizardry
289     if ($OPT{filter}) {
290     @txt = &filter(@txt);
291     }
292    
293     # And then a formatter (in our case a terminal formatter) turns
294     # all the markup into escape sequences and strings that can in
295     # turn be sent through a pager for instance
296     &$pager($format->as_string(\@txt));
297     }
298    
299     # Hey, that's it. The user out there is deeply impressed now. You
300     # can lean back again:-) He got a document that has some words
301     # hilighted and will probably read and enjoy it. Maybe he'll send
302     # you an email.
303     }
304    
305     sub filter {
306     my @result;
307     my @context;
308     my $lines = 0;
309     my $clines = 0;
310     my $elipsis = 0;
311    
312     print STDERR "Filter ...";
313     while (@_) {
314     my %tag = %{shift @_};
315     my $txt = shift @_;
316    
317     for (split /(\n)/, $txt) {
318     if ($_ eq "\n") {
319     if (exists $tag{_qt}) {
320     #die "Weird!";
321     push @result, {_i=>1}, "[WEIRD]";
322     } elsif ($lines) {
323     push @result, {}, $_;
324     $lines--;
325     } else {
326     push @context, {}, $_;
327     $clines++;
328     }
329     } else {
330     if (exists $tag{_qt}) {
331     push @result, {_i=>1}, "\n[ $elipsis linesĀ ]\n" if $elipsis;
332     push @result, @context, {%tag}, $_;
333     delete $tag{_qt};
334     @context = (); $clines = 0; $elipsis=0;
335     $lines = $OPT{filter}+1;
336     } elsif ($lines) {
337     push @result, \%tag, $_;
338     } else {
339     push @context, \%tag, $_;
340     }
341     }
342     if ($clines>$OPT{filter}) {
343     my (%tag, $txt);
344     while ($clines>$OPT{filter}) {
345     %tag = %{shift @context};
346     $txt = shift @context;
347     if ($txt =~ /\n/) {
348     $clines--;
349     $elipsis++;
350     }
351     }
352     }
353     }
354     }
355     print STDERR " done\n";
356     @result;
357     }
358    
359     sub less {
360     my $flags;
361     if ($WAIT::Config->{pager} =~ /less/) {
362     $flags = '-r';
363     } elsif ($WAIT::Config->{pager} =~ /more/) {
364     $flags = '-c';
365     }
366     open(PAGER, "|$WAIT::Config->{pager} $flags") or die;
367     print PAGER @_;
368     close PAGER;
369     }
370    
371     sub pager {
372     my @lines = split /\n/, $_[0];
373     my $line = 0;
374     for (@lines) {
375     print "$_\n"; $line++;
376     if ($line % 24 == 0) {
377     my $key = $term->readline("[return]");
378     return if $key =~ /^q/i;
379     }
380     }
381     }
382    
383    
384     __END__
385     ## ###################################################################
386     ## pod
387     ## ###################################################################
388    
389     =head1 NAME
390    
391     sman - Search and disply manuals interactive
392    
393     =head1 SYNOPSIS
394    
395     B<sman>
396     [B<-database> I<database name>]
397     [B<-dir> I<database directory>]
398     [B<-table> I<name>]
399     [B<-less>]
400     [B<-filter> I<num>]
401     [B<-max> I<num>]
402    
403     =head1 DESCRIPTION
404    
405     B<Sman> is an interactive search interface to your systems manual pages.
406    
407     =head2 OPTIONS
408    
409     =over 10
410    
411     =item B<-database> I<database name>
412    
413     Change the default database name to I<database name>.
414    
415     =item B<-dir> I<database directory>
416    
417     Change the default database directory to I<database directory>.
418    
419     =item B<-table> I<name>
420    
421     Use I<name> instead of C<man> as table name.
422    
423     =item B<-pager> I<name>
424    
425     Use I<name> instead of the default pager. If no I<name> is supplied a
426     buildin pager is used.
427    
428     =item B<-filter> I<num>
429    
430     Display only I<num> lines above and below an occurance of a search
431     term in the manual.
432    
433     =item B<-max> I<num>
434    
435     Display only I<num> hits. Default is to 10.
436    
437     =head1 SEE ALSO
438    
439     L<smakewhatis>.
440    
441     =head1 AUTHOR
442    
443     Ulrich Pfeifer E<lt>F<pfeifer@ls6.informatik.uni-dortmund.de>E<gt>

Properties

Name Value
cvs2svn:cvs-rev 1.2
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26