blib/lib/Devel/Trepan/Interface/Script.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 163 CC 10 SCAR 18.5
CC
10
Cov
0%
errmsg
20.0
new
12.0
read_command
12.0
CRAP 6.4
1 # -*- coding: utf-8 -*-
2 # This line is for testing purposes \
3 # Copyright (C) 2011-2012 Rocky Bernstein <rocky@cpan.org>
4  
5 # Module for reading debugger scripts
6  
7 use warnings; no warnings 'redefine';
8 use Exporter;
9 use IO::File;
10  
11 use rlib '../../..';
12  
13 package Devel::Trepan::Interface::Script;
14 our (@ISA);
15 use if !@ISA, Devel::Trepan::Interface;
16 use if !@ISA, Devel::Trepan::Interface::ComCodes;
17 use Devel::Trepan::IO::Input;
18 use Devel::Trepan::IO::StringArray;
19 use Devel::Trepan::Util qw(hash_merge);
20 use strict;
21 use vars qw(@EXPORT @ISA);
22 @ISA = qw(Devel::Trepan::Interface Exporter);
23  
24 use constant DEFAULT_OPTS => {
25 abort_on_error => 1,
26 confirm_val => 0,
27 verbose => 0
28 };
29
30 sub new
31 {
32 my ($class, $script_name, $out, $opts) = @_;
33 $opts = {} unless defined $opts;
34  
35 $opts = hash_merge($opts, DEFAULT_OPTS);
36  
37 my $self = {};
38 # FIXME if $script_name is invalid, we get undef $fh and then
39 # Interface->new uses STDIN.
40 my $fh = IO::File->new($script_name, 'r');
41 $self = Devel::Trepan::Interface->new($fh, $out, $opts);
42 $self->{script_name} = $script_name;
43 $self->{input_lineno} = 0;
44 $self->{buffer_output} = [];
45 unless ($opts->{verbose} or $out) {
46 $self->{output} = Devel::Trepan::IO::StringArrayOutput->new($self->{buffer_output});
47 }
48 bless $self, $class;
49 $self;
50 }
51  
52  
53 # Closes input only.
54 sub close($)
55 {
56 my $self = shift;
57 $self->{input}->close;
58 }
59  
60 # Called when a dangerous action is about to be done, to make
61 # sure it's okay.
62 #
63 # Could look also look for interactive input and
64 # use that. For now, though we'll simplify.
65 sub confirm($$$)
66 {
67 my ($self, $prompt, $default) = @_;
68 $self->{opts}{default_confirm};
69 }
70  
71 # Common routine for reporting debugger error messages.
72 #
73 sub errmsg($$;$)
74 {
75 my ($self, $msg, $prefix) = @_;
76 $prefix = '*** ' unless defined $prefix;
77 # self.verbose shows lines so we don't have to duplicate info
78 # here. Perhaps there should be a 'terse' mode to never show
79 # position info.
80 my $mess = sprintf "%s%s", $prefix, $msg;
81  
82 if ($self->{opts}{verbose}) {
83 my $location = sprintf("%s:%s: Error in source command file",
84 $self->{script_name},
85 $self->{input_lineno});
86 $mess = sprintf("%s:\n%s%s", $prefix, $location, $prefix, $msg);
87 }
88
89 $self->msg($mess);
90 # FIXME: should we just set a flag and report eof? to be more
91 # consistent with File and IO?
92 die if $self->{opts}{abort_on_error};
93 }
94  
95 sub msg($$)
96 {
97 my ($self, $msg) = @_;
98 ## FIXME: there must be a better way to do this...
99 if ($self->{output}->isa('Devel::Trepan::IO::TCPServer')) {
100 $self->{output}->writeline(PRINT . $msg);
101 } else {
102 $self->{output}->writeline($msg);
103 }
104 }
105  
106 sub is_interactive() { 0; }
107  
108 sub is_closed($)
109 {
110 my($self) = shift;
111 $self->{input}->eof;
112 }
113  
114 sub has_completion() { 0; }
115 sub has_term_readline($) { 0; }
116  
117 # Script interface to read a command. `prompt' is a parameter for
118 # compatibilty and is ignored.
119 sub read_command($;$)
120 {
121 my ($self, $prompt)=@_;
122 $prompt = '' unless defined $prompt;
123 $self->{input_lineno} += 1;
124 my $last = $self->readline();
125 my $line = '';
126 while ('\\' eq substr($last, -1)) {
127 $line .= substr($last, 0, -1) . "\n";
128 $last = $self->readline();
129 }
130 $line .= $last;
131  
132 if ($self->{opts}{verbose}) {
133 my $location = sprintf("%s line %s",
134 $self->{script_name},
135 $self->{input_lineno});
136 my $mess = sprintf '+ %s: %s', $location, $line;
137 $self->msg($mess);
138 }
139 # Do something with history?
140 return $line;
141 }
142  
143 # Script interface to read a line. `prompt' is a parameter for
144 # compatibilty and is ignored.
145 #
146 # Could decide make this look for interactive input?
147 sub readline($;$)
148 {
149 my ($self, $prompt) = @_;
150 $prompt = '' unless defined $prompt;
151 my $line = $self->{input}->getline;
152 chomp $line;
153 return $line;
154 }
155  
156 sub remove_history($;$)
157 {
158 }
159  
160 # sub DESTROY($)
161 # {
162 # my $self = shift;
163 # Devel::Trepan::Interface::DESTROY($self);
164 # }
165  
166 # Demo
167 unless (caller) {
168 my $intf = __PACKAGE__->new(__FILE__);
169 my $line = $intf->readline();
170 print "Line read: ${line}\n";
171 $line = $intf->read_command();
172 print "Second Line read: ${line}\n";
173 }
174  
175 1;