Local directory search "server" script 
Author Message
 Local directory search "server" script

Now that my X server is working again I've put together the first
version of my first half-way useful script. It is a "one shot" server
that's started by inetd for each request on a specific port set in
/etc/inetd.conf. That is at least how I use it on Linux; I can't say
what to do on Windows.

*** If you use this script, or even only try it, make sure that your
firewall blocks the port where it is listening! ***

The script uses two variants of grep: agrep and sgrep. The former
because it's a lot faster than what I could come up with for searching
files; the later because I've been lazy. Incidentally, sgrep is pretty
slow for this task, but it is incredibly neat for ad hoc extracting of
parts from structured files -- are there similarly comfortable ways to
do it natively in Ruby?

Todo
- Browser integration through a JavaScript triggered from the toolbar.
- Resolving of symbolic links.
- Searching of compressed files.
- Nice output formatting.
- Replacement of sgrep with something faster.
- "Transclusion" of CGI.unescape

Thanks for your help in writing this piece.

Michael

#! /usr/bin/ruby -w

require 'cgi'

def main




  args = parseArgs(getRequest())

  search(args['pattern'], args['dir'])

Quote:
})

  exit 0
end

def getRequest()
  rawRequest = $stdin.sysread(1024)
  request = rawRequest.split('\r\n').first
  request =~ /searchdir\?(.*) HTTP/
  $1
end

def parseArgs(rawArgs)
  args = {}
  rawArgs.split('+').each do |kv|
    key, value = kv.split('=')
    args[key.strip().downcase()] = CGI.unescape(value.strip())
  end
  args
end

def search(pat, dir)

  raise "Search for \"#{pat}\" in #{dir} failed with #{$?}!" if $? != 0
  matches.each() do |m|
    file, occ = m.split(':')
    occ = occ.to_i
    if occ > 0


    end
  end
end

def output(pat, dir, hits)
  print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n"

  print "<html>\r\n"
  print "<head><title>Search result for \"#{pat}\"</title></head>\r\n"
  print "<body>\r\n"

#{dir}</h1></p>\r\n"

  hits.each do |hit|
    formatHit(*hit)
  end

  print "</body></html>\r\n"
end

def formatHit(file, occ)
  linktext = file
  if html?(file)
    title = extractTitle(file)
    if !title.empty?
      linktext = title
    end
  end
  print "<p><a href=\"file://#{file}\">#{linktext}</a></p>\r\n"
end

def html?(file)
  name = File.basename(file)
  ext = file.split(".").last()
  ext.downcase!()
  ext.include?("htm")
  #false
end

def extractTitle(file)
  # This is slow, but excessively practical
  `sgrep -g html 'first(1, stag("TITLE") __ etag("TITLE") in
(stag("HEAD") .. etag("HEAD")))' #{file}`.strip()
end

main()

--
Michael Schuerig

http://www.*-*-*.com/



Wed, 23 Jul 2003 09:40:14 GMT  
 Local directory search "server" script

Quote:
>#! /usr/bin/ruby -w

>require 'cgi'

>def main





==Possible stupidity disclaimer ;)

Thanks
Mike Wilson
Unix Administrator
http://ruby.weblogs.com
___________________________________________________
GO.com Mail                                    
Get Your Free, Private E-mail at http://mail.go.com



Wed, 23 Jul 2003 10:30:35 GMT  
 Local directory search "server" script

Quote:

> >#! /usr/bin/ruby -w

> >require 'cgi'

> >def main




> ==Possible stupidity disclaimer ;)
> I've never seen instance variables used in a non-OO program yet, do you do
> that because you create a main method and you want those visible through
> the whole file while still being able to define them within your method? I


It's not meant to be clever, it's just the first thing that occurred to
me...

Michael

--
Michael Schuerig

http://www.schuerig.de/michael/



Wed, 23 Jul 2003 17:24:55 GMT  
 Local directory search "server" script

Quote:
-----Original Message-----


Date: Sat Feb 03 06:10:02 PST 2001
Subject: [ruby-talk:10306] Re: Local directory search "server" script


>> >#! /usr/bin/ruby -w

>> >require 'cgi'

>> >def main




>> ==Possible stupidity disclaimer ;)
>> I've never seen instance variables used in a non-OO program yet, do you do
>> that because you create a main method and you want those visible through
>> the whole file while still being able to define them within your method? I

>It's not meant to be clever, it's just the first thing that occurred to
>me...

I just realized that it's possible that you/someone may have thought that the "stupidity disclaimer" was referring to my opinion of what you were doing.  It wasn't, it was referring to me and my possibly stupid question.  I hope you understood that, sorry if you didn't.  I really need to think before I type...

Quote:
>Michael

>--
>Michael Schuerig

>http://www.schuerig.de/michael/

Mike Wilson
Unix Administrator
http://ruby.weblogs.com
___________________________________________________
GO.com Mail                                    
Get Your Free, Private E-mail at http://mail.go.com


Wed, 23 Jul 2003 22:56:40 GMT  
 Local directory search "server" script

Quote:

>...

> I just realized that it's possible that you/someone may have thought that the
> "stupidity disclaimer" was referring to my opinion of what you were doing.
> It wasn't, it was referring to me and my possibly stupid question.  I hope
> you understood that, sorry if you didn't.  I really need to think before I
> type...

Dunno about that.  This forum has always seemed to have an uncommonly
high civility quotient, in that when you say something ambiguous, your
civility tends to be assumed by everyone else.

I too have found myself in the habit of using instance vars in "non-oo"
scripts. Haven't really thought through my reasons, or the consequences.
It may just come down to having been told by someone, in the misty
forgotten past, that globals were an unqualified evil. :-)

  -- Mark



Thu, 24 Jul 2003 00:00:50 GMT  
 Local directory search "server" script

Quote:

> -----Original Message-----


> Date: Sat Feb 03 06:10:02 PST 2001
> Subject: [ruby-talk:10306] Re: Local directory search "server" script


> >> >#! /usr/bin/ruby -w

> >> >require 'cgi'

> >> >def main




> >> ==Possible stupidity disclaimer ;)
> >> I've never seen instance variables used in a non-OO program yet, do you do
> >> that because you create a main method and you want those visible through
> >> the whole file while still being able to define them within your method? I

> >It's not meant to be clever, it's just the first thing that occurred to
> >me...

> I just realized that it's possible that you/someone may have thought that
> the "stupidity disclaimer" was referring to my opinion of what you were
> doing.

No, I didn't even think of this. And anyway, I'm a Ruby Nuby and
therefore particularly interested in suggestions regarding style.
Actually, I changed the instances vars to globals in the meantime.

Michael

--
Michael Schuerig

http://www.schuerig.de/michael/



Thu, 24 Jul 2003 01:54:05 GMT  
 Local directory search "server" script

Quote:
-----Original Message-----


Date: Sat Feb 03 10:10:03 PST 2001
Subject: [ruby-talk:10313] Re: Local directory search "server" script


>> -----Original Message-----


>> Date: Sat Feb 03 06:10:02 PST 2001
>> Subject: [ruby-talk:10306] Re: Local directory search "server" script


>> >> >#! /usr/bin/ruby -w

>> >> >require 'cgi'

>> >> >def main




>> >> ==Possible stupidity disclaimer ;)
>> >> I've never seen instance variables used in a non-OO program yet, do you do
>> >> that because you create a main method and you want those visible through
>> >> the whole file while still being able to define them within your method? I

>> >It's not meant to be clever, it's just the first thing that occurred to
>> >me...

>> I just realized that it's possible that you/someone may have thought that
>> the "stupidity disclaimer" was referring to my opinion of what you were
>> doing.

>No, I didn't even think of this. And anyway, I'm a Ruby Nuby and
>therefore particularly interested in suggestions regarding style.
>Actually, I changed the instances vars to globals in the meantime.

>Michael

>--
>Michael Schuerig

>http://www.schuerig.de/michael/

I wasn't advocating the use of globals (I'm assuming when you say global, you mean $var), only trying to learn what using instance variables brings to the table when not used in class instances.  

I guess this leads to a question of how to best define variables within ruby.  Personally, I usually just define the necessary variables at the top of the file, outside of any method, but I'm not positive that that's the correct way either.  Anyone more knowledgable about this that can give advice?  

Mike Wilson
Unix Administrator
http://ruby.weblogs.com
___________________________________________________
GO.com Mail                                    
Get Your Free, Private E-mail at http://mail.go.com



Thu, 24 Jul 2003 03:19:37 GMT  
 Local directory search "server" script

Quote:


> Dunno about that.  This forum has always seemed to have an uncommonly
> high civility quotient, in that when you say something ambiguous, your
> civility tends to be assumed by everyone else.

What's wrong with us? Are we some kind of rebel cult going against the flow of
flames? LOL

Quote:
> It may just come down to having been told by someone, in the misty
> forgotten past, that globals were an unqualified evil. :-)

Well, globals aren't _inherently_ evil; it is how they are used (as in is their
use justified and proper) that determines that. GOTO has acquired the same
reputation, although, carefully and cleverly used, it is justified.

In some languages globalization is assumed and local variables must be
specifically declared as such. In others, including Ruby, localization is
assumed and global variables must specifically be declared as such. Instance
variables are a special hybrid -- local at the class level but 'global' within
the class (i.e. visible to all methods)

Regards,

Kent Starr



Thu, 24 Jul 2003 03:56:23 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Run a local script while "expecting"

2. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

3. Witness Lee and the "Local Churches": A Personal Testimony

4. Clipper Debugger "loses" Local variables

5. "Local" Batch Files

6. "local l : like Result"

7. local uses of "vars"

8. Dr. Scheme "local"?

9. Question about "exec in globals, locals"

10. Extraneous "eval" in ::itcl::local

11. New Tcl magazine "Tickle Talk", buy it at your local newsstand today

12. That old "Create Directory" question again

 

 
Powered by phpBB® Forum Software