-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxexpect
More file actions
executable file
·213 lines (182 loc) · 4.93 KB
/
xexpect
File metadata and controls
executable file
·213 lines (182 loc) · 4.93 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/perl
# xexpect screen scrapes X11 windows and performes actions if matching text
# patterns are found. xexpect is intended to be run as an rdesktop agent (option -G).
#
# Usage: xexpect <file>
#
# The xexpect file format is described below.
#
# Global commands:
#
# interval <seconds>
# Poll interval, how often to screen scrape the window.
#
# defmacro <name> <cmdline>
# Defined a macro.
#
# expect <regexp>
# Starts an expect block. If any text matches the regexp pattern, the
# commands in the block are executed.
#
# Commands in expect blocks:
#
# macro <name>
# Invokes macro.
#
# stdout <text>
# Outputs text on stdout.
#
#
# xexpect talks the following protocol on stdin/stdout.
#
# Commands accepted on stdin:
#
# init
# Just a greeting sent by rdesktop in agent mode.
#
# window <id>
# The window id (e.g. 0x5000095) to screen scrape.
#
# Commands sent to stdout:
#
# exit
# rdesktop should exit.
#
# send <string>
# Send string as fake key press events to the window. The string may
# contain the following special characters:
# ^m - return
# ^t - tab
# ^? - delete
# ^h - backspace
# ^^ - literal ^
# ^{symbol} - X11 key symbol, e.g. ^{Down} for down arrow
# ^{A-symbol} - same as above but with alt key, e.g. ^{A-o} sends Alt+o
use strict;
use warnings;
use Data::Dumper;
use File::Temp qw/tempfile/;
use File::Slurp qw/read_file/;
use IO::Select;
my $display = $ENV{'DISPLAY'};
#my $wid = $ENV{'WINDOWID'};
my $wid;
sub capture {
if (!$wid) {
print STDERR "Please set environment variable DISPLAY\n";
return;
}
if (!$wid) {
#print STDERR "Please set environment variable WINDOWID\n";
print STDERR "Have not received windowid param yet\n";
return;
}
my ($fh1, $jpg) = tempfile(SUFFIX => '.jpg', UNLINK => 1);
my ($fh2, $pnm) = tempfile(SUFFIX => '.pnm', UNLINK => 1);
my ($fh3, $txt) = tempfile(SUFFIX => '.txt', UNLINK => 1);
print STDERR "Capturing $wid at $display\n";
`import -window $wid $jpg`;
if ($?) {
print STDERR "import(1) failed\n";
exit 1;
}
`convert $jpg $pnm`;
if ($?) {
print STDERR "convert(1) failed\n";
exit 1;
}
`gocr -i $pnm > $txt`;
if ($?) {
print STDERR "gocr(1) failed\n";
exit 1;
}
my $output = read_file($txt);
print STDERR "OUTPUT: $output\n";
return $output;
}
my $toexpect;
my %actions;
my %macros;
my $interval = 5;
my $path = $ARGV[0];
open(FH, $path) or die "failed to open $path: $!";
while (my $line = <FH>) {
next if $line =~ /\s*#/;
next if $line =~ /^\s*$/;
chomp $line;
if ($line =~ /^\s*expect (.*)/) {
$toexpect = $1;
next;
}
if ($toexpect) {
if ($line =~ /^\s*(stdout|macro|interval)\s+(.*)/) {
push @{$actions{$toexpect}}, [$1, $2];
next;
}
} else {
if ($line =~ /^\s*interval\s+(.*)/) {
$interval = $1;
next;
}
if ($line =~ /^\s*defmacro\s+(.*?)\s+(.*)/) {
$macros{$1} = $2;
next;
}
}
print STDERR "Failed to parse '$line'\n";
}
close(FH);
print STDERR "Actions: " . Dumper(\%actions);
print STDERR "Macros: " . Dumper(\%macros);
my $sel = IO::Select->new();
$sel->add(\*STDIN);
$| = 1;
while (1) {
my $start = time();
while ($sel->can_read($interval)) {
my @lines = <STDIN>;
for my $line (@lines) {
if (!defined $line || $line eq '') {
exit(0)
}
chomp $line;
print STDERR "From rdesktop: $line\n";
if ($line =~ /^window (.*)$/) {
$wid = $1;
print STDERR "Will capture from window $wid\n";
}
}
}
next unless $wid;
my $left = $interval - (time() - $start);
sleep $left if $left > 0;
my $output = capture();
next unless $output;
for my $s (sort keys %actions) {
print STDERR "TRYING $s\n";
if ($output =~ /$s/) {
my @acts = @{$actions{$s}};
print STDERR "ACTIONS " . Dumper(\@acts);
while (my $a = shift @acts) {
print STDERR "ACTION @$a\n";
my ($cmd, $arg) = @$a;
if ($cmd eq 'macro') {
my $cmdline = $macros{$arg};
if ($cmdline) {
print STDERR "MACRO $arg\n";
unshift @acts, [split(/\s+/, $cmdline, 2)];
} else {
print STDERR "UNKNOWN MACRO '$arg'\n";
}
}
if ($cmd eq 'interval') {
$interval = $1;
}
if ($cmd eq 'stdout') {
print STDERR "WRITING TO STDOUT: $arg\n";
print $arg . "\n";
}
}
}
}
}