Psh/Builtins/Fc.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 150 CC 18 SCAR 50.8
CC
18
Cov
0%
bi_fc
240.0
_locate_command
20.0
BEGIN
2.0
CRAP 160.3
1 package Psh::Builtins::Fc;
2  
3 use strict;
4 require Psh;
5 require Psh::Util;
6 require Getopt::Std;
7  
8 =item * C<fc> -s [OLD=NEW] [command]
9  
10 The last command or the last command starting with
11 [command] is re-executed after OLD is subsituted with NEW.
12  
13 =item * C<fc> -p prepend [command]
14  
15 Prepends the text prepend in front of the last command or the
16 last command starting with [command] and re-execute it.
17  
18 =item * C<fc> -l
19  
20 Lists the last 15 commands, newest last
21  
22 =item * C<fc> [-e editor] [range]
23  
24 Edit the last command (or [range] commands) in either [editor],
25 C<$ENV{FCEDIT}>, C<$ENV{EDITOR}> or vi.
26  
27 =cut
28  
29 sub _locate_command {
30 my $command=shift;
31 my $comnum=$#Psh::history-1; # (otherwise we find the fc)
32 if ($command) {
33 my $found;
34 for (my $i=$comnum; $i>=0; $i--) {
35 if (Psh::Util::starts_with($Psh::history[$i],$command)) {
36 $comnum=$i;
37 $found=1;
38 last;
39 }
40 }
41 unless ($found) {
42 Psh::Util::print_error_i18n('bi_fc_notfound');
43 return undef;
44 }
45 }
46 return $comnum;
47 }
48  
49 sub bi_fc
50 {
51 my $line= shift;
52 local @ARGV = @{shift()};
53  
54 return (0,undef) unless $#Psh::history;
55  
56 my $opt={};
57 Getopt::Std::getopts('splre:',$opt);
58  
59 if ($opt->{'l'}) {
60 my $num=@Psh::history;
61 $num=15 if $num>15;
62 for (my $i=@Psh::history-$num; $i<@Psh::history; $i++) {
63 Psh::Util::print_out(' '.sprintf('%3d',$i+1).' '.$Psh::history[$i]."\n");
64 }
65 } elsif ($opt->{'s'}) {
66 my $subst='';
67 my $command='';
68 while ($_=pop(@ARGV)) {
69 if (/\=/) {
70 $subst=$_;
71 last;
72 }
73 $command.=$_;
74 }
75 my $comnum= _locate_command($command);
76 return (0,undef) unless defined $comnum;
77 my $comtext=$Psh::history[$comnum];
78 if ($subst) {
79 my ($old,$new)=$subst=~/^(.*?[^\\])\=(.*)$/;
80 $comtext=~s/$old/$new/;
81 }
82 Psh::Util::print_out($comtext."\n");
83 Psh::add_history($comtext);
84 return Psh::evl($comtext);
85 } elsif ($opt->{'p'}) {
86 my $prepend= shift @ARGV;
87 my $command= join ' ',@ARGV;
88 my $comnum= _locate_command($command);
89 return (0,undef) unless defined $comnum;
90 my $comtext="$prepend $Psh::history[$comnum]";
91 Psh::Util::print_out($comtext."\n");
92 Psh::add_history($comtext);
93 return Psh::evl($comtext);
94 } else {
95 if (!$Psh::interactive) {
96 Psh::Util::print_error("fc: not running interactively - cancelled\n");
97 return (0,undef);
98 }
99 my $file= Psh::OS::tmpnam();
100 my $editor= Psh::OS::get_editor($opt->{e} || $ENV{FCEDIT});
101 my $from=my $to=$#Psh::history;
102 if ($ARGV[0]=~/^\s*(\d+)-(\d+)/) {
103 ($from,$to)=($1,$2);
104 } elsif ($ARGV[0]=~/^\s*(\d+)\s*$/) {
105 ($from,$to)=($1,$1);
106 }
107 if ($from<0 or $to<0 or $from>$#Psh::history or
108 $to>$#Psh::history) {
109 Psh::Util::print_error("fc: specified range not in history\n");
110 return (0,undef);
111 }
112  
113 if (open(FILE,"> $file")) {
114 for (my $i=$from; $i<=$to; $i++) {
115 print FILE $Psh::history[$i-1]."\n";
116 }
117 close(FILE);
118 }
119 system("$editor $file");
120 Psh::process_file($file);
121 eval {
122 unlink($file);
123 };
124 }
125 return (1,undef);
126 }
127  
128  
129  
130 1;