1 |
cvsjoko |
1.1 |
#!/usr/bin/perl |
2 |
|
|
## |
3 |
|
|
## POE::Component::Terminal -- Terminal Component |
4 |
|
|
## |
5 |
cvsjoko |
1.4 |
## $Id: Terminal.pm,v 1.3 2002/06/15 04:24:15 cvsjoko Exp $ |
6 |
cvsjoko |
1.3 |
## |
7 |
|
|
## $Log: Terminal.pm,v $ |
8 |
cvsjoko |
1.4 |
## Revision 1.3 2002/06/15 04:24:15 cvsjoko |
9 |
|
|
## + added modifier for running in linux or win32 |
10 |
|
|
## + modified order of some commands in core key-polling |
11 |
|
|
## |
12 |
cvsjoko |
1.3 |
## Revision 1.2 2002/06/15 03:45:21 cvsjoko |
13 |
|
|
## + cvs id & log |
14 |
|
|
## + clearing inputbuffer just after getting the "enter"-key |
15 |
cvsjoko |
1.2 |
## |
16 |
cvsjoko |
1.1 |
## |
17 |
|
|
package POE::Component::Terminal; |
18 |
|
|
|
19 |
|
|
use strict; |
20 |
|
|
#use strict qw ( vars refs ); |
21 |
|
|
use warnings; |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
use Carp qw( croak carp ); |
25 |
|
|
|
26 |
|
|
use POE; |
27 |
|
|
|
28 |
|
|
use Term::ReadKey; |
29 |
|
|
use Data::Dumper; |
30 |
|
|
|
31 |
|
|
$POE::Component::Terminal::VERSION = 0.01; |
32 |
|
|
|
33 |
|
|
my @args_needed = qw ( |
34 |
|
|
Alias |
35 |
|
|
Caption |
36 |
|
|
PromptPrefix |
37 |
|
|
PromptPostfix |
38 |
|
|
RequestSession |
39 |
|
|
RequestState |
40 |
|
|
ResponseSession |
41 |
|
|
ResponseState |
42 |
|
|
PromptChangeState |
43 |
|
|
); |
44 |
|
|
|
45 |
|
|
my $states = { |
46 |
|
|
_start => 'start', |
47 |
|
|
_stop => 'stop', |
48 |
|
|
_signal => 'signal', |
49 |
|
|
_default => 'default', |
50 |
|
|
pollForKey => 'pollForKey', |
51 |
|
|
recievePrompt => 'recievePrompt', |
52 |
|
|
recieveResponse => 'recieveResponse', |
53 |
|
|
}; |
54 |
|
|
|
55 |
cvsjoko |
1.3 |
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
56 |
|
|
|
57 |
cvsjoko |
1.1 |
sub new { |
58 |
|
|
my $class = shift; |
59 |
|
|
$class = ref( $class ) || $class; |
60 |
|
|
#my( %args ) = ( %default_args, @_ ); |
61 |
|
|
my $args = shift; |
62 |
|
|
|
63 |
|
|
# bless class into object ($self) |
64 |
|
|
my $self = bless {}, $class; |
65 |
|
|
|
66 |
|
|
## Check args |
67 |
|
|
foreach my $arg_needed (@args_needed) { |
68 |
|
|
croak "Need $arg_needed argument." |
69 |
|
|
unless exists $args->{ $arg_needed }; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
# store entries from args to object ($self) |
73 |
|
|
map { $self->{$_} = $args->{$_}; } keys %{$args}; |
74 |
|
|
|
75 |
cvsjoko |
1.3 |
if ( RUNNING_IN_HELL () ) { |
76 |
|
|
$self->{conf}{EnterKey} = "\r"; |
77 |
|
|
} else { |
78 |
|
|
$self->{conf}{EnterKey} = "\n"; |
79 |
|
|
} |
80 |
|
|
|
81 |
cvsjoko |
1.1 |
## Make our session. See $states defined up above . . . |
82 |
|
|
POE::Session->create( object_states => [ $self => $states, ], ); |
83 |
|
|
|
84 |
|
|
return $self; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
## |
88 |
|
|
## start -- Startup state |
89 |
|
|
## |
90 |
|
|
sub start { |
91 |
|
|
my( $self, $kernel, $heap, $session, $sender ) = |
92 |
|
|
@_[ OBJECT, KERNEL, HEAP, SESSION, SENDER ]; |
93 |
|
|
|
94 |
|
|
## Pipe up if we're debugging |
95 |
|
|
print STDERR "## ", __PACKAGE__, "::start\r\n" if $self->{Debug}; |
96 |
|
|
|
97 |
|
|
## Set the alias requested so we can be post'd to |
98 |
|
|
$kernel->alias_set( $self->{Alias} ); |
99 |
|
|
|
100 |
|
|
## Remember session that created us for posting DispatchEvent |
101 |
|
|
$self->{ _target } = $sender; |
102 |
|
|
|
103 |
|
|
$heap->{state}{InputBuffer} = ''; |
104 |
|
|
$heap->{state}{Startup} = 1; |
105 |
|
|
$heap->{state}{Caption} = $self->{Caption}; |
106 |
|
|
|
107 |
|
|
# initially call worker-state |
108 |
|
|
$kernel->post($session, "pollForKey"); |
109 |
|
|
|
110 |
|
|
return; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
## |
114 |
|
|
## stop -- Shutdown state |
115 |
|
|
## |
116 |
|
|
sub stop { |
117 |
|
|
my( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
118 |
|
|
|
119 |
|
|
## Just pipe up if we're debugging |
120 |
|
|
print STDERR "## ", __PACKAGE__, "::stop\r\n" if $self->{_debug}; |
121 |
|
|
|
122 |
|
|
return |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
## |
126 |
|
|
## signal -- Handle any signals received |
127 |
|
|
## |
128 |
|
|
sub signal { |
129 |
|
|
my( $self, $heap, $signal ) = @_[ OBJECT, HEAP, ARG0 ]; |
130 |
|
|
|
131 |
|
|
## Just pipe up if we're debugging |
132 |
|
|
print STDERR "## ", __PACKAGE__, "::signal $signal\n" if $self->{_debug}; |
133 |
|
|
|
134 |
|
|
## shut things down on TERM, QUIT, or INT |
135 |
|
|
if( $signal =~ /^TERM|QUIT|INT/ ) { |
136 |
|
|
delete $heap->{wheel}; # toss our listening wheel reference |
137 |
|
|
|
138 |
|
|
## Check for child wheels |
139 |
|
|
my $live_wheels = scalar keys %{$heap->{wheels}}; |
140 |
|
|
if( $live_wheels ) { |
141 |
|
|
print STDERR "Exiting with $live_wheels clients still active\n" |
142 |
|
|
if $self->{_debug}; |
143 |
|
|
delete $heap->{wheels}; |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
return |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
## |
151 |
|
|
## default -- Catch any unhandled events for debugging |
152 |
|
|
## |
153 |
|
|
sub default { |
154 |
|
|
my( $self, $heap, $event ) = @_[ OBJECT, HEAP, ARG0 ]; |
155 |
|
|
|
156 |
|
|
## Just pipe up if we're debugging |
157 |
|
|
print STDERR "## ", __PACKAGE__, "::default got $event\n" |
158 |
|
|
if $self->{_debug}; |
159 |
|
|
|
160 |
|
|
return |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
# ================ POE stuff |
170 |
|
|
|
171 |
|
|
sub pollForKey { |
172 |
|
|
|
173 |
|
|
my ($self, $kernel, $heap, $session) = @_[OBJECT, KERNEL, HEAP, SESSION ]; |
174 |
|
|
|
175 |
|
|
my $prompt_inlay = $heap->{state}{PromptInlay}; |
176 |
|
|
if ($prompt_inlay) { $prompt_inlay .= ' '; } else { $prompt_inlay = ''; } |
177 |
|
|
|
178 |
|
|
my $prompt = "#> "; |
179 |
|
|
$prompt = $self->{PromptPrefix} . ' ' . $prompt_inlay . $self->{PromptPostfix}; |
180 |
|
|
|
181 |
|
|
if ( ($heap->{state}{KeyPressed} && !$heap->{state}{RequestPending}) || ($heap->{state}{Startup}) ) { |
182 |
|
|
$heap->{state}{Startup} = 0; |
183 |
|
|
$heap->{state}{KeyPressed} = 0; |
184 |
|
|
#ReadMode 4; # Turn off controls keys |
185 |
|
|
#ReadMode 1; |
186 |
|
|
#print $prompt; |
187 |
|
|
my $outputline = $prompt . $heap->{state}{InputBuffer}; |
188 |
|
|
my $s = ' ' x (length($outputline) + 1); |
189 |
|
|
print "\r", $s; |
190 |
|
|
print "\r", $outputline; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
my $key = ReadKey(-1); |
194 |
|
|
if ($key) { |
195 |
|
|
|
196 |
|
|
# normal key? |
197 |
|
|
if ($key =~ m/[a-zA-Z? ]/) { |
198 |
|
|
$heap->{state}{InputBuffer} .= $key; |
199 |
|
|
#print $key; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
# enter pressed? |
203 |
cvsjoko |
1.3 |
if ($key eq $self->{conf}{EnterKey}) { |
204 |
cvsjoko |
1.1 |
# send command |
205 |
|
|
my $buf = $heap->{state}{InputBuffer}; |
206 |
|
|
if ($buf) { |
207 |
cvsjoko |
1.2 |
$heap->{state}{InputBuffer} = ''; |
208 |
cvsjoko |
1.1 |
$heap->{state}{RequestPending} = 1; |
209 |
|
|
#print "ord: ", ord($buf), "\n"; |
210 |
|
|
my $response_target = { |
211 |
|
|
ResponseSession => $self->{ResponseSession}, |
212 |
|
|
ResponseState => $self->{ResponseState}, |
213 |
|
|
}; |
214 |
|
|
$kernel->post( $self->{RequestSession} => $self->{RequestState} => $buf, $response_target); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
# prepare new loop for fresh prompt |
218 |
|
|
print "\n"; |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
# backspace pressed? |
222 |
|
|
if (ord($key) == 8) { |
223 |
|
|
$heap->{state}{InputBuffer} = substr($heap->{state}{InputBuffer}, 0, -1); |
224 |
|
|
} |
225 |
|
|
|
226 |
cvsjoko |
1.3 |
# mark "KeyPressed" for next poll |
227 |
|
|
$heap->{state}{KeyPressed} = 1; |
228 |
|
|
|
229 |
cvsjoko |
1.1 |
} |
230 |
|
|
|
231 |
cvsjoko |
1.4 |
$kernel->post($session, "pollForKey"); |
232 |
cvsjoko |
1.3 |
#$kernel->delay("pollForKey", 0.1); |
233 |
cvsjoko |
1.4 |
#$kernel->delay("pollForKey", 0.01); |
234 |
cvsjoko |
1.1 |
|
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
sub recieveResponse { |
241 |
|
|
my ($kernel, $heap, $session, $response) = @_[KERNEL, HEAP, SESSION, ARG0]; |
242 |
|
|
if ($response) { print $response, "\n"; } |
243 |
|
|
$heap->{state}{RequestPending} = 0; |
244 |
|
|
} |
245 |
|
|
sub recievePrompt { |
246 |
|
|
my ($kernel, $heap, $session, $prompt_inlay) = @_[KERNEL, HEAP, SESSION, ARG0]; |
247 |
|
|
$heap->{state}{PromptInlay} = $prompt_inlay; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
|
251 |
|
|
1; |