summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
commit210367ec889f5910e270d6ea2c7ddb8a8d939e61 (patch)
treefeb35473da45947378fbc02defe39bcd79ef600e /lib
parent9c5b1986a36c7a700b4c76817e35aa874ba7907c (diff)
This commit was generated by cvs2svn to compensate for changes in r372,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/base64.rb45
-rw-r--r--lib/cgi-lib.rb39
-rw-r--r--lib/complex.rb14
-rw-r--r--lib/date.rb91
-rw-r--r--lib/debug.rb12
-rw-r--r--lib/delegate.rb100
-rw-r--r--lib/e2mmap.rb77
-rw-r--r--lib/eregex.rb11
-rw-r--r--lib/finalize.rb66
-rw-r--r--lib/find.rb4
-rw-r--r--lib/ftools.rb31
-rw-r--r--lib/ftplib.rb1123
-rw-r--r--lib/getopts.rb3
-rw-r--r--lib/importenv.rb15
-rw-r--r--lib/jcode.rb23
-rw-r--r--lib/mailread.rb5
-rw-r--r--lib/mathn.rb5
-rw-r--r--lib/matrix.rb239
-rw-r--r--lib/mkmf.rb144
-rw-r--r--lib/mutex_m.rb60
-rw-r--r--lib/observer.rb8
-rw-r--r--lib/parsearg.rb5
-rw-r--r--lib/parsedate.rb81
-rw-r--r--lib/ping.rb33
-rw-r--r--lib/pstore.rb39
-rw-r--r--lib/rational.rb9
-rw-r--r--lib/shellwords.rb16
-rw-r--r--lib/sync.rb13
-rw-r--r--lib/thread.rb163
-rw-r--r--lib/thwait.rb113
-rw-r--r--lib/tracer.rb147
-rw-r--r--lib/weakref.rb39
32 files changed, 1715 insertions, 1058 deletions
diff --git a/lib/base64.rb b/lib/base64.rb
index 96208a634d..d7461d82e7 100644
--- a/lib/base64.rb
+++ b/lib/base64.rb
@@ -1,50 +1,25 @@
-def decode64(str)
- string = ''
- for line in str.split("\n")
- line.delete!('^A-Za-z0-9+/') # remove non-base64 chars
- line.tr!('A-Za-z0-9+/', ' -_') # convert to uuencoded format
- len = ["#{32 + line.length * 3 / 4}"].pack("c")
- # compute length byte
- string += "#{len}#{line}".unpack("u") # uudecode and concatenate
- end
- return string
-end
+require "kconv"
-def j2e(str)
- while str =~ /\033\$B([^\033]*)\033\(B/
- s = $1
- pre, post = $`, $'
- s.gsub!(/./) { |ch|
- (ch[0]|0x80).chr
- }
- str = pre + s + post
- end
-# str.gsub!(/\033\$B([^\033]*)\033\(B/) {
-# $1.gsub!(/./) { |ch|
-# (ch[0]|0x80).chr
-# }
-# }
- str
+def decode64(str)
+ str.unpack("m")[0]
end
def decode_b(str)
str.gsub!(/=\?ISO-2022-JP\?B\?([!->@-~]+)\?=/i) {
decode64($1)
}
+ str = Kconv::toeuc(str)
+ str.gsub!(/=\?SHIFT_JIS\?B\?([!->@-~]+)\?=/i) {
+ decode64($1)
+ }
+ str = Kconv::toeuc(str)
str.gsub!(/\n/, ' ')
str.gsub!(/\0/, '')
- j2e(str)
+ str
end
def encode64(bin)
- encode = ""
- pad = 0
- [bin].pack("u").each do |uu|
- len = (2 + (uu[0] - 32)* 4) / 3
- encode << uu[1, len].tr('` -_', 'AA-Za-z0-9+/')
- pad += uu.length - 2 - len
- end
- encode + "=" * (pad % 3)
+ [bin].pack("m")
end
def b64encode(bin, len = 60)
diff --git a/lib/cgi-lib.rb b/lib/cgi-lib.rb
index 5234e046cc..7033f0f8c1 100644
--- a/lib/cgi-lib.rb
+++ b/lib/cgi-lib.rb
@@ -1,4 +1,3 @@
-#!/usr/local/bin/ruby
#
# Get CGI String
#
@@ -7,26 +6,26 @@
# foo = CGI.new
# foo['field'] <== value of 'field'
# foo.keys <== array of fields
-# foo.inputs <== hash of { <field> => <value> }
+# and foo has Hash class methods
# if running on Windows(IIS or PWS) then change cwd.
if ENV['SERVER_SOFTWARE'] =~ /^Microsoft-/ then
Dir.chdir ENV['PATH_TRANSLATED'].sub(/[^\\]+$/, '')
end
-require "shellwords.rb"
+require "delegate"
-class CGI
- include Shellwords
+class CGI < SimpleDelegator
attr("inputs")
# original is CGI.pm
def read_from_cmdline
- words = shellwords(if not ARGV.empty? then
+ require "shellwords.rb"
+ words = Shellwords.shellwords(if not ARGV.empty? then
ARGV.join(' ')
else
- print "(offline mode: enter name=value pairs on standard input)\n"
+ STDERR.print "(offline mode: enter name=value pairs on standard input)\n" if STDIN.tty?
readlines.join(' ').gsub(/\n/, '')
end.gsub(/\\=/, '%3D').gsub(/\\&/, '%26'))
@@ -47,32 +46,32 @@ class CGI
end
module_function :escape, :unescape
- def initialize
- # exception messages should be printed to stdout.
- STDERR.reopen(STDOUT)
+ def initialize(input = $stdin)
@inputs = {}
case ENV['REQUEST_METHOD']
when "GET"
+ # exception messages should be printed to stdout.
+ STDERR.reopen(STDOUT)
ENV['QUERY_STRING'] or ""
when "POST"
- $stdin.read ENV['CONTENT_LENGTH'].to_i
+ # exception messages should be printed to stdout.
+ STDERR.reopen(STDOUT)
+ input.read Integer(ENV['CONTENT_LENGTH'])
else
read_from_cmdline
end.split(/&/).each do |x|
key, val = x.split(/=/,2).collect{|x|unescape(x)}
- @inputs[key] += ("\0" if @inputs[key]) + (val or "")
+ if @inputs.include?(key)
+ @inputs[key] += "\0" + (val or "")
+ else
+ @inputs[key] = (val or "")
+ end
end
- end
- def keys
- @inputs.keys
+ super(@inputs)
end
- def [](key)
- @inputs[key]
- end
-
def CGI.message(msg, title = "")
print "Content-type: text/html\n\n"
print "<html><head><title>"
@@ -84,7 +83,7 @@ class CGI
end
def CGI.error
- m = $!.dup
+ m = $!.to_s.dup
m.gsub!(/&/, '&amp;')
m.gsub!(/</, '&lt;')
m.gsub!(/>/, '&gt;')
diff --git a/lib/complex.rb b/lib/complex.rb
index aa5d219d2f..59caad6ebc 100644
--- a/lib/complex.rb
+++ b/lib/complex.rb
@@ -1,8 +1,8 @@
#
# complex.rb -
# $Release Version: 0.5 $
-# $Revision: 1.1 $
-# $Date: 1996/11/11 04:25:19 $
+# $Revision: 1.3 $
+# $Date: 1998/07/08 10:05:28 $
# by Keiju ISHITSUKA(SHL Japan Inc.)
#
# --
@@ -59,6 +59,7 @@ def Complex(a, b = 0)
end
class Complex < Numeric
+ @RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-'
def Complex.generic?(other)
other.kind_of?(Integer) or
@@ -284,6 +285,11 @@ class Complex < Numeric
@real ^ @image
end
+ def inspect
+ sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
+ end
+
+
I = Complex(0,1)
attr :real
@@ -396,7 +402,7 @@ module Math
cos!(z)
else
Complex(cos!(z.real)*cosh!(z.image),
- sin!(z.real)*sinh!(z.image))
+ -sin!(z.real)*sinh!(z.image))
end
end
@@ -405,7 +411,7 @@ module Math
sin!(z)
else
Complex(sin!(z.real)*cosh!(z.image),
- -cos!(z.real)*sinh!(z.image))
+ cos!(z.real)*sinh!(z.image))
end
end
diff --git a/lib/date.rb b/lib/date.rb
index 998c2e8152..9de49bcbc7 100644
--- a/lib/date.rb
+++ b/lib/date.rb
@@ -1,8 +1,8 @@
#
# Date.rb -
# $Release Version: $
-# $Revision: 1.2 $
-# $Date: 1997/02/14 11:05:29 $
+# $Revision: 1.1.1.1.4.5 $
+# $Date: 1998/03/03 02:39:34 $
# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
#
# --
@@ -17,15 +17,34 @@
class Date
include Comparable
+ Weektag = [
+ "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
+ ]
+
+ Monthtag = [
+ "January","February","March","April", "May", "June","July",
+ "August", "September", "October", "November", "December"
+ ]
+
+ Monthtab = {
+ "jan"=>1, "feb"=>2, "mar"=>3, "apr"=>4, "may"=>5, "jun"=>6,
+ "jul"=>7, "aug"=>8, "sep"=>9, "oct"=>10, "nov"=>11, "dec"=>12
+ }
+
def initialize(y = 1, m = 1, d = 1)
- if y.kind_of?(String) && y.size == 8
- @year = y[0,4].to_i
- @month = y[4,2].to_i
- @day = y[6,2].to_i
+ if y.kind_of?(String)
+ case y
+ when /(\d\d\d\d)-?(?:(\d\d)-?(\d\d)?)?/
+ @year = $1.to_i
+ @month = if $2 then $2.to_i else 1 end
+ @day = if $3 then $3.to_i else 1 end
+ else
+ require 'parsedate'
+ @year, @month, @day = ParseDate.parsedate(y)
+ end
else
if m.kind_of?(String)
- ml = {"jan"=>1, "feb"=>2, "mar"=>3, "apr"=>4, "may"=>5, "jun"=>6, "jul"=>7, "aug"=>8, "sep"=>9, "oct"=>10, "nov"=>11, "dec"=>12}
- m = ml[m.downcase]
+ m = Monthtab[m.downcase]
if m.nil?
raise ArgumentError, "Wrong argument. (month)"
end
@@ -53,25 +72,35 @@ class Date
def period
return Date.period!(@year, @month, @day)
end
-
+
+ def jd
+ return period + 1721423
+ end
+
+ def mjd
+ return jd - 2400000.5
+ end
+
+ def to_s
+ format("%.3s, %.3s %2d %4d", name_of_week, name_of_month, @day, @year)
+ end
+
+ def inspect
+ to_s
+ end
+
def day_of_week
- dl = Date.daylist(@year)
- d = Date.jan1!(@year)
- for m in 1..(@month - 1)
- d += dl[m]
- end
- d += @day - 1
- if @year == 1752 && @month == 9 && @day >= 14
- d -= (14 - 3)
- end
- return (d % 7)
+ return (period + 5) % 7
end
- Weektag = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
def name_of_week
return Weektag[self.day_of_week]
end
+ def name_of_month
+ return Monthtag[@month-1]
+ end
+
def +(o)
if o.kind_of?(Numeric)
d = Integer(self.period + o)
@@ -80,6 +109,9 @@ class Date
else
raise TypeError, "Illegal type. (Integer or Date)"
end
+ if d <= 0
+ raise ArgumentError, "argument out of range. (self > other)"
+ end
return Date.at(d)
end
@@ -117,14 +149,13 @@ class Date
end
def leapyear?
- if Date.leapyear(@year) == 1
- return FALSE
- else
- return TRUE
- end
+ Date.leapyear(@year) != 1
end
def _check_date
+ if @year == nil or @month == nil or @day == nil
+ raise ArgumentError, "argument contains nil"
+ end
m = Date.daylist(@year)
if @month < 1 || @month > 12
raise ArgumentError, "argument(month) out of range."
@@ -151,7 +182,7 @@ end
def Date.at(d)
if d.kind_of? Time
- return Date.new(1900+d.year, d.mon+1, d.mday)
+ return Date.new(d.year, d.mon, d.mday)
end
if d.kind_of? Date
return Date.at(d.period)
@@ -189,10 +220,10 @@ def Date.period!(y, m, d)
p += dl[mm]
end
p += (y - 1) * 365 + ((y - 1) / 4.0).to_i
- if (y - 1) > 1752
- p -= ((y - 1 - 1752) / 100.0).to_i
- p += ((y - 1 - 1752) / 400.0).to_i
- p -= (14 - 3)
+ if y > 1752
+ p -= ((y - 1) / 100.0).to_i
+ p += ((y - 1) / 400.0).to_i
+ p += 2
elsif y == 1752 && m == 9 && d >= 14 && d <= 30
p -= (14 - 3)
end
diff --git a/lib/debug.rb b/lib/debug.rb
index 432c7b4d19..90270a3fe7 100644
--- a/lib/debug.rb
+++ b/lib/debug.rb
@@ -11,6 +11,8 @@ class DEBUGGER__
@scripts = {}
end
+ DEBUG_LAST_CMD = []
+
def interrupt
@stop_next = 1
end
@@ -40,6 +42,11 @@ class DEBUGGER__
STDOUT.flush
while input = STDIN.gets
input.chop!
+ if input == ""
+ input = DEBUG_LAST_CMD[0]
+ else
+ DEBUG_LAST_CMD[0] = input
+ end
case input
when /^b(reak)?\s+(([^:\n]+:)?.+)/
pos = $2
@@ -169,7 +176,7 @@ class DEBUGGER__
printf "no sourcefile available for %s\n", file
end
when /^p\s+/
- p debug_eval($', binding)
+ p debug_eval($', binding) #'
else
v = debug_eval(input, binding)
p v unless v == nil
@@ -187,10 +194,13 @@ class DEBUGGER__
return "\n" unless line
return line
end
+ save = $DEBUG
begin
+ $DEBUG = FALSE
f = open(file)
lines = @scripts[file] = f.readlines
rescue
+ $DEBUG = save
@scripts[file] = TRUE
return "\n"
end
diff --git a/lib/delegate.rb b/lib/delegate.rb
index e5943cead8..0771f2feeb 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -1,26 +1,51 @@
# Delegation class that delegates even methods defined in super class,
# which can not be covered with normal method_missing hack.
#
-# Delegater is the abstract delegation class. Need to redefine
-# `__getobj__' method in the subclass. SimpleDelegater is the
+# Delegator is the abstract delegation class. Need to redefine
+# `__getobj__' method in the subclass. SimpleDelegator is the
# concrete subclass for simple delegation.
#
# Usage:
# foo = Object.new
-# foo = SimpleDelegater.new(foo)
-# foo.type # => Object
+# foo2 = SimpleDelegator.new(foo)
+# foo.hash == foo2.hash # => true
+#
+# Foo = DelegateClass(Array)
+#
+# class ExtArray<DelegateClass(Array)
+# ...
+# end
-class Delegater
+class Delegator
def initialize(obj)
- preserved = ["id", "equal?", "__getobj__"]
+ preserved = ::Kernel.instance_methods
+ preserved -= ["to_s","to_a","inspect","==","=~","==="]
for t in self.type.ancestors
preserved |= t.instance_methods
- break if t == Delegater
+ preserved |= t.private_instance_methods
+ preserved |= t.protected_instance_methods
+ break if t == Delegator
end
for method in obj.methods
next if preserved.include? method
- eval "def self.#{method}(*args); __getobj__.send :#{method}, *args; end"
+ eval <<-EOS
+ def self.#{method}(*args, &block)
+ begin
+ __getobj__.__send__(:#{method}, *args, &block)
+ rescue Exception
+ c = -caller(0).size
+ if /:in `__getobj__'$/ =~ $@[c-1] #`
+ n = 1
+ else
+ c -= 1
+ n = 2
+ end
+ $@[c,n] = nil
+ raise
+ end
+ end
+ EOS
end
end
@@ -30,7 +55,7 @@ class Delegater
end
-class SimpleDelegater<Delegater
+class SimpleDelegator<Delegator
def initialize(obj)
super
@@ -41,4 +66,61 @@ class SimpleDelegater<Delegater
@obj
end
+ def __setobj__(obj)
+ @obj = obj
+ end
+end
+
+# backward compatibility ^_^;;;
+Delegater = Delegator
+SimpleDelegater = SimpleDelegator
+
+#
+def DelegateClass(superclass)
+ klass = Class.new
+ methods = superclass.instance_methods
+ methods -= ::Kernel.instance_methods
+ methods |= ["to_s","to_a","inspect","==","=~","==="]
+ klass.module_eval <<-EOS
+ def initialize(obj)
+ @obj = obj
+ end
+ EOS
+ for method in methods
+ klass.module_eval <<-EOS
+ def #{method}(*args, &block)
+ begin
+ @obj.__send__(:#{method}, *args, &block)
+ rescue
+ $@[0,2] = nil
+ raise
+ end
+ end
+ EOS
+ end
+ return klass;
+ end
+
+if __FILE__ == $0
+ class ExtArray<DelegateClass(Array)
+ def initialize()
+ super([])
+ end
+ end
+
+ ary = ExtArray.new
+ p ary.type
+ ary.push 25
+ p ary
+
+ foo = Object.new
+ def foo.test
+ 25
+ end
+ def foo.error
+ raise 'this is OK'
+ end
+ foo2 = SimpleDelegator.new(foo)
+ p foo.test == foo2.test # => true
+ foo2.error # raise error!
end
diff --git a/lib/e2mmap.rb b/lib/e2mmap.rb
index d10657bbad..bf860dc5c1 100644
--- a/lib/e2mmap.rb
+++ b/lib/e2mmap.rb
@@ -1,11 +1,22 @@
#
# e2mmap.rb - for ruby 1.1
-# $Release Version: 1.1$
-# $Revision: 1.4 $
-# $Date: 1997/08/18 07:12:12 $
+# $Release Version: 1.2$
+# $Revision: 1.8 $
+# $Date: 1998/08/19 15:22:22 $
# by Keiju ISHITSUKA
#
# --
+# Usage:
+#
+# class Foo
+# extend Exception2MassageMapper
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# def_e2meggage ExistingExceptionClass, "message..."
+# ...
+# end
+#
+# Foo.Fail NewExceptionClass, arg...
+# Foo.Fail ExistingExceptionClass, arg...
#
#
if VERSION < "1.1"
@@ -13,40 +24,60 @@ if VERSION < "1.1"
else
module Exception2MessageMapper
- RCS_ID='-$Header: /home/keiju/var/src/var.lib/ruby/RCS/e2mmap.rb,v 1.4 1997/08/18 07:12:12 keiju Exp keiju $-'
+ @RCS_ID='-$Id: e2mmap.rb,v 1.8 1998/08/19 15:22:22 keiju Exp keiju $-'
E2MM = Exception2MessageMapper
-
+
def E2MM.extend_object(cl)
super
cl.bind(self)
end
- # �����Ȥθߴ����Τ���˻Ĥ��Ƥ���.
+ # backward compatibility
def E2MM.extend_to(b)
c = eval("self", b)
c.extend(self)
end
-# public :fail
- # alias e2mm_fail fail
+ # public :fail
+ alias fail! fail
+
+ #def fail(err = nil, *rest)
+ # super
+ #end
- def fail(err = nil, *rest)
- Exception2MessageMapper.fail Exception2MessageMapper::ErrNotRegisteredException, err.to_s
+ def Fail(err = nil, *rest)
+ Exception2MessageMapper.Fail Exception2MessageMapper::ErrNotRegisteredException, err.inspect
end
def bind(cl)
self.module_eval %q^
- E2MM_ErrorMSG = {}
+ E2MM_ErrorMSG = {} unless self.const_defined?(:E2MM_ErrorMSG)
# fail(err, *rest)
- # err: �㳰
- # rest: ��å��������Ϥ��ѥ�᡼��
+ # err: Exception
+ # rest: Parameter accompanied with the exception
#
+ def self.Fail(err = nil, *rest)
+ if form = E2MM_ErrorMSG[err]
+ $! = err.new(sprintf(form, *rest))
+ $@ = caller(0) if [email protected]?
+ # e2mm_fail()
+ raise()
+# elsif self == Exception2MessageMapper
+# fail Exception2MessageMapper::ErrNotRegisteredException, err.to_s
+ else
+# print "super\n"
+ super
+ end
+ end
+
+ # ���θߴ����Τ���
def self.fail(err = nil, *rest)
- $@ = caller(0) if [email protected]?
if form = E2MM_ErrorMSG[err]
$! = err.new(sprintf(form, *rest))
+ $@ = caller(0) if [email protected]?
# e2mm_fail()
raise()
# elsif self == Exception2MessageMapper
@@ -63,7 +94,6 @@ else
# def_exception(c, m)
# c: exception
# m: message_form
- # �㳰c�Υ�å�������m�Ȥ���.
#
def self.def_e2message(c, m)
E2MM_ErrorMSG[c] = m
@@ -72,13 +102,21 @@ else
# def_exception(c, m)
# n: exception_name
# m: message_form
- # s: �㳰�����ѡ����饹(�ǥե����: Exception)
- # �㳰̾``c''�����㳰�������, ���Υ�å�������m�Ȥ���.
+ # s: superclass_of_exception (default: Exception)
+ # defines excaption named ``c'', whose message is ``m''.
#
#def def_exception(n, m)
- def self.def_exception(n, m, s = Exception)
+ def self.def_exception(n, m, s = nil)
n = n.id2name if n.kind_of?(Fixnum)
+ unless s
+ if defined?(StandardError)
+ s = StandardError
+ else
+ s = Exception
+ end
+ end
e = Class.new(s)
+
const_set(n, e)
E2MM_ErrorMSG[e] = m
# const_get(:E2MM_ErrorMSG)[e] = m
@@ -91,4 +129,3 @@ else
def_exception(:ErrNotRegisteredException, "not registerd exception(%s)")
end
end
-
diff --git a/lib/eregex.rb b/lib/eregex.rb
index f214f6a2d4..384d531e0f 100644
--- a/lib/eregex.rb
+++ b/lib/eregex.rb
@@ -30,10 +30,7 @@ class Regexp
end
end
-p "abc" =~ /b/|/c/
-p "abc" =~ /b/&/c/
-
-
-
-
-
+if __FILE__ == $0
+ p "abc" =~ /b/|/c/
+ p "abc" =~ /b/&/c/
+end
diff --git a/lib/finalize.rb b/lib/finalize.rb
index 9b2ffefcf5..a07e67d093 100644
--- a/lib/finalize.rb
+++ b/lib/finalize.rb
@@ -1,8 +1,8 @@
#
# finalizer.rb -
-# $Release Version: 0.2$
-# $Revision: 1.3 $
-# $Date: 1998/01/09 08:09:49 $
+# $Release Version: 0.3$
+# $Revision: 1.4 $
+# $Date: 1998/02/27 05:34:33 $
# by Keiju ISHITSUKA
#
# --
@@ -11,44 +11,42 @@
#
# add(obj, dependant, method = :finalize, *opt)
# add_dependency(obj, dependant, method = :finalize, *opt)
-# ��¸�ط� R_method(obj, dependant) ���ɲ�
+# add dependency R_method(obj, dependant)
#
# delete(obj_or_id, dependant, method = :finalize)
# delete_dependency(obj_or_id, dependant, method = :finalize)
-# ��¸�ط� R_method(obj, dependant) �κ��
+# delete dependency R_method(obj, dependant)
# delete_all_dependency(obj_or_id, dependant)
-# ��¸�ط� R_*(obj, dependant) �κ��
+# delete dependency R_*(obj, dependant)
# delete_by_dependant(dependant, method = :finalize)
-# ��¸�ط� R_method(*, dependant) �κ��
+# delete dependency R_method(*, dependant)
# delete_all_by_dependant(dependant)
-# ��¸�ط� R_*(*, dependant) �κ��
+# delete dependency R_*(*, dependant)
# delete_all
-# ���Ƥΰ�¸�ط��κ��.
+# delete all dependency R_*(*, *)
#
# finalize(obj_or_id, dependant, method = :finalize)
# finalize_dependency(obj_or_id, dependant, method = :finalize)
-# ��¸��Ϣ R_method(obj, dependtant) �Ƿ�Ф��dependant��
-# finalize����.
+# finalize the dependant connected by dependency R_method(obj, dependtant).
# finalize_all_dependency(obj_or_id, dependant)
-# ��¸��Ϣ R_*(obj, dependtant) �Ƿ�Ф��dependant��finalize����.
+# finalize all dependants connected by dependency R_*(obj, dependtant).
# finalize_by_dependant(dependant, method = :finalize)
-# ��¸��Ϣ R_method(*, dependtant) �Ƿ�Ф��dependant��finalize����.
+# finalize the dependant connected by dependency R_method(*, dependtant).
# fainalize_all_by_dependant(dependant)
-# ��¸��Ϣ R_*(*, dependtant) �Ƿ�Ф��dependant��finalize����.
+# finalize all dependants connected by dependency R_*(*, dependant).
# finalize_all
-# Finalizer����Ͽ��������Ƥ�dependant��finalize����
+# finalize all dependency registered to the Finalizer.
#
# safe{..}
-# gc����Finalizer����ư����Τ�ߤ��.
-#
+# stop invoking Finalizer on GC.
#
module Finalizer
- RCS_ID='-$Header: /home/keiju/var/src/var.lib/ruby/RCS/finalize.rb,v 1.3 1998/01/09 08:09:49 keiju Exp keiju $-'
+ RCS_ID='-$Id: finalize.rb,v 1.4 1998/02/27 05:34:33 keiju Exp keiju $-'
# @dependency: {id => [[dependant, method, *opt], ...], ...}
- # ��¸�ط� R_method(obj, dependant) ���ɲ�
+ # add dependency R_method(obj, dependant)
def add_dependency(obj, dependant, method = :finalize, *opt)
ObjectSpace.call_finalizer(obj)
method = method.intern unless method.kind_of?(Integer)
@@ -61,7 +59,7 @@ module Finalizer
end
alias add add_dependency
- # ��¸�ط� R_method(obj, dependant) �κ��
+ # delete dependency R_method(obj, dependant)
def delete_dependency(id, dependant, method = :finalize)
id = id.id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
@@ -75,7 +73,7 @@ module Finalizer
end
alias delete delete_dependency
- # ��¸�ط� R_*(obj, dependant) �κ��
+ # delete dependency R_*(obj, dependant)
def delete_all_dependency(id, dependant)
id = id.id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
@@ -88,30 +86,29 @@ module Finalizer
end
end
- # ��¸�ط� R_method(*, dependant) �κ��
+ # delete dependency R_method(*, dependant)
def delete_by_dependant(dependant, method = :finalize)
method = method.intern unless method.kind_of?(Integer)
- for id in Dependency.keys
+ for id in @dependency.keys
delete(id, dependant, method)
end
end
- # ��¸�ط� R_*(*, dependant) �κ��
+ # delete dependency R_*(*, dependant)
def delete_all_by_dependant(dependant)
for id in @dependency.keys
delete_all_dependency(id, dependant)
end
end
- # ��¸��Ϣ R_method(obj, dependtant) �Ƿ�Ф��dependant��finalize��
- # ��.
+ # finalize the depandant connected by dependency R_method(obj, dependtant)
def finalize_dependency(id, dependant, method = :finalize)
id = id.id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
for assocs in @dependency[id]
assocs.delete_if do
|d, m, *o|
- d.send(m, *o) if ret = d == dependant && m == method
+ d.send(m, id, *o) if ret = d == dependant && m == method
ret
end
@dependency.delete(id) if assoc.empty?
@@ -119,20 +116,20 @@ module Finalizer
end
alias finalize finalize_dependency
- # ��¸��Ϣ R_*(obj, dependtant) �Ƿ�Ф��dependant��finalize����.
+ # finalize all dependants connected by dependency R_*(obj, dependtant)
def finalize_all_dependency(id, dependant)
id = id.id unless id.kind_of?(Integer)
method = method.intern unless method.kind_of?(Integer)
for assoc in @dependency[id]
assoc.delete_if do
|d, m, *o|
- d.send(m, *o) if ret = d == dependant
+ d.send(m, id, *o) if ret = d == dependant
end
@dependency.delete(id) if assoc.empty?
end
end
- # ��¸��Ϣ R_method(*, dependtant) �Ƿ�Ф��dependant��finalize����.
+ # finalize the dependant connected by dependency R_method(*, dependtant)
def finalize_by_dependant(dependant, method = :finalize)
method = method.intern unless method.kind_of?(Integer)
for id in @dependency.keys
@@ -140,14 +137,14 @@ module Finalizer
end
end
- # ��¸��Ϣ R_*(*, dependtant) �Ƿ�Ф��dependant��finalize����.
+ # finalize all dependants connected by dependency R_*(*, dependtant)
def fainalize_all_by_dependant(dependant)
for id in @dependency.keys
finalize_all_dependency(id, dependant)
end
end
- # Finalizer����Ͽ����Ƥ������Ƥ�dependant��finalize����
+ # finalize all dependants registered to the Finalizer.
def finalize_all
for id, assocs in @dependency
for dependant, method, *opt in assocs
@@ -157,7 +154,7 @@ module Finalizer
end
end
- # finalize_* ������˸ƤӽФ�����Υ��ƥ졼��
+ # method to call finalize_* safely.
def safe
old_status = Thread.critical
Thread.critical = TRUE
@@ -167,7 +164,7 @@ module Finalizer
Thread.critical = old_status
end
- # ObjectSpace#add_finalizer�ؤ���Ͽ�ؿ�
+ # registering function to ObjectSpace#add_finalizer
def final_of(id)
if assocs = @dependency.delete(id)
for dependant, method, *opt in assocs
@@ -202,4 +199,3 @@ module Finalizer
private_class_method :final_of
end
-
diff --git a/lib/find.rb b/lib/find.rb
index 5ecc54329c..3f1b82d2b3 100644
--- a/lib/find.rb
+++ b/lib/find.rb
@@ -1,5 +1,5 @@
# Usage:
-# require "find.rb"
+# require "find"
#
# Find.find('/foo','/bar') {|f| ...}
# or
@@ -12,7 +12,7 @@ module Find
while file = path.shift
catch(:prune) {
yield file
- if File.directory? file and not File.symlink? file then
+ if File.directory? file then
d = Dir.open(file)
begin
for f in d
diff --git a/lib/ftools.rb b/lib/ftools.rb
index 59bc81b365..7ccc7a4468 100644
--- a/lib/ftools.rb
+++ b/lib/ftools.rb
@@ -30,7 +30,7 @@ class << File
to.binmode
begin
- while TRUE
+ while true
r = from.sysread(fsize)
rsize = r.size
w = 0
@@ -40,9 +40,9 @@ class << File
end
end
rescue EOFError
- ret = TRUE
+ ret = true
rescue
- ret = FALSE
+ ret = false
ensure
to.close
from.close
@@ -50,7 +50,7 @@ class << File
ret
end
- def copy from, to, verbose = FALSE
+ def copy from, to, verbose = false
$stderr.print from, " -> ", catname(from, to), "\n" if verbose
syscopy from, to
end
@@ -59,11 +59,11 @@ class << File
# move file
- def move from, to, verbose = FALSE
+ def move from, to, verbose = false
to = catname(from, to)
$stderr.print from, " -> ", to, "\n" if verbose
- if PLATFORM =~ /djgpp|cygwin32|mswin32/ and FileTest.file? to
+ if PLATFORM =~ /djgpp|cygwin|mswin32/ and FileTest.file? to
unlink to
end
begin
@@ -76,10 +76,10 @@ class << File
alias mv move
# compare two files
-# TRUE: identical
-# FALSE: not identical
+# true: identical
+# false: not identical
- def compare from, to, verbose = FALSE
+ def compare from, to, verbose = false
$stderr.print from, " <=> ", to, "\n" if verbose
fsize = size(from)
fsize = 1024 if fsize < 512
@@ -90,7 +90,7 @@ class << File
to = open(to, "r")
to.binmode
- ret = FALSE
+ ret = false
fr = tr = ''
begin
@@ -103,7 +103,7 @@ class << File
end
end
rescue
- ret = FALSE
+ ret = false
ensure
to.close
from.close
@@ -116,7 +116,7 @@ class << File
# unlink files safely
def safe_unlink(*files)
- verbose = if files[-1].is_a? String then FALSE else files.pop end
+ verbose = if files[-1].is_a? String then false else files.pop end
begin
$stderr.print files.join(" "), "\n" if verbose
chmod 0777, *files
@@ -129,7 +129,7 @@ class << File
alias rm_f safe_unlink
def makedirs(*dirs)
- verbose = if dirs[-1].is_a? String then FALSE else dirs.pop end
+ verbose = if dirs[-1].is_a? String then false else dirs.pop end
# mode = if dirs[-1].is_a? Fixnum then dirs.pop else 0755 end
mode = 0755
for dir in dirs
@@ -146,14 +146,15 @@ class << File
alias o_chmod chmod
def chmod(mode, *files)
- verbose = if files[-1].is_a? String then FALSE else files.pop end
+ verbose = if files[-1].is_a? String then false else files.pop end
$stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
o_chmod mode, *files
end
- def install(from, to, mode, verbose)
+ def install(from, to, mode = nil, verbose = false)
to = catname(from, to)
unless FileTest.exist? to and cmp from, to
+ unlink to if FileTest.exist? to
cp from, to, verbose
chmod mode, to, verbose if mode
end
diff --git a/lib/ftplib.rb b/lib/ftplib.rb
index 34ee2f8d62..617d85899b 100644
--- a/lib/ftplib.rb
+++ b/lib/ftplib.rb
@@ -1,617 +1,574 @@
-### ftplib.rb -*- Mode: ruby; tab-width: 8; -*-
+## ftplib.rb
-## $Revision: 1.5 $
-## $Date: 1997/09/16 08:03:31 $
-## by maeda shugo <[email protected]>
+# Author: Shugo Maeda <[email protected]>
+# Version: $Revision: 1.7 $
-### Code:
+## Code:
require "socket"
-require "sync" if defined? Thread
+require "monitor"
-class FTPError < Exception; end
+class FTPError < StandardError; end
class FTPReplyError < FTPError; end
class FTPTempError < FTPError; end
class FTPPermError < FTPError; end
class FTPProtoError < FTPError; end
class FTP
-
- RCS_ID = '$Id: ftplib.rb,v 1.5 1997/09/16 08:03:31 shugo Exp $'
-
- FTP_PORT = 21
- CRLF = "\r\n"
-
- attr :passive, TRUE
- attr :return_code, TRUE
- attr :debug_mode, TRUE
- attr :welcome
- attr :lastresp
-
- THREAD_SAFE = defined?(Thread) != FALSE
-
- if THREAD_SAFE
- def synchronize(mode = :EX)
- if @sync
- @sync.synchronize(mode) do
- yield
- end
- end
- end
-
- def sock_synchronize(mode = :EX)
- if @sock
- @sock.synchronize(mode) do
- yield
- end
- end
- end
- else
- def synchronize(mode = :EX)
- yield
- end
-
- def sock_synchronize(mode = :EX)
- yield
- end
- end
- private :sock_synchronize
-
- def FTP.open(host, user = nil, passwd = nil, acct = nil)
- new(host, user, passwd, acct)
- end
+
+ RCS_ID = %q$Id: ftplib.rb,v 1.7 1998/04/13 12:34:24 shugo Exp shugo $
+
+ include MonitorMixin
+
+ FTP_PORT = 21
+ CRLF = "\r\n"
+
+ attr_accessor :passive, :return_code, :debug_mode
+ attr_reader :welcome, :lastresp
+
+ def FTP.open(host, user = nil, passwd = nil, acct = nil)
+ new(host, user, passwd, acct)
+ end
- def initialize(host = nil, user = nil,
- passwd = nil, acct = nil)
- if THREAD_SAFE
- @sync = Sync.new
- end
- @passive = FALSE
- @return_code = "\n"
- @debug_mode = FALSE
- if host
- connect(host)
- if user
- login(user, passwd, acct)
- end
- end
- end
-
- def open_socket(host, port)
- if defined? SOCKSsocket and ENV["SOCKS_SERVER"]
- @passive = TRUE
- SOCKSsocket.open(host, port)
- else
- TCPsocket.open(host, port)
- end
- end
- private :open_socket
-
- def connect(host, port = FTP_PORT)
- if @debug_mode
- print "connect: ", host, ", ", port, "\n"
- end
- synchronize do
- @sock = open_socket(host, port)
- if THREAD_SAFE
- @sock.extend Sync_m
- end
- voidresp
- end
- end
-
- def sanitize(s)
- if s =~ /^PASS /i
- s[0, 5] + "*" * (s.length - 5)
- else
- s
- end
- end
- private :sanitize
-
- def putline(line)
- if @debug_mode
- print "put: ", sanitize(line), "\n"
- end
- line = line + CRLF
- @sock.write(line)
- end
- private :putline
-
- def getline
- line = @sock.readline # if get EOF, raise EOFError
- if line[-2, 2] == CRLF
- line = line[0 .. -3]
- elsif line[-1] == ?\r or
- line[-1] == ?\n
- line = line[0 .. -2]
- end
- if @debug_mode
- print "get: ", sanitize(line), "\n"
- end
- line
- end
- private :getline
-
- def getmultiline
- line = getline
- buff = line
- if line[3] == ?-
- code = line[0, 3]
- begin
- line = getline
- buff << "\n" << line
- end until line[0, 3] == code and line[3] != ?-
- end
- buff << "\n"
- end
- private :getmultiline
-
- def getresp
- resp = getmultiline
- @lastresp = resp[0, 3]
- c = resp[0]
- case c
- when ?1, ?2, ?3
- return resp
- when ?4
- raise FTPTempError, resp
- when ?5
- raise FTPPermError, resp
- else
- raise FTPProtoError, resp
- end
- end
- private :getresp
-
- def voidresp
- resp = getresp
- if resp[0] != ?2
- raise FTPReplyError, resp
- end
- end
- private :voidresp
-
- def sendcmd(cmd)
- synchronize do
- sock_synchronize do
- putline(cmd)
- getresp
- end
- end
- end
-
- def voidcmd(cmd)
- synchronize do
- sock_synchronize do
- putline(cmd)
- voidresp
- end
- end
- nil
- end
-
- def sendport(host, port)
- hbytes = host.split(".")
- pbytes = [port / 256, port % 256]
- bytes = hbytes + pbytes
- cmd = "PORT " + bytes.join(",")
- voidcmd(cmd)
- end
- private :sendport
-
- def makeport
- sock = TCPserver.open(0)
- port = sock.addr[1]
- host = TCPsocket.getaddress(@sock.addr[2])
- resp = sendport(host, port)
- sock
- end
- private :makeport
-
- def transfercmd(cmd)
- if @passive
- host, port = parse227(sendcmd("PASV"))
- conn = open_socket(host, port)
- resp = sendcmd(cmd)
- if resp[0] != ?1
- raise FTPReplyError, resp
- end
- else
- sock = makeport
- resp = sendcmd(cmd)
- if resp[0] != ?1
- raise FTPReplyError, resp
- end
- conn = sock.accept
- end
- conn
- end
- private :transfercmd
-
- def getaddress
- thishost = Socket.gethostname
- if not thishost.index(".")
- thishost = Socket.gethostbyname(thishost)[0]
- end
- if ENV.has_key?("LOGNAME")
- realuser = ENV["LOGNAME"]
- elsif ENV.has_key?("USER")
- realuser = ENV["USER"]
- else
- realuser = "anonymous"
- end
- realuser + "@" + thishost
- end
- private :getaddress
-
- def login(user = "anonymous", passwd = nil, acct = nil)
- if user == "anonymous" and passwd == nil
- passwd = getaddress
- end
-
- resp = ""
- synchronize do
- resp = sendcmd('USER ' + user)
- if resp[0] == ?3
- resp = sendcmd('PASS ' + passwd)
- end
- if resp[0] == ?3
- resp = sendcmd('ACCT ' + acct)
- end
- end
- if resp[0] != ?2
- raise FTPReplyError, resp
- end
- @welcome = resp
- end
+ def initialize(host = nil, user = nil, passwd = nil, acct = nil)
+ super
+ @passive = false
+ @return_code = "\n"
+ @debug_mode = false
+ if host
+ connect(host)
+ if user
+ login(user, passwd, acct)
+ end
+ end
+ end
+
+ def open_socket(host, port)
+ if defined? SOCKSsocket and ENV["SOCKS_SERVER"]
+ @passive = true
+ return SOCKSsocket.open(host, port)
+ else
+ return TCPsocket.open(host, port)
+ end
+ end
+ private :open_socket
+
+ def connect(host, port = FTP_PORT)
+ if @debug_mode
+ print "connect: ", host, ", ", port, "\n"
+ end
+ synchronize do
+ @sock = open_socket(host, port)
+ voidresp
+ end
+ end
+
+ def sanitize(s)
+ if s =~ /^PASS /i
+ return s[0, 5] + "*" * (s.length - 5)
+ else
+ return s
+ end
+ end
+ private :sanitize
+
+ def putline(line)
+ if @debug_mode
+ print "put: ", sanitize(line), "\n"
+ end
+ line = line + CRLF
+ @sock.write(line)
+ end
+ private :putline
+
+ def getline
+ line = @sock.readline # if get EOF, raise EOFError
+ if line[-2, 2] == CRLF
+ line = line[0 .. -3]
+ elsif line[-1] == ?\r or
+ line[-1] == ?\n
+ line = line[0 .. -2]
+ end
+ if @debug_mode
+ print "get: ", sanitize(line), "\n"
+ end
+ return line
+ end
+ private :getline
+
+ def getmultiline
+ line = getline
+ buff = line
+ if line[3] == ?-
+ code = line[0, 3]
+ begin
+ line = getline
+ buff << "\n" << line
+ end until line[0, 3] == code and line[3] != ?-
+ end
+ return buff << "\n"
+ end
+ private :getmultiline
+
+ def getresp
+ resp = getmultiline
+ @lastresp = resp[0, 3]
+ c = resp[0]
+ case c
+ when ?1, ?2, ?3
+ return resp
+ when ?4
+ raise FTPTempError, resp
+ when ?5
+ raise FTPPermError, resp
+ else
+ raise FTPProtoError, resp
+ end
+ end
+ private :getresp
+
+ def voidresp
+ resp = getresp
+ if resp[0] != ?2
+ raise FTPReplyError, resp
+ end
+ end
+ private :voidresp
+
+ def sendcmd(cmd)
+ synchronize do
+ putline(cmd)
+ return getresp
+ end
+ end
- def retrbinary(cmd, blocksize, callback = Proc.new)
- synchronize do
- voidcmd("TYPE I")
- conn = transfercmd(cmd)
- while TRUE
- data = conn.read(blocksize)
- break if data == nil
- callback.call(data)
- end
- conn.close
- voidresp
- end
- end
+ def voidcmd(cmd)
+ synchronize do
+ putline(cmd)
+ voidresp
+ end
+ end
- def retrlines(cmd, callback = nil)
- if iterator?
- callback = Proc.new
- elsif not callback.is_a?(Proc)
- callback = Proc.new {|line| print line, "\n"}
- end
- synchronize do
- voidcmd("TYPE A")
- conn = transfercmd(cmd)
- while TRUE
- line = conn.gets
- break if line == nil
- if line[-2, 2] == CRLF
- line = line[0 .. -3]
- elsif line[-1] == ?\n
- line = line[0 .. -2]
- end
- callback.call(line)
- end
- conn.close
- voidresp
- end
- end
+ def sendport(host, port)
+ hbytes = host.split(".")
+ pbytes = [port / 256, port % 256]
+ bytes = hbytes + pbytes
+ cmd = "PORT " + bytes.join(",")
+ voidcmd(cmd)
+ end
+ private :sendport
+
+ def makeport
+ sock = TCPserver.open(0)
+ port = sock.addr[1]
+ host = TCPsocket.getaddress(@sock.addr[2])
+ resp = sendport(host, port)
+ return sock
+ end
+ private :makeport
+
+ def transfercmd(cmd)
+ if @passive
+ host, port = parse227(sendcmd("PASV"))
+ conn = open_socket(host, port)
+ resp = sendcmd(cmd)
+ if resp[0] != ?1
+ raise FTPReplyError, resp
+ end
+ else
+ sock = makeport
+ resp = sendcmd(cmd)
+ if resp[0] != ?1
+ raise FTPReplyError, resp
+ end
+ conn = sock.accept
+ end
+ return conn
+ end
+ private :transfercmd
+
+ def getaddress
+ thishost = Socket.gethostname
+ if not thishost.index(".")
+ thishost = Socket.gethostbyname(thishost)[0]
+ end
+ if ENV.has_key?("LOGNAME")
+ realuser = ENV["LOGNAME"]
+ elsif ENV.has_key?("USER")
+ realuser = ENV["USER"]
+ else
+ realuser = "anonymous"
+ end
+ return realuser + "@" + thishost
+ end
+ private :getaddress
- def storbinary(cmd, file, blocksize, callback = nil)
- if iterator?
- callback = Proc.new
- end
- use_callback = callback.is_a?(Proc)
- synchronize do
- voidcmd("TYPE I")
- conn = transfercmd(cmd)
- while TRUE
- buf = file.read(blocksize)
- break if buf == nil
- conn.write(buf)
- if use_callback
- callback.call(buf)
- end
- end
- conn.close
- voidresp
- end
- end
+ def login(user = "anonymous", passwd = nil, acct = nil)
+ if user == "anonymous" and passwd == nil
+ passwd = getaddress
+ end
+
+ resp = ""
+ synchronize do
+ resp = sendcmd('USER ' + user)
+ if resp[0] == ?3
+ resp = sendcmd('PASS ' + passwd)
+ end
+ if resp[0] == ?3
+ resp = sendcmd('ACCT ' + acct)
+ end
+ end
+ if resp[0] != ?2
+ raise FTPReplyError, resp
+ end
+ @welcome = resp
+ end
+
+ def retrbinary(cmd, blocksize, callback = Proc.new)
+ synchronize do
+ voidcmd("TYPE I")
+ conn = transfercmd(cmd)
+ loop do
+ data = conn.read(blocksize)
+ break if data == nil
+ callback.call(data)
+ end
+ conn.close
+ voidresp
+ end
+ end
- def storlines(cmd, file, callback = nil)
- if iterator?
- callback = Proc.new
- end
- use_callback = callback.is_a?(Proc)
- synchronize do
- voidcmd("TYPE A")
- conn = transfercmd(cmd)
- while TRUE
- buf = file.gets
- break if buf == nil
- if buf[-2, 2] != CRLF
- if buf[-1] == ?\r or
- buf[-1] == ?\n
- buf = buf[0 .. -2]
- end
- buf = buf + CRLF
- end
- conn.write(buf)
- if use_callback
- callback.call(buf)
- end
- end
- conn.close
- voidresp
- end
- end
+ def retrlines(cmd, callback = nil)
+ if iterator?
+ callback = Proc.new
+ elsif not callback.is_a?(Proc)
+ callback = Proc.new {|line| print line, "\n"}
+ end
+ synchronize do
+ voidcmd("TYPE A")
+ conn = transfercmd(cmd)
+ loop do
+ line = conn.gets
+ break if line == nil
+ if line[-2, 2] == CRLF
+ line = line[0 .. -3]
+ elsif line[-1] == ?\n
+ line = line[0 .. -2]
+ end
+ callback.call(line)
+ end
+ conn.close
+ voidresp
+ end
+ end
+
+ def storbinary(cmd, file, blocksize, callback = nil)
+ if iterator?
+ callback = Proc.new
+ end
+ use_callback = callback.is_a?(Proc)
+ synchronize do
+ voidcmd("TYPE I")
+ conn = transfercmd(cmd)
+ loop do
+ buf = file.read(blocksize)
+ break if buf == nil
+ conn.write(buf)
+ callback.call(buf) if use_callback
+ end
+ conn.close
+ voidresp
+ end
+ end
- def getbinaryfile(remotefile, localfile,
- blocksize, callback = nil)
- if iterator?
- callback = Proc.new
- end
- use_callback = callback.is_a?(Proc)
- f = open(localfile, "w")
- begin
+ def storlines(cmd, file, callback = nil)
+ if iterator?
+ callback = Proc.new
+ end
+ use_callback = callback.is_a?(Proc)
+ synchronize do
+ voidcmd("TYPE A")
+ conn = transfercmd(cmd)
+ loop do
+ buf = file.gets
+ break if buf == nil
+ if buf[-2, 2] != CRLF
+ if buf[-1] == ?\r or
+ buf[-1] == ?\n
+ buf = buf[0 .. -2]
+ end
+ buf = buf + CRLF
+ end
+ conn.write(buf)
+ callback.call(buf) if use_callback
+ end
+ conn.close
+ voidresp
+ end
+ end
+
+ def getbinaryfile(remotefile, localfile, blocksize, callback = nil)
+ if iterator?
+ callback = Proc.new
+ end
+ use_callback = callback.is_a?(Proc)
+ f = open(localfile, "w")
+ begin
f.binmode
- retrbinary("RETR " + remotefile, blocksize) do |data|
- f.write(data)
- if use_callback
- callback.call(data)
- end
- end
- ensure
- f.close
+ retrbinary("RETR " + remotefile, blocksize) do |data|
+ f.write(data)
+ callback.call(data) if use_callback
end
- end
+ ensure
+ f.close
+ end
+ end
- def gettextfile(remotefile, localfile, callback = nil)
- if iterator?
- callback = Proc.new
- end
- use_callback = callback.is_a?(Proc)
- f = open(localfile, "w")
- begin
- retrlines("RETR " + remotefile) do |line|
- line = line + @return_code
- f.write(line)
- if use_callback
- callback.call(line)
- end
- end
- ensure
- f.close
- end
- end
+ def gettextfile(remotefile, localfile, callback = nil)
+ if iterator?
+ callback = Proc.new
+ end
+ use_callback = callback.is_a?(Proc)
+ f = open(localfile, "w")
+ begin
+ retrlines("RETR " + remotefile) do |line|
+ line = line + @return_code
+ f.write(line)
+ callback.call(line) if use_callback
+ end
+ ensure
+ f.close
+ end
+ end
- def putbinaryfile(localfile, remotefile,
- blocksize, callback = nil)
- if iterator?
- callback = Proc.new
- end
- use_callback = callback.is_a?(Proc)
- f = open(localfile)
- begin
+ def putbinaryfile(localfile, remotefile, blocksize, callback = nil)
+ if iterator?
+ callback = Proc.new
+ end
+ use_callback = callback.is_a?(Proc)
+ f = open(localfile)
+ begin
f.binmode
- storbinary("STOR " + remotefile, f, blocksize) do |data|
- if use_callback
- callback.call(data)
- end
- end
- ensure
- f.close
+ storbinary("STOR " + remotefile, f, blocksize) do |data|
+ callback.call(data) if use_callback
end
- end
-
- def puttextfile(localfile, remotefile, callback = nil)
- if iterator?
- callback = Proc.new
- end
- use_callback = callback.is_a?(Proc)
- f = open(localfile)
- begin
- storlines("STOR " + remotefile, f) do |line|
- if use_callback
- callback.call(line)
- end
- end
- ensure
- f.close
- end
- end
-
- def acct(account)
- cmd = "ACCT " + account
- voidcmd(cmd)
- end
+ ensure
+ f.close
+ end
+ end
+
+ def puttextfile(localfile, remotefile, callback = nil)
+ if iterator?
+ callback = Proc.new
+ end
+ use_callback = callback.is_a?(Proc)
+ f = open(localfile)
+ begin
+ storlines("STOR " + remotefile, f) do |line|
+ callback.call(line) if use_callback
+ end
+ ensure
+ f.close
+ end
+ end
- def nlst(dir = nil)
- cmd = "NLST"
- if dir
- cmd = cmd + " " + dir
- end
- files = []
+ def acct(account)
+ cmd = "ACCT " + account
+ voidcmd(cmd)
+ end
+
+ def nlst(dir = nil)
+ cmd = "NLST"
+ if dir
+ cmd = cmd + " " + dir
+ end
+ files = []
+ retrlines(cmd) do |line|
+ files.push(line)
+ end
+ return files
+ end
+
+ def list(*args, &block)
+ cmd = "LIST"
+ args.each do |arg|
+ cmd = cmd + " " + arg
+ end
+ if block
+ retrlines(cmd, &block)
+ else
+ lines = []
retrlines(cmd) do |line|
- files.push(line)
+ lines << line
end
- files
- end
-
- def list(*args)
- cmd = "LIST"
- if iterator?
- callback = Proc.new
- elsif args[-1].is_a?(Proc)
- callback = args.pop
- else
- callback = nil
- end
- args.each do |arg|
- cmd = cmd + " " + arg
- end
- retrlines(cmd, callback)
- end
- alias ls list
- alias dir list
-
- def rename(fromname, toname)
- resp = sendcmd("RNFR " + fromname)
- if resp[0] != ?3
- raise FTPReplyError, resp
- end
- voidcmd("RNTO " + toname)
- end
-
- def delete(filename)
- resp = sendcmd("DELE " + filename)
- if resp[0, 3] == "250"
- return
- elsif resp[0] == ?5
- raise FTPPermError, resp
- else
- raise FTPReplyError, resp
- end
- end
-
- def chdir(dirname)
- if dirname == ".."
- begin
- voidcmd("CDUP")
- return
- rescue FTPPermError
- if $![0, 3] != "500"
- raise FTPPermError, $!
- end
- end
- end
- cmd = "CWD " + dirname
- voidcmd(cmd)
- end
-
- def size(filename)
- resp = sendcmd("SIZE " + filename)
- if resp[0, 3] == "213"
- return Integer(resp[3 .. -1].strip)
- end
- end
-
- def mkdir(dirname)
- resp = sendcmd("MKD " + dirname)
- return parse257(resp)
- end
-
- def rmdir(dirname)
- voidcmd("RMD " + dirname)
- end
+ return lines
+ end
+ end
+ alias ls list
+ alias dir list
+
+ def rename(fromname, toname)
+ resp = sendcmd("RNFR " + fromname)
+ if resp[0] != ?3
+ raise FTPReplyError, resp
+ end
+ voidcmd("RNTO " + toname)
+ end
+
+ def delete(filename)
+ resp = sendcmd("DELE " + filename)
+ if resp[0, 3] == "250"
+ return
+ elsif resp[0] == ?5
+ raise FTPPermError, resp
+ else
+ raise FTPReplyError, resp
+ end
+ end
+
+ def chdir(dirname)
+ if dirname == ".."
+ begin
+ voidcmd("CDUP")
+ return
+ rescue FTPPermError
+ if $![0, 3] != "500"
+ raise FTPPermError, $!
+ end
+ end
+ end
+ cmd = "CWD " + dirname
+ voidcmd(cmd)
+ end
- def pwd
- resp = sendcmd("PWD")
+ def size(filename)
+ voidcmd("TYPE I")
+ resp = sendcmd("SIZE " + filename)
+ if resp[0, 3] != "213"
+ raise FTPReplyError, resp
+ end
+ return resp[3..-1].strip
+ end
+
+ MDTM_REGEXP = /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/
+
+ def mtime(filename, local = false)
+ str = mdtm(filename)
+ ary = str.scan(MDTM_REGEXP)[0].collect {|i| i.to_i}
+ return local ? Time.local(*ary) : Time.gm(*ary)
+ end
+
+ def mkdir(dirname)
+ resp = sendcmd("MKD " + dirname)
+ return parse257(resp)
+ end
+
+ def rmdir(dirname)
+ voidcmd("RMD " + dirname)
+ end
+
+ def pwd
+ resp = sendcmd("PWD")
return parse257(resp)
- end
- alias getdir pwd
-
- def system
- resp = sendcmd("SYST")
- if resp[0, 3] != "215"
- raise FTPReplyError, resp
- end
- return resp[4 .. -1]
- end
-
- def abort
- line = "ABOR" + CRLF
- resp = ""
- sock_synchronize do
- print "put: ABOR\n" if @debug_mode
- @sock.send(line, Socket::MSG_OOB)
- resp = getmultiline
- end
- unless ["426", "226", "225"].include?(resp[0, 3])
- raise FTPProtoError, resp
- end
- resp
- end
-
- def status
- line = "STAT" + CRLF
- resp = ""
- sock_synchronize do
- print "put: STAT\n" if @debug_mode
- @sock.send(line, Socket::MSG_OOB)
- resp = getresp
- end
- resp
- end
-
- def help(arg = nil)
- cmd = "HELP"
- if arg
- cmd = cmd + " " + arg
- end
- sendcmd(cmd)
- end
-
- def quit
- voidcmd("QUIT")
- end
-
- def close
- @sock.close if @sock and not @sock.closed?
- end
+ end
+ alias getdir pwd
+
+ def system
+ resp = sendcmd("SYST")
+ if resp[0, 3] != "215"
+ raise FTPReplyError, resp
+ end
+ return resp[4 .. -1]
+ end
+
+ def abort
+ line = "ABOR" + CRLF
+ print "put: ABOR\n" if @debug_mode
+ @sock.send(line, Socket::MSG_OOB)
+ resp = getmultiline
+ unless ["426", "226", "225"].include?(resp[0, 3])
+ raise FTPProtoError, resp
+ end
+ return resp
+ end
+ def status
+ line = "STAT" + CRLF
+ print "put: STAT\n" if @debug_mode
+ @sock.send(line, Socket::MSG_OOB)
+ return getresp
+ end
+
+ def mdtm(filename)
+ resp = sendcmd("MDTM " + filename)
+ if resp[0, 3] == "213"
+ return resp[3 .. -1].strip
+ end
+ end
+
+ def help(arg = nil)
+ cmd = "HELP"
+ if arg
+ cmd = cmd + " " + arg
+ end
+ sendcmd(cmd)
+ end
+
+ def quit
+ voidcmd("QUIT")
+ end
+
+ def close
+ @sock.close if @sock and not @sock.closed?
+ end
+
def closed?
@sock == nil or @sock.closed?
end
- def parse227(resp)
- if resp[0, 3] != "227"
- raise FTPReplyError, resp
- end
- left = resp.index("(")
- right = resp.index(")")
- if left == nil or right == nil
- raise FTPProtoError, resp
- end
- numbers = resp[left + 1 .. right - 1].split(",")
- if numbers.length != 6
- raise FTPProtoError, resp
- end
- host = numbers[0, 4].join(".")
- port = (Integer(numbers[4]) << 8) + Integer(numbers[5])
- return host, port
- end
- private :parse227
-
- def parse257(resp)
- if resp[0, 3] != "257"
- raise FTPReplyError, resp
- end
- if resp[3, 2] != ' "'
- return ""
- end
- dirname = ""
- i = 5
- n = resp.length
- while i < n
- c = resp[i, 1]
- i = i + 1
- if c == '"'
- if i > n or resp[i, 1] != '"'
- break
- end
- i = i + 1
- end
- dirname = dirname + c
- end
- return dirname
- end
- private :parse257
+ def parse227(resp)
+ if resp[0, 3] != "227"
+ raise FTPReplyError, resp
+ end
+ left = resp.index("(")
+ right = resp.index(")")
+ if left == nil or right == nil
+ raise FTPProtoError, resp
+ end
+ numbers = resp[left + 1 .. right - 1].split(",")
+ if numbers.length != 6
+ raise FTPProtoError, resp
+ end
+ host = numbers[0, 4].join(".")
+ port = (numbers[4].to_i << 8) + numbers[5].to_i
+ return host, port
+ end
+ private :parse227
+
+ def parse257(resp)
+ if resp[0, 3] != "257"
+ raise FTPReplyError, resp
+ end
+ if resp[3, 2] != ' "'
+ return ""
+ end
+ dirname = ""
+ i = 5
+ n = resp.length
+ while i < n
+ c = resp[i, 1]
+ i = i + 1
+ if c == '"'
+ if i > n or resp[i, 1] != '"'
+ break
+ end
+ i = i + 1
+ end
+ dirname = dirname + c
+ end
+ return dirname
+ end
+ private :parse257
end
+
+## ftplib.rb ends here
diff --git a/lib/getopts.rb b/lib/getopts.rb
index 6929f7e4fd..9e1e8a2cf6 100644
--- a/lib/getopts.rb
+++ b/lib/getopts.rb
@@ -1,4 +1,3 @@
-#!/usr/local/bin/ruby
#
# getopts.rb -
# $Release Version: $
@@ -11,7 +10,7 @@
#
#
-$RCS_ID="$Header$"
+$RCS_ID=%q$Header$
def isSingle(lopt)
if lopt.index(":")
diff --git a/lib/importenv.rb b/lib/importenv.rb
index 41253765ea..10b289199c 100644
--- a/lib/importenv.rb
+++ b/