diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Env.rb | 1 | ||||
-rw-r--r-- | lib/README | 61 | ||||
-rw-r--r-- | lib/delegate.rb | 2 | ||||
-rw-r--r-- | lib/ping.rb | 31 | ||||
-rw-r--r-- | lib/telnet.rb | 103 | ||||
-rw-r--r-- | lib/thread.rb | 8 | ||||
-rw-r--r-- | lib/timeout.rb | 42 |
7 files changed, 204 insertions, 44 deletions
diff --git a/lib/Env.rb b/lib/Env.rb index df14023f9e..b3ee3bae8a 100644 --- a/lib/Env.rb +++ b/lib/Env.rb @@ -2,6 +2,7 @@ # # Usage: # +# require 'Env' # p $USER # $USER = "matz" # p ENV["USER"] diff --git a/lib/README b/lib/README new file mode 100644 index 0000000000..09e5946ccb --- /dev/null +++ b/lib/README @@ -0,0 +1,61 @@ +English.rb access global variables by english names +Env.rb access environment variables as globals +README this file +base64.rb encode/decode base64 (bit obsolete) +cgi-lib.rb decode CGI data +complex.rb complex number suppor +date.rb date object (compatible) +date2.rb date object based on Julian date +debug.rb ruby debugger +delegate.rb delegate messages to other object +e2mmap.rb exception utilities +eregex.rb extended regular expression (just a proof of concept) +final.rb add finalizer to the object (simple) +finalize.rb add finalizer to the object +find.rb traverse directory tree +ftools.rb file tools +ftplib.rb ftp access library +getopts.rb parse command line options +importenv.rb access environment variables as globals +jcode.rb japanese text handling (replace String methods) +mailread.rb read mail headers +mathn.rb extended math operation +matrix.rb matrix calculation library +mkmf.rb Makefile maker +monitor.rb exclusive region monitor for thread +mutex_m.rb mutex mixin +observer.rb observer desing pattern library (provides Observable) +open3.rb open subprocess connection stdin/stdout/stderr +ostruct.rb python style object +parsearg.rb argument parser using getopts +parsedate.rb parse date string +ping.rb +profile.rb ruby profiler +pstore.rb persistent object strage using marshal +rational.rb rational number support +readbytes.rb define IO#readbytes +shell.rb shell like operation under Ruby (imcomlete) +shellwords.rb split into words like shell +singleton.rb singleton design pattern library +sync.rb 2 phase lock +telnet.rb telnet library +tempfile.rb temporary file that automatically removed +thread.rb thread support +thwait.rb thread syncronization class +timeout.rb provids timeout +tk.rb Tk interface +tkafter.rb +tkbgerror.rb Tk error module +tkcanvas.rb Tk canvas interface +tkclass.rb provides generic names for Tk classes +tkdialog.rb Tk dialog class +tkentry.rb Tk entry class +tkfont.rb Tk font support +tkmenubar.rb TK menubar utility +tkmngfocus.rb focus manager +tkpalette.rb pallete support +tkscrollbox.rb scroll box, also example of compound widget +tktext.rb text classes +tkvirtevent.rb virtual event support +tracer.rb execution tracer +weakref.rb weak reference class diff --git a/lib/delegate.rb b/lib/delegate.rb index 16e68c9d6c..d784586702 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -24,7 +24,7 @@ class Delegator preserved |= t.instance_methods break if t == Delegator end - preserved -= ["to_s","to_a","inspect","hash","eql?","==","=~","==="] + preserved -= ["to_s","to_a","inspect","==","=~","==="] for method in obj.methods next if preserved.include? method eval <<EOS diff --git a/lib/ping.rb b/lib/ping.rb index d742a50f99..08e05a01ec 100644 --- a/lib/ping.rb +++ b/lib/ping.rb @@ -26,6 +26,10 @@ # # The timeout in seconds. If not specified it will default to 5 seconds. # +# : service +# +# The service port to connect. The default is "echo". +# #= WARNING # # pingecho() uses user-level thread to implement the timeout, so it may block @@ -33,23 +37,26 @@ # #=end +require 'timeout' + module Ping require "socket" - def pingecho(host, timeout=5) + def pingecho(host, timeout=5, service="echo") begin - x = Thread.current - y = Thread.start { - sleep timeout - x.raise RuntimeError if x.status - } - s = TCPsocket.new(host, "echo") - s.close - return TRUE + timeout(timeout) do + s = TCPsocket.new(host, "echo") + s.close + end rescue - return FALSE; - ensure - Thread.kill y if y.status + return false end + return true end module_function "pingecho" end + +if $0 == __FILE__ + host = ARGV[0] + host ||= "localhost" + printf("%s alive? - %s\n", host, Ping.pingecho(host, 5)) +end diff --git a/lib/telnet.rb b/lib/telnet.rb index e5a8aa6b7b..e3c590c35d 100644 --- a/lib/telnet.rb +++ b/lib/telnet.rb @@ -1,8 +1,17 @@ # # telnet.rb -# ver0.141 1998/09/22 +# ver0.16 1998/10/09 # Wakou Aoyama <wakou@fsinet.or.jp> # +# ver0.16 1998/10/09 +# preprocess method change for the better +# add binmode method. +# change default Binmode +# TRUE --> FALSE +# +# ver0.15 1998/10/04 +# add telnetmode method. +# # ver0.141 1998/09/22 # change default prompt # /[$%#>] $/ --> /[$%#>] \Z/ @@ -33,7 +42,7 @@ # first release. # # == make new Telnet object -# host = Telnet.new({"Binmode" => TRUE, default: TRUE +# host = Telnet.new({"Binmode" => FALSE, default: FALSE # "Host" => "localhost", default: "localhost" # "Output_log" => "output_log", default: not output # "Dump_log" => "dump_log", default: not output @@ -76,6 +85,16 @@ # == send string # host.print("string") # +# == turn telnet command interpretation +# host.telnetmode # turn on/off +# host.telnetmode(TRUE) # on +# host.telnetmode(FALSE) # off +# +# == toggle newline translation +# host.binmode # turn TRUE/FALSE +# host.binmode(TRUE) # no translate newline +# host.binmode(FALSE) # translate newline +# # == login # host.login("username", "password") # host.login({"Name" => "username", @@ -204,6 +223,7 @@ class Telnet < SimpleDelegator OPT_AUTHENTICATION = 37.chr # Authentication Option OPT_ENCRYPT = 38.chr # Encryption Option OPT_NEW_ENVIRON = 39.chr # New Environment Option + OPT_EXOPL = 255.chr # Extended-Options-List NULL = "\000" CR = "\015" @@ -212,7 +232,7 @@ class Telnet < SimpleDelegator def initialize(options) @options = options - @options["Binmode"] = TRUE if not @options.include?("Binmode") + @options["Binmode"] = FALSE if not @options.include?("Binmode") @options["Host"] = "localhost" if not @options.include?("Host") @options["Port"] = 23 if not @options.include?("Port") @options["Prompt"] = /[$%#>] \Z/ if not @options.include?("Prompt") @@ -225,7 +245,7 @@ class Telnet < SimpleDelegator if @options.include?("Output_log") @log = File.open(@options["Output_log"], 'a+') @log.sync = TRUE - @log.binmode if @options["Binmode"] + @log.binmode end if @options.include?("Dump_log") @@ -250,7 +270,7 @@ class Telnet < SimpleDelegator } raise TimeOut, "timed-out; opening of the host" if is_timeout @sock.sync = TRUE - @sock.binmode if @options["Binmode"] + @sock.binmode message = "Connected to " + @options["Host"] + ".\n" STDOUT.write(message) @@ -260,46 +280,62 @@ class Telnet < SimpleDelegator super(@sock) end + def telnetmode(mode = 'turn') + if 'turn' == mode + @options["Telnetmode"] = @options["Telnetmode"] ? FALSE : TRUE + else + @options["Telnetmode"] = mode ? TRUE : FALSE + end + end + + def binmode(mode = 'turn') + if 'turn' == mode + @options["Binmode"] = @options["Binmode"] ? FALSE : TRUE + else + @options["Binmode"] = mode ? TRUE : FALSE + end + end + def preprocess(str) - str.gsub!(/#{CR}#{NULL}/no, CR) # combine CR+NULL into CR - str.gsub!(/#{EOL}/no, "\n") # combine EOL into "\n" + + if not @options["Binmode"] + str.gsub!(/#{CR}#{NULL}/no, CR) # combine CR+NULL into CR + str.gsub!(/#{EOL}/no, "\n") # combine EOL into "\n" + end # respond to "IAC DO x" - str.gsub!(/([^#{IAC}])?#{IAC}#{DO}(.|\n)/no){ - if OPT_BINARY == $2 + str.gsub!(/(?:(?!#{IAC}))?#{IAC}#{DO}([#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{OPT_EXOPL}])/no){ + if OPT_BINARY == $1 @telnet_option["BINARY"] = TRUE @sock.write(IAC + WILL + OPT_BINARY) - $1 else - @sock.write(IAC + WONT + $2) - $1 + @sock.write(IAC + WONT + $1) end + '' } # respond to "IAC DON'T x" with "IAC WON'T x" - str.gsub!(/([^#{IAC}])?#{IAC}#{DONT}(.|\n)/no){ - @sock.write(IAC + WONT + $2) - $1 + str.gsub!(/(?:(?!#{IAC}))?#{IAC}#{DONT}([#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{OPT_EXOPL}])/no){ + @sock.write(IAC + WONT + $1) + '' } # respond to "IAC WILL x" - str.gsub!(/([^#{IAC}])?#{IAC}#{WILL}(.|\n)/no){ - if OPT_SGA == $2 + str.gsub!(/(?:(?!#{IAC}))?#{IAC}#{WILL}([#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{OPT_EXOPL}])/no){ + if OPT_SGA == $1 @telnet_option["SGA"] = TRUE @sock.write(IAC + DO + OPT_SGA) - $1 - else - $1 end + '' } # ignore "IAC WON'T x" - str.gsub!(/([^#{IAC}])?#{IAC}#{WONT}(.|\n)/no, '\1') + str.gsub!(/(?:(?!#{IAC}))?#{IAC}#{WONT}[#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{OPT_EXOPL}]/no, '') # respond to "IAC AYT" (are you there) - str.gsub!(/([^#{IAC}])?#{IAC}#{AYT}/no){ + str.gsub!(/(?:(?!#{IAC}))?#{IAC}#{AYT}/no){ @sock.write("nobody here but us pigeons" + EOL) - $1 + '' } str.gsub(/#{IAC}#{IAC}/no, IAC) # handle escaped IAC characters @@ -340,15 +376,20 @@ class Telnet < SimpleDelegator end def print(string) - if @telnet_option["BINARY"] and @telnet_option["SGA"] - # IAC WILL SGA IAC DO BIN send EOL --> CR - @sock.write(string.gsub(/\n/, CR) + CR) - elsif @telnet_option["SGA"] - # IAC WILL SGA send EOL --> CR+NULL - @sock.write(string.gsub(/\n/, CR + NULL) + CR + NULL) + string.gsub!(/#{IAC}/no, IAC + IAC) if @options["Telnetmode"] + if @options["Binmode"] + @sock.write(string) else - # NONE send EOL --> LF - @sock.write(string.gsub(/\n/, LF) + LF) + if @telnet_option["BINARY"] and @telnet_option["SGA"] + # IAC WILL SGA IAC DO BIN send EOL --> CR + @sock.write(string.gsub(/\n/, CR) + CR) + elsif @telnet_option["SGA"] + # IAC WILL SGA send EOL --> CR+NULL + @sock.write(string.gsub(/\n/, CR + NULL) + CR + NULL) + else + # NONE send EOL --> LF + @sock.write(string.gsub(/\n/, LF) + LF) + end end end diff --git a/lib/thread.rb b/lib/thread.rb index 7706d59cbb..ec75144374 100644 --- a/lib/thread.rb +++ b/lib/thread.rb @@ -21,6 +21,8 @@ class Mutex def initialize @waiting = [] @locked = false; + @waiting.taint # enable tainted comunication + self.taint end def locked? @@ -72,6 +74,8 @@ class ConditionVariable def initialize @waiters = [] @waiters_mutex = Mutex.new + @waiters.taint # enable tainted comunication + self.taint end def wait(mutex) @@ -104,6 +108,9 @@ class Queue def initialize @que = [] @waiting = [] + @que.taint # enable tainted comunication + @waiting.taint + self.taint end def push(obj) @@ -152,6 +159,7 @@ class SizedQueue<Queue def initialize(max) @max = max @queue_wait = [] + @queue_wait.taint # enable tainted comunication super() end diff --git a/lib/timeout.rb b/lib/timeout.rb new file mode 100644 index 0000000000..d4ea758acd --- /dev/null +++ b/lib/timeout.rb @@ -0,0 +1,42 @@ +# +# timeout.rb -- execution timeout +# +#= SYNOPSIS +# +# require 'timeout' +# status = timeout(5) { +# # something may take time +# } +# +#= DESCRIPTION +# +# timeout executes the block. If the block execution terminates successfully +# before timeout, it returns true. If not, it terminates the execution and +# raise TimeoutError exception. +# +#== Parameters +# +# : timout +# +# The time in seconds to wait for block teminatation. +# +#=end + +class TimeoutError<StandardError +end + +Thread.abort_on_exception = true + +def timeout(sec) + begin + x = Thread.current + y = Thread.start { + sleep sec + x.raise TimeoutError, "execution expired" if x.status + } + yield sec + return true + ensure + Thread.kill y if y.status + end +end |