diff options
Diffstat (limited to 'lib/shell/command-processor.rb')
| -rw-r--r-- | lib/shell/command-processor.rb | 155 |
1 files changed, 78 insertions, 77 deletions
diff --git a/lib/shell/command-processor.rb b/lib/shell/command-processor.rb index ecf6c7d5eb..f3f2bf05b6 100644 --- a/lib/shell/command-processor.rb +++ b/lib/shell/command-processor.rb @@ -1,17 +1,15 @@ # -# shell/command-controller.rb - -# $Release Version: 0.6.0 $ +# shell/command-controller.rb - +# $Release Version: 0.7 $ # $Revision$ -# $Date$ -# by Keiju ISHITSUKA(Nippon Rational Inc.) +# by Keiju ISHITSUKA(keiju@ruby-lang.org) # # -- # -# +# # require "e2mmap" -require "ftools" require "thread" require "shell/error" @@ -26,16 +24,22 @@ class Shell # # initialize of Shell and related classes. # - NoDelegateMethods = ["initialize", "expand_path"] + m = [:initialize, :expand_path] + if Object.methods.first.kind_of?(String) + NoDelegateMethods = m.collect{|x| x.id2name} + else + NoDelegateMethods = m + end + def self.initialize install_builtin_commands - # define CommandProccessor#methods to Shell#methods and Filter#methods + # define CommandProcessor#methods to Shell#methods and Filter#methods for m in CommandProcessor.instance_methods(false) - NoDelegateMethods add_delegate_command_to_shell(m) end - + def self.method_added(id) add_delegate_command_to_shell(id) end @@ -80,7 +84,7 @@ class Shell # Shell#test # # - - # + # # CommandProcessor#foreach(path, rs) # path: String # rs: String - record separator @@ -111,13 +115,20 @@ class Shell # Dir#open (when path is directory) # mode has an effect only when path is a file # - def open(path, mode) + def open(path, mode = nil, perm = 0666, &b) path = expand_path(path) if File.directory?(path) - Dir.open(path) + Dir.open(path, &b) else - effect_umask do - File.open(path, mode) + if @shell.umask + f = File.open(path, mode, perm) + File.chmod(perm & ~@shell.umask, path) + if block_given? + f.each(&b) + end + f + else + f = File.open(path, mode, perm, &b) end end end @@ -130,12 +141,15 @@ class Shell # File#unlink (when path is file) # def unlink(path) + @shell.check_point + path = expand_path(path) if File.directory?(path) Dir.unlink(path) else IO.unlink(path) end + Void.new(@shell) end # @@ -154,7 +168,8 @@ class Shell # sh["e", "foo"] # sh[:exists?, "foo"] # sh["exists?", "foo"] - # + # + alias top_level_test test def test(command, file1, file2=nil) file1 = expand_path(file1) file2 = expand_path(file2) if file2 @@ -162,7 +177,11 @@ class Shell case command when Integer - top_level_test(command, file1, file2) + if file2 + top_level_test(command, file1, file2) + else + top_level_test(command, file1) + end when String if command.size == 1 if file2 @@ -192,22 +211,40 @@ class Shell # CommandProcessor#mkdir(*path) # path: String # same as Dir.mkdir() - # + # def mkdir(*path) + @shell.check_point + notify("mkdir #{path.join(' ')}") + + perm = nil + if path.last.kind_of?(Integer) + perm = path.pop + end for dir in path - Dir.mkdir(expand_path(dir)) + d = expand_path(dir) + if perm + Dir.mkdir(d, perm) + else + Dir.mkdir(d) + end + File.chmod(d, 0666 & ~@shell.umask) if @shell.umask end + Void.new(@shell) end # # CommandProcessor#rmdir(*path) # path: String # same as Dir.rmdir() - # + # def rmdir(*path) + @shell.check_point + notify("rmdir #{path.join(' ')}") + for dir in path Dir.rmdir(expand_path(dir)) end + Void.new(@shell) end # @@ -219,7 +256,7 @@ class Shell # example: # print sh.system("ls", "-l") # sh.system("ls", "-l") | sh.head > STDOUT - # + # def system(command, *opts) if opts.empty? if command =~ /\*|\?|\{|\}|\[|\]|<|>|\(|\)|~|&|\||\\|\$|;|'|`|"|\n/ @@ -299,35 +336,17 @@ class Shell # %pwd, %cwd -> @pwd def notify(*opts, &block) - Thread.exclusive do - Shell.notify(*opts) {|mes| - yield mes if iterator? - - mes.gsub!("%pwd", "#{@cwd}") - mes.gsub!("%cwd", "#{@cwd}") - } - end + Shell.notify(*opts) {|mes| + yield mes if iterator? + + mes.gsub!("%pwd", "#{@cwd}") + mes.gsub!("%cwd", "#{@cwd}") + } end # # private functions # - def effect_umask - if @shell.umask - Thread.critical = true - save = File.umask - begin - yield - ensure - File.umask save - Thread.critical = false - end - else - yield - end - end - private :effect_umask - def find_system_command(command) return command if /^\// =~ command case path = @system_commands[command] @@ -343,7 +362,7 @@ class Shell for p in @shell.system_path path = join(p, command) - if FileTest.exists?(path) + if FileTest.exist?(path) @system_commands[command] = path return path end @@ -364,10 +383,10 @@ class Shell SystemCommand.new(@shell, '#{path}', *opts) end]), nil, __FILE__, __LINE__ - 1) rescue SyntaxError - Shell.notify "warn: Can't define #{command} path: #{path}." + Shell.notify "warn: Can't define #{command} path: #{path}." end Shell.notify "Define #{command} path: #{path}.", Shell.debug? - Shell.notify("Definition of #{command}: ", d, + Shell.notify("Definition of #{command}: ", d, Shell.debug.kind_of?(Integer) && Shell.debug > 1) end @@ -399,7 +418,7 @@ class Shell @shell.__send__(:#{command}, *(CommandProcessor.alias_map[:#{ali}].call *opts)) end]), nil, __FILE__, __LINE__ - 1) - + else args = opts.collect{|opt| '"' + opt + '"'}.join(",") eval((d = %Q[def #{ali}(*opts) @@ -407,22 +426,22 @@ class Shell end]), nil, __FILE__, __LINE__ - 1) end rescue SyntaxError - Shell.notify "warn: Can't alias #{ali} command: #{command}." + Shell.notify "warn: Can't alias #{ali} command: #{command}." Shell.notify("Definition of #{ali}: ", d) raise end Shell.notify "Define #{ali} command: #{command}.", Shell.debug? - Shell.notify("Definition of #{ali}: ", d, + Shell.notify("Definition of #{ali}: ", d, Shell.debug.kind_of?(Integer) && Shell.debug > 1) self end - + def self.unalias_command(ali) ali = ali.id2name if ali.kind_of?(Symbol) @alias_map.delete ali.intern undef_system_command(ali) end - + # # CommandProcessor.def_builtin_commands(delegation_class, command_specs) # delegation_class: Class or Module @@ -453,8 +472,8 @@ class Shell #{delegation_class}.#{meth}(#{call_arg_str}) end] Shell.notify "Define #{meth}(#{arg_str})", Shell.debug? - Shell.notify("Definition of #{meth}: ", d, - Shell.debug.kind_of?(Integer) && Shell.debug > 1) + Shell.notify("Definition of #{meth}: ", d, + Shell.debug.kind_of?(Integer) && Shell.debug > 1) eval d end end @@ -494,14 +513,14 @@ class Shell #---------------------------------------------------------------------- # - # class initializing methods - + # class initializing methods - # #---------------------------------------------------------------------- def self.add_delegate_command_to_shell(id) id = id.intern if id.kind_of?(String) name = id.id2name if Shell.method_defined?(id) - Shell.notify "warn: override definnition of Shell##{name}." + Shell.notify "warn: override definition of Shell##{name}." Shell.notify "warn: alias Shell##{name} to Shell##{name}_org.\n" Shell.module_eval "alias #{name}_org #{name}" end @@ -517,7 +536,7 @@ class Shell end], __FILE__, __LINE__) if Shell::Filter.method_defined?(id) - Shell.notify "warn: override definnition of Shell::Filter##{name}." + Shell.notify "warn: override definition of Shell::Filter##{name}." Shell.notify "warn: alias Shell##{name} to Shell::Filter##{name}_org." Filter.module_eval "alias #{name}_org #{name}" end @@ -542,7 +561,7 @@ class Shell normal_delegation_file_methods = [ ["atime", ["FILENAME"]], ["basename", ["fn", "*opts"]], - ["chmod", ["mode", "*FILENAMES"]], + ["chmod", ["mode", "*FILENAMES"]], ["chown", ["owner", "group", "*FILENAME"]], ["ctime", ["FILENAMES"]], ["delete", ["*FILENAMES"]], @@ -565,27 +584,9 @@ class Shell alias_method :rm, :delete # method related FileTest - def_builtin_commands(FileTest, + def_builtin_commands(FileTest, FileTest.singleton_methods(false).collect{|m| [m, ["FILENAME"]]}) - # method related ftools - normal_delegation_ftools_methods = [ - ["syscopy", ["FILENAME_FROM", "FILENAME_TO"]], - ["copy", ["FILENAME_FROM", "FILENAME_TO"]], - ["move", ["FILENAME_FROM", "FILENAME_TO"]], - ["compare", ["FILENAME_FROM", "FILENAME_TO"]], - ["safe_unlink", ["*FILENAMES"]], - ["makedirs", ["*FILENAMES"]], - # ["chmod", ["mode", "*FILENAMES"]], - ["install", ["FILENAME_FROM", "FILENAME_TO", "mode"]], - ] - def_builtin_commands(File, - normal_delegation_ftools_methods) - alias_method :cmp, :compare - alias_method :mv, :move - alias_method :cp, :copy - alias_method :rm_f, :safe_unlink - alias_method :mkpath, :makedirs end end |
