2001-05-17 06:02:47 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# shell/command-controller.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
|
|
|
#
|
|
|
|
|
|
|
|
require "e2mmap"
|
|
|
|
require "thread"
|
|
|
|
|
|
|
|
require "shell/error"
|
|
|
|
require "shell/filter"
|
|
|
|
require "shell/system-command"
|
|
|
|
require "shell/builtin-command"
|
|
|
|
|
|
|
|
class Shell
|
2012-12-04 21:55:07 -05:00
|
|
|
# In order to execute a command on your OS, you need to define it as a
|
|
|
|
# Shell method.
|
|
|
|
#
|
|
|
|
# Alternatively, you can execute any command via
|
|
|
|
# Shell::CommandProcessor#system even if it is not defined.
|
2001-05-17 06:02:47 -04:00
|
|
|
class CommandProcessor
|
2011-05-18 20:07:25 -04:00
|
|
|
# include Error
|
2001-05-17 06:02:47 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# initialize of Shell and related classes.
|
|
|
|
#
|
2007-03-20 08:38:58 -04:00
|
|
|
m = [:initialize, :expand_path]
|
|
|
|
if Object.methods.first.kind_of?(String)
|
2008-12-18 02:54:50 -05:00
|
|
|
NoDelegateMethods = m.collect{|x| x.id2name}
|
2007-03-20 08:38:58 -04:00
|
|
|
else
|
|
|
|
NoDelegateMethods = m
|
|
|
|
end
|
|
|
|
|
2001-05-17 06:02:47 -04:00
|
|
|
def self.initialize
|
|
|
|
|
|
|
|
install_builtin_commands
|
|
|
|
|
2010-03-19 23:30:59 -04:00
|
|
|
# define CommandProcessor#methods to Shell#methods and Filter#methods
|
2003-05-22 12:37:22 -04:00
|
|
|
for m in CommandProcessor.instance_methods(false) - NoDelegateMethods
|
2011-05-18 17:19:18 -04:00
|
|
|
add_delegate_command_to_shell(m)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2001-05-17 06:02:47 -04:00
|
|
|
def self.method_added(id)
|
2011-05-18 17:19:18 -04:00
|
|
|
add_delegate_command_to_shell(id)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# include run file.
|
|
|
|
#
|
|
|
|
def self.run_config
|
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
load File.expand_path("~/.rb_shell") if ENV.key?("HOME")
|
2001-05-17 06:02:47 -04:00
|
|
|
rescue LoadError, Errno::ENOENT
|
|
|
|
rescue
|
2011-05-18 17:19:18 -04:00
|
|
|
print "load error: #{rc}\n"
|
|
|
|
print $!.class, ": ", $!, "\n"
|
|
|
|
for err in $@[0, $@.size - 2]
|
|
|
|
print "\t", err, "\n"
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(shell)
|
|
|
|
@shell = shell
|
|
|
|
@system_commands = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# CommandProcessor#expand_path(path)
|
2011-05-18 20:07:25 -04:00
|
|
|
# path: String
|
|
|
|
# return: String
|
|
|
|
# returns the absolute path for <path>
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
def expand_path(path)
|
|
|
|
@shell.expand_path(path)
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# foreach(path, record_separator) -> Enumerator
|
|
|
|
# foreach(path, record_separator) { block }
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# See IO.foreach when +path+ is a file.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# See Dir.foreach when +path+ is a directory.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
def foreach(path = nil, *rs)
|
|
|
|
path = "." unless path
|
|
|
|
path = expand_path(path)
|
|
|
|
|
|
|
|
if File.directory?(path)
|
2011-05-18 17:19:18 -04:00
|
|
|
Dir.foreach(path){|fn| yield fn}
|
2001-05-17 06:02:47 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
IO.foreach(path, *rs){|l| yield l}
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# open(path, mode, permissions) -> Enumerator
|
|
|
|
# open(path, mode, permissions) { block }
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# See IO.open when +path+ is a file.
|
|
|
|
#
|
|
|
|
# See Dir.open when +path+ is a directory.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2007-03-20 08:38:58 -04:00
|
|
|
def open(path, mode = nil, perm = 0666, &b)
|
2001-05-17 06:02:47 -04:00
|
|
|
path = expand_path(path)
|
|
|
|
if File.directory?(path)
|
2011-05-18 17:19:18 -04:00
|
|
|
Dir.open(path, &b)
|
2001-05-17 06:02:47 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
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
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# public :open
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# unlink(path)
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# See IO.unlink when +path+ is a file.
|
|
|
|
#
|
|
|
|
# See Dir.unlink when +path+ is a directory.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
def unlink(path)
|
2007-03-20 08:38:58 -04:00
|
|
|
@shell.check_point
|
|
|
|
|
2001-05-17 06:02:47 -04:00
|
|
|
path = expand_path(path)
|
|
|
|
if File.directory?(path)
|
2011-05-18 17:19:18 -04:00
|
|
|
Dir.unlink(path)
|
2001-05-17 06:02:47 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
IO.unlink(path)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
2007-03-20 08:38:58 -04:00
|
|
|
Void.new(@shell)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# See Shell::CommandProcessor#test
|
|
|
|
alias top_level_test test
|
|
|
|
# call-seq:
|
|
|
|
# test(command, file1, file2) -> true or false
|
|
|
|
# [command, file1, file2] -> true or false
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Tests if the given +command+ exists in +file1+, or optionally +file2+.
|
|
|
|
#
|
|
|
|
# Example:
|
2011-05-18 20:07:25 -04:00
|
|
|
# sh[?e, "foo"]
|
|
|
|
# sh[:e, "foo"]
|
|
|
|
# sh["e", "foo"]
|
|
|
|
# sh[:exists?, "foo"]
|
|
|
|
# sh["exists?", "foo"]
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2001-05-17 06:02:47 -04:00
|
|
|
def test(command, file1, file2=nil)
|
|
|
|
file1 = expand_path(file1)
|
|
|
|
file2 = expand_path(file2) if file2
|
|
|
|
command = command.id2name if command.kind_of?(Symbol)
|
|
|
|
|
|
|
|
case command
|
|
|
|
when Integer
|
2011-05-18 17:19:18 -04:00
|
|
|
if file2
|
|
|
|
top_level_test(command, file1, file2)
|
|
|
|
else
|
|
|
|
top_level_test(command, file1)
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
when String
|
2011-05-18 17:19:18 -04:00
|
|
|
if command.size == 1
|
|
|
|
if file2
|
|
|
|
top_level_test(command, file1, file2)
|
|
|
|
else
|
|
|
|
top_level_test(command, file1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if file2
|
|
|
|
FileTest.send(command, file1, file2)
|
|
|
|
else
|
|
|
|
FileTest.send(command, file1)
|
|
|
|
end
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
2012-12-04 21:55:07 -05:00
|
|
|
# See Shell::CommandProcessor#test
|
2001-05-17 06:02:47 -04:00
|
|
|
alias [] test
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# mkdir(path)
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Same as Dir.mkdir, except multiple directories are allowed.
|
2001-05-17 06:02:47 -04:00
|
|
|
def mkdir(*path)
|
2007-03-20 08:38:58 -04:00
|
|
|
@shell.check_point
|
|
|
|
notify("mkdir #{path.join(' ')}")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-20 08:38:58 -04:00
|
|
|
perm = nil
|
|
|
|
if path.last.kind_of?(Integer)
|
2011-05-18 17:19:18 -04:00
|
|
|
perm = path.pop
|
2007-03-20 08:38:58 -04:00
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
for dir in path
|
2011-05-18 17:19:18 -04:00
|
|
|
d = expand_path(dir)
|
|
|
|
if perm
|
|
|
|
Dir.mkdir(d, perm)
|
|
|
|
else
|
|
|
|
Dir.mkdir(d)
|
|
|
|
end
|
|
|
|
File.chmod(d, 0666 & ~@shell.umask) if @shell.umask
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
2007-03-20 08:38:58 -04:00
|
|
|
Void.new(@shell)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# rmdir(path)
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Same as Dir.rmdir, except multiple directories are allowed.
|
2001-05-17 06:02:47 -04:00
|
|
|
def rmdir(*path)
|
2007-03-20 08:38:58 -04:00
|
|
|
@shell.check_point
|
|
|
|
notify("rmdir #{path.join(' ')}")
|
|
|
|
|
2001-05-17 06:02:47 -04:00
|
|
|
for dir in path
|
2011-05-18 17:19:18 -04:00
|
|
|
Dir.rmdir(expand_path(dir))
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
2007-03-20 08:38:58 -04:00
|
|
|
Void.new(@shell)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# system(command, *options) -> SystemCommand
|
|
|
|
#
|
|
|
|
# Executes the given +command+ with the +options+ parameter.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Example:
|
2011-05-18 20:07:25 -04:00
|
|
|
# print sh.system("ls", "-l")
|
|
|
|
# sh.system("ls", "-l") | sh.head > STDOUT
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2001-05-17 06:02:47 -04:00
|
|
|
def system(command, *opts)
|
2004-03-21 07:21:11 -05:00
|
|
|
if opts.empty?
|
2011-05-18 17:19:18 -04:00
|
|
|
if command =~ /\*|\?|\{|\}|\[|\]|<|>|\(|\)|~|&|\||\\|\$|;|'|`|"|\n/
|
|
|
|
return SystemCommand.new(@shell, find_system_command("sh"), "-c", command)
|
|
|
|
else
|
|
|
|
command, *opts = command.split(/\s+/)
|
|
|
|
end
|
2004-03-21 07:21:11 -05:00
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
SystemCommand.new(@shell, find_system_command(command), *opts)
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# rehash
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Clears the command hash table.
|
2001-05-17 06:02:47 -04:00
|
|
|
def rehash
|
|
|
|
@system_commands = {}
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
def check_point # :nodoc:
|
2001-05-17 06:02:47 -04:00
|
|
|
@shell.process_controller.wait_all_jobs_execution
|
|
|
|
end
|
2012-12-04 21:55:07 -05:00
|
|
|
alias finish_all_jobs check_point # :nodoc:
|
2001-05-17 06:02:47 -04:00
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# transact { block }
|
|
|
|
#
|
|
|
|
# Executes a block as self
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# sh.transact { system("ls", "-l") | head > STDOUT }
|
2001-05-17 06:02:47 -04:00
|
|
|
def transact(&block)
|
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
@shell.instance_eval(&block)
|
2001-05-17 06:02:47 -04:00
|
|
|
ensure
|
2011-05-18 17:19:18 -04:00
|
|
|
check_point
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# out(device) { block }
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Calls <code>device.print</code> on the result passing the _block_ to
|
|
|
|
# #transact
|
2001-05-17 06:02:47 -04:00
|
|
|
def out(dev = STDOUT, &block)
|
2001-06-27 11:35:04 -04:00
|
|
|
dev.print transact(&block)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# echo(*strings) -> Echo
|
|
|
|
#
|
|
|
|
# Returns a Echo object, for the given +strings+
|
2001-05-17 06:02:47 -04:00
|
|
|
def echo(*strings)
|
|
|
|
Echo.new(@shell, *strings)
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# cat(*filename) -> Cat
|
|
|
|
#
|
|
|
|
# Returns a Cat object, for the given +filenames+
|
2001-05-17 06:02:47 -04:00
|
|
|
def cat(*filenames)
|
|
|
|
Cat.new(@shell, *filenames)
|
|
|
|
end
|
|
|
|
|
|
|
|
# def sort(*filenames)
|
|
|
|
# Sort.new(self, *filenames)
|
|
|
|
# end
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# glob(pattern) -> Glob
|
|
|
|
#
|
|
|
|
# Returns a Glob filter object, with the given +pattern+ object
|
2001-05-17 06:02:47 -04:00
|
|
|
def glob(pattern)
|
|
|
|
Glob.new(@shell, pattern)
|
|
|
|
end
|
|
|
|
|
|
|
|
def append(to, filter)
|
|
|
|
case to
|
|
|
|
when String
|
2011-05-18 17:19:18 -04:00
|
|
|
AppendFile.new(@shell, to, filter)
|
2001-05-17 06:02:47 -04:00
|
|
|
when IO
|
2011-05-18 17:19:18 -04:00
|
|
|
AppendIO.new(@shell, to, filter)
|
2001-05-17 06:02:47 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.Fail Error::CantApplyMethod, "append", to.class
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# tee(file) -> Tee
|
|
|
|
#
|
|
|
|
# Returns a Tee filter object, with the given +file+ command
|
2001-05-17 06:02:47 -04:00
|
|
|
def tee(file)
|
|
|
|
Tee.new(@shell, file)
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# concat(*jobs) -> Concat
|
|
|
|
#
|
|
|
|
# Returns a Concat object, for the given +jobs+
|
2001-05-17 06:02:47 -04:00
|
|
|
def concat(*jobs)
|
|
|
|
Concat.new(@shell, *jobs)
|
|
|
|
end
|
|
|
|
|
|
|
|
# %pwd, %cwd -> @pwd
|
* ext/pathname/lib/pathname.rb, ext/tk/lib/multi-tk.rb,
ext/tk/sample/demos-en/widget, lib/benchmark.rb, lib/irb/cmd/fork.rb,
lib/mkmf.rb, lib/net/ftp.rb, lib/net/smtp.rb, lib/open3.rb,
lib/pstore.rb, lib/rexml/element.rb, lib/rexml/light/node.rb,
lib/rinda/tuplespace.rb, lib/rss/maker/base.rb,
lib/rss/maker/entry.rb, lib/scanf.rb, lib/set.rb, lib/shell.rb,
lib/shell/command-processor.rb, lib/shell/process-controller.rb,
lib/shell/system-command.rb, lib/uri/common.rb: remove unused block
arguments to avoid creating Proc objects.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33638 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-11-05 03:37:47 -04:00
|
|
|
def notify(*opts)
|
2007-03-20 08:38:58 -04:00
|
|
|
Shell.notify(*opts) {|mes|
|
2011-05-18 17:19:18 -04:00
|
|
|
yield mes if iterator?
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2011-05-18 17:19:18 -04:00
|
|
|
mes.gsub!("%pwd", "#{@cwd}")
|
|
|
|
mes.gsub!("%cwd", "#{@cwd}")
|
2007-03-20 08:38:58 -04:00
|
|
|
}
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# private functions
|
|
|
|
#
|
|
|
|
def find_system_command(command)
|
|
|
|
return command if /^\// =~ command
|
|
|
|
case path = @system_commands[command]
|
|
|
|
when String
|
2011-05-18 17:19:18 -04:00
|
|
|
if exists?(path)
|
|
|
|
return path
|
|
|
|
else
|
|
|
|
Shell.Fail Error::CommandNotFound, command
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
when false
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.Fail Error::CommandNotFound, command
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
for p in @shell.system_path
|
2011-05-18 17:19:18 -04:00
|
|
|
path = join(p, command)
|
|
|
|
if FileTest.exist?(path)
|
|
|
|
@system_commands[command] = path
|
|
|
|
return path
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
@system_commands[command] = false
|
2004-03-21 07:21:11 -05:00
|
|
|
Shell.Fail Error::CommandNotFound, command
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# def_system_command(command, path) -> Shell::SystemCommand
|
|
|
|
#
|
|
|
|
# Defines a command, registering +path+ as a Shell method for the given
|
|
|
|
# +command+.
|
|
|
|
#
|
|
|
|
# Shell::CommandProcessor.def_system_command "ls"
|
|
|
|
# #=> Defines ls.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Shell::CommandProcessor.def_system_command "sys_sort", "sort"
|
|
|
|
# #=> Defines sys_sort as sort
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
def self.def_system_command(command, path = command)
|
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
eval((d = %Q[def #{command}(*opts)
|
2011-05-18 20:07:25 -04:00
|
|
|
SystemCommand.new(@shell, '#{path}', *opts)
|
2001-05-17 06:02:47 -04:00
|
|
|
end]), nil, __FILE__, __LINE__ - 1)
|
|
|
|
rescue SyntaxError
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.notify "warn: Can't define #{command} path: #{path}."
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
Shell.notify "Define #{command} path: #{path}.", Shell.debug?
|
2009-03-05 22:56:38 -05:00
|
|
|
Shell.notify("Definition of #{command}: ", d,
|
2011-05-18 20:07:25 -04:00
|
|
|
Shell.debug.kind_of?(Integer) && Shell.debug > 1)
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# undef_system_command(command) -> self
|
|
|
|
#
|
|
|
|
# Undefines a command
|
2001-05-17 06:02:47 -04:00
|
|
|
def self.undef_system_command(command)
|
|
|
|
command = command.id2name if command.kind_of?(Symbol)
|
|
|
|
remove_method(command)
|
|
|
|
Shell.module_eval{remove_method(command)}
|
|
|
|
Filter.module_eval{remove_method(command)}
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
@alias_map = {}
|
2012-12-04 21:55:07 -05:00
|
|
|
# Returns a list of aliased commands
|
2001-05-17 06:02:47 -04:00
|
|
|
def self.alias_map
|
|
|
|
@alias_map
|
|
|
|
end
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# alias_command(alias, command, *options) -> self
|
|
|
|
#
|
|
|
|
# Creates a command alias at the given +alias+ for the given +command+,
|
|
|
|
# passing any +options+ along with it.
|
|
|
|
#
|
|
|
|
# Shell::CommandProcessor.alias_command "lsC", "ls", "-CBF", "--show-control-chars"
|
|
|
|
# Shell::CommandProcessor.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]}
|
|
|
|
#
|
* ext/pathname/lib/pathname.rb, ext/tk/lib/multi-tk.rb,
ext/tk/sample/demos-en/widget, lib/benchmark.rb, lib/irb/cmd/fork.rb,
lib/mkmf.rb, lib/net/ftp.rb, lib/net/smtp.rb, lib/open3.rb,
lib/pstore.rb, lib/rexml/element.rb, lib/rexml/light/node.rb,
lib/rinda/tuplespace.rb, lib/rss/maker/base.rb,
lib/rss/maker/entry.rb, lib/scanf.rb, lib/set.rb, lib/shell.rb,
lib/shell/command-processor.rb, lib/shell/process-controller.rb,
lib/shell/system-command.rb, lib/uri/common.rb: remove unused block
arguments to avoid creating Proc objects.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33638 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-11-05 03:37:47 -04:00
|
|
|
def self.alias_command(ali, command, *opts)
|
2001-05-17 06:02:47 -04:00
|
|
|
ali = ali.id2name if ali.kind_of?(Symbol)
|
|
|
|
command = command.id2name if command.kind_of?(Symbol)
|
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
if iterator?
|
|
|
|
@alias_map[ali.intern] = proc
|
2001-05-17 06:02:47 -04:00
|
|
|
|
2011-05-18 17:19:18 -04:00
|
|
|
eval((d = %Q[def #{ali}(*opts)
|
2001-05-17 06:02:47 -04:00
|
|
|
@shell.__send__(:#{command},
|
|
|
|
*(CommandProcessor.alias_map[:#{ali}].call *opts))
|
2011-05-18 20:07:25 -04:00
|
|
|
end]), nil, __FILE__, __LINE__ - 1)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2011-05-18 17:19:18 -04:00
|
|
|
else
|
2011-05-18 20:07:25 -04:00
|
|
|
args = opts.collect{|opt| '"' + opt + '"'}.join(",")
|
|
|
|
eval((d = %Q[def #{ali}(*opts)
|
2001-05-17 06:02:47 -04:00
|
|
|
@shell.__send__(:#{command}, #{args}, *opts)
|
|
|
|
end]), nil, __FILE__, __LINE__ - 1)
|
2011-05-18 17:19:18 -04:00
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
rescue SyntaxError
|
2011-05-18 17:19:18 -04:00
|
|
|
Shell.notify "warn: Can't alias #{ali} command: #{command}."
|
|
|
|
Shell.notify("Definition of #{ali}: ", d)
|
|
|
|
raise
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
Shell.notify "Define #{ali} command: #{command}.", Shell.debug?
|
2009-03-05 22:56:38 -05:00
|
|
|
Shell.notify("Definition of #{ali}: ", d,
|
2011-05-18 20:07:25 -04:00
|
|
|
Shell.debug.kind_of?(Integer) && Shell.debug > 1)
|
2001-05-17 06:02:47 -04:00
|
|
|
self
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# unalias_command(alias) -> self
|
|
|
|
#
|
|
|
|
# Unaliases the given +alias+ command.
|
2001-05-17 06:02:47 -04:00
|
|
|
def self.unalias_command(ali)
|
|
|
|
ali = ali.id2name if ali.kind_of?(Symbol)
|
|
|
|
@alias_map.delete ali.intern
|
|
|
|
undef_system_command(ali)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# :nodoc:
|
|
|
|
#
|
|
|
|
# Delegates File and FileTest methods into Shell, including the following
|
|
|
|
# commands:
|
|
|
|
#
|
|
|
|
# * Shell#blockdev?(file)
|
|
|
|
# * Shell#chardev?(file)
|
|
|
|
# * Shell#directory?(file)
|
|
|
|
# * Shell#executable?(file)
|
|
|
|
# * Shell#executable_real?(file)
|
|
|
|
# * Shell#exist?(file)/Shell#exists?(file)
|
|
|
|
# * Shell#file?(file)
|
|
|
|
# * Shell#grpowned?(file)
|
|
|
|
# * Shell#owned?(file)
|
|
|
|
# * Shell#pipe?(file)
|
|
|
|
# * Shell#readable?(file)
|
|
|
|
# * Shell#readable_real?(file)
|
|
|
|
# * Shell#setgid?(file)
|
|
|
|
# * Shell#setuid?(file)
|
|
|
|
# * Shell#size(file)/Shell#size?(file)
|
|
|
|
# * Shell#socket?(file)
|
|
|
|
# * Shell#sticky?(file)
|
|
|
|
# * Shell#symlink?(file)
|
|
|
|
# * Shell#writable?(file)
|
|
|
|
# * Shell#writable_real?(file)
|
|
|
|
# * Shell#zero?(file)
|
|
|
|
# * Shell#syscopy(filename_from, filename_to)
|
|
|
|
# * Shell#copy(filename_from, filename_to)
|
|
|
|
# * Shell#move(filename_from, filename_to)
|
|
|
|
# * Shell#compare(filename_from, filename_to)
|
|
|
|
# * Shell#safe_unlink(*filenames)
|
|
|
|
# * Shell#makedirs(*filenames)
|
|
|
|
# * Shell#install(filename_from, filename_to, mode)
|
|
|
|
#
|
|
|
|
# And also, there are some aliases for convenience:
|
|
|
|
#
|
|
|
|
# * Shell#cmp <- Shell#compare
|
|
|
|
# * Shell#mv <- Shell#move
|
|
|
|
# * Shell#cp <- Shell#copy
|
|
|
|
# * Shell#rm_f <- Shell#safe_unlink
|
|
|
|
# * Shell#mkpath <- Shell#makedirs
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
def self.def_builtin_commands(delegation_class, command_specs)
|
|
|
|
for meth, args in command_specs
|
2011-05-18 17:19:18 -04:00
|
|
|
arg_str = args.collect{|arg| arg.downcase}.join(", ")
|
|
|
|
call_arg_str = args.collect{
|
|
|
|
|arg|
|
|
|
|
case arg
|
|
|
|
when /^(FILENAME.*)$/
|
|
|
|
format("expand_path(%s)", $1.downcase)
|
|
|
|
when /^(\*FILENAME.*)$/
|
|
|
|
# \*FILENAME* -> filenames.collect{|fn| expand_path(fn)}.join(", ")
|
|
|
|
$1.downcase + '.collect{|fn| expand_path(fn)}'
|
|
|
|
else
|
|
|
|
arg
|
|
|
|
end
|
|
|
|
}.join(", ")
|
|
|
|
d = %Q[def #{meth}(#{arg_str})
|
2011-05-18 20:07:25 -04:00
|
|
|
#{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)
|
|
|
|
eval d
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# call-seq:
|
|
|
|
# install_system_commands(prefix = "sys_")
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Defines all commands in the Shell.default_system_path as Shell method,
|
|
|
|
# all with given +prefix+ appended to their names.
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
2012-12-04 21:55:07 -05:00
|
|
|
# Any invalid character names are converted to +_+, and errors are passed
|
|
|
|
# to Shell.notify.
|
|
|
|
#
|
|
|
|
# Methods already defined are skipped.
|
2001-05-17 06:02:47 -04:00
|
|
|
def self.install_system_commands(pre = "sys_")
|
|
|
|
defined_meth = {}
|
|
|
|
for m in Shell.methods
|
2011-05-18 17:19:18 -04:00
|
|
|
defined_meth[m] = true
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
sh = Shell.new
|
|
|
|
for path in Shell.default_system_path
|
2011-05-18 17:19:18 -04:00
|
|
|
next unless sh.directory? path
|
|
|
|
sh.cd path
|
|
|
|
sh.foreach do
|
|
|
|
|cn|
|
|
|
|
if !defined_meth[pre + cn] && sh.file?(cn) && sh.executable?(cn)
|
|
|
|
command = (pre + cn).gsub(/\W/, "_").sub(/^([0-9])/, '_\1')
|
|
|
|
begin
|
|
|
|
def_system_command(command, sh.expand_path(cn))
|
|
|
|
rescue
|
|
|
|
Shell.notify "warn: Can't define #{command} path: #{cn}"
|
|
|
|
end
|
|
|
|
defined_meth[command] = command
|
|
|
|
end
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
def self.add_delegate_command_to_shell(id) # :nodoc:
|
2001-05-17 06:02:47 -04:00
|
|
|
id = id.intern if id.kind_of?(String)
|
|
|
|
name = id.id2name
|
|
|
|
if Shell.method_defined?(id)
|
2011-05-18 17:19:18 -04:00
|
|
|
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}"
|
2001-05-17 06:02:47 -04:00
|
|
|
end
|
|
|
|
Shell.notify "method added: Shell##{name}.", Shell.debug?
|
|
|
|
Shell.module_eval(%Q[def #{name}(*args, &block)
|
2011-05-18 20:07:25 -04:00
|
|
|
begin
|
|
|
|
@command_processor.__send__(:#{name}, *args, &block)
|
|
|
|
rescue Exception
|
|
|
|
$@.delete_if{|s| /:in `__getobj__'$/ =~ s} #`
|
|
|
|
$@.delete_if{|s| /^\\(eval\\):/ =~ s}
|
|
|
|
raise
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
end], __FILE__, __LINE__)
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
if Shell::Filter.method_defined?(id)
|
|
|
|
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
|
|
|
|
Shell.notify "method added: Shell::Filter##{name}.", Shell.debug?
|
|
|
|
Filter.module_eval(%Q[def #{name}(*args, &block)
|
|
|
|
begin
|
|
|
|
self | @shell.__send__(:#{name}, *args, &block)
|
|
|
|
rescue Exception
|
|
|
|
$@.delete_if{|s| /:in `__getobj__'$/ =~ s} #`
|
|
|
|
$@.delete_if{|s| /^\\(eval\\):/ =~ s}
|
|
|
|
raise
|
|
|
|
end
|
2001-05-17 06:02:47 -04:00
|
|
|
end], __FILE__, __LINE__)
|
|
|
|
end
|
|
|
|
|
2012-12-04 21:55:07 -05:00
|
|
|
# Delegates File methods into Shell, including the following commands:
|
|
|
|
#
|
|
|
|
# * Shell#atime(file)
|
|
|
|
# * Shell#basename(file, *opt)
|
|
|
|
# * Shell#chmod(mode, *files)
|
|
|
|
# * Shell#chown(owner, group, *file)
|
|
|
|
# * Shell#ctime(file)
|
|
|
|
# * Shell#delete(*file)
|
|
|
|
# * Shell#dirname(file)
|
|
|
|
# * Shell#ftype(file)
|
|
|
|
# * Shell#join(*file)
|
|
|
|
# * Shell#link(file_from, file_to)
|
|
|
|
# * Shell#lstat(file)
|
|
|
|
# * Shell#mtime(file)
|
|
|
|
# * Shell#readlink(file)
|
|
|
|
# * Shell#rename(file_from, file_to)
|
|
|
|
# * Shell#split(file)
|
|
|
|
# * Shell#stat(file)
|
|
|
|
# * Shell#symlink(file_from, file_to)
|
|
|
|
# * Shell#truncate(file, length)
|
|
|
|
# * Shell#utime(atime, mtime, *file)
|
2001-05-17 06:02:47 -04:00
|
|
|
#
|
|
|
|
def self.install_builtin_commands
|
|
|
|
# method related File.
|
2011-05-18 20:07:25 -04:00
|
|
|
# (exclude open/foreach/unlink)
|
2001-05-17 06:02:47 -04:00
|
|
|
normal_delegation_file_methods = [
|
2011-05-18 17:19:18 -04:00
|
|
|
["atime", ["FILENAME"]],
|
|
|
|
["basename", ["fn", "*opts"]],
|
|
|
|
["chmod", ["mode", "*FILENAMES"]],
|
|
|
|
["chown", ["owner", "group", "*FILENAME"]],
|
|
|
|
["ctime", ["FILENAMES"]],
|
|
|
|
["delete", ["*FILENAMES"]],
|
|
|
|
["dirname", ["FILENAME"]],
|
|
|
|
["ftype", ["FILENAME"]],
|
|
|
|
["join", ["*items"]],
|
|
|
|
["link", ["FILENAME_O", "FILENAME_N"]],
|
|
|
|
["lstat", ["FILENAME"]],
|
|
|
|
["mtime", ["FILENAME"]],
|
|
|
|
["readlink", ["FILENAME"]],
|
|
|
|
["rename", ["FILENAME_FROM", "FILENAME_TO"]],
|
|
|
|
# ["size", ["FILENAME"]],
|
|
|
|
["split", ["pathname"]],
|
|
|
|
["stat", ["FILENAME"]],
|
|
|
|
["symlink", ["FILENAME_O", "FILENAME_N"]],
|
|
|
|
["truncate", ["FILENAME", "length"]],
|
|
|
|
["utime", ["atime", "mtime", "*FILENAMES"]]]
|
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
def_builtin_commands(File, normal_delegation_file_methods)
|
|
|
|
alias_method :rm, :delete
|
2011-05-18 17:19:18 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
# method related FileTest
|
|
|
|
def_builtin_commands(FileTest,
|
|
|
|
FileTest.singleton_methods(false).collect{|m| [m, ["FILENAME"]]})
|
2001-05-17 06:02:47 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|