Initial commit
This commit is contained in:
BIN
ruby/lib/libmsvcrt-ruby210-static.a
Normal file
BIN
ruby/lib/libmsvcrt-ruby210-static.a
Normal file
Binary file not shown.
BIN
ruby/lib/libmsvcrt-ruby210.dll.a
Normal file
BIN
ruby/lib/libmsvcrt-ruby210.dll.a
Normal file
Binary file not shown.
56
ruby/lib/pkgconfig/ruby-2.1.pc
Normal file
56
ruby/lib/pkgconfig/ruby-2.1.pc
Normal file
@@ -0,0 +1,56 @@
|
||||
arch=i386-mingw32
|
||||
sitearch=i386-msvcrt
|
||||
prefix=C:/Users/Luis/Code/oneclick/rubyinstaller/sandbox/ruby21_mingw
|
||||
exec_prefix=${prefix}
|
||||
bindir=${exec_prefix}/bin
|
||||
libdir=${exec_prefix}/lib
|
||||
includedir=${prefix}/include
|
||||
MAJOR=2
|
||||
MINOR=1
|
||||
TEENY=0
|
||||
ruby_version=2.1.0
|
||||
RUBY_PROGRAM_VERSION=2.1.3
|
||||
RUBY_BASE_NAME=ruby
|
||||
RUBY_VERSION_NAME=${RUBY_BASE_NAME}-${ruby_version}
|
||||
RUBY_SO_NAME=msvcrt-${RUBY_BASE_NAME}${MAJOR}${MINOR}${TEENY}
|
||||
RUBY_INSTALL_NAME=${RUBY_BASE_NAME}
|
||||
DEFFILE=
|
||||
archlibdir=${libdir}/${arch}
|
||||
sitearchlibdir=${libdir}/${sitearch}
|
||||
archincludedir=${includedir}/${arch}
|
||||
sitearchincludedir=${includedir}/${sitearch}
|
||||
ruby=${bindir}/${RUBY_INSTALL_NAME}.exe
|
||||
rubylibprefix=${libdir}/${RUBY_BASE_NAME}
|
||||
rubyarchprefix=${rubylibprefix}/${arch}
|
||||
rubysitearchprefix=${rubylibprefix}/${sitearch}
|
||||
rubylibdir=${rubylibprefix}/${ruby_version}
|
||||
vendordir=${rubylibprefix}/vendor_ruby
|
||||
sitedir=${rubylibprefix}/site_ruby
|
||||
vendorlibdir=${vendordir}/${ruby_version}
|
||||
sitelibdir=${sitedir}/${ruby_version}
|
||||
rubyarchdir=${rubylibdir}/${arch}
|
||||
vendorarchdir=${vendorlibdir}/${sitearch}
|
||||
sitearchdir=${sitelibdir}/${sitearch}
|
||||
rubyhdrdir=${includedir}/${RUBY_VERSION_NAME}
|
||||
vendorhdrdir=${rubyhdrdir}/vendor_ruby
|
||||
sitehdrdir=${rubyhdrdir}/site_ruby
|
||||
rubyarchhdrdir=${rubyhdrdir}/${arch}
|
||||
vendorarchhdrdir=${vendorhdrdir}/${sitearch}
|
||||
sitearchhdrdir=${sitehdrdir}/${sitearch}
|
||||
LIBPATH=
|
||||
LIBRUBY_A=lib${RUBY_SO_NAME}-static.a
|
||||
LIBRUBY_SO=${RUBY_SO_NAME}.dll
|
||||
LIBRUBY=lib${RUBY_SO_NAME}.dll.a
|
||||
LIBRUBYARG_SHARED=-l${RUBY_SO_NAME}
|
||||
LIBRUBYARG_STATIC=-l${RUBY_SO_NAME}-static
|
||||
LIBRUBYARG=${LIBRUBYARG_SHARED}
|
||||
LIBS=-lshell32 -lws2_32 -liphlpapi -limagehlp -lshlwapi
|
||||
DLDFLAGS= -Wl,--enable-auto-image-base,--enable-auto-import ${DEFFILE}
|
||||
|
||||
Name: Ruby
|
||||
Description: Object Oriented Script Language
|
||||
Version: ${ruby_version}
|
||||
URL: http://www.ruby-lang.org
|
||||
Cflags: -I${rubyarchhdrdir} -I${rubyhdrdir}
|
||||
Libs: ${DLDFLAGS} ${LIBRUBYARG_SHARED} ${LIBS}
|
||||
Requires:
|
||||
185
ruby/lib/ruby/2.1.0/English.rb
Normal file
185
ruby/lib/ruby/2.1.0/English.rb
Normal file
@@ -0,0 +1,185 @@
|
||||
# Include the English library file in a Ruby script, and you can
|
||||
# reference the global variables such as \VAR{\$\_} using less
|
||||
# cryptic names, listed in the following table.% \vref{tab:english}.
|
||||
#
|
||||
# Without 'English':
|
||||
#
|
||||
# $\ = ' -- '
|
||||
# "waterbuffalo" =~ /buff/
|
||||
# print $", $', $$, "\n"
|
||||
#
|
||||
# With English:
|
||||
#
|
||||
# require "English"
|
||||
#
|
||||
# $OUTPUT_FIELD_SEPARATOR = ' -- '
|
||||
# "waterbuffalo" =~ /buff/
|
||||
# print $LOADED_FEATURES, $POSTMATCH, $PID, "\n"
|
||||
#
|
||||
# Below is a full list of descriptive aliases and their associated global
|
||||
# variable:
|
||||
#
|
||||
# $ERROR_INFO:: $!
|
||||
# $ERROR_POSITION:: $@
|
||||
# $FS:: $;
|
||||
# $FIELD_SEPARATOR:: $;
|
||||
# $OFS:: $,
|
||||
# $OUTPUT_FIELD_SEPARATOR:: $,
|
||||
# $RS:: $/
|
||||
# $INPUT_RECORD_SEPARATOR:: $/
|
||||
# $ORS:: $\
|
||||
# $OUTPUT_RECORD_SEPARATOR:: $\
|
||||
# $INPUT_LINE_NUMBER:: $.
|
||||
# $NR:: $.
|
||||
# $LAST_READ_LINE:: $_
|
||||
# $DEFAULT_OUTPUT:: $>
|
||||
# $DEFAULT_INPUT:: $<
|
||||
# $PID:: $$
|
||||
# $PROCESS_ID:: $$
|
||||
# $CHILD_STATUS:: $?
|
||||
# $LAST_MATCH_INFO:: $~
|
||||
# $IGNORECASE:: $=
|
||||
# $ARGV:: $*
|
||||
# $MATCH:: $&
|
||||
# $PREMATCH:: $`
|
||||
# $POSTMATCH:: $'
|
||||
# $LAST_PAREN_MATCH:: $+
|
||||
#
|
||||
module English end if false
|
||||
|
||||
# The exception object passed to +raise+.
|
||||
alias $ERROR_INFO $!
|
||||
|
||||
# The stack backtrace generated by the last
|
||||
# exception. <tt>See Kernel.caller</tt> for details. Thread local.
|
||||
alias $ERROR_POSITION $@
|
||||
|
||||
# The default separator pattern used by <tt>String.split</tt>. May be
|
||||
# set from the command line using the <tt>-F</tt> flag.
|
||||
alias $FS $;
|
||||
|
||||
# The default separator pattern used by <tt>String.split</tt>. May be
|
||||
# set from the command line using the <tt>-F</tt> flag.
|
||||
alias $FIELD_SEPARATOR $;
|
||||
|
||||
# The separator string output between the parameters to methods such
|
||||
# as <tt>Kernel.print</tt> and <tt>Array.join</tt>. Defaults to +nil+,
|
||||
# which adds no text.
|
||||
alias $OFS $,
|
||||
|
||||
# The separator string output between the parameters to methods such
|
||||
# as <tt>Kernel.print</tt> and <tt>Array.join</tt>. Defaults to +nil+,
|
||||
# which adds no text.
|
||||
alias $OUTPUT_FIELD_SEPARATOR $,
|
||||
|
||||
# The input record separator (newline by default). This is the value
|
||||
# that routines such as <tt>Kernel.gets</tt> use to determine record
|
||||
# boundaries. If set to +nil+, +gets+ will read the entire file.
|
||||
alias $RS $/
|
||||
|
||||
# The input record separator (newline by default). This is the value
|
||||
# that routines such as <tt>Kernel.gets</tt> use to determine record
|
||||
# boundaries. If set to +nil+, +gets+ will read the entire file.
|
||||
alias $INPUT_RECORD_SEPARATOR $/
|
||||
|
||||
# The string appended to the output of every call to methods such as
|
||||
# <tt>Kernel.print</tt> and <tt>IO.write</tt>. The default value is
|
||||
# +nil+.
|
||||
alias $ORS $\
|
||||
|
||||
# The string appended to the output of every call to methods such as
|
||||
# <tt>Kernel.print</tt> and <tt>IO.write</tt>. The default value is
|
||||
# +nil+.
|
||||
alias $OUTPUT_RECORD_SEPARATOR $\
|
||||
|
||||
# The number of the last line read from the current input file.
|
||||
alias $INPUT_LINE_NUMBER $.
|
||||
|
||||
# The number of the last line read from the current input file.
|
||||
alias $NR $.
|
||||
|
||||
# The last line read by <tt>Kernel.gets</tt> or
|
||||
# <tt>Kernel.readline</tt>. Many string-related functions in the
|
||||
# +Kernel+ module operate on <tt>$_</tt> by default. The variable is
|
||||
# local to the current scope. Thread local.
|
||||
alias $LAST_READ_LINE $_
|
||||
|
||||
# The destination of output for <tt>Kernel.print</tt>
|
||||
# and <tt>Kernel.printf</tt>. The default value is
|
||||
# <tt>$stdout</tt>.
|
||||
alias $DEFAULT_OUTPUT $>
|
||||
|
||||
# An object that provides access to the concatenation
|
||||
# of the contents of all the files
|
||||
# given as command-line arguments, or <tt>$stdin</tt>
|
||||
# (in the case where there are no
|
||||
# arguments). <tt>$<</tt> supports methods similar to a
|
||||
# +File+ object:
|
||||
# +inmode+, +close+,
|
||||
# <tt>closed?</tt>, +each+,
|
||||
# <tt>each_byte</tt>, <tt>each_line</tt>,
|
||||
# +eof+, <tt>eof?</tt>, +file+,
|
||||
# +filename+, +fileno+,
|
||||
# +getc+, +gets+, +lineno+,
|
||||
# <tt>lineno=</tt>, +path+,
|
||||
# +pos+, <tt>pos=</tt>,
|
||||
# +read+, +readchar+,
|
||||
# +readline+, +readlines+,
|
||||
# +rewind+, +seek+, +skip+,
|
||||
# +tell+, <tt>to_a</tt>, <tt>to_i</tt>,
|
||||
# <tt>to_io</tt>, <tt>to_s</tt>, along with the
|
||||
# methods in +Enumerable+. The method +file+
|
||||
# returns a +File+ object for the file currently
|
||||
# being read. This may change as <tt>$<</tt> reads
|
||||
# through the files on the command line. Read only.
|
||||
alias $DEFAULT_INPUT $<
|
||||
|
||||
# The process number of the program being executed. Read only.
|
||||
alias $PID $$
|
||||
|
||||
# The process number of the program being executed. Read only.
|
||||
alias $PROCESS_ID $$
|
||||
|
||||
# The exit status of the last child process to terminate. Read
|
||||
# only. Thread local.
|
||||
alias $CHILD_STATUS $?
|
||||
|
||||
# A +MatchData+ object that encapsulates the results of a successful
|
||||
# pattern match. The variables <tt>$&</tt>, <tt>$`</tt>, <tt>$'</tt>,
|
||||
# and <tt>$1</tt> to <tt>$9</tt> are all derived from
|
||||
# <tt>$~</tt>. Assigning to <tt>$~</tt> changes the values of these
|
||||
# derived variables. This variable is local to the current
|
||||
# scope.
|
||||
alias $LAST_MATCH_INFO $~
|
||||
|
||||
# If set to any value apart from +nil+ or +false+, all pattern matches
|
||||
# will be case insensitive, string comparisons will ignore case, and
|
||||
# string hash values will be case insensitive. Deprecated
|
||||
alias $IGNORECASE $=
|
||||
|
||||
# An array of strings containing the command-line
|
||||
# options from the invocation of the program. Options
|
||||
# used by the Ruby interpreter will have been
|
||||
# removed. Read only. Also known simply as +ARGV+.
|
||||
alias $ARGV $*
|
||||
|
||||
# The string matched by the last successful pattern
|
||||
# match. This variable is local to the current
|
||||
# scope. Read only.
|
||||
alias $MATCH $&
|
||||
|
||||
# The string preceding the match in the last
|
||||
# successful pattern match. This variable is local to
|
||||
# the current scope. Read only.
|
||||
alias $PREMATCH $`
|
||||
|
||||
# The string following the match in the last
|
||||
# successful pattern match. This variable is local to
|
||||
# the current scope. Read only.
|
||||
alias $POSTMATCH $'
|
||||
|
||||
# The contents of the highest-numbered group matched in the last
|
||||
# successful pattern match. Thus, in <tt>"cat" =~ /(c|a)(t|z)/</tt>,
|
||||
# <tt>$+</tt> will be set to "t". This variable is local to the
|
||||
# current scope. Read only.
|
||||
alias $LAST_PAREN_MATCH $+
|
||||
31
ruby/lib/ruby/2.1.0/Win32API.rb
Normal file
31
ruby/lib/ruby/2.1.0/Win32API.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
# -*- ruby -*-
|
||||
# for backward compatibility
|
||||
warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: Win32API is deprecated after Ruby 1.9.1; use dl directly instead" if $VERBOSE
|
||||
|
||||
require 'dl'
|
||||
|
||||
class Win32API
|
||||
DLL = {}
|
||||
TYPEMAP = {"0" => DL::TYPE_VOID, "S" => DL::TYPE_VOIDP, "I" => DL::TYPE_LONG}
|
||||
POINTER_TYPE = DL::SIZEOF_VOIDP == DL::SIZEOF_LONG_LONG ? 'q*' : 'l!*'
|
||||
|
||||
def initialize(dllname, func, import, export = "0", calltype = :stdcall)
|
||||
@proto = [import].join.tr("VPpNnLlIi", "0SSI").sub(/^(.)0*$/, '\1')
|
||||
handle = DLL[dllname] ||= DL.dlopen(dllname)
|
||||
@func = DL::CFunc.new(handle[func], TYPEMAP[export.tr("VPpNnLlIi", "0SSI")], func, calltype)
|
||||
rescue DL::DLError => e
|
||||
raise LoadError, e.message, e.backtrace
|
||||
end
|
||||
|
||||
def call(*args)
|
||||
import = @proto.split("")
|
||||
args.each_with_index do |x, i|
|
||||
args[i], = [x == 0 ? nil : x].pack("p").unpack(POINTER_TYPE) if import[i] == "S"
|
||||
args[i], = [x].pack("I").unpack("i") if import[i] == "I"
|
||||
end
|
||||
ret, = @func.call(args)
|
||||
return ret || 0
|
||||
end
|
||||
|
||||
alias Call call
|
||||
end
|
||||
136
ruby/lib/ruby/2.1.0/abbrev.rb
Normal file
136
ruby/lib/ruby/2.1.0/abbrev.rb
Normal file
@@ -0,0 +1,136 @@
|
||||
#!/usr/bin/env ruby
|
||||
#--
|
||||
# Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
|
||||
#
|
||||
# All rights reserved. You can redistribute and/or modify it under
|
||||
# the same terms as Ruby.
|
||||
#
|
||||
# $Idaemons: /home/cvs/rb/abbrev.rb,v 1.2 2001/05/30 09:37:45 knu Exp $
|
||||
# $RoughId: abbrev.rb,v 1.4 2003/10/14 19:45:42 knu Exp $
|
||||
# $Id: abbrev.rb 39362 2013-02-21 17:35:32Z zzak $
|
||||
#++
|
||||
|
||||
##
|
||||
# Calculates the set of unique abbreviations for a given set of strings.
|
||||
#
|
||||
# require 'abbrev'
|
||||
# require 'pp'
|
||||
#
|
||||
# pp Abbrev.abbrev(['ruby', 'rules'])
|
||||
#
|
||||
# Generates:
|
||||
#
|
||||
# { "rub" => "ruby",
|
||||
# "ruby" => "ruby",
|
||||
# "rul" => "rules",
|
||||
# "rule" => "rules",
|
||||
# "rules" => "rules" }
|
||||
#
|
||||
# It also provides an array core extension, Array#abbrev.
|
||||
#
|
||||
# pp %w{summer winter}.abbrev
|
||||
# #=> {"summe"=>"summer",
|
||||
# "summ"=>"summer",
|
||||
# "sum"=>"summer",
|
||||
# "su"=>"summer",
|
||||
# "s"=>"summer",
|
||||
# "winte"=>"winter",
|
||||
# "wint"=>"winter",
|
||||
# "win"=>"winter",
|
||||
# "wi"=>"winter",
|
||||
# "w"=>"winter",
|
||||
# "summer"=>"summer",
|
||||
# "winter"=>"winter"}
|
||||
|
||||
module Abbrev
|
||||
|
||||
# Given a set of strings, calculate the set of unambiguous
|
||||
# abbreviations for those strings, and return a hash where the keys
|
||||
# are all the possible abbreviations and the values are the full
|
||||
# strings.
|
||||
#
|
||||
# Thus, given +words+ is "car" and "cone", the keys pointing to "car" would
|
||||
# be "ca" and "car", while those pointing to "cone" would be "co", "con", and
|
||||
# "cone".
|
||||
#
|
||||
# require 'abbrev'
|
||||
#
|
||||
# Abbrev.abbrev(['car', 'cone'])
|
||||
# #=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"}
|
||||
#
|
||||
# The optional +pattern+ parameter is a pattern or a string. Only
|
||||
# input strings that match the pattern or start with the string
|
||||
# are included in the output hash.
|
||||
#
|
||||
# Abbrev.abbrev(%w{car box cone}, /b/)
|
||||
# #=> {"bo"=>"box", "b"=>"box", "box"=>"box"}
|
||||
def abbrev(words, pattern = nil)
|
||||
table = {}
|
||||
seen = Hash.new(0)
|
||||
|
||||
if pattern.is_a?(String)
|
||||
pattern = /\A#{Regexp.quote(pattern)}/ # regard as a prefix
|
||||
end
|
||||
|
||||
words.each do |word|
|
||||
next if word.empty?
|
||||
word.size.downto(1) { |len|
|
||||
abbrev = word[0...len]
|
||||
|
||||
next if pattern && pattern !~ abbrev
|
||||
|
||||
case seen[abbrev] += 1
|
||||
when 1
|
||||
table[abbrev] = word
|
||||
when 2
|
||||
table.delete(abbrev)
|
||||
else
|
||||
break
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
words.each do |word|
|
||||
next if pattern && pattern !~ word
|
||||
|
||||
table[word] = word
|
||||
end
|
||||
|
||||
table
|
||||
end
|
||||
|
||||
module_function :abbrev
|
||||
end
|
||||
|
||||
class Array
|
||||
# Calculates the set of unambiguous abbreviations for the strings in
|
||||
# +self+.
|
||||
#
|
||||
# require 'abbrev'
|
||||
# %w{ car cone }.abbrev
|
||||
# #=> {"ca" => "car", "con"=>"cone", "co" => "cone",
|
||||
# "car"=>"car", "cone" => "cone"}
|
||||
#
|
||||
# The optional +pattern+ parameter is a pattern or a string. Only
|
||||
# input strings that match the pattern or start with the string
|
||||
# are included in the output hash.
|
||||
#
|
||||
# %w{ fast boat day }.abbrev(/^.a/)
|
||||
# #=> {"fas"=>"fast", "fa"=>"fast", "da"=>"day",
|
||||
# "fast"=>"fast", "day"=>"day"}
|
||||
#
|
||||
# See also Abbrev.abbrev
|
||||
def abbrev(pattern = nil)
|
||||
Abbrev::abbrev(self, pattern)
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
while line = gets
|
||||
hash = line.split.abbrev
|
||||
|
||||
hash.sort.each do |k, v|
|
||||
puts "#{k} => #{v}"
|
||||
end
|
||||
end
|
||||
end
|
||||
91
ruby/lib/ruby/2.1.0/base64.rb
Normal file
91
ruby/lib/ruby/2.1.0/base64.rb
Normal file
@@ -0,0 +1,91 @@
|
||||
#
|
||||
# = base64.rb: methods for base64-encoding and -decoding strings
|
||||
#
|
||||
|
||||
# The Base64 module provides for the encoding (#encode64, #strict_encode64,
|
||||
# #urlsafe_encode64) and decoding (#decode64, #strict_decode64,
|
||||
# #urlsafe_decode64) of binary data using a Base64 representation.
|
||||
#
|
||||
# == Example
|
||||
#
|
||||
# A simple encoding and decoding.
|
||||
#
|
||||
# require "base64"
|
||||
#
|
||||
# enc = Base64.encode64('Send reinforcements')
|
||||
# # -> "U2VuZCByZWluZm9yY2VtZW50cw==\n"
|
||||
# plain = Base64.decode64(enc)
|
||||
# # -> "Send reinforcements"
|
||||
#
|
||||
# The purpose of using base64 to encode data is that it translates any
|
||||
# binary data into purely printable characters.
|
||||
|
||||
module Base64
|
||||
module_function
|
||||
|
||||
# Returns the Base64-encoded version of +bin+.
|
||||
# This method complies with RFC 2045.
|
||||
# Line feeds are added to every 60 encoded characters.
|
||||
#
|
||||
# require 'base64'
|
||||
# Base64.encode64("Now is the time for all good coders\nto learn Ruby")
|
||||
#
|
||||
# <i>Generates:</i>
|
||||
#
|
||||
# Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g
|
||||
# UnVieQ==
|
||||
def encode64(bin)
|
||||
[bin].pack("m")
|
||||
end
|
||||
|
||||
# Returns the Base64-decoded version of +str+.
|
||||
# This method complies with RFC 2045.
|
||||
# Characters outside the base alphabet are ignored.
|
||||
#
|
||||
# require 'base64'
|
||||
# str = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' +
|
||||
# 'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' +
|
||||
# 'ZSB0aHJlZQpBbmQgc28gb24uLi4K'
|
||||
# puts Base64.decode64(str)
|
||||
#
|
||||
# <i>Generates:</i>
|
||||
#
|
||||
# This is line one
|
||||
# This is line two
|
||||
# This is line three
|
||||
# And so on...
|
||||
def decode64(str)
|
||||
str.unpack("m").first
|
||||
end
|
||||
|
||||
# Returns the Base64-encoded version of +bin+.
|
||||
# This method complies with RFC 4648.
|
||||
# No line feeds are added.
|
||||
def strict_encode64(bin)
|
||||
[bin].pack("m0")
|
||||
end
|
||||
|
||||
# Returns the Base64-decoded version of +str+.
|
||||
# This method complies with RFC 4648.
|
||||
# ArgumentError is raised if +str+ is incorrectly padded or contains
|
||||
# non-alphabet characters. Note that CR or LF are also rejected.
|
||||
def strict_decode64(str)
|
||||
str.unpack("m0").first
|
||||
end
|
||||
|
||||
# Returns the Base64-encoded version of +bin+.
|
||||
# This method complies with ``Base 64 Encoding with URL and Filename Safe
|
||||
# Alphabet'' in RFC 4648.
|
||||
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
|
||||
def urlsafe_encode64(bin)
|
||||
strict_encode64(bin).tr("+/", "-_")
|
||||
end
|
||||
|
||||
# Returns the Base64-decoded version of +str+.
|
||||
# This method complies with ``Base 64 Encoding with URL and Filename Safe
|
||||
# Alphabet'' in RFC 4648.
|
||||
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
|
||||
def urlsafe_decode64(str)
|
||||
strict_decode64(str.tr("-_", "+/"))
|
||||
end
|
||||
end
|
||||
568
ruby/lib/ruby/2.1.0/benchmark.rb
Normal file
568
ruby/lib/ruby/2.1.0/benchmark.rb
Normal file
@@ -0,0 +1,568 @@
|
||||
#--
|
||||
# benchmark.rb - a performance benchmarking library
|
||||
#
|
||||
# $Id: benchmark.rb 43002 2013-09-20 16:05:48Z zzak $
|
||||
#
|
||||
# Created by Gotoken (gotoken@notwork.org).
|
||||
#
|
||||
# Documentation by Gotoken (original RD), Lyle Johnson (RDoc conversion), and
|
||||
# Gavin Sinclair (editing).
|
||||
#++
|
||||
#
|
||||
# == Overview
|
||||
#
|
||||
# The Benchmark module provides methods for benchmarking Ruby code, giving
|
||||
# detailed reports on the time taken for each task.
|
||||
#
|
||||
|
||||
# The Benchmark module provides methods to measure and report the time
|
||||
# used to execute Ruby code.
|
||||
#
|
||||
# * Measure the time to construct the string given by the expression
|
||||
# <code>"a"*1_000_000_000</code>:
|
||||
#
|
||||
# require 'benchmark'
|
||||
#
|
||||
# puts Benchmark.measure { "a"*1_000_000_000 }
|
||||
#
|
||||
# On my machine (OSX 10.8.3 on i5 1.7 Ghz) this generates:
|
||||
#
|
||||
# 0.350000 0.400000 0.750000 ( 0.835234)
|
||||
#
|
||||
# This report shows the user CPU time, system CPU time, the sum of
|
||||
# the user and system CPU times, and the elapsed real time. The unit
|
||||
# of time is seconds.
|
||||
#
|
||||
# * Do some experiments sequentially using the #bm method:
|
||||
#
|
||||
# require 'benchmark'
|
||||
#
|
||||
# n = 5000000
|
||||
# Benchmark.bm do |x|
|
||||
# x.report { for i in 1..n; a = "1"; end }
|
||||
# x.report { n.times do ; a = "1"; end }
|
||||
# x.report { 1.upto(n) do ; a = "1"; end }
|
||||
# end
|
||||
#
|
||||
# The result:
|
||||
#
|
||||
# user system total real
|
||||
# 1.010000 0.000000 1.010000 ( 1.014479)
|
||||
# 1.000000 0.000000 1.000000 ( 0.998261)
|
||||
# 0.980000 0.000000 0.980000 ( 0.981335)
|
||||
#
|
||||
# * Continuing the previous example, put a label in each report:
|
||||
#
|
||||
# require 'benchmark'
|
||||
#
|
||||
# n = 5000000
|
||||
# Benchmark.bm(7) do |x|
|
||||
# x.report("for:") { for i in 1..n; a = "1"; end }
|
||||
# x.report("times:") { n.times do ; a = "1"; end }
|
||||
# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
||||
# end
|
||||
#
|
||||
# The result:
|
||||
#
|
||||
# user system total real
|
||||
# for: 1.010000 0.000000 1.010000 ( 1.015688)
|
||||
# times: 1.000000 0.000000 1.000000 ( 1.003611)
|
||||
# upto: 1.030000 0.000000 1.030000 ( 1.028098)
|
||||
#
|
||||
# * The times for some benchmarks depend on the order in which items
|
||||
# are run. These differences are due to the cost of memory
|
||||
# allocation and garbage collection. To avoid these discrepancies,
|
||||
# the #bmbm method is provided. For example, to compare ways to
|
||||
# sort an array of floats:
|
||||
#
|
||||
# require 'benchmark'
|
||||
#
|
||||
# array = (1..1000000).map { rand }
|
||||
#
|
||||
# Benchmark.bmbm do |x|
|
||||
# x.report("sort!") { array.dup.sort! }
|
||||
# x.report("sort") { array.dup.sort }
|
||||
# end
|
||||
#
|
||||
# The result:
|
||||
#
|
||||
# Rehearsal -----------------------------------------
|
||||
# sort! 1.490000 0.010000 1.500000 ( 1.490520)
|
||||
# sort 1.460000 0.000000 1.460000 ( 1.463025)
|
||||
# -------------------------------- total: 2.960000sec
|
||||
#
|
||||
# user system total real
|
||||
# sort! 1.460000 0.000000 1.460000 ( 1.460465)
|
||||
# sort 1.450000 0.010000 1.460000 ( 1.448327)
|
||||
#
|
||||
# * Report statistics of sequential experiments with unique labels,
|
||||
# using the #benchmark method:
|
||||
#
|
||||
# require 'benchmark'
|
||||
# include Benchmark # we need the CAPTION and FORMAT constants
|
||||
#
|
||||
# n = 5000000
|
||||
# Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
|
||||
# tf = x.report("for:") { for i in 1..n; a = "1"; end }
|
||||
# tt = x.report("times:") { n.times do ; a = "1"; end }
|
||||
# tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
||||
# [tf+tt+tu, (tf+tt+tu)/3]
|
||||
# end
|
||||
#
|
||||
# The result:
|
||||
#
|
||||
# user system total real
|
||||
# for: 0.950000 0.000000 0.950000 ( 0.952039)
|
||||
# times: 0.980000 0.000000 0.980000 ( 0.984938)
|
||||
# upto: 0.950000 0.000000 0.950000 ( 0.946787)
|
||||
# >total: 2.880000 0.000000 2.880000 ( 2.883764)
|
||||
# >avg: 0.960000 0.000000 0.960000 ( 0.961255)
|
||||
|
||||
module Benchmark
|
||||
|
||||
BENCHMARK_VERSION = "2002-04-25" # :nodoc:
|
||||
|
||||
# Invokes the block with a Benchmark::Report object, which
|
||||
# may be used to collect and report on the results of individual
|
||||
# benchmark tests. Reserves +label_width+ leading spaces for
|
||||
# labels on each line. Prints +caption+ at the top of the
|
||||
# report, and uses +format+ to format each line.
|
||||
# Returns an array of Benchmark::Tms objects.
|
||||
#
|
||||
# If the block returns an array of
|
||||
# Benchmark::Tms objects, these will be used to format
|
||||
# additional lines of output. If +label+ parameters are
|
||||
# given, these are used to label these extra lines.
|
||||
#
|
||||
# _Note_: Other methods provide a simpler interface to this one, and are
|
||||
# suitable for nearly all benchmarking requirements. See the examples in
|
||||
# Benchmark, and the #bm and #bmbm methods.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# require 'benchmark'
|
||||
# include Benchmark # we need the CAPTION and FORMAT constants
|
||||
#
|
||||
# n = 5000000
|
||||
# Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
|
||||
# tf = x.report("for:") { for i in 1..n; a = "1"; end }
|
||||
# tt = x.report("times:") { n.times do ; a = "1"; end }
|
||||
# tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
||||
# [tf+tt+tu, (tf+tt+tu)/3]
|
||||
# end
|
||||
#
|
||||
# Generates:
|
||||
#
|
||||
# user system total real
|
||||
# for: 0.970000 0.000000 0.970000 ( 0.970493)
|
||||
# times: 0.990000 0.000000 0.990000 ( 0.989542)
|
||||
# upto: 0.970000 0.000000 0.970000 ( 0.972854)
|
||||
# >total: 2.930000 0.000000 2.930000 ( 2.932889)
|
||||
# >avg: 0.976667 0.000000 0.976667 ( 0.977630)
|
||||
#
|
||||
|
||||
def benchmark(caption = "", label_width = nil, format = nil, *labels) # :yield: report
|
||||
sync = STDOUT.sync
|
||||
STDOUT.sync = true
|
||||
label_width ||= 0
|
||||
label_width += 1
|
||||
format ||= FORMAT
|
||||
print ' '*label_width + caption unless caption.empty?
|
||||
report = Report.new(label_width, format)
|
||||
results = yield(report)
|
||||
Array === results and results.grep(Tms).each {|t|
|
||||
print((labels.shift || t.label || "").ljust(label_width), t.format(format))
|
||||
}
|
||||
report.list
|
||||
ensure
|
||||
STDOUT.sync = sync unless sync.nil?
|
||||
end
|
||||
|
||||
|
||||
# A simple interface to the #benchmark method, #bm generates sequential
|
||||
# reports with labels. The parameters have the same meaning as for
|
||||
# #benchmark.
|
||||
#
|
||||
# require 'benchmark'
|
||||
#
|
||||
# n = 5000000
|
||||
# Benchmark.bm(7) do |x|
|
||||
# x.report("for:") { for i in 1..n; a = "1"; end }
|
||||
# x.report("times:") { n.times do ; a = "1"; end }
|
||||
# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
||||
# end
|
||||
#
|
||||
# Generates:
|
||||
#
|
||||
# user system total real
|
||||
# for: 0.960000 0.000000 0.960000 ( 0.957966)
|
||||
# times: 0.960000 0.000000 0.960000 ( 0.960423)
|
||||
# upto: 0.950000 0.000000 0.950000 ( 0.954864)
|
||||
#
|
||||
|
||||
def bm(label_width = 0, *labels, &blk) # :yield: report
|
||||
benchmark(CAPTION, label_width, FORMAT, *labels, &blk)
|
||||
end
|
||||
|
||||
|
||||
# Sometimes benchmark results are skewed because code executed
|
||||
# earlier encounters different garbage collection overheads than
|
||||
# that run later. #bmbm attempts to minimize this effect by running
|
||||
# the tests twice, the first time as a rehearsal in order to get the
|
||||
# runtime environment stable, the second time for
|
||||
# real. GC.start is executed before the start of each of
|
||||
# the real timings; the cost of this is not included in the
|
||||
# timings. In reality, though, there's only so much that #bmbm can
|
||||
# do, and the results are not guaranteed to be isolated from garbage
|
||||
# collection and other effects.
|
||||
#
|
||||
# Because #bmbm takes two passes through the tests, it can
|
||||
# calculate the required label width.
|
||||
#
|
||||
# require 'benchmark'
|
||||
#
|
||||
# array = (1..1000000).map { rand }
|
||||
#
|
||||
# Benchmark.bmbm do |x|
|
||||
# x.report("sort!") { array.dup.sort! }
|
||||
# x.report("sort") { array.dup.sort }
|
||||
# end
|
||||
#
|
||||
# Generates:
|
||||
#
|
||||
# Rehearsal -----------------------------------------
|
||||
# sort! 1.440000 0.010000 1.450000 ( 1.446833)
|
||||
# sort 1.440000 0.000000 1.440000 ( 1.448257)
|
||||
# -------------------------------- total: 2.890000sec
|
||||
#
|
||||
# user system total real
|
||||
# sort! 1.460000 0.000000 1.460000 ( 1.458065)
|
||||
# sort 1.450000 0.000000 1.450000 ( 1.455963)
|
||||
#
|
||||
# #bmbm yields a Benchmark::Job object and returns an array of
|
||||
# Benchmark::Tms objects.
|
||||
#
|
||||
def bmbm(width = 0) # :yield: job
|
||||
job = Job.new(width)
|
||||
yield(job)
|
||||
width = job.width + 1
|
||||
sync = STDOUT.sync
|
||||
STDOUT.sync = true
|
||||
|
||||
# rehearsal
|
||||
puts 'Rehearsal '.ljust(width+CAPTION.length,'-')
|
||||
ets = job.list.inject(Tms.new) { |sum,(label,item)|
|
||||
print label.ljust(width)
|
||||
res = Benchmark.measure(&item)
|
||||
print res.format
|
||||
sum + res
|
||||
}.format("total: %tsec")
|
||||
print " #{ets}\n\n".rjust(width+CAPTION.length+2,'-')
|
||||
|
||||
# take
|
||||
print ' '*width + CAPTION
|
||||
job.list.map { |label,item|
|
||||
GC.start
|
||||
print label.ljust(width)
|
||||
Benchmark.measure(label, &item).tap { |res| print res }
|
||||
}
|
||||
ensure
|
||||
STDOUT.sync = sync unless sync.nil?
|
||||
end
|
||||
|
||||
#
|
||||
# Returns the time used to execute the given block as a
|
||||
# Benchmark::Tms object.
|
||||
#
|
||||
def measure(label = "") # :yield:
|
||||
t0, r0 = Process.times, Time.now
|
||||
yield
|
||||
t1, r1 = Process.times, Time.now
|
||||
Benchmark::Tms.new(t1.utime - t0.utime,
|
||||
t1.stime - t0.stime,
|
||||
t1.cutime - t0.cutime,
|
||||
t1.cstime - t0.cstime,
|
||||
r1 - r0,
|
||||
label)
|
||||
end
|
||||
|
||||
#
|
||||
# Returns the elapsed real time used to execute the given block.
|
||||
#
|
||||
def realtime # :yield:
|
||||
r0 = Time.now
|
||||
yield
|
||||
Time.now - r0
|
||||
end
|
||||
|
||||
module_function :benchmark, :measure, :realtime, :bm, :bmbm
|
||||
|
||||
#
|
||||
# A Job is a sequence of labelled blocks to be processed by the
|
||||
# Benchmark.bmbm method. It is of little direct interest to the user.
|
||||
#
|
||||
class Job # :nodoc:
|
||||
#
|
||||
# Returns an initialized Job instance.
|
||||
# Usually, one doesn't call this method directly, as new
|
||||
# Job objects are created by the #bmbm method.
|
||||
# +width+ is a initial value for the label offset used in formatting;
|
||||
# the #bmbm method passes its +width+ argument to this constructor.
|
||||
#
|
||||
def initialize(width)
|
||||
@width = width
|
||||
@list = []
|
||||
end
|
||||
|
||||
#
|
||||
# Registers the given label and block pair in the job list.
|
||||
#
|
||||
def item(label = "", &blk) # :yield:
|
||||
raise ArgumentError, "no block" unless block_given?
|
||||
label = label.to_s
|
||||
w = label.length
|
||||
@width = w if @width < w
|
||||
@list << [label, blk]
|
||||
self
|
||||
end
|
||||
|
||||
alias report item
|
||||
|
||||
# An array of 2-element arrays, consisting of label and block pairs.
|
||||
attr_reader :list
|
||||
|
||||
# Length of the widest label in the #list.
|
||||
attr_reader :width
|
||||
end
|
||||
|
||||
#
|
||||
# This class is used by the Benchmark.benchmark and Benchmark.bm methods.
|
||||
# It is of little direct interest to the user.
|
||||
#
|
||||
class Report # :nodoc:
|
||||
#
|
||||
# Returns an initialized Report instance.
|
||||
# Usually, one doesn't call this method directly, as new
|
||||
# Report objects are created by the #benchmark and #bm methods.
|
||||
# +width+ and +format+ are the label offset and
|
||||
# format string used by Tms#format.
|
||||
#
|
||||
def initialize(width = 0, format = nil)
|
||||
@width, @format, @list = width, format, []
|
||||
end
|
||||
|
||||
#
|
||||
# Prints the +label+ and measured time for the block,
|
||||
# formatted by +format+. See Tms#format for the
|
||||
# formatting rules.
|
||||
#
|
||||
def item(label = "", *format, &blk) # :yield:
|
||||
print label.to_s.ljust(@width)
|
||||
@list << res = Benchmark.measure(label, &blk)
|
||||
print res.format(@format, *format)
|
||||
res
|
||||
end
|
||||
|
||||
alias report item
|
||||
|
||||
# An array of Benchmark::Tms objects representing each item.
|
||||
attr_reader :list
|
||||
end
|
||||
|
||||
|
||||
|
||||
#
|
||||
# A data object, representing the times associated with a benchmark
|
||||
# measurement.
|
||||
#
|
||||
class Tms
|
||||
|
||||
# Default caption, see also Benchmark::CAPTION
|
||||
CAPTION = " user system total real\n"
|
||||
|
||||
# Default format string, see also Benchmark::FORMAT
|
||||
FORMAT = "%10.6u %10.6y %10.6t %10.6r\n"
|
||||
|
||||
# User CPU time
|
||||
attr_reader :utime
|
||||
|
||||
# System CPU time
|
||||
attr_reader :stime
|
||||
|
||||
# User CPU time of children
|
||||
attr_reader :cutime
|
||||
|
||||
# System CPU time of children
|
||||
attr_reader :cstime
|
||||
|
||||
# Elapsed real time
|
||||
attr_reader :real
|
||||
|
||||
# Total time, that is +utime+ + +stime+ + +cutime+ + +cstime+
|
||||
attr_reader :total
|
||||
|
||||
# Label
|
||||
attr_reader :label
|
||||
|
||||
#
|
||||
# Returns an initialized Tms object which has
|
||||
# +utime+ as the user CPU time, +stime+ as the system CPU time,
|
||||
# +cutime+ as the children's user CPU time, +cstime+ as the children's
|
||||
# system CPU time, +real+ as the elapsed real time and +label+ as the label.
|
||||
#
|
||||
def initialize(utime = 0.0, stime = 0.0, cutime = 0.0, cstime = 0.0, real = 0.0, label = nil)
|
||||
@utime, @stime, @cutime, @cstime, @real, @label = utime, stime, cutime, cstime, real, label.to_s
|
||||
@total = @utime + @stime + @cutime + @cstime
|
||||
end
|
||||
|
||||
#
|
||||
# Returns a new Tms object whose times are the sum of the times for this
|
||||
# Tms object, plus the time required to execute the code block (+blk+).
|
||||
#
|
||||
def add(&blk) # :yield:
|
||||
self + Benchmark.measure(&blk)
|
||||
end
|
||||
|
||||
#
|
||||
# An in-place version of #add.
|
||||
#
|
||||
def add!(&blk)
|
||||
t = Benchmark.measure(&blk)
|
||||
@utime = utime + t.utime
|
||||
@stime = stime + t.stime
|
||||
@cutime = cutime + t.cutime
|
||||
@cstime = cstime + t.cstime
|
||||
@real = real + t.real
|
||||
self
|
||||
end
|
||||
|
||||
#
|
||||
# Returns a new Tms object obtained by memberwise summation
|
||||
# of the individual times for this Tms object with those of the other
|
||||
# Tms object.
|
||||
# This method and #/() are useful for taking statistics.
|
||||
#
|
||||
def +(other); memberwise(:+, other) end
|
||||
|
||||
#
|
||||
# Returns a new Tms object obtained by memberwise subtraction
|
||||
# of the individual times for the other Tms object from those of this
|
||||
# Tms object.
|
||||
#
|
||||
def -(other); memberwise(:-, other) end
|
||||
|
||||
#
|
||||
# Returns a new Tms object obtained by memberwise multiplication
|
||||
# of the individual times for this Tms object by _x_.
|
||||
#
|
||||
def *(x); memberwise(:*, x) end
|
||||
|
||||
#
|
||||
# Returns a new Tms object obtained by memberwise division
|
||||
# of the individual times for this Tms object by _x_.
|
||||
# This method and #+() are useful for taking statistics.
|
||||
#
|
||||
def /(x); memberwise(:/, x) end
|
||||
|
||||
#
|
||||
# Returns the contents of this Tms object as
|
||||
# a formatted string, according to a format string
|
||||
# like that passed to Kernel.format. In addition, #format
|
||||
# accepts the following extensions:
|
||||
#
|
||||
# <tt>%u</tt>:: Replaced by the user CPU time, as reported by Tms#utime.
|
||||
# <tt>%y</tt>:: Replaced by the system CPU time, as reported by #stime (Mnemonic: y of "s*y*stem")
|
||||
# <tt>%U</tt>:: Replaced by the children's user CPU time, as reported by Tms#cutime
|
||||
# <tt>%Y</tt>:: Replaced by the children's system CPU time, as reported by Tms#cstime
|
||||
# <tt>%t</tt>:: Replaced by the total CPU time, as reported by Tms#total
|
||||
# <tt>%r</tt>:: Replaced by the elapsed real time, as reported by Tms#real
|
||||
# <tt>%n</tt>:: Replaced by the label string, as reported by Tms#label (Mnemonic: n of "*n*ame")
|
||||
#
|
||||
# If _format_ is not given, FORMAT is used as default value, detailing the
|
||||
# user, system and real elapsed time.
|
||||
#
|
||||
def format(format = nil, *args)
|
||||
str = (format || FORMAT).dup
|
||||
str.gsub!(/(%[-+.\d]*)n/) { "#{$1}s" % label }
|
||||
str.gsub!(/(%[-+.\d]*)u/) { "#{$1}f" % utime }
|
||||
str.gsub!(/(%[-+.\d]*)y/) { "#{$1}f" % stime }
|
||||
str.gsub!(/(%[-+.\d]*)U/) { "#{$1}f" % cutime }
|
||||
str.gsub!(/(%[-+.\d]*)Y/) { "#{$1}f" % cstime }
|
||||
str.gsub!(/(%[-+.\d]*)t/) { "#{$1}f" % total }
|
||||
str.gsub!(/(%[-+.\d]*)r/) { "(#{$1}f)" % real }
|
||||
format ? str % args : str
|
||||
end
|
||||
|
||||
#
|
||||
# Same as #format.
|
||||
#
|
||||
def to_s
|
||||
format
|
||||
end
|
||||
|
||||
#
|
||||
# Returns a new 6-element array, consisting of the
|
||||
# label, user CPU time, system CPU time, children's
|
||||
# user CPU time, children's system CPU time and elapsed
|
||||
# real time.
|
||||
#
|
||||
def to_a
|
||||
[@label, @utime, @stime, @cutime, @cstime, @real]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
#
|
||||
# Returns a new Tms object obtained by memberwise operation +op+
|
||||
# of the individual times for this Tms object with those of the other
|
||||
# Tms object.
|
||||
#
|
||||
# +op+ can be a mathematical operation such as <tt>+</tt>, <tt>-</tt>,
|
||||
# <tt>*</tt>, <tt>/</tt>
|
||||
#
|
||||
def memberwise(op, x)
|
||||
case x
|
||||
when Benchmark::Tms
|
||||
Benchmark::Tms.new(utime.__send__(op, x.utime),
|
||||
stime.__send__(op, x.stime),
|
||||
cutime.__send__(op, x.cutime),
|
||||
cstime.__send__(op, x.cstime),
|
||||
real.__send__(op, x.real)
|
||||
)
|
||||
else
|
||||
Benchmark::Tms.new(utime.__send__(op, x),
|
||||
stime.__send__(op, x),
|
||||
cutime.__send__(op, x),
|
||||
cstime.__send__(op, x),
|
||||
real.__send__(op, x)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# The default caption string (heading above the output times).
|
||||
CAPTION = Benchmark::Tms::CAPTION
|
||||
|
||||
# The default format string used to display times. See also Benchmark::Tms#format.
|
||||
FORMAT = Benchmark::Tms::FORMAT
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
include Benchmark
|
||||
|
||||
n = ARGV[0].to_i.nonzero? || 50000
|
||||
puts %Q([#{n} times iterations of `a = "1"'])
|
||||
benchmark(CAPTION, 7, FORMAT) do |x|
|
||||
x.report("for:") {for _ in 1..n; _ = "1"; end} # Benchmark.measure
|
||||
x.report("times:") {n.times do ; _ = "1"; end}
|
||||
x.report("upto:") {1.upto(n) do ; _ = "1"; end}
|
||||
end
|
||||
|
||||
benchmark do
|
||||
[
|
||||
measure{for _ in 1..n; _ = "1"; end}, # Benchmark.measure
|
||||
measure{n.times do ; _ = "1"; end},
|
||||
measure{1.upto(n) do ; _ = "1"; end}
|
||||
]
|
||||
end
|
||||
end
|
||||
87
ruby/lib/ruby/2.1.0/bigdecimal/jacobian.rb
Normal file
87
ruby/lib/ruby/2.1.0/bigdecimal/jacobian.rb
Normal file
@@ -0,0 +1,87 @@
|
||||
#
|
||||
# require 'bigdecimal/jacobian'
|
||||
#
|
||||
# Provides methods to compute the Jacobian matrix of a set of equations at a
|
||||
# point x. In the methods below:
|
||||
#
|
||||
# f is an Object which is used to compute the Jacobian matrix of the equations.
|
||||
# It must provide the following methods:
|
||||
#
|
||||
# f.values(x):: returns the values of all functions at x
|
||||
#
|
||||
# f.zero:: returns 0.0
|
||||
# f.one:: returns 1.0
|
||||
# f.two:: returns 2.0
|
||||
# f.ten:: returns 10.0
|
||||
#
|
||||
# f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
|
||||
#
|
||||
# x is the point at which to compute the Jacobian.
|
||||
#
|
||||
# fx is f.values(x).
|
||||
#
|
||||
module Jacobian
|
||||
module_function
|
||||
|
||||
# Determines the equality of two numbers by comparing to zero, or using the epsilon value
|
||||
def isEqual(a,b,zero=0.0,e=1.0e-8)
|
||||
aa = a.abs
|
||||
bb = b.abs
|
||||
if aa == zero && bb == zero then
|
||||
true
|
||||
else
|
||||
if ((a-b)/(aa+bb)).abs < e then
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Computes the derivative of f[i] at x[i].
|
||||
# fx is the value of f at x.
|
||||
def dfdxi(f,fx,x,i)
|
||||
nRetry = 0
|
||||
n = x.size
|
||||
xSave = x[i]
|
||||
ok = 0
|
||||
ratio = f.ten*f.ten*f.ten
|
||||
dx = x[i].abs/ratio
|
||||
dx = fx[i].abs/ratio if isEqual(dx,f.zero,f.zero,f.eps)
|
||||
dx = f.one/f.ten if isEqual(dx,f.zero,f.zero,f.eps)
|
||||
until ok>0 do
|
||||
deriv = []
|
||||
nRetry += 1
|
||||
if nRetry > 100
|
||||
raise "Singular Jacobian matrix. No change at x[" + i.to_s + "]"
|
||||
end
|
||||
dx = dx*f.two
|
||||
x[i] += dx
|
||||
fxNew = f.values(x)
|
||||
for j in 0...n do
|
||||
if !isEqual(fxNew[j],fx[j],f.zero,f.eps) then
|
||||
ok += 1
|
||||
deriv <<= (fxNew[j]-fx[j])/dx
|
||||
else
|
||||
deriv <<= f.zero
|
||||
end
|
||||
end
|
||||
x[i] = xSave
|
||||
end
|
||||
deriv
|
||||
end
|
||||
|
||||
# Computes the Jacobian of f at x. fx is the value of f at x.
|
||||
def jacobian(f,fx,x)
|
||||
n = x.size
|
||||
dfdx = Array::new(n*n)
|
||||
for i in 0...n do
|
||||
df = dfdxi(f,fx,x,i)
|
||||
for j in 0...n do
|
||||
dfdx[j*n+i] = df[j]
|
||||
end
|
||||
end
|
||||
dfdx
|
||||
end
|
||||
end
|
||||
88
ruby/lib/ruby/2.1.0/bigdecimal/ludcmp.rb
Normal file
88
ruby/lib/ruby/2.1.0/bigdecimal/ludcmp.rb
Normal file
@@ -0,0 +1,88 @@
|
||||
require 'bigdecimal'
|
||||
|
||||
#
|
||||
# Solves a*x = b for x, using LU decomposition.
|
||||
#
|
||||
module LUSolve
|
||||
module_function
|
||||
|
||||
# Performs LU decomposition of the n by n matrix a.
|
||||
def ludecomp(a,n,zero=0,one=1)
|
||||
prec = BigDecimal.limit(nil)
|
||||
ps = []
|
||||
scales = []
|
||||
for i in 0...n do # pick up largest(abs. val.) element in each row.
|
||||
ps <<= i
|
||||
nrmrow = zero
|
||||
ixn = i*n
|
||||
for j in 0...n do
|
||||
biggst = a[ixn+j].abs
|
||||
nrmrow = biggst if biggst>nrmrow
|
||||
end
|
||||
if nrmrow>zero then
|
||||
scales <<= one.div(nrmrow,prec)
|
||||
else
|
||||
raise "Singular matrix"
|
||||
end
|
||||
end
|
||||
n1 = n - 1
|
||||
for k in 0...n1 do # Gaussian elimination with partial pivoting.
|
||||
biggst = zero;
|
||||
for i in k...n do
|
||||
size = a[ps[i]*n+k].abs*scales[ps[i]]
|
||||
if size>biggst then
|
||||
biggst = size
|
||||
pividx = i
|
||||
end
|
||||
end
|
||||
raise "Singular matrix" if biggst<=zero
|
||||
if pividx!=k then
|
||||
j = ps[k]
|
||||
ps[k] = ps[pividx]
|
||||
ps[pividx] = j
|
||||
end
|
||||
pivot = a[ps[k]*n+k]
|
||||
for i in (k+1)...n do
|
||||
psin = ps[i]*n
|
||||
a[psin+k] = mult = a[psin+k].div(pivot,prec)
|
||||
if mult!=zero then
|
||||
pskn = ps[k]*n
|
||||
for j in (k+1)...n do
|
||||
a[psin+j] -= mult.mult(a[pskn+j],prec)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
raise "Singular matrix" if a[ps[n1]*n+n1] == zero
|
||||
ps
|
||||
end
|
||||
|
||||
# Solves a*x = b for x, using LU decomposition.
|
||||
#
|
||||
# a is a matrix, b is a constant vector, x is the solution vector.
|
||||
#
|
||||
# ps is the pivot, a vector which indicates the permutation of rows performed
|
||||
# during LU decomposition.
|
||||
def lusolve(a,b,ps,zero=0.0)
|
||||
prec = BigDecimal.limit(nil)
|
||||
n = ps.size
|
||||
x = []
|
||||
for i in 0...n do
|
||||
dot = zero
|
||||
psin = ps[i]*n
|
||||
for j in 0...i do
|
||||
dot = a[psin+j].mult(x[j],prec) + dot
|
||||
end
|
||||
x <<= b[ps[i]] - dot
|
||||
end
|
||||
(n-1).downto(0) do |i|
|
||||
dot = zero
|
||||
psin = ps[i]*n
|
||||
for j in (i+1)...n do
|
||||
dot = a[psin+j].mult(x[j],prec) + dot
|
||||
end
|
||||
x[i] = (x[i]-dot).div(a[psin+i],prec)
|
||||
end
|
||||
x
|
||||
end
|
||||
end
|
||||
231
ruby/lib/ruby/2.1.0/bigdecimal/math.rb
Normal file
231
ruby/lib/ruby/2.1.0/bigdecimal/math.rb
Normal file
@@ -0,0 +1,231 @@
|
||||
require 'bigdecimal'
|
||||
|
||||
#
|
||||
#--
|
||||
# Contents:
|
||||
# sqrt(x, prec)
|
||||
# sin (x, prec)
|
||||
# cos (x, prec)
|
||||
# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
|
||||
# PI (prec)
|
||||
# E (prec) == exp(1.0,prec)
|
||||
#
|
||||
# where:
|
||||
# x ... BigDecimal number to be computed.
|
||||
# |x| must be small enough to get convergence.
|
||||
# prec ... Number of digits to be obtained.
|
||||
#++
|
||||
#
|
||||
# Provides mathematical functions.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# require "bigdecimal/math"
|
||||
#
|
||||
# include BigMath
|
||||
#
|
||||
# a = BigDecimal((PI(100)/2).to_s)
|
||||
# puts sin(a,100) # => 0.10000000000000000000......E1
|
||||
#
|
||||
module BigMath
|
||||
module_function
|
||||
|
||||
# call-seq:
|
||||
# sqrt(decimal, numeric) -> BigDecimal
|
||||
#
|
||||
# Computes the square root of +decimal+ to the specified number of digits of
|
||||
# precision, +numeric+.
|
||||
#
|
||||
# BigMath::sqrt(BigDecimal.new('2'), 16).to_s
|
||||
# #=> "0.14142135623730950488016887242096975E1"
|
||||
#
|
||||
def sqrt(x, prec)
|
||||
x.sqrt(prec)
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# sin(decimal, numeric) -> BigDecimal
|
||||
#
|
||||
# Computes the sine of +decimal+ to the specified number of digits of
|
||||
# precision, +numeric+.
|
||||
#
|
||||
# If +decimal+ is Infinity or NaN, returns NaN.
|
||||
#
|
||||
# BigMath::sin(BigMath::PI(5)/4, 5).to_s
|
||||
# #=> "0.70710678118654752440082036563292800375E0"
|
||||
#
|
||||
def sin(x, prec)
|
||||
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
|
||||
return BigDecimal("NaN") if x.infinite? || x.nan?
|
||||
n = prec + BigDecimal.double_fig
|
||||
one = BigDecimal("1")
|
||||
two = BigDecimal("2")
|
||||
x = -x if neg = x < 0
|
||||
if x > (twopi = two * BigMath.PI(prec))
|
||||
if x > 30
|
||||
x %= twopi
|
||||
else
|
||||
x -= twopi while x > twopi
|
||||
end
|
||||
end
|
||||
x1 = x
|
||||
x2 = x.mult(x,n)
|
||||
sign = 1
|
||||
y = x
|
||||
d = y
|
||||
i = one
|
||||
z = one
|
||||
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
||||
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
||||
sign = -sign
|
||||
x1 = x2.mult(x1,n)
|
||||
i += two
|
||||
z *= (i-one) * i
|
||||
d = sign * x1.div(z,m)
|
||||
y += d
|
||||
end
|
||||
neg ? -y : y
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# cos(decimal, numeric) -> BigDecimal
|
||||
#
|
||||
# Computes the cosine of +decimal+ to the specified number of digits of
|
||||
# precision, +numeric+.
|
||||
#
|
||||
# If +decimal+ is Infinity or NaN, returns NaN.
|
||||
#
|
||||
# BigMath::cos(BigMath::PI(4), 16).to_s
|
||||
# #=> "-0.999999999999999999999999999999856613163740061349E0"
|
||||
#
|
||||
def cos(x, prec)
|
||||
raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
|
||||
return BigDecimal("NaN") if x.infinite? || x.nan?
|
||||
n = prec + BigDecimal.double_fig
|
||||
one = BigDecimal("1")
|
||||
two = BigDecimal("2")
|
||||
x = -x if x < 0
|
||||
if x > (twopi = two * BigMath.PI(prec))
|
||||
if x > 30
|
||||
x %= twopi
|
||||
else
|
||||
x -= twopi while x > twopi
|
||||
end
|
||||
end
|
||||
x1 = one
|
||||
x2 = x.mult(x,n)
|
||||
sign = 1
|
||||
y = one
|
||||
d = y
|
||||
i = BigDecimal("0")
|
||||
z = one
|
||||
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
||||
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
||||
sign = -sign
|
||||
x1 = x2.mult(x1,n)
|
||||
i += two
|
||||
z *= (i-one) * i
|
||||
d = sign * x1.div(z,m)
|
||||
y += d
|
||||
end
|
||||
y
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# atan(decimal, numeric) -> BigDecimal
|
||||
#
|
||||
# Computes the arctangent of +decimal+ to the specified number of digits of
|
||||
# precision, +numeric+.
|
||||
#
|
||||
# If +decimal+ is NaN, returns NaN.
|
||||
#
|
||||
# BigMath::atan(BigDecimal.new('-1'), 16).to_s
|
||||
# #=> "-0.785398163397448309615660845819878471907514682065E0"
|
||||
#
|
||||
def atan(x, prec)
|
||||
raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
|
||||
return BigDecimal("NaN") if x.nan?
|
||||
pi = PI(prec)
|
||||
x = -x if neg = x < 0
|
||||
return pi.div(neg ? -2 : 2, prec) if x.infinite?
|
||||
return pi / (neg ? -4 : 4) if x.round(prec) == 1
|
||||
x = BigDecimal("1").div(x, prec) if inv = x > 1
|
||||
x = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5
|
||||
n = prec + BigDecimal.double_fig
|
||||
y = x
|
||||
d = y
|
||||
t = x
|
||||
r = BigDecimal("3")
|
||||
x2 = x.mult(x,n)
|
||||
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
||||
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
||||
t = -t.mult(x2,n)
|
||||
d = t.div(r,m)
|
||||
y += d
|
||||
r += 2
|
||||
end
|
||||
y *= 2 if dbl
|
||||
y = pi / 2 - y if inv
|
||||
y = -y if neg
|
||||
y
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# PI(numeric) -> BigDecimal
|
||||
#
|
||||
# Computes the value of pi to the specified number of digits of precision,
|
||||
# +numeric+.
|
||||
#
|
||||
# BigMath::PI(10).to_s
|
||||
# #=> "0.3141592653589793238462643388813853786957412E1"
|
||||
#
|
||||
def PI(prec)
|
||||
raise ArgumentError, "Zero or negative argument for PI" if prec <= 0
|
||||
n = prec + BigDecimal.double_fig
|
||||
zero = BigDecimal("0")
|
||||
one = BigDecimal("1")
|
||||
two = BigDecimal("2")
|
||||
|
||||
m25 = BigDecimal("-0.04")
|
||||
m57121 = BigDecimal("-57121")
|
||||
|
||||
pi = zero
|
||||
|
||||
d = one
|
||||
k = one
|
||||
t = BigDecimal("-80")
|
||||
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
|
||||
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
||||
t = t*m25
|
||||
d = t.div(k,m)
|
||||
k = k+two
|
||||
pi = pi + d
|
||||
end
|
||||
|
||||
d = one
|
||||
k = one
|
||||
t = BigDecimal("956")
|
||||
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
|
||||
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
||||
t = t.div(m57121,n)
|
||||
d = t.div(k,m)
|
||||
pi = pi + d
|
||||
k = k+two
|
||||
end
|
||||
pi
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# E(numeric) -> BigDecimal
|
||||
#
|
||||
# Computes e (the base of natural logarithms) to the specified number of
|
||||
# digits of precision, +numeric+.
|
||||
#
|
||||
# BigMath::E(10).to_s
|
||||
# #=> "0.271828182845904523536028752390026306410273E1"
|
||||
#
|
||||
def E(prec)
|
||||
raise ArgumentError, "Zero or negative precision for E" if prec <= 0
|
||||
BigMath.exp(1, prec)
|
||||
end
|
||||
end
|
||||
79
ruby/lib/ruby/2.1.0/bigdecimal/newton.rb
Normal file
79
ruby/lib/ruby/2.1.0/bigdecimal/newton.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
require "bigdecimal/ludcmp"
|
||||
require "bigdecimal/jacobian"
|
||||
|
||||
#
|
||||
# newton.rb
|
||||
#
|
||||
# Solves the nonlinear algebraic equation system f = 0 by Newton's method.
|
||||
# This program is not dependent on BigDecimal.
|
||||
#
|
||||
# To call:
|
||||
# n = nlsolve(f,x)
|
||||
# where n is the number of iterations required,
|
||||
# x is the initial value vector
|
||||
# f is an Object which is used to compute the values of the equations to be solved.
|
||||
# It must provide the following methods:
|
||||
#
|
||||
# f.values(x):: returns the values of all functions at x
|
||||
#
|
||||
# f.zero:: returns 0.0
|
||||
# f.one:: returns 1.0
|
||||
# f.two:: returns 2.0
|
||||
# f.ten:: returns 10.0
|
||||
#
|
||||
# f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
|
||||
#
|
||||
# On exit, x is the solution vector.
|
||||
#
|
||||
module Newton
|
||||
include LUSolve
|
||||
include Jacobian
|
||||
module_function
|
||||
|
||||
def norm(fv,zero=0.0) # :nodoc:
|
||||
s = zero
|
||||
n = fv.size
|
||||
for i in 0...n do
|
||||
s += fv[i]*fv[i]
|
||||
end
|
||||
s
|
||||
end
|
||||
|
||||
# See also Newton
|
||||
def nlsolve(f,x)
|
||||
nRetry = 0
|
||||
n = x.size
|
||||
|
||||
f0 = f.values(x)
|
||||
zero = f.zero
|
||||
one = f.one
|
||||
two = f.two
|
||||
p5 = one/two
|
||||
d = norm(f0,zero)
|
||||
minfact = f.ten*f.ten*f.ten
|
||||
minfact = one/minfact
|
||||
e = f.eps
|
||||
while d >= e do
|
||||
nRetry += 1
|
||||
# Not yet converged. => Compute Jacobian matrix
|
||||
dfdx = jacobian(f,f0,x)
|
||||
# Solve dfdx*dx = -f0 to estimate dx
|
||||
dx = lusolve(dfdx,f0,ludecomp(dfdx,n,zero,one),zero)
|
||||
fact = two
|
||||
xs = x.dup
|
||||
begin
|
||||
fact *= p5
|
||||
if fact < minfact then
|
||||
raise "Failed to reduce function values."
|
||||
end
|
||||
for i in 0...n do
|
||||
x[i] = xs[i] - dx[i]*fact
|
||||
end
|
||||
f0 = f.values(x)
|
||||
dn = norm(f0,zero)
|
||||
end while(dn>=d)
|
||||
d = dn
|
||||
end
|
||||
nRetry
|
||||
end
|
||||
end
|
||||
127
ruby/lib/ruby/2.1.0/bigdecimal/util.rb
Normal file
127
ruby/lib/ruby/2.1.0/bigdecimal/util.rb
Normal file
@@ -0,0 +1,127 @@
|
||||
# BigDecimal extends the native Integer class to provide the #to_d method.
|
||||
#
|
||||
# When you require the BigDecimal library in your application, this methodwill
|
||||
# be available on Integer objects.
|
||||
class Integer < Numeric
|
||||
# call-seq:
|
||||
# int.to_d -> bigdecimal
|
||||
#
|
||||
# Convert +int+ to a BigDecimal and return it.
|
||||
#
|
||||
# require 'bigdecimal'
|
||||
# require 'bigdecimal/util'
|
||||
#
|
||||
# 42.to_d
|
||||
# # => #<BigDecimal:1008ef070,'0.42E2',9(36)>
|
||||
#
|
||||
def to_d
|
||||
BigDecimal(self)
|
||||
end
|
||||
end
|
||||
|
||||
# BigDecimal extends the native Float class to provide the #to_d method.
|
||||
#
|
||||
# When you require BigDecimal in your application, this method will be
|
||||
# available on Float objects.
|
||||
class Float < Numeric
|
||||
# call-seq:
|
||||
# flt.to_d -> bigdecimal
|
||||
#
|
||||
# Convert +flt+ to a BigDecimal and return it.
|
||||
#
|
||||
# require 'bigdecimal'
|
||||
# require 'bigdecimal/util'
|
||||
#
|
||||
# 0.5.to_d
|
||||
# # => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
|
||||
#
|
||||
def to_d(precision=nil)
|
||||
BigDecimal(self, precision || Float::DIG)
|
||||
end
|
||||
end
|
||||
|
||||
# BigDecimal extends the native String class to provide the #to_d method.
|
||||
#
|
||||
# When you require BigDecimal in your application, this method will be
|
||||
# available on String objects.
|
||||
class String
|
||||
# call-seq:
|
||||
# string.to_d -> bigdecimal
|
||||
#
|
||||
# Convert +string+ to a BigDecimal and return it.
|
||||
#
|
||||
# require 'bigdecimal'
|
||||
# require 'bigdecimal/util'
|
||||
#
|
||||
# "0.5".to_d
|
||||
# # => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
|
||||
#
|
||||
def to_d
|
||||
BigDecimal(self)
|
||||
end
|
||||
end
|
||||
|
||||
# BigDecimal extends the native Numeric class to provide the #to_digits and
|
||||
# #to_d methods.
|
||||
#
|
||||
# When you require BigDecimal in your application, this method will be
|
||||
# available on BigDecimal objects.
|
||||
class BigDecimal < Numeric
|
||||
# call-seq:
|
||||
# a.to_digits -> string
|
||||
#
|
||||
# Converts a BigDecimal to a String of the form "nnnnnn.mmm".
|
||||
# This method is deprecated; use BigDecimal#to_s("F") instead.
|
||||
#
|
||||
# require 'bigdecimal'
|
||||
# require 'bigdecimal/util'
|
||||
#
|
||||
# d = BigDecimal.new("3.14")
|
||||
# d.to_digits
|
||||
# # => "3.14"
|
||||
def to_digits
|
||||
if self.nan? || self.infinite? || self.zero?
|
||||
self.to_s
|
||||
else
|
||||
i = self.to_i.to_s
|
||||
_,f,_,z = self.frac.split
|
||||
i + "." + ("0"*(-z)) + f
|
||||
end
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# a.to_d -> bigdecimal
|
||||
#
|
||||
# Returns self.
|
||||
def to_d
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
# BigDecimal extends the native Rational class to provide the #to_d method.
|
||||
#
|
||||
# When you require BigDecimal in your application, this method will be
|
||||
# available on Rational objects.
|
||||
class Rational < Numeric
|
||||
# call-seq:
|
||||
# r.to_d(precision) -> bigdecimal
|
||||
#
|
||||
# Converts a Rational to a BigDecimal.
|
||||
#
|
||||
# The required +precision+ parameter is used to determine the amount of
|
||||
# significant digits for the result. See BigDecimal#div for more information,
|
||||
# as it is used along with the #denominator and the +precision+ for
|
||||
# parameters.
|
||||
#
|
||||
# r = (22/7.0).to_r
|
||||
# # => (7077085128725065/2251799813685248)
|
||||
# r.to_d(3)
|
||||
# # => #<BigDecimal:1a44d08,'0.314E1',18(36)>
|
||||
def to_d(precision)
|
||||
if precision <= 0
|
||||
raise ArgumentError, "negative precision"
|
||||
end
|
||||
num = self.numerator
|
||||
BigDecimal(num).div(self.denominator, precision)
|
||||
end
|
||||
end
|
||||
297
ruby/lib/ruby/2.1.0/cgi.rb
Normal file
297
ruby/lib/ruby/2.1.0/cgi.rb
Normal file
@@ -0,0 +1,297 @@
|
||||
#
|
||||
# cgi.rb - cgi support library
|
||||
#
|
||||
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
||||
#
|
||||
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
||||
#
|
||||
# Author: Wakou Aoyama <wakou@ruby-lang.org>
|
||||
#
|
||||
# Documentation: Wakou Aoyama (RDoc'd and embellished by William Webber)
|
||||
#
|
||||
|
||||
raise "Please, use ruby 1.9.0 or later." if RUBY_VERSION < "1.9.0"
|
||||
|
||||
# == Overview
|
||||
#
|
||||
# The Common Gateway Interface (CGI) is a simple protocol for passing an HTTP
|
||||
# request from a web server to a standalone program, and returning the output
|
||||
# to the web browser. Basically, a CGI program is called with the parameters
|
||||
# of the request passed in either in the environment (GET) or via $stdin
|
||||
# (POST), and everything it prints to $stdout is returned to the client.
|
||||
#
|
||||
# This file holds the CGI class. This class provides functionality for
|
||||
# retrieving HTTP request parameters, managing cookies, and generating HTML
|
||||
# output.
|
||||
#
|
||||
# The file CGI::Session provides session management functionality; see that
|
||||
# class for more details.
|
||||
#
|
||||
# See http://www.w3.org/CGI/ for more information on the CGI protocol.
|
||||
#
|
||||
# == Introduction
|
||||
#
|
||||
# CGI is a large class, providing several categories of methods, many of which
|
||||
# are mixed in from other modules. Some of the documentation is in this class,
|
||||
# some in the modules CGI::QueryExtension and CGI::HtmlExtension. See
|
||||
# CGI::Cookie for specific information on handling cookies, and cgi/session.rb
|
||||
# (CGI::Session) for information on sessions.
|
||||
#
|
||||
# For queries, CGI provides methods to get at environmental variables,
|
||||
# parameters, cookies, and multipart request data. For responses, CGI provides
|
||||
# methods for writing output and generating HTML.
|
||||
#
|
||||
# Read on for more details. Examples are provided at the bottom.
|
||||
#
|
||||
# == Queries
|
||||
#
|
||||
# The CGI class dynamically mixes in parameter and cookie-parsing
|
||||
# functionality, environmental variable access, and support for
|
||||
# parsing multipart requests (including uploaded files) from the
|
||||
# CGI::QueryExtension module.
|
||||
#
|
||||
# === Environmental Variables
|
||||
#
|
||||
# The standard CGI environmental variables are available as read-only
|
||||
# attributes of a CGI object. The following is a list of these variables:
|
||||
#
|
||||
#
|
||||
# AUTH_TYPE HTTP_HOST REMOTE_IDENT
|
||||
# CONTENT_LENGTH HTTP_NEGOTIATE REMOTE_USER
|
||||
# CONTENT_TYPE HTTP_PRAGMA REQUEST_METHOD
|
||||
# GATEWAY_INTERFACE HTTP_REFERER SCRIPT_NAME
|
||||
# HTTP_ACCEPT HTTP_USER_AGENT SERVER_NAME
|
||||
# HTTP_ACCEPT_CHARSET PATH_INFO SERVER_PORT
|
||||
# HTTP_ACCEPT_ENCODING PATH_TRANSLATED SERVER_PROTOCOL
|
||||
# HTTP_ACCEPT_LANGUAGE QUERY_STRING SERVER_SOFTWARE
|
||||
# HTTP_CACHE_CONTROL REMOTE_ADDR
|
||||
# HTTP_FROM REMOTE_HOST
|
||||
#
|
||||
#
|
||||
# For each of these variables, there is a corresponding attribute with the
|
||||
# same name, except all lower case and without a preceding HTTP_.
|
||||
# +content_length+ and +server_port+ are integers; the rest are strings.
|
||||
#
|
||||
# === Parameters
|
||||
#
|
||||
# The method #params() returns a hash of all parameters in the request as
|
||||
# name/value-list pairs, where the value-list is an Array of one or more
|
||||
# values. The CGI object itself also behaves as a hash of parameter names
|
||||
# to values, but only returns a single value (as a String) for each
|
||||
# parameter name.
|
||||
#
|
||||
# For instance, suppose the request contains the parameter
|
||||
# "favourite_colours" with the multiple values "blue" and "green". The
|
||||
# following behaviour would occur:
|
||||
#
|
||||
# cgi.params["favourite_colours"] # => ["blue", "green"]
|
||||
# cgi["favourite_colours"] # => "blue"
|
||||
#
|
||||
# If a parameter does not exist, the former method will return an empty
|
||||
# array, the latter an empty string. The simplest way to test for existence
|
||||
# of a parameter is by the #has_key? method.
|
||||
#
|
||||
# === Cookies
|
||||
#
|
||||
# HTTP Cookies are automatically parsed from the request. They are available
|
||||
# from the #cookies() accessor, which returns a hash from cookie name to
|
||||
# CGI::Cookie object.
|
||||
#
|
||||
# === Multipart requests
|
||||
#
|
||||
# If a request's method is POST and its content type is multipart/form-data,
|
||||
# then it may contain uploaded files. These are stored by the QueryExtension
|
||||
# module in the parameters of the request. The parameter name is the name
|
||||
# attribute of the file input field, as usual. However, the value is not
|
||||
# a string, but an IO object, either an IOString for small files, or a
|
||||
# Tempfile for larger ones. This object also has the additional singleton
|
||||
# methods:
|
||||
#
|
||||
# #local_path():: the path of the uploaded file on the local filesystem
|
||||
# #original_filename():: the name of the file on the client computer
|
||||
# #content_type():: the content type of the file
|
||||
#
|
||||
# == Responses
|
||||
#
|
||||
# The CGI class provides methods for sending header and content output to
|
||||
# the HTTP client, and mixes in methods for programmatic HTML generation
|
||||
# from CGI::HtmlExtension and CGI::TagMaker modules. The precise version of HTML
|
||||
# to use for HTML generation is specified at object creation time.
|
||||
#
|
||||
# === Writing output
|
||||
#
|
||||
# The simplest way to send output to the HTTP client is using the #out() method.
|
||||
# This takes the HTTP headers as a hash parameter, and the body content
|
||||
# via a block. The headers can be generated as a string using the #http_header()
|
||||
# method. The output stream can be written directly to using the #print()
|
||||
# method.
|
||||
#
|
||||
# === Generating HTML
|
||||
#
|
||||
# Each HTML element has a corresponding method for generating that
|
||||
# element as a String. The name of this method is the same as that
|
||||
# of the element, all lowercase. The attributes of the element are
|
||||
# passed in as a hash, and the body as a no-argument block that evaluates
|
||||
# to a String. The HTML generation module knows which elements are
|
||||
# always empty, and silently drops any passed-in body. It also knows
|
||||
# which elements require matching closing tags and which don't. However,
|
||||
# it does not know what attributes are legal for which elements.
|
||||
#
|
||||
# There are also some additional HTML generation methods mixed in from
|
||||
# the CGI::HtmlExtension module. These include individual methods for the
|
||||
# different types of form inputs, and methods for elements that commonly
|
||||
# take particular attributes where the attributes can be directly specified
|
||||
# as arguments, rather than via a hash.
|
||||
#
|
||||
# === Utility HTML escape and other methods like a function.
|
||||
#
|
||||
# There are some utility tool defined in cgi/util.rb .
|
||||
# And when include, you can use utility methods like a function.
|
||||
#
|
||||
# == Examples of use
|
||||
#
|
||||
# === Get form values
|
||||
#
|
||||
# require "cgi"
|
||||
# cgi = CGI.new
|
||||
# value = cgi['field_name'] # <== value string for 'field_name'
|
||||
# # if not 'field_name' included, then return "".
|
||||
# fields = cgi.keys # <== array of field names
|
||||
#
|
||||
# # returns true if form has 'field_name'
|
||||
# cgi.has_key?('field_name')
|
||||
# cgi.has_key?('field_name')
|
||||
# cgi.include?('field_name')
|
||||
#
|
||||
# CAUTION! cgi['field_name'] returned an Array with the old
|
||||
# cgi.rb(included in Ruby 1.6)
|
||||
#
|
||||
# === Get form values as hash
|
||||
#
|
||||
# require "cgi"
|
||||
# cgi = CGI.new
|
||||
# params = cgi.params
|
||||
#
|
||||
# cgi.params is a hash.
|
||||
#
|
||||
# cgi.params['new_field_name'] = ["value"] # add new param
|
||||
# cgi.params['field_name'] = ["new_value"] # change value
|
||||
# cgi.params.delete('field_name') # delete param
|
||||
# cgi.params.clear # delete all params
|
||||
#
|
||||
#
|
||||
# === Save form values to file
|
||||
#
|
||||
# require "pstore"
|
||||
# db = PStore.new("query.db")
|
||||
# db.transaction do
|
||||
# db["params"] = cgi.params
|
||||
# end
|
||||
#
|
||||
#
|
||||
# === Restore form values from file
|
||||
#
|
||||
# require "pstore"
|
||||
# db = PStore.new("query.db")
|
||||
# db.transaction do
|
||||
# cgi.params = db["params"]
|
||||
# end
|
||||
#
|
||||
#
|
||||
# === Get multipart form values
|
||||
#
|
||||
# require "cgi"
|
||||
# cgi = CGI.new
|
||||
# value = cgi['field_name'] # <== value string for 'field_name'
|
||||
# value.read # <== body of value
|
||||
# value.local_path # <== path to local file of value
|
||||
# value.original_filename # <== original filename of value
|
||||
# value.content_type # <== content_type of value
|
||||
#
|
||||
# and value has StringIO or Tempfile class methods.
|
||||
#
|
||||
# === Get cookie values
|
||||
#
|
||||
# require "cgi"
|
||||
# cgi = CGI.new
|
||||
# values = cgi.cookies['name'] # <== array of 'name'
|
||||
# # if not 'name' included, then return [].
|
||||
# names = cgi.cookies.keys # <== array of cookie names
|
||||
#
|
||||
# and cgi.cookies is a hash.
|
||||
#
|
||||
# === Get cookie objects
|
||||
#
|
||||
# require "cgi"
|
||||
# cgi = CGI.new
|
||||
# for name, cookie in cgi.cookies
|
||||
# cookie.expires = Time.now + 30
|
||||
# end
|
||||
# cgi.out("cookie" => cgi.cookies) {"string"}
|
||||
#
|
||||
# cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
|
||||
#
|
||||
# require "cgi"
|
||||
# cgi = CGI.new
|
||||
# cgi.cookies['name'].expires = Time.now + 30
|
||||
# cgi.out("cookie" => cgi.cookies['name']) {"string"}
|
||||
#
|
||||
# === Print http header and html string to $DEFAULT_OUTPUT ($>)
|
||||
#
|
||||
# require "cgi"
|
||||
# cgi = CGI.new("html4") # add HTML generation methods
|
||||
# cgi.out do
|
||||
# cgi.html do
|
||||
# cgi.head do
|
||||
# cgi.title { "TITLE" }
|
||||
# end +
|
||||
# cgi.body do
|
||||
# cgi.form("ACTION" => "uri") do
|
||||
# cgi.p do
|
||||
# cgi.textarea("get_text") +
|
||||
# cgi.br +
|
||||
# cgi.submit
|
||||
# end
|
||||
# end +
|
||||
# cgi.pre do
|
||||
# CGI::escapeHTML(
|
||||
# "params: #{cgi.params.inspect}\n" +
|
||||
# "cookies: #{cgi.cookies.inspect}\n" +
|
||||
# ENV.collect do |key, value|
|
||||
# "#{key} --> #{value}\n"
|
||||
# end.join("")
|
||||
# )
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# # add HTML generation methods
|
||||
# CGI.new("html3") # html3.2
|
||||
# CGI.new("html4") # html4.01 (Strict)
|
||||
# CGI.new("html4Tr") # html4.01 Transitional
|
||||
# CGI.new("html4Fr") # html4.01 Frameset
|
||||
# CGI.new("html5") # html5
|
||||
#
|
||||
# === Some utility methods
|
||||
#
|
||||
# require 'cgi/util'
|
||||
# CGI.escapeHTML('Usage: foo "bar" <baz>')
|
||||
#
|
||||
#
|
||||
# === Some utility methods like a function
|
||||
#
|
||||
# require 'cgi/util'
|
||||
# include CGI::Util
|
||||
# escapeHTML('Usage: foo "bar" <baz>')
|
||||
# h('Usage: foo "bar" <baz>') # alias
|
||||
#
|
||||
#
|
||||
|
||||
class CGI
|
||||
end
|
||||
|
||||
require 'cgi/core'
|
||||
require 'cgi/cookie'
|
||||
require 'cgi/util'
|
||||
CGI.autoload(:HtmlExtension, 'cgi/html')
|
||||
170
ruby/lib/ruby/2.1.0/cgi/cookie.rb
Normal file
170
ruby/lib/ruby/2.1.0/cgi/cookie.rb
Normal file
@@ -0,0 +1,170 @@
|
||||
require 'cgi/util'
|
||||
class CGI
|
||||
# Class representing an HTTP cookie.
|
||||
#
|
||||
# In addition to its specific fields and methods, a Cookie instance
|
||||
# is a delegator to the array of its values.
|
||||
#
|
||||
# See RFC 2965.
|
||||
#
|
||||
# == Examples of use
|
||||
# cookie1 = CGI::Cookie.new("name", "value1", "value2", ...)
|
||||
# cookie1 = CGI::Cookie.new("name" => "name", "value" => "value")
|
||||
# cookie1 = CGI::Cookie.new('name' => 'name',
|
||||
# 'value' => ['value1', 'value2', ...],
|
||||
# 'path' => 'path', # optional
|
||||
# 'domain' => 'domain', # optional
|
||||
# 'expires' => Time.now, # optional
|
||||
# 'secure' => true # optional
|
||||
# )
|
||||
#
|
||||
# cgi.out("cookie" => [cookie1, cookie2]) { "string" }
|
||||
#
|
||||
# name = cookie1.name
|
||||
# values = cookie1.value
|
||||
# path = cookie1.path
|
||||
# domain = cookie1.domain
|
||||
# expires = cookie1.expires
|
||||
# secure = cookie1.secure
|
||||
#
|
||||
# cookie1.name = 'name'
|
||||
# cookie1.value = ['value1', 'value2', ...]
|
||||
# cookie1.path = 'path'
|
||||
# cookie1.domain = 'domain'
|
||||
# cookie1.expires = Time.now + 30
|
||||
# cookie1.secure = true
|
||||
class Cookie < Array
|
||||
@@accept_charset="UTF-8" unless defined?(@@accept_charset)
|
||||
|
||||
# Create a new CGI::Cookie object.
|
||||
#
|
||||
# :call-seq:
|
||||
# Cookie.new(name_string,*value)
|
||||
# Cookie.new(options_hash)
|
||||
#
|
||||
# +name_string+::
|
||||
# The name of the cookie; in this form, there is no #domain or
|
||||
# #expiration. The #path is gleaned from the +SCRIPT_NAME+ environment
|
||||
# variable, and #secure is false.
|
||||
# <tt>*value</tt>::
|
||||
# value or list of values of the cookie
|
||||
# +options_hash+::
|
||||
# A Hash of options to initialize this Cookie. Possible options are:
|
||||
#
|
||||
# name:: the name of the cookie. Required.
|
||||
# value:: the cookie's value or list of values.
|
||||
# path:: the path for which this cookie applies. Defaults to the
|
||||
# the value of the +SCRIPT_NAME+ environment variable.
|
||||
# domain:: the domain for which this cookie applies.
|
||||
# expires:: the time at which this cookie expires, as a +Time+ object.
|
||||
# secure:: whether this cookie is a secure cookie or not (default to
|
||||
# false). Secure cookies are only transmitted to HTTPS
|
||||
# servers.
|
||||
#
|
||||
# These keywords correspond to attributes of the cookie object.
|
||||
def initialize(name = "", *value)
|
||||
@domain = nil
|
||||
@expires = nil
|
||||
if name.kind_of?(String)
|
||||
@name = name
|
||||
%r|^(.*/)|.match(ENV["SCRIPT_NAME"])
|
||||
@path = ($1 or "")
|
||||
@secure = false
|
||||
return super(value)
|
||||
end
|
||||
|
||||
options = name
|
||||
unless options.has_key?("name")
|
||||
raise ArgumentError, "`name' required"
|
||||
end
|
||||
|
||||
@name = options["name"]
|
||||
value = Array(options["value"])
|
||||
# simple support for IE
|
||||
if options["path"]
|
||||
@path = options["path"]
|
||||
else
|
||||
%r|^(.*/)|.match(ENV["SCRIPT_NAME"])
|
||||
@path = ($1 or "")
|
||||
end
|
||||
@domain = options["domain"]
|
||||
@expires = options["expires"]
|
||||
@secure = options["secure"] == true ? true : false
|
||||
|
||||
super(value)
|
||||
end
|
||||
|
||||
# Name of this cookie, as a +String+
|
||||
attr_accessor :name
|
||||
# Path for which this cookie applies, as a +String+
|
||||
attr_accessor :path
|
||||
# Domain for which this cookie applies, as a +String+
|
||||
attr_accessor :domain
|
||||
# Time at which this cookie expires, as a +Time+
|
||||
attr_accessor :expires
|
||||
# True if this cookie is secure; false otherwise
|
||||
attr_reader("secure")
|
||||
|
||||
# Returns the value or list of values for this cookie.
|
||||
def value
|
||||
self
|
||||
end
|
||||
|
||||
# Replaces the value of this cookie with a new value or list of values.
|
||||
def value=(val)
|
||||
replace(Array(val))
|
||||
end
|
||||
|
||||
# Set whether the Cookie is a secure cookie or not.
|
||||
#
|
||||
# +val+ must be a boolean.
|
||||
def secure=(val)
|
||||
@secure = val if val == true or val == false
|
||||
@secure
|
||||
end
|
||||
|
||||
# Convert the Cookie to its string representation.
|
||||
def to_s
|
||||
val = collect{|v| CGI.escape(v) }.join("&")
|
||||
buf = "#{@name}=#{val}"
|
||||
buf << "; domain=#{@domain}" if @domain
|
||||
buf << "; path=#{@path}" if @path
|
||||
buf << "; expires=#{CGI::rfc1123_date(@expires)}" if @expires
|
||||
buf << "; secure" if @secure == true
|
||||
buf
|
||||
end
|
||||
|
||||
# Parse a raw cookie string into a hash of cookie-name=>Cookie
|
||||
# pairs.
|
||||
#
|
||||
# cookies = CGI::Cookie.parse("raw_cookie_string")
|
||||
# # { "name1" => cookie1, "name2" => cookie2, ... }
|
||||
#
|
||||
def self.parse(raw_cookie)
|
||||
cookies = Hash.new([])
|
||||
return cookies unless raw_cookie
|
||||
|
||||
raw_cookie.split(/[;,]\s?/).each do |pairs|
|
||||
name, values = pairs.split('=',2)
|
||||
next unless name and values
|
||||
name = CGI.unescape(name)
|
||||
values ||= ""
|
||||
values = values.split('&').collect{|v| CGI.unescape(v,@@accept_charset) }
|
||||
if cookies.has_key?(name)
|
||||
values = cookies[name].value + values
|
||||
end
|
||||
cookies[name] = Cookie.new(name, *values)
|
||||
end
|
||||
|
||||
cookies
|
||||
end
|
||||
|
||||
# A summary of cookie string.
|
||||
def inspect
|
||||
"#<CGI::Cookie: #{self.to_s.inspect}>"
|
||||
end
|
||||
|
||||
end # class Cookie
|
||||
end
|
||||
|
||||
|
||||
859
ruby/lib/ruby/2.1.0/cgi/core.rb
Normal file
859
ruby/lib/ruby/2.1.0/cgi/core.rb
Normal file
@@ -0,0 +1,859 @@
|
||||
#--
|
||||
# Methods for generating HTML, parsing CGI-related parameters, and
|
||||
# generating HTTP responses.
|
||||
#++
|
||||
class CGI
|
||||
|
||||
$CGI_ENV = ENV # for FCGI support
|
||||
|
||||
# String for carriage return
|
||||
CR = "\015"
|
||||
|
||||
# String for linefeed
|
||||
LF = "\012"
|
||||
|
||||
# Standard internet newline sequence
|
||||
EOL = CR + LF
|
||||
|
||||
REVISION = '$Id: core.rb 44184 2013-12-13 16:11:12Z akr $' #:nodoc:
|
||||
|
||||
# Whether processing will be required in binary vs text
|
||||
NEEDS_BINMODE = File::BINARY != 0
|
||||
|
||||
# Path separators in different environments.
|
||||
PATH_SEPARATOR = {'UNIX'=>'/', 'WINDOWS'=>'\\', 'MACINTOSH'=>':'}
|
||||
|
||||
# HTTP status codes.
|
||||
HTTP_STATUS = {
|
||||
"OK" => "200 OK",
|
||||
"PARTIAL_CONTENT" => "206 Partial Content",
|
||||
"MULTIPLE_CHOICES" => "300 Multiple Choices",
|
||||
"MOVED" => "301 Moved Permanently",
|
||||
"REDIRECT" => "302 Found",
|
||||
"NOT_MODIFIED" => "304 Not Modified",
|
||||
"BAD_REQUEST" => "400 Bad Request",
|
||||
"AUTH_REQUIRED" => "401 Authorization Required",
|
||||
"FORBIDDEN" => "403 Forbidden",
|
||||
"NOT_FOUND" => "404 Not Found",
|
||||
"METHOD_NOT_ALLOWED" => "405 Method Not Allowed",
|
||||
"NOT_ACCEPTABLE" => "406 Not Acceptable",
|
||||
"LENGTH_REQUIRED" => "411 Length Required",
|
||||
"PRECONDITION_FAILED" => "412 Precondition Failed",
|
||||
"SERVER_ERROR" => "500 Internal Server Error",
|
||||
"NOT_IMPLEMENTED" => "501 Method Not Implemented",
|
||||
"BAD_GATEWAY" => "502 Bad Gateway",
|
||||
"VARIANT_ALSO_VARIES" => "506 Variant Also Negotiates"
|
||||
}
|
||||
|
||||
# :startdoc:
|
||||
|
||||
# Synonym for ENV.
|
||||
def env_table
|
||||
ENV
|
||||
end
|
||||
|
||||
# Synonym for $stdin.
|
||||
def stdinput
|
||||
$stdin
|
||||
end
|
||||
|
||||
# Synonym for $stdout.
|
||||
def stdoutput
|
||||
$stdout
|
||||
end
|
||||
|
||||
private :env_table, :stdinput, :stdoutput
|
||||
|
||||
# Create an HTTP header block as a string.
|
||||
#
|
||||
# :call-seq:
|
||||
# http_header(content_type_string="text/html")
|
||||
# http_header(headers_hash)
|
||||
#
|
||||
# Includes the empty line that ends the header block.
|
||||
#
|
||||
# +content_type_string+::
|
||||
# If this form is used, this string is the <tt>Content-Type</tt>
|
||||
# +headers_hash+::
|
||||
# A Hash of header values. The following header keys are recognized:
|
||||
#
|
||||
# type:: The Content-Type header. Defaults to "text/html"
|
||||
# charset:: The charset of the body, appended to the Content-Type header.
|
||||
# nph:: A boolean value. If true, prepend protocol string and status
|
||||
# code, and date; and sets default values for "server" and
|
||||
# "connection" if not explicitly set.
|
||||
# status::
|
||||
# The HTTP status code as a String, returned as the Status header. The
|
||||
# values are:
|
||||
#
|
||||
# OK:: 200 OK
|
||||
# PARTIAL_CONTENT:: 206 Partial Content
|
||||
# MULTIPLE_CHOICES:: 300 Multiple Choices
|
||||
# MOVED:: 301 Moved Permanently
|
||||
# REDIRECT:: 302 Found
|
||||
# NOT_MODIFIED:: 304 Not Modified
|
||||
# BAD_REQUEST:: 400 Bad Request
|
||||
# AUTH_REQUIRED:: 401 Authorization Required
|
||||
# FORBIDDEN:: 403 Forbidden
|
||||
# NOT_FOUND:: 404 Not Found
|
||||
# METHOD_NOT_ALLOWED:: 405 Method Not Allowed
|
||||
# NOT_ACCEPTABLE:: 406 Not Acceptable
|
||||
# LENGTH_REQUIRED:: 411 Length Required
|
||||
# PRECONDITION_FAILED:: 412 Precondition Failed
|
||||
# SERVER_ERROR:: 500 Internal Server Error
|
||||
# NOT_IMPLEMENTED:: 501 Method Not Implemented
|
||||
# BAD_GATEWAY:: 502 Bad Gateway
|
||||
# VARIANT_ALSO_VARIES:: 506 Variant Also Negotiates
|
||||
#
|
||||
# server:: The server software, returned as the Server header.
|
||||
# connection:: The connection type, returned as the Connection header (for
|
||||
# instance, "close".
|
||||
# length:: The length of the content that will be sent, returned as the
|
||||
# Content-Length header.
|
||||
# language:: The language of the content, returned as the Content-Language
|
||||
# header.
|
||||
# expires:: The time on which the current content expires, as a +Time+
|
||||
# object, returned as the Expires header.
|
||||
# cookie::
|
||||
# A cookie or cookies, returned as one or more Set-Cookie headers. The
|
||||
# value can be the literal string of the cookie; a CGI::Cookie object;
|
||||
# an Array of literal cookie strings or Cookie objects; or a hash all of
|
||||
# whose values are literal cookie strings or Cookie objects.
|
||||
#
|
||||
# These cookies are in addition to the cookies held in the
|
||||
# @output_cookies field.
|
||||
#
|
||||
# Other headers can also be set; they are appended as key: value.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# http_header
|
||||
# # Content-Type: text/html
|
||||
#
|
||||
# http_header("text/plain")
|
||||
# # Content-Type: text/plain
|
||||
#
|
||||
# http_header("nph" => true,
|
||||
# "status" => "OK", # == "200 OK"
|
||||
# # "status" => "200 GOOD",
|
||||
# "server" => ENV['SERVER_SOFTWARE'],
|
||||
# "connection" => "close",
|
||||
# "type" => "text/html",
|
||||
# "charset" => "iso-2022-jp",
|
||||
# # Content-Type: text/html; charset=iso-2022-jp
|
||||
# "length" => 103,
|
||||
# "language" => "ja",
|
||||
# "expires" => Time.now + 30,
|
||||
# "cookie" => [cookie1, cookie2],
|
||||
# "my_header1" => "my_value"
|
||||
# "my_header2" => "my_value")
|
||||
#
|
||||
# This method does not perform charset conversion.
|
||||
def http_header(options='text/html')
|
||||
if options.is_a?(String)
|
||||
content_type = options
|
||||
buf = _header_for_string(content_type)
|
||||
elsif options.is_a?(Hash)
|
||||
if options.size == 1 && options.has_key?('type')
|
||||
content_type = options['type']
|
||||
buf = _header_for_string(content_type)
|
||||
else
|
||||
buf = _header_for_hash(options.dup)
|
||||
end
|
||||
else
|
||||
raise ArgumentError.new("expected String or Hash but got #{options.class}")
|
||||
end
|
||||
if defined?(MOD_RUBY)
|
||||
_header_for_modruby(buf)
|
||||
return ''
|
||||
else
|
||||
buf << EOL # empty line of separator
|
||||
return buf
|
||||
end
|
||||
end # http_header()
|
||||
|
||||
# This method is an alias for #http_header, when HTML5 tag maker is inactive.
|
||||
#
|
||||
# NOTE: use #http_header to create HTTP header blocks, this alias is only
|
||||
# provided for backwards compatibility.
|
||||
#
|
||||
# Using #header with the HTML5 tag maker will create a <header> element.
|
||||
alias :header :http_header
|
||||
|
||||
def _header_for_string(content_type) #:nodoc:
|
||||
buf = ''
|
||||
if nph?()
|
||||
buf << "#{$CGI_ENV['SERVER_PROTOCOL'] || 'HTTP/1.0'} 200 OK#{EOL}"
|
||||
buf << "Date: #{CGI.rfc1123_date(Time.now)}#{EOL}"
|
||||
buf << "Server: #{$CGI_ENV['SERVER_SOFTWARE']}#{EOL}"
|
||||
buf << "Connection: close#{EOL}"
|
||||
end
|
||||
buf << "Content-Type: #{content_type}#{EOL}"
|
||||
if @output_cookies
|
||||
@output_cookies.each {|cookie| buf << "Set-Cookie: #{cookie}#{EOL}" }
|
||||
end
|
||||
return buf
|
||||
end # _header_for_string
|
||||
private :_header_for_string
|
||||
|
||||
def _header_for_hash(options) #:nodoc:
|
||||
buf = ''
|
||||
## add charset to option['type']
|
||||
options['type'] ||= 'text/html'
|
||||
charset = options.delete('charset')
|
||||
options['type'] += "; charset=#{charset}" if charset
|
||||
## NPH
|
||||
options.delete('nph') if defined?(MOD_RUBY)
|
||||
if options.delete('nph') || nph?()
|
||||
protocol = $CGI_ENV['SERVER_PROTOCOL'] || 'HTTP/1.0'
|
||||
status = options.delete('status')
|
||||
status = HTTP_STATUS[status] || status || '200 OK'
|
||||
buf << "#{protocol} #{status}#{EOL}"
|
||||
buf << "Date: #{CGI.rfc1123_date(Time.now)}#{EOL}"
|
||||
options['server'] ||= $CGI_ENV['SERVER_SOFTWARE'] || ''
|
||||
options['connection'] ||= 'close'
|
||||
end
|
||||
## common headers
|
||||
status = options.delete('status')
|
||||
buf << "Status: #{HTTP_STATUS[status] || status}#{EOL}" if status
|
||||
server = options.delete('server')
|
||||
buf << "Server: #{server}#{EOL}" if server
|
||||
connection = options.delete('connection')
|
||||
buf << "Connection: #{connection}#{EOL}" if connection
|
||||
type = options.delete('type')
|
||||
buf << "Content-Type: #{type}#{EOL}" #if type
|
||||
length = options.delete('length')
|
||||
buf << "Content-Length: #{length}#{EOL}" if length
|
||||
language = options.delete('language')
|
||||
buf << "Content-Language: #{language}#{EOL}" if language
|
||||
expires = options.delete('expires')
|
||||
buf << "Expires: #{CGI.rfc1123_date(expires)}#{EOL}" if expires
|
||||
## cookie
|
||||
if cookie = options.delete('cookie')
|
||||
case cookie
|
||||
when String, Cookie
|
||||
buf << "Set-Cookie: #{cookie}#{EOL}"
|
||||
when Array
|
||||
arr = cookie
|
||||
arr.each {|c| buf << "Set-Cookie: #{c}#{EOL}" }
|
||||
when Hash
|
||||
hash = cookie
|
||||
hash.each_value {|c| buf << "Set-Cookie: #{c}#{EOL}" }
|
||||
end
|
||||
end
|
||||
if @output_cookies
|
||||
@output_cookies.each {|c| buf << "Set-Cookie: #{c}#{EOL}" }
|
||||
end
|
||||
## other headers
|
||||
options.each do |key, value|
|
||||
buf << "#{key}: #{value}#{EOL}"
|
||||
end
|
||||
return buf
|
||||
end # _header_for_hash
|
||||
private :_header_for_hash
|
||||
|
||||
def nph? #:nodoc:
|
||||
return /IIS\/(\d+)/.match($CGI_ENV['SERVER_SOFTWARE']) && $1.to_i < 5
|
||||
end
|
||||
|
||||
def _header_for_modruby(buf) #:nodoc:
|
||||
request = Apache::request
|
||||
buf.scan(/([^:]+): (.+)#{EOL}/o) do |name, value|
|
||||
warn sprintf("name:%s value:%s\n", name, value) if $DEBUG
|
||||
case name
|
||||
when 'Set-Cookie'
|
||||
request.headers_out.add(name, value)
|
||||
when /^status$/i
|
||||
request.status_line = value
|
||||
request.status = value.to_i
|
||||
when /^content-type$/i
|
||||
request.content_type = value
|
||||
when /^content-encoding$/i
|
||||
request.content_encoding = value
|
||||
when /^location$/i
|
||||
request.status = 302 if request.status == 200
|
||||
request.headers_out[name] = value
|
||||
else
|
||||
request.headers_out[name] = value
|
||||
end
|
||||
end
|
||||
request.send_http_header
|
||||
return ''
|
||||
end
|
||||
private :_header_for_modruby
|
||||
|
||||
# Print an HTTP header and body to $DEFAULT_OUTPUT ($>)
|
||||
#
|
||||
# :call-seq:
|
||||
# cgi.out(content_type_string='text/html')
|
||||
# cgi.out(headers_hash)
|
||||
#
|
||||
# +content_type_string+::
|
||||
# If a string is passed, it is assumed to be the content type.
|
||||
# +headers_hash+::
|
||||
# This is a Hash of headers, similar to that used by #http_header.
|
||||
# +block+::
|
||||
# A block is required and should evaluate to the body of the response.
|
||||
#
|
||||
# <tt>Content-Length</tt> is automatically calculated from the size of
|
||||
# the String returned by the content block.
|
||||
#
|
||||
# If <tt>ENV['REQUEST_METHOD'] == "HEAD"</tt>, then only the header
|
||||
# is output (the content block is still required, but it is ignored).
|
||||
#
|
||||
# If the charset is "iso-2022-jp" or "euc-jp" or "shift_jis" then the
|
||||
# content is converted to this charset, and the language is set to "ja".
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# cgi = CGI.new
|
||||
# cgi.out{ "string" }
|
||||
# # Content-Type: text/html
|
||||
# # Content-Length: 6
|
||||
# #
|
||||
# # string
|
||||
#
|
||||
# cgi.out("text/plain") { "string" }
|
||||
# # Content-Type: text/plain
|
||||
# # Content-Length: 6
|
||||
# #
|
||||
# # string
|
||||
#
|
||||
# cgi.out("nph" => true,
|
||||
# "status" => "OK", # == "200 OK"
|
||||
# "server" => ENV['SERVER_SOFTWARE'],
|
||||
# "connection" => "close",
|
||||
# "type" => "text/html",
|
||||
# "charset" => "iso-2022-jp",
|
||||
# # Content-Type: text/html; charset=iso-2022-jp
|
||||
# "language" => "ja",
|
||||
# "expires" => Time.now + (3600 * 24 * 30),
|
||||
# "cookie" => [cookie1, cookie2],
|
||||
# "my_header1" => "my_value",
|
||||
# "my_header2" => "my_value") { "string" }
|
||||
# # HTTP/1.1 200 OK
|
||||
# # Date: Sun, 15 May 2011 17:35:54 GMT
|
||||
# # Server: Apache 2.2.0
|
||||
# # Connection: close
|
||||
# # Content-Type: text/html; charset=iso-2022-jp
|
||||
# # Content-Length: 6
|
||||
# # Content-Language: ja
|
||||
# # Expires: Tue, 14 Jun 2011 17:35:54 GMT
|
||||
# # Set-Cookie: foo
|
||||
# # Set-Cookie: bar
|
||||
# # my_header1: my_value
|
||||
# # my_header2: my_value
|
||||
# #
|
||||
# # string
|
||||
def out(options = "text/html") # :yield:
|
||||
|
||||
options = { "type" => options } if options.kind_of?(String)
|
||||
content = yield
|
||||
options["length"] = content.bytesize.to_s
|
||||
output = stdoutput
|
||||
output.binmode if defined? output.binmode
|
||||
output.print http_header(options)
|
||||
output.print content unless "HEAD" == env_table['REQUEST_METHOD']
|
||||
end
|
||||
|
||||
|
||||
# Print an argument or list of arguments to the default output stream
|
||||
#
|
||||
# cgi = CGI.new
|
||||
# cgi.print # default: cgi.print == $DEFAULT_OUTPUT.print
|
||||
def print(*options)
|
||||
stdoutput.print(*options)
|
||||
end
|
||||
|
||||
# Parse an HTTP query string into a hash of key=>value pairs.
|
||||
#
|
||||
# params = CGI::parse("query_string")
|
||||
# # {"name1" => ["value1", "value2", ...],
|
||||
# # "name2" => ["value1", "value2", ...], ... }
|
||||
#
|
||||
def CGI::parse(query)
|
||||
params = {}
|
||||
query.split(/[&;]/).each do |pairs|
|
||||
key, value = pairs.split('=',2).collect{|v| CGI::unescape(v) }
|
||||
|
||||
next unless key
|
||||
|
||||
params[key] ||= []
|
||||
params[key].push(value) if value
|
||||
end
|
||||
|
||||
params.default=[].freeze
|
||||
params
|
||||
end
|
||||
|
||||
# Maximum content length of post data
|
||||
##MAX_CONTENT_LENGTH = 2 * 1024 * 1024
|
||||
|
||||
# Maximum content length of multipart data
|
||||
MAX_MULTIPART_LENGTH = 128 * 1024 * 1024
|
||||
|
||||
# Maximum number of request parameters when multipart
|
||||
MAX_MULTIPART_COUNT = 128
|
||||
|
||||
# Mixin module that provides the following:
|
||||
#
|
||||
# 1. Access to the CGI environment variables as methods. See
|
||||
# documentation to the CGI class for a list of these variables. The
|
||||
# methods are exposed by removing the leading +HTTP_+ (if it exists) and
|
||||
# downcasing the name. For example, +auth_type+ will return the
|
||||
# environment variable +AUTH_TYPE+, and +accept+ will return the value
|
||||
# for +HTTP_ACCEPT+.
|
||||
#
|
||||
# 2. Access to cookies, including the cookies attribute.
|
||||
#
|
||||
# 3. Access to parameters, including the params attribute, and overloading
|
||||
# #[] to perform parameter value lookup by key.
|
||||
#
|
||||
# 4. The initialize_query method, for initializing the above
|
||||
# mechanisms, handling multipart forms, and allowing the
|
||||
# class to be used in "offline" mode.
|
||||
#
|
||||
module QueryExtension
|
||||
|
||||
%w[ CONTENT_LENGTH SERVER_PORT ].each do |env|
|
||||
define_method(env.sub(/^HTTP_/, '').downcase) do
|
||||
(val = env_table[env]) && Integer(val)
|
||||
end
|
||||
end
|
||||
|
||||
%w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
|
||||
PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
|
||||
REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME
|
||||
SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE
|
||||
|
||||
HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
|
||||
HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
|
||||
HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ].each do |env|
|
||||
define_method(env.sub(/^HTTP_/, '').downcase) do
|
||||
env_table[env]
|
||||
end
|
||||
end
|
||||
|
||||
# Get the raw cookies as a string.
|
||||
def raw_cookie
|
||||
env_table["HTTP_COOKIE"]
|
||||
end
|
||||
|
||||
# Get the raw RFC2965 cookies as a string.
|
||||
def raw_cookie2
|
||||
env_table["HTTP_COOKIE2"]
|
||||
end
|
||||
|
||||
# Get the cookies as a hash of cookie-name=>Cookie pairs.
|
||||
attr_accessor :cookies
|
||||
|
||||
# Get the parameters as a hash of name=>values pairs, where
|
||||
# values is an Array.
|
||||
attr_reader :params
|
||||
|
||||
# Get the uploaded files as a hash of name=>values pairs
|
||||
attr_reader :files
|
||||
|
||||
# Set all the parameters.
|
||||
def params=(hash)
|
||||
@params.clear
|
||||
@params.update(hash)
|
||||
end
|
||||
|
||||
##
|
||||
# Parses multipart form elements according to
|
||||
# http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
|
||||
#
|
||||
# Returns a hash of multipart form parameters with bodies of type StringIO or
|
||||
# Tempfile depending on whether the multipart form element exceeds 10 KB
|
||||
#
|
||||
# params[name => body]
|
||||
#
|
||||
def read_multipart(boundary, content_length)
|
||||
## read first boundary
|
||||
stdin = stdinput
|
||||
first_line = "--#{boundary}#{EOL}"
|
||||
content_length -= first_line.bytesize
|
||||
status = stdin.read(first_line.bytesize)
|
||||
raise EOFError.new("no content body") unless status
|
||||
raise EOFError.new("bad content body") unless first_line == status
|
||||
## parse and set params
|
||||
params = {}
|
||||
@files = {}
|
||||
boundary_rexp = /--#{Regexp.quote(boundary)}(#{EOL}|--)/
|
||||
boundary_size = "#{EOL}--#{boundary}#{EOL}".bytesize
|
||||
boundary_end = nil
|
||||
buf = ''
|
||||
bufsize = 10 * 1024
|
||||
max_count = MAX_MULTIPART_COUNT
|
||||
n = 0
|
||||
tempfiles = []
|
||||
while true
|
||||
(n += 1) < max_count or raise StandardError.new("too many parameters.")
|
||||
## create body (StringIO or Tempfile)
|
||||
body = create_body(bufsize < content_length)
|
||||
tempfiles << body if defined?(Tempfile) && body.kind_of?(Tempfile)
|
||||
class << body
|
||||
if method_defined?(:path)
|
||||
alias local_path path
|
||||
else
|
||||
def local_path
|
||||
nil
|
||||
end
|
||||
end
|
||||
attr_reader :original_filename, :content_type
|
||||
end
|
||||
## find head and boundary
|
||||
head = nil
|
||||
separator = EOL * 2
|
||||
until head && matched = boundary_rexp.match(buf)
|
||||
if !head && pos = buf.index(separator)
|
||||
len = pos + EOL.bytesize
|
||||
head = buf[0, len]
|
||||
buf = buf[(pos+separator.bytesize)..-1]
|
||||
else
|
||||
if head && buf.size > boundary_size
|
||||
len = buf.size - boundary_size
|
||||
body.print(buf[0, len])
|
||||
buf[0, len] = ''
|
||||
end
|
||||
c = stdin.read(bufsize < content_length ? bufsize : content_length)
|
||||
raise EOFError.new("bad content body") if c.nil? || c.empty?
|
||||
buf << c
|
||||
content_length -= c.bytesize
|
||||
end
|
||||
end
|
||||
## read to end of boundary
|
||||
m = matched
|
||||
len = m.begin(0)
|
||||
s = buf[0, len]
|
||||
if s =~ /(\r?\n)\z/
|
||||
s = buf[0, len - $1.bytesize]
|
||||
end
|
||||
body.print(s)
|
||||
buf = buf[m.end(0)..-1]
|
||||
boundary_end = m[1]
|
||||
content_length = -1 if boundary_end == '--'
|
||||
## reset file cursor position
|
||||
body.rewind
|
||||
## original filename
|
||||
/Content-Disposition:.* filename=(?:"(.*?)"|([^;\r\n]*))/i.match(head)
|
||||
filename = $1 || $2 || ''
|
||||
filename = CGI.unescape(filename) if unescape_filename?()
|
||||
body.instance_variable_set(:@original_filename, filename.taint)
|
||||
## content type
|
||||
/Content-Type: (.*)/i.match(head)
|
||||
(content_type = $1 || '').chomp!
|
||||
body.instance_variable_set(:@content_type, content_type.taint)
|
||||
## query parameter name
|
||||
/Content-Disposition:.* name=(?:"(.*?)"|([^;\r\n]*))/i.match(head)
|
||||
name = $1 || $2 || ''
|
||||
if body.original_filename.empty?
|
||||
value=body.read.dup.force_encoding(@accept_charset)
|
||||
body.unlink if defined?(Tempfile) && body.kind_of?(Tempfile)
|
||||
(params[name] ||= []) << value
|
||||
unless value.valid_encoding?
|
||||
if @accept_charset_error_block
|
||||
@accept_charset_error_block.call(name,value)
|
||||
else
|
||||
raise InvalidEncoding,"Accept-Charset encoding error"
|
||||
end
|
||||
end
|
||||
class << params[name].last;self;end.class_eval do
|
||||
define_method(:read){self}
|
||||
define_method(:original_filename){""}
|
||||
define_method(:content_type){""}
|
||||
end
|
||||
else
|
||||
(params[name] ||= []) << body
|
||||
@files[name]=body
|
||||
end
|
||||
## break loop
|
||||
break if content_length == -1
|
||||
end
|
||||
raise EOFError, "bad boundary end of body part" unless boundary_end =~ /--/
|
||||
params.default = []
|
||||
params
|
||||
rescue Exception
|
||||
if tempfiles
|
||||
tempfiles.each {|t|
|
||||
if t.path
|
||||
t.unlink
|
||||
end
|
||||
}
|
||||
end
|
||||
raise
|
||||
end # read_multipart
|
||||
private :read_multipart
|
||||
def create_body(is_large) #:nodoc:
|
||||
if is_large
|
||||
require 'tempfile'
|
||||
body = Tempfile.new('CGI', encoding: Encoding::ASCII_8BIT)
|
||||
else
|
||||
begin
|
||||
require 'stringio'
|
||||
body = StringIO.new("".force_encoding(Encoding::ASCII_8BIT))
|
||||
rescue LoadError
|
||||
require 'tempfile'
|
||||
body = Tempfile.new('CGI', encoding: Encoding::ASCII_8BIT)
|
||||
end
|
||||
end
|
||||
body.binmode if defined? body.binmode
|
||||
return body
|
||||
end
|
||||
def unescape_filename? #:nodoc:
|
||||
user_agent = $CGI_ENV['HTTP_USER_AGENT']
|
||||
return /Mac/i.match(user_agent) && /Mozilla/i.match(user_agent) && !/MSIE/i.match(user_agent)
|
||||
end
|
||||
|
||||
# offline mode. read name=value pairs on standard input.
|
||||
def read_from_cmdline
|
||||
require "shellwords"
|
||||
|
||||
string = unless ARGV.empty?
|
||||
ARGV.join(' ')
|
||||
else
|
||||
if STDIN.tty?
|
||||
STDERR.print(
|
||||
%|(offline mode: enter name=value pairs on standard input)\n|
|
||||
)
|
||||
end
|
||||
array = readlines rescue nil
|
||||
if not array.nil?
|
||||
array.join(' ').gsub(/\n/n, '')
|
||||
else
|
||||
""
|
||||
end
|
||||
end.gsub(/\\=/n, '%3D').gsub(/\\&/n, '%26')
|
||||
|
||||
words = Shellwords.shellwords(string)
|
||||
|
||||
if words.find{|x| /=/n.match(x) }
|
||||
words.join('&')
|
||||
else
|
||||
words.join('+')
|
||||
end
|
||||
end
|
||||
private :read_from_cmdline
|
||||
|
||||
# A wrapper class to use a StringIO object as the body and switch
|
||||
# to a TempFile when the passed threshold is passed.
|
||||
# Initialize the data from the query.
|
||||
#
|
||||
# Handles multipart forms (in particular, forms that involve file uploads).
|
||||
# Reads query parameters in the @params field, and cookies into @cookies.
|
||||
def initialize_query()
|
||||
if ("POST" == env_table['REQUEST_METHOD']) and
|
||||
%r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|.match(env_table['CONTENT_TYPE'])
|
||||
raise StandardError.new("too large multipart data.") if env_table['CONTENT_LENGTH'].to_i > MAX_MULTIPART_LENGTH
|
||||
boundary = $1.dup
|
||||
@multipart = true
|
||||
@params = read_multipart(boundary, Integer(env_table['CONTENT_LENGTH']))
|
||||
else
|
||||
@multipart = false
|
||||
@params = CGI::parse(
|
||||
case env_table['REQUEST_METHOD']
|
||||
when "GET", "HEAD"
|
||||
if defined?(MOD_RUBY)
|
||||
Apache::request.args or ""
|
||||
else
|
||||
env_table['QUERY_STRING'] or ""
|
||||
end
|
||||
when "POST"
|
||||
stdinput.binmode if defined? stdinput.binmode
|
||||
stdinput.read(Integer(env_table['CONTENT_LENGTH'])) or ''
|
||||
else
|
||||
read_from_cmdline
|
||||
end.dup.force_encoding(@accept_charset)
|
||||
)
|
||||
unless Encoding.find(@accept_charset) == Encoding::ASCII_8BIT
|
||||
@params.each do |key,values|
|
||||
values.each do |value|
|
||||
unless value.valid_encoding?
|
||||
if @accept_charset_error_block
|
||||
@accept_charset_error_block.call(key,value)
|
||||
else
|
||||
raise InvalidEncoding,"Accept-Charset encoding error"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@cookies = CGI::Cookie::parse((env_table['HTTP_COOKIE'] or env_table['COOKIE']))
|
||||
end
|
||||
private :initialize_query
|
||||
|
||||
# Returns whether the form contained multipart/form-data
|
||||
def multipart?
|
||||
@multipart
|
||||
end
|
||||
|
||||
# Get the value for the parameter with a given key.
|
||||
#
|
||||
# If the parameter has multiple values, only the first will be
|
||||
# retrieved; use #params to get the array of values.
|
||||
def [](key)
|
||||
params = @params[key]
|
||||
return '' unless params
|
||||
value = params[0]
|
||||
if @multipart
|
||||
if value
|
||||
return value
|
||||
elsif defined? StringIO
|
||||
StringIO.new("".force_encoding(Encoding::ASCII_8BIT))
|
||||
else
|
||||
Tempfile.new("CGI",encoding: Encoding::ASCII_8BIT)
|
||||
end
|
||||
else
|
||||
str = if value then value.dup else "" end
|
||||
str
|
||||
end
|
||||
end
|
||||
|
||||
# Return all query parameter names as an array of String.
|
||||
def keys(*args)
|
||||
@params.keys(*args)
|
||||
end
|
||||
|
||||
# Returns true if a given query string parameter exists.
|
||||
def has_key?(*args)
|
||||
@params.has_key?(*args)
|
||||
end
|
||||
alias key? has_key?
|
||||
alias include? has_key?
|
||||
|
||||
end # QueryExtension
|
||||
|
||||
# Exception raised when there is an invalid encoding detected
|
||||
class InvalidEncoding < Exception; end
|
||||
|
||||
# @@accept_charset is default accept character set.
|
||||
# This default value default is "UTF-8"
|
||||
# If you want to change the default accept character set
|
||||
# when create a new CGI instance, set this:
|
||||
#
|
||||
# CGI.accept_charset = "EUC-JP"
|
||||
#
|
||||
@@accept_charset="UTF-8"
|
||||
|
||||
# Return the accept character set for all new CGI instances.
|
||||
def self.accept_charset
|
||||
@@accept_charset
|
||||
end
|
||||
|
||||
# Set the accept character set for all new CGI instances.
|
||||
def self.accept_charset=(accept_charset)
|
||||
@@accept_charset=accept_charset
|
||||
end
|
||||
|
||||
# Return the accept character set for this CGI instance.
|
||||
attr_reader :accept_charset
|
||||
|
||||
# Create a new CGI instance.
|
||||
#
|
||||
# :call-seq:
|
||||
# CGI.new(tag_maker) { block }
|
||||
# CGI.new(options_hash = {}) { block }
|
||||
#
|
||||
#
|
||||
# <tt>tag_maker</tt>::
|
||||
# This is the same as using the +options_hash+ form with the value <tt>{
|
||||
# :tag_maker => tag_maker }</tt> Note that it is recommended to use the
|
||||
# +options_hash+ form, since it also allows you specify the charset you
|
||||
# will accept.
|
||||
# <tt>options_hash</tt>::
|
||||
# A Hash that recognizes two options:
|
||||
#
|
||||
# <tt>:accept_charset</tt>::
|
||||
# specifies encoding of received query string. If omitted,
|
||||
# <tt>@@accept_charset</tt> is used. If the encoding is not valid, a
|
||||
# CGI::InvalidEncoding will be raised.
|
||||
#
|
||||
# Example. Suppose <tt>@@accept_charset</tt> is "UTF-8"
|
||||
#
|
||||
# when not specified:
|
||||
#
|
||||
# cgi=CGI.new # @accept_charset # => "UTF-8"
|
||||
#
|
||||
# when specified as "EUC-JP":
|
||||
#
|
||||
# cgi=CGI.new(:accept_charset => "EUC-JP") # => "EUC-JP"
|
||||
#
|
||||
# <tt>:tag_maker</tt>::
|
||||
# String that specifies which version of the HTML generation methods to
|
||||
# use. If not specified, no HTML generation methods will be loaded.
|
||||
#
|
||||
# The following values are supported:
|
||||
#
|
||||
# "html3":: HTML 3.x
|
||||
# "html4":: HTML 4.0
|
||||
# "html4Tr":: HTML 4.0 Transitional
|
||||
# "html4Fr":: HTML 4.0 with Framesets
|
||||
# "html5":: HTML 5
|
||||
#
|
||||
# <tt>block</tt>::
|
||||
# If provided, the block is called when an invalid encoding is
|
||||
# encountered. For example:
|
||||
#
|
||||
# encoding_errors={}
|
||||
# cgi=CGI.new(:accept_charset=>"EUC-JP") do |name,value|
|
||||
# encoding_errors[name] = value
|
||||
# end
|
||||
#
|
||||
# Finally, if the CGI object is not created in a standard CGI call
|
||||
# environment (that is, it can't locate REQUEST_METHOD in its environment),
|
||||
# then it will run in "offline" mode. In this mode, it reads its parameters
|
||||
# from the command line or (failing that) from standard input. Otherwise,
|
||||
# cookies and other parameters are parsed automatically from the standard
|
||||
# CGI locations, which varies according to the REQUEST_METHOD.
|
||||
def initialize(options = {}, &block) # :yields: name, value
|
||||
@accept_charset_error_block = block_given? ? block : nil
|
||||
@options={:accept_charset=>@@accept_charset}
|
||||
case options
|
||||
when Hash
|
||||
@options.merge!(options)
|
||||
when String
|
||||
@options[:tag_maker]=options
|
||||
end
|
||||
@accept_charset=@options[:accept_charset]
|
||||
if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE")
|
||||
Apache.request.setup_cgi_env
|
||||
end
|
||||
|
||||
extend QueryExtension
|
||||
@multipart = false
|
||||
|
||||
initialize_query() # set @params, @cookies
|
||||
@output_cookies = nil
|
||||
@output_hidden = nil
|
||||
|
||||
case @options[:tag_maker]
|
||||
when "html3"
|
||||
require 'cgi/html'
|
||||
extend Html3
|
||||
extend HtmlExtension
|
||||
when "html4"
|
||||
require 'cgi/html'
|
||||
extend Html4
|
||||
extend HtmlExtension
|
||||
when "html4Tr"
|
||||
require 'cgi/html'
|
||||
extend Html4Tr
|
||||
extend HtmlExtension
|
||||
when "html4Fr"
|
||||
require 'cgi/html'
|
||||
extend Html4Tr
|
||||
extend Html4Fr
|
||||
extend HtmlExtension
|
||||
when "html5"
|
||||
require 'cgi/html'
|
||||
extend Html5
|
||||
extend HtmlExtension
|
||||
end
|
||||
end
|
||||
|
||||
end # class CGI
|
||||
|
||||
|
||||
1034
ruby/lib/ruby/2.1.0/cgi/html.rb
Normal file
1034
ruby/lib/ruby/2.1.0/cgi/html.rb
Normal file
File diff suppressed because it is too large
Load Diff
531
ruby/lib/ruby/2.1.0/cgi/session.rb
Normal file
531
ruby/lib/ruby/2.1.0/cgi/session.rb
Normal file
@@ -0,0 +1,531 @@
|
||||
#
|
||||
# cgi/session.rb - session support for cgi scripts
|
||||
#
|
||||
# Copyright (C) 2001 Yukihiro "Matz" Matsumoto
|
||||
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
||||
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
||||
#
|
||||
# Author: Yukihiro "Matz" Matsumoto
|
||||
#
|
||||
# Documentation: William Webber (william@williamwebber.com)
|
||||
|
||||
require 'cgi'
|
||||
require 'tmpdir'
|
||||
|
||||
class CGI
|
||||
|
||||
# == Overview
|
||||
#
|
||||
# This file provides the CGI::Session class, which provides session
|
||||
# support for CGI scripts. A session is a sequence of HTTP requests
|
||||
# and responses linked together and associated with a single client.
|
||||
# Information associated with the session is stored
|
||||
# on the server between requests. A session id is passed between client
|
||||
# and server with every request and response, transparently
|
||||
# to the user. This adds state information to the otherwise stateless
|
||||
# HTTP request/response protocol.
|
||||
#
|
||||
# == Lifecycle
|
||||
#
|
||||
# A CGI::Session instance is created from a CGI object. By default,
|
||||
# this CGI::Session instance will start a new session if none currently
|
||||
# exists, or continue the current session for this client if one does
|
||||
# exist. The +new_session+ option can be used to either always or
|
||||
# never create a new session. See #new() for more details.
|
||||
#
|
||||
# #delete() deletes a session from session storage. It
|
||||
# does not however remove the session id from the client. If the client
|
||||
# makes another request with the same id, the effect will be to start
|
||||
# a new session with the old session's id.
|
||||
#
|
||||
# == Setting and retrieving session data.
|
||||
#
|
||||
# The Session class associates data with a session as key-value pairs.
|
||||
# This data can be set and retrieved by indexing the Session instance
|
||||
# using '[]', much the same as hashes (although other hash methods
|
||||
# are not supported).
|
||||
#
|
||||
# When session processing has been completed for a request, the
|
||||
# session should be closed using the close() method. This will
|
||||
# store the session's state to persistent storage. If you want
|
||||
# to store the session's state to persistent storage without
|
||||
# finishing session processing for this request, call the update()
|
||||
# method.
|
||||
#
|
||||
# == Storing session state
|
||||
#
|
||||
# The caller can specify what form of storage to use for the session's
|
||||
# data with the +database_manager+ option to CGI::Session::new. The
|
||||
# following storage classes are provided as part of the standard library:
|
||||
#
|
||||
# CGI::Session::FileStore:: stores data as plain text in a flat file. Only
|
||||
# works with String data. This is the default
|
||||
# storage type.
|
||||
# CGI::Session::MemoryStore:: stores data in an in-memory hash. The data
|
||||
# only persists for as long as the current Ruby
|
||||
# interpreter instance does.
|
||||
# CGI::Session::PStore:: stores data in Marshalled format. Provided by
|
||||
# cgi/session/pstore.rb. Supports data of any type,
|
||||
# and provides file-locking and transaction support.
|
||||
#
|
||||
# Custom storage types can also be created by defining a class with
|
||||
# the following methods:
|
||||
#
|
||||
# new(session, options)
|
||||
# restore # returns hash of session data.
|
||||
# update
|
||||
# close
|
||||
# delete
|
||||
#
|
||||
# Changing storage type mid-session does not work. Note in particular
|
||||
# that by default the FileStore and PStore session data files have the
|
||||
# same name. If your application switches from one to the other without
|
||||
# making sure that filenames will be different
|
||||
# and clients still have old sessions lying around in cookies, then
|
||||
# things will break nastily!
|
||||
#
|
||||
# == Maintaining the session id.
|
||||
#
|
||||
# Most session state is maintained on the server. However, a session
|
||||
# id must be passed backwards and forwards between client and server
|
||||
# to maintain a reference to this session state.
|
||||
#
|
||||
# The simplest way to do this is via cookies. The CGI::Session class
|
||||
# provides transparent support for session id communication via cookies
|
||||
# if the client has cookies enabled.
|
||||
#
|
||||
# If the client has cookies disabled, the session id must be included
|
||||
# as a parameter of all requests sent by the client to the server. The
|
||||
# CGI::Session class in conjunction with the CGI class will transparently
|
||||
# add the session id as a hidden input field to all forms generated
|
||||
# using the CGI#form() HTML generation method. No built-in support is
|
||||
# provided for other mechanisms, such as URL re-writing. The caller is
|
||||
# responsible for extracting the session id from the session_id
|
||||
# attribute and manually encoding it in URLs and adding it as a hidden
|
||||
# input to HTML forms created by other mechanisms. Also, session expiry
|
||||
# is not automatically handled.
|
||||
#
|
||||
# == Examples of use
|
||||
#
|
||||
# === Setting the user's name
|
||||
#
|
||||
# require 'cgi'
|
||||
# require 'cgi/session'
|
||||
# require 'cgi/session/pstore' # provides CGI::Session::PStore
|
||||
#
|
||||
# cgi = CGI.new("html4")
|
||||
#
|
||||
# session = CGI::Session.new(cgi,
|
||||
# 'database_manager' => CGI::Session::PStore, # use PStore
|
||||
# 'session_key' => '_rb_sess_id', # custom session key
|
||||
# 'session_expires' => Time.now + 30 * 60, # 30 minute timeout
|
||||
# 'prefix' => 'pstore_sid_') # PStore option
|
||||
# if cgi.has_key?('user_name') and cgi['user_name'] != ''
|
||||
# # coerce to String: cgi[] returns the
|
||||
# # string-like CGI::QueryExtension::Value
|
||||
# session['user_name'] = cgi['user_name'].to_s
|
||||
# elsif !session['user_name']
|
||||
# session['user_name'] = "guest"
|
||||
# end
|
||||
# session.close
|
||||
#
|
||||
# === Creating a new session safely
|
||||
#
|
||||
# require 'cgi'
|
||||
# require 'cgi/session'
|
||||
#
|
||||
# cgi = CGI.new("html4")
|
||||
#
|
||||
# # We make sure to delete an old session if one exists,
|
||||
# # not just to free resources, but to prevent the session
|
||||
# # from being maliciously hijacked later on.
|
||||
# begin
|
||||
# session = CGI::Session.new(cgi, 'new_session' => false)
|
||||
# session.delete
|
||||
# rescue ArgumentError # if no old session
|
||||
# end
|
||||
# session = CGI::Session.new(cgi, 'new_session' => true)
|
||||
# session.close
|
||||
#
|
||||
class Session
|
||||
|
||||
class NoSession < RuntimeError #:nodoc:
|
||||
end
|
||||
|
||||
# The id of this session.
|
||||
attr_reader :session_id, :new_session
|
||||
|
||||
def Session::callback(dbman) #:nodoc:
|
||||
Proc.new{
|
||||
dbman[0].close unless dbman.empty?
|
||||
}
|
||||
end
|
||||
|
||||
# Create a new session id.
|
||||
#
|
||||
# The session id is an MD5 hash based upon the time,
|
||||
# a random number, and a constant string. This routine
|
||||
# is used internally for automatically generated
|
||||
# session ids.
|
||||
def create_new_id
|
||||
require 'securerandom'
|
||||
begin
|
||||
session_id = SecureRandom.hex(16)
|
||||
rescue NotImplementedError
|
||||
require 'digest/md5'
|
||||
md5 = Digest::MD5::new
|
||||
now = Time::now
|
||||
md5.update(now.to_s)
|
||||
md5.update(String(now.usec))
|
||||
md5.update(String(rand(0)))
|
||||
md5.update(String($$))
|
||||
md5.update('foobar')
|
||||
session_id = md5.hexdigest
|
||||
end
|
||||
session_id
|
||||
end
|
||||
private :create_new_id
|
||||
|
||||
# Create a new CGI::Session object for +request+.
|
||||
#
|
||||
# +request+ is an instance of the +CGI+ class (see cgi.rb).
|
||||
# +option+ is a hash of options for initialising this
|
||||
# CGI::Session instance. The following options are
|
||||
# recognised:
|
||||
#
|
||||
# session_key:: the parameter name used for the session id.
|
||||
# Defaults to '_session_id'.
|
||||
# session_id:: the session id to use. If not provided, then
|
||||
# it is retrieved from the +session_key+ parameter
|
||||
# of the request, or automatically generated for
|
||||
# a new session.
|
||||
# new_session:: if true, force creation of a new session. If not set,
|
||||
# a new session is only created if none currently
|
||||
# exists. If false, a new session is never created,
|
||||
# and if none currently exists and the +session_id+
|
||||
# option is not set, an ArgumentError is raised.
|
||||
# database_manager:: the name of the class providing storage facilities
|
||||
# for session state persistence. Built-in support
|
||||
# is provided for +FileStore+ (the default),
|
||||
# +MemoryStore+, and +PStore+ (from
|
||||
# cgi/session/pstore.rb). See the documentation for
|
||||
# these classes for more details.
|
||||
#
|
||||
# The following options are also recognised, but only apply if the
|
||||
# session id is stored in a cookie.
|
||||
#
|
||||
# session_expires:: the time the current session expires, as a
|
||||
# +Time+ object. If not set, the session will terminate
|
||||
# when the user's browser is closed.
|
||||
# session_domain:: the hostname domain for which this session is valid.
|
||||
# If not set, defaults to the hostname of the server.
|
||||
# session_secure:: if +true+, this session will only work over HTTPS.
|
||||
# session_path:: the path for which this session applies. Defaults
|
||||
# to the directory of the CGI script.
|
||||
#
|
||||
# +option+ is also passed on to the session storage class initializer; see
|
||||
# the documentation for each session storage class for the options
|
||||
# they support.
|
||||
#
|
||||
# The retrieved or created session is automatically added to +request+
|
||||
# as a cookie, and also to its +output_hidden+ table, which is used
|
||||
# to add hidden input elements to forms.
|
||||
#
|
||||
# *WARNING* the +output_hidden+
|
||||
# fields are surrounded by a <fieldset> tag in HTML 4 generation, which
|
||||
# is _not_ invisible on many browsers; you may wish to disable the
|
||||
# use of fieldsets with code similar to the following
|
||||
# (see http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/37805)
|
||||
#
|
||||
# cgi = CGI.new("html4")
|
||||
# class << cgi
|
||||
# undef_method :fieldset
|
||||
# end
|
||||
#
|
||||
def initialize(request, option={})
|
||||
@new_session = false
|
||||
session_key = option['session_key'] || '_session_id'
|
||||
session_id = option['session_id']
|
||||
unless session_id
|
||||
if option['new_session']
|
||||
session_id = create_new_id
|
||||
@new_session = true
|
||||
end
|
||||
end
|
||||
unless session_id
|
||||
if request.key?(session_key)
|
||||
session_id = request[session_key]
|
||||
session_id = session_id.read if session_id.respond_to?(:read)
|
||||
end
|
||||
unless session_id
|
||||
session_id, = request.cookies[session_key]
|
||||
end
|
||||
unless session_id
|
||||
unless option.fetch('new_session', true)
|
||||
raise ArgumentError, "session_key `%s' should be supplied"%session_key
|
||||
end
|
||||
session_id = create_new_id
|
||||
@new_session = true
|
||||
end
|
||||
end
|
||||
@session_id = session_id
|
||||
dbman = option['database_manager'] || FileStore
|
||||
begin
|
||||
@dbman = dbman::new(self, option)
|
||||
rescue NoSession
|
||||
unless option.fetch('new_session', true)
|
||||
raise ArgumentError, "invalid session_id `%s'"%session_id
|
||||
end
|
||||
session_id = @session_id = create_new_id unless session_id
|
||||
@new_session=true
|
||||
retry
|
||||
end
|
||||
request.instance_eval do
|
||||
@output_hidden = {session_key => session_id} unless option['no_hidden']
|
||||
@output_cookies = [
|
||||
Cookie::new("name" => session_key,
|
||||
"value" => session_id,
|
||||
"expires" => option['session_expires'],
|
||||
"domain" => option['session_domain'],
|
||||
"secure" => option['session_secure'],
|
||||
"path" =>
|
||||
if option['session_path']
|
||||
option['session_path']
|
||||
elsif ENV["SCRIPT_NAME"]
|
||||
File::dirname(ENV["SCRIPT_NAME"])
|
||||
else
|
||||
""
|
||||
end)
|
||||
] unless option['no_cookies']
|
||||
end
|
||||
@dbprot = [@dbman]
|
||||
ObjectSpace::define_finalizer(self, Session::callback(@dbprot))
|
||||
end
|
||||
|
||||
# Retrieve the session data for key +key+.
|
||||
def [](key)
|
||||
@data ||= @dbman.restore
|
||||
@data[key]
|
||||
end
|
||||
|
||||
# Set the session data for key +key+.
|
||||
def []=(key, val)
|
||||
@write_lock ||= true
|
||||
@data ||= @dbman.restore
|
||||
@data[key] = val
|
||||
end
|
||||
|
||||
# Store session data on the server. For some session storage types,
|
||||
# this is a no-op.
|
||||
def update
|
||||
@dbman.update
|
||||
end
|
||||
|
||||
# Store session data on the server and close the session storage.
|
||||
# For some session storage types, this is a no-op.
|
||||
def close
|
||||
@dbman.close
|
||||
@dbprot.clear
|
||||
end
|
||||
|
||||
# Delete the session from storage. Also closes the storage.
|
||||
#
|
||||
# Note that the session's data is _not_ automatically deleted
|
||||
# upon the session expiring.
|
||||
def delete
|
||||
@dbman.delete
|
||||
@dbprot.clear
|
||||
end
|
||||
|
||||
# File-based session storage class.
|
||||
#
|
||||
# Implements session storage as a flat file of 'key=value' values.
|
||||
# This storage type only works directly with String values; the
|
||||
# user is responsible for converting other types to Strings when
|
||||
# storing and from Strings when retrieving.
|
||||
class FileStore
|
||||
# Create a new FileStore instance.
|
||||
#
|
||||
# This constructor is used internally by CGI::Session. The
|
||||
# user does not generally need to call it directly.
|
||||
#
|
||||
# +session+ is the session for which this instance is being
|
||||
# created. The session id must only contain alphanumeric
|
||||
# characters; automatically generated session ids observe
|
||||
# this requirement.
|
||||
#
|
||||
# +option+ is a hash of options for the initializer. The
|
||||
# following options are recognised:
|
||||
#
|
||||
# tmpdir:: the directory to use for storing the FileStore
|
||||
# file. Defaults to Dir::tmpdir (generally "/tmp"
|
||||
# on Unix systems).
|
||||
# prefix:: the prefix to add to the session id when generating
|
||||
# the filename for this session's FileStore file.
|
||||
# Defaults to "cgi_sid_".
|
||||
# suffix:: the prefix to add to the session id when generating
|
||||
# the filename for this session's FileStore file.
|
||||
# Defaults to the empty string.
|
||||
#
|
||||
# This session's FileStore file will be created if it does
|
||||
# not exist, or opened if it does.
|
||||
def initialize(session, option={})
|
||||
dir = option['tmpdir'] || Dir::tmpdir
|
||||
prefix = option['prefix'] || 'cgi_sid_'
|
||||
suffix = option['suffix'] || ''
|
||||
id = session.session_id
|
||||
require 'digest/md5'
|
||||
md5 = Digest::MD5.hexdigest(id)[0,16]
|
||||
@path = dir+"/"+prefix+md5+suffix
|
||||
if File::exist? @path
|
||||
@hash = nil
|
||||
else
|
||||
unless session.new_session
|
||||
raise CGI::Session::NoSession, "uninitialized session"
|
||||
end
|
||||
@hash = {}
|
||||
end
|
||||
end
|
||||
|
||||
# Restore session state from the session's FileStore file.
|
||||
#
|
||||
# Returns the session state as a hash.
|
||||
def restore
|
||||
unless @hash
|
||||
@hash = {}
|
||||
begin
|
||||
lockf = File.open(@path+".lock", "r")
|
||||
lockf.flock File::LOCK_SH
|
||||
f = File.open(@path, 'r')
|
||||
for line in f
|
||||
line.chomp!
|
||||
k, v = line.split('=',2)
|
||||
@hash[CGI::unescape(k)] = Marshal.restore(CGI::unescape(v))
|
||||
end
|
||||
ensure
|
||||
f.close unless f.nil?
|
||||
lockf.close if lockf
|
||||
end
|
||||
end
|
||||
@hash
|
||||
end
|
||||
|
||||
# Save session state to the session's FileStore file.
|
||||
def update
|
||||
return unless @hash
|
||||
begin
|
||||
lockf = File.open(@path+".lock", File::CREAT|File::RDWR, 0600)
|
||||
lockf.flock File::LOCK_EX
|
||||
f = File.open(@path+".new", File::CREAT|File::TRUNC|File::WRONLY, 0600)
|
||||
for k,v in @hash
|
||||
f.printf "%s=%s\n", CGI::escape(k), CGI::escape(String(Marshal.dump(v)))
|
||||
end
|
||||
f.close
|
||||
File.rename @path+".new", @path
|
||||
ensure
|
||||
f.close if f and !f.closed?
|
||||
lockf.close if lockf
|
||||
end
|
||||
end
|
||||
|
||||
# Update and close the session's FileStore file.
|
||||
def close
|
||||
update
|
||||
end
|
||||
|
||||
# Close and delete the session's FileStore file.
|
||||
def delete
|
||||
File::unlink @path+".lock" rescue nil
|
||||
File::unlink @path+".new" rescue nil
|
||||
File::unlink @path rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
# In-memory session storage class.
|
||||
#
|
||||
# Implements session storage as a global in-memory hash. Session
|
||||
# data will only persist for as long as the Ruby interpreter
|
||||
# instance does.
|
||||
class MemoryStore
|
||||
GLOBAL_HASH_TABLE = {} #:nodoc:
|
||||
|
||||
# Create a new MemoryStore instance.
|
||||
#
|
||||
# +session+ is the session this instance is associated with.
|
||||
# +option+ is a list of initialisation options. None are
|
||||
# currently recognised.
|
||||
def initialize(session, option=nil)
|
||||
@session_id = session.session_id
|
||||
unless GLOBAL_HASH_TABLE.key?(@session_id)
|
||||
unless session.new_session
|
||||
raise CGI::Session::NoSession, "uninitialized session"
|
||||
end
|
||||
GLOBAL_HASH_TABLE[@session_id] = {}
|
||||
end
|
||||
end
|
||||
|
||||
# Restore session state.
|
||||
#
|
||||
# Returns session data as a hash.
|
||||
def restore
|
||||
GLOBAL_HASH_TABLE[@session_id]
|
||||
end
|
||||
|
||||
# Update session state.
|
||||
#
|
||||
# A no-op.
|
||||
def update
|
||||
# don't need to update; hash is shared
|
||||
end
|
||||
|
||||
# Close session storage.
|
||||
#
|
||||
# A no-op.
|
||||
def close
|
||||
# don't need to close
|
||||
end
|
||||
|
||||
# Delete the session state.
|
||||
def delete
|
||||
GLOBAL_HASH_TABLE.delete(@session_id)
|
||||
end
|
||||
end
|
||||
|
||||
# Dummy session storage class.
|
||||
#
|
||||
# Implements session storage place holder. No actual storage
|
||||
# will be done.
|
||||
class NullStore
|
||||
# Create a new NullStore instance.
|
||||
#
|
||||
# +session+ is the session this instance is associated with.
|
||||
# +option+ is a list of initialisation options. None are
|
||||
# currently recognised.
|
||||
def initialize(session, option=nil)
|
||||
end
|
||||
|
||||
# Restore (empty) session state.
|
||||
def restore
|
||||
{}
|
||||
end
|
||||
|
||||
# Update session state.
|
||||
#
|
||||
# A no-op.
|
||||
def update
|
||||
end
|
||||
|
||||
# Close session storage.
|
||||
#
|
||||
# A no-op.
|
||||
def close
|
||||
end
|
||||
|
||||
# Delete the session state.
|
||||
#
|
||||
# A no-op.
|
||||
def delete
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
111
ruby/lib/ruby/2.1.0/cgi/session/pstore.rb
Normal file
111
ruby/lib/ruby/2.1.0/cgi/session/pstore.rb
Normal file
@@ -0,0 +1,111 @@
|
||||
#
|
||||
# cgi/session/pstore.rb - persistent storage of marshalled session data
|
||||
#
|
||||
# Documentation: William Webber (william@williamwebber.com)
|
||||
#
|
||||
# == Overview
|
||||
#
|
||||
# This file provides the CGI::Session::PStore class, which builds
|
||||
# persistent of session data on top of the pstore library. See
|
||||
# cgi/session.rb for more details on session storage managers.
|
||||
|
||||
require 'cgi/session'
|
||||
require 'pstore'
|
||||
|
||||
class CGI
|
||||
class Session
|
||||
# PStore-based session storage class.
|
||||
#
|
||||
# This builds upon the top-level PStore class provided by the
|
||||
# library file pstore.rb. Session data is marshalled and stored
|
||||
# in a file. File locking and transaction services are provided.
|
||||
class PStore
|
||||
# Create a new CGI::Session::PStore instance
|
||||
#
|
||||
# This constructor is used internally by CGI::Session. The
|
||||
# user does not generally need to call it directly.
|
||||
#
|
||||
# +session+ is the session for which this instance is being
|
||||
# created. The session id must only contain alphanumeric
|
||||
# characters; automatically generated session ids observe
|
||||
# this requirement.
|
||||
#
|
||||
# +option+ is a hash of options for the initializer. The
|
||||
# following options are recognised:
|
||||
#
|
||||
# tmpdir:: the directory to use for storing the PStore
|
||||
# file. Defaults to Dir::tmpdir (generally "/tmp"
|
||||
# on Unix systems).
|
||||
# prefix:: the prefix to add to the session id when generating
|
||||
# the filename for this session's PStore file.
|
||||
# Defaults to the empty string.
|
||||
#
|
||||
# This session's PStore file will be created if it does
|
||||
# not exist, or opened if it does.
|
||||
def initialize(session, option={})
|
||||
dir = option['tmpdir'] || Dir::tmpdir
|
||||
prefix = option['prefix'] || ''
|
||||
id = session.session_id
|
||||
require 'digest/md5'
|
||||
md5 = Digest::MD5.hexdigest(id)[0,16]
|
||||
path = dir+"/"+prefix+md5
|
||||
path.untaint
|
||||
if File::exist?(path)
|
||||
@hash = nil
|
||||
else
|
||||
unless session.new_session
|
||||
raise CGI::Session::NoSession, "uninitialized session"
|
||||
end
|
||||
@hash = {}
|
||||
end
|
||||
@p = ::PStore.new(path)
|
||||
@p.transaction do |p|
|
||||
File.chmod(0600, p.path)
|
||||
end
|
||||
end
|
||||
|
||||
# Restore session state from the session's PStore file.
|
||||
#
|
||||
# Returns the session state as a hash.
|
||||
def restore
|
||||
unless @hash
|
||||
@p.transaction do
|
||||
@hash = @p['hash'] || {}
|
||||
end
|
||||
end
|
||||
@hash
|
||||
end
|
||||
|
||||
# Save session state to the session's PStore file.
|
||||
def update
|
||||
@p.transaction do
|
||||
@p['hash'] = @hash
|
||||
end
|
||||
end
|
||||
|
||||
# Update and close the session's PStore file.
|
||||
def close
|
||||
update
|
||||
end
|
||||
|
||||
# Close and delete the session's PStore file.
|
||||
def delete
|
||||
path = @p.path
|
||||
File::unlink path
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
# :enddoc:
|
||||
STDIN.reopen("/dev/null")
|
||||
cgi = CGI.new
|
||||
session = CGI::Session.new(cgi, 'database_manager' => CGI::Session::PStore)
|
||||
session['key'] = {'k' => 'v'}
|
||||
puts session['key'].class
|
||||
fail unless Hash === session['key']
|
||||
puts session['key'].inspect
|
||||
fail unless session['key'].inspect == '{"k"=>"v"}'
|
||||
end
|
||||
202
ruby/lib/ruby/2.1.0/cgi/util.rb
Normal file
202
ruby/lib/ruby/2.1.0/cgi/util.rb
Normal file
@@ -0,0 +1,202 @@
|
||||
class CGI; module Util; end; extend Util; end
|
||||
module CGI::Util
|
||||
@@accept_charset="UTF-8" unless defined?(@@accept_charset)
|
||||
# URL-encode a string.
|
||||
# url_encoded_string = CGI::escape("'Stop!' said Fred")
|
||||
# # => "%27Stop%21%27+said+Fred"
|
||||
def escape(string)
|
||||
encoding = string.encoding
|
||||
string.b.gsub(/([^ a-zA-Z0-9_.-]+)/) do |m|
|
||||
'%' + m.unpack('H2' * m.bytesize).join('%').upcase
|
||||
end.tr(' ', '+').force_encoding(encoding)
|
||||
end
|
||||
|
||||
# URL-decode a string with encoding(optional).
|
||||
# string = CGI::unescape("%27Stop%21%27+said+Fred")
|
||||
# # => "'Stop!' said Fred"
|
||||
def unescape(string,encoding=@@accept_charset)
|
||||
str=string.tr('+', ' ').b.gsub(/((?:%[0-9a-fA-F]{2})+)/) do |m|
|
||||
[m.delete('%')].pack('H*')
|
||||
end.force_encoding(encoding)
|
||||
str.valid_encoding? ? str : str.force_encoding(string.encoding)
|
||||
end
|
||||
|
||||
# The set of special characters and their escaped values
|
||||
TABLE_FOR_ESCAPE_HTML__ = {
|
||||
"'" => ''',
|
||||
'&' => '&',
|
||||
'"' => '"',
|
||||
'<' => '<',
|
||||
'>' => '>',
|
||||
}
|
||||
|
||||
# Escape special characters in HTML, namely &\"<>
|
||||
# CGI::escapeHTML('Usage: foo "bar" <baz>')
|
||||
# # => "Usage: foo "bar" <baz>"
|
||||
def escapeHTML(string)
|
||||
string.gsub(/['&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
|
||||
end
|
||||
|
||||
# Unescape a string that has been HTML-escaped
|
||||
# CGI::unescapeHTML("Usage: foo "bar" <baz>")
|
||||
# # => "Usage: foo \"bar\" <baz>"
|
||||
def unescapeHTML(string)
|
||||
return string unless string.include? '&'
|
||||
enc = string.encoding
|
||||
if enc != Encoding::UTF_8 && [Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(enc)
|
||||
return string.gsub(Regexp.new('&(apos|amp|quot|gt|lt|#[0-9]+|#x[0-9A-Fa-f]+);'.encode(enc))) do
|
||||
case $1.encode(Encoding::US_ASCII)
|
||||
when 'apos' then "'".encode(enc)
|
||||
when 'amp' then '&'.encode(enc)
|
||||
when 'quot' then '"'.encode(enc)
|
||||
when 'gt' then '>'.encode(enc)
|
||||
when 'lt' then '<'.encode(enc)
|
||||
when /\A#0*(\d+)\z/ then $1.to_i.chr(enc)
|
||||
when /\A#x([0-9a-f]+)\z/i then $1.hex.chr(enc)
|
||||
end
|
||||
end
|
||||
end
|
||||
asciicompat = Encoding.compatible?(string, "a")
|
||||
string.gsub(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#[xX][0-9A-Fa-f]+);/) do
|
||||
match = $1.dup
|
||||
case match
|
||||
when 'apos' then "'"
|
||||
when 'amp' then '&'
|
||||
when 'quot' then '"'
|
||||
when 'gt' then '>'
|
||||
when 'lt' then '<'
|
||||
when /\A#0*(\d+)\z/
|
||||
n = $1.to_i
|
||||
if enc == Encoding::UTF_8 or
|
||||
enc == Encoding::ISO_8859_1 && n < 256 or
|
||||
asciicompat && n < 128
|
||||
n.chr(enc)
|
||||
else
|
||||
"&##{$1};"
|
||||
end
|
||||
when /\A#x([0-9a-f]+)\z/i
|
||||
n = $1.hex
|
||||
if enc == Encoding::UTF_8 or
|
||||
enc == Encoding::ISO_8859_1 && n < 256 or
|
||||
asciicompat && n < 128
|
||||
n.chr(enc)
|
||||
else
|
||||
"&#x#{$1};"
|
||||
end
|
||||
else
|
||||
"&#{match};"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Synonym for CGI::escapeHTML(str)
|
||||
def escape_html(str)
|
||||
escapeHTML(str)
|
||||
end
|
||||
|
||||
# Synonym for CGI::unescapeHTML(str)
|
||||
def unescape_html(str)
|
||||
unescapeHTML(str)
|
||||
end
|
||||
|
||||
# Escape only the tags of certain HTML elements in +string+.
|
||||
#
|
||||
# Takes an element or elements or array of elements. Each element
|
||||
# is specified by the name of the element, without angle brackets.
|
||||
# This matches both the start and the end tag of that element.
|
||||
# The attribute list of the open tag will also be escaped (for
|
||||
# instance, the double-quotes surrounding attribute values).
|
||||
#
|
||||
# print CGI::escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
|
||||
# # "<BR><A HREF="url"></A>"
|
||||
#
|
||||
# print CGI::escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
|
||||
# # "<BR><A HREF="url"></A>"
|
||||
def escapeElement(string, *elements)
|
||||
elements = elements[0] if elements[0].kind_of?(Array)
|
||||
unless elements.empty?
|
||||
string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/i) do
|
||||
CGI::escapeHTML($&)
|
||||
end
|
||||
else
|
||||
string
|
||||
end
|
||||
end
|
||||
|
||||
# Undo escaping such as that done by CGI::escapeElement()
|
||||
#
|
||||
# print CGI::unescapeElement(
|
||||
# CGI::escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
|
||||
# # "<BR><A HREF="url"></A>"
|
||||
#
|
||||
# print CGI::unescapeElement(
|
||||
# CGI::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
|
||||
# # "<BR><A HREF="url"></A>"
|
||||
def unescapeElement(string, *elements)
|
||||
elements = elements[0] if elements[0].kind_of?(Array)
|
||||
unless elements.empty?
|
||||
string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/i) do
|
||||
unescapeHTML($&)
|
||||
end
|
||||
else
|
||||
string
|
||||
end
|
||||
end
|
||||
|
||||
# Synonym for CGI::escapeElement(str)
|
||||
def escape_element(str)
|
||||
escapeElement(str)
|
||||
end
|
||||
|
||||
# Synonym for CGI::unescapeElement(str)
|
||||
def unescape_element(str)
|
||||
unescapeElement(str)
|
||||
end
|
||||
|
||||
# Abbreviated day-of-week names specified by RFC 822
|
||||
RFC822_DAYS = %w[ Sun Mon Tue Wed Thu Fri Sat ]
|
||||
|
||||
# Abbreviated month names specified by RFC 822
|
||||
RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
|
||||
|
||||
# Format a +Time+ object as a String using the format specified by RFC 1123.
|
||||
#
|
||||
# CGI::rfc1123_date(Time.now)
|
||||
# # Sat, 01 Jan 2000 00:00:00 GMT
|
||||
def rfc1123_date(time)
|
||||
t = time.clone.gmtime
|
||||
return format("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
|
||||
RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
|
||||
t.hour, t.min, t.sec)
|
||||
end
|
||||
|
||||
# Prettify (indent) an HTML string.
|
||||
#
|
||||
# +string+ is the HTML string to indent. +shift+ is the indentation
|
||||
# unit to use; it defaults to two spaces.
|
||||
#
|
||||
# print CGI::pretty("<HTML><BODY></BODY></HTML>")
|
||||
# # <HTML>
|
||||
# # <BODY>
|
||||
# # </BODY>
|
||||
# # </HTML>
|
||||
#
|
||||
# print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
|
||||
# # <HTML>
|
||||
# # <BODY>
|
||||
# # </BODY>
|
||||
# # </HTML>
|
||||
#
|
||||
def pretty(string, shift = " ")
|
||||
lines = string.gsub(/(?!\A)<.*?>/m, "\n\\0").gsub(/<.*?>(?!\n)/m, "\\0\n")
|
||||
end_pos = 0
|
||||
while end_pos = lines.index(/^<\/(\w+)/, end_pos)
|
||||
element = $1.dup
|
||||
start_pos = lines.rindex(/^\s*<#{element}/i, end_pos)
|
||||
lines[start_pos ... end_pos] = "__" + lines[start_pos ... end_pos].gsub(/\n(?!\z)/, "\n" + shift) + "__"
|
||||
end
|
||||
lines.gsub(/^((?:#{Regexp::quote(shift)})*)__(?=<\/?\w)/, '\1')
|
||||
end
|
||||
|
||||
alias h escapeHTML
|
||||
end
|
||||
400
ruby/lib/ruby/2.1.0/cmath.rb
Normal file
400
ruby/lib/ruby/2.1.0/cmath.rb
Normal file
@@ -0,0 +1,400 @@
|
||||
##
|
||||
# CMath is a library that provides trigonometric and transcendental
|
||||
# functions for complex numbers.
|
||||
#
|
||||
# == Usage
|
||||
#
|
||||
# To start using this library, simply:
|
||||
#
|
||||
# require "cmath"
|
||||
#
|
||||
# Square root of a negative number is a complex number.
|
||||
#
|
||||
# CMath.sqrt(-9) #=> 0+3.0i
|
||||
#
|
||||
|
||||
module CMath
|
||||
|
||||
include Math
|
||||
|
||||
alias exp! exp
|
||||
alias log! log
|
||||
alias log2! log2
|
||||
alias log10! log10
|
||||
alias sqrt! sqrt
|
||||
alias cbrt! cbrt
|
||||
|
||||
alias sin! sin
|
||||
alias cos! cos
|
||||
alias tan! tan
|
||||
|
||||
alias sinh! sinh
|
||||
alias cosh! cosh
|
||||
alias tanh! tanh
|
||||
|
||||
alias asin! asin
|
||||
alias acos! acos
|
||||
alias atan! atan
|
||||
alias atan2! atan2
|
||||
|
||||
alias asinh! asinh
|
||||
alias acosh! acosh
|
||||
alias atanh! atanh
|
||||
|
||||
##
|
||||
# Math::E raised to the +z+ power
|
||||
#
|
||||
# exp(Complex(0,0)) #=> 1.0+0.0i
|
||||
# exp(Complex(0,PI)) #=> -1.0+1.2246467991473532e-16i
|
||||
# exp(Complex(0,PI/2.0)) #=> 6.123233995736766e-17+1.0i
|
||||
def exp(z)
|
||||
begin
|
||||
if z.real?
|
||||
exp!(z)
|
||||
else
|
||||
ere = exp!(z.real)
|
||||
Complex(ere * cos!(z.imag),
|
||||
ere * sin!(z.imag))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Returns the natural logarithm of Complex. If a second argument is given,
|
||||
# it will be the base of logarithm.
|
||||
#
|
||||
# log(Complex(0,0)) #=> -Infinity+0.0i
|
||||
def log(*args)
|
||||
begin
|
||||
z, b = args
|
||||
unless b.nil? || b.kind_of?(Numeric)
|
||||
raise TypeError, "Numeric Number required"
|
||||
end
|
||||
if z.real? and z >= 0 and (b.nil? or b >= 0)
|
||||
log!(*args)
|
||||
else
|
||||
a = Complex(log!(z.abs), z.arg)
|
||||
if b
|
||||
a /= log(b)
|
||||
end
|
||||
a
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the base 2 logarithm of +z+
|
||||
def log2(z)
|
||||
begin
|
||||
if z.real? and z >= 0
|
||||
log2!(z)
|
||||
else
|
||||
log(z) / log!(2)
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the base 10 logarithm of +z+
|
||||
def log10(z)
|
||||
begin
|
||||
if z.real? and z >= 0
|
||||
log10!(z)
|
||||
else
|
||||
log(z) / log!(10)
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Returns the non-negative square root of Complex.
|
||||
# sqrt(-1) #=> 0+1.0i
|
||||
# sqrt(Complex(-1,0)) #=> 0.0+1.0i
|
||||
# sqrt(Complex(0,8)) #=> 2.0+2.0i
|
||||
def sqrt(z)
|
||||
begin
|
||||
if z.real?
|
||||
if z < 0
|
||||
Complex(0, sqrt!(-z))
|
||||
else
|
||||
sqrt!(z)
|
||||
end
|
||||
else
|
||||
if z.imag < 0 ||
|
||||
(z.imag == 0 && z.imag.to_s[0] == '-')
|
||||
sqrt(z.conjugate).conjugate
|
||||
else
|
||||
r = z.abs
|
||||
x = z.real
|
||||
Complex(sqrt!((r + x) / 2.0), sqrt!((r - x) / 2.0))
|
||||
end
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the principal value of the cube root of +z+
|
||||
def cbrt(z)
|
||||
z ** (1.0/3)
|
||||
end
|
||||
|
||||
##
|
||||
# returns the sine of +z+, where +z+ is given in radians
|
||||
def sin(z)
|
||||
begin
|
||||
if z.real?
|
||||
sin!(z)
|
||||
else
|
||||
Complex(sin!(z.real) * cosh!(z.imag),
|
||||
cos!(z.real) * sinh!(z.imag))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the cosine of +z+, where +z+ is given in radians
|
||||
def cos(z)
|
||||
begin
|
||||
if z.real?
|
||||
cos!(z)
|
||||
else
|
||||
Complex(cos!(z.real) * cosh!(z.imag),
|
||||
-sin!(z.real) * sinh!(z.imag))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the tangent of +z+, where +z+ is given in radians
|
||||
def tan(z)
|
||||
begin
|
||||
if z.real?
|
||||
tan!(z)
|
||||
else
|
||||
sin(z) / cos(z)
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the hyperbolic sine of +z+, where +z+ is given in radians
|
||||
def sinh(z)
|
||||
begin
|
||||
if z.real?
|
||||
sinh!(z)
|
||||
else
|
||||
Complex(sinh!(z.real) * cos!(z.imag),
|
||||
cosh!(z.real) * sin!(z.imag))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the hyperbolic cosine of +z+, where +z+ is given in radians
|
||||
def cosh(z)
|
||||
begin
|
||||
if z.real?
|
||||
cosh!(z)
|
||||
else
|
||||
Complex(cosh!(z.real) * cos!(z.imag),
|
||||
sinh!(z.real) * sin!(z.imag))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the hyperbolic tangent of +z+, where +z+ is given in radians
|
||||
def tanh(z)
|
||||
begin
|
||||
if z.real?
|
||||
tanh!(z)
|
||||
else
|
||||
sinh(z) / cosh(z)
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the arc sine of +z+
|
||||
def asin(z)
|
||||
begin
|
||||
if z.real? and z >= -1 and z <= 1
|
||||
asin!(z)
|
||||
else
|
||||
(-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the arc cosine of +z+
|
||||
def acos(z)
|
||||
begin
|
||||
if z.real? and z >= -1 and z <= 1
|
||||
acos!(z)
|
||||
else
|
||||
(-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the arc tangent of +z+
|
||||
def atan(z)
|
||||
begin
|
||||
if z.real?
|
||||
atan!(z)
|
||||
else
|
||||
1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the arc tangent of +y+ divided by +x+ using the signs of +y+ and
|
||||
# +x+ to determine the quadrant
|
||||
def atan2(y,x)
|
||||
begin
|
||||
if y.real? and x.real?
|
||||
atan2!(y,x)
|
||||
else
|
||||
(-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the inverse hyperbolic sine of +z+
|
||||
def asinh(z)
|
||||
begin
|
||||
if z.real?
|
||||
asinh!(z)
|
||||
else
|
||||
log(z + sqrt(1.0 + z * z))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the inverse hyperbolic cosine of +z+
|
||||
def acosh(z)
|
||||
begin
|
||||
if z.real? and z >= 1
|
||||
acosh!(z)
|
||||
else
|
||||
log(z + sqrt(z * z - 1.0))
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# returns the inverse hyperbolic tangent of +z+
|
||||
def atanh(z)
|
||||
begin
|
||||
if z.real? and z >= -1 and z <= 1
|
||||
atanh!(z)
|
||||
else
|
||||
log((1.0 + z) / (1.0 - z)) / 2.0
|
||||
end
|
||||
rescue NoMethodError
|
||||
handle_no_method_error
|
||||
end
|
||||
end
|
||||
|
||||
module_function :exp!
|
||||
module_function :exp
|
||||
module_function :log!
|
||||
module_function :log
|
||||
module_function :log2!
|
||||
module_function :log2
|
||||
module_function :log10!
|
||||
module_function :log10
|
||||
module_function :sqrt!
|
||||
module_function :sqrt
|
||||
module_function :cbrt!
|
||||
module_function :cbrt
|
||||
|
||||
module_function :sin!
|
||||
module_function :sin
|
||||
module_function :cos!
|
||||
module_function :cos
|
||||
module_function :tan!
|
||||
module_function :tan
|
||||
|
||||
module_function :sinh!
|
||||
module_function :sinh
|
||||
module_function :cosh!
|
||||
module_function :cosh
|
||||
module_function :tanh!
|
||||
module_function :tanh
|
||||
|
||||
module_function :asin!
|
||||
module_function :asin
|
||||
module_function :acos!
|
||||
module_function :acos
|
||||
module_function :atan!
|
||||
module_function :atan
|
||||
module_function :atan2!
|
||||
module_function :atan2
|
||||
|
||||
module_function :asinh!
|
||||
module_function :asinh
|
||||
module_function :acosh!
|
||||
module_function :acosh
|
||||
module_function :atanh!
|
||||
module_function :atanh
|
||||
|
||||
module_function :frexp
|
||||
module_function :ldexp
|
||||
module_function :hypot
|
||||
module_function :erf
|
||||
module_function :erfc
|
||||
module_function :gamma
|
||||
module_function :lgamma
|
||||
|
||||
private
|
||||
def handle_no_method_error # :nodoc:
|
||||
if $!.name == :real?
|
||||
raise TypeError, "Numeric Number required"
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
module_function :handle_no_method_error
|
||||
|
||||
end
|
||||
|
||||
28
ruby/lib/ruby/2.1.0/complex.rb
Normal file
28
ruby/lib/ruby/2.1.0/complex.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
# :enddoc:
|
||||
|
||||
warn('lib/complex.rb is deprecated') if $VERBOSE
|
||||
|
||||
require 'cmath'
|
||||
|
||||
unless defined?(Math.exp!)
|
||||
Object.instance_eval{remove_const :Math}
|
||||
Math = CMath
|
||||
end
|
||||
|
||||
def Complex.generic? (other)
|
||||
other.kind_of?(Integer) ||
|
||||
other.kind_of?(Float) ||
|
||||
other.kind_of?(Rational)
|
||||
end
|
||||
|
||||
class Complex
|
||||
|
||||
alias image imag
|
||||
|
||||
end
|
||||
|
||||
class Numeric
|
||||
|
||||
def im() Complex(0, self) end
|
||||
|
||||
end
|
||||
2323
ruby/lib/ruby/2.1.0/csv.rb
Normal file
2323
ruby/lib/ruby/2.1.0/csv.rb
Normal file
File diff suppressed because it is too large
Load Diff
61
ruby/lib/ruby/2.1.0/date.rb
Normal file
61
ruby/lib/ruby/2.1.0/date.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
# date.rb: Written by Tadayoshi Funaba 1998-2011
|
||||
|
||||
require 'date_core'
|
||||
require 'date/format'
|
||||
|
||||
class Date
|
||||
|
||||
class Infinity < Numeric # :nodoc:
|
||||
|
||||
include Comparable
|
||||
|
||||
def initialize(d=1) @d = d <=> 0 end
|
||||
|
||||
def d() @d end
|
||||
|
||||
protected :d
|
||||
|
||||
def zero? () false end
|
||||
def finite? () false end
|
||||
def infinite? () d.nonzero? end
|
||||
def nan? () d.zero? end
|
||||
|
||||
def abs() self.class.new end
|
||||
|
||||
def -@ () self.class.new(-d) end
|
||||
def +@ () self.class.new(+d) end
|
||||
|
||||
def <=> (other)
|
||||
case other
|
||||
when Infinity; return d <=> other.d
|
||||
when Numeric; return d
|
||||
else
|
||||
begin
|
||||
l, r = other.coerce(self)
|
||||
return l <=> r
|
||||
rescue NoMethodError
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def coerce(other)
|
||||
case other
|
||||
when Numeric; return -d, d
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def to_f
|
||||
return 0 if @d == 0
|
||||
if @d > 0
|
||||
Float::INFINITY
|
||||
else
|
||||
-Float::INFINITY
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
1
ruby/lib/ruby/2.1.0/date/format.rb
Normal file
1
ruby/lib/ruby/2.1.0/date/format.rb
Normal file
@@ -0,0 +1 @@
|
||||
# format.rb: Written by Tadayoshi Funaba 1999-2011
|
||||
1087
ruby/lib/ruby/2.1.0/debug.rb
Normal file
1087
ruby/lib/ruby/2.1.0/debug.rb
Normal file
File diff suppressed because it is too large
Load Diff
445
ruby/lib/ruby/2.1.0/delegate.rb
Normal file
445
ruby/lib/ruby/2.1.0/delegate.rb
Normal file
@@ -0,0 +1,445 @@
|
||||
# = delegate -- Support for the Delegation Pattern
|
||||
#
|
||||
# Documentation by James Edward Gray II and Gavin Sinclair
|
||||
|
||||
##
|
||||
# This library provides three different ways to delegate method calls to an
|
||||
# object. The easiest to use is SimpleDelegator. Pass an object to the
|
||||
# constructor and all methods supported by the object will be delegated. This
|
||||
# object can be changed later.
|
||||
#
|
||||
# Going a step further, the top level DelegateClass method allows you to easily
|
||||
# setup delegation through class inheritance. This is considerably more
|
||||
# flexible and thus probably the most common use for this library.
|
||||
#
|
||||
# Finally, if you need full control over the delegation scheme, you can inherit
|
||||
# from the abstract class Delegator and customize as needed. (If you find
|
||||
# yourself needing this control, have a look at Forwardable which is also in
|
||||
# the standard library. It may suit your needs better.)
|
||||
#
|
||||
# SimpleDelegator's implementation serves as a nice example if the use of
|
||||
# Delegator:
|
||||
#
|
||||
# class SimpleDelegator < Delegator
|
||||
# def initialize(obj)
|
||||
# super # pass obj to Delegator constructor, required
|
||||
# @delegate_sd_obj = obj # store obj for future use
|
||||
# end
|
||||
#
|
||||
# def __getobj__
|
||||
# @delegate_sd_obj # return object we are delegating to, required
|
||||
# end
|
||||
#
|
||||
# def __setobj__(obj)
|
||||
# @delegate_sd_obj = obj # change delegation object,
|
||||
# # a feature we're providing
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# == Notes
|
||||
#
|
||||
# Be advised, RDoc will not detect delegated methods.
|
||||
#
|
||||
class Delegator < BasicObject
|
||||
kernel = ::Kernel.dup
|
||||
kernel.class_eval do
|
||||
alias __raise__ raise
|
||||
[:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m|
|
||||
undef_method m
|
||||
end
|
||||
private_instance_methods.each do |m|
|
||||
if /\Ablock_given\?\z|iterator\?\z|\A__.*__\z/ =~ m
|
||||
next
|
||||
end
|
||||
undef_method m
|
||||
end
|
||||
end
|
||||
include kernel
|
||||
|
||||
# :stopdoc:
|
||||
def self.const_missing(n)
|
||||
::Object.const_get(n)
|
||||
end
|
||||
# :startdoc:
|
||||
|
||||
#
|
||||
# Pass in the _obj_ to delegate method calls to. All methods supported by
|
||||
# _obj_ will be delegated to.
|
||||
#
|
||||
def initialize(obj)
|
||||
__setobj__(obj)
|
||||
end
|
||||
|
||||
#
|
||||
# Handles the magic of delegation through \_\_getobj\_\_.
|
||||
#
|
||||
def method_missing(m, *args, &block)
|
||||
r = true
|
||||
target = self.__getobj__ {r = false}
|
||||
begin
|
||||
if r && target.respond_to?(m)
|
||||
target.__send__(m, *args, &block)
|
||||
elsif ::Kernel.respond_to?(m, true)
|
||||
::Kernel.instance_method(m).bind(self).(*args, &block)
|
||||
else
|
||||
super(m, *args, &block)
|
||||
end
|
||||
ensure
|
||||
$@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:(?:#{[__LINE__-7, __LINE__-5, __LINE__-3].join('|')}):"o =~ t} if $@
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Checks for a method provided by this the delegate object by forwarding the
|
||||
# call through \_\_getobj\_\_.
|
||||
#
|
||||
def respond_to_missing?(m, include_private)
|
||||
r = true
|
||||
target = self.__getobj__ {r = false}
|
||||
r &&= target.respond_to?(m, include_private)
|
||||
if r && include_private && !target.respond_to?(m, false)
|
||||
warn "#{caller(3)[0]}: delegator does not forward private method \##{m}"
|
||||
return false
|
||||
end
|
||||
r
|
||||
end
|
||||
|
||||
#
|
||||
# Returns the methods available to this delegate object as the union
|
||||
# of this object's and \_\_getobj\_\_ methods.
|
||||
#
|
||||
def methods(all=true)
|
||||
__getobj__.methods(all) | super
|
||||
end
|
||||
|
||||
#
|
||||
# Returns the methods available to this delegate object as the union
|
||||
# of this object's and \_\_getobj\_\_ public methods.
|
||||
#
|
||||
def public_methods(all=true)
|
||||
__getobj__.public_methods(all) | super
|
||||
end
|
||||
|
||||
#
|
||||
# Returns the methods available to this delegate object as the union
|
||||
# of this object's and \_\_getobj\_\_ protected methods.
|
||||
#
|
||||
def protected_methods(all=true)
|
||||
__getobj__.protected_methods(all) | super
|
||||
end
|
||||
|
||||
# Note: no need to specialize private_methods, since they are not forwarded
|
||||
|
||||
#
|
||||
# Returns true if two objects are considered of equal value.
|
||||
#
|
||||
def ==(obj)
|
||||
return true if obj.equal?(self)
|
||||
self.__getobj__ == obj
|
||||
end
|
||||
|
||||
#
|
||||
# Returns true if two objects are not considered of equal value.
|
||||
#
|
||||
def !=(obj)
|
||||
return false if obj.equal?(self)
|
||||
__getobj__ != obj
|
||||
end
|
||||
|
||||
#
|
||||
# Delegates ! to the \_\_getobj\_\_
|
||||
#
|
||||
def !
|
||||
!__getobj__
|
||||
end
|
||||
|
||||
#
|
||||
# This method must be overridden by subclasses and should return the object
|
||||
# method calls are being delegated to.
|
||||
#
|
||||
def __getobj__
|
||||
__raise__ ::NotImplementedError, "need to define `__getobj__'"
|
||||
end
|
||||
|
||||
#
|
||||
# This method must be overridden by subclasses and change the object delegate
|
||||
# to _obj_.
|
||||
#
|
||||
def __setobj__(obj)
|
||||
__raise__ ::NotImplementedError, "need to define `__setobj__'"
|
||||
end
|
||||
|
||||
#
|
||||
# Serialization support for the object returned by \_\_getobj\_\_.
|
||||
#
|
||||
def marshal_dump
|
||||
ivars = instance_variables.reject {|var| /\A@delegate_/ =~ var}
|
||||
[
|
||||
:__v2__,
|
||||
ivars, ivars.map{|var| instance_variable_get(var)},
|
||||
__getobj__
|
||||
]
|
||||
end
|
||||
|
||||
#
|
||||
# Reinitializes delegation from a serialized object.
|
||||
#
|
||||
def marshal_load(data)
|
||||
version, vars, values, obj = data
|
||||
if version == :__v2__
|
||||
vars.each_with_index{|var, i| instance_variable_set(var, values[i])}
|
||||
__setobj__(obj)
|
||||
else
|
||||
__setobj__(data)
|
||||
end
|
||||
end
|
||||
|
||||
def initialize_clone(obj) # :nodoc:
|
||||
self.__setobj__(obj.__getobj__.clone)
|
||||
end
|
||||
def initialize_dup(obj) # :nodoc:
|
||||
self.__setobj__(obj.__getobj__.dup)
|
||||
end
|
||||
private :initialize_clone, :initialize_dup
|
||||
|
||||
##
|
||||
# :method: trust
|
||||
# Trust both the object returned by \_\_getobj\_\_ and self.
|
||||
#
|
||||
|
||||
##
|
||||
# :method: untrust
|
||||
# Untrust both the object returned by \_\_getobj\_\_ and self.
|
||||
#
|
||||
|
||||
##
|
||||
# :method: taint
|
||||
# Taint both the object returned by \_\_getobj\_\_ and self.
|
||||
#
|
||||
|
||||
##
|
||||
# :method: untaint
|
||||
# Untaint both the object returned by \_\_getobj\_\_ and self.
|
||||
#
|
||||
|
||||
##
|
||||
# :method: freeze
|
||||
# Freeze both the object returned by \_\_getobj\_\_ and self.
|
||||
#
|
||||
|
||||
[:trust, :untrust, :taint, :untaint, :freeze].each do |method|
|
||||
define_method method do
|
||||
__getobj__.send(method)
|
||||
super()
|
||||
end
|
||||
end
|
||||
|
||||
@delegator_api = self.public_instance_methods
|
||||
def self.public_api # :nodoc:
|
||||
@delegator_api
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# A concrete implementation of Delegator, this class provides the means to
|
||||
# delegate all supported method calls to the object passed into the constructor
|
||||
# and even to change the object being delegated to at a later time with
|
||||
# #__setobj__.
|
||||
#
|
||||
# class User
|
||||
# def born_on
|
||||
# Date.new(1989, 09, 10)
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# class UserDecorator < SimpleDelegator
|
||||
# def birth_year
|
||||
# born_on.year
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# decorated_user = UserDecorator.new(User.new)
|
||||
# decorated_user.birth_year #=> 1989
|
||||
# decorated_user.__getobj__ #=> #<User: ...>
|
||||
#
|
||||
# A SimpleDelegator instance can take advantage of the fact that SimpleDelegator
|
||||
# is a subclass of +Delegator+ to call <tt>super</tt> to have methods called on
|
||||
# the object being delegated to.
|
||||
#
|
||||
# class SuperArray < SimpleDelegator
|
||||
# def [](*args)
|
||||
# super + 1
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# SuperArray.new([1])[0] #=> 2
|
||||
#
|
||||
# Here's a simple example that takes advantage of the fact that
|
||||
# SimpleDelegator's delegation object can be changed at any time.
|
||||
#
|
||||
# class Stats
|
||||
# def initialize
|
||||
# @source = SimpleDelegator.new([])
|
||||
# end
|
||||
#
|
||||
# def stats(records)
|
||||
# @source.__setobj__(records)
|
||||
#
|
||||
# "Elements: #{@source.size}\n" +
|
||||
# " Non-Nil: #{@source.compact.size}\n" +
|
||||
# " Unique: #{@source.uniq.size}\n"
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# s = Stats.new
|
||||
# puts s.stats(%w{James Edward Gray II})
|
||||
# puts
|
||||
# puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])
|
||||
#
|
||||
# Prints:
|
||||
#
|
||||
# Elements: 4
|
||||
# Non-Nil: 4
|
||||
# Unique: 4
|
||||
#
|
||||
# Elements: 8
|
||||
# Non-Nil: 7
|
||||
# Unique: 6
|
||||
#
|
||||
class SimpleDelegator<Delegator
|
||||
# Returns the current object method calls are being delegated to.
|
||||
def __getobj__
|
||||
unless defined?(@delegate_sd_obj)
|
||||
return yield if block_given?
|
||||
__raise__ ::ArgumentError, "not delegated"
|
||||
end
|
||||
@delegate_sd_obj
|
||||
end
|
||||
|
||||
#
|
||||
# Changes the delegate object to _obj_.
|
||||
#
|
||||
# It's important to note that this does *not* cause SimpleDelegator's methods
|
||||
# to change. Because of this, you probably only want to change delegation
|
||||
# to objects of the same type as the original delegate.
|
||||
#
|
||||
# Here's an example of changing the delegation object.
|
||||
#
|
||||
# names = SimpleDelegator.new(%w{James Edward Gray II})
|
||||
# puts names[1] # => Edward
|
||||
# names.__setobj__(%w{Gavin Sinclair})
|
||||
# puts names[1] # => Sinclair
|
||||
#
|
||||
def __setobj__(obj)
|
||||
__raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj)
|
||||
@delegate_sd_obj = obj
|
||||
end
|
||||
end
|
||||
|
||||
def Delegator.delegating_block(mid) # :nodoc:
|
||||
lambda do |*args, &block|
|
||||
target = self.__getobj__
|
||||
begin
|
||||
target.__send__(mid, *args, &block)
|
||||
ensure
|
||||
$@.delete_if {|t| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/o =~ t} if $@
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# The primary interface to this library. Use to setup delegation when defining
|
||||
# your class.
|
||||
#
|
||||
# class MyClass < DelegateClass(ClassToDelegateTo) # Step 1
|
||||
# def initialize
|
||||
# super(obj_of_ClassToDelegateTo) # Step 2
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# Here's a sample of use from Tempfile which is really a File object with a
|
||||
# few special rules about storage location and when the File should be
|
||||
# deleted. That makes for an almost textbook perfect example of how to use
|
||||
# delegation.
|
||||
#
|
||||
# class Tempfile < DelegateClass(File)
|
||||
# # constant and class member data initialization...
|
||||
#
|
||||
# def initialize(basename, tmpdir=Dir::tmpdir)
|
||||
# # build up file path/name in var tmpname...
|
||||
#
|
||||
# @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)
|
||||
#
|
||||
# # ...
|
||||
#
|
||||
# super(@tmpfile)
|
||||
#
|
||||
# # below this point, all methods of File are supported...
|
||||
# end
|
||||
#
|
||||
# # ...
|
||||
# end
|
||||
#
|
||||
def DelegateClass(superclass)
|
||||
klass = Class.new(Delegator)
|
||||
methods = superclass.instance_methods
|
||||
methods -= ::Delegator.public_api
|
||||
methods -= [:to_s,:inspect,:=~,:!~,:===]
|
||||
klass.module_eval do
|
||||
def __getobj__ # :nodoc:
|
||||
unless defined?(@delegate_dc_obj)
|
||||
return yield if block_given?
|
||||
__raise__ ::ArgumentError, "not delegated"
|
||||
end
|
||||
@delegate_dc_obj
|
||||
end
|
||||
def __setobj__(obj) # :nodoc:
|
||||
__raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj)
|
||||
@delegate_dc_obj = obj
|
||||
end
|
||||
methods.each do |method|
|
||||
define_method(method, Delegator.delegating_block(method))
|
||||
end
|
||||
end
|
||||
klass.define_singleton_method :public_instance_methods do |all=true|
|
||||
super(all) - superclass.protected_instance_methods
|
||||
end
|
||||
klass.define_singleton_method :protected_instance_methods do |all=true|
|
||||
super(all) | superclass.protected_instance_methods
|
||||
end
|
||||
return klass
|
||||
end
|
||||
|
||||
# :enddoc:
|
||||
|
||||
if __FILE__ == $0
|
||||
class ExtArray<DelegateClass(Array)
|
||||
def initialize()
|
||||
super([])
|
||||
end
|
||||
end
|
||||
|
||||
ary = ExtArray.new
|
||||
p ary.class
|
||||
ary.push 25
|
||||
p ary
|
||||
ary.push 42
|
||||
ary.each {|x| p x}
|
||||
|
||||
foo = Object.new
|
||||
def foo.test
|
||||
25
|
||||
end
|
||||
def foo.iter
|
||||
yield self
|
||||
end
|
||||
def foo.error
|
||||
raise 'this is OK'
|
||||
end
|
||||
foo2 = SimpleDelegator.new(foo)
|
||||
p foo2
|
||||
foo2.instance_eval{print "foo\n"}
|
||||
p foo.test == foo2.test # => true
|
||||
p foo2.iter{[55,true]} # => true
|
||||
foo2.error # raise error!
|
||||
end
|
||||
90
ruby/lib/ruby/2.1.0/digest.rb
Normal file
90
ruby/lib/ruby/2.1.0/digest.rb
Normal file
@@ -0,0 +1,90 @@
|
||||
require 'digest.so'
|
||||
|
||||
module Digest
|
||||
def self.const_missing(name) # :nodoc:
|
||||
case name
|
||||
when :SHA256, :SHA384, :SHA512
|
||||
lib = 'digest/sha2.so'
|
||||
else
|
||||
lib = File.join('digest', name.to_s.downcase)
|
||||
end
|
||||
|
||||
begin
|
||||
require lib
|
||||
rescue LoadError
|
||||
raise LoadError, "library not found for class Digest::#{name} -- #{lib}", caller(1)
|
||||
end
|
||||
unless Digest.const_defined?(name)
|
||||
raise NameError, "uninitialized constant Digest::#{name}", caller(1)
|
||||
end
|
||||
Digest.const_get(name)
|
||||
end
|
||||
|
||||
class ::Digest::Class
|
||||
# Creates a digest object and reads a given file, _name_.
|
||||
# Optional arguments are passed to the constructor of the digest
|
||||
# class.
|
||||
#
|
||||
# p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
|
||||
# # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
|
||||
def self.file(name, *args)
|
||||
new(*args).file(name)
|
||||
end
|
||||
|
||||
# Returns the base64 encoded hash value of a given _string_. The
|
||||
# return value is properly padded with '=' and contains no line
|
||||
# feeds.
|
||||
def self.base64digest(str, *args)
|
||||
[digest(str, *args)].pack('m0')
|
||||
end
|
||||
end
|
||||
|
||||
module Instance
|
||||
# Updates the digest with the contents of a given file _name_ and
|
||||
# returns self.
|
||||
def file(name)
|
||||
File.open(name, "rb") {|f|
|
||||
buf = ""
|
||||
while f.read(16384, buf)
|
||||
update buf
|
||||
end
|
||||
}
|
||||
self
|
||||
end
|
||||
|
||||
# If none is given, returns the resulting hash value of the digest
|
||||
# in a base64 encoded form, keeping the digest's state.
|
||||
#
|
||||
# If a +string+ is given, returns the hash value for the given
|
||||
# +string+ in a base64 encoded form, resetting the digest to the
|
||||
# initial state before and after the process.
|
||||
#
|
||||
# In either case, the return value is properly padded with '=' and
|
||||
# contains no line feeds.
|
||||
def base64digest(str = nil)
|
||||
[str ? digest(str) : digest].pack('m0')
|
||||
end
|
||||
|
||||
# Returns the resulting hash value and resets the digest to the
|
||||
# initial state.
|
||||
def base64digest!
|
||||
[digest!].pack('m0')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# Digest(name) -> digest_subclass
|
||||
#
|
||||
# Returns a Digest subclass by +name+.
|
||||
#
|
||||
# require 'digest'
|
||||
#
|
||||
# Digest("MD5")
|
||||
# # => Digest::MD5
|
||||
#
|
||||
# Digest("Foo")
|
||||
# # => LoadError: library not found for class Digest::Foo -- digest/foo
|
||||
def Digest(name)
|
||||
Digest.const_get(name)
|
||||
end
|
||||
302
ruby/lib/ruby/2.1.0/digest/hmac.rb
Normal file
302
ruby/lib/ruby/2.1.0/digest/hmac.rb
Normal file
@@ -0,0 +1,302 @@
|
||||
# == License
|
||||
#
|
||||
# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
|
||||
#
|
||||
# Documentation by Akinori MUSHA
|
||||
#
|
||||
# All rights reserved. You can redistribute and/or modify it under
|
||||
# the same terms as Ruby.
|
||||
#
|
||||
# $Id: hmac.rb 43668 2013-11-13 10:09:28Z zzak $
|
||||
#
|
||||
|
||||
warn "use of the experimetal library 'digest/hmac' is discouraged; require 'openssl' and use OpenSSL::HMAC instead." if $VERBOSE
|
||||
|
||||
require 'digest'
|
||||
|
||||
module Digest
|
||||
# = digest/hmac.rb
|
||||
#
|
||||
# An experimental implementation of HMAC keyed-hashing algorithm
|
||||
#
|
||||
# == Overview
|
||||
#
|
||||
# CAUTION: Use of this library is discouraged, because this
|
||||
# implementation was meant to be experimental but somehow got into the
|
||||
# 1.9 series without being noticed. Please use OpenSSL::HMAC in the
|
||||
# "openssl" library instead.
|
||||
#
|
||||
# == Examples
|
||||
#
|
||||
# require 'digest/hmac'
|
||||
#
|
||||
# # one-liner example
|
||||
# puts Digest::HMAC.hexdigest("data", "hash key", Digest::SHA1)
|
||||
#
|
||||
# # rather longer one
|
||||
# hmac = Digest::HMAC.new("foo", Digest::RMD160)
|
||||
#
|
||||
# buf = ""
|
||||
# while stream.read(16384, buf)
|
||||
# hmac.update(buf)
|
||||
# end
|
||||
#
|
||||
# puts hmac.hexdigest
|
||||
#
|
||||
class HMAC < Digest::Class
|
||||
|
||||
# Creates a Digest::HMAC instance.
|
||||
|
||||
def initialize(key, digester)
|
||||
@md = digester.new
|
||||
|
||||
block_len = @md.block_length
|
||||
|
||||
if key.bytesize > block_len
|
||||
key = @md.digest(key)
|
||||
end
|
||||
|
||||
ipad = Array.new(block_len, 0x36)
|
||||
opad = Array.new(block_len, 0x5c)
|
||||
|
||||
key.bytes.each_with_index { |c, i|
|
||||
ipad[i] ^= c
|
||||
opad[i] ^= c
|
||||
}
|
||||
|
||||
@key = key.freeze
|
||||
@ipad = ipad.pack('C*').freeze
|
||||
@opad = opad.pack('C*').freeze
|
||||
@md.update(@ipad)
|
||||
end
|
||||
|
||||
def initialize_copy(other) # :nodoc:
|
||||
@md = other.instance_eval { @md.clone }
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# hmac.update(string) -> hmac
|
||||
# hmac << string -> hmac
|
||||
#
|
||||
# Updates the hmac using a given +string+ and returns self.
|
||||
def update(text)
|
||||
@md.update(text)
|
||||
self
|
||||
end
|
||||
alias << update
|
||||
|
||||
# call-seq:
|
||||
# hmac.reset -> hmac
|
||||
#
|
||||
# Resets the hmac to the initial state and returns self.
|
||||
def reset
|
||||
@md.reset
|
||||
@md.update(@ipad)
|
||||
self
|
||||
end
|
||||
|
||||
def finish # :nodoc:
|
||||
d = @md.digest!
|
||||
@md.update(@opad)
|
||||
@md.update(d)
|
||||
@md.digest!
|
||||
end
|
||||
private :finish
|
||||
|
||||
# call-seq:
|
||||
# hmac.digest_length -> Integer
|
||||
#
|
||||
# Returns the length in bytes of the hash value of the digest.
|
||||
def digest_length
|
||||
@md.digest_length
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# hmac.block_length -> Integer
|
||||
#
|
||||
# Returns the block length in bytes of the hmac.
|
||||
def block_length
|
||||
@md.block_length
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# hmac.inspect -> string
|
||||
#
|
||||
# Creates a printable version of the hmac object.
|
||||
def inspect
|
||||
sprintf('#<%s: key=%s, digest=%s>', self.class.name, @key.inspect, @md.inspect.sub(/^\#<(.*)>$/) { $1 });
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
eval DATA.gets(nil), nil, $0, DATA.lineno
|
||||
end
|
||||
|
||||
__END__
|
||||
|
||||
require 'test/unit'
|
||||
|
||||
module TM_HMAC
|
||||
def test_s_hexdigest
|
||||
cases.each { |h|
|
||||
digesters.each { |d|
|
||||
assert_equal(h[:hexdigest], Digest::HMAC.hexdigest(h[:data], h[:key], d))
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def test_hexdigest
|
||||
cases.each { |h|
|
||||
digesters.each { |d|
|
||||
hmac = Digest::HMAC.new(h[:key], d)
|
||||
|
||||
hmac.update(h[:data])
|
||||
|
||||
assert_equal(h[:hexdigest], hmac.hexdigest)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def test_reset
|
||||
cases.each { |h|
|
||||
digesters.each { |d|
|
||||
hmac = Digest::HMAC.new(h[:key], d)
|
||||
hmac.update("test")
|
||||
hmac.reset
|
||||
hmac.update(h[:data])
|
||||
|
||||
assert_equal(h[:hexdigest], hmac.hexdigest)
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
class TC_HMAC_MD5 < Test::Unit::TestCase
|
||||
include TM_HMAC
|
||||
|
||||
def digesters
|
||||
[Digest::MD5, Digest::MD5.new]
|
||||
end
|
||||
|
||||
# Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
|
||||
def cases
|
||||
[
|
||||
{
|
||||
:key => "\x0b" * 16,
|
||||
:data => "Hi There",
|
||||
:hexdigest => "9294727a3638bb1c13f48ef8158bfc9d",
|
||||
}, {
|
||||
:key => "Jefe",
|
||||
:data => "what do ya want for nothing?",
|
||||
:hexdigest => "750c783e6ab0b503eaa86e310a5db738",
|
||||
}, {
|
||||
:key => "\xaa" * 16,
|
||||
:data => "\xdd" * 50,
|
||||
:hexdigest => "56be34521d144c88dbb8c733f0e8b3f6",
|
||||
}, {
|
||||
:key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
|
||||
:data => "\xcd" * 50,
|
||||
:hexdigest => "697eaf0aca3a3aea3a75164746ffaa79",
|
||||
}, {
|
||||
:key => "\x0c" * 16,
|
||||
:data => "Test With Truncation",
|
||||
:hexdigest => "56461ef2342edc00f9bab995690efd4c",
|
||||
}, {
|
||||
:key => "\xaa" * 80,
|
||||
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
|
||||
:hexdigest => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
|
||||
}, {
|
||||
:key => "\xaa" * 80,
|
||||
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
||||
:hexdigest => "6f630fad67cda0ee1fb1f562db3aa53e",
|
||||
}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
class TC_HMAC_SHA1 < Test::Unit::TestCase
|
||||
include TM_HMAC
|
||||
|
||||
def digesters
|
||||
[Digest::SHA1, Digest::SHA1.new]
|
||||
end
|
||||
|
||||
# Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
|
||||
def cases
|
||||
[
|
||||
{
|
||||
:key => "\x0b" * 20,
|
||||
:data => "Hi There",
|
||||
:hexdigest => "b617318655057264e28bc0b6fb378c8ef146be00",
|
||||
}, {
|
||||
:key => "Jefe",
|
||||
:data => "what do ya want for nothing?",
|
||||
:hexdigest => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
|
||||
}, {
|
||||
:key => "\xaa" * 20,
|
||||
:data => "\xdd" * 50,
|
||||
:hexdigest => "125d7342b9ac11cd91a39af48aa17b4f63f175d3",
|
||||
}, {
|
||||
:key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
|
||||
:data => "\xcd" * 50,
|
||||
:hexdigest => "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
|
||||
}, {
|
||||
:key => "\x0c" * 20,
|
||||
:data => "Test With Truncation",
|
||||
:hexdigest => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
|
||||
}, {
|
||||
:key => "\xaa" * 80,
|
||||
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
|
||||
:hexdigest => "aa4ae5e15272d00e95705637ce8a3b55ed402112",
|
||||
}, {
|
||||
:key => "\xaa" * 80,
|
||||
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
||||
:hexdigest => "e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
|
||||
}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
class TC_HMAC_RMD160 < Test::Unit::TestCase
|
||||
include TM_HMAC
|
||||
|
||||
def digesters
|
||||
[Digest::RMD160, Digest::RMD160.new]
|
||||
end
|
||||
|
||||
# Taken from RFC 2286: Test Cases for HMAC-RIPEMD160 and HMAC-RIPEMD128
|
||||
def cases
|
||||
[
|
||||
{
|
||||
:key => "\x0b" * 20,
|
||||
:data => "Hi There",
|
||||
:hexdigest => "24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668",
|
||||
}, {
|
||||
:key => "Jefe",
|
||||
:data => "what do ya want for nothing?",
|
||||
:hexdigest => "dda6c0213a485a9e24f4742064a7f033b43c4069",
|
||||
}, {
|
||||
:key => "\xaa" * 20,
|
||||
:data => "\xdd" * 50,
|
||||
:hexdigest => "b0b105360de759960ab4f35298e116e295d8e7c1",
|
||||
}, {
|
||||
:key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
|
||||
:data => "\xcd" * 50,
|
||||
:hexdigest => "d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4",
|
||||
}, {
|
||||
:key => "\x0c" * 20,
|
||||
:data => "Test With Truncation",
|
||||
:hexdigest => "7619693978f91d90539ae786500ff3d8e0518e39",
|
||||
}, {
|
||||
:key => "\xaa" * 80,
|
||||
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
|
||||
:hexdigest => "6466ca07ac5eac29e1bd523e5ada7605b791fd8b",
|
||||
}, {
|
||||
:key => "\xaa" * 80,
|
||||
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
||||
:hexdigest => "69ea60798d71616cce5fd0871e23754cd75d5a0a",
|
||||
}
|
||||
]
|
||||
end
|
||||
end
|
||||
107
ruby/lib/ruby/2.1.0/digest/sha2.rb
Normal file
107
ruby/lib/ruby/2.1.0/digest/sha2.rb
Normal file
@@ -0,0 +1,107 @@
|
||||
#--
|
||||
# sha2.rb - defines Digest::SHA2 class which wraps up the SHA256,
|
||||
# SHA384, and SHA512 classes.
|
||||
#++
|
||||
# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
|
||||
#
|
||||
# All rights reserved. You can redistribute and/or modify it under the same
|
||||
# terms as Ruby.
|
||||
#
|
||||
# $Id: sha2.rb 35293 2012-04-10 22:41:04Z drbrain $
|
||||
|
||||
require 'digest'
|
||||
require 'digest/sha2.so'
|
||||
|
||||
module Digest
|
||||
#
|
||||
# A meta digest provider class for SHA256, SHA384 and SHA512.
|
||||
#
|
||||
class SHA2 < Digest::Class
|
||||
# call-seq:
|
||||
# Digest::SHA2.new(bitlen = 256) -> digest_obj
|
||||
#
|
||||
# Creates a new SHA2 hash object with a given bit length.
|
||||
#
|
||||
# Valid bit lengths are 256, 384 and 512.
|
||||
def initialize(bitlen = 256)
|
||||
case bitlen
|
||||
when 256
|
||||
@sha2 = Digest::SHA256.new
|
||||
when 384
|
||||
@sha2 = Digest::SHA384.new
|
||||
when 512
|
||||
@sha2 = Digest::SHA512.new
|
||||
else
|
||||
raise ArgumentError, "unsupported bit length: %s" % bitlen.inspect
|
||||
end
|
||||
@bitlen = bitlen
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# digest_obj.reset -> digest_obj
|
||||
#
|
||||
# Resets the digest to the initial state and returns self.
|
||||
def reset
|
||||
@sha2.reset
|
||||
self
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# digest_obj.update(string) -> digest_obj
|
||||
# digest_obj << string -> digest_obj
|
||||
#
|
||||
# Updates the digest using a given _string_ and returns self.
|
||||
def update(str)
|
||||
@sha2.update(str)
|
||||
self
|
||||
end
|
||||
alias << update
|
||||
|
||||
def finish # :nodoc:
|
||||
@sha2.digest!
|
||||
end
|
||||
private :finish
|
||||
|
||||
|
||||
# call-seq:
|
||||
# digest_obj.block_length -> Integer
|
||||
#
|
||||
# Returns the block length of the digest in bytes.
|
||||
#
|
||||
# Digest::SHA256.new.block_length * 8
|
||||
# # => 512
|
||||
# Digest::SHA384.new.block_length * 8
|
||||
# # => 1024
|
||||
# Digest::SHA512.new.block_length * 8
|
||||
# # => 1024
|
||||
def block_length
|
||||
@sha2.block_length
|
||||
end
|
||||
|
||||
# call-seq:
|
||||
# digest_obj.digest_length -> Integer
|
||||
#
|
||||
# Returns the length of the hash value of the digest in bytes.
|
||||
#
|
||||
# Digest::SHA256.new.digest_length * 8
|
||||
# # => 256
|
||||
# Digest::SHA384.new.digest_length * 8
|
||||
# # => 384
|
||||
# Digest::SHA512.new.digest_length * 8
|
||||
# # => 512
|
||||
#
|
||||
# For example, digests produced by Digest::SHA256 will always be 32 bytes
|
||||
# (256 bits) in size.
|
||||
def digest_length
|
||||
@sha2.digest_length
|
||||
end
|
||||
|
||||
def initialize_copy(other) # :nodoc:
|
||||
@sha2 = other.instance_eval { @sha2.clone }
|
||||
end
|
||||
|
||||
def inspect # :nodoc:
|
||||
"#<%s:%d %s>" % [self.class.name, @bitlen, hexdigest]
|
||||
end
|
||||
end
|
||||
end
|
||||
15
ruby/lib/ruby/2.1.0/dl.rb
Normal file
15
ruby/lib/ruby/2.1.0/dl.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'dl.so'
|
||||
|
||||
begin
|
||||
require 'fiddle' unless Object.const_defined?(:Fiddle)
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
warn "DL is deprecated, please use Fiddle"
|
||||
|
||||
module DL
|
||||
# Returns true if DL is using Fiddle, the libffi wrapper.
|
||||
def self.fiddle?
|
||||
Object.const_defined?(:Fiddle)
|
||||
end
|
||||
end
|
||||
112
ruby/lib/ruby/2.1.0/dl/callback.rb
Normal file
112
ruby/lib/ruby/2.1.0/dl/callback.rb
Normal file
@@ -0,0 +1,112 @@
|
||||
require 'dl'
|
||||
require 'thread'
|
||||
|
||||
module DL
|
||||
# The mutual exclusion (Mutex) semaphore for the DL module
|
||||
SEM = Mutex.new # :nodoc:
|
||||
|
||||
if DL.fiddle?
|
||||
# A Hash of callback Procs
|
||||
#
|
||||
# Uses Fiddle
|
||||
CdeclCallbackProcs = {} # :nodoc:
|
||||
|
||||
# A Hash of the addresses of callback Proc
|
||||
#
|
||||
# Uses Fiddle
|
||||
CdeclCallbackAddrs = {} # :nodoc:
|
||||
|
||||
# A Hash of Stdcall callback Procs
|
||||
#
|
||||
# Uses Fiddle on win32
|
||||
StdcallCallbackProcs = {} # :nodoc:
|
||||
|
||||
# A Hash of the addresses of Stdcall callback Procs
|
||||
#
|
||||
# Uses Fiddle on win32
|
||||
StdcallCallbackAddrs = {} # :nodoc:
|
||||
end
|
||||
|
||||
def set_callback_internal(proc_entry, addr_entry, argc, ty, abi = nil, &cbp)
|
||||
if( argc < 0 )
|
||||
raise(ArgumentError, "arity should not be less than 0.")
|
||||
end
|
||||
addr = nil
|
||||
|
||||
if DL.fiddle?
|
||||
abi ||= Fiddle::Function::DEFAULT
|
||||
closure = Fiddle::Closure::BlockCaller.new(ty, [TYPE_VOIDP] * argc, abi, &cbp)
|
||||
proc_entry[closure.to_i] = closure
|
||||
addr = closure.to_i
|
||||
else
|
||||
SEM.synchronize{
|
||||
ary = proc_entry[ty]
|
||||
(0...MAX_CALLBACK).each{|n|
|
||||
idx = (n * DLSTACK_SIZE) + argc
|
||||
if( ary[idx].nil? )
|
||||
ary[idx] = cbp
|
||||
addr = addr_entry[ty][idx]
|
||||
break
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
addr
|
||||
end
|
||||
|
||||
def set_cdecl_callback(ty, argc, &cbp)
|
||||
set_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, argc, ty, &cbp)
|
||||
end
|
||||
|
||||
def set_stdcall_callback(ty, argc, &cbp)
|
||||
if DL.fiddle?
|
||||
set_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, argc, ty, Fiddle::Function::STDCALL, &cbp)
|
||||
else
|
||||
set_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, argc, ty, &cbp)
|
||||
end
|
||||
end
|
||||
|
||||
def remove_callback_internal(proc_entry, addr_entry, addr, ctype = nil)
|
||||
if DL.fiddle?
|
||||
addr = addr.to_i
|
||||
return false unless proc_entry.key?(addr)
|
||||
proc_entry.delete(addr)
|
||||
true
|
||||
else
|
||||
index = nil
|
||||
if( ctype )
|
||||
addr_entry[ctype].each_with_index{|xaddr, idx|
|
||||
if( xaddr == addr )
|
||||
index = idx
|
||||
end
|
||||
}
|
||||
else
|
||||
addr_entry.each{|ty,entry|
|
||||
entry.each_with_index{|xaddr, idx|
|
||||
if( xaddr == addr )
|
||||
index = idx
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
if( index and proc_entry[ctype][index] )
|
||||
proc_entry[ctype][index] = nil
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def remove_cdecl_callback(addr, ctype = nil)
|
||||
remove_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, addr, ctype)
|
||||
end
|
||||
|
||||
def remove_stdcall_callback(addr, ctype = nil)
|
||||
remove_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, addr, ctype)
|
||||
end
|
||||
|
||||
alias set_callback set_cdecl_callback
|
||||
alias remove_callback remove_cdecl_callback
|
||||
end
|
||||
156
ruby/lib/ruby/2.1.0/dl/cparser.rb
Normal file
156
ruby/lib/ruby/2.1.0/dl/cparser.rb
Normal file
@@ -0,0 +1,156 @@
|
||||
module DL
|
||||
# Methods for parsing C struct and C prototype signatures.
|
||||
module CParser
|
||||
# Parses a C struct's members
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# parse_struct_signature(['int i', 'char c'])
|
||||
# => [[DL::TYPE_INT, DL::TYPE_CHAR], ["i", "c"]]
|
||||
#
|
||||
def parse_struct_signature(signature, tymap=nil)
|
||||
if( signature.is_a?(String) )
|
||||
signature = signature.split(/\s*,\s*/)
|
||||
end
|
||||
mems = []
|
||||
tys = []
|
||||
signature.each{|msig|
|
||||
tks = msig.split(/\s+(\*)?/)
|
||||
ty = tks[0..-2].join(" ")
|
||||
member = tks[-1]
|
||||
|
||||
case ty
|
||||
when /\[(\d+)\]/
|
||||
n = $1.to_i
|
||||
ty.gsub!(/\s*\[\d+\]/,"")
|
||||
ty = [ty, n]
|
||||
when /\[\]/
|
||||
ty.gsub!(/\s*\[\]/, "*")
|
||||
end
|
||||
|
||||
case member
|
||||
when /\[(\d+)\]/
|
||||
ty = [ty, $1.to_i]
|
||||
member.gsub!(/\s*\[\d+\]/,"")
|
||||
when /\[\]/
|
||||
ty = ty + "*"
|
||||
member.gsub!(/\s*\[\]/, "")
|
||||
end
|
||||
|
||||
mems.push(member)
|
||||
tys.push(parse_ctype(ty,tymap))
|
||||
}
|
||||
return tys, mems
|
||||
end
|
||||
|
||||
# Parses a C prototype signature
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# include DL::CParser
|
||||
# => Object
|
||||
#
|
||||
# parse_signature('double sum(double, double)')
|
||||
# => ["sum", DL::TYPE_DOUBLE, [DL::TYPE_DOUBLE, DL::TYPE_DOUBLE]]
|
||||
#
|
||||
def parse_signature(signature, tymap=nil)
|
||||
tymap ||= {}
|
||||
signature = signature.gsub(/\s+/, " ").strip
|
||||
case signature
|
||||
when /^([\w@\*\s]+)\(([\w\*\s\,\[\]]*)\)$/
|
||||
ret = $1
|
||||
(args = $2).strip!
|
||||
ret = ret.split(/\s+/)
|
||||
args = args.split(/\s*,\s*/)
|
||||
func = ret.pop
|
||||
if( func =~ /^\*/ )
|
||||
func.gsub!(/^\*+/,"")
|
||||
ret.push("*")
|
||||
end
|
||||
ret = ret.join(" ")
|
||||
return [func, parse_ctype(ret, tymap), args.collect{|arg| parse_ctype(arg, tymap)}]
|
||||
else
|
||||
raise(RuntimeError,"can't parse the function prototype: #{signature}")
|
||||
end
|
||||
end
|
||||
|
||||
# Given a String of C type +ty+, return the corresponding DL constant.
|
||||
#
|
||||
# +ty+ can also accept an Array of C type Strings, and will returned in a
|
||||
# corresponding Array.
|
||||
#
|
||||
# If Hash +tymap+ is provided, +ty+ is expected to be the key, and the
|
||||
# value will be the C type to be looked up.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# parse_ctype('int')
|
||||
# => DL::TYPE_INT
|
||||
#
|
||||
# parse_ctype('double')
|
||||
# => DL::TYPE_DOUBLE
|
||||
#
|
||||
# parse_ctype('unsigned char')
|
||||
# => -DL::TYPE_CHAR
|
||||
#
|
||||
def parse_ctype(ty, tymap=nil)
|
||||
tymap ||= {}
|
||||
case ty
|
||||
when Array
|
||||
return [parse_ctype(ty[0], tymap), ty[1]]
|
||||
when "void"
|
||||
return TYPE_VOID
|
||||
when "char"
|
||||
return TYPE_CHAR
|
||||
when "unsigned char"
|
||||
return -TYPE_CHAR
|
||||
when "short"
|
||||
return TYPE_SHORT
|
||||
when "unsigned short"
|
||||
return -TYPE_SHORT
|
||||
when "int"
|
||||
return TYPE_INT
|
||||
when "unsigned int", 'uint'
|
||||
return -TYPE_INT
|
||||
when "long"
|
||||
return TYPE_LONG
|
||||
when "unsigned long"
|
||||
return -TYPE_LONG
|
||||
when "long long"
|
||||
if( defined?(TYPE_LONG_LONG) )
|
||||
return TYPE_LONG_LONG
|
||||
else
|
||||
raise(RuntimeError, "unsupported type: #{ty}")
|
||||
end
|
||||
when "unsigned long long"
|
||||
if( defined?(TYPE_LONG_LONG) )
|
||||
return -TYPE_LONG_LONG
|
||||
else
|
||||
raise(RuntimeError, "unsupported type: #{ty}")
|
||||
end
|
||||
when "float"
|
||||
return TYPE_FLOAT
|
||||
when "double"
|
||||
return TYPE_DOUBLE
|
||||
when "size_t"
|
||||
return TYPE_SIZE_T
|
||||
when "ssize_t"
|
||||
return TYPE_SSIZE_T
|
||||
when "ptrdiff_t"
|
||||
return TYPE_PTRDIFF_T
|
||||
when "intptr_t"
|
||||
return TYPE_INTPTR_T
|
||||
when "uintptr_t"
|
||||
return TYPE_UINTPTR_T
|
||||
when /\*/, /\[\s*\]/
|
||||
return TYPE_VOIDP
|
||||
else
|
||||
if( tymap[ty] )
|
||||
return parse_ctype(tymap[ty], tymap)
|
||||
else
|
||||
raise(DLError, "unknown type: #{ty}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
251
ruby/lib/ruby/2.1.0/dl/func.rb
Normal file
251
ruby/lib/ruby/2.1.0/dl/func.rb
Normal file
@@ -0,0 +1,251 @@
|
||||
require 'dl'
|
||||
require 'dl/callback'
|
||||
require 'dl/stack'
|
||||
require 'dl/value'
|
||||
require 'thread'
|
||||
|
||||
module DL
|
||||
parent = DL.fiddle? ? Fiddle::Function : Object
|
||||
|
||||
class Function < parent
|
||||
include DL
|
||||
include ValueUtil
|
||||
|
||||
if DL.fiddle?
|
||||
# :stopdoc:
|
||||
CALL_TYPE_TO_ABI = Hash.new { |h, k|
|
||||
raise RuntimeError, "unsupported call type: #{k}"
|
||||
}.merge({ :stdcall =>
|
||||
(Fiddle::Function::STDCALL rescue Fiddle::Function::DEFAULT),
|
||||
:cdecl => Fiddle::Function::DEFAULT,
|
||||
nil => Fiddle::Function::DEFAULT
|
||||
}).freeze
|
||||
private_constant :CALL_TYPE_TO_ABI
|
||||
# :startdoc:
|
||||
|
||||
def self.call_type_to_abi(call_type) # :nodoc:
|
||||
CALL_TYPE_TO_ABI[call_type]
|
||||
end
|
||||
private_class_method :call_type_to_abi
|
||||
|
||||
class FiddleClosureCFunc < Fiddle::Closure # :nodoc: all
|
||||
def initialize ctype, arg, abi, name
|
||||
@name = name
|
||||
super(ctype, arg, abi)
|
||||
end
|
||||
def name
|
||||
@name
|
||||
end
|
||||
def ptr
|
||||
to_i
|
||||
end
|
||||
end
|
||||
private_constant :FiddleClosureCFunc
|
||||
|
||||
def self.class_fiddle_closure_cfunc # :nodoc:
|
||||
FiddleClosureCFunc
|
||||
end
|
||||
private_class_method :class_fiddle_closure_cfunc
|
||||
end
|
||||
|
||||
def initialize cfunc, argtypes, abi = nil, &block
|
||||
if DL.fiddle?
|
||||
abi ||= CALL_TYPE_TO_ABI[(cfunc.calltype rescue nil)]
|
||||
if block_given?
|
||||
@cfunc = Class.new(FiddleClosureCFunc) {
|
||||
define_method(:call, block)
|
||||
}.new(cfunc.ctype, argtypes, abi, cfunc.name)
|
||||
else
|
||||
@cfunc = cfunc
|
||||
end
|
||||
|
||||
@args = argtypes
|
||||
super(@cfunc, @args.reject { |x| x == TYPE_VOID }, cfunc.ctype, abi)
|
||||
else
|
||||
@cfunc = cfunc
|
||||
@stack = Stack.new(argtypes.collect{|ty| ty.abs})
|
||||
if( @cfunc.ctype < 0 )
|
||||
@cfunc.ctype = @cfunc.ctype.abs
|
||||
@unsigned = true
|
||||
else
|
||||
@unsigned = false
|
||||
end
|
||||
if block_given?
|
||||
bind(&block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_i()
|
||||
@cfunc.to_i
|
||||
end
|
||||
|
||||
def name
|
||||
@cfunc.name
|
||||
end
|
||||
|
||||
def call(*args, &block)
|
||||
if DL.fiddle?
|
||||
if block_given?
|
||||
args.find { |a| DL::Function === a }.bind_at_call(&block)
|
||||
end
|
||||
super
|
||||
else
|
||||
funcs = []
|
||||
if $SAFE >= 1 && args.any? { |x| x.tainted? }
|
||||
raise SecurityError, "tainted parameter not allowed"
|
||||
end
|
||||
_args = wrap_args(args, @stack.types, funcs, &block)
|
||||
r = @cfunc.call(@stack.pack(_args))
|
||||
funcs.each{|f| f.unbind_at_call()}
|
||||
return wrap_result(r)
|
||||
end
|
||||
end
|
||||
|
||||
def wrap_result(r)
|
||||
case @cfunc.ctype
|
||||
when TYPE_VOIDP
|
||||
r = CPtr.new(r)
|
||||
else
|
||||
if( @unsigned )
|
||||
r = unsigned_value(r, @cfunc.ctype)
|
||||
end
|
||||
end
|
||||
r
|
||||
end
|
||||
|
||||
def bind(&block)
|
||||
if DL.fiddle?
|
||||
@cfunc = Class.new(FiddleClosureCFunc) {
|
||||
def initialize ctype, args, abi, name, block
|
||||
super(ctype, args, abi, name)
|
||||
@block = block
|
||||
end
|
||||
|
||||
def call *args
|
||||
@block.call(*args)
|
||||
end
|
||||
}.new(@cfunc.ctype, @args, abi, name, block)
|
||||
@ptr = @cfunc
|
||||
return nil
|
||||
else
|
||||
if( !block )
|
||||
raise(RuntimeError, "block must be given.")
|
||||
end
|
||||
unless block.lambda?
|
||||
block = Class.new(self.class){define_method(:call, block); def initialize(obj); obj.instance_variables.each{|s| instance_variable_set(s, obj.instance_variable_get(s))}; end}.new(self).method(:call)
|
||||
end
|
||||
if( @cfunc.ptr == 0 )
|
||||
cb = Proc.new{|*args|
|
||||
ary = @stack.unpack(args)
|
||||
@stack.types.each_with_index{|ty, idx|
|
||||
case ty
|
||||
when TYPE_VOIDP
|
||||
ary[idx] = CPtr.new(ary[idx])
|
||||
end
|
||||
}
|
||||
r = block.call(*ary)
|
||||
wrap_arg(r, @cfunc.ctype, [])
|
||||
}
|
||||
case @cfunc.calltype
|
||||
when :cdecl
|
||||
@cfunc.ptr = set_cdecl_callback(@cfunc.ctype, @stack.size, &cb)
|
||||
when :stdcall
|
||||
@cfunc.ptr = set_stdcall_callback(@cfunc.ctype, @stack.size, &cb)
|
||||
else
|
||||
raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
|
||||
end
|
||||
if( @cfunc.ptr == 0 )
|
||||
raise(RuntimeException, "can't bind C function.")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def unbind()
|
||||
if DL.fiddle? then
|
||||
if @cfunc.kind_of?(Fiddle::Closure) and @cfunc.ptr != 0 then
|
||||
call_type = case abi
|
||||
when CALL_TYPE_TO_ABI[nil]
|
||||
nil
|
||||
when CALL_TYPE_TO_ABI[:stdcall]
|
||||
:stdcall
|
||||
else
|
||||
raise(RuntimeError, "unsupported abi: #{abi}")
|
||||
end
|
||||
@cfunc = CFunc.new(0, @cfunc.ctype, name, call_type)
|
||||
return 0
|
||||
elsif @cfunc.ptr != 0 then
|
||||
@cfunc.ptr = 0
|
||||
return 0
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
if( @cfunc.ptr != 0 )
|
||||
case @cfunc.calltype
|
||||
when :cdecl
|
||||
remove_cdecl_callback(@cfunc.ptr, @cfunc.ctype)
|
||||
when :stdcall
|
||||
remove_stdcall_callback(@cfunc.ptr, @cfunc.ctype)
|
||||
else
|
||||
raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
|
||||
end
|
||||
@cfunc.ptr = 0
|
||||
end
|
||||
end
|
||||
|
||||
def bound?()
|
||||
@cfunc.ptr != 0
|
||||
end
|
||||
|
||||
def bind_at_call(&block)
|
||||
bind(&block)
|
||||
end
|
||||
|
||||
def unbind_at_call()
|
||||
end
|
||||
end
|
||||
|
||||
class TempFunction < Function
|
||||
def bind_at_call(&block)
|
||||
bind(&block)
|
||||
end
|
||||
|
||||
def unbind_at_call()
|
||||
unbind()
|
||||
end
|
||||
end
|
||||
|
||||
class CarriedFunction < Function
|
||||
def initialize(cfunc, argtypes, n)
|
||||
super(cfunc, argtypes)
|
||||
@carrier = []
|
||||
@index = n
|
||||
@mutex = Mutex.new
|
||||
end
|
||||
|
||||
def create_carrier(data)
|
||||
ary = []
|
||||
userdata = [ary, data]
|
||||
@mutex.lock()
|
||||
@carrier.push(userdata)
|
||||
return dlwrap(userdata)
|
||||
end
|
||||
|
||||
def bind_at_call(&block)
|
||||
userdata = @carrier[-1]
|
||||
userdata[0].push(block)
|
||||
bind{|*args|
|
||||
ptr = args[@index]
|
||||
if( !ptr )
|
||||
raise(RuntimeError, "The index of userdata should be lower than #{args.size}.")
|
||||
end
|
||||
userdata = dlunwrap(Integer(ptr))
|
||||
args[@index] = userdata[1]
|
||||
userdata[0][0].call(*args)
|
||||
}
|
||||
@mutex.unlock()
|
||||
end
|
||||
end
|
||||
end
|
||||
268
ruby/lib/ruby/2.1.0/dl/import.rb
Normal file
268
ruby/lib/ruby/2.1.0/dl/import.rb
Normal file
@@ -0,0 +1,268 @@
|
||||
require 'dl'
|
||||
require 'dl/func.rb'
|
||||
require 'dl/struct.rb'
|
||||
require 'dl/cparser.rb'
|
||||
|
||||
module DL
|
||||
class CompositeHandler
|
||||
def initialize(handlers)
|
||||
@handlers = handlers
|
||||
end
|
||||
|
||||
def handlers()
|
||||
@handlers
|
||||
end
|
||||
|
||||
def sym(symbol)
|
||||
@handlers.each{|handle|
|
||||
if( handle )
|
||||
begin
|
||||
addr = handle.sym(symbol)
|
||||
return addr
|
||||
rescue DLError
|
||||
end
|
||||
end
|
||||
}
|
||||
return nil
|
||||
end
|
||||
|
||||
def [](symbol)
|
||||
sym(symbol)
|
||||
end
|
||||
end
|
||||
|
||||
# DL::Importer includes the means to dynamically load libraries and build
|
||||
# modules around them including calling extern functions within the C
|
||||
# library that has been loaded.
|
||||
#
|
||||
# == Example
|
||||
#
|
||||
# require 'dl'
|
||||
# require 'dl/import'
|
||||
#
|
||||
# module LibSum
|
||||
# extend DL::Importer
|
||||
# dlload './libsum.so'
|
||||
# extern 'double sum(double*, int)'
|
||||
# extern 'double split(double)'
|
||||
# end
|
||||
#
|
||||
module Importer
|
||||
include DL
|
||||
include CParser
|
||||
extend Importer
|
||||
|
||||
def dlload(*libs)
|
||||
handles = libs.collect{|lib|
|
||||
case lib
|
||||
when nil
|
||||
nil
|
||||
when Handle
|
||||
lib
|
||||
when Importer
|
||||
lib.handlers
|
||||
else
|
||||
begin
|
||||
DL.dlopen(lib)
|
||||
rescue DLError
|
||||
raise(DLError, "can't load #{lib}")
|
||||
end
|
||||
end
|
||||
}.flatten()
|
||||
@handler = CompositeHandler.new(handles)
|
||||
@func_map = {}
|
||||
@type_alias = {}
|
||||
end
|
||||
|
||||
def typealias(alias_type, orig_type)
|
||||
@type_alias[alias_type] = orig_type
|
||||
end
|
||||
|
||||
def sizeof(ty)
|
||||
@type_alias ||= nil
|
||||
case ty
|
||||
when String
|
||||
ty = parse_ctype(ty, @type_alias).abs()
|
||||
case ty
|
||||
when TYPE_CHAR
|
||||
return SIZEOF_CHAR
|
||||
when TYPE_SHORT
|
||||
return SIZEOF_SHORT
|
||||
when TYPE_INT
|
||||
return SIZEOF_INT
|
||||
when TYPE_LONG
|
||||
return SIZEOF_LONG
|
||||
when TYPE_LONG_LONG
|
||||
return SIZEOF_LONG_LON
|
||||
when TYPE_FLOAT
|
||||
return SIZEOF_FLOAT
|
||||
when TYPE_DOUBLE
|
||||
return SIZEOF_DOUBLE
|
||||
when TYPE_VOIDP
|
||||
return SIZEOF_VOIDP
|
||||
else
|
||||
raise(DLError, "unknown type: #{ty}")
|
||||
end
|
||||
when Class
|
||||
if( ty.instance_methods().include?(:to_ptr) )
|
||||
return ty.size()
|
||||
end
|
||||
end
|
||||
return CPtr[ty].size()
|
||||
end
|
||||
|
||||
def parse_bind_options(opts)
|
||||
h = {}
|
||||
while( opt = opts.shift() )
|
||||
case opt
|
||||
when :stdcall, :cdecl
|
||||
h[:call_type] = opt
|
||||
when :carried, :temp, :temporal, :bind
|
||||
h[:callback_type] = opt
|
||||
h[:carrier] = opts.shift()
|
||||
else
|
||||
h[opt] = true
|
||||
end
|
||||
end
|
||||
h
|
||||
end
|
||||
private :parse_bind_options
|
||||
|
||||
def extern(signature, *opts)
|
||||
@type_alias ||= nil
|
||||
symname, ctype, argtype = parse_signature(signature, @type_alias)
|
||||
opt = parse_bind_options(opts)
|
||||
f = import_function(symname, ctype, argtype, opt[:call_type])
|
||||
name = symname.gsub(/@.+/,'')
|
||||
@func_map[name] = f
|
||||
# define_method(name){|*args,&block| f.call(*args,&block)}
|
||||
begin
|
||||
/^(.+?):(\d+)/ =~ caller.first
|
||||
file, line = $1, $2.to_i
|
||||
rescue
|
||||
file, line = __FILE__, __LINE__+3
|
||||
end
|
||||
module_eval(<<-EOS, file, line)
|
||||
def #{name}(*args, &block)
|
||||
@func_map['#{name}'].call(*args,&block)
|
||||
end
|
||||
EOS
|
||||
module_function(name)
|
||||
f
|
||||
end
|
||||
|
||||
def bind(signature, *opts, &blk)
|
||||
@type_alias ||= nil
|
||||
name, ctype, argtype = parse_signature(signature, @type_alias)
|
||||
h = parse_bind_options(opts)
|
||||
case h[:callback_type]
|
||||
when :bind, nil
|
||||
f = bind_function(name, ctype, argtype, h[:call_type], &blk)
|
||||
when :temp, :temporal
|
||||
f = create_temp_function(name, ctype, argtype, h[:call_type])
|
||||
when :carried
|
||||
f = create_carried_function(name, ctype, argtype, h[:call_type], h[:carrier])
|
||||
else
|
||||
raise(RuntimeError, "unknown callback type: #{h[:callback_type]}")
|
||||
end
|
||||
@func_map[name] = f
|
||||
#define_method(name){|*args,&block| f.call(*args,&block)}
|
||||
begin
|
||||
/^(.+?):(\d+)/ =~ caller.first
|
||||
file, line = $1, $2.to_i
|
||||
rescue
|
||||
file, line = __FILE__, __LINE__+3
|
||||
end
|
||||
module_eval(<<-EOS, file, line)
|
||||
def #{name}(*args,&block)
|
||||
@func_map['#{name}'].call(*args,&block)
|
||||
end
|
||||
EOS
|
||||
module_function(name)
|
||||
f
|
||||
end
|
||||
|
||||
# Creates a class to wrap the C struct described by +signature+.
|
||||
#
|
||||
# MyStruct = struct ['int i', 'char c']
|
||||
def struct(signature)
|
||||
@type_alias ||= nil
|
||||
tys, mems = parse_struct_signature(signature, @type_alias)
|
||||
DL::CStructBuilder.create(CStruct, tys, mems)
|
||||
end
|
||||
|
||||
# Creates a class to wrap the C union described by +signature+.
|
||||
#
|
||||
# MyUnion = union ['int i', 'char c']
|
||||
def union(signature)
|
||||
@type_alias ||= nil
|
||||
tys, mems = parse_struct_signature(signature, @type_alias)
|
||||
DL::CStructBuilder.create(CUnion, tys, mems)
|
||||
end
|
||||
|
||||
def [](name)
|
||||
@func_map[name]
|
||||
end
|
||||
|
||||
def create_value(ty, val=nil)
|
||||
s = struct([ty + " value"])
|
||||
ptr = s.malloc()
|
||||
if( val )
|
||||
ptr.value = val
|
||||
end
|
||||
return ptr
|
||||
end
|
||||
alias value create_value
|
||||
|
||||
def import_value(ty, addr)
|
||||
s = struct([ty + " value"])
|
||||
ptr = s.new(addr)
|
||||
return ptr
|
||||
end
|
||||
|
||||
def handler
|
||||
defined?(@handler) or raise "call dlload before importing symbols and functions"
|
||||
@handler
|
||||
end
|
||||
|
||||
def import_symbol(name)
|
||||
addr = handler.sym(name)
|
||||
if( !addr )
|
||||
raise(DLError, "cannot find the symbol: #{name}")
|
||||
end
|
||||
CPtr.new(addr)
|
||||
end
|
||||
|
||||
def import_function(name, ctype, argtype, call_type = nil)
|
||||
addr = handler.sym(name)
|
||||
if( !addr )
|
||||
raise(DLError, "cannot find the function: #{name}()")
|
||||
end
|
||||
Function.new(CFunc.new(addr, ctype, name, call_type || :cdecl), argtype)
|
||||
end
|
||||
|
||||
def bind_function(name, ctype, argtype, call_type = nil, &block)
|
||||
if DL.fiddle?
|
||||
klass = Function.instance_eval { class_fiddle_closure_cfunc }
|
||||
abi = Function.instance_eval { call_type_to_abi(call_type) }
|
||||
closure = Class.new(klass) {
|
||||
define_method(:call, block)
|
||||
}.new(ctype, argtype, abi, name)
|
||||
|
||||
Function.new(closure, argtype, abi)
|
||||
else
|
||||
f = Function.new(CFunc.new(0, ctype, name, call_type || :cdecl), argtype)
|
||||
f.bind(&block)
|
||||
f
|
||||
end
|
||||
end
|
||||
|
||||
def create_temp_function(name, ctype, argtype, call_type = nil)
|
||||
TempFunction.new(CFunc.new(0, ctype, name, call_type || :cdecl), argtype)
|
||||
end
|
||||
|
||||
def create_carried_function(name, ctype, argtype, call_type = nil, n = 0)
|
||||
CarriedFunction.new(CFunc.new(0, ctype, name, call_type || :cdecl), argtype, n)
|
||||
end
|
||||
end
|
||||
end
|
||||
128
ruby/lib/ruby/2.1.0/dl/pack.rb
Normal file
128
ruby/lib/ruby/2.1.0/dl/pack.rb
Normal file
@@ -0,0 +1,128 @@
|
||||
require 'dl'
|
||||
|
||||
module DL
|
||||
module PackInfo
|
||||
ALIGN_MAP = {
|
||||
TYPE_VOIDP => ALIGN_VOIDP,
|
||||
TYPE_CHAR => ALIGN_CHAR,
|
||||
TYPE_SHORT => ALIGN_SHORT,
|
||||
TYPE_INT => ALIGN_INT,
|
||||
TYPE_LONG => ALIGN_LONG,
|
||||
TYPE_FLOAT => ALIGN_FLOAT,
|
||||
TYPE_DOUBLE => ALIGN_DOUBLE,
|
||||
-TYPE_CHAR => ALIGN_CHAR,
|
||||
-TYPE_SHORT => ALIGN_SHORT,
|
||||
-TYPE_INT => ALIGN_INT,
|
||||
-TYPE_LONG => ALIGN_LONG,
|
||||
}
|
||||
|
||||
PACK_MAP = {
|
||||
TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG) ? "q" : "l!"),
|
||||
TYPE_CHAR => "c",
|
||||
TYPE_SHORT => "s!",
|
||||
TYPE_INT => "i!",
|
||||
TYPE_LONG => "l!",
|
||||
TYPE_FLOAT => "f",
|
||||
TYPE_DOUBLE => "d",
|
||||
-TYPE_CHAR => "c",
|
||||
-TYPE_SHORT => "s!",
|
||||
-TYPE_INT => "i!",
|
||||
-TYPE_LONG => "l!",
|
||||
}
|
||||
|
||||
SIZE_MAP = {
|
||||
TYPE_VOIDP => SIZEOF_VOIDP,
|
||||
TYPE_CHAR => SIZEOF_CHAR,
|
||||
TYPE_SHORT => SIZEOF_SHORT,
|
||||
TYPE_INT => SIZEOF_INT,
|
||||
TYPE_LONG => SIZEOF_LONG,
|
||||
TYPE_FLOAT => SIZEOF_FLOAT,
|
||||
TYPE_DOUBLE => SIZEOF_DOUBLE,
|
||||
-TYPE_CHAR => SIZEOF_CHAR,
|
||||
-TYPE_SHORT => SIZEOF_SHORT,
|
||||
-TYPE_INT => SIZEOF_INT,
|
||||
-TYPE_LONG => SIZEOF_LONG,
|
||||
}
|
||||
if defined?(TYPE_LONG_LONG)
|
||||
ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_MAP[-TYPE_LONG_LONG] = ALIGN_LONG_LONG
|
||||
PACK_MAP[TYPE_LONG_LONG] = PACK_MAP[-TYPE_LONG_LONG] = "q"
|
||||
SIZE_MAP[TYPE_LONG_LONG] = SIZE_MAP[-TYPE_LONG_LONG] = SIZEOF_LONG_LONG
|
||||
end
|
||||
|
||||
def align(addr, align)
|
||||
d = addr % align
|
||||
if( d == 0 )
|
||||
addr
|
||||
else
|
||||
addr + (align - d)
|
||||
end
|
||||
end
|
||||
module_function :align
|
||||
end
|
||||
|
||||
class Packer
|
||||
include PackInfo
|
||||
|
||||
def self.[](*types)
|
||||
new(types)
|
||||
end
|
||||
|
||||
def initialize(types)
|
||||
parse_types(types)
|
||||
end
|
||||
|
||||
def size()
|
||||
@size
|
||||
end
|
||||
|
||||
def pack(ary)
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
ary.pack(@template)
|
||||
when SIZEOF_LONG_LONG
|
||||
ary.pack(@template)
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
|
||||
def unpack(ary)
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
ary.join().unpack(@template)
|
||||
when SIZEOF_LONG_LONG
|
||||
ary.join().unpack(@template)
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_types(types)
|
||||
@template = ""
|
||||
addr = 0
|
||||
types.each{|t|
|
||||
orig_addr = addr
|
||||
if( t.is_a?(Array) )
|
||||
addr = align(orig_addr, ALIGN_MAP[TYPE_VOIDP])
|
||||
else
|
||||
addr = align(orig_addr, ALIGN_MAP[t])
|
||||
end
|
||||
d = addr - orig_addr
|
||||
if( d > 0 )
|
||||
@template << "x#{d}"
|
||||
end
|
||||
if( t.is_a?(Array) )
|
||||
@template << (PACK_MAP[t[0]] * t[1])
|
||||
addr += (SIZE_MAP[t[0]] * t[1])
|
||||
else
|
||||
@template << PACK_MAP[t]
|
||||
addr += SIZE_MAP[t]
|
||||
end
|
||||
}
|
||||
addr = align(addr, ALIGN_MAP[TYPE_VOIDP])
|
||||
@size = addr
|
||||
end
|
||||
end
|
||||
end
|
||||
116
ruby/lib/ruby/2.1.0/dl/stack.rb
Normal file
116
ruby/lib/ruby/2.1.0/dl/stack.rb
Normal file
@@ -0,0 +1,116 @@
|
||||
require 'dl'
|
||||
|
||||
module DL
|
||||
class Stack
|
||||
def self.[](*types)
|
||||
new(types)
|
||||
end
|
||||
|
||||
def initialize(types)
|
||||
parse_types(types)
|
||||
end
|
||||
|
||||
def size()
|
||||
@size
|
||||
end
|
||||
|
||||
def types()
|
||||
@types
|
||||
end
|
||||
|
||||
def pack(ary)
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
ary.pack(@template).unpack('l!*')
|
||||
when SIZEOF_LONG_LONG
|
||||
ary.pack(@template).unpack('q*')
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
|
||||
def unpack(ary)
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
ary.pack('l!*').unpack(@template)
|
||||
when SIZEOF_LONG_LONG
|
||||
ary.pack('q*').unpack(@template)
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def align(addr, align)
|
||||
d = addr % align
|
||||
if( d == 0 )
|
||||
addr
|
||||
else
|
||||
addr + (align - d)
|
||||
end
|
||||
end
|
||||
|
||||
ALIGN_MAP = {
|
||||
TYPE_VOIDP => ALIGN_VOIDP,
|
||||
TYPE_CHAR => ALIGN_VOIDP,
|
||||
TYPE_SHORT => ALIGN_VOIDP,
|
||||
TYPE_INT => ALIGN_VOIDP,
|
||||
TYPE_LONG => ALIGN_VOIDP,
|
||||
TYPE_FLOAT => ALIGN_FLOAT,
|
||||
TYPE_DOUBLE => ALIGN_DOUBLE,
|
||||
}
|
||||
|
||||
PACK_MAP = {
|
||||
TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG)? "q" : "l!"),
|
||||
TYPE_CHAR => "c",
|
||||
TYPE_SHORT => "s!",
|
||||
TYPE_INT => "i!",
|
||||
TYPE_LONG => "l!",
|
||||
TYPE_FLOAT => "f",
|
||||
TYPE_DOUBLE => "d",
|
||||
}
|
||||
|
||||
SIZE_MAP = {
|
||||
TYPE_VOIDP => SIZEOF_VOIDP,
|
||||
TYPE_CHAR => SIZEOF_CHAR,
|
||||
TYPE_SHORT => SIZEOF_SHORT,
|
||||
TYPE_INT => SIZEOF_INT,
|
||||
TYPE_LONG => SIZEOF_LONG,
|
||||
TYPE_FLOAT => SIZEOF_FLOAT,
|
||||
TYPE_DOUBLE => SIZEOF_DOUBLE,
|
||||
}
|
||||
if defined?(TYPE_LONG_LONG)
|
||||
ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_LONG_LONG
|
||||
PACK_MAP[TYPE_LONG_LONG] = "q"
|
||||
SIZE_MAP[TYPE_LONG_LONG] = SIZEOF_LONG_LONG
|
||||
end
|
||||
|
||||
def parse_types(types)
|
||||
@types = types
|
||||
@template = ""
|
||||
addr = 0
|
||||
types.each{|t|
|
||||
addr = add_padding(addr, ALIGN_MAP[t])
|
||||
@template << PACK_MAP[t]
|
||||
addr += SIZE_MAP[t]
|
||||
}
|
||||
addr = add_padding(addr, ALIGN_MAP[SIZEOF_VOIDP])
|
||||
if( addr % SIZEOF_VOIDP == 0 )
|
||||
@size = addr / SIZEOF_VOIDP
|
||||
else
|
||||
@size = (addr / SIZEOF_VOIDP) + 1
|
||||
end
|
||||
end
|
||||
|
||||
def add_padding(addr, align)
|
||||
orig_addr = addr
|
||||
addr = align(orig_addr, align)
|
||||
d = addr - orig_addr
|
||||
if( d > 0 )
|
||||
@template << "x#{d}"
|
||||
end
|
||||
addr
|
||||
end
|
||||
end
|
||||
end
|
||||
236
ruby/lib/ruby/2.1.0/dl/struct.rb
Normal file
236
ruby/lib/ruby/2.1.0/dl/struct.rb
Normal file
@@ -0,0 +1,236 @@
|
||||
require 'dl'
|
||||
require 'dl/value'
|
||||
require 'dl/pack.rb'
|
||||
|
||||
module DL
|
||||
# C struct shell
|
||||
class CStruct
|
||||
# accessor to DL::CStructEntity
|
||||
def CStruct.entity_class()
|
||||
CStructEntity
|
||||
end
|
||||
end
|
||||
|
||||
# C union shell
|
||||
class CUnion
|
||||
# accessor to DL::CUnionEntity
|
||||
def CUnion.entity_class()
|
||||
CUnionEntity
|
||||
end
|
||||
end
|
||||
|
||||
# Used to construct C classes (CUnion, CStruct, etc)
|
||||
#
|
||||
# DL::Importer#struct and DL::Importer#union wrap this functionality in an
|
||||
# easy-to-use manner.
|
||||
module CStructBuilder
|
||||
# Construct a new class given a C:
|
||||
# * class +klass+ (CUnion, CStruct, or other that provide an
|
||||
# #entity_class)
|
||||
# * +types+ (DL:TYPE_INT, DL::TYPE_SIZE_T, etc., see the C types
|
||||
# constants)
|
||||
# * corresponding +members+
|
||||
#
|
||||
# DL::Importer#struct and DL::Importer#union wrap this functionality in an
|
||||
# easy-to-use manner.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# require 'dl/struct'
|
||||
# require 'dl/cparser'
|
||||
#
|
||||
# include DL::CParser
|
||||
#
|
||||
# types, members = parse_struct_signature(['int i','char c'])
|
||||
#
|
||||
# MyStruct = DL::CStructBuilder.create(CUnion, types, members)
|
||||
#
|
||||
# obj = MyStruct.allocate
|
||||
#
|
||||
def create(klass, types, members)
|
||||
new_class = Class.new(klass){
|
||||
define_method(:initialize){|addr|
|
||||
@entity = klass.entity_class.new(addr, types)
|
||||
@entity.assign_names(members)
|
||||
}
|
||||
define_method(:to_ptr){ @entity }
|
||||
define_method(:to_i){ @entity.to_i }
|
||||
members.each{|name|
|
||||
define_method(name){ @entity[name] }
|
||||
define_method(name + "="){|val| @entity[name] = val }
|
||||
}
|
||||
}
|
||||
size = klass.entity_class.size(types)
|
||||
new_class.module_eval(<<-EOS, __FILE__, __LINE__+1)
|
||||
def new_class.size()
|
||||
#{size}
|
||||
end
|
||||
def new_class.malloc()
|
||||
addr = DL.malloc(#{size})
|
||||
new(addr)
|
||||
end
|
||||
EOS
|
||||
return new_class
|
||||
end
|
||||
module_function :create
|
||||
end
|
||||
|
||||
# A C struct wrapper
|
||||
class CStructEntity < (DL.fiddle? ? Fiddle::Pointer : CPtr)
|
||||
include PackInfo
|
||||
include ValueUtil
|
||||
|
||||
# Allocates a C struct the +types+ provided. The C function +func+ is
|
||||
# called when the instance is garbage collected.
|
||||
def CStructEntity.malloc(types, func = nil)
|
||||
addr = DL.malloc(CStructEntity.size(types))
|
||||
CStructEntity.new(addr, types, func)
|
||||
end
|
||||
|
||||
# Given +types+, returns the offset for the packed sizes of those types
|
||||
#
|
||||
# DL::CStructEntity.size([DL::TYPE_DOUBLE, DL::TYPE_INT, DL::TYPE_CHAR,
|
||||
# DL::TYPE_VOIDP])
|
||||
# => 24
|
||||
def CStructEntity.size(types)
|
||||
offset = 0
|
||||
|
||||
max_align = types.map { |type, count = 1|
|
||||
last_offset = offset
|
||||
|
||||
align = PackInfo::ALIGN_MAP[type]
|
||||
offset = PackInfo.align(last_offset, align) +
|
||||
(PackInfo::SIZE_MAP[type] * count)
|
||||
|
||||
align
|
||||
}.max
|
||||
|
||||
PackInfo.align(offset, max_align)
|
||||
end
|
||||
|
||||
# Wraps the C pointer +addr+ as a C struct with the given +types+. The C
|
||||
# function +func+ is called when the instance is garbage collected.
|
||||
#
|
||||
# See also DL::CPtr.new
|
||||
def initialize(addr, types, func = nil)
|
||||
set_ctypes(types)
|
||||
super(addr, @size, func)
|
||||
end
|
||||
|
||||
# Set the names of the +members+ in this C struct
|
||||
def assign_names(members)
|
||||
@members = members
|
||||
end
|
||||
|
||||
# Given +types+, calculate the offsets and sizes for the types in the
|
||||
# struct.
|
||||
def set_ctypes(types)
|
||||
@ctypes = types
|
||||
@offset = []
|
||||
offset = 0
|
||||
|
||||
max_align = types.map { |type, count = 1|
|
||||
orig_offset = offset
|
||||
align = ALIGN_MAP[type]
|
||||
offset = PackInfo.align(orig_offset, align)
|
||||
|
||||
@offset << offset
|
||||
|
||||
offset += (SIZE_MAP[type] * count)
|
||||
|
||||
align
|
||||
}.max
|
||||
|
||||
@size = PackInfo.align(offset, max_align)
|
||||
end
|
||||
|
||||
# Fetch struct member +name+
|
||||
def [](name)
|
||||
idx = @members.index(name)
|
||||
if( idx.nil? )
|
||||
raise(ArgumentError, "no such member: #{name}")
|
||||
end
|
||||
ty = @ctypes[idx]
|
||||
if( ty.is_a?(Array) )
|
||||
r = super(@offset[idx], SIZE_MAP[ty[0]] * ty[1])
|
||||
else
|
||||
r = super(@offset[idx], SIZE_MAP[ty.abs])
|
||||
end
|
||||
packer = Packer.new([ty])
|
||||
val = packer.unpack([r])
|
||||
case ty
|
||||
when Array
|
||||
case ty[0]
|
||||
when TYPE_VOIDP
|
||||
val = val.collect{|v| CPtr.new(v)}
|
||||
end
|
||||
when TYPE_VOIDP
|
||||
val = CPtr.new(val[0])
|
||||
else
|
||||
val = val[0]
|
||||
end
|
||||
if( ty.is_a?(Integer) && (ty < 0) )
|
||||
return unsigned_value(val, ty)
|
||||
elsif( ty.is_a?(Array) && (ty[0] < 0) )
|
||||
return val.collect{|v| unsigned_value(v,ty[0])}
|
||||
else
|
||||
return val
|
||||
end
|
||||
end
|
||||
|
||||
# Set struct member +name+, to value +val+
|
||||
def []=(name, val)
|
||||
idx = @members.index(name)
|
||||
if( idx.nil? )
|
||||
raise(ArgumentError, "no such member: #{name}")
|
||||
end
|
||||
ty = @ctypes[idx]
|
||||
packer = Packer.new([ty])
|
||||
val = wrap_arg(val, ty, [])
|
||||
buff = packer.pack([val].flatten())
|
||||
super(@offset[idx], buff.size, buff)
|
||||
if( ty.is_a?(Integer) && (ty < 0) )
|
||||
return unsigned_value(val, ty)
|
||||
elsif( ty.is_a?(Array) && (ty[0] < 0) )
|
||||
return val.collect{|v| unsigned_value(v,ty[0])}
|
||||
else
|
||||
return val
|
||||
end
|
||||
end
|
||||
|
||||
def to_s() # :nodoc:
|
||||
super(@size)
|
||||
end
|
||||
end
|
||||
|
||||
# A C union wrapper
|
||||
class CUnionEntity < CStructEntity
|
||||
include PackInfo
|
||||
|
||||
# Allocates a C union the +types+ provided. The C function +func+ is
|
||||
# called when the instance is garbage collected.
|
||||
def CUnionEntity.malloc(types, func=nil)
|
||||
addr = DL.malloc(CUnionEntity.size(types))
|
||||
CUnionEntity.new(addr, types, func)
|
||||
end
|
||||
|
||||
# Given +types+, returns the size needed for the union.
|
||||
#
|
||||
# DL::CUnionEntity.size([DL::TYPE_DOUBLE, DL::TYPE_INT, DL::TYPE_CHAR,
|
||||
# DL::TYPE_VOIDP])
|
||||
# => 8
|
||||
def CUnionEntity.size(types)
|
||||
types.map { |type, count = 1|
|
||||
PackInfo::SIZE_MAP[type] * count
|
||||
}.max
|
||||
end
|
||||
|
||||
# Given +types+, calculate the necessary offset and for each union member
|
||||
def set_ctypes(types)
|
||||
@ctypes = types
|
||||
@offset = Array.new(types.length, 0)
|
||||
@size = self.class.size types
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
71
ruby/lib/ruby/2.1.0/dl/types.rb
Normal file
71
ruby/lib/ruby/2.1.0/dl/types.rb
Normal file
@@ -0,0 +1,71 @@
|
||||
module DL
|
||||
# Adds Windows type aliases to the including class for use with
|
||||
# DL::Importer.
|
||||
#
|
||||
# The aliases added are:
|
||||
# * ATOM
|
||||
# * BOOL
|
||||
# * BYTE
|
||||
# * DWORD
|
||||
# * DWORD32
|
||||
# * DWORD64
|
||||
# * HANDLE
|
||||
# * HDC
|
||||
# * HINSTANCE
|
||||
# * HWND
|
||||
# * LPCSTR
|
||||
# * LPSTR
|
||||
# * PBYTE
|
||||
# * PDWORD
|
||||
# * PHANDLE
|
||||
# * PVOID
|
||||
# * PWORD
|
||||
# * UCHAR
|
||||
# * UINT
|
||||
# * ULONG
|
||||
# * WORD
|
||||
module Win32Types
|
||||
def included(m) # :nodoc:
|
||||
m.module_eval{
|
||||
typealias "DWORD", "unsigned long"
|
||||
typealias "PDWORD", "unsigned long *"
|
||||
typealias "DWORD32", "unsigned long"
|
||||
typealias "DWORD64", "unsigned long long"
|
||||
typealias "WORD", "unsigned short"
|
||||
typealias "PWORD", "unsigned short *"
|
||||
typealias "BOOL", "int"
|
||||
typealias "ATOM", "int"
|
||||
typealias "BYTE", "unsigned char"
|
||||
typealias "PBYTE", "unsigned char *"
|
||||
typealias "UINT", "unsigned int"
|
||||
typealias "ULONG", "unsigned long"
|
||||
typealias "UCHAR", "unsigned char"
|
||||
typealias "HANDLE", "uintptr_t"
|
||||
typealias "PHANDLE", "void*"
|
||||
typealias "PVOID", "void*"
|
||||
typealias "LPCSTR", "char*"
|
||||
typealias "LPSTR", "char*"
|
||||
typealias "HINSTANCE", "unsigned int"
|
||||
typealias "HDC", "unsigned int"
|
||||
typealias "HWND", "unsigned int"
|
||||
}
|
||||
end
|
||||
module_function :included
|
||||
end
|
||||
|
||||
# Adds basic type aliases to the including class for use with DL::Importer.
|
||||
#
|
||||
# The aliases added are +uint+ and +u_int+ (<tt>unsigned int</tt>) and
|
||||
# +ulong+ and +u_long+ (<tt>unsigned long</tt>)
|
||||
module BasicTypes
|
||||
def included(m) # :nodoc:
|
||||
m.module_eval{
|
||||
typealias "uint", "unsigned int"
|
||||
typealias "u_int", "unsigned int"
|
||||
typealias "ulong", "unsigned long"
|
||||
typealias "u_long", "unsigned long"
|
||||
}
|
||||
end
|
||||
module_function :included
|
||||
end
|
||||
end
|
||||
114
ruby/lib/ruby/2.1.0/dl/value.rb
Normal file
114
ruby/lib/ruby/2.1.0/dl/value.rb
Normal file
@@ -0,0 +1,114 @@
|
||||
require 'dl'
|
||||
|
||||
module DL
|
||||
module ValueUtil
|
||||
def unsigned_value(val, ty)
|
||||
case ty.abs
|
||||
when TYPE_CHAR
|
||||
[val].pack("c").unpack("C")[0]
|
||||
when TYPE_SHORT
|
||||
[val].pack("s!").unpack("S!")[0]
|
||||
when TYPE_INT
|
||||
[val].pack("i!").unpack("I!")[0]
|
||||
when TYPE_LONG
|
||||
[val].pack("l!").unpack("L!")[0]
|
||||
when TYPE_LONG_LONG
|
||||
[val].pack("q").unpack("Q")[0]
|
||||
else
|
||||
val
|
||||
end
|
||||
end
|
||||
|
||||
def signed_value(val, ty)
|
||||
case ty.abs
|
||||
when TYPE_CHAR
|
||||
[val].pack("C").unpack("c")[0]
|
||||
when TYPE_SHORT
|
||||
[val].pack("S!").unpack("s!")[0]
|
||||
when TYPE_INT
|
||||
[val].pack("I!").unpack("i!")[0]
|
||||
when TYPE_LONG
|
||||
[val].pack("L!").unpack("l!")[0]
|
||||
when TYPE_LONG_LONG
|
||||
[val].pack("Q").unpack("q")[0]
|
||||
else
|
||||
val
|
||||
end
|
||||
end
|
||||
|
||||
def wrap_args(args, tys, funcs, &block)
|
||||
result = []
|
||||
tys ||= []
|
||||
args.each_with_index{|arg, idx|
|
||||
result.push(wrap_arg(arg, tys[idx], funcs, &block))
|
||||
}
|
||||
result
|
||||
end
|
||||
|
||||
def wrap_arg(arg, ty, funcs = [], &block)
|
||||
require 'dl/func'
|
||||
|
||||
funcs ||= []
|
||||
case arg
|
||||
when nil
|
||||
return 0
|
||||
when CPtr
|
||||
return arg.to_i
|
||||
when IO
|
||||
case ty
|
||||
when TYPE_VOIDP
|
||||
return CPtr[arg].to_i
|
||||
else
|
||||
return arg.to_i
|
||||
end
|
||||
when Function
|
||||
if( block )
|
||||
arg.bind_at_call(&block)
|
||||
funcs.push(arg)
|
||||
elsif !arg.bound?
|
||||
raise(RuntimeError, "block must be given.")
|
||||
end
|
||||
return arg.to_i
|
||||
when String
|
||||
if( ty.is_a?(Array) )
|
||||
return arg.unpack('C*')
|
||||
else
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
return [arg].pack("p").unpack("l!")[0]
|
||||
when SIZEOF_LONG_LONG
|
||||
return [arg].pack("p").unpack("q")[0]
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
when Float, Integer
|
||||
return arg
|
||||
when Array
|
||||
if( ty.is_a?(Array) ) # used only by struct
|
||||
case ty[0]
|
||||
when TYPE_VOIDP
|
||||
return arg.collect{|v| Integer(v)}
|
||||
when TYPE_CHAR
|
||||
if( arg.is_a?(String) )
|
||||
return val.unpack('C*')
|
||||
end
|
||||
end
|
||||
return arg
|
||||
else
|
||||
return arg
|
||||
end
|
||||
else
|
||||
if( arg.respond_to?(:to_ptr) )
|
||||
return arg.to_ptr.to_i
|
||||
else
|
||||
begin
|
||||
return Integer(arg)
|
||||
rescue
|
||||
raise(ArgumentError, "unknown argument type: #{arg.class}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
2
ruby/lib/ruby/2.1.0/drb.rb
Normal file
2
ruby/lib/ruby/2.1.0/drb.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
require 'drb/drb'
|
||||
|
||||
250
ruby/lib/ruby/2.1.0/drb/acl.rb
Normal file
250
ruby/lib/ruby/2.1.0/drb/acl.rb
Normal file
@@ -0,0 +1,250 @@
|
||||
# Copyright (c) 2000,2002,2003 Masatoshi SEKI
|
||||
#
|
||||
# acl.rb is copyrighted free software by Masatoshi SEKI.
|
||||
# You can redistribute it and/or modify it under the same terms as Ruby.
|
||||
|
||||
require 'ipaddr'
|
||||
|
||||
##
|
||||
# Simple Access Control Lists.
|
||||
#
|
||||
# Access control lists are composed of "allow" and "deny" halves to control
|
||||
# access. Use "all" or "*" to match any address. To match a specific address
|
||||
# use any address or address mask that IPAddr can understand.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# list = %w[
|
||||
# deny all
|
||||
# allow 192.168.1.1
|
||||
# allow ::ffff:192.168.1.2
|
||||
# allow 192.168.1.3
|
||||
# ]
|
||||
#
|
||||
# # From Socket#peeraddr, see also ACL#allow_socket?
|
||||
# addr = ["AF_INET", 10, "lc630", "192.168.1.3"]
|
||||
#
|
||||
# acl = ACL.new
|
||||
# p acl.allow_addr?(addr) # => true
|
||||
#
|
||||
# acl = ACL.new(list, ACL::DENY_ALLOW)
|
||||
# p acl.allow_addr?(addr) # => true
|
||||
|
||||
class ACL
|
||||
|
||||
##
|
||||
# The current version of ACL
|
||||
|
||||
VERSION=["2.0.0"]
|
||||
|
||||
##
|
||||
# An entry in an ACL
|
||||
|
||||
class ACLEntry
|
||||
|
||||
##
|
||||
# Creates a new entry using +str+.
|
||||
#
|
||||
# +str+ may be "*" or "all" to match any address, an IP address string
|
||||
# to match a specific address, an IP address mask per IPAddr, or one
|
||||
# containing "*" to match part of an IPv4 address.
|
||||
|
||||
def initialize(str)
|
||||
if str == '*' or str == 'all'
|
||||
@pat = [:all]
|
||||
elsif str.include?('*')
|
||||
@pat = [:name, dot_pat(str)]
|
||||
else
|
||||
begin
|
||||
@pat = [:ip, IPAddr.new(str)]
|
||||
rescue ArgumentError
|
||||
@pat = [:name, dot_pat(str)]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
##
|
||||
# Creates a regular expression to match IPv4 addresses
|
||||
|
||||
def dot_pat_str(str)
|
||||
list = str.split('.').collect { |s|
|
||||
(s == '*') ? '.+' : s
|
||||
}
|
||||
list.join("\\.")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
##
|
||||
# Creates a Regexp to match an address.
|
||||
|
||||
def dot_pat(str)
|
||||
exp = "^" + dot_pat_str(str) + "$"
|
||||
Regexp.new(exp)
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
##
|
||||
# Matches +addr+ against this entry.
|
||||
|
||||
def match(addr)
|
||||
case @pat[0]
|
||||
when :all
|
||||
true
|
||||
when :ip
|
||||
begin
|
||||
ipaddr = IPAddr.new(addr[3])
|
||||
ipaddr = ipaddr.ipv4_mapped if @pat[1].ipv6? && ipaddr.ipv4?
|
||||
rescue ArgumentError
|
||||
return false
|
||||
end
|
||||
(@pat[1].include?(ipaddr)) ? true : false
|
||||
when :name
|
||||
(@pat[1] =~ addr[2]) ? true : false
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# A list of ACLEntry objects. Used to implement the allow and deny halves
|
||||
# of an ACL
|
||||
|
||||
class ACLList
|
||||
|
||||
##
|
||||
# Creates an empty ACLList
|
||||
|
||||
def initialize
|
||||
@list = []
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
##
|
||||
# Matches +addr+ against each ACLEntry in this list.
|
||||
|
||||
def match(addr)
|
||||
@list.each do |e|
|
||||
return true if e.match(addr)
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
##
|
||||
# Adds +str+ as an ACLEntry in this list
|
||||
|
||||
def add(str)
|
||||
@list.push(ACLEntry.new(str))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
##
|
||||
# Default to deny
|
||||
|
||||
DENY_ALLOW = 0
|
||||
|
||||
##
|
||||
# Default to allow
|
||||
|
||||
ALLOW_DENY = 1
|
||||
|
||||
##
|
||||
# Creates a new ACL from +list+ with an evaluation +order+ of DENY_ALLOW or
|
||||
# ALLOW_DENY.
|
||||
#
|
||||
# An ACL +list+ is an Array of "allow" or "deny" and an address or address
|
||||
# mask or "all" or "*" to match any address:
|
||||
#
|
||||
# %w[
|
||||
# deny all
|
||||
# allow 192.0.2.2
|
||||
# allow 192.0.2.128/26
|
||||
# ]
|
||||
|
||||
def initialize(list=nil, order = DENY_ALLOW)
|
||||
@order = order
|
||||
@deny = ACLList.new
|
||||
@allow = ACLList.new
|
||||
install_list(list) if list
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
##
|
||||
# Allow connections from Socket +soc+?
|
||||
|
||||
def allow_socket?(soc)
|
||||
allow_addr?(soc.peeraddr)
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
##
|
||||
# Allow connections from addrinfo +addr+? It must be formatted like
|
||||
# Socket#peeraddr:
|
||||
#
|
||||
# ["AF_INET", 10, "lc630", "192.0.2.1"]
|
||||
|
||||
def allow_addr?(addr)
|
||||
case @order
|
||||
when DENY_ALLOW
|
||||
return true if @allow.match(addr)
|
||||
return false if @deny.match(addr)
|
||||
return true
|
||||
when ALLOW_DENY
|
||||
return false if @deny.match(addr)
|
||||
return true if @allow.match(addr)
|
||||
return false
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
##
|
||||
# Adds +list+ of ACL entries to this ACL.
|
||||
|
||||
def install_list(list)
|
||||
i = 0
|
||||
while i < list.size
|
||||
permission, domain = list.slice(i,2)
|
||||
case permission.downcase
|
||||
when 'allow'
|
||||
@allow.add(domain)
|
||||
when 'deny'
|
||||
@deny.add(domain)
|
||||
else
|
||||
raise "Invalid ACL entry #{list.to_s}"
|
||||
end
|
||||
i += 2
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
# example
|
||||
list = %w(deny all
|
||||
allow 192.168.1.1
|
||||
allow ::ffff:192.168.1.2
|
||||
allow 192.168.1.3
|
||||
)
|
||||
|
||||
addr = ["AF_INET", 10, "lc630", "192.168.1.3"]
|
||||
|
||||
acl = ACL.new
|
||||
p acl.allow_addr?(addr)
|
||||
|
||||
acl = ACL.new(list, ACL::DENY_ALLOW)
|
||||
p acl.allow_addr?(addr)
|
||||
end
|
||||
|
||||
1833
ruby/lib/ruby/2.1.0/drb/drb.rb
Normal file
1833
ruby/lib/ruby/2.1.0/drb/drb.rb
Normal file
File diff suppressed because it is too large
Load Diff
14
ruby/lib/ruby/2.1.0/drb/eq.rb
Normal file
14
ruby/lib/ruby/2.1.0/drb/eq.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module DRb
|
||||
class DRbObject # :nodoc:
|
||||
def ==(other)
|
||||
return false unless DRbObject === other
|
||||
(@ref == other.__drbref) && (@uri == other.__drburi)
|
||||
end
|
||||
|
||||
def hash
|
||||
[@uri, @ref].hash
|
||||
end
|
||||
|
||||
alias eql? ==
|
||||
end
|
||||
end
|
||||
73
ruby/lib/ruby/2.1.0/drb/extserv.rb
Normal file
73
ruby/lib/ruby/2.1.0/drb/extserv.rb
Normal file
@@ -0,0 +1,73 @@
|
||||
=begin
|
||||
external service
|
||||
Copyright (c) 2000,2002 Masatoshi SEKI
|
||||
=end
|
||||
|
||||
require 'drb/drb'
|
||||
require 'monitor'
|
||||
|
||||
module DRb
|
||||
class ExtServ
|
||||
include MonitorMixin
|
||||
include DRbUndumped
|
||||
|
||||
def initialize(there, name, server=nil)
|
||||
super()
|
||||
@server = server || DRb::primary_server
|
||||
@name = name
|
||||
ro = DRbObject.new(nil, there)
|
||||
synchronize do
|
||||
@invoker = ro.regist(name, DRbObject.new(self, @server.uri))
|
||||
end
|
||||
end
|
||||
attr_reader :server
|
||||
|
||||
def front
|
||||
DRbObject.new(nil, @server.uri)
|
||||
end
|
||||
|
||||
def stop_service
|
||||
synchronize do
|
||||
@invoker.unregist(@name)
|
||||
server = @server
|
||||
@server = nil
|
||||
server.stop_service
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def alive?
|
||||
@server ? @server.alive? : false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# :stopdoc:
|
||||
|
||||
if __FILE__ == $0
|
||||
class Foo
|
||||
include DRbUndumped
|
||||
|
||||
def initialize(str)
|
||||
@str = str
|
||||
end
|
||||
|
||||
def hello(it)
|
||||
"#{it}: #{self}"
|
||||
end
|
||||
|
||||
def to_s
|
||||
@str
|
||||
end
|
||||
end
|
||||
|
||||
cmd = ARGV.shift
|
||||
case cmd
|
||||
when 'itest1', 'itest2'
|
||||
front = Foo.new(cmd)
|
||||
manager = DRb::DRbServer.new(nil, front)
|
||||
es = DRb::ExtServ.new(ARGV.shift, ARGV.shift, manager)
|
||||
es.server.thread.join
|
||||
end
|
||||
end
|
||||
|
||||
93
ruby/lib/ruby/2.1.0/drb/extservm.rb
Normal file
93
ruby/lib/ruby/2.1.0/drb/extservm.rb
Normal file
@@ -0,0 +1,93 @@
|
||||
=begin
|
||||
external service manager
|
||||
Copyright (c) 2000 Masatoshi SEKI
|
||||
=end
|
||||
|
||||
require 'drb/drb'
|
||||
require 'thread'
|
||||
require 'monitor'
|
||||
|
||||
module DRb
|
||||
class ExtServManager
|
||||
include DRbUndumped
|
||||
include MonitorMixin
|
||||
|
||||
@@command = {}
|
||||
|
||||
def self.command
|
||||
@@command
|
||||
end
|
||||
|
||||
def self.command=(cmd)
|
||||
@@command = cmd
|
||||
end
|
||||
|
||||
def initialize
|
||||
super()
|
||||
@cond = new_cond
|
||||
@servers = {}
|
||||
@waiting = []
|
||||
@queue = Queue.new
|
||||
@thread = invoke_thread
|
||||
@uri = nil
|
||||
end
|
||||
attr_accessor :uri
|
||||
|
||||
def service(name)
|
||||
synchronize do
|
||||
while true
|
||||
server = @servers[name]
|
||||
return server if server && server.alive?
|
||||
invoke_service(name)
|
||||
@cond.wait
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def regist(name, ro)
|
||||
synchronize do
|
||||
@servers[name] = ro
|
||||
@cond.signal
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def unregist(name)
|
||||
synchronize do
|
||||
@servers.delete(name)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def invoke_thread
|
||||
Thread.new do
|
||||
while true
|
||||
name = @queue.pop
|
||||
invoke_service_command(name, @@command[name])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def invoke_service(name)
|
||||
@queue.push(name)
|
||||
end
|
||||
|
||||
def invoke_service_command(name, command)
|
||||
raise "invalid command. name: #{name}" unless command
|
||||
synchronize do
|
||||
return if @servers.include?(name)
|
||||
@servers[name] = false
|
||||
end
|
||||
uri = @uri || DRb.uri
|
||||
if command.respond_to? :to_ary
|
||||
command = command.to_ary + [uri, name]
|
||||
pid = spawn(*command)
|
||||
else
|
||||
pid = spawn("#{command} #{uri} #{name}")
|
||||
end
|
||||
th = Process.detach(pid)
|
||||
th[:drb_service] = name
|
||||
th
|
||||
end
|
||||
end
|
||||
end
|
||||
160
ruby/lib/ruby/2.1.0/drb/gw.rb
Normal file
160
ruby/lib/ruby/2.1.0/drb/gw.rb
Normal file
@@ -0,0 +1,160 @@
|
||||
require 'drb/drb'
|
||||
require 'monitor'
|
||||
|
||||
module DRb
|
||||
|
||||
# Gateway id conversion forms a gateway between different DRb protocols or
|
||||
# networks.
|
||||
#
|
||||
# The gateway needs to install this id conversion and create servers for
|
||||
# each of the protocols or networks it will be a gateway between. It then
|
||||
# needs to create a server that attaches to each of these networks. For
|
||||
# example:
|
||||
#
|
||||
# require 'drb/drb'
|
||||
# require 'drb/unix'
|
||||
# require 'drb/gw'
|
||||
#
|
||||
# DRb.install_id_conv DRb::GWIdConv.new
|
||||
# gw = DRb::GW.new
|
||||
# s1 = DRb::DRbServer.new 'drbunix:/path/to/gateway', gw
|
||||
# s2 = DRb::DRbServer.new 'druby://example:10000', gw
|
||||
#
|
||||
# s1.thread.join
|
||||
# s2.thread.join
|
||||
#
|
||||
# Each client must register services with the gateway, for example:
|
||||
#
|
||||
# DRb.start_service 'drbunix:', nil # an anonymous server
|
||||
# gw = DRbObject.new nil, 'drbunix:/path/to/gateway'
|
||||
# gw[:unix] = some_service
|
||||
# DRb.thread.join
|
||||
|
||||
class GWIdConv < DRbIdConv
|
||||
def to_obj(ref) # :nodoc:
|
||||
if Array === ref && ref[0] == :DRbObject
|
||||
return DRbObject.new_with(ref[1], ref[2])
|
||||
end
|
||||
super(ref)
|
||||
end
|
||||
end
|
||||
|
||||
# The GW provides a synchronized store for participants in the gateway to
|
||||
# communicate.
|
||||
|
||||
class GW
|
||||
include MonitorMixin
|
||||
|
||||
# Creates a new GW
|
||||
|
||||
def initialize
|
||||
super()
|
||||
@hash = {}
|
||||
end
|
||||
|
||||
# Retrieves +key+ from the GW
|
||||
|
||||
def [](key)
|
||||
synchronize do
|
||||
@hash[key]
|
||||
end
|
||||
end
|
||||
|
||||
# Stores value +v+ at +key+ in the GW
|
||||
|
||||
def []=(key, v)
|
||||
synchronize do
|
||||
@hash[key] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class DRbObject # :nodoc:
|
||||
def self._load(s)
|
||||
uri, ref = Marshal.load(s)
|
||||
if DRb.uri == uri
|
||||
return ref ? DRb.to_obj(ref) : DRb.front
|
||||
end
|
||||
|
||||
self.new_with(DRb.uri, [:DRbObject, uri, ref])
|
||||
end
|
||||
|
||||
def _dump(lv)
|
||||
if DRb.uri == @uri
|
||||
if Array === @ref && @ref[0] == :DRbObject
|
||||
Marshal.dump([@ref[1], @ref[2]])
|
||||
else
|
||||
Marshal.dump([@uri, @ref]) # ??
|
||||
end
|
||||
else
|
||||
Marshal.dump([DRb.uri, [:DRbObject, @uri, @ref]])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
=begin
|
||||
DRb.install_id_conv(DRb::GWIdConv.new)
|
||||
|
||||
front = DRb::GW.new
|
||||
|
||||
s1 = DRb::DRbServer.new('drbunix:/tmp/gw_b_a', front)
|
||||
s2 = DRb::DRbServer.new('drbunix:/tmp/gw_b_c', front)
|
||||
|
||||
s1.thread.join
|
||||
s2.thread.join
|
||||
=end
|
||||
|
||||
=begin
|
||||
# foo.rb
|
||||
|
||||
require 'drb/drb'
|
||||
|
||||
class Foo
|
||||
include DRbUndumped
|
||||
def initialize(name, peer=nil)
|
||||
@name = name
|
||||
@peer = peer
|
||||
end
|
||||
|
||||
def ping(obj)
|
||||
puts "#{@name}: ping: #{obj.inspect}"
|
||||
@peer.ping(self) if @peer
|
||||
end
|
||||
end
|
||||
=end
|
||||
|
||||
=begin
|
||||
# gw_a.rb
|
||||
require 'drb/unix'
|
||||
require 'foo'
|
||||
|
||||
obj = Foo.new('a')
|
||||
DRb.start_service("drbunix:/tmp/gw_a", obj)
|
||||
|
||||
robj = DRbObject.new_with_uri('drbunix:/tmp/gw_b_a')
|
||||
robj[:a] = obj
|
||||
|
||||
DRb.thread.join
|
||||
=end
|
||||
|
||||
=begin
|
||||
# gw_c.rb
|
||||
require 'drb/unix'
|
||||
require 'foo'
|
||||
|
||||
foo = Foo.new('c', nil)
|
||||
|
||||
DRb.start_service("drbunix:/tmp/gw_c", nil)
|
||||
|
||||
robj = DRbObject.new_with_uri("drbunix:/tmp/gw_b_c")
|
||||
|
||||
puts "c->b"
|
||||
a = robj[:a]
|
||||
sleep 2
|
||||
|
||||
a.ping(foo)
|
||||
|
||||
DRb.thread.join
|
||||
=end
|
||||
|
||||
34
ruby/lib/ruby/2.1.0/drb/invokemethod.rb
Normal file
34
ruby/lib/ruby/2.1.0/drb/invokemethod.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# for ruby-1.8.0
|
||||
|
||||
module DRb # :nodoc: all
|
||||
class DRbServer
|
||||
module InvokeMethod18Mixin
|
||||
def block_yield(x)
|
||||
if x.size == 1 && x[0].class == Array
|
||||
x[0] = DRbArray.new(x[0])
|
||||
end
|
||||
@block.call(*x)
|
||||
end
|
||||
|
||||
def perform_with_block
|
||||
@obj.__send__(@msg_id, *@argv) do |*x|
|
||||
jump_error = nil
|
||||
begin
|
||||
block_value = block_yield(x)
|
||||
rescue LocalJumpError
|
||||
jump_error = $!
|
||||
end
|
||||
if jump_error
|
||||
case jump_error.reason
|
||||
when :break
|
||||
break(jump_error.exit_value)
|
||||
else
|
||||
raise jump_error
|
||||
end
|
||||
end
|
||||
block_value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
25
ruby/lib/ruby/2.1.0/drb/observer.rb
Normal file
25
ruby/lib/ruby/2.1.0/drb/observer.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
require 'observer'
|
||||
|
||||
module DRb
|
||||
# The Observable module extended to DRb. See Observable for details.
|
||||
module DRbObservable
|
||||
include Observable
|
||||
|
||||
# Notifies observers of a change in state. See also
|
||||
# Observable#notify_observers
|
||||
def notify_observers(*arg)
|
||||
if defined? @observer_state and @observer_state
|
||||
if defined? @observer_peers
|
||||
@observer_peers.each do |observer, method|
|
||||
begin
|
||||
observer.send(method, *arg)
|
||||
rescue
|
||||
delete_observer(observer)
|
||||
end
|
||||
end
|
||||
end
|
||||
@observer_state = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
344
ruby/lib/ruby/2.1.0/drb/ssl.rb
Normal file
344
ruby/lib/ruby/2.1.0/drb/ssl.rb
Normal file
@@ -0,0 +1,344 @@
|
||||
require 'socket'
|
||||
require 'openssl'
|
||||
require 'drb/drb'
|
||||
require 'singleton'
|
||||
|
||||
module DRb
|
||||
|
||||
# The protocol for DRb over an SSL socket
|
||||
#
|
||||
# The URI for a DRb socket over SSL is:
|
||||
# <code>drbssl://<host>:<port>?<option></code>. The option is optional
|
||||
class DRbSSLSocket < DRbTCPSocket
|
||||
|
||||
# SSLConfig handles the needed SSL information for establishing a
|
||||
# DRbSSLSocket connection, including generating the X509 / RSA pair.
|
||||
#
|
||||
# An instance of this config can be passed to DRbSSLSocket.new,
|
||||
# DRbSSLSocket.open and DRbSSLSocket.open_server
|
||||
#
|
||||
# See DRb::DRbSSLSocket::SSLConfig.new for more details
|
||||
class SSLConfig
|
||||
|
||||
# Default values for a SSLConfig instance.
|
||||
#
|
||||
# See DRb::DRbSSLSocket::SSLConfig.new for more details
|
||||
DEFAULT = {
|
||||
:SSLCertificate => nil,
|
||||
:SSLPrivateKey => nil,
|
||||
:SSLClientCA => nil,
|
||||
:SSLCACertificatePath => nil,
|
||||
:SSLCACertificateFile => nil,
|
||||
:SSLTmpDhCallback => nil,
|
||||
:SSLVerifyMode => ::OpenSSL::SSL::VERIFY_NONE,
|
||||
:SSLVerifyDepth => nil,
|
||||
:SSLVerifyCallback => nil, # custom verification
|
||||
:SSLCertificateStore => nil,
|
||||
# Must specify if you use auto generated certificate.
|
||||
:SSLCertName => nil, # e.g. [["CN","fqdn.example.com"]]
|
||||
:SSLCertComment => "Generated by Ruby/OpenSSL"
|
||||
}
|
||||
|
||||
# Create a new DRb::DRbSSLSocket::SSLConfig instance
|
||||
#
|
||||
# The DRb::DRbSSLSocket will take either a +config+ Hash or an instance
|
||||
# of SSLConfig, and will setup the certificate for its session for the
|
||||
# configuration. If want it to generate a generic certificate, the bare
|
||||
# minimum is to provide the :SSLCertName
|
||||
#
|
||||
# === Config options
|
||||
#
|
||||
# From +config+ Hash:
|
||||
#
|
||||
# :SSLCertificate ::
|
||||
# An instance of OpenSSL::X509::Certificate. If this is not provided,
|
||||
# then a generic X509 is generated, with a correspond :SSLPrivateKey
|
||||
#
|
||||
# :SSLPrivateKey ::
|
||||
# A private key instance, like OpenSSL::PKey::RSA. This key must be
|
||||
# the key that signed the :SSLCertificate
|
||||
#
|
||||
# :SSLClientCA ::
|
||||
# An OpenSSL::X509::Certificate, or Array of certificates that will
|
||||
# used as ClientCAs in the SSL Context
|
||||
#
|
||||
# :SSLCACertificatePath ::
|
||||
# A path to the directory of CA certificates. The certificates must
|
||||
# be in PEM format.
|
||||
#
|
||||
# :SSLCACertificateFile ::
|
||||
# A path to a CA certificate file, in PEM format.
|
||||
#
|
||||
# :SSLTmpDhCallback ::
|
||||
# A DH callback. See OpenSSL::SSL::SSLContext.tmp_dh_callback
|
||||
#
|
||||
# :SSLVerifyMode ::
|
||||
# This is the SSL verification mode. See OpenSSL::SSL::VERIFY_* for
|
||||
# available modes. The default is OpenSSL::SSL::VERIFY_NONE
|
||||
#
|
||||
# :SSLVerifyDepth ::
|
||||
# Number of CA certificates to walk, when verifying a certificate
|
||||
# chain.
|
||||
#
|
||||
# :SSLVerifyCallback ::
|
||||
# A callback to be used for additional verification. See
|
||||
# OpenSSL::SSL::SSLContext.verify_callback
|
||||
#
|
||||
# :SSLCertificateStore ::
|
||||
# A OpenSSL::X509::Store used for verification of certificates
|
||||
#
|
||||
# :SSLCertName ::
|
||||
# Issuer name for the certificate. This is required when generating
|
||||
# the certificate (if :SSLCertificate and :SSLPrivateKey were not
|
||||
# given). The value of this is to be an Array of pairs:
|
||||
#
|
||||
# [["C", "Raleigh"], ["ST","North Carolina"],
|
||||
# ["CN","fqdn.example.com"]]
|
||||
#
|
||||
# See also OpenSSL::X509::Name
|
||||
#
|
||||
# :SSLCertComment ::
|
||||
# A comment to be used for generating the certificate. The default is
|
||||
# "Generated by Ruby/OpenSSL"
|
||||
#
|
||||
#
|
||||
# === Example
|
||||
#
|
||||
# These values can be added after the fact, like a Hash.
|
||||
#
|
||||
# require 'drb/ssl'
|
||||
# c = DRb::DRbSSLSocket::SSLConfig.new {}
|
||||
# c[:SSLCertificate] =
|
||||
# OpenSSL::X509::Certificate.new(File.read('mycert.crt'))
|
||||
# c[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(File.read('mycert.key'))
|
||||
# c[:SSLVerifyMode] = OpenSSL::SSL::VERIFY_PEER
|
||||
# c[:SSLCACertificatePath] = "/etc/ssl/certs/"
|
||||
# c.setup_certificate
|
||||
#
|
||||
# or
|
||||
#
|
||||
# require 'drb/ssl'
|
||||
# c = DRb::DRbSSLSocket::SSLConfig.new({
|
||||
# :SSLCertName => [["CN" => DRb::DRbSSLSocket.getservername]]
|
||||
# })
|
||||
# c.setup_certificate
|
||||
#
|
||||
def initialize(config)
|
||||
@config = config
|
||||
@cert = config[:SSLCertificate]
|
||||
@pkey = config[:SSLPrivateKey]
|
||||
@ssl_ctx = nil
|
||||
end
|
||||
|
||||
# A convenience method to access the values like a Hash
|
||||
def [](key);
|
||||
@config[key] || DEFAULT[key]
|
||||
end
|
||||
|
||||
# Connect to IO +tcp+, with context of the current certificate
|
||||
# configuration
|
||||
def connect(tcp)
|
||||
ssl = ::OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
|
||||
ssl.sync = true
|
||||
ssl.connect
|
||||
ssl
|
||||
end
|
||||
|
||||
# Accept connection to IO +tcp+, with context of the current certificate
|
||||
# configuration
|
||||
def accept(tcp)
|
||||
ssl = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
|
||||
ssl.sync = true
|
||||
ssl.accept
|
||||
ssl
|
||||
end
|
||||
|
||||
# Ensures that :SSLCertificate and :SSLPrivateKey have been provided
|
||||
# or that a new certificate is generated with the other parameters
|
||||
# provided.
|
||||
def setup_certificate
|
||||
if @cert && @pkey
|
||||
return
|
||||
end
|
||||
|
||||
rsa = OpenSSL::PKey::RSA.new(1024){|p, n|
|
||||
next unless self[:verbose]
|
||||
case p
|
||||
when 0; $stderr.putc "." # BN_generate_prime
|
||||
when 1; $stderr.putc "+" # BN_generate_prime
|
||||
when 2; $stderr.putc "*" # searching good prime,
|
||||
# n = #of try,
|
||||
# but also data from BN_generate_prime
|
||||
when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,
|
||||
# but also data from BN_generate_prime
|
||||
else; $stderr.putc "*" # BN_generate_prime
|
||||
end
|
||||
}
|
||||
|
||||
cert = OpenSSL::X509::Certificate.new
|
||||
cert.version = 3
|
||||
cert.serial = 0
|
||||
name = OpenSSL::X509::Name.new(self[:SSLCertName])
|
||||
cert.subject = name
|
||||
cert.issuer = name
|
||||
cert.not_before = Time.now
|
||||
cert.not_after = Time.now + (365*24*60*60)
|
||||
cert.public_key = rsa.public_key
|
||||
|
||||
ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
|
||||
cert.extensions = [
|
||||
ef.create_extension("basicConstraints","CA:FALSE"),
|
||||
ef.create_extension("subjectKeyIdentifier", "hash") ]
|
||||
ef.issuer_certificate = cert
|
||||
cert.add_extension(ef.create_extension("authorityKeyIdentifier",
|
||||
"keyid:always,issuer:always"))
|
||||
if comment = self[:SSLCertComment]
|
||||
cert.add_extension(ef.create_extension("nsComment", comment))
|
||||
end
|
||||
cert.sign(rsa, OpenSSL::Digest::SHA1.new)
|
||||
|
||||
@cert = cert
|
||||
@pkey = rsa
|
||||
end
|
||||
|
||||
# Establish the OpenSSL::SSL::SSLContext with the configuration
|
||||
# parameters provided.
|
||||
def setup_ssl_context
|
||||
ctx = ::OpenSSL::SSL::SSLContext.new
|
||||
ctx.cert = @cert
|
||||
ctx.key = @pkey
|
||||
ctx.client_ca = self[:SSLClientCA]
|
||||
ctx.ca_path = self[:SSLCACertificatePath]
|
||||
ctx.ca_file = self[:SSLCACertificateFile]
|
||||
ctx.tmp_dh_callback = self[:SSLTmpDhCallback]
|
||||
ctx.verify_mode = self[:SSLVerifyMode]
|
||||
ctx.verify_depth = self[:SSLVerifyDepth]
|
||||
ctx.verify_callback = self[:SSLVerifyCallback]
|
||||
ctx.cert_store = self[:SSLCertificateStore]
|
||||
@ssl_ctx = ctx
|
||||
end
|
||||
end
|
||||
|
||||
# Parse the dRuby +uri+ for an SSL connection.
|
||||
#
|
||||
# Expects drbssl://...
|
||||
#
|
||||
# Raises DRbBadScheme or DRbBadURI if +uri+ is not matching or malformed
|
||||
def self.parse_uri(uri) # :nodoc:
|
||||
if uri =~ /^drbssl:\/\/(.*?):(\d+)(\?(.*))?$/
|
||||
host = $1
|
||||
port = $2.to_i
|
||||
option = $4
|
||||
[host, port, option]
|
||||
else
|
||||
raise(DRbBadScheme, uri) unless uri =~ /^drbssl:/
|
||||
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
||||
end
|
||||
end
|
||||
|
||||
# Return an DRb::DRbSSLSocket instance as a client-side connection,
|
||||
# with the SSL connected. This is called from DRb::start_service or while
|
||||
# connecting to a remote object:
|
||||
#
|
||||
# DRb.start_service 'drbssl://localhost:0', front, config
|
||||
#
|
||||
# +uri+ is the URI we are connected to,
|
||||
# <code>'drbssl://localhost:0'</code> above, +config+ is our
|
||||
# configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig
|
||||
def self.open(uri, config)
|
||||
host, port, = parse_uri(uri)
|
||||
host.untaint
|
||||
port.untaint
|
||||
soc = TCPSocket.open(host, port)
|
||||
ssl_conf = SSLConfig::new(config)
|
||||
ssl_conf.setup_ssl_context
|
||||
ssl = ssl_conf.connect(soc)
|
||||
self.new(uri, ssl, ssl_conf, true)
|
||||
end
|
||||
|
||||
# Returns a DRb::DRbSSLSocket instance as a server-side connection, with
|
||||
# the SSL connected. This is called from DRb::start_service or while
|
||||
# connecting to a remote object:
|
||||
#
|
||||
# DRb.start_service 'drbssl://localhost:0', front, config
|
||||
#
|
||||
# +uri+ is the URI we are connected to,
|
||||
# <code>'drbssl://localhost:0'</code> above, +config+ is our
|
||||
# configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig
|
||||
def self.open_server(uri, config)
|
||||
uri = 'drbssl://:0' unless uri
|
||||
host, port, = parse_uri(uri)
|
||||
if host.size == 0
|
||||
host = getservername
|
||||
soc = open_server_inaddr_any(host, port)
|
||||
else
|
||||
soc = TCPServer.open(host, port)
|
||||
end
|
||||
port = soc.addr[1] if port == 0
|
||||
@uri = "drbssl://#{host}:#{port}"
|
||||
|
||||
ssl_conf = SSLConfig.new(config)
|
||||
ssl_conf.setup_certificate
|
||||
ssl_conf.setup_ssl_context
|
||||
self.new(@uri, soc, ssl_conf, false)
|
||||
end
|
||||
|
||||
# This is a convenience method to parse +uri+ and separate out any
|
||||
# additional options appended in the +uri+.
|
||||
#
|
||||
# Returns an option-less uri and the option => [uri,option]
|
||||
#
|
||||
# The +config+ is completely unused, so passing nil is sufficient.
|
||||
def self.uri_option(uri, config) # :nodoc:
|
||||
host, port, option = parse_uri(uri)
|
||||
return "drbssl://#{host}:#{port}", option
|
||||
end
|
||||
|
||||
# Create a DRb::DRbSSLSocket instance.
|
||||
#
|
||||
# +uri+ is the URI we are connected to.
|
||||
# +soc+ is the tcp socket we are bound to.
|
||||
# +config+ is our configuration. Either a Hash or SSLConfig
|
||||
# +is_established+ is a boolean of whether +soc+ is currently established
|
||||
#
|
||||
# This is called automatically based on the DRb protocol.
|
||||
def initialize(uri, soc, config, is_established)
|
||||
@ssl = is_established ? soc : nil
|
||||
super(uri, soc.to_io, config)
|
||||
end
|
||||
|
||||
# Returns the SSL stream
|
||||
def stream; @ssl; end # :nodoc:
|
||||
|
||||
# Closes the SSL stream before closing the dRuby connection.
|
||||
def close # :nodoc:
|
||||
if @ssl
|
||||
@ssl.close
|
||||
@ssl = nil
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
def accept # :nodoc:
|
||||
begin
|
||||
while true
|
||||
soc = @socket.accept
|
||||
break if (@acl ? @acl.allow_socket?(soc) : true)
|
||||
soc.close
|
||||
end
|
||||
begin
|
||||
ssl = @config.accept(soc)
|
||||
rescue Exception
|
||||
soc.close
|
||||
raise
|
||||
end
|
||||
self.class.new(uri, ssl, @config, true)
|
||||
rescue OpenSSL::SSL::SSLError
|
||||
warn("#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})") if @config[:verbose]
|
||||
retry
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
DRbProtocol.add_protocol(DRbSSLSocket)
|
||||
end
|
||||
101
ruby/lib/ruby/2.1.0/drb/timeridconv.rb
Normal file
101
ruby/lib/ruby/2.1.0/drb/timeridconv.rb
Normal file
@@ -0,0 +1,101 @@
|
||||
require 'drb/drb'
|
||||
require 'monitor'
|
||||
|
||||
module DRb
|
||||
|
||||
# Timer id conversion keeps objects alive for a certain amount of time after
|
||||
# their last access. The default time period is 600 seconds and can be
|
||||
# changed upon initialization.
|
||||
#
|
||||
# To use TimerIdConv:
|
||||
#
|
||||
# DRb.install_id_conv TimerIdConv.new 60 # one minute
|
||||
|
||||
class TimerIdConv < DRbIdConv
|
||||
class TimerHolder2 # :nodoc:
|
||||
include MonitorMixin
|
||||
|
||||
class InvalidIndexError < RuntimeError; end
|
||||
|
||||
def initialize(timeout=600)
|
||||
super()
|
||||
@sentinel = Object.new
|
||||
@gc = {}
|
||||
@curr = {}
|
||||
@renew = {}
|
||||
@timeout = timeout
|
||||
@keeper = keeper
|
||||
end
|
||||
|
||||
def add(obj)
|
||||
synchronize do
|
||||
key = obj.__id__
|
||||
@curr[key] = obj
|
||||
return key
|
||||
end
|
||||
end
|
||||
|
||||
def fetch(key, dv=@sentinel)
|
||||
synchronize do
|
||||
obj = peek(key)
|
||||
if obj == @sentinel
|
||||
return dv unless dv == @sentinel
|
||||
raise InvalidIndexError
|
||||
end
|
||||
@renew[key] = obj # KeepIt
|
||||
return obj
|
||||
end
|
||||
end
|
||||
|
||||
def include?(key)
|
||||
synchronize do
|
||||
obj = peek(key)
|
||||
return false if obj == @sentinel
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def peek(key)
|
||||
synchronize do
|
||||
return @curr.fetch(key, @renew.fetch(key, @gc.fetch(key, @sentinel)))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def alternate
|
||||
synchronize do
|
||||
@gc = @curr # GCed
|
||||
@curr = @renew
|
||||
@renew = {}
|
||||
end
|
||||
end
|
||||
|
||||
def keeper
|
||||
Thread.new do
|
||||
loop do
|
||||
alternate
|
||||
sleep(@timeout)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Creates a new TimerIdConv which will hold objects for +timeout+ seconds.
|
||||
def initialize(timeout=600)
|
||||
@holder = TimerHolder2.new(timeout)
|
||||
end
|
||||
|
||||
def to_obj(ref) # :nodoc:
|
||||
return super if ref.nil?
|
||||
@holder.fetch(ref)
|
||||
rescue TimerHolder2::InvalidIndexError
|
||||
raise "invalid reference"
|
||||
end
|
||||
|
||||
def to_id(obj) # :nodoc:
|
||||
return @holder.add(obj)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DRb.install_id_conv(TimerIdConv.new)
|
||||
115
ruby/lib/ruby/2.1.0/drb/unix.rb
Normal file
115
ruby/lib/ruby/2.1.0/drb/unix.rb
Normal file
@@ -0,0 +1,115 @@
|
||||
require 'socket'
|
||||
require 'drb/drb'
|
||||
require 'tmpdir'
|
||||
|
||||
raise(LoadError, "UNIXServer is required") unless defined?(UNIXServer)
|
||||
|
||||
module DRb
|
||||
|
||||
# Implements DRb over a UNIX socket
|
||||
#
|
||||
# DRb UNIX socket URIs look like <code>drbunix:<path>?<option></code>. The
|
||||
# option is optional.
|
||||
|
||||
class DRbUNIXSocket < DRbTCPSocket
|
||||
# :stopdoc:
|
||||
def self.parse_uri(uri)
|
||||
if /^drbunix:(.*?)(\?(.*))?$/ =~ uri
|
||||
filename = $1
|
||||
option = $3
|
||||
[filename, option]
|
||||
else
|
||||
raise(DRbBadScheme, uri) unless uri =~ /^drbunix:/
|
||||
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
||||
end
|
||||
end
|
||||
|
||||
def self.open(uri, config)
|
||||
filename, = parse_uri(uri)
|
||||
filename.untaint
|
||||
soc = UNIXSocket.open(filename)
|
||||
self.new(uri, soc, config)
|
||||
end
|
||||
|
||||
def self.open_server(uri, config)
|
||||
filename, = parse_uri(uri)
|
||||
if filename.size == 0
|
||||
soc = temp_server
|
||||
filename = soc.path
|
||||
uri = 'drbunix:' + soc.path
|
||||
else
|
||||
soc = UNIXServer.open(filename)
|
||||
end
|
||||
owner = config[:UNIXFileOwner]
|
||||
group = config[:UNIXFileGroup]
|
||||
if owner || group
|
||||
require 'etc'
|
||||
owner = Etc.getpwnam( owner ).uid if owner
|
||||
group = Etc.getgrnam( group ).gid if group
|
||||
File.chown owner, group, filename
|
||||
end
|
||||
mode = config[:UNIXFileMode]
|
||||
File.chmod(mode, filename) if mode
|
||||
|
||||
self.new(uri, soc, config, true)
|
||||
end
|
||||
|
||||
def self.uri_option(uri, config)
|
||||
filename, option = parse_uri(uri)
|
||||
return "drbunix:#{filename}", option
|
||||
end
|
||||
|
||||
def initialize(uri, soc, config={}, server_mode = false)
|
||||
super(uri, soc, config)
|
||||
set_sockopt(@socket)
|
||||
@server_mode = server_mode
|
||||
@acl = nil
|
||||
end
|
||||
|
||||
# import from tempfile.rb
|
||||
Max_try = 10
|
||||
private
|
||||
def self.temp_server
|
||||
tmpdir = Dir::tmpdir
|
||||
n = 0
|
||||
while true
|
||||
begin
|
||||
tmpname = sprintf('%s/druby%d.%d', tmpdir, $$, n)
|
||||
lock = tmpname + '.lock'
|
||||
unless File.exist?(tmpname) or File.exist?(lock)
|
||||
Dir.mkdir(lock)
|
||||
break
|
||||
end
|
||||
rescue
|
||||
raise "cannot generate tempfile `%s'" % tmpname if n >= Max_try
|
||||
#sleep(1)
|
||||
end
|
||||
n += 1
|
||||
end
|
||||
soc = UNIXServer.new(tmpname)
|
||||
Dir.rmdir(lock)
|
||||
soc
|
||||
end
|
||||
|
||||
public
|
||||
def close
|
||||
return unless @socket
|
||||
path = @socket.path if @server_mode
|
||||
@socket.close
|
||||
File.unlink(path) if @server_mode
|
||||
@socket = nil
|
||||
end
|
||||
|
||||
def accept
|
||||
s = @socket.accept
|
||||
self.class.new(nil, s, @config)
|
||||
end
|
||||
|
||||
def set_sockopt(soc)
|
||||
soc.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC
|
||||
end
|
||||
end
|
||||
|
||||
DRbProtocol.add_protocol(DRbUNIXSocket)
|
||||
# :startdoc:
|
||||
end
|
||||
176
ruby/lib/ruby/2.1.0/e2mmap.rb
Normal file
176
ruby/lib/ruby/2.1.0/e2mmap.rb
Normal file
@@ -0,0 +1,176 @@
|
||||
#
|
||||
#--
|
||||
# e2mmap.rb - for Ruby 1.1
|
||||
# $Release Version: 2.0$
|
||||
# $Revision: 1.10 $
|
||||
# by Keiju ISHITSUKA
|
||||
#
|
||||
#++
|
||||
#
|
||||
# Helper module for easily defining exceptions with predefined messages.
|
||||
#
|
||||
# == Usage
|
||||
#
|
||||
# 1.
|
||||
# class Foo
|
||||
# extend Exception2MessageMapper
|
||||
# def_e2message ExistingExceptionClass, "message..."
|
||||
# def_exception :NewExceptionClass, "message..."[, superclass]
|
||||
# ...
|
||||
# end
|
||||
#
|
||||
# 2.
|
||||
# module Error
|
||||
# extend Exception2MessageMapper
|
||||
# def_e2message ExistingExceptionClass, "message..."
|
||||
# def_exception :NewExceptionClass, "message..."[, superclass]
|
||||
# ...
|
||||
# end
|
||||
# class Foo
|
||||
# include Error
|
||||
# ...
|
||||
# end
|
||||
#
|
||||
# foo = Foo.new
|
||||
# foo.Fail ....
|
||||
#
|
||||
# 3.
|
||||
# module Error
|
||||
# extend Exception2MessageMapper
|
||||
# def_e2message ExistingExceptionClass, "message..."
|
||||
# def_exception :NewExceptionClass, "message..."[, superclass]
|
||||
# ...
|
||||
# end
|
||||
# class Foo
|
||||
# extend Exception2MessageMapper
|
||||
# include Error
|
||||
# ...
|
||||
# end
|
||||
#
|
||||
# Foo.Fail NewExceptionClass, arg...
|
||||
# Foo.Fail ExistingExceptionClass, arg...
|
||||
#
|
||||
#
|
||||
module Exception2MessageMapper
|
||||
@RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
|
||||
|
||||
E2MM = Exception2MessageMapper # :nodoc:
|
||||
|
||||
def E2MM.extend_object(cl)
|
||||
super
|
||||
cl.bind(self) unless cl < E2MM
|
||||
end
|
||||
|
||||
def bind(cl)
|
||||
self.module_eval %[
|
||||
def Raise(err = nil, *rest)
|
||||
Exception2MessageMapper.Raise(self.class, err, *rest)
|
||||
end
|
||||
alias Fail Raise
|
||||
|
||||
def self.included(mod)
|
||||
mod.extend Exception2MessageMapper
|
||||
end
|
||||
]
|
||||
end
|
||||
|
||||
# Fail(err, *rest)
|
||||
# err: exception
|
||||
# rest: message arguments
|
||||
#
|
||||
def Raise(err = nil, *rest)
|
||||
E2MM.Raise(self, err, *rest)
|
||||
end
|
||||
alias Fail Raise
|
||||
alias fail Raise
|
||||
|
||||
# def_e2message(c, m)
|
||||
# c: exception
|
||||
# m: message_form
|
||||
# define exception c with message m.
|
||||
#
|
||||
def def_e2message(c, m)
|
||||
E2MM.def_e2message(self, c, m)
|
||||
end
|
||||
|
||||
# def_exception(n, m, s)
|
||||
# n: exception_name
|
||||
# m: message_form
|
||||
# s: superclass(default: StandardError)
|
||||
# define exception named ``c'' with message m.
|
||||
#
|
||||
def def_exception(n, m, s = StandardError)
|
||||
E2MM.def_exception(self, n, m, s)
|
||||
end
|
||||
|
||||
#
|
||||
# Private definitions.
|
||||
#
|
||||
# {[class, exp] => message, ...}
|
||||
@MessageMap = {}
|
||||
|
||||
# E2MM.def_e2message(k, e, m)
|
||||
# k: class to define exception under.
|
||||
# e: exception
|
||||
# m: message_form
|
||||
# define exception c with message m.
|
||||
#
|
||||
def E2MM.def_e2message(k, c, m)
|
||||
E2MM.instance_eval{@MessageMap[[k, c]] = m}
|
||||
c
|
||||
end
|
||||
|
||||
# E2MM.def_exception(k, n, m, s)
|
||||
# k: class to define exception under.
|
||||
# n: exception_name
|
||||
# m: message_form
|
||||
# s: superclass(default: StandardError)
|
||||
# define exception named ``c'' with message m.
|
||||
#
|
||||
def E2MM.def_exception(k, n, m, s = StandardError)
|
||||
n = n.id2name if n.kind_of?(Fixnum)
|
||||
e = Class.new(s)
|
||||
E2MM.instance_eval{@MessageMap[[k, e]] = m}
|
||||
k.const_set(n, e)
|
||||
end
|
||||
|
||||
# Fail(klass, err, *rest)
|
||||
# klass: class to define exception under.
|
||||
# err: exception
|
||||
# rest: message arguments
|
||||
#
|
||||
def E2MM.Raise(klass = E2MM, err = nil, *rest)
|
||||
if form = e2mm_message(klass, err)
|
||||
b = $@.nil? ? caller(1) : $@
|
||||
#p $@
|
||||
#p __FILE__
|
||||
b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
|
||||
raise err, sprintf(form, *rest), b
|
||||
else
|
||||
E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
|
||||
end
|
||||
end
|
||||
class << E2MM
|
||||
alias Fail Raise
|
||||
end
|
||||
|
||||
def E2MM.e2mm_message(klass, exp)
|
||||
for c in klass.ancestors
|
||||
if mes = @MessageMap[[c,exp]]
|
||||
#p mes
|
||||
m = klass.instance_eval('"' + mes + '"')
|
||||
return m
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
class << self
|
||||
alias message e2mm_message
|
||||
end
|
||||
|
||||
E2MM.def_exception(E2MM,
|
||||
:ErrNotRegisteredException,
|
||||
"not registered exception(%s)")
|
||||
end
|
||||
|
||||
|
||||
1009
ruby/lib/ruby/2.1.0/erb.rb
Normal file
1009
ruby/lib/ruby/2.1.0/erb.rb
Normal file
File diff suppressed because it is too large
Load Diff
55
ruby/lib/ruby/2.1.0/fiddle.rb
Normal file
55
ruby/lib/ruby/2.1.0/fiddle.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
require 'fiddle.so'
|
||||
require 'fiddle/function'
|
||||
require 'fiddle/closure'
|
||||
|
||||
module Fiddle
|
||||
if WINDOWS
|
||||
# Returns the last win32 +Error+ of the current executing +Thread+ or nil
|
||||
# if none
|
||||
def self.win32_last_error
|
||||
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__]
|
||||
end
|
||||
|
||||
# Sets the last win32 +Error+ of the current executing +Thread+ to +error+
|
||||
def self.win32_last_error= error
|
||||
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the last +Error+ of the current executing +Thread+ or nil if none
|
||||
def self.last_error
|
||||
Thread.current[:__FIDDLE_LAST_ERROR__]
|
||||
end
|
||||
|
||||
# Sets the last +Error+ of the current executing +Thread+ to +error+
|
||||
def self.last_error= error
|
||||
Thread.current[:__DL2_LAST_ERROR__] = error
|
||||
Thread.current[:__FIDDLE_LAST_ERROR__] = error
|
||||
end
|
||||
|
||||
# call-seq: dlopen(library) => Fiddle::Handle
|
||||
#
|
||||
# Creates a new handler that opens +library+, and returns an instance of
|
||||
# Fiddle::Handle.
|
||||
#
|
||||
# If +nil+ is given for the +library+, Fiddle::Handle::DEFAULT is used, which
|
||||
# is the equivalent to RTLD_DEFAULT. See <code>man 3 dlopen</code> for more.
|
||||
#
|
||||
# lib = Fiddle.dlopen(nil)
|
||||
#
|
||||
# The default is dependent on OS, and provide a handle for all libraries
|
||||
# already loaded. For example, in most cases you can use this to access
|
||||
# +libc+ functions, or ruby functions like +rb_str_new+.
|
||||
#
|
||||
# See Fiddle::Handle.new for more.
|
||||
def dlopen library
|
||||
Fiddle::Handle.new library
|
||||
end
|
||||
module_function :dlopen
|
||||
|
||||
# Add constants for backwards compat
|
||||
|
||||
RTLD_GLOBAL = Handle::RTLD_GLOBAL # :nodoc:
|
||||
RTLD_LAZY = Handle::RTLD_LAZY # :nodoc:
|
||||
RTLD_NOW = Handle::RTLD_NOW # :nodoc:
|
||||
end
|
||||
48
ruby/lib/ruby/2.1.0/fiddle/closure.rb
Normal file
48
ruby/lib/ruby/2.1.0/fiddle/closure.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
module Fiddle
|
||||
class Closure
|
||||
|
||||
# the C type of the return of the FFI closure
|
||||
attr_reader :ctype
|
||||
|
||||
# arguments of the FFI closure
|
||||
attr_reader :args
|
||||
|
||||
# Extends Fiddle::Closure to allow for building the closure in a block
|
||||
class BlockCaller < Fiddle::Closure
|
||||
|
||||
# == Description
|
||||
#
|
||||
# Construct a new BlockCaller object.
|
||||
#
|
||||
# * +ctype+ is the C type to be returned
|
||||
# * +args+ are passed the callback
|
||||
# * +abi+ is the abi of the closure
|
||||
#
|
||||
# If there is an error in preparing the +ffi_cif+ or +ffi_prep_closure+,
|
||||
# then a RuntimeError will be raised.
|
||||
#
|
||||
# == Example
|
||||
#
|
||||
# include Fiddle
|
||||
#
|
||||
# cb = Closure::BlockCaller.new(TYPE_INT, [TYPE_INT]) do |one|
|
||||
# one
|
||||
# end
|
||||
#
|
||||
# func = Function.new(cb, [TYPE_INT], TYPE_INT)
|
||||
#
|
||||
def initialize ctype, args, abi = Fiddle::Function::DEFAULT, &block
|
||||
super(ctype, args, abi)
|
||||
@block = block
|
||||
end
|
||||
|
||||
# Calls the constructed BlockCaller, with +args+
|
||||
#
|
||||
# For an example see Fiddle::Closure::BlockCaller.new
|
||||
#
|
||||
def call *args
|
||||
@block.call(*args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
176
ruby/lib/ruby/2.1.0/fiddle/cparser.rb
Normal file
176
ruby/lib/ruby/2.1.0/fiddle/cparser.rb
Normal file
@@ -0,0 +1,176 @@
|
||||
module Fiddle
|
||||
# A mixin that provides methods for parsing C struct and prototype signatures.
|
||||
#
|
||||
# == Example
|
||||
# require 'fiddle/import'
|
||||
#
|
||||
# include Fiddle::CParser
|
||||
# #=> Object
|
||||
#
|
||||
# parse_ctype('int increment(int)')
|
||||
# #=> ["increment", Fiddle::TYPE_INT, [Fiddle::TYPE_INT]]
|
||||
#
|
||||
module CParser
|
||||
# Parses a C struct's members
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# include Fiddle::CParser
|
||||
# #=> Object
|
||||
#
|
||||
# parse_struct_signature(['int i', 'char c'])
|
||||
# #=> [[Fiddle::TYPE_INT, Fiddle::TYPE_CHAR], ["i", "c"]]
|
||||
#
|
||||
def parse_struct_signature(signature, tymap=nil)
|
||||
if( signature.is_a?(String) )
|
||||
signature = signature.split(/\s*,\s*/)
|
||||
end
|
||||
mems = []
|
||||
tys = []
|
||||
signature.each{|msig|
|
||||
tks = msig.split(/\s+(\*)?/)
|
||||
ty = tks[0..-2].join(" ")
|
||||
member = tks[-1]
|
||||
|
||||
case ty
|
||||
when /\[(\d+)\]/
|
||||
n = $1.to_i
|
||||
ty.gsub!(/\s*\[\d+\]/,"")
|
||||
ty = [ty, n]
|
||||
when /\[\]/
|
||||
ty.gsub!(/\s*\[\]/, "*")
|
||||
end
|
||||
|
||||
case member
|
||||
when /\[(\d+)\]/
|
||||
ty = [ty, $1.to_i]
|
||||
member.gsub!(/\s*\[\d+\]/,"")
|
||||
when /\[\]/
|
||||
ty = ty + "*"
|
||||
member.gsub!(/\s*\[\]/, "")
|
||||
end
|
||||
|
||||
mems.push(member)
|
||||
tys.push(parse_ctype(ty,tymap))
|
||||
}
|
||||
return tys, mems
|
||||
end
|
||||
|
||||
# Parses a C prototype signature
|
||||
#
|
||||
# If Hash +tymap+ is provided, the return value and the arguments from the
|
||||
# +signature+ are expected to be keys, and the value will be the C type to
|
||||
# be looked up.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# include Fiddle::CParser
|
||||
# #=> Object
|
||||
#
|
||||
# parse_signature('double sum(double, double)')
|
||||
# #=> ["sum", Fiddle::TYPE_DOUBLE, [Fiddle::TYPE_DOUBLE, Fiddle::TYPE_DOUBLE]]
|
||||
#
|
||||
def parse_signature(signature, tymap=nil)
|
||||
tymap ||= {}
|
||||
signature = signature.gsub(/\s+/, " ").strip
|
||||
case signature
|
||||
when /^([\w@\*\s]+)\(([\w\*\s\,\[\]]*)\)$/
|
||||
ret = $1
|
||||
(args = $2).strip!
|
||||
ret = ret.split(/\s+/)
|
||||
args = args.split(/\s*,\s*/)
|
||||
func = ret.pop
|
||||
if( func =~ /^\*/ )
|
||||
func.gsub!(/^\*+/,"")
|
||||
ret.push("*")
|
||||
end
|
||||
ret = ret.join(" ")
|
||||
return [func, parse_ctype(ret, tymap), args.collect{|arg| parse_ctype(arg, tymap)}]
|
||||
else
|
||||
raise(RuntimeError,"can't parse the function prototype: #{signature}")
|
||||
end
|
||||
end
|
||||
|
||||
# Given a String of C type +ty+, returns the corresponding Fiddle constant.
|
||||
#
|
||||
# +ty+ can also accept an Array of C type Strings, and will be returned in
|
||||
# a corresponding Array.
|
||||
#
|
||||
# If Hash +tymap+ is provided, +ty+ is expected to be the key, and the
|
||||
# value will be the C type to be looked up.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# include Fiddle::CParser
|
||||
# #=> Object
|
||||
#
|
||||
# parse_ctype('int')
|
||||
# #=> Fiddle::TYPE_INT
|
||||
#
|
||||
# parse_ctype('double')
|
||||
# #=> Fiddle::TYPE_DOUBLE
|
||||
#
|
||||
# parse_ctype('unsigned char')
|
||||
# #=> -Fiddle::TYPE_CHAR
|
||||
#
|
||||
def parse_ctype(ty, tymap=nil)
|
||||
tymap ||= {}
|
||||
case ty
|
||||
when Array
|
||||
return [parse_ctype(ty[0], tymap), ty[1]]
|
||||
when "void"
|
||||
return TYPE_VOID
|
||||
when "char"
|
||||
return TYPE_CHAR
|
||||
when "unsigned char"
|
||||
return -TYPE_CHAR
|
||||
when "short"
|
||||
return TYPE_SHORT
|
||||
when "unsigned short"
|
||||
return -TYPE_SHORT
|
||||
when "int"
|
||||
return TYPE_INT
|
||||
when "unsigned int", 'uint'
|
||||
return -TYPE_INT
|
||||
when "long"
|
||||
return TYPE_LONG
|
||||
when "unsigned long"
|
||||
return -TYPE_LONG
|
||||
when "long long"
|
||||
if( defined?(TYPE_LONG_LONG) )
|
||||
return TYPE_LONG_LONG
|
||||
else
|
||||
raise(RuntimeError, "unsupported type: #{ty}")
|
||||
end
|
||||
when "unsigned long long"
|
||||
if( defined?(TYPE_LONG_LONG) )
|
||||
return -TYPE_LONG_LONG
|
||||
else
|
||||
raise(RuntimeError, "unsupported type: #{ty}")
|
||||
end
|
||||
when "float"
|
||||
return TYPE_FLOAT
|
||||
when "double"
|
||||
return TYPE_DOUBLE
|
||||
when "size_t"
|
||||
return TYPE_SIZE_T
|
||||
when "ssize_t"
|
||||
return TYPE_SSIZE_T
|
||||
when "ptrdiff_t"
|
||||
return TYPE_PTRDIFF_T
|
||||
when "intptr_t"
|
||||
return TYPE_INTPTR_T
|
||||
when "uintptr_t"
|
||||
return TYPE_UINTPTR_T
|
||||
when /\*/, /\[\s*\]/
|
||||
return TYPE_VOIDP
|
||||
else
|
||||
if( tymap[ty] )
|
||||
return parse_ctype(tymap[ty], tymap)
|
||||
else
|
||||
raise(DLError, "unknown type: #{ty}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
ruby/lib/ruby/2.1.0/fiddle/function.rb
Normal file
17
ruby/lib/ruby/2.1.0/fiddle/function.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module Fiddle
|
||||
class Function
|
||||
# The ABI of the Function.
|
||||
attr_reader :abi
|
||||
|
||||
# The address of this function
|
||||
attr_reader :ptr
|
||||
|
||||
# The name of this function
|
||||
attr_reader :name
|
||||
|
||||
# The integer memory location of this function
|
||||
def to_i
|
||||
ptr.to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
314
ruby/lib/ruby/2.1.0/fiddle/import.rb
Normal file
314
ruby/lib/ruby/2.1.0/fiddle/import.rb
Normal file
@@ -0,0 +1,314 @@
|
||||
require 'fiddle'
|
||||
require 'fiddle/struct'
|
||||
require 'fiddle/cparser'
|
||||
|
||||
module Fiddle
|
||||
|
||||
# Used internally by Fiddle::Importer
|
||||
class CompositeHandler
|
||||
# Create a new handler with the open +handlers+
|
||||
#
|
||||
# Used internally by Fiddle::Importer.dlload
|
||||
def initialize(handlers)
|
||||
@handlers = handlers
|
||||
end
|
||||
|
||||
# Array of the currently loaded libraries.
|
||||
def handlers()
|
||||
@handlers
|
||||
end
|
||||
|
||||
# Returns the address as an Integer from any handlers with the function
|
||||
# named +symbol+.
|
||||
#
|
||||
# Raises a DLError if the handle is closed.
|
||||
def sym(symbol)
|
||||
@handlers.each{|handle|
|
||||
if( handle )
|
||||
begin
|
||||
addr = handle.sym(symbol)
|
||||
return addr
|
||||
rescue DLError
|
||||
end
|
||||
end
|
||||
}
|
||||
return nil
|
||||
end
|
||||
|
||||
# See Fiddle::CompositeHandler.sym
|
||||
def [](symbol)
|
||||
sym(symbol)
|
||||
end
|
||||
end
|
||||
|
||||
# A DSL that provides the means to dynamically load libraries and build
|
||||
# modules around them including calling extern functions within the C
|
||||
# library that has been loaded.
|
||||
#
|
||||
# == Example
|
||||
#
|
||||
# require 'fiddle'
|
||||
# require 'fiddle/import'
|
||||
#
|
||||
# module LibSum
|
||||
# extend Fiddle::Importer
|
||||
# dlload './libsum.so'
|
||||
# extern 'double sum(double*, int)'
|
||||
# extern 'double split(double)'
|
||||
# end
|
||||
#
|
||||
module Importer
|
||||
include Fiddle
|
||||
include CParser
|
||||
extend Importer
|
||||
|
||||
# Creates an array of handlers for the given +libs+, can be an instance of
|
||||
# Fiddle::Handle, Fiddle::Importer, or will create a new istance of
|
||||
# Fiddle::Handle using Fiddle.dlopen
|
||||
#
|
||||
# Raises a DLError if the library cannot be loaded.
|
||||
#
|
||||
# See Fiddle.dlopen
|
||||
def dlload(*libs)
|
||||
handles = libs.collect{|lib|
|
||||
case lib
|
||||
when nil
|
||||
nil
|
||||
when Handle
|
||||
lib
|
||||
when Importer
|
||||
lib.handlers
|
||||
else
|
||||
begin
|
||||
Fiddle.dlopen(lib)
|
||||
rescue DLError
|
||||
raise(DLError, "can't load #{lib}")
|
||||
end
|
||||
end
|
||||
}.flatten()
|
||||
@handler = CompositeHandler.new(handles)
|
||||
@func_map = {}
|
||||
@type_alias = {}
|
||||
end
|
||||
|
||||
# Sets the type alias for +alias_type+ as +orig_type+
|
||||
def typealias(alias_type, orig_type)
|
||||
@type_alias[alias_type] = orig_type
|
||||
end
|
||||
|
||||
# Returns the sizeof +ty+, using Fiddle::Importer.parse_ctype to determine
|
||||
# the C type and the appropriate Fiddle constant.
|
||||
def sizeof(ty)
|
||||
case ty
|
||||
when String
|
||||
ty = parse_ctype(ty, @type_alias).abs()
|
||||
case ty
|
||||
when TYPE_CHAR
|
||||
return SIZEOF_CHAR
|
||||
when TYPE_SHORT
|
||||
return SIZEOF_SHORT
|
||||
when TYPE_INT
|
||||
return SIZEOF_INT
|
||||
when TYPE_LONG
|
||||
return SIZEOF_LONG
|
||||
when TYPE_LONG_LONG
|
||||
return SIZEOF_LONG_LONG
|
||||
when TYPE_FLOAT
|
||||
return SIZEOF_FLOAT
|
||||
when TYPE_DOUBLE
|
||||
return SIZEOF_DOUBLE
|
||||
when TYPE_VOIDP
|
||||
return SIZEOF_VOIDP
|
||||
else
|
||||
raise(DLError, "unknown type: #{ty}")
|
||||
end
|
||||
when Class
|
||||
if( ty.instance_methods().include?(:to_ptr) )
|
||||
return ty.size()
|
||||
end
|
||||
end
|
||||
return Pointer[ty].size()
|
||||
end
|
||||
|
||||
def parse_bind_options(opts)
|
||||
h = {}
|
||||
while( opt = opts.shift() )
|
||||
case opt
|
||||
when :stdcall, :cdecl
|
||||
h[:call_type] = opt
|
||||
when :carried, :temp, :temporal, :bind
|
||||
h[:callback_type] = opt
|
||||
h[:carrier] = opts.shift()
|
||||
else
|
||||
h[opt] = true
|
||||
end
|
||||
end
|
||||
h
|
||||
end
|
||||
private :parse_bind_options
|
||||
|
||||
# :stopdoc:
|
||||
CALL_TYPE_TO_ABI = Hash.new { |h, k|
|
||||
raise RuntimeError, "unsupported call type: #{k}"
|
||||
}.merge({ :stdcall => (Function::STDCALL rescue Function::DEFAULT),
|
||||
:cdecl => Function::DEFAULT,
|
||||
nil => Function::DEFAULT
|
||||
}).freeze
|
||||
private_constant :CALL_TYPE_TO_ABI
|
||||
# :startdoc:
|
||||
|
||||
# Creates a global method from the given C +signature+.
|
||||
def extern(signature, *opts)
|
||||
symname, ctype, argtype = parse_signature(signature, @type_alias)
|
||||
opt = parse_bind_options(opts)
|
||||
f = import_function(symname, ctype, argtype, opt[:call_type])
|
||||
name = symname.gsub(/@.+/,'')
|
||||
@func_map[name] = f
|
||||
# define_method(name){|*args,&block| f.call(*args,&block)}
|
||||
begin
|
||||
/^(.+?):(\d+)/ =~ caller.first
|
||||
file, line = $1, $2.to_i
|
||||
rescue
|
||||
file, line = __FILE__, __LINE__+3
|
||||
end
|
||||
module_eval(<<-EOS, file, line)
|
||||
def #{name}(*args, &block)
|
||||
@func_map['#{name}'].call(*args,&block)
|
||||
end
|
||||
EOS
|
||||
module_function(name)
|
||||
f
|
||||
end
|
||||
|
||||
# Creates a global method from the given C +signature+ using the given
|
||||
# +opts+ as bind parameters with the given block.
|
||||
def bind(signature, *opts, &blk)
|
||||
name, ctype, argtype = parse_signature(signature, @type_alias)
|
||||
h = parse_bind_options(opts)
|
||||
case h[:callback_type]
|
||||
when :bind, nil
|
||||
f = bind_function(name, ctype, argtype, h[:call_type], &blk)
|
||||
else
|
||||
raise(RuntimeError, "unknown callback type: #{h[:callback_type]}")
|
||||
end
|
||||
@func_map[name] = f
|
||||
#define_method(name){|*args,&block| f.call(*args,&block)}
|
||||
begin
|
||||
/^(.+?):(\d+)/ =~ caller.first
|
||||
file, line = $1, $2.to_i
|
||||
rescue
|
||||
file, line = __FILE__, __LINE__+3
|
||||
end
|
||||
module_eval(<<-EOS, file, line)
|
||||
def #{name}(*args,&block)
|
||||
@func_map['#{name}'].call(*args,&block)
|
||||
end
|
||||
EOS
|
||||
module_function(name)
|
||||
f
|
||||
end
|
||||
|
||||
# Creates a class to wrap the C struct described by +signature+.
|
||||
#
|
||||
# MyStruct = struct ['int i', 'char c']
|
||||
def struct(signature)
|
||||
tys, mems = parse_struct_signature(signature, @type_alias)
|
||||
Fiddle::CStructBuilder.create(CStruct, tys, mems)
|
||||
end
|
||||
|
||||
# Creates a class to wrap the C union described by +signature+.
|
||||
#
|
||||
# MyUnion = union ['int i', 'char c']
|
||||
def union(signature)
|
||||
tys, mems = parse_struct_signature(signature, @type_alias)
|
||||
Fiddle::CStructBuilder.create(CUnion, tys, mems)
|
||||
end
|
||||
|
||||
# Returns the function mapped to +name+, that was created by either
|
||||
# Fiddle::Importer.extern or Fiddle::Importer.bind
|
||||
def [](name)
|
||||
@func_map[name]
|
||||
end
|
||||
|
||||
# Creates a class to wrap the C struct with the value +ty+
|
||||
#
|
||||
# See also Fiddle::Importer.struct
|
||||
def create_value(ty, val=nil)
|
||||
s = struct([ty + " value"])
|
||||
ptr = s.malloc()
|
||||
if( val )
|
||||
ptr.value = val
|
||||
end
|
||||
return ptr
|
||||
end
|
||||
alias value create_value
|
||||
|
||||
# Returns a new instance of the C struct with the value +ty+ at the +addr+
|
||||
# address.
|
||||
def import_value(ty, addr)
|
||||
s = struct([ty + " value"])
|
||||
ptr = s.new(addr)
|
||||
return ptr
|
||||
end
|
||||
|
||||
|
||||
# The Fiddle::CompositeHandler instance
|
||||
#
|
||||
# Will raise an error if no handlers are open.
|
||||
def handler
|
||||
@handler or raise "call dlload before importing symbols and functions"
|
||||
end
|
||||
|
||||
# Returns a new Fiddle::Pointer instance at the memory address of the given
|
||||
# +name+ symbol.
|
||||
#
|
||||
# Raises a DLError if the +name+ doesn't exist.
|
||||
#
|
||||
# See Fiddle::CompositeHandler.sym and Fiddle::Handle.sym
|
||||
def import_symbol(name)
|
||||
addr = handler.sym(name)
|
||||
if( !addr )
|
||||
raise(DLError, "cannot find the symbol: #{name}")
|
||||
end
|
||||
Pointer.new(addr)
|
||||
end
|
||||
|
||||
# Returns a new Fiddle::Function instance at the memory address of the given
|
||||
# +name+ function.
|
||||
#
|
||||
# Raises a DLError if the +name+ doesn't exist.
|
||||
#
|
||||
# * +argtype+ is an Array of arguments, passed to the +name+ function.
|
||||
# * +ctype+ is the return type of the function
|
||||
# * +call_type+ is the ABI of the function
|
||||
#
|
||||
# See also Fiddle:Function.new
|
||||
#
|
||||
# See Fiddle::CompositeHandler.sym and Fiddle::Handler.sym
|
||||
def import_function(name, ctype, argtype, call_type = nil)
|
||||
addr = handler.sym(name)
|
||||
if( !addr )
|
||||
raise(DLError, "cannot find the function: #{name}()")
|
||||
end
|
||||
Function.new(addr, argtype, ctype, CALL_TYPE_TO_ABI[call_type],
|
||||
name: name)
|
||||
end
|
||||
|
||||
# Returns a new closure wrapper for the +name+ function.
|
||||
#
|
||||
# * +ctype+ is the return type of the function
|
||||
# * +argtype+ is an Array of arguments, passed to the callback function
|
||||
# * +call_type+ is the abi of the closure
|
||||
# * +block+ is passed to the callback
|
||||
#
|
||||
# See Fiddle::Closure
|
||||
def bind_function(name, ctype, argtype, call_type = nil, &block)
|
||||
abi = CALL_TYPE_TO_ABI[call_type]
|
||||
closure = Class.new(Fiddle::Closure) {
|
||||
define_method(:call, block)
|
||||
}.new(ctype, argtype, abi)
|
||||
|
||||
Function.new(closure, argtype, ctype, abi, name: name)
|
||||
end
|
||||
end
|
||||
end
|
||||
128
ruby/lib/ruby/2.1.0/fiddle/pack.rb
Normal file
128
ruby/lib/ruby/2.1.0/fiddle/pack.rb
Normal file
@@ -0,0 +1,128 @@
|
||||
require 'fiddle'
|
||||
|
||||
module Fiddle
|
||||
module PackInfo # :nodoc: all
|
||||
ALIGN_MAP = {
|
||||
TYPE_VOIDP => ALIGN_VOIDP,
|
||||
TYPE_CHAR => ALIGN_CHAR,
|
||||
TYPE_SHORT => ALIGN_SHORT,
|
||||
TYPE_INT => ALIGN_INT,
|
||||
TYPE_LONG => ALIGN_LONG,
|
||||
TYPE_FLOAT => ALIGN_FLOAT,
|
||||
TYPE_DOUBLE => ALIGN_DOUBLE,
|
||||
-TYPE_CHAR => ALIGN_CHAR,
|
||||
-TYPE_SHORT => ALIGN_SHORT,
|
||||
-TYPE_INT => ALIGN_INT,
|
||||
-TYPE_LONG => ALIGN_LONG,
|
||||
}
|
||||
|
||||
PACK_MAP = {
|
||||
TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG) ? "q" : "l!"),
|
||||
TYPE_CHAR => "c",
|
||||
TYPE_SHORT => "s!",
|
||||
TYPE_INT => "i!",
|
||||
TYPE_LONG => "l!",
|
||||
TYPE_FLOAT => "f",
|
||||
TYPE_DOUBLE => "d",
|
||||
-TYPE_CHAR => "c",
|
||||
-TYPE_SHORT => "s!",
|
||||
-TYPE_INT => "i!",
|
||||
-TYPE_LONG => "l!",
|
||||
}
|
||||
|
||||
SIZE_MAP = {
|
||||
TYPE_VOIDP => SIZEOF_VOIDP,
|
||||
TYPE_CHAR => SIZEOF_CHAR,
|
||||
TYPE_SHORT => SIZEOF_SHORT,
|
||||
TYPE_INT => SIZEOF_INT,
|
||||
TYPE_LONG => SIZEOF_LONG,
|
||||
TYPE_FLOAT => SIZEOF_FLOAT,
|
||||
TYPE_DOUBLE => SIZEOF_DOUBLE,
|
||||
-TYPE_CHAR => SIZEOF_CHAR,
|
||||
-TYPE_SHORT => SIZEOF_SHORT,
|
||||
-TYPE_INT => SIZEOF_INT,
|
||||
-TYPE_LONG => SIZEOF_LONG,
|
||||
}
|
||||
if defined?(TYPE_LONG_LONG)
|
||||
ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_MAP[-TYPE_LONG_LONG] = ALIGN_LONG_LONG
|
||||
PACK_MAP[TYPE_LONG_LONG] = PACK_MAP[-TYPE_LONG_LONG] = "q"
|
||||
SIZE_MAP[TYPE_LONG_LONG] = SIZE_MAP[-TYPE_LONG_LONG] = SIZEOF_LONG_LONG
|
||||
end
|
||||
|
||||
def align(addr, align)
|
||||
d = addr % align
|
||||
if( d == 0 )
|
||||
addr
|
||||
else
|
||||
addr + (align - d)
|
||||
end
|
||||
end
|
||||
module_function :align
|
||||
end
|
||||
|
||||
class Packer # :nodoc: all
|
||||
include PackInfo
|
||||
|
||||
def self.[](*types)
|
||||
new(types)
|
||||
end
|
||||
|
||||
def initialize(types)
|
||||
parse_types(types)
|
||||
end
|
||||
|
||||
def size()
|
||||
@size
|
||||
end
|
||||
|
||||
def pack(ary)
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
ary.pack(@template)
|
||||
when SIZEOF_LONG_LONG
|
||||
ary.pack(@template)
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
|
||||
def unpack(ary)
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
ary.join().unpack(@template)
|
||||
when SIZEOF_LONG_LONG
|
||||
ary.join().unpack(@template)
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_types(types)
|
||||
@template = ""
|
||||
addr = 0
|
||||
types.each{|t|
|
||||
orig_addr = addr
|
||||
if( t.is_a?(Array) )
|
||||
addr = align(orig_addr, ALIGN_MAP[TYPE_VOIDP])
|
||||
else
|
||||
addr = align(orig_addr, ALIGN_MAP[t])
|
||||
end
|
||||
d = addr - orig_addr
|
||||
if( d > 0 )
|
||||
@template << "x#{d}"
|
||||
end
|
||||
if( t.is_a?(Array) )
|
||||
@template << (PACK_MAP[t[0]] * t[1])
|
||||
addr += (SIZE_MAP[t[0]] * t[1])
|
||||
else
|
||||
@template << PACK_MAP[t]
|
||||
addr += SIZE_MAP[t]
|
||||
end
|
||||
}
|
||||
addr = align(addr, ALIGN_MAP[TYPE_VOIDP])
|
||||
@size = addr
|
||||
end
|
||||
end
|
||||
end
|
||||
243
ruby/lib/ruby/2.1.0/fiddle/struct.rb
Normal file
243
ruby/lib/ruby/2.1.0/fiddle/struct.rb
Normal file
@@ -0,0 +1,243 @@
|
||||
require 'fiddle'
|
||||
require 'fiddle/value'
|
||||
require 'fiddle/pack'
|
||||
|
||||
module Fiddle
|
||||
# C struct shell
|
||||
class CStruct
|
||||
# accessor to Fiddle::CStructEntity
|
||||
def CStruct.entity_class
|
||||
CStructEntity
|
||||
end
|
||||
end
|
||||
|
||||
# C union shell
|
||||
class CUnion
|
||||
# accessor to Fiddle::CUnionEntity
|
||||
def CUnion.entity_class
|
||||
CUnionEntity
|
||||
end
|
||||
end
|
||||
|
||||
# Used to construct C classes (CUnion, CStruct, etc)
|
||||
#
|
||||
# Fiddle::Importer#struct and Fiddle::Importer#union wrap this functionality in an
|
||||
# easy-to-use manner.
|
||||
module CStructBuilder
|
||||
# Construct a new class given a C:
|
||||
# * class +klass+ (CUnion, CStruct, or other that provide an
|
||||
# #entity_class)
|
||||
# * +types+ (Fiddle::TYPE_INT, Fiddle::TYPE_SIZE_T, etc., see the C types
|
||||
# constants)
|
||||
# * corresponding +members+
|
||||
#
|
||||
# Fiddle::Importer#struct and Fiddle::Importer#union wrap this functionality in an
|
||||
# easy-to-use manner.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# require 'fiddle/struct'
|
||||
# require 'fiddle/cparser'
|
||||
#
|
||||
# include Fiddle::CParser
|
||||
#
|
||||
# types, members = parse_struct_signature(['int i','char c'])
|
||||
#
|
||||
# MyStruct = Fiddle::CStructBuilder.create(Fiddle::CUnion, types, members)
|
||||
#
|
||||
# obj = MyStruct.allocate
|
||||
#
|
||||
def create(klass, types, members)
|
||||
new_class = Class.new(klass){
|
||||
define_method(:initialize){|addr|
|
||||
@entity = klass.entity_class.new(addr, types)
|
||||
@entity.assign_names(members)
|
||||
}
|
||||
define_method(:to_ptr){ @entity }
|
||||
define_method(:to_i){ @entity.to_i }
|
||||
members.each{|name|
|
||||
define_method(name){ @entity[name] }
|
||||
define_method(name + "="){|val| @entity[name] = val }
|
||||
}
|
||||
}
|
||||
size = klass.entity_class.size(types)
|
||||
new_class.module_eval(<<-EOS, __FILE__, __LINE__+1)
|
||||
def new_class.size()
|
||||
#{size}
|
||||
end
|
||||
def new_class.malloc()
|
||||
addr = Fiddle.malloc(#{size})
|
||||
new(addr)
|
||||
end
|
||||
EOS
|
||||
return new_class
|
||||
end
|
||||
module_function :create
|
||||
end
|
||||
|
||||
# A C struct wrapper
|
||||
class CStructEntity < Fiddle::Pointer
|
||||
include PackInfo
|
||||
include ValueUtil
|
||||
|
||||
# Allocates a C struct with the +types+ provided.
|
||||
#
|
||||
# When the instance is garbage collected, the C function +func+ is called.
|
||||
def CStructEntity.malloc(types, func = nil)
|
||||
addr = Fiddle.malloc(CStructEntity.size(types))
|
||||
CStructEntity.new(addr, types, func)
|
||||
end
|
||||
|
||||
# Returns the offset for the packed sizes for the given +types+.
|
||||
#
|
||||
# Fiddle::CStructEntity.size(
|
||||
# [ Fiddle::TYPE_DOUBLE,
|
||||
# Fiddle::TYPE_INT,
|
||||
# Fiddle::TYPE_CHAR,
|
||||
# Fiddle::TYPE_VOIDP ]) #=> 24
|
||||
def CStructEntity.size(types)
|
||||
offset = 0
|
||||
|
||||
max_align = types.map { |type, count = 1|
|
||||
last_offset = offset
|
||||
|
||||
align = PackInfo::ALIGN_MAP[type]
|
||||
offset = PackInfo.align(last_offset, align) +
|
||||
(PackInfo::SIZE_MAP[type] * count)
|
||||
|
||||
align
|
||||
}.max
|
||||
|
||||
PackInfo.align(offset, max_align)
|
||||
end
|
||||
|
||||
# Wraps the C pointer +addr+ as a C struct with the given +types+.
|
||||
#
|
||||
# When the instance is garbage collected, the C function +func+ is called.
|
||||
#
|
||||
# See also Fiddle::Pointer.new
|
||||
def initialize(addr, types, func = nil)
|
||||
set_ctypes(types)
|
||||
super(addr, @size, func)
|
||||
end
|
||||
|
||||
# Set the names of the +members+ in this C struct
|
||||
def assign_names(members)
|
||||
@members = members
|
||||
end
|
||||
|
||||
# Calculates the offsets and sizes for the given +types+ in the struct.
|
||||
def set_ctypes(types)
|
||||
@ctypes = types
|
||||
@offset = []
|
||||
offset = 0
|
||||
|
||||
max_align = types.map { |type, count = 1|
|
||||
orig_offset = offset
|
||||
align = ALIGN_MAP[type]
|
||||
offset = PackInfo.align(orig_offset, align)
|
||||
|
||||
@offset << offset
|
||||
|
||||
offset += (SIZE_MAP[type] * count)
|
||||
|
||||
align
|
||||
}.max
|
||||
|
||||
@size = PackInfo.align(offset, max_align)
|
||||
end
|
||||
|
||||
# Fetch struct member +name+
|
||||
def [](name)
|
||||
idx = @members.index(name)
|
||||
if( idx.nil? )
|
||||
raise(ArgumentError, "no such member: #{name}")
|
||||
end
|
||||
ty = @ctypes[idx]
|
||||
if( ty.is_a?(Array) )
|
||||
r = super(@offset[idx], SIZE_MAP[ty[0]] * ty[1])
|
||||
else
|
||||
r = super(@offset[idx], SIZE_MAP[ty.abs])
|
||||
end
|
||||
packer = Packer.new([ty])
|
||||
val = packer.unpack([r])
|
||||
case ty
|
||||
when Array
|
||||
case ty[0]
|
||||
when TYPE_VOIDP
|
||||
val = val.collect{|v| Pointer.new(v)}
|
||||
end
|
||||
when TYPE_VOIDP
|
||||
val = Pointer.new(val[0])
|
||||
else
|
||||
val = val[0]
|
||||
end
|
||||
if( ty.is_a?(Integer) && (ty < 0) )
|
||||
return unsigned_value(val, ty)
|
||||
elsif( ty.is_a?(Array) && (ty[0] < 0) )
|
||||
return val.collect{|v| unsigned_value(v,ty[0])}
|
||||
else
|
||||
return val
|
||||
end
|
||||
end
|
||||
|
||||
# Set struct member +name+, to value +val+
|
||||
def []=(name, val)
|
||||
idx = @members.index(name)
|
||||
if( idx.nil? )
|
||||
raise(ArgumentError, "no such member: #{name}")
|
||||
end
|
||||
ty = @ctypes[idx]
|
||||
packer = Packer.new([ty])
|
||||
val = wrap_arg(val, ty, [])
|
||||
buff = packer.pack([val].flatten())
|
||||
super(@offset[idx], buff.size, buff)
|
||||
if( ty.is_a?(Integer) && (ty < 0) )
|
||||
return unsigned_value(val, ty)
|
||||
elsif( ty.is_a?(Array) && (ty[0] < 0) )
|
||||
return val.collect{|v| unsigned_value(v,ty[0])}
|
||||
else
|
||||
return val
|
||||
end
|
||||
end
|
||||
|
||||
def to_s() # :nodoc:
|
||||
super(@size)
|
||||
end
|
||||
end
|
||||
|
||||
# A C union wrapper
|
||||
class CUnionEntity < CStructEntity
|
||||
include PackInfo
|
||||
|
||||
# Allocates a C union the +types+ provided.
|
||||
#
|
||||
# When the instance is garbage collected, the C function +func+ is called.
|
||||
def CUnionEntity.malloc(types, func=nil)
|
||||
addr = Fiddle.malloc(CUnionEntity.size(types))
|
||||
CUnionEntity.new(addr, types, func)
|
||||
end
|
||||
|
||||
# Returns the size needed for the union with the given +types+.
|
||||
#
|
||||
# Fiddle::CUnionEntity.size(
|
||||
# [ Fiddle::TYPE_DOUBLE,
|
||||
# Fiddle::TYPE_INT,
|
||||
# Fiddle::TYPE_CHAR,
|
||||
# Fiddle::TYPE_VOIDP ]) #=> 8
|
||||
def CUnionEntity.size(types)
|
||||
types.map { |type, count = 1|
|
||||
PackInfo::SIZE_MAP[type] * count
|
||||
}.max
|
||||
end
|
||||
|
||||
# Calculate the necessary offset and for each union member with the given
|
||||
# +types+
|
||||
def set_ctypes(types)
|
||||
@ctypes = types
|
||||
@offset = Array.new(types.length, 0)
|
||||
@size = self.class.size types
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
71
ruby/lib/ruby/2.1.0/fiddle/types.rb
Normal file
71
ruby/lib/ruby/2.1.0/fiddle/types.rb
Normal file
@@ -0,0 +1,71 @@
|
||||
module Fiddle
|
||||
# Adds Windows type aliases to the including class for use with
|
||||
# Fiddle::Importer.
|
||||
#
|
||||
# The aliases added are:
|
||||
# * ATOM
|
||||
# * BOOL
|
||||
# * BYTE
|
||||
# * DWORD
|
||||
# * DWORD32
|
||||
# * DWORD64
|
||||
# * HANDLE
|
||||
# * HDC
|
||||
# * HINSTANCE
|
||||
# * HWND
|
||||
# * LPCSTR
|
||||
# * LPSTR
|
||||
# * PBYTE
|
||||
# * PDWORD
|
||||
# * PHANDLE
|
||||
# * PVOID
|
||||
# * PWORD
|
||||
# * UCHAR
|
||||
# * UINT
|
||||
# * ULONG
|
||||
# * WORD
|
||||
module Win32Types
|
||||
def included(m) # :nodoc:
|
||||
m.module_eval{
|
||||
typealias "DWORD", "unsigned long"
|
||||
typealias "PDWORD", "unsigned long *"
|
||||
typealias "DWORD32", "unsigned long"
|
||||
typealias "DWORD64", "unsigned long long"
|
||||
typealias "WORD", "unsigned short"
|
||||
typealias "PWORD", "unsigned short *"
|
||||
typealias "BOOL", "int"
|
||||
typealias "ATOM", "int"
|
||||
typealias "BYTE", "unsigned char"
|
||||
typealias "PBYTE", "unsigned char *"
|
||||
typealias "UINT", "unsigned int"
|
||||
typealias "ULONG", "unsigned long"
|
||||
typealias "UCHAR", "unsigned char"
|
||||
typealias "HANDLE", "uintptr_t"
|
||||
typealias "PHANDLE", "void*"
|
||||
typealias "PVOID", "void*"
|
||||
typealias "LPCSTR", "char*"
|
||||
typealias "LPSTR", "char*"
|
||||
typealias "HINSTANCE", "unsigned int"
|
||||
typealias "HDC", "unsigned int"
|
||||
typealias "HWND", "unsigned int"
|
||||
}
|
||||
end
|
||||
module_function :included
|
||||
end
|
||||
|
||||
# Adds basic type aliases to the including class for use with Fiddle::Importer.
|
||||
#
|
||||
# The aliases added are +uint+ and +u_int+ (<tt>unsigned int</tt>) and
|
||||
# +ulong+ and +u_long+ (<tt>unsigned long</tt>)
|
||||
module BasicTypes
|
||||
def included(m) # :nodoc:
|
||||
m.module_eval{
|
||||
typealias "uint", "unsigned int"
|
||||
typealias "u_int", "unsigned int"
|
||||
typealias "ulong", "unsigned long"
|
||||
typealias "u_long", "unsigned long"
|
||||
}
|
||||
end
|
||||
module_function :included
|
||||
end
|
||||
end
|
||||
112
ruby/lib/ruby/2.1.0/fiddle/value.rb
Normal file
112
ruby/lib/ruby/2.1.0/fiddle/value.rb
Normal file
@@ -0,0 +1,112 @@
|
||||
require 'fiddle'
|
||||
|
||||
module Fiddle
|
||||
module ValueUtil #:nodoc: all
|
||||
def unsigned_value(val, ty)
|
||||
case ty.abs
|
||||
when TYPE_CHAR
|
||||
[val].pack("c").unpack("C")[0]
|
||||
when TYPE_SHORT
|
||||
[val].pack("s!").unpack("S!")[0]
|
||||
when TYPE_INT
|
||||
[val].pack("i!").unpack("I!")[0]
|
||||
when TYPE_LONG
|
||||
[val].pack("l!").unpack("L!")[0]
|
||||
when TYPE_LONG_LONG
|
||||
[val].pack("q").unpack("Q")[0]
|
||||
else
|
||||
val
|
||||
end
|
||||
end
|
||||
|
||||
def signed_value(val, ty)
|
||||
case ty.abs
|
||||
when TYPE_CHAR
|
||||
[val].pack("C").unpack("c")[0]
|
||||
when TYPE_SHORT
|
||||
[val].pack("S!").unpack("s!")[0]
|
||||
when TYPE_INT
|
||||
[val].pack("I!").unpack("i!")[0]
|
||||
when TYPE_LONG
|
||||
[val].pack("L!").unpack("l!")[0]
|
||||
when TYPE_LONG_LONG
|
||||
[val].pack("Q").unpack("q")[0]
|
||||
else
|
||||
val
|
||||
end
|
||||
end
|
||||
|
||||
def wrap_args(args, tys, funcs, &block)
|
||||
result = []
|
||||
tys ||= []
|
||||
args.each_with_index{|arg, idx|
|
||||
result.push(wrap_arg(arg, tys[idx], funcs, &block))
|
||||
}
|
||||
result
|
||||
end
|
||||
|
||||
def wrap_arg(arg, ty, funcs = [], &block)
|
||||
funcs ||= []
|
||||
case arg
|
||||
when nil
|
||||
return 0
|
||||
when Pointer
|
||||
return arg.to_i
|
||||
when IO
|
||||
case ty
|
||||
when TYPE_VOIDP
|
||||
return Pointer[arg].to_i
|
||||
else
|
||||
return arg.to_i
|
||||
end
|
||||
when Function
|
||||
if( block )
|
||||
arg.bind_at_call(&block)
|
||||
funcs.push(arg)
|
||||
elsif !arg.bound?
|
||||
raise(RuntimeError, "block must be given.")
|
||||
end
|
||||
return arg.to_i
|
||||
when String
|
||||
if( ty.is_a?(Array) )
|
||||
return arg.unpack('C*')
|
||||
else
|
||||
case SIZEOF_VOIDP
|
||||
when SIZEOF_LONG
|
||||
return [arg].pack("p").unpack("l!")[0]
|
||||
when SIZEOF_LONG_LONG
|
||||
return [arg].pack("p").unpack("q")[0]
|
||||
else
|
||||
raise(RuntimeError, "sizeof(void*)?")
|
||||
end
|
||||
end
|
||||
when Float, Integer
|
||||
return arg
|
||||
when Array
|
||||
if( ty.is_a?(Array) ) # used only by struct
|
||||
case ty[0]
|
||||
when TYPE_VOIDP
|
||||
return arg.collect{|v| Integer(v)}
|
||||
when TYPE_CHAR
|
||||
if( arg.is_a?(String) )
|
||||
return val.unpack('C*')
|
||||
end
|
||||
end
|
||||
return arg
|
||||
else
|
||||
return arg
|
||||
end
|
||||
else
|
||||
if( arg.respond_to?(:to_ptr) )
|
||||
return arg.to_ptr.to_i
|
||||
else
|
||||
begin
|
||||
return Integer(arg)
|
||||
rescue
|
||||
raise(ArgumentError, "unknown argument type: #{arg.class}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
1764
ruby/lib/ruby/2.1.0/fileutils.rb
Normal file
1764
ruby/lib/ruby/2.1.0/fileutils.rb
Normal file
File diff suppressed because it is too large
Load Diff
85
ruby/lib/ruby/2.1.0/find.rb
Normal file
85
ruby/lib/ruby/2.1.0/find.rb
Normal file
@@ -0,0 +1,85 @@
|
||||
#
|
||||
# find.rb: the Find module for processing all files under a given directory.
|
||||
#
|
||||
|
||||
#
|
||||
# The +Find+ module supports the top-down traversal of a set of file paths.
|
||||
#
|
||||
# For example, to total the size of all files under your home directory,
|
||||
# ignoring anything in a "dot" directory (e.g. $HOME/.ssh):
|
||||
#
|
||||
# require 'find'
|
||||
#
|
||||
# total_size = 0
|
||||
#
|
||||
# Find.find(ENV["HOME"]) do |path|
|
||||
# if FileTest.directory?(path)
|
||||
# if File.basename(path)[0] == ?.
|
||||
# Find.prune # Don't look any further into this directory.
|
||||
# else
|
||||
# next
|
||||
# end
|
||||
# else
|
||||
# total_size += FileTest.size(path)
|
||||
# end
|
||||
# end
|
||||
#
|
||||
module Find
|
||||
|
||||
#
|
||||
# Calls the associated block with the name of every file and directory listed
|
||||
# as arguments, then recursively on their subdirectories, and so on.
|
||||
#
|
||||
# Returns an enumerator if no block is given.
|
||||
#
|
||||
# See the +Find+ module documentation for an example.
|
||||
#
|
||||
def find(*paths) # :yield: path
|
||||
block_given? or return enum_for(__method__, *paths)
|
||||
|
||||
fs_encoding = Encoding.find("filesystem")
|
||||
|
||||
paths.collect!{|d| raise Errno::ENOENT unless File.exist?(d); d.dup}.each do |path|
|
||||
enc = path.encoding == Encoding::US_ASCII ? fs_encoding : path.encoding
|
||||
ps = [path]
|
||||
while file = ps.shift
|
||||
catch(:prune) do
|
||||
yield file.dup.taint
|
||||
begin
|
||||
s = File.lstat(file)
|
||||
rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
|
||||
next
|
||||
end
|
||||
if s.directory? then
|
||||
begin
|
||||
fs = Dir.entries(file, encoding: enc)
|
||||
rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
|
||||
next
|
||||
end
|
||||
fs.sort!
|
||||
fs.reverse_each {|f|
|
||||
next if f == "." or f == ".."
|
||||
f = File.join(file, f)
|
||||
ps.unshift f.untaint
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
#
|
||||
# Skips the current file or directory, restarting the loop with the next
|
||||
# entry. If the current file is a directory, that directory will not be
|
||||
# recursively entered. Meaningful only within the block associated with
|
||||
# Find::find.
|
||||
#
|
||||
# See the +Find+ module documentation for an example.
|
||||
#
|
||||
def prune
|
||||
throw :prune
|
||||
end
|
||||
|
||||
module_function :find, :prune
|
||||
end
|
||||
289
ruby/lib/ruby/2.1.0/forwardable.rb
Normal file
289
ruby/lib/ruby/2.1.0/forwardable.rb
Normal file
@@ -0,0 +1,289 @@
|
||||
#
|
||||
# forwardable.rb -
|
||||
# $Release Version: 1.1$
|
||||
# $Revision: 40906 $
|
||||
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
||||
# original definition by delegator.rb
|
||||
# Revised by Daniel J. Berger with suggestions from Florian Gross.
|
||||
#
|
||||
# Documentation by James Edward Gray II and Gavin Sinclair
|
||||
|
||||
|
||||
|
||||
# The Forwardable module provides delegation of specified
|
||||
# methods to a designated object, using the methods #def_delegator
|
||||
# and #def_delegators.
|
||||
#
|
||||
# For example, say you have a class RecordCollection which
|
||||
# contains an array <tt>@records</tt>. You could provide the lookup method
|
||||
# #record_number(), which simply calls #[] on the <tt>@records</tt>
|
||||
# array, like this:
|
||||
#
|
||||
# require 'forwardable'
|
||||
#
|
||||
# class RecordCollection
|
||||
# attr_accessor :records
|
||||
# extend Forwardable
|
||||
# def_delegator :@records, :[], :record_number
|
||||
# end
|
||||
#
|
||||
# We can use the lookup method like so:
|
||||
#
|
||||
# r = RecordCollection.new
|
||||
# r.records = [4,5,6]
|
||||
# r.record_number(0) # => 4
|
||||
#
|
||||
# Further, if you wish to provide the methods #size, #<<, and #map,
|
||||
# all of which delegate to @records, this is how you can do it:
|
||||
#
|
||||
# class RecordCollection # re-open RecordCollection class
|
||||
# def_delegators :@records, :size, :<<, :map
|
||||
# end
|
||||
#
|
||||
# r = RecordCollection.new
|
||||
# r.records = [1,2,3]
|
||||
# r.record_number(0) # => 1
|
||||
# r.size # => 3
|
||||
# r << 4 # => [1, 2, 3, 4]
|
||||
# r.map { |x| x * 2 } # => [2, 4, 6, 8]
|
||||
#
|
||||
# You can even extend regular objects with Forwardable.
|
||||
#
|
||||
# my_hash = Hash.new
|
||||
# my_hash.extend Forwardable # prepare object for delegation
|
||||
# my_hash.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
||||
# my_hash.puts "Howdy!"
|
||||
#
|
||||
# == Another example
|
||||
#
|
||||
# We want to rely on what has come before obviously, but with delegation we can
|
||||
# take just the methods we need and even rename them as appropriate. In many
|
||||
# cases this is preferable to inheritance, which gives us the entire old
|
||||
# interface, even if much of it isn't needed.
|
||||
#
|
||||
# class Queue
|
||||
# extend Forwardable
|
||||
#
|
||||
# def initialize
|
||||
# @q = [ ] # prepare delegate object
|
||||
# end
|
||||
#
|
||||
# # setup preferred interface, enq() and deq()...
|
||||
# def_delegator :@q, :push, :enq
|
||||
# def_delegator :@q, :shift, :deq
|
||||
#
|
||||
# # support some general Array methods that fit Queues well
|
||||
# def_delegators :@q, :clear, :first, :push, :shift, :size
|
||||
# end
|
||||
#
|
||||
# q = Queue.new
|
||||
# q.enq 1, 2, 3, 4, 5
|
||||
# q.push 6
|
||||
#
|
||||
# q.shift # => 1
|
||||
# while q.size > 0
|
||||
# puts q.deq
|
||||
# end
|
||||
#
|
||||
# q.enq "Ruby", "Perl", "Python"
|
||||
# puts q.first
|
||||
# q.clear
|
||||
# puts q.first
|
||||
#
|
||||
# This should output:
|
||||
#
|
||||
# 2
|
||||
# 3
|
||||
# 4
|
||||
# 5
|
||||
# 6
|
||||
# Ruby
|
||||
# nil
|
||||
#
|
||||
# == Notes
|
||||
#
|
||||
# Be advised, RDoc will not detect delegated methods.
|
||||
#
|
||||
# +forwardable.rb+ provides single-method delegation via the def_delegator and
|
||||
# def_delegators methods. For full-class delegation via DelegateClass, see
|
||||
# +delegate.rb+.
|
||||
#
|
||||
module Forwardable
|
||||
# Version of +forwardable.rb+
|
||||
FORWARDABLE_VERSION = "1.1.0"
|
||||
|
||||
FILE_REGEXP = %r"#{Regexp.quote(__FILE__)}"
|
||||
|
||||
@debug = nil
|
||||
class << self
|
||||
# If true, <tt>__FILE__</tt> will remain in the backtrace in the event an
|
||||
# Exception is raised.
|
||||
attr_accessor :debug
|
||||
end
|
||||
|
||||
# Takes a hash as its argument. The key is a symbol or an array of
|
||||
# symbols. These symbols correspond to method names. The value is
|
||||
# the accessor to which the methods will be delegated.
|
||||
#
|
||||
# :call-seq:
|
||||
# delegate method => accessor
|
||||
# delegate [method, method, ...] => accessor
|
||||
#
|
||||
def instance_delegate(hash)
|
||||
hash.each{ |methods, accessor|
|
||||
methods = [methods] unless methods.respond_to?(:each)
|
||||
methods.each{ |method|
|
||||
def_instance_delegator(accessor, method)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
#
|
||||
# Shortcut for defining multiple delegator methods, but with no
|
||||
# provision for using a different name. The following two code
|
||||
# samples have the same effect:
|
||||
#
|
||||
# def_delegators :@records, :size, :<<, :map
|
||||
#
|
||||
# def_delegator :@records, :size
|
||||
# def_delegator :@records, :<<
|
||||
# def_delegator :@records, :map
|
||||
#
|
||||
def def_instance_delegators(accessor, *methods)
|
||||
methods.delete("__send__")
|
||||
methods.delete("__id__")
|
||||
for method in methods
|
||||
def_instance_delegator(accessor, method)
|
||||
end
|
||||
end
|
||||
|
||||
# Define +method+ as delegator instance method with an optional
|
||||
# alias name +ali+. Method calls to +ali+ will be delegated to
|
||||
# +accessor.method+.
|
||||
#
|
||||
# class MyQueue
|
||||
# extend Forwardable
|
||||
# attr_reader :queue
|
||||
# def initialize
|
||||
# @queue = []
|
||||
# end
|
||||
#
|
||||
# def_delegator :@queue, :push, :mypush
|
||||
# end
|
||||
#
|
||||
# q = MyQueue.new
|
||||
# q.mypush 42
|
||||
# q.queue #=> [42]
|
||||
# q.push 23 #=> NoMethodError
|
||||
#
|
||||
def def_instance_delegator(accessor, method, ali = method)
|
||||
line_no = __LINE__; str = %{
|
||||
def #{ali}(*args, &block)
|
||||
begin
|
||||
#{accessor}.__send__(:#{method}, *args, &block)
|
||||
rescue Exception
|
||||
$@.delete_if{|s| Forwardable::FILE_REGEXP =~ s} unless Forwardable::debug
|
||||
::Kernel::raise
|
||||
end
|
||||
end
|
||||
}
|
||||
# If it's not a class or module, it's an instance
|
||||
begin
|
||||
module_eval(str, __FILE__, line_no)
|
||||
rescue
|
||||
instance_eval(str, __FILE__, line_no)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
alias delegate instance_delegate
|
||||
alias def_delegators def_instance_delegators
|
||||
alias def_delegator def_instance_delegator
|
||||
end
|
||||
|
||||
# SingleForwardable can be used to setup delegation at the object level as well.
|
||||
#
|
||||
# printer = String.new
|
||||
# printer.extend SingleForwardable # prepare object for delegation
|
||||
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
||||
# printer.puts "Howdy!"
|
||||
#
|
||||
# Also, SingleForwardable can be used to set up delegation for a Class or Module.
|
||||
#
|
||||
# class Implementation
|
||||
# def self.service
|
||||
# puts "serviced!"
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# module Facade
|
||||
# extend SingleForwardable
|
||||
# def_delegator :Implementation, :service
|
||||
# end
|
||||
#
|
||||
# Facade.service #=> serviced!
|
||||
#
|
||||
# If you want to use both Forwardable and SingleForwardable, you can
|
||||
# use methods def_instance_delegator and def_single_delegator, etc.
|
||||
module SingleForwardable
|
||||
# Takes a hash as its argument. The key is a symbol or an array of
|
||||
# symbols. These symbols correspond to method names. The value is
|
||||
# the accessor to which the methods will be delegated.
|
||||
#
|
||||
# :call-seq:
|
||||
# delegate method => accessor
|
||||
# delegate [method, method, ...] => accessor
|
||||
#
|
||||
def single_delegate(hash)
|
||||
hash.each{ |methods, accessor|
|
||||
methods = [methods] unless methods.respond_to?(:each)
|
||||
methods.each{ |method|
|
||||
def_single_delegator(accessor, method)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
#
|
||||
# Shortcut for defining multiple delegator methods, but with no
|
||||
# provision for using a different name. The following two code
|
||||
# samples have the same effect:
|
||||
#
|
||||
# def_delegators :@records, :size, :<<, :map
|
||||
#
|
||||
# def_delegator :@records, :size
|
||||
# def_delegator :@records, :<<
|
||||
# def_delegator :@records, :map
|
||||
#
|
||||
def def_single_delegators(accessor, *methods)
|
||||
methods.delete("__send__")
|
||||
methods.delete("__id__")
|
||||
for method in methods
|
||||
def_single_delegator(accessor, method)
|
||||
end
|
||||
end
|
||||
|
||||
# :call-seq:
|
||||
# def_single_delegator(accessor, method, new_name=method)
|
||||
#
|
||||
# Defines a method _method_ which delegates to _accessor_ (i.e. it calls
|
||||
# the method of the same name in _accessor_). If _new_name_ is
|
||||
# provided, it is used as the name for the delegate method.
|
||||
def def_single_delegator(accessor, method, ali = method)
|
||||
str = %{
|
||||
def #{ali}(*args, &block)
|
||||
begin
|
||||
#{accessor}.__send__(:#{method}, *args, &block)
|
||||
rescue Exception
|
||||
$@.delete_if{|s| Forwardable::FILE_REGEXP =~ s} unless Forwardable::debug
|
||||
::Kernel::raise
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
instance_eval(str, __FILE__, __LINE__)
|
||||
end
|
||||
|
||||
alias delegate single_delegate
|
||||
alias def_delegators def_single_delegators
|
||||
alias def_delegator def_single_delegator
|
||||
end
|
||||
612
ruby/lib/ruby/2.1.0/getoptlong.rb
Normal file
612
ruby/lib/ruby/2.1.0/getoptlong.rb
Normal file
@@ -0,0 +1,612 @@
|
||||
#
|
||||
# GetoptLong for Ruby
|
||||
#
|
||||
# Copyright (C) 1998, 1999, 2000 Motoyuki Kasahara.
|
||||
#
|
||||
# You may redistribute and/or modify this library under the same license
|
||||
# terms as Ruby.
|
||||
#
|
||||
# See GetoptLong for documentation.
|
||||
#
|
||||
# Additional documents and the latest version of `getoptlong.rb' can be
|
||||
# found at http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/
|
||||
|
||||
# The GetoptLong class allows you to parse command line options similarly to
|
||||
# the GNU getopt_long() C library call. Note, however, that GetoptLong is a
|
||||
# pure Ruby implementation.
|
||||
#
|
||||
# GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
|
||||
# as single letter options like <tt>-f</tt>
|
||||
#
|
||||
# The empty option <tt>--</tt> (two minus symbols) is used to end option
|
||||
# processing. This can be particularly important if options have optional
|
||||
# arguments.
|
||||
#
|
||||
# Here is a simple example of usage:
|
||||
#
|
||||
# require 'getoptlong'
|
||||
#
|
||||
# opts = GetoptLong.new(
|
||||
# [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
||||
# [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
|
||||
# [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
|
||||
# )
|
||||
#
|
||||
# dir = nil
|
||||
# name = nil
|
||||
# repetitions = 1
|
||||
# opts.each do |opt, arg|
|
||||
# case opt
|
||||
# when '--help'
|
||||
# puts <<-EOF
|
||||
# hello [OPTION] ... DIR
|
||||
#
|
||||
# -h, --help:
|
||||
# show help
|
||||
#
|
||||
# --repeat x, -n x:
|
||||
# repeat x times
|
||||
#
|
||||
# --name [name]:
|
||||
# greet user by name, if name not supplied default is John
|
||||
#
|
||||
# DIR: The directory in which to issue the greeting.
|
||||
# EOF
|
||||
# when '--repeat'
|
||||
# repetitions = arg.to_i
|
||||
# when '--name'
|
||||
# if arg == ''
|
||||
# name = 'John'
|
||||
# else
|
||||
# name = arg
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# if ARGV.length != 1
|
||||
# puts "Missing dir argument (try --help)"
|
||||
# exit 0
|
||||
# end
|
||||
#
|
||||
# dir = ARGV.shift
|
||||
#
|
||||
# Dir.chdir(dir)
|
||||
# for i in (1..repetitions)
|
||||
# print "Hello"
|
||||
# if name
|
||||
# print ", #{name}"
|
||||
# end
|
||||
# puts
|
||||
# end
|
||||
#
|
||||
# Example command line:
|
||||
#
|
||||
# hello -n 6 --name -- /tmp
|
||||
#
|
||||
class GetoptLong
|
||||
#
|
||||
# Orderings.
|
||||
#
|
||||
ORDERINGS = [REQUIRE_ORDER = 0, PERMUTE = 1, RETURN_IN_ORDER = 2]
|
||||
|
||||
#
|
||||
# Argument flags.
|
||||
#
|
||||
ARGUMENT_FLAGS = [NO_ARGUMENT = 0, REQUIRED_ARGUMENT = 1,
|
||||
OPTIONAL_ARGUMENT = 2]
|
||||
|
||||
#
|
||||
# Status codes.
|
||||
#
|
||||
STATUS_YET, STATUS_STARTED, STATUS_TERMINATED = 0, 1, 2
|
||||
|
||||
#
|
||||
# Error types.
|
||||
#
|
||||
class Error < StandardError; end
|
||||
class AmbiguousOption < Error; end
|
||||
class NeedlessArgument < Error; end
|
||||
class MissingArgument < Error; end
|
||||
class InvalidOption < Error; end
|
||||
|
||||
#
|
||||
# Set up option processing.
|
||||
#
|
||||
# The options to support are passed to new() as an array of arrays.
|
||||
# Each sub-array contains any number of String option names which carry
|
||||
# the same meaning, and one of the following flags:
|
||||
#
|
||||
# GetoptLong::NO_ARGUMENT :: Option does not take an argument.
|
||||
#
|
||||
# GetoptLong::REQUIRED_ARGUMENT :: Option always takes an argument.
|
||||
#
|
||||
# GetoptLong::OPTIONAL_ARGUMENT :: Option may or may not take an argument.
|
||||
#
|
||||
# The first option name is considered to be the preferred (canonical) name.
|
||||
# Other than that, the elements of each sub-array can be in any order.
|
||||
#
|
||||
def initialize(*arguments)
|
||||
#
|
||||
# Current ordering.
|
||||
#
|
||||
if ENV.include?('POSIXLY_CORRECT')
|
||||
@ordering = REQUIRE_ORDER
|
||||
else
|
||||
@ordering = PERMUTE
|
||||
end
|
||||
|
||||
#
|
||||
# Hash table of option names.
|
||||
# Keys of the table are option names, and their values are canonical
|
||||
# names of the options.
|
||||
#
|
||||
@canonical_names = Hash.new
|
||||
|
||||
#
|
||||
# Hash table of argument flags.
|
||||
# Keys of the table are option names, and their values are argument
|
||||
# flags of the options.
|
||||
#
|
||||
@argument_flags = Hash.new
|
||||
|
||||
#
|
||||
# Whether error messages are output to $stderr.
|
||||
#
|
||||
@quiet = FALSE
|
||||
|
||||
#
|
||||
# Status code.
|
||||
#
|
||||
@status = STATUS_YET
|
||||
|
||||
#
|
||||
# Error code.
|
||||
#
|
||||
@error = nil
|
||||
|
||||
#
|
||||
# Error message.
|
||||
#
|
||||
@error_message = nil
|
||||
|
||||
#
|
||||
# Rest of catenated short options.
|
||||
#
|
||||
@rest_singles = ''
|
||||
|
||||
#
|
||||
# List of non-option-arguments.
|
||||
# Append them to ARGV when option processing is terminated.
|
||||
#
|
||||
@non_option_arguments = Array.new
|
||||
|
||||
if 0 < arguments.length
|
||||
set_options(*arguments)
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Set the handling of the ordering of options and arguments.
|
||||
# A RuntimeError is raised if option processing has already started.
|
||||
#
|
||||
# The supplied value must be a member of GetoptLong::ORDERINGS. It alters
|
||||
# the processing of options as follows:
|
||||
#
|
||||
# <b>REQUIRE_ORDER</b> :
|
||||
#
|
||||
# Options are required to occur before non-options.
|
||||
#
|
||||
# Processing of options ends as soon as a word is encountered that has not
|
||||
# been preceded by an appropriate option flag.
|
||||
#
|
||||
# For example, if -a and -b are options which do not take arguments,
|
||||
# parsing command line arguments of '-a one -b two' would result in
|
||||
# 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being
|
||||
# processed as an option/arg pair.
|
||||
#
|
||||
# This is the default ordering, if the environment variable
|
||||
# POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
|
||||
#
|
||||
# <b>PERMUTE</b> :
|
||||
#
|
||||
# Options can occur anywhere in the command line parsed. This is the
|
||||
# default behavior.
|
||||
#
|
||||
# Every sequence of words which can be interpreted as an option (with or
|
||||
# without argument) is treated as an option; non-option words are skipped.
|
||||
#
|
||||
# For example, if -a does not require an argument and -b optionally takes
|
||||
# an argument, parsing '-a one -b two three' would result in ('-a','') and
|
||||
# ('-b', 'two') being processed as option/arg pairs, and 'one','three'
|
||||
# being left in ARGV.
|
||||
#
|
||||
# If the ordering is set to PERMUTE but the environment variable
|
||||
# POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for
|
||||
# compatibility with GNU getopt_long.
|
||||
#
|
||||
# <b>RETURN_IN_ORDER</b> :
|
||||
#
|
||||
# All words on the command line are processed as options. Words not
|
||||
# preceded by a short or long option flag are passed as arguments
|
||||
# with an option of '' (empty string).
|
||||
#
|
||||
# For example, if -a requires an argument but -b does not, a command line
|
||||
# of '-a one -b two three' would result in option/arg pairs of ('-a', 'one')
|
||||
# ('-b', ''), ('', 'two'), ('', 'three') being processed.
|
||||
#
|
||||
def ordering=(ordering)
|
||||
#
|
||||
# The method is failed if option processing has already started.
|
||||
#
|
||||
if @status != STATUS_YET
|
||||
set_error(ArgumentError, "argument error")
|
||||
raise RuntimeError,
|
||||
"invoke ordering=, but option processing has already started"
|
||||
end
|
||||
|
||||
#
|
||||
# Check ordering.
|
||||
#
|
||||
if !ORDERINGS.include?(ordering)
|
||||
raise ArgumentError, "invalid ordering `#{ordering}'"
|
||||
end
|
||||
if ordering == PERMUTE && ENV.include?('POSIXLY_CORRECT')
|
||||
@ordering = REQUIRE_ORDER
|
||||
else
|
||||
@ordering = ordering
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Return ordering.
|
||||
#
|
||||
attr_reader :ordering
|
||||
|
||||
#
|
||||
# Set options. Takes the same argument as GetoptLong.new.
|
||||
#
|
||||
# Raises a RuntimeError if option processing has already started.
|
||||
#
|
||||
def set_options(*arguments)
|
||||
#
|
||||
# The method is failed if option processing has already started.
|
||||
#
|
||||
if @status != STATUS_YET
|
||||
raise RuntimeError,
|
||||
"invoke set_options, but option processing has already started"
|
||||
end
|
||||
|
||||
#
|
||||
# Clear tables of option names and argument flags.
|
||||
#
|
||||
@canonical_names.clear
|
||||
@argument_flags.clear
|
||||
|
||||
arguments.each do |arg|
|
||||
if !arg.is_a?(Array)
|
||||
raise ArgumentError, "the option list contains non-Array argument"
|
||||
end
|
||||
|
||||
#
|
||||
# Find an argument flag and it set to `argument_flag'.
|
||||
#
|
||||
argument_flag = nil
|
||||
arg.each do |i|
|
||||
if ARGUMENT_FLAGS.include?(i)
|
||||
if argument_flag != nil
|
||||
raise ArgumentError, "too many argument-flags"
|
||||
end
|
||||
argument_flag = i
|
||||
end
|
||||
end
|
||||
|
||||
raise ArgumentError, "no argument-flag" if argument_flag == nil
|
||||
|
||||
canonical_name = nil
|
||||
arg.each do |i|
|
||||
#
|
||||
# Check an option name.
|
||||
#
|
||||
next if i == argument_flag
|
||||
begin
|
||||
if !i.is_a?(String) || i !~ /^-([^-]|-.+)$/
|
||||
raise ArgumentError, "an invalid option `#{i}'"
|
||||
end
|
||||
if (@canonical_names.include?(i))
|
||||
raise ArgumentError, "option redefined `#{i}'"
|
||||
end
|
||||
rescue
|
||||
@canonical_names.clear
|
||||
@argument_flags.clear
|
||||
raise
|
||||
end
|
||||
|
||||
#
|
||||
# Register the option (`i') to the `@canonical_names' and
|
||||
# `@canonical_names' Hashes.
|
||||
#
|
||||
if canonical_name == nil
|
||||
canonical_name = i
|
||||
end
|
||||
@canonical_names[i] = canonical_name
|
||||
@argument_flags[i] = argument_flag
|
||||
end
|
||||
raise ArgumentError, "no option name" if canonical_name == nil
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
#
|
||||
# Set/Unset `quiet' mode.
|
||||
#
|
||||
attr_writer :quiet
|
||||
|
||||
#
|
||||
# Return the flag of `quiet' mode.
|
||||
#
|
||||
attr_reader :quiet
|
||||
|
||||
#
|
||||
# `quiet?' is an alias of `quiet'.
|
||||
#
|
||||
alias quiet? quiet
|
||||
|
||||
#
|
||||
# Explicitly terminate option processing.
|
||||
#
|
||||
def terminate
|
||||
return nil if @status == STATUS_TERMINATED
|
||||
raise RuntimeError, "an error has occurred" if @error != nil
|
||||
|
||||
@status = STATUS_TERMINATED
|
||||
@non_option_arguments.reverse_each do |argument|
|
||||
ARGV.unshift(argument)
|
||||
end
|
||||
|
||||
@canonical_names = nil
|
||||
@argument_flags = nil
|
||||
@rest_singles = nil
|
||||
@non_option_arguments = nil
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
#
|
||||
# Returns true if option processing has terminated, false otherwise.
|
||||
#
|
||||
def terminated?
|
||||
return @status == STATUS_TERMINATED
|
||||
end
|
||||
|
||||
#
|
||||
# Set an error (a protected method).
|
||||
#
|
||||
def set_error(type, message)
|
||||
$stderr.print("#{$0}: #{message}\n") if !@quiet
|
||||
|
||||
@error = type
|
||||
@error_message = message
|
||||
@canonical_names = nil
|
||||
@argument_flags = nil
|
||||
@rest_singles = nil
|
||||
@non_option_arguments = nil
|
||||
|
||||
raise type, message
|
||||
end
|
||||
protected :set_error
|
||||
|
||||
#
|
||||
# Examine whether an option processing is failed.
|
||||
#
|
||||
attr_reader :error
|
||||
|
||||
#
|
||||
# `error?' is an alias of `error'.
|
||||
#
|
||||
alias error? error
|
||||
|
||||
# Return the appropriate error message in POSIX-defined format.
|
||||
# If no error has occurred, returns nil.
|
||||
#
|
||||
def error_message
|
||||
return @error_message
|
||||
end
|
||||
|
||||
#
|
||||
# Get next option name and its argument, as an Array of two elements.
|
||||
#
|
||||
# The option name is always converted to the first (preferred)
|
||||
# name given in the original options to GetoptLong.new.
|
||||
#
|
||||
# Example: ['--option', 'value']
|
||||
#
|
||||
# Returns nil if the processing is complete (as determined by
|
||||
# STATUS_TERMINATED).
|
||||
#
|
||||
def get
|
||||
option_name, option_argument = nil, ''
|
||||
|
||||
#
|
||||
# Check status.
|
||||
#
|
||||
return nil if @error != nil
|
||||
case @status
|
||||
when STATUS_YET
|
||||
@status = STATUS_STARTED
|
||||
when STATUS_TERMINATED
|
||||
return nil
|
||||
end
|
||||
|
||||
#
|
||||
# Get next option argument.
|
||||
#
|
||||
if 0 < @rest_singles.length
|
||||
argument = '-' + @rest_singles
|
||||
elsif (ARGV.length == 0)
|
||||
terminate
|
||||
return nil
|
||||
elsif @ordering == PERMUTE
|
||||
while 0 < ARGV.length && ARGV[0] !~ /^-./
|
||||
@non_option_arguments.push(ARGV.shift)
|
||||
end
|
||||
if ARGV.length == 0
|
||||
terminate
|
||||
return nil
|
||||
end
|
||||
argument = ARGV.shift
|
||||
elsif @ordering == REQUIRE_ORDER
|
||||
if (ARGV[0] !~ /^-./)
|
||||
terminate
|
||||
return nil
|
||||
end
|
||||
argument = ARGV.shift
|
||||
else
|
||||
argument = ARGV.shift
|
||||
end
|
||||
|
||||
#
|
||||
# Check the special argument `--'.
|
||||
# `--' indicates the end of the option list.
|
||||
#
|
||||
if argument == '--' && @rest_singles.length == 0
|
||||
terminate
|
||||
return nil
|
||||
end
|
||||
|
||||
#
|
||||
# Check for long and short options.
|
||||
#
|
||||
if argument =~ /^(--[^=]+)/ && @rest_singles.length == 0
|
||||
#
|
||||
# This is a long style option, which start with `--'.
|
||||
#
|
||||
pattern = $1
|
||||
if @canonical_names.include?(pattern)
|
||||
option_name = pattern
|
||||
else
|
||||
#
|
||||
# The option `option_name' is not registered in `@canonical_names'.
|
||||
# It may be an abbreviated.
|
||||
#
|
||||
matches = []
|
||||
@canonical_names.each_key do |key|
|
||||
if key.index(pattern) == 0
|
||||
option_name = key
|
||||
matches << key
|
||||
end
|
||||
end
|
||||
if 2 <= matches.length
|
||||
set_error(AmbiguousOption, "option `#{argument}' is ambiguous between #{matches.join(', ')}")
|
||||
elsif matches.length == 0
|
||||
set_error(InvalidOption, "unrecognized option `#{argument}'")
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Check an argument to the option.
|
||||
#
|
||||
if @argument_flags[option_name] == REQUIRED_ARGUMENT
|
||||
if argument =~ /=(.*)$/
|
||||
option_argument = $1
|
||||
elsif 0 < ARGV.length
|
||||
option_argument = ARGV.shift
|
||||
else
|
||||
set_error(MissingArgument,
|
||||
"option `#{argument}' requires an argument")
|
||||
end
|
||||
elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
|
||||
if argument =~ /=(.*)$/
|
||||
option_argument = $1
|
||||
elsif 0 < ARGV.length && ARGV[0] !~ /^-./
|
||||
option_argument = ARGV.shift
|
||||
else
|
||||
option_argument = ''
|
||||
end
|
||||
elsif argument =~ /=(.*)$/
|
||||
set_error(NeedlessArgument,
|
||||
"option `#{option_name}' doesn't allow an argument")
|
||||
end
|
||||
|
||||
elsif argument =~ /^(-(.))(.*)/
|
||||
#
|
||||
# This is a short style option, which start with `-' (not `--').
|
||||
# Short options may be catenated (e.g. `-l -g' is equivalent to
|
||||
# `-lg').
|
||||
#
|
||||
option_name, ch, @rest_singles = $1, $2, $3
|
||||
|
||||
if @canonical_names.include?(option_name)
|
||||
#
|
||||
# The option `option_name' is found in `@canonical_names'.
|
||||
# Check its argument.
|
||||
#
|
||||
if @argument_flags[option_name] == REQUIRED_ARGUMENT
|
||||
if 0 < @rest_singles.length
|
||||
option_argument = @rest_singles
|
||||
@rest_singles = ''
|
||||
elsif 0 < ARGV.length
|
||||
option_argument = ARGV.shift
|
||||
else
|
||||
# 1003.2 specifies the format of this message.
|
||||
set_error(MissingArgument, "option requires an argument -- #{ch}")
|
||||
end
|
||||
elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
|
||||
if 0 < @rest_singles.length
|
||||
option_argument = @rest_singles
|
||||
@rest_singles = ''
|
||||
elsif 0 < ARGV.length && ARGV[0] !~ /^-./
|
||||
option_argument = ARGV.shift
|
||||
else
|
||||
option_argument = ''
|
||||
end
|
||||
end
|
||||
else
|
||||
#
|
||||
# This is an invalid option.
|
||||
# 1003.2 specifies the format of this message.
|
||||
#
|
||||
if ENV.include?('POSIXLY_CORRECT')
|
||||
set_error(InvalidOption, "invalid option -- #{ch}")
|
||||
else
|
||||
set_error(InvalidOption, "invalid option -- #{ch}")
|
||||
end
|
||||
end
|
||||
else
|
||||
#
|
||||
# This is a non-option argument.
|
||||
# Only RETURN_IN_ORDER falled into here.
|
||||
#
|
||||
return '', argument
|
||||
end
|
||||
|
||||
return @canonical_names[option_name], option_argument
|
||||
end
|
||||
|
||||
#
|
||||
# `get_option' is an alias of `get'.
|
||||
#
|
||||
alias get_option get
|
||||
|
||||
# Iterator version of `get'.
|
||||
#
|
||||
# The block is called repeatedly with two arguments:
|
||||
# The first is the option name.
|
||||
# The second is the argument which followed it (if any).
|
||||
# Example: ('--opt', 'value')
|
||||
#
|
||||
# The option name is always converted to the first (preferred)
|
||||
# name given in the original options to GetoptLong.new.
|
||||
#
|
||||
def each
|
||||
loop do
|
||||
option_name, option_argument = get_option
|
||||
break if option_name == nil
|
||||
yield option_name, option_argument
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# `each_option' is an alias of `each'.
|
||||
#
|
||||
alias each_option each
|
||||
end
|
||||
310
ruby/lib/ruby/2.1.0/gserver.rb
Normal file
310
ruby/lib/ruby/2.1.0/gserver.rb
Normal file
@@ -0,0 +1,310 @@
|
||||
#
|
||||
# Copyright (C) 2001 John W. Small All Rights Reserved
|
||||
#
|
||||
# Author:: John W. Small
|
||||
# Documentation:: Gavin Sinclair
|
||||
# Licence:: Ruby License
|
||||
|
||||
require "socket"
|
||||
require "thread"
|
||||
|
||||
#
|
||||
# GServer implements a generic server, featuring thread pool management,
|
||||
# simple logging, and multi-server management. See HttpServer in
|
||||
# <tt>xmlrpc/httpserver.rb</tt> in the Ruby standard library for an example of
|
||||
# GServer in action.
|
||||
#
|
||||
# Any kind of application-level server can be implemented using this class.
|
||||
# It accepts multiple simultaneous connections from clients, up to an optional
|
||||
# maximum number. Several _services_ (i.e. one service per TCP port) can be
|
||||
# run simultaneously, and stopped at any time through the class method
|
||||
# <tt>GServer.stop(port)</tt>. All the threading issues are handled, saving
|
||||
# you the effort. All events are optionally logged, but you can provide your
|
||||
# own event handlers if you wish.
|
||||
#
|
||||
# == Example
|
||||
#
|
||||
# Using GServer is simple. Below we implement a simple time server, run it,
|
||||
# query it, and shut it down. Try this code in +irb+:
|
||||
#
|
||||
# require 'gserver'
|
||||
#
|
||||
# #
|
||||
# # A server that returns the time in seconds since 1970.
|
||||
# #
|
||||
# class TimeServer < GServer
|
||||
# def initialize(port=10001, *args)
|
||||
# super(port, *args)
|
||||
# end
|
||||
# def serve(io)
|
||||
# io.puts(Time.now.to_i)
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# # Run the server with logging enabled (it's a separate thread).
|
||||
# server = TimeServer.new
|
||||
# server.audit = true # Turn logging on.
|
||||
# server.start
|
||||
#
|
||||
# # *** Now point your browser to http://localhost:10001 to see it working ***
|
||||
#
|
||||
# # See if it's still running.
|
||||
# GServer.in_service?(10001) # -> true
|
||||
# server.stopped? # -> false
|
||||
#
|
||||
# # Shut the server down gracefully.
|
||||
# server.shutdown
|
||||
#
|
||||
# # Alternatively, stop it immediately.
|
||||
# GServer.stop(10001)
|
||||
# # or, of course, "server.stop".
|
||||
#
|
||||
# All the business of accepting connections and exception handling is taken
|
||||
# care of. All we have to do is implement the method that actually serves the
|
||||
# client.
|
||||
#
|
||||
# === Advanced
|
||||
#
|
||||
# As the example above shows, the way to use GServer is to subclass it to
|
||||
# create a specific server, overriding the +serve+ method. You can override
|
||||
# other methods as well if you wish, perhaps to collect statistics, or emit
|
||||
# more detailed logging.
|
||||
#
|
||||
# * #connecting
|
||||
# * #disconnecting
|
||||
# * #starting
|
||||
# * #stopping
|
||||
#
|
||||
# The above methods are only called if auditing is enabled, via #audit=.
|
||||
#
|
||||
# You can also override #log and #error if, for example, you wish to use a
|
||||
# more sophisticated logging system.
|
||||
#
|
||||
class GServer
|
||||
|
||||
DEFAULT_HOST = "127.0.0.1"
|
||||
|
||||
def serve(io)
|
||||
end
|
||||
|
||||
@@services = {} # Hash of opened ports, i.e. services
|
||||
@@servicesMutex = Mutex.new
|
||||
|
||||
# Stop the server running on the given port, bound to the given host
|
||||
#
|
||||
# +port+:: port, as a Fixnum, of the server to stop
|
||||
# +host+:: host on which to find the server to stop
|
||||
def GServer.stop(port, host = DEFAULT_HOST)
|
||||
@@servicesMutex.synchronize {
|
||||
@@services[host][port].stop
|
||||
}
|
||||
end
|
||||
|
||||
# Check if a server is running on the given port and host
|
||||
#
|
||||
# +port+:: port, as a Fixnum, of the server to check
|
||||
# +host+:: host on which to find the server to check
|
||||
#
|
||||
# Returns true if a server is running on that port and host.
|
||||
def GServer.in_service?(port, host = DEFAULT_HOST)
|
||||
@@services.has_key?(host) and
|
||||
@@services[host].has_key?(port)
|
||||
end
|
||||
|
||||
# Stop the server
|
||||
def stop
|
||||
@connectionsMutex.synchronize {
|
||||
if @tcpServerThread
|
||||
@tcpServerThread.raise "stop"
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
# Returns true if the server has stopped.
|
||||
def stopped?
|
||||
@tcpServerThread == nil
|
||||
end
|
||||
|
||||
# Schedule a shutdown for the server
|
||||
def shutdown
|
||||
@shutdown = true
|
||||
end
|
||||
|
||||
# Return the current number of connected clients
|
||||
def connections
|
||||
@connections.size
|
||||
end
|
||||
|
||||
# Join with the server thread
|
||||
def join
|
||||
@tcpServerThread.join if @tcpServerThread
|
||||
end
|
||||
|
||||
# Port on which to listen, as a Fixnum
|
||||
attr_reader :port
|
||||
# Host on which to bind, as a String
|
||||
attr_reader :host
|
||||
# Maximum number of connections to accept at a time, as a Fixnum
|
||||
attr_reader :maxConnections
|
||||
# IO Device on which log messages should be written
|
||||
attr_accessor :stdlog
|
||||
# Set to true to cause the callbacks #connecting, #disconnecting, #starting,
|
||||
# and #stopping to be called during the server's lifecycle
|
||||
attr_accessor :audit
|
||||
# Set to true to show more detailed logging
|
||||
attr_accessor :debug
|
||||
|
||||
# Called when a client connects, if auditing is enabled.
|
||||
#
|
||||
# +client+:: a TCPSocket instance representing the client that connected
|
||||
#
|
||||
# Return true to allow this client to connect, false to prevent it.
|
||||
def connecting(client)
|
||||
addr = client.peeraddr
|
||||
log("#{self.class.to_s} #{@host}:#{@port} client:#{addr[1]} " +
|
||||
"#{addr[2]}<#{addr[3]}> connect")
|
||||
true
|
||||
end
|
||||
|
||||
|
||||
# Called when a client disconnects, if audition is enabled.
|
||||
#
|
||||
# +clientPort+:: the port of the client that is connecting
|
||||
def disconnecting(clientPort)
|
||||
log("#{self.class.to_s} #{@host}:#{@port} " +
|
||||
"client:#{clientPort} disconnect")
|
||||
end
|
||||
|
||||
protected :connecting, :disconnecting
|
||||
|
||||
# Called when the server is starting up, if auditing is enabled.
|
||||
def starting()
|
||||
log("#{self.class.to_s} #{@host}:#{@port} start")
|
||||
end
|
||||
|
||||
# Called when the server is shutting down, if auditing is enabled.
|
||||
def stopping()
|
||||
log("#{self.class.to_s} #{@host}:#{@port} stop")
|
||||
end
|
||||
|
||||
protected :starting, :stopping
|
||||
|
||||
# Called if #debug is true whenever an unhandled exception is raised.
|
||||
# This implementation simply logs the backtrace.
|
||||
#
|
||||
# +detail+:: the Exception that was caught
|
||||
def error(detail)
|
||||
log(detail.backtrace.join("\n"))
|
||||
end
|
||||
|
||||
# Log a message to #stdlog, if it's defined. This implementation
|
||||
# outputs the timestamp and message to the log.
|
||||
#
|
||||
# +msg+:: the message to log
|
||||
def log(msg)
|
||||
if @stdlog
|
||||
@stdlog.puts("[#{Time.new.ctime}] %s" % msg)
|
||||
@stdlog.flush
|
||||
end
|
||||
end
|
||||
|
||||
protected :error, :log
|
||||
|
||||
# Create a new server
|
||||
#
|
||||
# +port+:: the port, as a Fixnum, on which to listen
|
||||
# +host+:: the host to bind to
|
||||
# +maxConnections+:: the maximum number of simultaneous connections to
|
||||
# accept
|
||||
# +stdlog+:: IO device on which to log messages
|
||||
# +audit+:: if true, lifecycle callbacks will be called. See #audit
|
||||
# +debug+:: if true, error messages are logged. See #debug
|
||||
def initialize(port, host = DEFAULT_HOST, maxConnections = 4,
|
||||
stdlog = $stderr, audit = false, debug = false)
|
||||
@tcpServerThread = nil
|
||||
@port = port
|
||||
@host = host
|
||||
@maxConnections = maxConnections
|
||||
@connections = []
|
||||
@connectionsMutex = Mutex.new
|
||||
@connectionsCV = ConditionVariable.new
|
||||
@stdlog = stdlog
|
||||
@audit = audit
|
||||
@debug = debug
|
||||
end
|
||||
|
||||
# Start the server if it isn't already running
|
||||
#
|
||||
# +maxConnections+::
|
||||
# override +maxConnections+ given to the constructor. A negative
|
||||
# value indicates that the value from the constructor should be used.
|
||||
def start(maxConnections = -1)
|
||||
raise "server is already running" if !stopped?
|
||||
@shutdown = false
|
||||
@maxConnections = maxConnections if maxConnections > 0
|
||||
@@servicesMutex.synchronize {
|
||||
if GServer.in_service?(@port,@host)
|
||||
raise "Port already in use: #{host}:#{@port}!"
|
||||
end
|
||||
@tcpServer = TCPServer.new(@host,@port)
|
||||
@port = @tcpServer.addr[1]
|
||||
@@services[@host] = {} unless @@services.has_key?(@host)
|
||||
@@services[@host][@port] = self;
|
||||
}
|
||||
@tcpServerThread = Thread.new {
|
||||
begin
|
||||
starting if @audit
|
||||
while !@shutdown
|
||||
@connectionsMutex.synchronize {
|
||||
while @connections.size >= @maxConnections
|
||||
@connectionsCV.wait(@connectionsMutex)
|
||||
end
|
||||
}
|
||||
client = @tcpServer.accept
|
||||
Thread.new(client) { |myClient|
|
||||
@connections << Thread.current
|
||||
begin
|
||||
myPort = myClient.peeraddr[1]
|
||||
serve(myClient) if !@audit or connecting(myClient)
|
||||
rescue => detail
|
||||
error(detail) if @debug
|
||||
ensure
|
||||
begin
|
||||
myClient.close
|
||||
rescue
|
||||
end
|
||||
@connectionsMutex.synchronize {
|
||||
@connections.delete(Thread.current)
|
||||
@connectionsCV.signal
|
||||
}
|
||||
disconnecting(myPort) if @audit
|
||||
end
|
||||
}
|
||||
end
|
||||
rescue => detail
|
||||
error(detail) if @debug
|
||||
ensure
|
||||
begin
|
||||
@tcpServer.close
|
||||
rescue
|
||||
end
|
||||
if @shutdown
|
||||
@connectionsMutex.synchronize {
|
||||
while @connections.size > 0
|
||||
@connectionsCV.wait(@connectionsMutex)
|
||||
end
|
||||
}
|
||||
else
|
||||
@connections.each { |c| c.raise "stop" }
|
||||
end
|
||||
@tcpServerThread = nil
|
||||
@@servicesMutex.synchronize {
|
||||
@@services[@host].delete(@port)
|
||||
}
|
||||
stopping if @audit
|
||||
end
|
||||
}
|
||||
self
|
||||
end
|
||||
|
||||
end
|
||||
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/bigdecimal.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/bigdecimal.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/continuation.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/continuation.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/coverage.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/coverage.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/date_core.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/date_core.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/dbm.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/dbm.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/bubblebabble.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/bubblebabble.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/md5.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/md5.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/rmd160.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/rmd160.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/sha1.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/sha1.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/sha2.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/digest/sha2.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/dl.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/dl.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/dl/callback.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/dl/callback.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/big5.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/big5.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/cp949.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/cp949.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/emacs_mule.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/emacs_mule.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/encdb.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/encdb.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/euc_jp.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/euc_jp.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/euc_kr.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/euc_kr.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/euc_tw.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/euc_tw.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/gb18030.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/gb18030.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/gb2312.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/gb2312.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/gbk.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/gbk.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_1.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_1.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_10.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_10.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_11.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_11.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_13.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_13.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_14.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_14.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_15.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_15.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_16.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_16.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_2.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_2.so
Normal file
Binary file not shown.
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_3.so
Normal file
BIN
ruby/lib/ruby/2.1.0/i386-mingw32/enc/iso_8859_3.so
Normal file
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user