CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/ui/text/input/buffer.rb
Views: 11704
1
# -*- coding: binary -*-
2
3
module Rex
4
module Ui
5
module Text
6
7
require 'rex/io/stream_abstraction'
8
9
###
10
#
11
# This class implements input against a socket.
12
#
13
###
14
class Input::Buffer < Rex::Ui::Text::Input
15
16
class BufferSock
17
include Rex::IO::StreamAbstraction
18
def write(buf, opts={})
19
syswrite(buf)
20
end
21
22
def monitor_rsock(*args, **kwargs)
23
dlog('monitor_rsock: Skipping - functionality not required')
24
end
25
end
26
27
def initialize
28
@sock = BufferSock.new
29
@sock.initialize_abstraction
30
end
31
32
def close
33
@sock.cleanup_abstraction
34
end
35
36
def sysread(len = 1)
37
@sock.rsock.sysread(len)
38
end
39
40
def put(msg, opts={})
41
@sock.lsock.write(msg)
42
end
43
44
#
45
# Wait for a line of input to be read from a socket.
46
#
47
def gets
48
# Initialize the line buffer
49
line = ''
50
51
# Read data one byte at a time until we see a LF
52
while (true)
53
break if line.include?("\n")
54
55
# Read another character of input
56
char = @sock.rsock.getc
57
58
# Append this character to the string
59
line << char
60
end
61
62
return line
63
end
64
65
#
66
# Returns whether or not EOF has been reached on stdin.
67
#
68
def eof?
69
@sock.lsock.closed?
70
end
71
72
#
73
# Returns the file descriptor associated with a socket.
74
#
75
def fd
76
return @sock.rsock
77
end
78
end
79
80
end
81
end
82
end
83
84