« lib/Devel/Trepan/Complete.pm
↑ Summary
blib/lib/Devel/Trepan/CmdProcessor/Command/Set_Subcmd/Highlight.pm »
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011, 2013 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | |||
| 4 | use warnings; no warnings 'redefine'; | ||
| 5 | |||
| 6 | use rlib '../../../../..'; | ||
| 7 | package Devel::Trepan::CmdProcessor::Command::SubsubcmdMgr; | ||
| 8 | no warnings 'redefine'; | ||
| 9 | |||
| 10 | use File::Basename; | ||
| 11 | use File::Spec; | ||
| 12 | |||
| 13 | ## FIXME core is not exporting properly. | ||
| 14 | use Devel::Trepan::CmdProcessor::Command::Subcmd::Core; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | ## FIXME: @SUBCMD_ISA and @SUBCMD_VARS should come from Core. | ||
| 18 | use vars qw(@ISA @EXPORT $HELP $NAME @ALIASES | ||
| 19 | @SUBCMD_ISA @SUBCMD_VARS); | ||
| 20 | @ISA = qw(Devel::Trepan::CmdProcessor::Command::Subcmd); | ||
| 21 | use vars @SUBCMD_VARS; # Value inherited from parent | ||
| 22 | |||
| 23 | use constant MIN_ARGS => 0; | ||
| 24 | use constant MAX_ARGS => undef; | ||
| 25 | use constant NEED_STACK => 0; | ||
| 26 | |||
| 27 | # attr_accessor :subcmds # Trepan::Subcmd | ||
| 28 | # attr_reader :name # Name of command | ||
| 29 | # attr_reader :last_args # Last arguments seen | ||
| 30 | |||
| 31 | # Initialize show subcommands. Note: instance variable name | ||
| 32 | # has to be setcmds ('set' + 'cmds') for subcommand completion | ||
| 33 | # to work. | ||
| 34 | sub new($$$) | ||
| 35 | { | ||
| 36 | my ($class, $parent, $name) = @_; | ||
| 37 | my @prefix = split('::', $class); | ||
| 38 | shift @prefix; shift @prefix; shift @prefix; shift @prefix; | ||
| 39 | my $self = { | ||
| 40 | subcmds => {}, | ||
| 41 | name => $name, | ||
| 42 | proc => $parent->{proc}, | ||
| 43 | parent => $parent, | ||
| 44 | prefix => \@prefix, | ||
| 45 | cmd_str => join(' ', map {lc $_} @prefix) | ||
| 46 | }; | ||
| 47 | # Initialization | ||
| 48 | my $parent_name = ucfirst $parent->{name}; | ||
| 49 | my $base_prefix="Devel::Trepan::CmdProcessor::Command::$parent_name"; | ||
| 50 | my $excluded_cmd_vars = {'$HELP' => 1, | ||
| 51 | '$NAME'=>2}; | ||
| 52 | for my $field (@Devel::Trepan::CmdProcessor::Command::Subcmd::SUBCMD_VARS) { | ||
| 53 | next if exists $excluded_cmd_vars->{$field} && | ||
| 54 | $excluded_cmd_vars->{$field} == 2; | ||
| 55 | my $sigil = substr($field, 0, 1); | ||
| 56 | my $new_field = index('$@', $sigil) >= 0 ? substr($field, 1) : $field; | ||
| 57 | if ($sigil eq '$') { | ||
| 58 | my $lc_field = lc $new_field; | ||
| 59 | $self->{$lc_field} = eval "\$${class}::${new_field}"; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | my @ary = eval "${class}::ALIASES()"; | ||
| 63 | $self->{aliases} = @ary ? [@ary] : []; | ||
| 64 | no strict 'refs'; | ||
| 65 | my $short_help = eval "${class}::SHORT_HELP()"; | ||
| 66 | $self->{short_help} = $short_help if $short_help; | ||
| 67 | bless $self, $class; | ||
| 68 | $self->load_debugger_subsubcommands($parent); | ||
| 69 | $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | # Create an instance of each of the debugger subcommands. Commands are | ||
| 73 | # found by importing files in the directory 'name' + '_Subcmd'. Some | ||
| 74 | # files are excluded via an array set in initialize. For each of the | ||
| 75 | # remaining files, we 'require' them and scan for class names inside | ||
| 76 | # those files and for each class name, we will create an instance of | ||
| 77 | # that class. The set of TrepanCommand class instances form set of | ||
| 78 | # possible debugger commands. | ||
| 79 | sub load_debugger_subsubcommands($$) | ||
| 80 | { | ||
| 81 | my ($self,$parent) = @_; | ||
| 82 | $self->{cmd_names} = (); | ||
| 83 | $self->{subcmd_names} = (); | ||
| 84 | $self->{cmd_basenames} = (); | ||
| 85 | my $cmd_dir = dirname(__FILE__); | ||
| 86 | my $parent_name = ucfirst $self->{name}; | ||
| 87 | my $cmd_name = $self->{prefix}[0]; | ||
| 88 | my @path = ($cmd_dir, '..', "${cmd_name}_Subcmd", | ||
| 89 | $parent_name . '_Subcmd'); | ||
| 90 | my $subcmd_dir = File::Spec->catfile(@path); | ||
| 91 | if (-d $subcmd_dir) { | ||
| 92 | my @files = glob(File::Spec->catfile($subcmd_dir, '*.pm')); | ||
| 93 | for my $pm (@files) { | ||
| 94 | my $basename = basename($pm, '.pm'); | ||
| 95 | my $item = sprintf("%s::%s::%s", | ||
| 96 | ucfirst($cmd_name), | ||
| 97 | ucfirst($parent_name), | ||
| 98 | ucfirst($basename)); | ||
| 99 | if (-d File::Spec->catfile(dirname($pm), $basename . '_Subcmd')) { | ||
| 100 | push @{$self->{subcmd_names}}, $item; | ||
| 101 | } else { | ||
| 102 | push @{$self->{cmd_names}}, $item; | ||
| 103 | push @{$self->{cmd_basenames}}, $basename; | ||
| 104 | } | ||
| 105 | my $rc=0; | ||
| 106 | eval{require "$pm"; $rc=1}; | ||
| 107 | if ($rc eq 'Skip me!') { | ||
| 108 | ; | ||
| 109 | } elsif ($rc) { | ||
| 110 | $self->setup_subsubcommand($parent, $item, $basename); | ||
| 111 | } else { | ||
| 112 | my $proc = $parent->{proc}; | ||
| 113 | $proc->errmsg("Trouble reading ${pm}:"); | ||
| 114 | $proc->errmsg($@); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub setup_subsubcommand($$$$) | ||
| 121 | { | ||
| 122 | my ($self, $parent, $cmd_prefix, $name) = @_; | ||
| 123 | my $parent_name = $parent->{name}; | ||
| 124 | my $cmd_obj; | ||
| 125 | my $cmd_name = lc $name; | ||
| 126 | my $new_cmd = "\$cmd_obj=Devel::Trepan::CmdProcessor::Command::" . | ||
| 127 | "${cmd_prefix}->new(\$self, '$cmd_name'); 1"; | ||
| 128 | if (eval $new_cmd) { | ||
| 129 | # Add to hash of commands, and list of subcmds | ||
| 130 | $self->{subcmds}{$cmd_name} = $cmd_obj; | ||
| 131 | $self->add($cmd_obj, $cmd_name); | ||
| 132 | } else { | ||
| 133 | my $proc = $parent->{proc}; | ||
| 134 | $proc->errmsg("Error instantiating ${cmd_prefix}"); | ||
| 135 | $proc->errmsg($@); | ||
| 136 | } | ||
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | # Find subcmd in self->subcmds | ||
| 141 | sub lookup($$;$) | ||
| 142 | { | ||
| 143 | my ($self, $subcmd_prefix, $use_regexp) = @_; | ||
| 144 | $use_regexp = 0 if scalar @_ < 3; | ||
| 145 | my $compare; | ||
| 146 | if (!$self->{proc}{settings}{abbrev}) { | ||
| 147 | $compare = sub($) { my $name = shift; $name eq $subcmd_prefix}; | ||
| 148 | } elsif ($use_regexp) { | ||
| 149 | $compare = sub($) { my $name = shift; $name =~ /^${subcmd_prefix}/}; | ||
| 150 | } else { | ||
| 151 | $compare = sub($) { | ||
| 152 | my $name = shift; 0 == index($name, $subcmd_prefix) | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | my @candidates = (); | ||
| 156 | while (my ($subcmd_name, $subcmd) = each %{$self->{subcmds}}) { | ||
| 157 | if ($compare->($subcmd_name)) { | ||
| 158 | push @candidates, $subcmd; | ||
| 159 | } | ||
| 160 | |||
| 161 | } | ||
| 162 | if (scalar @candidates == 1) { | ||
| 163 | return $candidates[0]; | ||
| 164 | } | ||
| 165 | return undef; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Show short help for a subcommand. | ||
| 169 | sub short_help($$$;$) | ||
| 170 | { | ||
| 171 | my ($self, $subcmd_cb, $subcmd_name, $label) = @_; | ||
| 172 | $label = 0 unless defined $label; | ||
| 173 | my $entry = $self->lookup($subcmd_name); | ||
| 174 | if ($entry) { | ||
| 175 | my $prefix = ''; | ||
| 176 | $prefix = $entry->{name} if $label; | ||
| 177 | if (exist $entry->{short_help}) { | ||
| 178 | $prefix .= ' -- ' if $prefix; | ||
| 179 | $self->{proc}->msg($prefix . $entry->{short_help}); | ||
| 180 | } | ||
| 181 | } else { | ||
| 182 | $self->{proc}->undefined_subcmd($self->{cmd_str}, $subcmd_name); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | # Add subcmd to the available subcommands for this object. | ||
| 187 | # It will have the supplied docstring, and subcmd_cb will be called | ||
| 188 | # when we want to run the command. min_len is the minimum length | ||
| 189 | # allowed to abbreviate the command. in_list indicates with the | ||
| 190 | # show command will be run when giving a list of all sub commands | ||
| 191 | # of this object. Some commands have long output like "show commands" | ||
| 192 | # so we might not want to show that. | ||
| 193 | sub add($$;$) | ||
| 194 | { | ||
| 195 | my ($self, $subcmd_cb, $subcmd_name) = @_; | ||
| 196 | $subcmd_name ||= $subcmd_cb->{name}; | ||
| 197 | |||
| 198 | # We keep a list of subcommands to assist command completion | ||
| 199 | push @{$self->{cmdlist}}, $subcmd_name; | ||
| 200 | } | ||
| 201 | |||
| 202 | # FIXME: remove this completely. | ||
| 203 | # help for subcommands | ||
| 204 | # Note: format of help is compatible with ddd. | ||
| 205 | sub help($$) | ||
| 206 | { | ||
| 207 | my ($self, $args) = @_; | ||
| 208 | if (scalar @$args <= 3) { | ||
| 209 | # "help cmd subcmd". Give the general help for the command part. | ||
| 210 | return $self->{help}; | ||
| 211 | } | ||
| 212 | |||
| 213 | my $subcmd_name = $args->[3]; | ||
| 214 | my @help_text = (); | ||
| 215 | my $subcmds_ref = $self->{subcmds}; | ||
| 216 | my @subcmds = $self->list(); | ||
| 217 | |||
| 218 | if ('*' eq $subcmd_name) { | ||
| 219 | @help_text = (sprintf("B<List of subcommands for command I<%s>:>", | ||
| 220 | $self->{cmd_str})); | ||
| 221 | |||
| 222 | my $subcmds = $self->{parent}->columnize_commands(\@subcmds); | ||
| 223 | chomp $subcmds; | ||
| 224 | push @help_text, $subcmds; | ||
| 225 | # Double carriage return because of perlpod | ||
| 226 | return join("\n\n", @help_text); | ||
| 227 | } | ||
| 228 | |||
| 229 | # "help cmd subcmd". Give help specific for that subcommand. | ||
| 230 | my $cmd = $self->lookup($subcmd_name, 0); | ||
| 231 | if (defined $cmd) { | ||
| 232 | if ($cmd->can("help")) { | ||
| 233 | return $cmd->help($args); | ||
| 234 | } else { | ||
| 235 | return $cmd->{help}; | ||
| 236 | } | ||
| 237 | } else { | ||
| 238 | my $proc = $self->{proc}; | ||
| 239 | my @matches = sort(grep /^${subcmd_name}/, @subcmds); | ||
| 240 | my $name = $self->{cmd_str}; | ||
| 241 | print "HI!\n"; | ||
| 242 | if (0 == scalar @matches) { | ||
| 243 | $proc->errmsg("No ${name} subcommands found matching /^{$subcmd_name}/. Try \"help\" $name."); | ||
| 244 | return undef; | ||
| 245 | } elsif (1 == scalar @matches) { | ||
| 246 | $args->[-1] = $matches[0]; | ||
| 247 | $self->help($args); | ||
| 248 | } else { | ||
| 249 | @help_text = ("Subcommands of \"$name\" matching /^${subcmd_name}/:"); | ||
| 250 | my @sort_matches = sort @matches; | ||
| 251 | push @help_text, $self->{parent}{cmd}->columnize_commands(\@sort_matches); | ||
| 252 | return @help_text; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | sub list($) { | ||
| 258 | my $self = shift; | ||
| 259 | sort keys %{$self->{subcmds}}; | ||
| 260 | } | ||
| 261 | |||
| 262 | # # Return an Array of subcommands that can start with +arg+. If none | ||
| 263 | # # found we just return +arg+. | ||
| 264 | # # FIXME: Not used any more? | ||
| 265 | # sub complete(prefix) | ||
| 266 | # Trepan::Complete.complete_token(@subcmds.subcmds.keys, prefix) | ||
| 267 | # } | ||
| 268 | |||
| 269 | sub complete_token_with_next($) | ||
| 270 | { | ||
| 271 | my ($self, $prefix) = @_; | ||
| 272 | my $subcmds = $self->{subcmds}; | ||
| 273 | return Devel::Trepan::Complete::complete_token_with_next($subcmds, | ||
| 274 | $prefix); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub run($$) | ||
| 278 | { | ||
| 279 | my ($self, $args) = @_; | ||
| 280 | $self->{last_args} = $args; | ||
| 281 | # require Enbugger; Enbugger->stop; | ||
| 282 | my $args_len = scalar @$args; | ||
| 283 | if ($args_len < 3 || $args_len == 3 && $args->[-1] eq '*') { | ||
| 284 | $self->{proc}->summary_list($self->{cmd_str}, $self->{subcmds}); | ||
| 285 | return 0; | ||
| 286 | } | ||
| 287 | |||
| 288 | my $subcmd_prefix = $args->[2]; | ||
| 289 | # We were given: cmd subcmd subcmd ... | ||
| 290 | # Run that. | ||
| 291 | my $subcmd = $self->lookup($subcmd_prefix); | ||
| 292 | if ($subcmd) { | ||
| 293 | if ($self->{proc}->ok_for_running($subcmd, $subcmd->{cmd_str}, | ||
| 294 | $args_len-3)) { | ||
| 295 | $subcmd->run($args); | ||
| 296 | } | ||
| 297 | } else { | ||
| 298 | $self->{proc}->undefined_subcmd($self->{name}, $subcmd_prefix); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | unless(caller) { | ||
| 303 | # Demo it. | ||
| 304 | require Devel::Trepan::CmdProcessor; | ||
| 305 | require Devel::Trepan::CmdProcessor::Command::Set; | ||
| 306 | my $proc = Devel::Trepan::CmdProcessor->new(undef, 'bogus'); | ||
| 307 | my $set_cmd = Devel::Trepan::CmdProcessor::Command::Set->new($proc, 'Set'); | ||
| 308 | my $mgr = __PACKAGE__->new($proc, $set_cmd); | ||
| 309 | print $mgr, "\n"; | ||
| 310 | print join(', ', %{$mgr->{subcmds}}), "\n"; | ||
| 311 | $set_cmd->lookup('a'); | ||
| 312 | } | ||
| 313 | |||
| 314 | 1; |