summaryrefslogtreecommitdiff
path: root/sample/pty
diff options
context:
space:
mode:
Diffstat (limited to 'sample/pty')
-rw-r--r--sample/pty/expect_sample.rb58
-rw-r--r--sample/pty/script.rb37
-rw-r--r--sample/pty/shl.rb93
3 files changed, 188 insertions, 0 deletions
diff --git a/sample/pty/expect_sample.rb b/sample/pty/expect_sample.rb
new file mode 100644
index 0000000000..199d98b79c
--- /dev/null
+++ b/sample/pty/expect_sample.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+#
+# sample program of expect.rb
+#
+# by A. Ito
+#
+# This program reports the latest version of Ruby interpreter
+# by connecting to ftp server at ruby-lang.org.
+#
+require 'pty'
+require 'expect'
+
+fnames = []
+PTY.spawn("ftp ftp.ruby-lang.org") do |r_f,w_f,pid|
+ w_f.sync = true
+
+ $expect_verbose = false
+
+ if !ENV['USER'].nil?
+ username = ENV['USER']
+ elsif !ENV['LOGNAME'].nil?
+ username = ENV['LOGNAME']
+ else
+ username = 'guest'
+ end
+
+ r_f.expect(/^Name.*: /) do
+ w_f.puts("ftp")
+ end
+ r_f.expect(/word:/) do
+ w_f.puts("#{username}@")
+ end
+ r_f.expect(/> /) do
+ w_f.puts("cd pub/ruby")
+ end
+ r_f.expect("> ") do
+ w_f.print "pass\n"
+ end
+ r_f.expect("> ") do
+ w_f.print "dir\n"
+ end
+
+ r_f.expect(/[^\-]> /) do |output|
+ for x in output[0].split("\n")
+ if x =~ /(ruby.*?\.tar\.gz)/ then
+ fnames.push $1
+ end
+ end
+ end
+ begin
+ w_f.print "quit\n"
+ rescue
+ end
+end
+
+print "The latest ruby interpreter is "
+print fnames.sort.pop
+print "\n"
diff --git a/sample/pty/script.rb b/sample/pty/script.rb
new file mode 100644
index 0000000000..c6659a4807
--- /dev/null
+++ b/sample/pty/script.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+require 'pty'
+
+if ARGV.size == 0 then
+ ofile = "typescript"
+else
+ ofile = ARGV[0]
+end
+
+logfile = File.open(ofile,"a")
+
+system "stty -echo raw lnext ^_"
+
+PTY.spawn("/bin/csh") do |r_pty,w_pty,pid|
+
+ Thread.new do
+ while true
+ w_pty.print STDIN.getc.chr
+ w_pty.flush
+ end
+ end
+
+ begin
+ while true
+ c = r_pty.sysread(512)
+ break if c.nil?
+ print c
+ STDOUT.flush
+ logfile.print c
+ end
+ rescue
+ # print $@,':',$!,"\n"
+ logfile.close
+ end
+end
+
+system "stty echo -raw lnext ^v"
diff --git a/sample/pty/shl.rb b/sample/pty/shl.rb
new file mode 100644
index 0000000000..980748e8f5
--- /dev/null
+++ b/sample/pty/shl.rb
@@ -0,0 +1,93 @@
+# frozen_string_literal: true
+#
+# old-fashioned 'shl' like program
+# by A. Ito
+#
+# commands:
+# c creates new shell
+# C-z suspends shell
+# p lists all shell
+# 0,1,... choose shell
+# q quit
+
+require 'pty'
+require 'io/console'
+
+$shells = []
+
+$r_pty = nil
+$w_pty = nil
+
+def writer
+ STDIN.raw!
+ begin
+ while true
+ c = STDIN.getc
+ if c == ?\C-z then
+ $reader.raise('Suspend')
+ return 'Suspend'
+ end
+ $w_pty.print c.chr
+ $w_pty.flush
+ end
+ rescue
+ $reader.raise('Exit')
+ return 'Exit'
+ ensure
+ STDIN.cooked!
+ end
+end
+
+$reader = Thread.new {
+ while true
+ begin
+ Thread.stop unless $r_pty
+ c = $r_pty.getc
+ if c.nil? then
+ Thread.main.raise('Exit')
+ Thread.stop
+ end
+ print c.chr
+ STDOUT.flush
+ rescue
+ Thread.stop
+ end
+ end
+}
+
+# $reader.raise(nil)
+
+
+while true
+ print ">> "
+ STDOUT.flush
+ n = nil
+ case gets
+ when /^c/i
+ $shells << PTY.spawn("/bin/csh")
+ n = -1
+ when /^p/i
+ $shells.each_with_index do |s, i|
+ if s
+ print i,"\n"
+ end
+ end
+ when /^([0-9]+)/
+ n = $1.to_i
+ if $shells[n].nil?
+ print "\##{i} doesn't exist\n"
+ n = nil
+ end
+ when /^q/i
+ exit
+ end
+ if n
+ $r_pty, $w_pty, pid = $shells[n]
+ $reader.run
+ if writer == 'Exit' then
+ Process.wait(pid)
+ $shells[n] = nil
+ $shells.pop until $shells.empty? or $shells[-1]
+ end
+ end
+end