| 1 | # -*- coding: utf-8 -*- | ||||||||||||||||||||||||||||
| 2 | # Copyright (C) 2011-2012, 2014 Rocky Bernstein <rocky@cpan.org> | ||||||||||||||||||||||||||||
| 3 | # Interface for client (i.e. user to communication-device) interaction. | ||||||||||||||||||||||||||||
| 4 | # The debugged program is at the other end of the communcation. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 39 | ▶ | use warnings; use utf8; no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 39 | ▶ | use rlib '../../..'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Interface for a user which is attached to a debugged process via | ||||||||||||||||||||||||||||
| 10 | # some sort of communication medium (e.g. socket, tty, FIFOs). This | ||||||||||||||||||||||||||||
| 11 | # could be on the same computer in a different process or on a remote | ||||||||||||||||||||||||||||
| 12 | # computer. | ||||||||||||||||||||||||||||
| 13 | package Devel::Trepan::Interface::Client; | ||||||||||||||||||||||||||||
| 14 | 39 | our (@ISA); | |||||||||||||||||||||||||||
| 15 | 39 | ▶ | use if !@ISA, Devel::Trepan::Interface; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 39 | ▶ | use if !@ISA, Devel::Trepan::Interface::ComCodes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 39 | ▶ | use if !@ISA, Devel::Trepan::Interface::User; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 39 | ▶ | use if !@ISA, Devel::Trepan::IO::Input; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 39 | ▶ | use Devel::Trepan::Util qw(hash_merge); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 39 | ▶ | use if !@ISA, Devel::Trepan::IO::TCPClient; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 39 | ▶ | use if !@ISA, Devel::Trepan::IO::FIFOClient; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 39 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | use constant HAVE_TTY => eval q(use Devel::Trepan::IO::TTYClient; 1) ? 1 : 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.9
Subroutine
BEGIN: called CC=1 SCAR=0.5
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 39 | @ISA = qw(Devel::Trepan::Interface Exporter); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 39 | ▶ | use constant DEFAULT_INIT_CONNECTION_OPTS => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | open => 1, | ||||||||||||||||||||||||||||
| 31 | client => ['tcp'] | ||||||||||||||||||||||||||||
| 32 | 39 | }; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub new | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 36 | 0 | my($class, $inp, $out, $user_opts, $connection_opts) = @_; | |||||||||||||||||||||||||||
| 37 | 0 | $connection_opts = hash_merge($connection_opts, | |||||||||||||||||||||||||||
| 38 | DEFAULT_INIT_CONNECTION_OPTS); | ||||||||||||||||||||||||||||
| 39 | 0 | my $client; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | unless (defined($inp)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | my $client_opts = $connection_opts->{'client'}; | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ('tty' eq $client_opts->[0] and HAVE_TTY) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/2
"tty" eq $client_opts->[0] and 0
MC/DC0%0/1
"tty" eq $client_opts->[0] and 0
"tty" eq $client_opts->[0]
Truth table0%0/2
A
A= "tty" eq $client_opts->[0]
| |||||||||||||||||||||||||||||
| 43 | 0 | my $tty_opts = { | |||||||||||||||||||||||||||
| 44 | inpty_name => $client_opts->[1], | ||||||||||||||||||||||||||||
| 45 | outpty_name => $client_opts->[2], | ||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | 0 | $client = Devel::Trepan::IO::TTYClient->new($tty_opts); | |||||||||||||||||||||||||||
| 48 | } elsif ('tcp' eq $client_opts->[0]) { | ||||||||||||||||||||||||||||
| 49 | 0 | $client = Devel::Trepan::IO::TCPClient->new($connection_opts); | |||||||||||||||||||||||||||
| 50 | } elsif ('fifo' eq $client_opts->[0]) { | ||||||||||||||||||||||||||||
| 51 | 0 | $client = Devel::Trepan::IO::FIFOClient->new($connection_opts); | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 0 | die "Unknown communication protocol: $client_opts->[0]"; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | my $self = { | |||||||||||||||||||||||||||
| 57 | output => $client, | ||||||||||||||||||||||||||||
| 58 | input => $client, | ||||||||||||||||||||||||||||
| 59 | user => Devel::Trepan::Interface::User->new($inp, $out, $user_opts) | ||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||
| 61 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 62 | 0 | return $self; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub is_closed($) | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_closed: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 69 | $self->{input}->is_closed | ||||||||||||||||||||||||||||
| 70 | 0 | } | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Called when a dangerous action is about to be done to make sure | ||||||||||||||||||||||||||||
| 73 | # it's okay. `prompt' is printed; user response is returned. | ||||||||||||||||||||||||||||
| 74 | # FIXME: make common routine for this and user.rb | ||||||||||||||||||||||||||||
| 75 | sub confirm($;$$) | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
confirm: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my ($self, $prompt, $default) = @_; | |||||||||||||||||||||||||||
| 78 | 0 | $self->{user}->confirm($prompt, $default); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub has_completion($) | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
has_completion: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | 0 | $self->{user}->has_completion; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub set_completion($$$) | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_completion: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 0 | my ($self, $completion_fn, $list_completion_fn) = @_; | |||||||||||||||||||||||||||
| 90 | 0 | $self->{user}->set_completion($completion_fn, $list_completion_fn); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub read_command($$) | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 0 | my ($self, $prompt) = @_; | |||||||||||||||||||||||||||
| 96 | 0 | $self->{user}->read_command($prompt); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Send a message back to the server (in contrast to the local user | ||||||||||||||||||||||||||||
| 100 | # output channel). | ||||||||||||||||||||||||||||
| 101 | sub read_remote | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_remote: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 104 | 0 | my $coded_line = undef; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | until ($coded_line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | $coded_line = $self->{input}->read_msg; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | my $control = substr($coded_line, 0, 1); | |||||||||||||||||||||||||||
| 109 | 0 | my $remote_line = substr($coded_line, 1); | |||||||||||||||||||||||||||
| 110 | 0 | return ($control, $remote_line); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Send a message back to the server (in contrast to the local user | ||||||||||||||||||||||||||||
| 114 | # output channel). | ||||||||||||||||||||||||||||
| 115 | sub write_remote($$$) | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
write_remote: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($self, $code, $msg) = @_; | |||||||||||||||||||||||||||
| 118 | # FIXME change into write_xxx | ||||||||||||||||||||||||||||
| 119 | 0 | $self->{output}->writeline($code . $msg); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Demo | ||||||||||||||||||||||||||||
| 123 | 39 | ▶ | unless (caller) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | print "HAVE_TTY: ", HAVE_TTY, "\n"; | |||||||||||||||||||||||||||
| 125 | 0 | my $intf = Devel::Trepan::Interface::Client->new(undef, undef, undef, undef, | |||||||||||||||||||||||||||
| 126 | {open => 0}); | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 39 | 1; | |||||||||||||||||||||||||||