« blib/lib/Devel/Trepan/CmdProcessor/Command/Info_Subcmd/Line.pm
↑ Summary
lib/Devel/Trepan/Processor/Running.pm »
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011-2012 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | # Trepan command input validation routines. A String type is | ||
| 4 | # usually passed in as the argument to validation routines. | ||
| 5 | |||
| 6 | use strict; use warnings; | ||
| 7 | use Exporter; | ||
| 8 | |||
| 9 | use rlib '../../..'; | ||
| 10 | |||
| 11 | package Devel::Trepan::CmdProcessor; | ||
| 12 | |||
| 13 | use Cwd 'abs_path'; | ||
| 14 | use Devel::Trepan::DB::Breakpoint; | ||
| 15 | use Devel::Trepan::DB::LineCache; | ||
| 16 | no warnings 'redefine'; | ||
| 17 | |||
| 18 | # require_relative '../app/cmd_parse' | ||
| 19 | # require_relative '../app/condition' | ||
| 20 | # require_relative '../app/file' | ||
| 21 | # require_relative '../app/thread' | ||
| 22 | |||
| 23 | # require_relative 'location' # for resolve_file_with_dir | ||
| 24 | |||
| 25 | # attr_reader :file_exists_proc # Like File.exists? but checks using | ||
| 26 | # # cached files | ||
| 27 | |||
| 28 | # Check that arg is an Integer between opts->{min_value} and | ||
| 29 | # opts->{max_value} | ||
| 30 | sub get_an_int($$$) | ||
| 31 | { | ||
| 32 | my ($self, $arg, $opts) = @_; | ||
| 33 | $opts ||= {}; | ||
| 34 | my $ret_value = $self->get_int_noerr($arg); | ||
| 35 | if (! defined $ret_value) { | ||
| 36 | if ($opts->{msg_on_error}) { | ||
| 37 | $self->errmsg($opts->{msg_on_error}); | ||
| 38 | } else { | ||
| 39 | $self->errmsg("Expecting an integer, got: ${arg}."); | ||
| 40 | } | ||
| 41 | return undef; | ||
| 42 | } | ||
| 43 | if (defined($opts->{min_value}) and $ret_value < $opts->{min_value}) { | ||
| 44 | my $msg = sprintf("Expecting integer value to be at least %d; got %d.", | ||
| 45 | $opts->{min_value}, $ret_value); | ||
| 46 | $self->errmsg($msg); | ||
| 47 | return undef; | ||
| 48 | } elsif (defined($opts->{max_value}) and $ret_value > $opts->{max_value}) { | ||
| 49 | my $msg = sprintf("Expecting integer value to be at most %d; got %d.", | ||
| 50 | $opts->{max_value}, $ret_value); | ||
| 51 | $self->errmsg($msg); | ||
| 52 | return undef; | ||
| 53 | } | ||
| 54 | return $ret_value; | ||
| 55 | } | ||
| 56 | |||
| 57 | use constant DEFAULT_GET_INT_OPTS => { | ||
| 58 | min_value => 0, default => 1, cmdname => undef, max_value => undef | ||
| 59 | }; | ||
| 60 | use Devel::Trepan::Util qw(hash_merge); | ||
| 61 | |||
| 62 | # # If argument parameter 'arg' is not given, then use what is in | ||
| 63 | # # $opts->{default}. If String 'arg' evaluates to an integer between | ||
| 64 | # # least min_value and at_most, use that. Otherwise report an | ||
| 65 | # # error. If there's a stack frame use that for bindings in | ||
| 66 | # # evaluation. | ||
| 67 | # sub get_int($$;$) | ||
| 68 | # { | ||
| 69 | # my ($self, $arg, $opts)= @_; | ||
| 70 | # $opts ||={}; | ||
| 71 | |||
| 72 | # return $opts->{default} unless $arg; | ||
| 73 | # $opts = hash_merge($opts, DEFAULT_GET_INT_OPTS); | ||
| 74 | # my $val = $arg ? $self->get_int_noerr($arg) : $opts->{default}; | ||
| 75 | # unless ($val) { | ||
| 76 | # if ($opts->{cmdname}) { | ||
| 77 | # my $msg = sprintf("Command '%s' expects an integer; " + | ||
| 78 | # "got: %s.", $opts->{cmdname}, $arg); | ||
| 79 | # $self->errmsg($msg); | ||
| 80 | # } else { | ||
| 81 | # $self->errmsg('Expecting a positive integer, got: ${arg}'); | ||
| 82 | # } | ||
| 83 | # return undef; | ||
| 84 | # } | ||
| 85 | |||
| 86 | # if ($val < $opts->{min_value}) { | ||
| 87 | # if ($opts->{cmdname}) { | ||
| 88 | # my $msg = sprintf("Command '%s' expects an integer at least" . | ||
| 89 | # ' %d; got: %d.', | ||
| 90 | # $opts->{cmdname}, $opts->{min_value}, | ||
| 91 | # $opts->{default}); | ||
| 92 | # $self->errmsg($msg); | ||
| 93 | # } else { | ||
| 94 | # my $msg = sprintf("Expecting a positive integer at least" . | ||
| 95 | # ' %d; got: %d', | ||
| 96 | # $opts->{min_value}, $opts->{default}); | ||
| 97 | # $self->errmsg($msg); | ||
| 98 | # } | ||
| 99 | # return undef; | ||
| 100 | # elsif ($self->opts{max_value} and $val > $self->opts{max_value}) { | ||
| 101 | # if ($self->opts{cmdname}) { | ||
| 102 | # my $msg = sprintf("Command '%s' expects an integer at most" . | ||
| 103 | # ' %d; got: %d', $opts->{cmdname}, | ||
| 104 | # $opts->{max_value}, $val); | ||
| 105 | # $self->errmsg($msg); | ||
| 106 | # } | ||
| 107 | # } else { | ||
| 108 | # my $msg = sprintf("Expecting an integer at most %d; got: %d", | ||
| 109 | # $opts->{:max_value}, $val); | ||
| 110 | # $self->errmsg($msg); | ||
| 111 | # } | ||
| 112 | # return undef; | ||
| 113 | # } | ||
| 114 | # return $val | ||
| 115 | # } | ||
| 116 | |||
| 117 | sub get_int_list($$;$) | ||
| 118 | { | ||
| 119 | my ($self, $args, $opts) = @_; | ||
| 120 | $opts = {} unless defined $opts; | ||
| 121 | map {$self->get_an_int($_, $opts)} @{$args}; # .compact | ||
| 122 | } | ||
| 123 | |||
| 124 | # Eval arg and it is an integer return the value. Otherwise | ||
| 125 | # return undef; | ||
| 126 | sub get_int_noerr($$) | ||
| 127 | { | ||
| 128 | my ($self, $arg) = @_; | ||
| 129 | my $val = eval { | ||
| 130 | no warnings 'all'; | ||
| 131 | eval($arg); | ||
| 132 | }; | ||
| 133 | if (defined $val) { | ||
| 134 | return $val =~ /^[+-]?\d+$/ ? $val : undef; | ||
| 135 | } else { | ||
| 136 | return undef; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # Return true if arg is 'on' or 1 and false arg is 'off' or 0. | ||
| 141 | # Any other value is returns undef. | ||
| 142 | sub get_onoff($$;$$) | ||
| 143 | { | ||
| 144 | my ($self, $arg, $default, $print_error) = @_; | ||
| 145 | $print_error = 1 unless defined $print_error; | ||
| 146 | unless (defined $arg) { | ||
| 147 | unless (defined $default) { | ||
| 148 | if ($print_error) { | ||
| 149 | $self->errmsg("Expecting 'on', 1, 'off', or 0. Got nothing."); | ||
| 150 | return undef; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | return $default | ||
| 154 | } | ||
| 155 | my $darg = lc $arg; | ||
| 156 | return 1 if ($arg eq '1') || ($darg eq 'on'); | ||
| 157 | return 0 if ($arg eq '0') || ($darg eq'off'); | ||
| 158 | |||
| 159 | $self->errmsg("Expecting 'on', 1, 'off', or 0. Got: ${arg}.") if | ||
| 160 | $print_error; | ||
| 161 | return undef; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub is_method($$) | ||
| 165 | { | ||
| 166 | my ($self, $method_name) = @_; | ||
| 167 | my ($filename, $fn, $line_num) = DB::find_subline($method_name) ; | ||
| 168 | return !!$line_num; | ||
| 169 | } | ||
| 170 | |||
| 171 | # NOTE: this is slated to disappear | ||
| 172 | # parse: file line [rest...] | ||
| 173 | # line [rest..] | ||
| 174 | # fn [rest..] | ||
| 175 | # returns (filename, line_num, fn, rest) | ||
| 176 | # NOTE: Test for failure should only be on $line_num | ||
| 177 | sub parse_position($$;$) | ||
| 178 | { | ||
| 179 | my ($self, $args, $validate_line_num) = @_; | ||
| 180 | my @args = @$args; | ||
| 181 | my $size = scalar @args; | ||
| 182 | my $gobble_count = 0; | ||
| 183 | $validate_line_num = 0 unless defined $validate_line_num; | ||
| 184 | |||
| 185 | if (0 == $size) { | ||
| 186 | no warnings 'once'; | ||
| 187 | return ($DB::filename, $DB::line, undef, 0, ()); | ||
| 188 | } | ||
| 189 | my ($filename, $line_num, $fn); | ||
| 190 | my $first_arg = shift @args; | ||
| 191 | if ($first_arg =~ /^\d+$/) { | ||
| 192 | $line_num = $first_arg; | ||
| 193 | $filename = $DB::filename; | ||
| 194 | $gobble_count = 1; | ||
| 195 | $fn = undef; | ||
| 196 | } else { | ||
| 197 | ($filename, $fn, $line_num) = DB::find_subline($first_arg) ; | ||
| 198 | unless ($line_num) { | ||
| 199 | $filename = $first_arg; | ||
| 200 | my $mapped_filename = map_file($filename); | ||
| 201 | if (-r $mapped_filename) { | ||
| 202 | if (scalar @args == 0) { | ||
| 203 | $line_num = 1; | ||
| 204 | } else { | ||
| 205 | $line_num = shift @args; | ||
| 206 | } | ||
| 207 | unless ($line_num =~ /\d+/) { | ||
| 208 | $self->errmsg("Got filename $first_arg, " . | ||
| 209 | "expecting $line_num to a line number"); | ||
| 210 | return ($filename, undef, undef, 0, @args); | ||
| 211 | } | ||
| 212 | } else { | ||
| 213 | $self->errmsg("Expecting $first_arg to be a file " . | ||
| 214 | "or function name"); | ||
| 215 | return ($filename, undef, $fn, 0, @args); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | $gobble_count = 1; | ||
| 219 | } | ||
| 220 | if ($validate_line_num) { | ||
| 221 | local(*DB::dbline) = "::_<'$filename" ; | ||
| 222 | if (!defined($DB::dbline[$line_num]) || $DB::dbline[$line_num] == 0) { | ||
| 223 | $self->errmsg("Line $line_num of file $filename not a stopping line"); | ||
| 224 | return ($filename, undef, $fn, 0, @args); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | return ($filename, $line_num, $fn, $gobble_count, @args); | ||
| 228 | } | ||
| 229 | |||
| 230 | unless (caller) { | ||
| 231 | no strict; | ||
| 232 | require Devel::Trepan::DB; | ||
| 233 | my @onoff = qw(1 0 on off); | ||
| 234 | for my $val (@onoff) { | ||
| 235 | printf "onoff(${val}) = %s\n", get_onoff('bogus', $val); | ||
| 236 | } | ||
| 237 | |||
| 238 | for my $val (qw(1 1E bad 1+1 -5)) { | ||
| 239 | my $result = get_int_noerr('bogus', $val); | ||
| 240 | $result = '<undef>' unless defined $result; | ||
| 241 | print "get_int_noerr(${val}) = $result\n"; | ||
| 242 | } | ||
| 243 | |||
| 244 | no warnings 'redefine'; | ||
| 245 | require Devel::Trepan::CmdProcessor; | ||
| 246 | my $proc = Devel::Trepan::CmdProcessor::new(__PACKAGE__); | ||
| 247 | my @aref = $proc->get_int_list(['1+0', '3-1', '3']); | ||
| 248 | print join(', ', @aref), "\n"; | ||
| 249 | |||
| 250 | @aref = $proc->get_int_list(['a', '2', '3']); | ||
| 251 | print join(', ', @aref[1..2]), "\n"; | ||
| 252 | |||
| 253 | local @position = (); | ||
| 254 | sub print_position() { | ||
| 255 | my @call_values = caller(0); | ||
| 256 | for my $arg (@position) { | ||
| 257 | print defined($arg) ? $arg : 'undef'; | ||
| 258 | print "\n"; | ||
| 259 | } | ||
| 260 | print "\n"; | ||
| 261 | return @call_values; | ||
| 262 | } | ||
| 263 | my @call_values = foo(); | ||
| 264 | |||
| 265 | $DB::package = 'main'; | ||
| 266 | print_position; | ||
| 267 | # print '=' * 40 | ||
| 268 | # ['Array.map', 'Trepan::CmdProcessor.new', | ||
| 269 | # 'foo', 'cmdproc.errmsg'].each do |str| | ||
| 270 | # print "#{str} should be method: #{!!cmdproc.method?(str)}" | ||
| 271 | # } | ||
| 272 | # print '=' * 40 | ||
| 273 | |||
| 274 | # # FIXME: | ||
| 275 | # print "Trepan::CmdProcessor.allocate is: #{cmdproc.get_method('Trepan::CmdProcessor.allocate')}" | ||
| 276 | |||
| 277 | # ['food', '.errmsg'].each do |str| | ||
| 278 | # print "#{str} should be false: #{cmdproc.method?(str).to_s}" | ||
| 279 | # } | ||
| 280 | # print '-' * 20 | ||
| 281 | # p cmdproc.breakpoint_position('foo', true) | ||
| 282 | # p cmdproc.breakpoint_position('@0', true) | ||
| 283 | # p cmdproc.breakpoint_position("#{__LINE__}", true) | ||
| 284 | # p cmdproc.breakpoint_position("#{__FILE__} @0", false) | ||
| 285 | # p cmdproc.breakpoint_position("#{__FILE__}:#{__LINE__}", true) | ||
| 286 | # p cmdproc.breakpoint_position("#{__FILE__} #{__LINE__} if 1 == a", true) | ||
| 287 | # p cmdproc.breakpoint_position("cmdproc.errmsg", false) | ||
| 288 | # p cmdproc.breakpoint_position("cmdproc.errmsg:@0", false) | ||
| 289 | # ### p cmdproc.breakpoint_position(%w(2 if a > b)) | ||
| 290 | } | ||
| 291 | |||
| 292 | 1; |