From: shugo@... (Shugo Maeda) Date: 1998-02-16T17:45:46+09:00 Subject: [ruby-list:6442] Re: rgrep (Re: call for scripts) --Multipart_Mon_Feb_16_17:43:14_1998-1 Content-Type: text/plain; charset=ISO-2022-JP 前田です。 In message "[ruby-list:6431] Re: rgrep (Re: call for scripts)" Shugo Maeda wrote: |grepを呼ばないように修正してみます。 修正しました。 typeを使っているので1.1でないと動きませんが、1.0でも動かないと まずいでしょうか。 |# オプションなどは大幅に減ってしまいそうですが(^^; 結局hinqsの5つだけになりました。 --Multipart_Mon_Feb_16_17:43:14_1998-1 Content-Type: application/octet-stream Content-Disposition: attachment; filename="rgrep.rb" Content-Transfer-Encoding: 7bit #!/usr/local/bin/ruby require "getopts" $pattern = nil $filename = nil $exit_status = 1 ERROR_MSG_TABLE = { Errno::ENOENT => "No such file or directory", Errno::EACCES => "Permission denied" } def handle_file begin yield rescue Errno::ENOENT, Errno::EACCES unless $OPT_s errmsg = ERROR_MSG_TABLE[$!.type] $!.scan(/#{errmsg} - (.*)$/) do |filename| $stderr.puts("rgrep: #{filename}: #{errmsg}") end end end end def scan_files(path) if FileTest.directory?(path) if path[-1] != ?/ path = path + "/" end handle_file do Dir.foreach(path) do |file| if file != "." and file != ".." # pass_block(:scan_files, path + file) scan_files(path + file) do |file| yield(file) end end end end else yield(path) end end def print_usage $stderr.puts("usage: rgrep [-[hinqs]] []") end def grep(file) while file.gets if $_ =~ $pattern $exit_status = 0 unless $OPT_q print($filename, ":") unless $OPT_h print($., ":") if $OPT_n print end end end end trap("INT") do exit(1) end unless getopts("hinqs") $stderr.puts("rgrep: illegal option") print_usage exit(2) end if ARGV.empty? print_usage exit(2) else $pattern = Regexp.new(ARGV.shift, $OPT_i) end unless $OPT_h $OPT_h = ARGV.empty? || (ARGV.size == 1 && !FileTest.directory?(ARGV[0])) end for path in ARGV scan_files(path) do |file| $filename = file handle_file do f = open(file) begin grep(f) ensure f.close end end end end grep($stdin) if ARGV.empty? exit($exit_status) --Multipart_Mon_Feb_16_17:43:14_1998-1 Content-Type: text/plain; charset=ISO-2022-JP -- 前田 修吾 --Multipart_Mon_Feb_16_17:43:14_1998-1--