Metabrik/Core/Shell.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Metabrik/Core/Log.pm ↑ Summary
1 #
2 # $Id$
3 #
4 # core::shell Brik
5 #
6 package Metabrik::Core::Shell;
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Breaking.Feature.Fix
11 our $VERSION = '1.41';
12 our $FIX = '0';
13  
14 2 ▶use base qw(Term::Shell Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 use IO::All;
17  
18 sub brik_properties {
19 return {
20 revision => '$Revision$',
21 tags => [ qw(main core) ],
22 attributes => {
23 echo => [ qw(0|1) ],
24 help_show_base_attributes => [ qw(0|1) ],
25 help_show_base_commands => [ qw(0|1) ],
26 help_show_base_all => [ qw(0|1) ], # Both Attributes and Commands
27 help_show_inherited_attributes => [ qw(0|1) ],
28 help_show_inherited_commands => [ qw(0|1) ],
29 help_show_inherited_all => [ qw(0|1) ], # Both Attributes and Commands
30 help_show_all => [ qw(0|1) ], # Both Attributes and Commands for base and inherited
31 comp_show_base_attributes => [ qw(0|1) ],
32 comp_show_base_commands => [ qw(0|1) ],
33 comp_show_base_all => [ qw(0|1) ],
34 comp_show_inherited_attributes => [ qw(0|1) ],
35 comp_show_inherited_commands => [ qw(0|1) ],
36 comp_show_inherited_all => [ qw(0|1) ], # Both Attributes and Commands
37 comp_show_all => [ qw(0|1) ], # Both Attributes and Commands for base and inherited
38 show_base_attributes => [ qw(0|1) ],
39 show_base_commands => [ qw(0|1) ],
40 show_base_all => [ qw(0|1) ],
41 show_inherited_attributes => [ qw(0|1) ],
42 show_inherited_commands => [ qw(0|1) ],
43 show_inherited_all => [ qw(0|1) ],
44 show_all => [ qw(0|1) ], # Both Attributes and Commands for base and inherited
45 aliases_completion => [ qw(0|1) ], # Complete aliases to show original Command
46 ps1 => [ qw(prompt) ],
47 capture_mode => [ qw(0|1) ],
48 # These are used by Term::Shell
49 #path_home => [ qw(directory) ],
50 #path_cwd => [ qw(directory) ],
51 #prompt => [ qw(string) ],
52 #_aliases => [ qw(INTERNAL) ],
53 #_executables => [ qw(INTERNAL) ],
54 },
55 attributes_default => {
56 echo => 1,
57 help_show_base_attributes => 0,
58 help_show_base_commands => 0,
59 help_show_base_all => 0,
60 help_show_inherited_attributes => 0,
61 help_show_inherited_commands => 0,
62 help_show_inherited_all => 0,
63 help_show_all => 0,
64 comp_show_base_attributes => 0,
65 comp_show_base_commands => 0,
66 comp_show_base_all => 0,
67 comp_show_inherited_attributes => 0,
68 comp_show_inherited_commands => 0,
69 comp_show_inherited_all => 0,
70 comp_show_all => 0,
71 show_base_attributes => 0,
72 show_base_commands => 0,
73 show_base_all => 0,
74 show_inherited_attributes => 0,
75 show_inherited_commands => 0,
76 show_inherited_all => 0,
77 show_all => 0,
78 aliases_completion => 0,
79 #capture_mode => 1,
80 capture_mode => 0, # Do not capture by default.
81 # It makes users uncertain of what is happening
82 # when executing any external command.
83 },
84 commands => {
85 splash => [ ],
86 pwd => [ ],
87 full_pwd => [ ],
88 get_available_help => [ ],
89 get_help_attributes => [ qw(Brik) ],
90 get_help_commands => [ qw(Brik) ],
91 get_comp_attributes => [ qw(Brik) ],
92 get_comp_commands => [ qw(Brik) ],
93 # Term::Shell stuff
94 cmd => [ qw(Cmd) ],
95 cmdloop => [ ],
96 run_use => [ qw(Brik) ],
97 run_help => [ qw(Brik) ],
98 run_set => [ qw(Brik Attribute Value) ],
99 run_get => [ qw(Brik) ],
100 run_run => [ qw(Brik Command) ],
101 run_alias => [ qw(alias Cmd) ],
102 run_cd => [ qw(directory) ],
103 run_code => [ qw(Code) ],
104 run_exit => [ ],
105 },
106 require_modules => {
107 'Data::Dump' => [ qw(dump) ],
108 'File::HomeDir' => [ ],
109 'Cwd' => [ ],
110 'PPI' => [ ],
111 },
112 };
113 }
114  
115 sub new {
116 # Call Term::Shell new()
117 my $self = shift->SUPER::new(@_);
118  
119 # Call Metabrik new()
120 $self->Metabrik::new(@_);
121  
122 # We have to set of default_attributes again normally called by Brik::new():
123 # Otherwise default attributes are not set properly because of Perl inheritance scheme
124 $self->brik_set_default_attributes;
125  
126 # Now write Term::Shell default values we gave, like context, global, log, ...
127 my %h = @_;
128 for my $k (keys %h) {
129 $self->{$k} = $h{$k};
130 }
131  
132 return $self;
133 }
134  
135 sub brik_init {
136 my $self = shift;
137  
138 # Allow user to break out of multiline mode or run Commands
139 # Note: Gnu readline() is blocking SIGs, we have to hit enter so
140 # Ctrl+C is executed.
141 $SIG{INT} = sub {
142 $self->log->debug("SIGINT: captured for pid[$$] ".
143 ($$ == $self->global->pid ? '(main process)' : '')
144 );
145 $self->_update_prompt;
146 if ($self->global->exit_on_sigint) {
147 $self->run_exit;
148 }
149 return 1;
150 };
151  
152 # Gather executable files from PATH
153 my @path = split(':', ($ENV{PATH} || ''));
154 my %executables = ();
155 for my $path (@path) {
156 my @files = ();
157 eval {
158 @files = io($path)->all_files;
159 };
160 if ($@) {
161 chomp($@);
162 $self->log->debug("brik_init: $path: all_files: $@");
163 next;
164 };
165 for my $file (@files) {
166 if ($file->is_executable) {
167 my $filename = $file->filename;
168 $executables{$filename}++;
169  
170 # Without a handler, we would not be able to autoload the run Command
171 $self->add_handler("run_$filename");
172 }
173 }
174 }
175  
176 $self->{_executables} = \%executables;
177  
178 return $self->SUPER::brik_init(@_);
179 }
180  
181 sub splash {
182 my $self = shift;
183  
184 # No context, no splash screen cause nothing to show.
185 if (! defined($self->context)) {
186 return 1;
187 }
188  
189 my $con = $self->context;
190  
191 my $version = $con->run('core::global', 'brik_version');
192  
193 my $available_count = keys %{$con->available};
194 my $used_count = keys %{$con->used};
195  
196 # ASCII art courtesy: http://patorjk.com/software/taag/#p=testall&f=Graffiti&t=MetabriK
197 print<<EOF
198  
199 ███▄ ▄███▓▓█████▄▄▄█████▓ ▄▄▄ ▄▄▄▄ ██▀███ ██▓ ██ ▄█▀
200 ▓██▒▀█▀ ██▒▓█ ▀▓ ██▒ ▓▒▒████▄ ▓█████▄ ▓██ ▒ ██▒▓██▒ ██▄█▒
201 ▓██ ▓██░▒███ ▒ ▓██░ ▒░▒██ ▀█▄ ▒██▒ ▄██▓██ ░▄█ ▒▒██▒▓███▄░
202 ▒██ ▒██ ▒▓█ ▄░ ▓██▓ ░ ░██▄▄▄▄██ ▒██░█▀ ▒██▀▀█▄ ░██░▓██ █▄
203 ▒██▒ ░██▒░▒████▒ ▒██▒ ░ ▓█ ▓██▒░▓█ ▀█▓░██▓ ▒██▒░██░▒██▒ █▄
204 ░ ▒░ ░ ░░░ ▒░ ░ ▒ ░░ ▒▒ ▓▒█░░▒▓███▀▒░ ▒▓ ░▒▓░░▓ ▒ ▒▒ ▓▒
205 ░ ░ ░ ░ ░ ░ ░ ▒ ▒▒ ░▒░▒ ░ ░▒ ░ ▒░ ▒ ░░ ░▒ ▒░
206 ░ ░ ░ ░ ░ ▒ ░ ░ ░░ ░ ▒ ░░ ░░ ░
207 ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
208 ░
209  
210 --[ Welcome to Metabrik - Knowledge is in your head, Detail is in the code ]--
211 --[ Briks available: $available_count ]--
212 --[ Briks used: $used_count ]--
213 --[ Version $version ]--
214  
215 There is a Brik for that.
216  
217 EOF
218 ;
219  
220 return 1;
221 }
222  
223 sub pwd {
224 my $self = shift;
225  
226 return $self->{path_cwd};
227 }
228  
229 sub full_pwd {
230 my $self = shift;
231  
232 my $global = $self->global;
233  
234 my $pwd = $self->pwd;
235 my $homedir = $global->homedir;
236  
237 $pwd =~ s{^~}{$homedir};
238  
239 return $pwd;
240 }
241  
242 sub get_available_help {
243 my $self = shift;
244  
245 if (! defined($self->context)) {
246 return $self->log->error("get_available_help: no core::context Brik");
247 }
248  
249 my @used = sort { $a cmp $b } keys %{$self->context->used};
250 my @aliases = sort { $a cmp $b } keys %{$self->{_aliases}};
251 my @commands = sort { $a cmp $b } keys %{$self->brik_commands};
252  
253 # Skip class functions
254 @commands = grep (!/^brik_/, @commands);
255  
256 # Remove leading run_ string
257 for (@aliases, @commands) {
258 s/^run_//;
259 }
260  
261 return { briks => \@used, aliases => \@aliases, commands => \@commands };
262 }
263  
264 #
265 # Term::Shell stuff
266 #
267 our $AUTOLOAD;
268  
269 sub AUTOLOAD {
270 my $self = shift;
271 my (@args) = @_;
272  
273 #$self->log->debug("autoload[$AUTOLOAD]");
274  
275 if ($AUTOLOAD !~ /^Metabrik::Core::Shell::run_/) {
276 return 1;
277 }
278  
279 (my $command = $AUTOLOAD) =~ s/^Metabrik::Core::Shell:://;
280  
281 #$self->log->debug("AUTOLOAD: command[$command] args[@args]");
282  
283 #my $aliases = $self->_aliases;
284 my $aliases = $self->{_aliases};
285 if (exists($aliases->{$command})) {
286 my $cmd = $aliases->{$command};
287 return $self->cmd(join(' ', $cmd, @args));
288 }
289  
290 if (! defined($self->context)) {
291 return $self->log->error("AUTOLOAD: no core::context Brik");
292 }
293  
294 my $context = $self->context;
295  
296 if ($context->is_used('shell::command')) {
297 my $sc_command = $self->capture_mode ? 'capture' : 'system';
298 (my $exec = $command) =~ s/^run_//;
299 my $executables = $self->{_executables};
300 if (exists($executables->{$exec})) {
301 my $cmd = "run shell::command $sc_command $exec";
302 return $self->cmd(join(' ', $cmd, @args));
303 }
304 }
305 else {
306 $self->log->verbose("AUTOLOAD: Brik [shell::command] not loaded, skipping");
307 }
308  
309 return 1;
310 }
311  
312 #sub _word_may_be_brik {
313 # my $self = shift;
314 # my ($word) = @_;
315 #
316 # my $context = $self->context;
317 # my $used = $context->used;
318 #
319 # for (keys %$used) {
320 # return $used if /^$word/;
321 # }
322 #
323 # return 0;
324 #}
325  
326 # We overwrite inherited Term::Shell rl_complete() sub to have control
327 sub rl_complete {
328 my $self = shift;
329 my ($word, $line, $start) = @_;
330  
331 my @comp = ();
332  
333 # If it's a command, complete 'run_'.
334 # For that, command must not be blank and not start with '/'.
335 if (($start == 0 || substr($line, 0, $start) =~ /^\s*$/)
336 && ($word !~ m{/})) {
337 $self->log->debug("rl_complete: word[$word] start[$start] line[$line]");
338 @comp = $self->complete('', $word, $line, $start);
339  
340 $self->log->debug("rl_complete: comp[@comp]");
341  
342 # XXX: broken feature since Term::ReadLine::Gnu 1.27
343 # If we found something and it's an alias, we complete with the original command
344 #if (defined($comp[0]) && $self->comp_aliases && exists($self->{_aliases}{"run_".$comp[0]})) {
345 #$self->log->debug("rl_complete: original[".$self->{_aliases}{"run_$comp[0]"}."]");
346  
347 #my @words = split(/\s+/, $self->{_aliases}{"run_$comp[0]"});
348 #if (exists($self->{_aliases}{"run_$words[0]"})) {
349 #@comp = $self->{_aliases}{"run_".$words[0]};
350 #}
351 #else {
352 #@comp = $self->{_aliases}{"run_$comp[0]"};
353 #}
354 #}
355 ## Or maybe it's a Brik, and we want to prefix it automagically with 'run' Command
356 #elsif (my $available = $self->_word_may_be_brik($word)) {
357 #for (keys %$available) {
358 #push @comp, "run $_" if /^$word/;
359 #}
360 #}
361 }
362 # If it's a subcommand, send it to any custom completion function for the
363 # function:
364 else {
365 my $command = ($self->line_parsed($line))[0];
366 $self->log->debug("rl_complete: send to custom completion");
367 @comp = $self->complete($command, $word, $line, $start);
368 }
369  
370 $self->log->debug("rl_complete: return comp[@comp] count[".scalar(@comp)."]");
371  
372 return @comp;
373 }
374  
375 # Converts Windows path
376 sub _convert_path {
377 my ($path) = @_;
378  
379 $path =~ s/\\/\//g;
380  
381 return $path;
382 }
383  
384 #
385 # Term::Shell::main stuff
386 #
387 use Cwd;
388 use File::HomeDir;
389  
390 sub _update_path_home {
391 my $self = shift;
392  
393 #$self->path_home(_convert_path(home()));
394 $self->{path_home} = _convert_path(File::HomeDir->my_home || '/tmp');
395  
396 return 1;
397 }
398  
399 sub _update_path_cwd {
400 my $self = shift;
401  
402 my $cwd = _convert_path(Cwd::getcwd() || '/tmp');
403 #$self->log->debug("cwd [$cwd]");
404 #my $home = $self->path_home;
405 my $home = $self->{path_home} || '/tmp';
406 #$self->log->debug("home [$home]");
407 $cwd =~ s/^$home/~/;
408  
409 #$self->path_cwd($cwd);
410 $self->{path_cwd} = $cwd;
411  
412 return 1;
413 }
414  
415 sub _update_prompt {
416 my $self = shift;
417 my ($prompt) = @_;
418  
419 if (defined($prompt)) {
420 #$self->prompt($prompt);
421 $self->{prompt} = $prompt;
422 }
423 else {
424 my $ps1 = $self->ps1;
425 #my $cwd = $self->path_cwd;
426 my $cwd = $self->{path_cwd};
427  
428 my $prompt = defined($ps1) ? "$ps1:$cwd> " : "Meta:$cwd> ";
429  
430 if ($^O =~ /win32/i) {
431 $prompt =~ s/> /\$ /;
432 }
433 elsif ($< == 0) {
434 $prompt =~ s/> /# /;
435 }
436  
437 #$self->prompt($prompt);
438 $self->{prompt} = $prompt;
439 }
440  
441 return 1;
442 }
443  
444 sub init {
445 my $self = shift;
446  
447 $|++;
448  
449 $self->_update_path_home;
450 $self->_update_path_cwd;
451 $self->_update_prompt;
452  
453 # Default: 'us,ue,md,me', see `man 5 termcap' and Term::Cap
454 # See also Term::ReadLine LoadTermCap() and ornaments() subs.
455 $self->term->ornaments('md,me');
456  
457 # Force Commands to be entered entirely to avoid ambiguity.
458 # Example: type 'my' will result in excuting Perl code, and not 'mymeta-cpanfile'.
459 $self->{API}{match_uniq} = 0;
460  
461 return $self;
462 }
463  
464 sub prompt_str {
465 my $self = shift;
466  
467 #return $self->prompt;
468 return $self->{prompt};
469 }
470  
471 sub cmd_is_complete {
472 my $self = shift;
473 my ($lines) = @_;
474  
475 my $string = join("\n", @$lines);
476  
477 my $document = PPI::Document->new(\$string);
478 if (! $document) {
479 return $self->log->error("cmd_is_complete: cannot parse Perl string");
480 }
481  
482 # Courtesy of Perl::Shell complete() function
483 my $r = $document->find_any(sub {
484 $_[1]->isa('PPI::Structure') and ! $_[1]->finish
485 });
486  
487 return $r ? 0 : 1;
488 }
489  
490 sub cmd_to_code {
491 my $self = shift;
492 my ($line) = @_;
493  
494 # Example: a run Command converted to Perl Code:
495 # 'run shell::command system "echo $_"';
496 # $SHE->cmd("run shell::command system \"echo $_\"");
497  
498 $self->log->debug("cmd_to_code: before: [$line]");
499 if ($line =~ /^\s*'\s*((?:use|set|get|run)\s.*?)\s*'\s*;?\s*$/) {
500 # We have to escape " chars
501 (my $new = $1) =~ s{"}{\\"}g;
502 # We have to escape $ variables too so they are completed
503 $new =~ s{\$}{\\\$}g;
504 $self->log->debug("cmd_to_code: new: [$new]");
505 $line = '$SHE->cmd("'.$new.'");';
506 }
507 $self->log->debug("cmd_to_code: after: [$line]");
508  
509 return $line;
510 }
511  
512 sub cmd {
513 my $self = shift;
514 my ($cmd) = @_;
515  
516 # If there is a command like `exit 1', we exit and return the code number
517 if ($cmd =~ m{^\s*exit(?:\s+(\d+))}) {
518 $self->log->verbose("cmd: exiting [$cmd]");
519 $self->run_exit;
520 exit($1);
521 }
522  
523 return $self->SUPER::cmd($cmd);
524 }
525  
526 sub process_line {
527 my $self = shift;
528 my ($line, $lines) = @_;
529  
530 $self->log->debug("process_line: [$line]");
531  
532 # Skip comments
533 if ($line =~ /^\s*#/) {
534 return 0;
535 }
536 # Skip blank lines
537 if ($line =~ /^\s*$/) {
538 return 0;
539 }
540  
541 push @$lines, $line;
542  
543 # If a closure is open, we are in multiline mode
544 if (! $self->cmd_is_complete($lines)) {
545 # If it looks like a Metabrik command, we rewrite it to a Perl code string
546 # This is to support 'run <Brik> <Command> <Args>' within multiline.
547 $lines->[-1] = $self->cmd_to_code($line);
548 $self->_update_prompt('.. ');
549 return 1;
550 }
551  
552 $self->log->debug("process_line: lines[@$lines]");
553  
554 my $cmd = join('', @$lines);
555  
556 $self->cmd($cmd);
557  
558 $self->_update_prompt;
559  
560 return 0;
561 }
562  
563 sub cmdloop {
564 my $self = shift;
565 my ($lines) = @_;
566  
567 my @lines = ();
568  
569 # User provided lines to execute (script)
570 if (defined($lines)) {
571 for my $line (@$lines) {
572 if ($self->process_line($line, \@lines)) {
573 next; # We are in multiline mode
574 }
575 else {
576 @lines = (); # Command is complete, we ran it and now reset line buffer.
577 }
578  
579 last if $self->{stop};
580 }
581 }
582 # Or we are in interactive mode (shell)
583 else {
584 $self->{stop} = 0;
585 $self->preloop;
586  
587 while (defined(my $line = $self->readline($self->prompt_str))) {
588 if ($self->process_line($line, \@lines)) {
589 next; # We are in multiline mode
590 }
591 else {
592 @lines = (); # Command is complete, we ran it and now reset line buffer.
593 }
594  
595 last if $self->{stop};
596 }
597  
598 $self->run_exit;
599  
600 return $self->postloop;
601 }
602  
603 return 1;
604 }
605  
606 #
607 # Term::Shell::run stuff
608 #
609 sub run_exit {
610 my $self = shift;
611  
612 my $context = $self->context;
613  
614 if ($context->is_used('shell::history')) {
615 $context->run('shell::history', 'write');
616 }
617  
618 # Global clean-up
619 $context->brik_fini;
620  
621 return $self->stoploop;
622 }
623  
624 sub comp_exit {
625 my $self = shift;
626  
627 $self->log->debug("comp_exit: true");
628  
629 return ();
630 }
631  
632 sub run_alias {
633 my $self = shift;
634 my ($alias, @cmd) = @_;
635  
636 #my $aliases = $self->_aliases;
637 my $aliases = $self->{_aliases};
638  
639 if (! defined($alias)) {
640 for my $this (sort { $a cmp $b } keys %$aliases) {
641 ($alias = $this) =~ s/^run_//;
642 printf("alias %-10s \"%s\"\n", $alias, $aliases->{$this});
643 }
644  
645 return 1;
646 }
647 elsif (length($alias) && @cmd == 0) {
648 $alias =~ s/^run_//;
649 if (exists($aliases->{"run_$alias"})) {
650 printf("alias %-10s \"%s\"\n", $alias, $aliases->{"run_$alias"});
651 }
652 else {
653 $self->log->info("alias: no alias by that name [$alias]");
654 }
655  
656 return 1;
657 }
658  
659 $aliases->{"run_$alias"} = join(' ', @cmd);
660 #$self->_aliases($aliases);
661 $self->{_aliases} = $aliases;
662  
663 $self->add_handler("run_$alias");
664  
665 return 1;
666 }
667  
668 sub comp_alias {
669 my $self = shift;
670  
671 $self->log->debug("comp_alias: true");
672  
673 return ();
674 }
675  
676 sub run_cd {
677 my $self = shift;
678 my ($dir, @args) = @_;
679  
680 if (defined($dir)) {
681 if ($dir =~ m{^~}) {
682 #$dir = $self->path_home;
683 $dir =~ s{^~}{$self->{path_home}};
684 }
685 if (! -d $dir) {
686 return $self->log->error("cd: directory [$dir] does not exist");
687 }
688 chdir($dir)
689 or return $self->log->error("cd: chdir failed for directory [$dir]: $!");
690 $self->_update_path_cwd;
691 }
692 else {
693 #chdir($self->path_home);
694 chdir($self->{path_home})
695 or return $self->log->error("cd: chdir failed for directory [$dir]: $!");
696 $self->_update_path_cwd;
697 }
698  
699 $self->_update_prompt;
700  
701 return 1;
702 }
703  
704 sub comp_cd {
705 my $self = shift;
706 my ($word, $line, $start) = @_;
707  
708 $self->log->debug("comp_cd: true");
709  
710 return $self->catch_comp_sub($word, $start, $line);
711 }
712  
713 sub run_code {
714 my $self = shift;
715  
716 my $context = $self->context;
717  
718 my $line = $self->line;
719 $line =~ s/^code\s+//;
720  
721 if (! length($line)) {
722 return $self->log->info('code <code>');
723 }
724  
725 $self->log->debug("run_code: code[$line]");
726  
727 my $r;
728 eval {
729 # So we can interrupt the do($line) execution
730 local $SIG{INT} = sub {
731 $self->log->debug("run_code: SIG received");
732 if ($self->global->exit_on_sigint) {
733 $self->log->debug("run_code: exiting");
734 $self->run_exit;
735 }
736 die("interrupted by user\n");
737 };
738 $r = $context->do($line);
739 };
740 if (! defined($r)) {
741 return $self->log->error("run_code: unable to execute Code [$line]");
742 }
743  
744 if ($self->echo) {
745 $self->page(Data::Dump::dump($r)."\n");
746 }
747  
748 return $r;
749 }
750  
751 sub comp_code {
752 my $self = shift;
753 my ($word, $line, $start) = @_;
754  
755 $self->log->debug("comp_code: true");
756  
757 return $self->catch_comp_sub($word, $start, $line);
758 }
759  
760 sub run_use {
761 my $self = shift;
762 my ($brik, @args) = @_;
763  
764 my $context = $self->context;
765  
766 if (! defined($brik)) {
767 return $self->log->info('use <brik>');
768 }
769  
770 my $r;
771 # If Brik starts with a minuscule, we want to use Brik in Metabrik sens.
772 # Otherwise, it is a use command in the Perl sens.
773 if ($brik =~ /^[a-z]/ && $brik =~ /::/) {
774 $r = $context->use($brik) or return;
775 if ($r) {
776 $self->log->verbose("use: Brik [$brik] success");
777 }
778 }
779 else {
780 return $self->run_code($brik, @args);
781 }
782  
783 return $r;
784 }
785  
786 sub comp_use {
787 my $self = shift;
788 my ($word, $line, $start) = @_;
789  
790 $self->log->debug("comp_use: true");
791  
792 my $context = $self->context;
793  
794 my @words = $self->line_parsed($line);
795 my $count = scalar(@words);
796  
797 $self->log->debug("word[$word] line[$line] start[$start] count[$count]");
798  
799 my @comp = ();
800  
801 # We want to find available Briks by using completion
802 if (($count == 1)
803 || ($count == 2 && length($word) > 0)) {
804 my $available = $context->available;
805 if ($self->log->level > 2 && ! defined($available)) {
806 $self->log->debug("\ncomp_use: can't fetch available Briks");
807 return ();
808 }
809  
810 for my $a (keys %$available) {
811 push @comp, $a if $a =~ /^$word/;
812 }
813 }
814  
815 return @comp;
816 }
817  
818 sub get_help_attributes {
819 my $self = shift;
820 my ($brik) = @_;
821  
822 if (! defined($brik)) {
823 return $self->log->error($self->brik_help_run('get_help_attributes'));
824 }
825  
826 my $context = $self->context;
827  
828 if (! $context->is_used($brik)) {
829 return {};
830 }
831  
832 my $used = $context->used;
833  
834 my $attributes = $used->{$brik}->brik_own_attributes;
835  
836 my $base_attributes = {};
837 if ($self->help_show_base_attributes || $self->help_show_base_all
838 || $self->show_base_attributes || $self->show_base_all
839 || $self->help_show_all || $self->show_all) {
840 $base_attributes = $used->{$brik}->brik_base_attributes;
841 }
842  
843 my $inherited_attributes = {};
844 if ($self->help_show_inherited_attributes || $self->help_show_inherited_all
845 || $self->show_inherited_attributes || $self->show_inherited_all
846 || $self->help_show_all || $self->show_all) {
847 $inherited_attributes = $used->{$brik}->brik_inherited_attributes;
848 }
849  
850 for my $attribute (keys %$base_attributes) {
851 $attributes->{$attribute} = $base_attributes->{$attribute};
852 }
853  
854 for my $attribute (keys %$inherited_attributes) {
855 $attributes->{$attribute} = $inherited_attributes->{$attribute};
856 }
857  
858 return $attributes;
859 }
860  
861 sub get_help_commands {
862 my $self = shift;
863 my ($brik) = @_;
864  
865 if (! defined($brik)) {
866 return $self->log->error($self->brik_help_run('get_help_commands'));
867 }
868  
869 my $context = $self->context;
870  
871 if (! $context->is_used($brik)) {
872 return {};
873 }
874  
875 my $used = $context->used;
876  
877 my $commands = $used->{$brik}->brik_own_commands;
878  
879 my $base_commands = {};
880 if ($self->help_show_base_commands || $self->help_show_base_all
881 || $self->show_base_commands || $self->show_base_all
882 || $self->help_show_all || $self->show_all) {
883 $base_commands = $used->{$brik}->brik_base_commands;
884 }
885  
886 my $inherited_commands = {};
887 if ($self->help_show_inherited_commands || $self->help_show_inherited_all
888 || $self->show_inherited_commands || $self->show_inherited_all
889 || $self->help_show_all || $self->show_all) {
890 $inherited_commands = $used->{$brik}->brik_inherited_commands;
891 }
892  
893 for my $command (keys %$base_commands) {
894 $commands->{$command} = $base_commands->{$command};
895 }
896  
897 for my $command (keys %$inherited_commands) {
898 $commands->{$command} = $inherited_commands->{$command};
899 }
900  
901 return $commands;
902 }
903  
904 sub get_comp_attributes {
905 my $self = shift;
906 my ($brik) = @_;
907  
908 if (! defined($brik)) {
909 return $self->log->error($self->brik_help_run('get_comp_attributes'));
910 }
911  
912 my $context = $self->context;
913  
914 if (! $context->is_used($brik)) {
915 return {};
916 }
917  
918 my $used = $context->used;
919  
920 my $attributes = $used->{$brik}->brik_own_attributes;
921  
922 my $base_attributes = {};
923 if ($self->comp_show_base_attributes || $self->comp_show_base_all
924 || $self->show_base_attributes || $self->show_base_all
925 || $self->comp_show_all || $self->show_all) {
926 $base_attributes = $used->{$brik}->brik_base_attributes;
927 }
928  
929 my $inherited_attributes = {};
930 if ($self->comp_show_inherited_attributes || $self->comp_show_inherited_all
931 || $self->show_inherited_attributes || $self->show_inherited_all
932 || $self->comp_show_all || $self->show_all) {
933 $inherited_attributes = $used->{$brik}->brik_inherited_attributes;
934 }
935  
936 for my $attribute (keys %$base_attributes) {
937 $attributes->{$attribute} = $base_attributes->{$attribute};
938 }
939  
940 for my $attribute (keys %$inherited_attributes) {
941 $attributes->{$attribute} = $inherited_attributes->{$attribute};
942 }
943  
944 return $attributes;
945 }
946  
947 sub get_comp_commands {
948 my $self = shift;
949 my ($brik) = @_;
950  
951 if (! defined($brik)) {
952 return $self->log->error($self->brik_help_run('get_comp_commands'));
953 }
954  
955 my $context = $self->context;
956  
957 if (! $context->is_used($brik)) {
958 return {};
959 }
960  
961 my $used = $context->used;
962  
963 my $commands = $used->{$brik}->brik_own_commands;
964  
965 my $base_commands = {};
966 if ($self->comp_show_base_commands || $self->comp_show_base_all
967 || $self->show_base_commands || $self->show_base_all
968 || $self->comp_show_all || $self->show_all) {
969 $base_commands = $used->{$brik}->brik_base_commands;
970 }
971  
972 my $inherited_commands = {};
973 if ($self->comp_show_inherited_commands || $self->comp_show_inherited_all
974 || $self->show_inherited_commands || $self->show_inherited_all
975 || $self->comp_show_all || $self->show_all) {
976 $inherited_commands = $used->{$brik}->brik_inherited_commands;
977 }
978  
979 for my $command (keys %$base_commands) {
980 $commands->{$command} = $base_commands->{$command};
981 }
982  
983 for my $command (keys %$inherited_commands) {
984 $commands->{$command} = $inherited_commands->{$command};
985 }
986  
987 return $commands;
988 }
989  
990 sub run_help {
991 my $self = shift;
992 my ($arg1, $arg2) = @_;
993  
994 my $context = $self->context;
995  
996 my $help = $self->get_available_help;
997 my %aliases = map { $_ => 1 } @{$help->{aliases}};
998 my %briks = map { $_ => 1 } @{$help->{briks}};
999 my %commands = map { $_ => 1 } @{$help->{commands}};
1000  
1001 # Print the list of available Briks, Aliases, Commands
1002 if (! defined($arg1)) {
1003 $self->log->info("For more help, print help <Command>:");
1004 $self->log->info(" ");
1005  
1006 for my $this (sort { $a cmp $b } @{$help->{briks}}) {
1007 $self->log->info(" $this - Brik");
1008 }
1009  
1010 for my $this (sort { $a cmp $b } @{$help->{aliases}}) {
1011 $self->log->info(" $this - Alias");
1012 }
1013  
1014 for my $this (sort { $a cmp $b } @{$help->{commands}}) {
1015 $self->log->info(" $this - Command");
1016 }
1017  
1018 return 1;
1019 }
1020 elsif (! defined($arg2)) {
1021 # Help for a Brik
1022 if ($context->is_used($arg1)) {
1023 my $used_brik = $context->used->{$arg1};
1024  
1025 my $attributes = $self->get_help_attributes($arg1);
1026 my $commands = $self->get_help_commands($arg1);
1027  
1028 for my $attribute (sort { $a cmp $b } keys %$attributes) {
1029 my $help = "set $arg1 ".$used_brik->brik_help_set($attribute);
1030 $self->log->info($help) if defined($help);
1031 }
1032  
1033 for my $command (sort { $a cmp $b } keys %$commands) {
1034 my $help = "run $arg1 ".$used_brik->brik_help_run($command);
1035 $self->log->info($help) if defined($help);
1036 }
1037 }
1038 # Help for a Command
1039 elsif (exists($commands{$arg1})) {
1040 for my $this (sort { $a cmp $b } keys %commands) {
1041 return $self->log->info("$arg1 - core::shell Command, see 'help core::shell $arg1'");
1042 }
1043 }
1044 # Help for an Alias
1045 elsif (exists($aliases{$arg1})) {
1046 return $self->log->info("$arg1 - no help for Aliases");
1047 }
1048 else {
1049 return $self->log->info("Command [$arg1] not found");
1050 }
1051 }
1052 # Both arg1 and arg2 are defined
1053 else {
1054 if (exists($briks{$arg1})) {
1055 my $used_brik = $context->used->{$arg1};
1056  
1057 my $attributes = $used_brik->brik_attributes;
1058 my $commands = $used_brik->brik_commands;
1059  
1060 my $base_attributes = $used_brik->brik_base_attributes;
1061 my $base_commands = $used_brik->brik_base_commands;
1062  
1063 my $help;
1064 if (exists($attributes->{$arg2}) || exists($base_attributes->{$arg2})) {
1065 $help = $used_brik->brik_help_set($arg2);
1066 }
1067 elsif (exists($commands->{$arg2}) || exists($base_commands->{$arg2})) {
1068 $help = $used_brik->brik_help_run($arg2);
1069 }
1070 else {
1071 $help = "Attribute or Command [$arg2] not found for Brik [$arg1]";
1072 }
1073  
1074 return $self->log->info($help);
1075 }
1076 else {
1077 return $self->log->info("Command [$arg1] not found");
1078 }
1079 }
1080  
1081 return 1;
1082 }
1083  
1084 sub comp_help {
1085 my $self = shift;
1086 my ($word, $line, $start) = @_;
1087  
1088 $self->log->debug("comp_help: true");
1089  
1090 my @words = $self->line_parsed($line);
1091 my $count = scalar(@words);
1092  
1093 $self->log->debug("word[$word] line[$line] start[$start] count[$count]");
1094  
1095 my @comp = ();
1096  
1097 # We want to find help for used Briks/Aliases/Commands by using completion
1098 if ($count == 1 || ($count == 2 && length($word) > 0)) {
1099 my $help = $self->get_available_help;
1100 # No need to complete aliases, there is no help
1101 #for my $a (@{$help->{briks}}, @{$help->{aliases}}, @{$help->{commands}}) {
1102 for my $a (@{$help->{briks}}, @{$help->{commands}}) {
1103 next unless length($a);
1104 push @comp, $a if $a =~ /^$word/;
1105 }
1106 }
1107 # We want to complete entered Command and Attributes
1108 else {
1109 push @comp, $self->comp_run(@_);
1110 push @comp, $self->comp_set(@_);
1111 return @comp;
1112 }
1113  
1114 return @comp;
1115 }
1116  
1117 sub run_set {
1118 my $self = shift;
1119 my ($brik, $attribute, $value) = @_;
1120  
1121 my $context = $self->context;
1122  
1123 if (! defined($brik) || ! defined($attribute) || ! defined($value)) {
1124 return $self->log->info("set <brik> <attribute> <value>");
1125 }
1126  
1127 my $r = $context->set($brik, $attribute, $value);
1128 if (! defined($r)) {
1129 return $self->log->error("set: unable to set Attribute [$attribute] for Brik [$brik]");
1130 }
1131  
1132 return $r;
1133 }
1134  
1135 sub comp_set {
1136 my $self = shift;
1137 my ($word, $line, $start) = @_;
1138  
1139 $self->log->debug("comp_set: true");
1140  
1141 my $context = $self->context;
1142  
1143 # Completion is for used Briks only
1144 my $used = $context->used;
1145 if (! defined($used)) {
1146 $self->log->debug("comp_set: can't fetch used Briks");
1147 return ();
1148 }
1149  
1150 my @words = $self->line_parsed($line);
1151 my $count = scalar(@words);
1152  
1153 $self->log->debug("word[$word] line[$line] start[$start] count[$count]");
1154  
1155 my $brik = defined($words[1]) ? $words[1] : undef;
1156  
1157 my @comp = ();
1158  
1159 # We want completion for used Briks
1160 if (($count == 1)
1161 || ($count == 2 && length($word) > 0)) {
1162 for my $a (keys %$used) {
1163 push @comp, $a if $a =~ /^$word/;
1164 }
1165 }
1166 # We fetch Brik Attributes
1167 elsif ($count == 2 && length($word) == 0) {
1168 if ($self->log->level > 2 && ! exists($used->{$brik})) {
1169 $self->log->debug("comp_set: Brik [$brik] not used");
1170 return ();
1171 }
1172  
1173 my $attributes = $self->get_comp_attributes($brik);
1174  
1175 for my $attribute (keys %$attributes) {
1176 push @comp, $attribute;
1177 }
1178 }
1179 # We want to complete entered Attribute
1180 elsif ($count == 3 && length($word) > 0) {
1181 if ($self->log->level > 2 && ! exists($used->{$brik})) {
1182 $self->log->debug("comp_set: Brik [$brik] not used");
1183 return ();
1184 }
1185  
1186 my $attributes = $self->get_comp_attributes($brik);
1187  
1188 for my $attribute (keys %$attributes) {
1189 if ($attribute =~ /^$word/) {
1190 push @comp, $attribute;
1191 }
1192 }
1193 }
1194 # Else, default completion method on remaining word
1195 else {
1196 return $self->catch_comp_sub($word, $start, $line);
1197 }
1198  
1199 return @comp;
1200 }
1201  
1202 sub run_get {
1203 my $self = shift;
1204 my ($brik, $attribute) = @_;
1205  
1206 my $context = $self->context;
1207  
1208 # get is called without args, we display everything
1209 if (! defined($brik)) {
1210 my $used = $context->used or return;
1211  
1212 for my $brik (sort { $a cmp $b } keys %$used) {
1213 my $attributes = $self->get_help_attributes($brik);
1214  
1215 for my $attribute (sort { $a cmp $b } keys %$attributes) {
1216 $self->log->info("$brik $attribute ".$context->get($brik, $attribute));
1217 }
1218 }
1219 }
1220 # get is called with only a Brik as an arg, we show its Attributes
1221 elsif (defined($brik) && ! defined($attribute)) {
1222 my $used = $context->used or return;
1223  
1224 if (! exists($used->{$brik})) {
1225 return $self->log->error("get: Brik [$brik] not used");
1226 }
1227  
1228 my $attributes = $self->get_help_attributes($brik);
1229  
1230 for my $attribute (sort { $a cmp $b } keys %$attributes) {
1231 $self->log->info("$brik $attribute ".$context->get($brik, $attribute));
1232 }
1233 }
1234 # get is called with is a Brik and an Attribute
1235 elsif (defined($brik) && defined($attribute)) {
1236 my $used = $context->used or return;
1237  
1238 if (! exists($used->{$brik})) {
1239 return $self->log->error("get: Brik [$brik] not used");
1240 }
1241  
1242 if (! $used->{$brik}->brik_has_attribute($attribute)) {
1243 return $self->log->error("get: Attribute [$attribute] does not exist for Brik [$brik]");
1244 }
1245  
1246 $self->log->info("$brik $attribute ".$context->get($brik, $attribute));
1247 }
1248  
1249 return 1;
1250 }
1251  
1252 sub comp_get {
1253 my $self = shift;
1254  
1255 $self->log->debug("comp_get: true");
1256  
1257 return $self->comp_set(@_);
1258 }
1259  
1260 sub run_run {
1261 my $self = shift;
1262 my ($brik, $command, @args) = @_;
1263  
1264 my $context = $self->context;
1265  
1266 if (! defined($brik) || ! defined($command)) {
1267 return $self->log->info("run <brik> <command> [ <arg1> <arg2> .. <argN> ]");
1268 }
1269  
1270 my $r;
1271 {
1272 local $SIG{INT} = sub {
1273 $self->log->debug("run_run: SIG received");
1274 if ($self->global->exit_on_sigint) {
1275 $self->log->debug("run_run: exiting");
1276 $self->run_exit;
1277 }
1278 die("interrupted by user\n");
1279 };
1280  
1281 if ($self->log->level > 2) {
1282 my ($module, $file, $line) = caller();
1283 $self->log->debug("run_run: called by module [$module] from [$file] line[$line]");
1284 }
1285  
1286 # Hack for Term::Shell: we have to convert its exec function to our execute Command
1287 my ($module) = caller();
1288 if ($module eq 'Term::Shell' && $command eq 'exec') {
1289 $command = 'execute';
1290 }
1291  
1292 $r = $context->run($brik, $command, @args);
1293 if (! defined($r)) {
1294 return $self->log->error("run: unable to execute Command [$command] for Brik [$brik]");
1295 }
1296 }
1297  
1298 if ($self->echo) {
1299 $self->page(Data::Dump::dump($r)."\n");
1300 }
1301  
1302 return $r;
1303 }
1304  
1305 sub comp_run {
1306 my $self = shift;
1307 my ($word, $line, $start) = @_;
1308  
1309 $self->log->debug("comp_run: true");
1310  
1311 my $context = $self->context;
1312  
1313 my @words = $self->line_parsed($line);
1314 my $count = scalar(@words);
1315 my $last = $words[-1];
1316  
1317 $self->log->debug("comp_run: words[@words] | word[$word] line[$line] ".
1318 "start[$start] | last[$last]");
1319  
1320 # Completion is for used Briks only
1321 my $used = $context->used;
1322 if (! defined($used)) {
1323 $self->log->debug("comp_run: can't fetch used Briks");
1324 return ();
1325 }
1326  
1327 my $brik = defined($words[1]) ? $words[1] : undef;
1328  
1329 my @comp = ();
1330  
1331 # We want completion for used Briks
1332 if (($count == 1)
1333 || ($count == 2 && length($word) > 0)) {
1334 for my $a (keys %$used) {
1335 push @comp, $a if $a =~ /^$word/;
1336 }
1337 }
1338 # We fetch Brik Commands
1339 elsif ($count == 2 && length($word) == 0) {
1340 if ($self->log->level > 2) {
1341 if (! exists($used->{$brik})) {
1342 $self->log->debug("comp_run: Brik [$brik] not used");
1343 return ();
1344 }
1345 }
1346  
1347 my $commands = $self->get_comp_commands($brik);
1348  
1349 for my $command (keys %$commands) {
1350 push @comp, $command;
1351 }
1352 }
1353 # We want to complete entered Command and Attributes
1354 elsif ($count == 3 && length($word) > 0) {
1355 if ($self->log->level > 2) {
1356 if (! exists($used->{$brik})) {
1357 $self->log->debug("comp_run: Brik [$brik] not used");
1358 return ();
1359 }
1360 }
1361  
1362 my $commands = $self->get_comp_commands($brik);
1363  
1364 for my $command (keys %$commands) {
1365 if ($command =~ /^$word/) {
1366 push @comp, $command;
1367 }
1368 }
1369 }
1370 # Else, default completion method on remaining word
1371 else {
1372 return $self->catch_comp_sub($word, $start, $line);
1373 }
1374  
1375 return @comp;
1376 }
1377  
1378 #
1379 # Term::Shell::catch stuff
1380 #
1381 sub catch_run {
1382 my $self = shift;
1383 my (@args) = @_;
1384  
1385 my $context = $self->context;
1386  
1387 $self->log->debug("catch_run: args [@args]");
1388  
1389 # If it starts with a '/', we really want to 'run shell::command system'
1390 if ($context->is_used('shell::command')) {
1391 my $sc_command = $self->capture_mode ? 'capture' : 'system';
1392 if (defined($args[0]) && $args[0] =~ m{^\s*/}) {
1393 my $cmd = "run shell::command $sc_command";
1394 return $self->cmd(join(' ', $cmd, @args));
1395 }
1396 }
1397  
1398 # Default to execute Perl commands
1399 return $self->run_code(@args);
1400 }
1401  
1402 # Taken from file::find Brik, to make core::shell independant from it.
1403 sub _file_find {
1404 my $self = shift;
1405 my ($path) = @_;
1406  
1407 my @dirs = ();
1408 my @files = ();
1409  
1410 my $dirpattern = '.*';
1411 my $filepattern = '.*';
1412  
1413 # Handle finding of directories
1414 my @tmp_dirs = ();
1415 eval {
1416 @tmp_dirs = io($path)->all_dirs;
1417 };
1418 if ($@) {
1419 if ($self->log->level > 2) {
1420 chomp($@);
1421 $self->log->debug("all: $path: dirs: $@");
1422 }
1423 return { directories => [], files => [] };
1424 }
1425 for my $this (@tmp_dirs) {
1426 if ($this =~ /$dirpattern/) {
1427 push @dirs, "$this/";
1428 }
1429 }
1430  
1431 # Handle finding of files
1432 my @tmp_files = ();
1433 eval {
1434 @tmp_files = io($path)->all_files;
1435 };
1436 if ($@) {
1437 if ($self->log->level > 2) {
1438 chomp($@);
1439 $self->log->debug("all: $path: files: $@");
1440 }
1441 return { directories => [], files => [] };
1442 }
1443 for my $this (@tmp_files) {
1444 if ($this =~ /$filepattern/) {
1445 push @files, "$this";
1446 }
1447 }
1448  
1449 @dirs = map { s/^\.\///; $_ } @dirs; # Remove leading dot slash
1450 @files = map { s/^\.\///; $_ } @files; # Remove leading dot slash
1451  
1452 return {
1453 directories => \@dirs,
1454 files => \@files,
1455 };
1456 }
1457  
1458 # 1. $word - The word the user is trying to complete.
1459 # 2. $line - The line as typed by the user so far.
1460 # 3. $start - The offset into $line where $word starts.
1461 sub catch_comp_sub {
1462 my $self = shift;
1463 # Strange, we had to reverse order for $start and $line only for catch_comp() method.
1464 my ($word, $start, $line) = @_;
1465  
1466 $self->log->debug("catch_comp_sub: true");
1467  
1468 my $context = $self->context;
1469  
1470 my $attribs = $self->term->Attribs;
1471 $attribs->{completion_suppress_append} = 1;
1472  
1473 my @words = $self->line_parsed($line);
1474 my $count = scalar(@words);
1475 my $last = $words[-1];
1476  
1477 $self->log->debug("catch_comp_sub: words[@words] | word[$word] line[$line] start[$start] | last[$last]");
1478  
1479 # Be default, we will read the current directory
1480 if (! length($word)) {
1481 $word = '.';
1482 }
1483  
1484 $self->log->debug("catch_comp_sub: DEFAULT: words[@words] | word[$word] line[$line] start[$start] | last[$last]");
1485  
1486 my @comp = ();
1487  
1488 # We don't use $word here, because the $ is stripped. We have to use $word[-1]
1489 # We also check against $line, if we have a trailing space, the word was complete.
1490 if ($last =~ /^\$/ && $line !~ /\s+$/) {
1491 my $variables = $context->variables;
1492  
1493 for my $this (@$variables) {
1494 $this =~ s/^\$//;
1495 $self->log->debug("variable[$this] start[$start]");
1496 if ($this =~ /^$word/) {
1497 push @comp, $this;
1498 }
1499 }
1500 }
1501 else {
1502 my $path = '.';
1503  
1504 #my $home = $self->path_home;
1505 my $home = $self->{path_home};
1506 $word =~ s/^~/$home/;
1507  
1508 if ($word =~ /^(.*)\/.*$/) {
1509 $path = $1 || '/';
1510 }
1511  
1512 $self->log->debug("path[$path]");
1513  
1514 my $found = $self->_file_find($path);
1515  
1516 for my $this (@{$found->{files}}, @{$found->{directories}}) {
1517 $self->log->debug("check[$this]");
1518 if ($this =~ /^$word/) {
1519 push @comp, $this;
1520 }
1521 }
1522 }
1523  
1524 # If there are some whitespace, we put between quotes
1525 for (@comp) {
1526 if (m{\s+}) {
1527 s/^/"/;
1528 s/$/"/;
1529 }
1530 }
1531  
1532 return @comp;
1533 }
1534  
1535 # 1. $word - The word the user is trying to complete.
1536 # 2. $line - The line as typed by the user so far.
1537 # 3. $start - The offset into $line where $word starts.
1538 # The true default completion method for Term::Shell when no comp_* matched.
1539 # Ugly, we should merge with comp_catch_sub().
1540 # Bug from Term::Shell: $start is not an offset in that case.
1541 sub catch_comp {
1542 my $self = shift;
1543 # Strange, we had to reverse order for $start and $line only for catch_comp() method.
1544 my ($word, $start, $line) = @_;
1545  
1546 $self->log->debug("catch_comp: true");
1547  
1548 my $context = $self->context;
1549  
1550 my $attribs = $self->term->Attribs;
1551 $attribs->{completion_suppress_append} = 1;
1552  
1553 my @words = $self->line_parsed($line);
1554 my $count = scalar(@words);
1555 my $last = $words[-1];
1556  
1557 $self->log->debug("catch_comp: words[@words] | word[$word] line[$line] start[$start] | last[$last]");
1558  
1559 # Be default, we will read the current directory
1560 if (! length($start)) {
1561 $start = '.';
1562 }
1563  
1564 $self->log->debug("catch_comp: DEFAULT: words[@words] | word[$word] line[$line] start[$start] | last[$last]");
1565  
1566 my @comp = ();
1567  
1568 # We don't use $start here, because the $ is stripped. We have to use $word[-1]
1569 # We also check against $line, if we have a trailing space, the word was complete.
1570 if ($last =~ /^\$/ && $line !~ /\s+$/) {
1571 my $variables = $context->variables;
1572  
1573 for my $this (@$variables) {
1574 $this =~ s/^\$//;
1575 $self->log->debug("variable[$this] start[$start]");
1576 if ($this =~ /^$start/) {
1577 push @comp, $this;
1578 }
1579 }
1580 }
1581 # XXX: broken feature since Term::ReadLine::Gnu 1.27
1582 # If we just finished completing an alias, we return the full command
1583 #elsif (exists($self->{_aliases}{"run_$last"})) {
1584 #push @comp, $self->{_aliases}{"run_$last"};
1585 #}
1586 # Otherwise, we complete relative filenames
1587 else {
1588 my $path = '.';
1589  
1590 #my $home = $self->path_home;
1591 my $home = $self->{path_home};
1592 $start =~ s/^~/$home/;
1593  
1594 if ($start =~ /^(.*)\/.*$/) {
1595 $path = $1 || '/';
1596 }
1597 $self->log->debug("path[$path]");
1598  
1599 my $found = $self->_file_find($path);
1600  
1601 for my $this (@{$found->{files}}, @{$found->{directories}}) {
1602 $self->log->debug("check[$this]");
1603 if ($this =~ /^$start/) {
1604 push @comp, $this;
1605 }
1606 }
1607 }
1608  
1609 # If there are some whitespace, we put between quotes
1610 for (@comp) {
1611 if (m{\s+}) {
1612 s/^/"/;
1613 s/$/"/;
1614 }
1615 }
1616  
1617 $self->log->debug("catch_comp: possible [@comp]");
1618  
1619 return @comp;
1620 }
1621  
1622 1;
1623  
1624 __END__
« Metabrik/Core/Log.pm ↑ Summary