« blib/lib/Devel/Trepan/CmdProcessor/Command/Show_Subcmd/Display_Subcmd/Eval.pm
↑ Summary
blib/lib/Devel/Trepan/CmdProcessor/Command/Subcmd/Core.pm »
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011-2013 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | # A base class for debugger subcommands. | ||
| 4 | # | ||
| 5 | use Exporter; | ||
| 6 | use warnings; | ||
| 7 | no warnings 'redefine'; | ||
| 8 | |||
| 9 | use rlib '../../../../..'; | ||
| 10 | use Devel::Trepan::CmdProcessor::Command::Subcmd::Core; | ||
| 11 | |||
| 12 | package Devel::Trepan::CmdProcessor::Command::Subsubcmd; | ||
| 13 | use Devel::Trepan::CmdProcessor::Validate; | ||
| 14 | |||
| 15 | BEGIN { | ||
| 16 | @SUBCMD_VARS = qw($HELP $IN_LIST $RUN_CMD $MIN_ABBREV | ||
| 17 | $NAME $SHORT_HELP @SUBCMD_VARS | ||
| 18 | @SUBCMD_ISA); | ||
| 19 | } | ||
| 20 | use strict; | ||
| 21 | |||
| 22 | my $NotImplementedMessage = | ||
| 23 | "This method must be overridden in a subsubclass"; | ||
| 24 | |||
| 25 | use vars qw(@SUBCMD_VARS @EXPORT @ISA @SUBCMD_ISA); | ||
| 26 | use vars @SUBCMD_VARS; | ||
| 27 | @ISA = qw(Exporter); | ||
| 28 | |||
| 29 | @SUBCMD_ISA = qw(Devel::Trepan::CmdProcessor::Command::Subsubcmd); | ||
| 30 | @EXPORT = @SUBCMD_VARS; | ||
| 31 | |||
| 32 | # attr_reader :name | ||
| 33 | |||
| 34 | $IN_LIST = 1; # Show item in help list of commands | ||
| 35 | $RUN_CMD = 1; # Run subcommand for those subcommands like "show" | ||
| 36 | # which append current settings to list output. | ||
| 37 | use constant MIN_ARGS => 0; | ||
| 38 | use constant MAX_ARGS => 0; | ||
| 39 | $MIN_ABBREV = 1; | ||
| 40 | use constant NEED_STACK => 0; | ||
| 41 | $NAME = 'your_command_name'; | ||
| 42 | |||
| 43 | |||
| 44 | # $cmd contains the command object that this | ||
| 45 | # command is invoked through. A debugger field gives access to | ||
| 46 | # the stack frame and I/O. | ||
| 47 | sub new($$$) | ||
| 48 | { | ||
| 49 | my ($class, $parent, $name) = @_; | ||
| 50 | my $self = {parent => $parent}; | ||
| 51 | |||
| 52 | # Convenience class access. We don't expect that any of these | ||
| 53 | # will change over the course of the program execution like | ||
| 54 | # errmsg(), msg(), and msg_nocr() might. (See the note below | ||
| 55 | # on these latter 3 methods.) | ||
| 56 | # | ||
| 57 | $self->{dbgr} = $parent->{dbgr}; | ||
| 58 | $self->{proc} = $parent->{proc}; | ||
| 59 | |||
| 60 | # FIXME: Inheritence of vars is not working the way I had hoped. | ||
| 61 | # So this is a workaround. | ||
| 62 | my $base_prefix="Devel::Trepan::CmdProcessor::Command::Subcmd::"; | ||
| 63 | for my $field (@SUBCMD_VARS) { | ||
| 64 | my $sigil = substr($field, 0, 1); | ||
| 65 | my $new_field = index('$@', $sigil) >= 0 ? substr($field, 1) : $field; | ||
| 66 | if ($sigil eq '$') { | ||
| 67 | $self->{lc $new_field} = | ||
| 68 | eval "\$${class}::${new_field} || \$${base_prefix}${new_field}"; | ||
| 69 | } elsif ($sigil eq '@') { | ||
| 70 | $self->{lc $new_field} = eval "[\@${class}::${new_field}]"; | ||
| 71 | } else { | ||
| 72 | die "Woah - bad sigil: $sigil"; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | # Done after above since $NAME is in @SUBCMD_VARS; | ||
| 76 | $self->{name} = $name; | ||
| 77 | $self->{short_help} ||= $self->{help}; | ||
| 78 | bless $self, $class; | ||
| 79 | $self->set_name_prefix($class); | ||
| 80 | $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | # Convenience short-hand for @proc.confirm | ||
| 84 | sub confirm($$;$) { | ||
| 85 | my ($self, $msg, $default) = @_; | ||
| 86 | return($self->{proc}->confirm($msg, $default)); | ||
| 87 | } | ||
| 88 | |||
| 89 | # Set a Boolean-valued debugger setting. | ||
| 90 | sub run_set_bool($$;$) | ||
| 91 | { | ||
| 92 | my ($self, $args, $default) = @_; | ||
| 93 | $default = 1 if scalar @_ < 3; | ||
| 94 | my $onoff_arg = @$args < 4 ? 'on' : $args->[3]; | ||
| 95 | my $key = $self->{subcmd_setting_key}; | ||
| 96 | $self->{proc}{settings}{$key} = $self->{proc}->get_onoff($onoff_arg); | ||
| 97 | $self->run_show_bool(); | ||
| 98 | } | ||
| 99 | |||
| 100 | # set an Integer-valued debugger setting. | ||
| 101 | sub run_set_int($$$;$$) | ||
| 102 | { | ||
| 103 | my ($self, $arg, $msg_on_error, $min_value, $max_value) = @_; | ||
| 104 | my $proc = $self->{proc}; | ||
| 105 | if ($arg =~/^\s*$/) { | ||
| 106 | $proc->errmsg('You need to supply a number.'); | ||
| 107 | return undef; | ||
| 108 | } | ||
| 109 | my $val = $proc->get_an_int($arg, | ||
| 110 | {max_value => $max_value, | ||
| 111 | min_value => $min_value, | ||
| 112 | msg_on_error => $msg_on_error | ||
| 113 | }); | ||
| 114 | if (defined ($val)) { | ||
| 115 | my $subcmd_setting_key = $self->{subcmd_setting_key}; | ||
| 116 | $proc->{settings}{$subcmd_setting_key} = $val; | ||
| 117 | $self->run_show_int(); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | # Generic subcommand showing a boolean-valued debugger setting. | ||
| 122 | sub run_show_bool($;$) | ||
| 123 | { | ||
| 124 | my ($self, $what) = @_; | ||
| 125 | my $proc = $self->{proc}; | ||
| 126 | my $key = $self->{subcmd_setting_key}; | ||
| 127 | my $val = $self->show_onoff($proc->{settings}{$key}); | ||
| 128 | $what = $self->{cmd_str} unless $what; | ||
| 129 | $proc->msg(sprintf "%s is %s.", $what, $val); | ||
| 130 | } | ||
| 131 | |||
| 132 | # Generic subcommand integer value display | ||
| 133 | sub run_show_int($;$) | ||
| 134 | { | ||
| 135 | my ($self, $what) = @_; | ||
| 136 | my $proc = $self->{proc}; | ||
| 137 | my $subcmd_setting_key = $self->{subcmd_setting_key}; | ||
| 138 | my $val = $proc->{settings}{$subcmd_setting_key}; | ||
| 139 | $what = $self->{cmd_str} unless ($what); | ||
| 140 | $proc->msg(sprintf "%s is %d.", $what, $val); | ||
| 141 | } | ||
| 142 | |||
| 143 | # Generic subcommand value display. Pass in a hash which may | ||
| 144 | # which optionally contain: | ||
| 145 | # | ||
| 146 | # :name - the String name of key in settings to use. If :value | ||
| 147 | # (described below) is set, then setting :name does | ||
| 148 | # nothing. | ||
| 149 | # | ||
| 150 | # :what - the String name of what we are showing. If none is | ||
| 151 | # given, then we use the part of the SHORT_HELP string. | ||
| 152 | # | ||
| 153 | # :value - a String value associated with "what" above. If none | ||
| 154 | # is given, then we pick up the value from settings. | ||
| 155 | # | ||
| 156 | sub run_show_val($;$) | ||
| 157 | { | ||
| 158 | my ($self, $opts) = @_; | ||
| 159 | $opts ||= {}; | ||
| 160 | my $what = exists $opts->{what} ? $opts->{what} : $self->{string_in_show}; | ||
| 161 | my $name = exists $opts->{name} ? $opts->{name} : $self->{name}; | ||
| 162 | my $val = exists $opts->{value} ? $opts->{value} : $self->{settings}{$name}; | ||
| 163 | my $msg = sprintf("%s is %s.", $what, $val); | ||
| 164 | $self->msg($msg); | ||
| 165 | } | ||
| 166 | |||
| 167 | # sub save_command_from_settings | ||
| 168 | # ["${subcmd_prefix_string} ${settings[subcmd_setting_key]}"] | ||
| 169 | # } | ||
| 170 | |||
| 171 | sub subcmd_prefix_string($) | ||
| 172 | { | ||
| 173 | my $self = shift; | ||
| 174 | join(' ', $self->{prefix}); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub subcmd_setting_key($) | ||
| 178 | { | ||
| 179 | my $self = shift; | ||
| 180 | return $self->{subcmd_setting_key} if $self->{subcmd_setting_key}; | ||
| 181 | my @prefix = @{$self->{prefix}}; shift @prefix; | ||
| 182 | $self->{subcmd_setting_key} = join('', @prefix); | ||
| 183 | } | ||
| 184 | |||
| 185 | # Return 'on' for true and 'off' for false, and ?? for anything else. | ||
| 186 | sub show_onoff($$) | ||
| 187 | { | ||
| 188 | my ($self, $bool) = @_; | ||
| 189 | if (!defined($bool)) { | ||
| 190 | return 'unset'; | ||
| 191 | } elsif ($bool) { | ||
| 192 | return 'on'; | ||
| 193 | } else { | ||
| 194 | return 'off' | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | sub set_name_prefix($$) | ||
| 199 | { | ||
| 200 | my ($self, $class) = @_; | ||
| 201 | my @prefix = split(/::/, $class); | ||
| 202 | splice(@prefix, 0, 4); # Remove Devel::Trepan::CmdProcessor::Command | ||
| 203 | @prefix = map {lc $_} @prefix; | ||
| 204 | $self->{prefix} = \@prefix; | ||
| 205 | $self->{cmd_str} = join(' ', @prefix); | ||
| 206 | $self->{subcmd_setting_key} = "$prefix[1]$prefix[2]"; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub string_in_show($) | ||
| 210 | { | ||
| 211 | my ($self, $bool) = @_; | ||
| 212 | my $skip_len = length('Show '); | ||
| 213 | ucfirst substr($self->{short_help}, $skip_len); | ||
| 214 | } | ||
| 215 | |||
| 216 | sub summary_help($$) | ||
| 217 | { | ||
| 218 | my ($self, $subcmd_name) = @_; | ||
| 219 | my $msg = sprintf("%-12s: %s", $subcmd_name, $self->{short_help}); | ||
| 220 | $self->msg_nocr($msg); | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | package Devel::Trepan::CmdProcessor::Command::SetBoolSubsubcmd; | ||
| 225 | use vars qw(@ISA); | ||
| 226 | @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subsubcmd); | ||
| 227 | |||
| 228 | our $MIN_ARGS = 0; | ||
| 229 | our $MAX_ARGS = 1; | ||
| 230 | |||
| 231 | sub complete($$) | ||
| 232 | { | ||
| 233 | my ($self, $prefix) = @_; | ||
| 234 | Devel::Trepan::Complete::complete_token(['on', 'off'], | ||
| 235 | $prefix); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub run($$) { | ||
| 239 | my ($self, $args) = @_; | ||
| 240 | $self->run_set_bool($args); | ||
| 241 | } | ||
| 242 | |||
| 243 | sub save_command($) { | ||
| 244 | my ($self) = @_; | ||
| 245 | my %settings = $self->{settings}; | ||
| 246 | my $val = $settings{$self->subcmd_setting_key()} ? 'on' : 'off'; | ||
| 247 | [$self->subcmd_prefix_string . " ${val}"]; | ||
| 248 | } | ||
| 249 | |||
| 250 | package Devel::Trepan::CmdProcessor::Command::ShowBoolSubsubcmd; | ||
| 251 | use vars qw(@ISA); | ||
| 252 | @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subsubcmd); | ||
| 253 | |||
| 254 | sub run($) | ||
| 255 | { | ||
| 256 | my ($self, $args) = @_; | ||
| 257 | $self->run_show_bool($self->string_in_show()); | ||
| 258 | } | ||
| 259 | |||
| 260 | package Devel::Trepan::CmdProcessor::Command::ShowIntSubsubcmd; | ||
| 261 | use vars qw(@ISA); | ||
| 262 | @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subsubcmd); | ||
| 263 | |||
| 264 | sub run($) { | ||
| 265 | my ($self, $args) = @_; | ||
| 266 | my $doc = $self->{short_help}; | ||
| 267 | my $len = length($doc) - 5; | ||
| 268 | $doc = ucfirst substr($doc, 5, $len); | ||
| 269 | $self->run_show_int($doc); | ||
| 270 | } | ||
| 271 | |||
| 272 | unless (caller) { | ||
| 273 | # Demo it. | ||
| 274 | # require Devel::Trepan::CmdProcessor::Mock; | ||
| 275 | # my $proc = Devel::Trepan::CmdProcessor::Mock::setup(); | ||
| 276 | # my %cmds = %{$proc->{commands}}; | ||
| 277 | # print join(', ', keys %cmds), "\n"; | ||
| 278 | # my $subcmd = | ||
| 279 | # Devel::Trepan::CmdProcessor::Command::Subcmd->new($cmds{'quit'}); | ||
| 280 | # print join(', ', keys %{$subcmd->{settings}}), "\n"; | ||
| 281 | # print $subcmd->show_onoff($subcmd->{settings}{autoeval}), "\n"; | ||
| 282 | # $subcmd->run_set_int($proc, 'Just a test'); | ||
| 283 | } | ||
| 284 | |||
| 285 | 1; |