-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.rb
147 lines (124 loc) · 3.74 KB
/
io.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright (C) 2010, 2011 Rocky Bernstein <[email protected]>
# classes to support communication to and from the debugger. This
# communcation might be to/from another process or another computer.
# And reading may be from a debugger command script.
#
# For example, we'd like to support Sockets, and serial lines and file
# reading, as well a readline-type input. Encryption and Authentication
# methods might decorate some of the communication channels.
#
# Some ideas originiated as part of Matt Fleming's 2006 Google Summer of
# Code project.
module Trepan
NotImplementedMessage = 'This method must be overriden in a subclass' unless
defined?(NotImplementedMessage)
class InputBase
attr_reader :input
attr_reader :line_edit
DEFAULT_OPTS = {
:line_edit => false,
} unless defined?(DEFAULT_OPTS)
def initialize(inp, opts={})
@opts = DEFAULT_OPTS.merge(opts)
@input = inp
@line_edit = opts[:line_edit]
end
def close
@input.close unless @input.closed?
end
def eof?
begin
@input.eof?
rescue IOError
true
end
end
# Read a line of input. EOFError will be raised on EOF.
#
# Note that we don't support prompting first. Instead, arrange
# to call Trepan::Output.write() first with the prompt. If
# `use_raw' is set raw_input() will be used in that is supported
# by the specific input input. If this option is left None as is
# normally expected the value from the class initialization is
# used.
def readline
@input.readline
end
end
# This is an abstract class that specifies debugger output.
class OutputBase
attr_accessor :flush_after_write
attr_reader :output
def initialize(out, opts={})
@output = out
@flush_after_write = false
@eof = false
end
def close
@output.close if @output
@eof = true
end
def eof?
@eof
end
def flush
@output.flush
end
# Use this to set where to write to. output can be a
# file object or a string. This code raises IOError on error.
def write(*args)
@output.print(*args)
end
# used to write to a debugger that is connected to this
# `str' written will have a newline added to it
#
def writeline(msg)
@output.write("%s\n" % msg)
end
end
# This is an abstract class that specifies debugger input output when
# handled by the same channel, e.g. a socket or tty.
#
class InOutBase
def initialize(inout, opts={})
@opts = DEFAULT_OPTS.merge(opts)
@inout = inout
end
def close
@inout.close() if @inout
end
def eof?
begin
@input.eof?
rescue IOError
true
end
end
def flush
@inout.flush
end
# Read a line of input. EOFError will be raised on EOF.
#
# Note that we don't support prompting first. Instead, arrange to
# call DebuggerOutput.write() first with the prompt. If `use_raw'
# is set raw_input() will be used in that is supported by the
# specific input input. If this option is left nil as is normally
# expected the value from the class initialization is used.
def readline(use_raw=nil)
@input.readline
end
# Use this to set where to write to. output can be a
# file object or a string. This code raises IOError on error.
#
# Use this to set where to write to. output can be a
# file object or a string. This code raises IOError on error.
def write(*args)
@inout.write(*args)
end
# used to write to a debugger that is connected to this
# server; `str' written will have a newline added to it
def writeline( msg)
@inout.write("%s\n" % msg)
end
end
end