1# 2# shell/builtin-command.rb - 3# $Release Version: 0.7 $ 4# $Revision: 31641 $ 5# by Keiju ISHITSUKA(keiju@ruby-lang.org) 6# 7# -- 8# 9# 10# 11 12require "shell/filter" 13 14class Shell 15 class BuiltInCommand<Filter 16 def wait? 17 false 18 end 19 def active? 20 true 21 end 22 end 23 24 class Void < BuiltInCommand 25 def initialize(sh, *opts) 26 super sh 27 end 28 29 def each(rs = nil) 30 # do nothing 31 end 32 end 33 34 class Echo < BuiltInCommand 35 def initialize(sh, *strings) 36 super sh 37 @strings = strings 38 end 39 40 def each(rs = nil) 41 rs = @shell.record_separator unless rs 42 for str in @strings 43 yield str + rs 44 end 45 end 46 end 47 48 class Cat < BuiltInCommand 49 def initialize(sh, *filenames) 50 super sh 51 @cat_files = filenames 52 end 53 54 def each(rs = nil) 55 if @cat_files.empty? 56 super 57 else 58 for src in @cat_files 59 @shell.foreach(src, rs){|l| yield l} 60 end 61 end 62 end 63 end 64 65 class Glob < BuiltInCommand 66 def initialize(sh, pattern) 67 super sh 68 69 @pattern = pattern 70 end 71 72 def each(rs = nil) 73 if @pattern[0] == ?/ 74 @files = Dir[@pattern] 75 else 76 prefix = @shell.pwd+"/" 77 @files = Dir[prefix+@pattern].collect{|p| p.sub(prefix, "")} 78 end 79 rs = @shell.record_separator unless rs 80 for f in @files 81 yield f+rs 82 end 83 end 84 end 85 86# class Sort < Cat 87# def initialize(sh, *filenames) 88# super 89# end 90# 91# def each(rs = nil) 92# ary = [] 93# super{|l| ary.push l} 94# for l in ary.sort! 95# yield l 96# end 97# end 98# end 99 100 class AppendIO < BuiltInCommand 101 def initialize(sh, io, filter) 102 super sh 103 @input = filter 104 @io = io 105 end 106 107 def input=(filter) 108 @input.input=filter 109 for l in @input 110 @io << l 111 end 112 end 113 114 end 115 116 class AppendFile < AppendIO 117 def initialize(sh, to_filename, filter) 118 @file_name = to_filename 119 io = sh.open(to_filename, "a") 120 super(sh, io, filter) 121 end 122 123 def input=(filter) 124 begin 125 super 126 ensure 127 @io.close 128 end 129 end 130 end 131 132 class Tee < BuiltInCommand 133 def initialize(sh, filename) 134 super sh 135 @to_filename = filename 136 end 137 138 def each(rs = nil) 139 to = @shell.open(@to_filename, "w") 140 begin 141 super{|l| to << l; yield l} 142 ensure 143 to.close 144 end 145 end 146 end 147 148 class Concat < BuiltInCommand 149 def initialize(sh, *jobs) 150 super(sh) 151 @jobs = jobs 152 end 153 154 def each(rs = nil) 155 while job = @jobs.shift 156 job.each{|l| yield l} 157 end 158 end 159 end 160end 161