File: CuptInteractive.pm

package info (click to toggle)
cupt 2.10.4%2Bnmu2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,144 kB
  • sloc: cpp: 23,642; perl: 1,599; sh: 40; makefile: 19
file content (59 lines) | stat: -rw-r--r-- 943 bytes parent folder | download | duplicates (2)
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
package CuptInteractive;

use strict;
use warnings;

use Expect::Simple;

sub new {
	my ($class, $command, $prompt) = @_;
	my $o = {};
	$o->{_impl} = Expect::Simple->new({
		'Cmd' => $command,
		'Prompt' => $prompt//'cupt> ',
		'DisconnectCmd' => 'q',
		'Timeout' => '20',
	});
	$o->{_error} = '';
	my $self = bless($o, $class);

	$self->{_initial_output} = $self->_last_output();

	return $self;
}

sub initial_output {
	my $self = shift;
	return $self->{_initial_output};
}

sub execute {
	my ($self, $text) = @_;

	if ($self->{_error}) {
		return 'Previously: ' . $self->{_error};
	}

	eval {
		$self->{_impl}->send($text);
	};
	if ($@) {
		$self->{_error} = $self->{_impl}->before() . "\n\n" . $@;
		return $self->{_error};
	}

	my $result = $self->_last_output();
	$result =~ s/^\Q$text\E\n//;
	return $result;
}

sub _last_output {
	my $self = shift;

	my $result = $self->{_impl}->before();
	$result =~ s/\r//g;
	return $result;
}

1;