summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-17 10:35:32 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-17 10:35:32 +0000
commit0deafc16c4ed7f34f37a165eb242a64a6e755b59 (patch)
tree3cf57fb69299f107fe21da4325efe85173604d3e /lib
parentb119069bac8b255d5892ba76d34264d8b230d3d2 (diff)
* doc/shell.rd*, lib/shell*: bring shell.rb 0.6 onto the ruby_1_6
branch. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@1426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/shell.rb273
-rw-r--r--lib/shell/command-processor.rb27
-rw-r--r--lib/shell/process-controller.rb16
3 files changed, 298 insertions, 18 deletions
diff --git a/lib/shell.rb b/lib/shell.rb
new file mode 100644
index 0000000000..5babc4175b
--- /dev/null
+++ b/lib/shell.rb
@@ -0,0 +1,273 @@
+#
+# shell.rb -
+# $Release Version: 0.6.0 $
+# $Revision: 1.8 $
+# $Date: 2001/03/19 09:01:11 $
+# by Keiju ISHITSUKA(Nippon Rational Inc.)
+#
+# --
+#
+#
+#
+
+require "e2mmap"
+require "thread"
+
+require "shell/error"
+require "shell/command-processor"
+require "shell/process-controller"
+
+class Shell
+ @RCS_ID='-$Id: shell.rb,v 1.8 2001/03/19 09:01:11 keiju Exp keiju $-'
+
+ include Error
+ extend Exception2MessageMapper
+
+# @cascade = true
+ # debug: true -> normal debug
+ # debug: 1 -> eval definition debug
+ # debug: 2 -> detail inspect debug
+ @debug = false
+ @verbose = true
+
+ class << Shell
+ attr :cascade, true
+ attr :debug, true
+ attr :verbose, true
+
+# alias cascade? cascade
+ alias debug? debug
+ alias verbose? verbose
+ @verbose = true
+
+ def debug=(val)
+ @debug = val
+ @verbose = val if val
+ end
+
+ def cd(path)
+ sh = new
+ sh.cd path
+ sh
+ end
+
+ def default_system_path
+ if @default_system_path
+ @default_system_path
+ else
+ ENV["PATH"].split(":")
+ end
+ end
+
+ def default_system_path=(path)
+ @default_system_path = path
+ end
+
+ def default_record_separator
+ if @default_record_separator
+ @default_record_separator
+ else
+ $/
+ end
+ end
+
+ def default_record_separator=(rs)
+ @default_record_separator = rs
+ end
+ end
+
+ def initialize
+ @cwd = Dir.pwd
+ @dir_stack = []
+ @umask = nil
+
+ @system_path = Shell.default_system_path
+ @record_separator = Shell.default_record_separator
+
+ @command_processor = CommandProcessor.new(self)
+ @process_controller = ProcessController.new(self)
+
+ @verbose = Shell.verbose
+ @debug = Shell.debug
+ end
+
+ attr_reader :system_path
+
+ def system_path=(path)
+ @system_path = path
+ rehash
+ end
+
+ attr :umask, true
+ attr :record_separator, true
+
+ attr :verbose, true
+ attr :debug, true
+
+ def debug=(val)
+ @debug = val
+ @verbose = val if val
+ end
+
+ alias verbose? verbose
+ alias debug? debug
+
+ attr_reader :command_processor
+ attr_reader :process_controller
+
+ def expand_path(path)
+ if /^\// =~ path
+ File.expand_path(path)
+ else
+ File.expand_path(File.join(@cwd, path))
+ end
+ end
+
+ # ほとんどのShell Command は CommandProcessor により定義される.
+
+ #
+ # Dir関連メソッド
+ #
+ # Shell#cwd/dir/getwd/pwd
+ # Shell#chdir/cd
+ # Shell#pushdir/pushd
+ # Shell#popdir/popd
+ # Shell#mkdir
+ # Shell#rmdir
+
+ attr :cwd
+ alias dir cwd
+ alias getwd cwd
+ alias pwd cwd
+
+ attr :dir_stack
+ alias dirs dir_stack
+
+ # イテレータとして呼ばれると一時的にcdすることになる.
+ def chdir(path = nil)
+ if iterator?
+ cwd_old = @cwd
+ begin
+ chdir(path)
+ yield
+ ensure
+ chdir(cwd_old)
+ end
+ else
+ path = "~" unless path
+ @cwd = expand_path(path)
+ notify "current dir: #{@cwd}"
+ rehash
+ self
+ end
+ end
+ alias cd chdir
+
+ def pushdir(path = nil)
+ if iterator?
+ pushdir(path)
+ begin
+ yield
+ ensure
+ popdir
+ end
+ elsif path
+ @dir_stack.push @cwd
+ chdir path
+ notify "dir stack: [#{@dir_stack.join ', '}]"
+ self
+ else
+ if pop = @dir_stack.pop
+ @dir_stack.push @cwd
+ chdir pop
+ notify "dir stack: [#{@dir_stack.join ', '}]"
+ self
+ else
+ Shell.Fail DirStackEmpty
+ end
+ end
+ end
+ alias pushd pushdir
+
+ def popdir
+ if pop = @dir_stack.pop
+ chdir pop
+ notify "dir stack: [#{@dir_stack.join ', '}]"
+ self
+ else
+ Shell.Fail DirStackEmpty
+ end
+ end
+ alias popd popdir
+
+
+ #
+ # process 管理
+ #
+ def jobs
+ @process_controller.jobs
+ end
+
+ def kill(sig, command)
+ @process_controller.kill_job(sig, command)
+ end
+
+ #
+ # command 定義
+ #
+ def Shell.def_system_command(command, path = command)
+ CommandProcessor.def_system_command(command, path)
+ end
+
+ def Shell.undef_system_command(command)
+ CommandProcessor.undef_system_command(command)
+ end
+
+ def Shell.alias_command(ali, command, *opts, &block)
+ CommandProcessor.alias_command(ali, command, *opts, &block)
+ end
+
+ def Shell.unalias_command(ali)
+ CommandProcessor.unalias_command(ali)
+ end
+
+ def Shell.install_system_commands(pre = "sys_")
+ CommandProcessor.install_system_commands(pre)
+ end
+
+ #
+ def inspect
+ if debug.kind_of?(Integer) && debug > 2
+ super
+ else
+ to_s
+ end
+ end
+
+ def self.notify(*opts, &block)
+ Thread.exclusive do
+ if opts[-1].kind_of?(String)
+ yorn = verbose?
+ else
+ yorn = opts.pop
+ end
+ return unless yorn
+
+ _head = true
+ print *opts.collect{|mes|
+ mes = mes.dup
+ yield mes if iterator?
+ if _head
+ _head = false
+ "shell: " + mes
+ else
+ " " + mes
+ end
+ }.join("\n")+"\n"
+ end
+ end
+
+ CommandProcessor.initialize
+ CommandProcessor.run_config
+end
+
diff --git a/lib/shell/command-processor.rb b/lib/shell/command-processor.rb
index fa253b3705..38e38151fa 100644
--- a/lib/shell/command-processor.rb
+++ b/lib/shell/command-processor.rb
@@ -65,14 +65,14 @@ class Shell
# CommandProcessor#expand_path(path)
# path: String
# return: String
- # returns the absolute path for <path>
+ # pwdからみた絶対パスを返す
#
def expand_path(path)
@shell.expand_path(path)
end
#
- # File related commands
+ # File関連コマンド
# Shell#foreach
# Shell#open
# Shell#unlink
@@ -87,7 +87,7 @@ class Shell
# Same as:
# File#foreach (when path is file)
# Dir#foreach (when path is directory)
- # path is relative to pwd
+ # pathはpwdからの相対パスになる
#
def foreach(path = nil, *rs)
path = "." unless path
@@ -108,7 +108,7 @@ class Shell
# Same as:
# File#open (when path is file)
# Dir#open (when path is directory)
- # mode has an effect only when path is a file
+ # modeはpathがファイルの時だけ有効
#
def open(path, mode)
path = expand_path(path)
@@ -181,7 +181,7 @@ class Shell
alias [] test
#
- # Dir related methods
+ # Dir関連メソッド
#
# Shell#mkdir
# Shell#rmdir
@@ -454,11 +454,18 @@ class Shell
#
# CommandProcessor.install_system_commands(pre)
# pre: String - command name prefix
- # defines every command which belongs in default_system_path via
- # CommandProcessor.command(). It doesn't define already defined
- # methods twice. By default, "pre_" is prefixes to each method
- # name. Characters that may not be used in a method name are
- # all converted to '_'. Definition errors are just ignored.
+ # define CommandProcessor.command() from all command of
+ # default_system_path. If a method exists, and names of it and
+ # the target command are the same, the method is not defined.
+ # Default action prefix "sys_" to the method name. The character
+ # which is not forgiven as a method name (when the first char is
+ # alphabet or char is alpha-numeric) converts into ``_''. A
+ # definition error is ignored.
+ # (Meaning same in Japanese: default_system_path上にのるコマンドを定
+ # 義する. すでに同名のメソッドが存在する時は, 定義を行なわない. デ
+ # フォルトでは, 全てのメソッドには接頭子"sys_"をつける. メソッド名
+ # として許されないキャラクタ(英数時以外とメソッド名の先頭が数値に
+ # なる場合)は, 強制的に``_''に変換する. 定義エラーは無視する.)
#
def self.install_system_commands(pre = "sys_")
defined_meth = {}
diff --git a/lib/shell/process-controller.rb b/lib/shell/process-controller.rb
index 26fb1d9f08..5cbbe0c500 100644
--- a/lib/shell/process-controller.rb
+++ b/lib/shell/process-controller.rb
@@ -102,7 +102,7 @@ class Shell
end
end
- # schedule a command
+ # jobのスケジュールの追加
def add_schedule(command)
@jobs_sync.synchronize(:EX) do
ProcessController.activate(self)
@@ -114,7 +114,7 @@ class Shell
end
end
- # start a job
+ # job を開始する
def start_job(command = nil)
@jobs_sync.synchronize(:EX) do
if command
@@ -127,7 +127,7 @@ class Shell
@active_jobs.push command
command.start
- # start all jobs that input from the job
+ # そのjobをinputとするjobも開始する
for job in @waiting_jobs
start_job(job) if job.input == command
end
@@ -146,7 +146,7 @@ class Shell
end
end
- # terminate a job
+ # jobの終了
def terminate_job(command)
@jobs_sync.synchronize(:EX) do
@active_jobs.delete command
@@ -157,7 +157,7 @@ class Shell
end
end
- # kill a job
+ # jobの強制終了
def kill_job(sig, command)
@jobs_sync.synchronize(:SH) do
if @waiting_jobs.delete command
@@ -177,7 +177,7 @@ class Shell
end
end
- # wait for all jobs to terminate
+ # すべてのjobの実行終了待ち
def wait_all_jobs_execution
@job_monitor.synchronize do
begin
@@ -190,7 +190,7 @@ class Shell
end
end
- # simple fork
+ # 簡単なfork
def sfork(command, &block)
pipe_me_in, pipe_peer_out = IO.pipe
pipe_peer_in, pipe_me_out = IO.pipe
@@ -237,7 +237,7 @@ class Shell
command.notify "warn: job(%id) was done already waitipd."
_pid = true
ensure
- # when the process ends, wait until the command termintes
+ # プロセス終了時にコマンド実行が終わるまで待たせるため.
if _pid
else
command.notify("notice: Process finishing...",