| 1 |
#!/usr/bin/perl -wT |
| 2 |
# (cannot use /usr/bin/env here) |
| 3 |
# |
| 4 |
# This script implements a simple remote-control mechanism for Tk |
| 5 |
# applications. It allows you to select an application and then type |
| 6 |
# commands to that application. |
| 7 |
|
| 8 |
|
| 9 |
require 5.002; |
| 10 |
use English; |
| 11 |
|
| 12 |
use Tk; |
| 13 |
use Tk::ErrorDialog; |
| 14 |
use strict; |
| 15 |
|
| 16 |
sub get_eval_status; sub prompt; |
| 17 |
|
| 18 |
$ENV{HOME} = '/home/bug'; |
| 19 |
|
| 20 |
my $MW = MainWindow->new; |
| 21 |
$MW->minsize(1, 1); |
| 22 |
$MW->ErrorDialog->configure('-cleanupcode' => \&prompt); |
| 23 |
|
| 24 |
my $app = "local"; # application name that we're sending to |
| 25 |
my $lastCommand = ""; # use this command if !! entered |
| 26 |
|
| 27 |
# Create menu bar. Arrange to recreate all the information in the |
| 28 |
# applications sub-menu whenever it is cascaded to. |
| 29 |
|
| 30 |
my $menu = $MW->Frame(-relief => 'raised', -bd => 2); |
| 31 |
my $menu_file = $menu->Menubutton(-text => "File", -underline => 0); |
| 32 |
my $SELECT_APPLICATION = 'Select Application'; |
| 33 |
$menu_file->cascade(-label => $SELECT_APPLICATION, -underline => 0); |
| 34 |
$menu_file->command(-label => 'Quit', -command => \&exit, -underline => 0); |
| 35 |
my $menu_file_m = $menu_file->cget(-menu); |
| 36 |
my $menu_file_m_apps = $menu_file_m->Menu; |
| 37 |
$menu_file_m->entryconfigure($SELECT_APPLICATION, -menu => $menu_file_m_apps); |
| 38 |
$menu_file_m->configure(-postcommand => \&fillAppsMenu); |
| 39 |
$menu->pack(-side => 'top', -fill => 'x'); |
| 40 |
$menu_file->pack(-side => 'left'); |
| 41 |
|
| 42 |
# Create text window and scrollbar. |
| 43 |
|
| 44 |
my $t = $MW->Text(-relief => "raised", -borderwidth => 2, -setgrid => 1); |
| 45 |
my $s = $MW->Scrollbar(-relief => "flat", -command => ['yview', $t]); |
| 46 |
$t->configure(-yscrollcommand => ['set', $s]); |
| 47 |
$s->pack(-side => 'right', -fill => 'both'); |
| 48 |
$t->pack(-side => 'left'); |
| 49 |
|
| 50 |
# Perl -w handler to fill text widget with eval errors. |
| 51 |
|
| 52 |
$SIG{'__WARN__'} = \&get_eval_status; |
| 53 |
|
| 54 |
# Create a binding to forward commands to the target application, plus modify |
| 55 |
# many of the built-in bindings so that only information in the current |
| 56 |
# command can be deleted (can still set the cursor earlier in the text and |
| 57 |
# select and insert; just can't delete). |
| 58 |
|
| 59 |
$t->bindtags([$t, 'Tk::Text', $MW, 'all']); # use *my* bindings before |
| 60 |
# considering those of class Text |
| 61 |
$t->bind('<Return>' => sub { |
| 62 |
my $t = shift; |
| 63 |
$t->mark('set', 'insert', 'end - 1c'); |
| 64 |
$t->insert('insert', "\n"); |
| 65 |
&invoke(); |
| 66 |
$t->break; |
| 67 |
}); |
| 68 |
$t->bind('<Delete>' => sub { |
| 69 |
my $t = shift; |
| 70 |
if (defined $t->tag('nextrange', 'sel', '1.0', 'end')) { |
| 71 |
$t->tag('remove', 'sel', 'sel.first', 'promptEnd'); |
| 72 |
} else { |
| 73 |
$t->break if $t->compare('insert', '<', 'promptEnd'); |
| 74 |
} |
| 75 |
}); |
| 76 |
$t->bind('<BackSpace>' => sub { |
| 77 |
my $t = shift; |
| 78 |
if (defined $t->tag('nextrange', 'sel', '1.0', 'end')) { |
| 79 |
$t->tag('remove', 'sel', 'sel.first', 'promptEnd'); |
| 80 |
} else { |
| 81 |
$t->break if $t->compare('insert', '<', 'promptEnd'); |
| 82 |
} |
| 83 |
}); |
| 84 |
$t->bind('<Control-d>' => sub { |
| 85 |
my $t = shift; |
| 86 |
$t->break if $t->compare('insert', '<', 'promptEnd'); |
| 87 |
}); |
| 88 |
$t->bind('<Control-k>' => sub { |
| 89 |
my $t = shift; |
| 90 |
$t->mark('set', 'insert', 'promptEnd') if $t->compare('insert', '<', 'promptEnd'); |
| 91 |
}); |
| 92 |
$t->bind('<Control-t>' => sub { |
| 93 |
my $t = shift; |
| 94 |
$t->break if $t->compare('insert', '<', 'promptEnd'); |
| 95 |
}); |
| 96 |
$t->bind('<Meta-d>' => sub { |
| 97 |
my $t = shift; |
| 98 |
$t->break if $t->compare('insert', '<', 'promptEnd'); |
| 99 |
}); |
| 100 |
$t->bind('<Meta-BackSpace>' => sub { |
| 101 |
my $t = shift; |
| 102 |
$t->break if $t->compare('insert', '<', 'promptEnd'); |
| 103 |
}); |
| 104 |
$t->bind('<Control-h>' => sub { |
| 105 |
my $t = shift; |
| 106 |
$t->break if $t->compare('insert', '<', 'promptEnd'); |
| 107 |
}); |
| 108 |
$t->bind('<Control-x>' => sub { |
| 109 |
my $t = shift; |
| 110 |
$t->tag('remove', 'sel', 'sel.first', 'promptEnd'); |
| 111 |
}); |
| 112 |
|
| 113 |
$t->tag('configure', 'bold', |
| 114 |
-font => "*-Courier-Bold-R-Normal-*-120-*-*-*-*-*-*", |
| 115 |
); |
| 116 |
$app = $MW->name; |
| 117 |
$MW->title("Tk Remote Controller - $app"); |
| 118 |
$MW->iconname($app); |
| 119 |
prompt; |
| 120 |
$t->focus(); |
| 121 |
|
| 122 |
MainLoop; |
| 123 |
|
| 124 |
sub prompt { |
| 125 |
|
| 126 |
# This procedure is used to print out a prompt at the insertion point |
| 127 |
# (which should be at the beginning of a line right now). |
| 128 |
|
| 129 |
$t->insert('insert', "$app: "); |
| 130 |
$t->mark('set', 'promptEnd', 'insert'); |
| 131 |
$t->mark('gravity', 'promptEnd', 'left'); |
| 132 |
$t->tag('add', 'bold', 'promptEnd linestart', 'promptEnd'); |
| 133 |
|
| 134 |
} # end prompt |
| 135 |
|
| 136 |
sub invoke { |
| 137 |
|
| 138 |
# The procedure below executes a command (it takes everything on the |
| 139 |
# current line after the prompt and either sends it to the remote |
| 140 |
# application or executes it locally, depending on "app". |
| 141 |
|
| 142 |
my $cmd = $t->get('promptEnd', 'insert'); |
| 143 |
my $result = ''; |
| 144 |
|
| 145 |
if($cmd eq "!!\n") { |
| 146 |
$cmd = $lastCommand; |
| 147 |
} else { |
| 148 |
$lastCommand = $cmd; |
| 149 |
} |
| 150 |
if($app eq "local") { |
| 151 |
eval $cmd; get_eval_status; |
| 152 |
} else { |
| 153 |
$t->send($app,$cmd); |
| 154 |
} |
| 155 |
prompt; |
| 156 |
$t->mark('set','promptEnd','insert'); |
| 157 |
$t->yview(-pickplace => 'insert'); |
| 158 |
|
| 159 |
} # end invoke |
| 160 |
|
| 161 |
sub newApp { |
| 162 |
|
| 163 |
# The following procedure is invoked to change the application that we're |
| 164 |
# talking to, or update the current prompt. |
| 165 |
|
| 166 |
my $appName = shift; |
| 167 |
$app = $appName; |
| 168 |
$t->mark('gravity', 'promptEnd', 'right'); |
| 169 |
$t->delete("promptEnd linestart", "promptEnd"); |
| 170 |
$t->insert("promptEnd", "$appName: "); |
| 171 |
$t->tag("add", "bold", "promptEnd linestart", "promptEnd"); |
| 172 |
$t->mark('gravity', 'promptEnd', 'left'); |
| 173 |
return ''; |
| 174 |
|
| 175 |
} # end newApp |
| 176 |
|
| 177 |
sub fillAppsMenu { |
| 178 |
|
| 179 |
# The procedure below will fill in the applications sub-menu with a list |
| 180 |
# of all the applications that currently exist. |
| 181 |
|
| 182 |
my $i; eval {$menu_file_m_apps->delete(0, 'last')}; |
| 183 |
foreach $i (sort $MW->interps) { |
| 184 |
$menu_file_m_apps->add("command", |
| 185 |
-label => $i, |
| 186 |
-command => [sub { &newApp($_[0]);},$i], |
| 187 |
); |
| 188 |
} |
| 189 |
$menu_file_m_apps->add("command", |
| 190 |
-label => "local", |
| 191 |
-command => sub { &newApp("local"); }, |
| 192 |
); |
| 193 |
|
| 194 |
} # end fillAppsMenu |
| 195 |
|
| 196 |
sub get_eval_status { |
| 197 |
|
| 198 |
# Inform user of any eval errors. |
| 199 |
|
| 200 |
chomp ($EVAL_ERROR, @_); |
| 201 |
my $errors = join '', $EVAL_ERROR, @_; |
| 202 |
$t->insert('insert',"$errors\n") if $errors; |
| 203 |
$EVAL_ERROR = ''; # prevent $t->break error for local app |
| 204 |
|
| 205 |
} # end get_eval_status |
| 206 |
|
| 207 |
sub Tk::Receive { |
| 208 |
|
| 209 |
# For security you must roll you own `receive' command, run with |
| 210 |
# taint checks on and untaint the received data. |
| 211 |
|
| 212 |
my($window, $cmd) = @_; |
| 213 |
|
| 214 |
chop $cmd; |
| 215 |
$cmd =~ /(.*)/; |
| 216 |
$cmd = $1; |
| 217 |
eval $cmd; get_eval_status; |
| 218 |
|
| 219 |
} # end receive |
| 220 |
|