Home | History | Annotate | Line # | Download | only in util
infosrch revision 1.1
      1 #!/usr/local/bin/perl -w
      2 # Id: infosrch,v 1.2 2004/04/11 17:56:47 karl Exp 
      3 # infosrch does a regex search on an info manual.
      4 # By Harry Putnam <reader (at] newsguy.com>.
      5 
      6 ($myscript = $0) =~ s:^.*/::; 
      7 $six = '';
      8 
      9 if($ARGV[0] eq "help"){
     10    &usage;
     11    exit;
     12 }
     13 if($ARGV[0] eq "-e"){
     14    shift;
     15    $six = "true";
     16 }
     17 if(!$ARGV[1]){
     18    &usage;
     19    exit;
     20 }
     21 
     22 $target = shift;
     23 $regex  = shift;
     24 
     25 $shell_proc  =  "info --output - --subnodes 2>/dev/null $target";
     26 
     27 open(SHELL_PROC," $shell_proc|");
     28 while(<SHELL_PROC>){
     29   chomp;
     30   push @lines,$_;
     31 } 
     32 close(SHELL_PROC);
     33 $cnt = 0;
     34 for(@lines){
     35    if(/$regex/ && !$six){
     36       print "$target\n   $lines[($cnt-1)]\n<$cnt> $lines[$cnt]\n   $lines[($cnt+1)]\n";
     37       print "-- \n";
     38    }elsif(/$regex/ && $six){
     39         print "$target\n";
     40         if($lines[($cnt-6)]){
     41            print "    $lines[($cnt-6)]\n";
     42         }
     43         if($lines[($cnt-5)]){
     44            print "    $lines[($cnt-5)]\n";
     45         }
     46         if($lines[($cnt-4)]){
     47            print "    $lines[($cnt-4)]\n";
     48         }
     49         if($lines[($cnt-3)]){
     50            print "    $lines[($cnt-3)]\n";
     51         }
     52         if($lines[($cnt-2)]){
     53            print "    $lines[($cnt-2)]\n";
     54         }
     55         if($lines[($cnt-1)]){
     56            print "    $lines[($cnt-1)]\n";
     57         }
     58         if($lines[$cnt]){
     59            print "$cnt $lines[$cnt]\n";
     60         }
     61         if($lines[($cnt+1)]){
     62            print "    $lines[($cnt+1)]\n";
     63         }
     64         if($lines[($cnt+2)]){
     65            print "    $lines[($cnt+2)]\n";
     66         }
     67         if($lines[($cnt+3)]){
     68            print "    $lines[($cnt+3)]\n";
     69         }
     70         if($lines[($cnt+4)]){
     71            print "    $lines[($cnt+4)]\n";
     72         }
     73         if($lines[($cnt+5)]){
     74            print "    $lines[($cnt+5)]\n";
     75         }
     76         if($lines[($cnt+6)]){
     77            print "    $lines[($cnt+6)]\n";
     78         }
     79         print "-- \n";
     80      }
     81      $cnt++;
     82 }        
     83 
     84 sub usage {
     85   print <<EOM;
     86 
     87 Purpose: Extract full text from info node and search it by regex
     88 Usage: $myscript [-e] TARGET REGEX
     89 
     90 Where TARGET is an info node such as `emacs', `bash' etc, and
     91 REGEX is what you want to find in it.
     92 
     93 The -e flag is not required but if used then 6 lines preceding and six
     94 lines following any hits will be printed.  The default (with no -e flag)
     95 is to print one line before and after.
     96 
     97 The output has the line number prepended to the line containing the
     98 actual regex.
     99 
    100 Info command used:
    101   info --output - --subnodes 2>/dev/null TARGET
    102 
    103 EOM
    104 }
    105