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
|
|
|
#
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
class Shell #:nodoc:
|
|
|
|
# Any result of command exection is a Filter.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# This class includes Enumerable, therefore a Filter object can use all
|
|
|
|
# Enumerable
|
|
|
|
# facilities.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
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
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# each(record_separator=nil) { block }
|
|
|
|
#
|
|
|
|
# Iterates a block for each line.
|
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
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# < source
|
|
|
|
#
|
|
|
|
# Inputs from +source+, which is either a string of a file name or an IO
|
|
|
|
# object.
|
2001-05-17 06:02:47 -04:00
|
|
|
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
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# > source
|
|
|
|
#
|
|
|
|
# Outputs from +source+, which is either a string of a file name or an IO
|
|
|
|
# object.
|
2001-05-17 06:02:47 -04:00
|
|
|
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
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# >> source
|
|
|
|
#
|
|
|
|
# Appends the output to +source+, which is either a string of a file name
|
|
|
|
# or an IO object.
|
2001-05-17 06:02:47 -04:00
|
|
|
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
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# | filter
|
|
|
|
#
|
|
|
|
# Processes a pipeline.
|
2001-05-17 06:02:47 -04:00
|
|
|
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
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# filter1 + filter2
|
|
|
|
#
|
|
|
|
# Outputs +filter1+, and then +filter2+ using Join.new
|
2001-05-17 06:02:47 -04:00
|
|
|
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
|