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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
#!/usr/bin/env perl
# You can use this test program to grill rlwrap - I wrote it in perl because I'm lazy
# TODO: use Term::ReadKey if available
use utf8; # so literals and identifiers can be in UTF-8
eval "require 5.012_001"; # or later to get "unicode_strings" feature
$@ and warn "Your perl version is rather antique; expect some problems.\n";
use strict; # quote strings, declare variables
use warnings; # on by default
use warnings qw(FATAL utf8); # fatalize encoding glitches
use open qw(:std :utf8); # undeclared streams in UTF-8
use charnames qw(:full :short); # unneeded in v5.16
use Getopt::Std;
eval "use Term::ReadKey";
my $have_ReadKey = not $@;
my $interrupt_char = "^C";
my $opt_d;
getopts('d:');
my $debug_file = $opt_d;
if ($debug_file) {
open DEBUG, ">$debug_file" or die "Couldn't not open $debug_file: $!\n";
}
use vars qw($prompting $prompt);
$|=1;
use POSIX qw(:termios_h :signal_h setsid);
my ($term, $oterm, $fd_stdin, $errorcode, $sigset_blocked);
my $verbose = 1;
$prompt = (my $original_prompt = "pid %p, type :h for help > ");
sub lprint($);
init();
lprint "\n\n";
help();
while(1) {
local $prompting = 1;
prompt();
$_ = <>;
defined $_ or exit 0;
/^:a(\s+(.*))/ and change_argv($2);
/^:b/ and run_bash();
/^:B/ and progress_bar();
/^:c\b/ and change_prompt();
/^:C/ and countdown();
/^:cd(\s+(.*))?/ and chdir($2 ? $2 : $ENV{HOME});
/^:d/ and die_eloquently();
/^:D/ and fork_child_and_then_die();
(/^:e\s*([+-]?\d+)?/) and exit ($1 || 0);
/^:f/ and do_fork();
/^:F/ and forget_last_newline();
/^:h/ and help();
/^:H/ and hanky_panky();
/^:i/ and toggle_interrupt_char();
/^:l/ and long_and_difficult_string();
/^:p/ and pass();
/^:P/ and print_chunky(scalar `cat $0`);
/^:r/ and reset_prompt();
/^:R/ and raw();
/^:s/ and segfault();
/^:t/ and trickle();
/^:S/ and trickle2();
/^:u/ and utf8();
/^:T/ and test_controlling_terminal();
/^:v/ and toggle_verbosity();
/^!!(.*)/ and perl($1);
/^!([^!].*)/ and shell($1);
/^:w/ and ridiculously_wide_prompt();
/^(:|!)/ or show_input($_);
}
########################### subs ################################################
sub init {
my (@signals_to_block);
$fd_stdin = fileno(STDIN);
# system ("reset");
$sigset_blocked = POSIX::SigSet->new;
$sigset_blocked -> fillset() or die "Could not fill \$sigset_blocked: $!\n";
$term = POSIX::Termios->new();
$term->getattr($fd_stdin);
$oterm = $term->getlflag();
install_signal_handlers();
}
sub help {
print <<EOF;
This program accepts input lines and reports what you typed. Input
lines that start with : or ! are special, and are useful to (stress-)test
rlwraps behaviour when the client does things like:
! <cmd> run <cmd> in shell
!!<exp> evaluate Perl expression <exp>
:a <new command line> change commandline
:b run ./bash or bash with current prompt (to compare readline behaviour with weird prompts)
:B show a progress bar using Unicode block elements
:c change prompt
:cd [<dir>] chdir to <dir> (or \$HOME)
:C countdown in prompt
:d die eloquently
:D fork, spawn a child and die (testing --skip-setctty).
:e [N] exit (with error code N)
:f fork and let child take over (parent waits)
:F forget last newline
:h help
:H hanky-panky with backspace and carriage return
:i toggle interrupt char between CTRL C and CTRL G
:l print a long and difficult text
:p ask "passsword"
:P print chunky
:r reset prompt
:R raw mode (char-at-a-time)
:s and sefgault
:S and trickle slowly
:t trickle output (10 chars)
:T test controlling terminal
:u try some utf-8
:v toggle verbosity (e.g. when receiving a signal)
:w ridiculously wide prompt
EOF
}
sub prompt {
my $sprompt = shift || $prompt;
return unless $prompt;
my $pid = $$;
$sprompt =~ s/%p/$pid/g;
$sprompt =~ s/%t/`tty`/eg;
chomp(my $pwd = `pwd`);
$pwd =~ s/^$ENV{HOME}/~/;
$sprompt =~ s/%d/$pwd/eg;
#$sprompt =~ s/\n//g;
lprint $sprompt;
}
sub change_argv {
my ($cmdline) = @_;
$0=$cmdline;
}
sub run_bash {
my $bashprompt = $prompt;
$bashprompt =~ s/\e(\[[\d;]*m)/\\[\\e$1\\]/g;
my $rcfile = "/tmp/bashprompt.$$";
open OUT, ">$rcfile";
print OUT "PS1=\"$bashprompt\"\n";
close OUT;
system "bash --rcfile $rcfile";
unlink $rcfile;
}
sub progress_bar {
for (my $i=0; $i < 200; $i++) {
#print "\x{1F642}"; # smiley - example of wide (multi-cell) character that will confuse rlwrap when in a prompt;
print "\x{2588}"; # "\N{FULL BLOCK}";
select(undef, undef, undef, 0.02);
}
print "\n";
}
sub pass {
noecho();
prompt (local $prompt = "Password: ");
my $input = <>;
show_input($input);
cooked();
}
sub do_fork {
my $pid;
return unless ($pid = fork);
waitpid($pid,0);
exit 0;
}
sub shell {
local $prompting;
my($command) = @_;
system($command);
cooked();
}
sub perl {
local $prompting;
my($exp) = @_;
my $result = eval $exp;
if ($@) {
print "error: $@\n";
} else {
print "OK, result = $result\n";
}
cooked();
}
sub trickle {
local $prompting;
my $i;
foreach my $c (split " ", ("trickle, trackle, trockle, " x 4) . ">") {
print "$c ";
print "\n" if ++$i % 2 == 0;
sleep 1;
}
my $input = <>;
show_input($input);
}
sub trickle2 {
local $prompting;
my $i;
foreach my $c (split //, "trickle, trackle > ") {
print $c;
sleep 1;
}
my $input = <>;
show_input($input);
}
sub hanky_panky {
for (my $i = 99; $i > 95 ; $i--) {
print "$i bottles of beer on the wall, $i bottles of beef";
sleep 1; print "\br";
sleep 1; print "\r";
}
print "\nYawn!\n"
}
sub toggle_interrupt_char {
$interrupt_char = $interrupt_char eq "^C" ? "^G" : "^C";
print "interrupt char is now '$interrupt_char'\n";
system "stty intr '$interrupt_char'";
}
sub countdown {
local $prompting;
for (my $i = 9; $i >= 0; $i--) {
print "\r countdown: $i >";
sleep 1;
}
my $input = <>;
show_input($input);
}
sub test_controlling_terminal {
if (not open DEVTTY, ">/dev/tty") {
print "I could not open /dev/tty, so there's no controlling terminal ($!)\n";
} else {
print DEVTTY "found controlling terminal: /dev/tty speaking here!\n";
}
}
sub fork_child_and_then_die {
my $pid;
if ($pid = fork) { # parent
select(undef, undef, undef, 0.1);
die "parent dying while child is still alive\n";
} else { # child
open DEVTTY, ">/dev/tty";
$SIG{'HUP'} = sub {print DEVTTY "Child received SIGHUP, goodbye!\n"; exit}; # parent may already have closed DEVTTY
for (my $i = 0; $i < 5; $i++) {
print DEVTTY "child at $i/5....\n";
sleep 1;
}
print DEVTTY "Child says goodbye!\n";
exit;
}
}
sub show_input {
my ($input) = @_;
defined $input or exit;
$input =~ s/\r?\n$//;
my $comment = "";
length $input or $comment = "(nothing)";
lprint "\nYou typed '$input' $comment\n";
}
sub change_prompt {
my $input;
my ($termwidth) = eval "GetTerminalSize";
{ local $prompt = "New prompt here > ";
my $redblah = red("blah");
lprint "\%p -> pid, \%t -> tty, %d -> pwd, red{blah} -> $redblah, \\n -newline, 4*x -> xxxx" .($have_ReadKey ? ", %w -> termwidth\n" : "\n");
prompt();
$input = <>;
$input =~ s/\r?\n$//;
$input =~ s/\\n/\n/g;
$input =~ s/\%w/$termwidth/ge;
$input =~ s/\((\d.*?)\)/eval($1)/ge;
$input =~ s/(\d+)\*([^ {}]+)/$2 x $1/ge;
$input =~ s/red\{(.*?)\}/red($1)/eg;
}
$prompt = $input;
}
sub segfault {
kill 'SEGV', $$;
}
sub red {
my ($text) = @_;
return colour($text,31);
}
sub blue {
my ($text) = @_;
return colour($text,34);
}
sub colour {
my ($text, $colourcode) = @_;
$text = "\e[1;${colourcode}m$text\e[0m" if $ENV{TERM} =~ /ansi|xterm|rxvt|cygwin|linux|screen|tmux/;
return $text;
}
sub long_and_difficult_string {
my $text = (red("hot") . " and ". blue("cold"). ", ") x 3000;
print "$text\n";
}
sub reset_prompt {
$prompt = $original_prompt;
}
sub ridiculously_wide_prompt {
$prompt = "Supercalifragilistic, " x 10; # 220
$prompt .= "Expialidocious > "; # + 17 = 237
}
sub utf8 {
$prompt = "Íslenska: ";
printf "Ég get etið gler án þess að meiða mig\n";
}
sub raw {
binmode(STDIN, ":raw");
cbreak();
my $key;
prompt (local $prompt = "Press Any Key >");
sysread(STDIN, $key, 1);
my $c = ord $key;
cooked();
lprint "\nYou typed a '$key' (ASCII $c)\n";
binmode(STDIN, ":utf8");
}
sub toggle_verbosity {
$verbose = not $verbose;
print ($verbose ? "verbose now\n" : "not verbose now\n");
}
sub forget_last_newline {
print "Some long\ntext, but without\na final newline";
sleep 20;
}
sub die_eloquently {
my $last_words = <<EOF;
Then, as the life ebbed out of you, you answered, O knight
Patroclus: "Hector, vaunt as you will, for Jove the son of Saturn
and Apollo have vouchsafed you victory; it is they who have vanquished
me so easily, and they who have stripped the armour from my shoulders;
had twenty such men as you attacked me, all of them would have
fallen before my spear. Fate and the son of Leto have overpowered
me, and among mortal men Euphorbus; you are yourself third only in the
killing of me. I say further, and lay my saying to your heart, you too
shall live but for a little season; death and the day of your doom are
close upon you, and they will lay you low by the hand of Achilles
son of Aeacus."
When he had thus spoken his eyes were closed in death, his soul left
his body and flitted down to the house of Hades, mourning its sad fate
and bidding farewell to the youth and vigor of its manhood.
EOF
$last_words =~ s/\s+/ /g;
print "\n$last_words\n";
exit 0;
}
sub print_chunky {
my ($text) = @_;
binmode STDOUT, ':raw';
local $| = undef;
while ($text) {
syswrite STDOUT, substr($text, 0, int(rand(20)), "");
select(undef, undef, undef, 0.01);
}
binmode STDOUT,':utf8';
}
sub handle_signal {
my ($sig) = @_;
my ($old_sigset);
sigprocmask(SIG_BLOCK, $sigset_blocked, $old_sigset) or die "Could not block signals: $!\n";
print ($sig =~ /INT/ ? "Caught $sig (type :e to exit)\n" : "Caught $sig\n") if $verbose;
prompt() if $prompting and $verbose;
sigprocmask(SIG_UNBLOCK, $old_sigset) or die "Could not unblock signals: $!\n";
}
sub install_signal_handlers {
foreach my $signal (keys %SIG) {
$SIG{$signal} = \&handle_signal unless ($signal =~ /CHLD|CLD|TSTP|NUM|_/);
}
}
sub cbreak {
$term->setlflag($oterm & ~(ECHO|ECHOK|ICANON));
$term->setcc(VTIME, 1);
$term->setattr($fd_stdin, TCSANOW);
}
sub cooked {
return unless defined $fd_stdin;
$term->setlflag($oterm);
$term->setcc(VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
}
sub noecho {
$term->setlflag($oterm & ~(ECHO));
$term->setcc(VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
}
sub END {
local $?; # because POSIX::Termios functions call system() and may thus reset $?
cooked();
lprint "\n";
}
sub lprint($) {
my ($text) = @_;
eval '"a" =~ /[[:^print:]]/'; # check whether this perl knows (negated) POSIX character class syntax (not before perl 5.6.0?)
if (! $@) {
$text =~ s/([^[:print:]\s\e])/sprintf("\\x%02x", (unpack "c", $1))/eg; # show unprintable characters in hex;
}
if ($debug_file) {
syswrite DEBUG, $text;
}
print $text;
}
# Local variables:
# mode:cperl
# End:
|