From: Bryan B. <br...@bu...> - 2013-02-15 14:38:01
|
I've written an autoexpect.pl, well not really. Its a hack that runs the real autoexpect, then parses the resulting script and converts it to perl. Ugly I know, but it works ok. The one problem I have is that I would like to capture window size and bake it into the script, as some expecting seems to be affected by this. For example: when I run say: ./autoexpect.pl man man, do something like <space><space>1Gq, the resulting script hangs when you get to 1G because the pager is sending characters based on window size. I can $exp->slave()->clone_winsize_from(*STDIN) as long as its ran from the same window it was created. But I dont really know how to store STDIN size info, and reproduce it on a new Expect object called in a different environment. Anyway, here you go: --> #!/usr/bin/perl # # Autoexpect conversion tool (tcl => perl) # use strict; # Globals my $expect_script = "script.pl"; my $autoexpect_script = "script.exp"; # Values for the expect script being created my $log_file = undef; my $log_stdout = 1; # Usage statement if ( ! @ARGV ) { print "Usage: $0 [autoexpect flags] <program>\n"; exit(-1); } # Doesnt work without the real deal my $autoexpect = `which autoexpect`; chomp $autoexpect; if ( ! stat($autoexpect) ) { print "Error: you must have the real autoexpect for this to work\n"; exit(-1); } # Launch the real autoexpect my $return = system("$autoexpect -f $autoexpect_script @ARGV"); if ($return != 0 ) { print "Warning: autoexpect seems to have failed with code: $return\n"; } # Parse the resulting script, convert it to perl, and write that to file open(AUTOEXPECT, "$autoexpect_script") or die "Error: autoexpect script missing: $autoexpect_script\n"; open(PROG, ">$expect_script") or die "Error: could not open $expect_script for writing: $!\n"; print PROG "#!/usr/bin/perl use strict; use Expect; my \$exp = new Expect; \$exp->log_stdout($log_stdout); "; print PROG "\$exp->log_file(\"$log_file\", \"w\");\n" if $log_file; print PROG "\n"; my $last_match = 256; # max size of the expect match string while (<AUTOEXPECT>) { if ( m/^spawn (.*)$/ ) { my $program = $1; print PROG "\$exp->spawn(\"$program\");\n\n"; next; } if ( m/^sleep (.*)$/ ) { my $sleep = $1; print PROG "select(undef, undef, undef, $sleep);\n"; next; } if ( m/^send .* \"(.*)\"$/ ) { my $string = $1; print PROG "\$exp->clear_accum();\n"; print PROG "\$exp->send(\"$string\");\n"; next; } # Only matching the last line of the expect match string if ( m/^expect .exact \"(.*)\"$/ ) { my $string = $1; $string = substr($string, -$last_match) if length($string) > $last_match; print PROG "\$exp->expect(undef, \"$string\");\n"; next; } elsif ( m/^(.*)\"$/ ) { my $string = $1; $string = substr($string, -$last_match) if length($string) > $last_match; print PROG "\$exp->expect(undef, \"$string\");\n"; next; } } close(AUTOEXPECT); print PROG "\n"; print PROG "\$exp->hard_close();\n"; print PROG "\n"; close(PROG); <-- Bryan Bueter |