2 # shell/system-command.rb -
3 # $Release Version: 0.7 $
5 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
12 require "shell/filter"
15 class SystemCommand < Filter
16 def initialize(sh, command, *opts)
17 if t = opts.find{|opt| !opt.kind_of?(String) && opt.class}
18 Shell.Fail Error::TypeError, t.class, "String"
24 @input_queue = Queue.new
27 sh.process_controller.add_schedule(self)
34 @shell.process_controller.waiting_job?(self)
38 @shell.process_controller.active_job?(self)
49 notify([@command, *@opts].join(" "))
51 @pid, @pipe_in, @pipe_out = @shell.process_controller.sfork(self) {
54 exec(@command, *@opts)
63 @pipe_out.flush if @pipe_out and !@pipe_out.closed?
79 Process.kill(sig, @pid)
84 notify "Job(%id) start imp-pipe.", @shell.debug?
85 rs = @shell.record_separator unless rs
89 while l = @pipe_in.gets
96 if !ProcessController::USING_AT_EXIT_WHEN_PROCESS_EXIT and _eop
97 notify("warn: Process finishing...",
98 "wait for Job[%id] to finish pipe importing.",
99 "You can use Shell#transact or Shell#check_point for more safe execution.")
102 notify "job(%id}) close imp-pipe.", @shell.debug?
103 @input_queue.push :EOF
110 notify "job(%id) start exp-pipe.", @shell.debug?
115 ProcessController::block_output_synchronize do
120 rescue Errno::EPIPE, Errno::EIO
123 if !ProcessController::USING_AT_EXIT_WHEN_PROCESS_EXIT and _eop
124 notify("shell: warn: Process finishing...",
125 "wait for Job(%id) to finish pipe exporting.",
126 "You can use Shell#transact or Shell#check_point for more safe execution.")
129 notify "job(%id) close exp-pipe.", @shell.debug?
135 alias super_each each
137 while (l = @input_queue.pop) != :EOF
143 # if you wish to output:
144 # "shell: job(#{@command}:#{@pid}) close pipe-out."
146 # mes: "job(%id) close pipe-out."
147 # yorn: Boolean(@shell.debug? or @shell.verbose?)
148 def notify(*opts, &block)
149 @shell.notify(*opts) do |mes|
150 yield mes if iterator?
152 mes.gsub!("%id", "#{@command}:##{@pid}")
153 mes.gsub!("%name", "#{@command}")
154 mes.gsub!("%pid", "#{@pid}")