ShellKit/LineBuffer.pm untested

filter: Statementstmt 0.0% 0 / 155 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 270 CC 33 SCAR 45.2
CC
33
Cov
0%
shell
210.0
shell
110.0
io_get_command
42.0
CRAP 92.0
1 ######################################################################
2  
3 use vars qw( %Printable );
4 %Printable = ( ( map { chr($_), unpack('H2', chr($_)) } (0..255) ),
5 "\\"=>'\\', "\r"=>'r', "\n"=>'n', "\t"=>'t', "\""=>'"' );
6  
7 # $special_characters_escaped = printable( $source_string );
8 sub printable ($) {
9 local $_ = ( defined $_[0] ? $_[0] : '' );
10 s/([\r\n\t\"\\\x00-\x1f\x7F-\xFF])/\\$Printable{$1}/g;
11 return $_;
12 }
13  
14 sub quote_non_words ($) {
15 ( ! length $_[0] or $_[0] =~ /[^\w\_\-\/\.\:\#]/ ) ? '"'.$_[0].'"' : $_[0]
16 }
17  
18 # quoted_and_escaped = qprintable( $source_string );
19 sub qprintable ($) { quote_non_words printable $_[0] }
20  
21 sub list2string {
22 join ( ' ', map qprintable($_), @_ );
23 }
24  
25 ######################################################################
26  
27 sub shell_cmd_eval {
28 my $shell = shift;
29 my $command = shift;
30  
31 $command =~ s/\s+/ /mg;
32 if ( length $command > 32 ) { $command = substr($command, 0, 30) . "..." }
33 print "$command: " . join(', ', map { defined $_ ? $_ : '(undef)' } scalar @results ? @results : "Completed.") );
34
35 return 1;
36 }
37  
38 ######################################################################
39  
40 sub shell_prompt_continuation {
41 my $shell = shift;
42 my $d_prompt = $shell->shell_prompt;
43 (my $c_prompt = $d_prompt) =~ s/[\w\:\-]/ /g;
44 $c_prompt;
45 }
46  
47 use vars qw( $ShellInput );
48  
49 sub io_get_command {
50 my $continuation = "";
51 LINE: while () {
52 local $_ = &$ShellInput( length($continuation) ? $shell->shell_prompt_continuation : $shell->shell_prompt );
53 last LINE unless defined ( $_ );
54
55 if (s/\\$//s) {
56 chomp;
57 $continuation .= $_;
58 next LINE ;
59 } else {
60 $_ = "$continuation$_" if ( length $continuation );
61 $continuation = "";
62 }
63
64 if ( /^<<$/ ) {
65 my $line = '';
66 while ( <> ) {
67 $line .= $_
68 }
69 $_ = $line;
70 }
71
72 s/^\s+//;
73 s/\s+$//;
74
75 $continuation = "";
76 return $_;
77 }
78 }
79  
80 ######################################################################
81  
82 sub shell_err {
83 my $shell = shift;
84 warn "$shell: " . join(' ', @_, "\n" );
85 }
86  
87 ######################################################################
88  
89 use vars qw( @CommandHandlers );
90  
91 sub shell {
92 my $shell = shift;
93 $shell = $shell->shell_require( shift ) if ( scalar @_ );
94 $ShellInput ||= \&io_get_command();
95
96 $shell->shell_start;
97 local %Alias = ( $shell->shell_aliases );
98 COMMAND: while () {
99 local $_ = &$ShellInput( );
100 last COMMAND unless defined ( $_ );
101
102 if ( defined $Alias{$_} ) {
103 $shell->io_print_lines($shell->shell_prompt_continuation() . $Alias{$_});
104 $_ = $Alias{$_};
105 }
106
107 my $cmd = ( $_ =~ /^(\S+)\s/ )[0];
108 if ( defined $cmd and defined $Alias{$cmd} ) {
109 s/^\Q$cmd\E/$Alias{$cmd}/;
110 $shell->io_print_lines( $shell->shell_prompt_continuation() . $_);
111 }
112
113 if ( ! length $_ ) {
114 next COMMAND;
115 }
116
117 my @failures;
118 foreach my $cmd_type ( @CommandHandlers ) {
119 my ($status,$info) = $shell->$cmd_type( $_ );
120 if ( $status ) {
121 if ( $info ) {
122 push @failures, $cmd_type, $info;
123 last;
124 }
125 next COMMAND;
126 } else {
127 push @failures, $cmd_type, $info;
128 }
129 }
130 my $excerpt = $_;
131 $excerpt =~ s/\s+/ /mg;
132 if ( length $excerpt > 32 ) { $excerpt = substr($excerpt, 0, 30) . "..." }
133
134 $shell->io_print_lines( "$excerpt: Failed." );
135 while ( scalar @failures ) {
136 my $method = shift @failures;
137 my $message = shift @failures;
138 next unless $message;
139 $message =~ s/\n/\n /g;
140 $shell->io_print_lines( " $method: $message" );
141 }
142 }
143 }
144  
145 sub shell_auto_command {
146 my $shell = shift;
147 my $command = shift;
148 $shell->io_print_lines( $shell->shell_prompt_continuation() .
149 list2string($command, @_) );
150 $shell->$command( @_ );
151 }
152  
153 ######################################################################
154  
155 use vars qw( @CommandHandlers );
156  
157 sub shell {
158 my $shell = shift;
159 $shell = $shell->shell_require( shift ) if ( scalar @_ );
160 $ShellInput ||= $shell->shell_input();
161
162 my $continuation = "";
163
164 $shell->shell_start;
165 local %Alias = ( $shell->shell_aliases );
166 COMMAND: while () {
167 local $_ = &$ShellInput( length($continuation) ? $shell->shell_prompt_continuation : $shell->shell_prompt );
168 last COMMAND unless defined ( $_ );
169
170 if (s/\\$//s) {
171 chomp;
172 $continuation .= $_;
173 next COMMAND ;
174 } else {
175 $_ = "$continuation$_" if ( length $continuation );
176 $continuation = "";
177 }
178
179 if ( /^<<$/ ) {
180 my $line = '';
181 while ( <> ) {
182 $line .= $_
183 }
184 $_ = $line;
185 }
186
187 s/^\s+//;
188 s/\s+$//;
189
190 if ( defined $Alias{$_} ) {
191 $shell->shell_out_lines($shell->shell_prompt_continuation() . $Alias{$_});
192 $_ = $Alias{$_};
193 }
194
195 my $cmd = ( $_ =~ /^(\S+)\s/ )[0];
196 if ( defined $cmd and defined $Alias{$cmd} ) {
197 s/^\Q$cmd\E/$Alias{$cmd}/;
198 $shell->shell_out_lines( $shell->shell_prompt_continuation() . $_);
199 }
200
201 if ( ! length $_ ) {
202 next COMMAND;
203 }
204
205 my @failures;
206 foreach my $cmd_type ( @CommandHandlers ) {
207 my ($status,$info) = $shell->$cmd_type( $_ );
208 if ( $status ) {
209 $continuation = "";
210 if ( $info ) {
211 push @failures, $cmd_type, $info;
212 last;
213 }
214 next COMMAND;
215 } else {
216 push @failures, $cmd_type, $info;
217 }
218 }
219 my $excerpt = $_;
220 $excerpt =~ s/\s+/ /mg;
221 if ( length $excerpt > 32 ) { $excerpt = substr($excerpt, 0, 30) . "..." }
222
223 $shell->shell_out_lines( "$excerpt: Failed." );
224 while ( scalar @failures ) {
225 my $method = shift @failures;
226 my $message = shift @failures;
227 next unless $message;
228 $message =~ s/\n/\n /g;
229 $shell->shell_out_lines( " $method: $message" );
230 }
231 }
232 }
233  
234 sub shell_auto_command {
235 my $shell = shift;
236 my $command = shift;
237 $shell->shell_out_lines( $shell->shell_prompt_continuation() .
238 list2string($command, @_) );
239 $shell->$command( @_ );
240 }
241  
242 ######################################################################
243  
244  
245 To enter commands, you can:
246  
247 =over 4
248  
249 =item *
250  
251 Use standard line-editing control keys, including filename tab-completion and a command history (curtesy of Term::Readline).
252  
253 =item *
254  
255 End a line with a backslash character to continue it on another line.
256  
257 =item *
258  
259 Enter "<<" followed by return, then several lines of text, followed by return and control-d.
260  
261 =back
262  
263  
264  
265  
266  
267 =head1 EXAMPLES
268  
269 =head2 Declaring a subroutine
270  
271 In the below, I use the << notation to enter a multi-line block of Perl, terminated by a control-D.
272  
273 You can then call any subroutines you've defined on the shell prompt line.
274
275 ~> perl lib/Shell/Shell.pm
276 Term::ShellKit: Starting interactive shell
277 Term::ShellKit> <<
278
279 sub count {
280 my $count = shift;
281 join(', ', ( 0 .. $count ) )
282 }
283 ^D
284
285 Term::ShellKit> count(3)
286 count(3): 0, 1, 2, 3
287 Term::ShellKit> count(5)
288 count(5): 0, 1, 2, 3, 4, 5
289  
290  
291 =head2 Declaring a shell command
292  
293 To create a new shell command, define a function using the above << syntax.
294  
295 You can then call those functions with space-separated arguments on the shell prompt line. These commands can use existing shell methods for further interaction with the user.
296  
297 ~> perl lib/Shell/Shell.pm
298 Term::ShellKit: Starting interactive shell
299 Term::ShellKit> <<
300
301 sub do_count {
302 my $count = shift || $Term::ShellKitInput->( 'do_count to: ' );
303 $shell->shell_out_lines( join(', ', ( 0 .. $count ) ) );
304 }
305 ^D
306
307 Term::ShellKit> do_count 3
308 0, 1, 2, 3
309  
310 Term::ShellKit> do_count
311 do_count to: 3
312 0, 1, 2, 3
313  
314