2001-05-17 06:02:47 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# shell/filter.rb -
|
2011-05-18 20:07:25 -04:00
|
|
|
# $Release Version: 0.7 $
|
|
|
|
# $Revision$
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
class Shell
|
|
|
|
#
|
|
|
|
# Filter
|
|
|
|
# A method to require
|
|
|
|
# each()
|
|
|
|
#
|
|
|
|
class Filter
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
def initialize(sh)
|
2011-05-18 20:07:25 -04:00
|
|
|
@shell = sh # parent shell
|
|
|
|
@input = nil # input filter
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :input
|
|
|
|
|
|
|
|
def input=(filter)
|
|
|
|
@input = filter
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2001-05-17 06:02:47 -04:00
|
|
|
def each(rs = nil)
|
|
|
|
rs = @shell.record_separator unless rs
|
|
|
|
if @input
|
2011-05-18 17:19:18 -04:00
|
|
|
@input.each(rs){|l| yield l}
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def < (src)
|
|
|
|
case src
|
|
|
|
when String
|
2011-05-18 17:19:18 -04:00
|
|
|
cat = Cat.new(@shell, src)
|
|
|
|
cat | self
|
2001-05-17 06:02:47 -04:00
|
|
|
when IO
|
2011-05-18 17:19:18 -04:00
|
|
|
self.input = src
|
|
|
|
self
|
2001-05-17 06:02:47 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.Fail Error::CantApplyMethod, "<", to.class
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def > (to)
|
|
|
|
case to
|
|
|
|
when String
|
2011-05-18 17:19:18 -04:00
|
|
|
dst = @shell.open(to, "w")
|
|
|
|
begin
|
|
|
|
each(){|l| dst << l}
|
|
|
|
ensure
|
|
|
|
dst.close
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
when IO
|
2011-05-18 17:19:18 -04:00
|
|
|
each(){|l| to << l}
|
2001-05-17 06:02:47 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.Fail Error::CantApplyMethod, ">", to.class
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def >> (to)
|
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.cd(@shell.pwd).append(to, self)
|
2003-02-07 14:00:21 -05:00
|
|
|
rescue CantApplyMethod
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.Fail Error::CantApplyMethod, ">>", to.class
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def | (filter)
|
|
|
|
filter.input = self
|
|
|
|
if active?
|
2011-05-18 17:19:18 -04:00
|
|
|
@shell.process_controller.start_job filter
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
filter
|
|
|
|
end
|
|
|
|
|
|
|
|
def + (filter)
|
|
|
|
Join.new(@shell, self, filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
|
|
|
ary = []
|
|
|
|
each(){|l| ary.push l}
|
|
|
|
ary
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
str = ""
|
|
|
|
each(){|l| str.concat l}
|
|
|
|
str
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
if @shell.debug.kind_of?(Integer) && @shell.debug > 2
|
2011-05-18 17:19:18 -04:00
|
|
|
super
|
2001-05-17 06:02:47 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
to_s
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|