Portal/Pi.pm untested

filter: Statementstmt 0.0% 0 / 303 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 71 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 566 CC 67 SCAR 70.5
CC
67
Cov
0%
cmd
1980.0
resolve_path
182.0
update
132.0
CRAP 1151.5
1 use strict;
2 package NetServer::Portal::Pi;
3 use NetServer::Portal qw(term);
4 use Cwd;
5 use Symbol;
6 use Scalar::Util qw(reftype);
7 use Data::Dumper;
8  
9 NetServer::Portal->register(cmd => "pi",
10 title => "Perl Introspector",
11 package => __PACKAGE__);
12  
13 if (!defined &Data::Dumper::Maxdepth) {
14 warn "Data::Dumper::Maxdepth is not available
15 NetServer::Portal::Pi will be too verbose";
16 eval q[sub Data::Dumper::Maxdepth{}];
17 }
18 sub new_dumper {
19 my $o = Data::Dumper->new(@_);
20 $o->Indent(1);
21 $o;
22 }
23  
24 my $Help = "*** Perl Introspector ***
25  
26  
27 Type ':help' to load this buffer!
28  
29 output buffer commands:
30 , previous screen full
31 . next screen full
32 :10 jump to line 10 (like vi)
33 /REx search for /REx/
34  
35 < previous buffer
36 > next buffer
37  
38 :w <file> write buffer to <file>
39 :history show history of commands
40  
41 navigational commands:
42  
43 ls [-1afpv]
44 -1 single column format
45 -a show everything
46 -f functions
47 -p sub-packages
48 -v variables (scalars, arrays, hashes, and IOs)
49  
50 cd .. move to preceeding \"directory\"
51 cd <package> use 'ls' to see options
52 cd \$code must evaluate to a REF
53 (for fun, try: cd [1,2,[],4,[],6])
54  
55 :pwd show the working \"directory\"
56 ";
57  
58 sub new {
59 my ($class, $client) = @_;
60 my $o = $client->conf(__PACKAGE__);
61 $o->{Package} ||= [];
62 $o->{Path} ||= [];
63 $o->{I} ||= [];
64 if (!exists $o->{O}) {
65 $o->{O} = [ { line => 0, buf => [split /\n/, $Help] } ];
66 }
67 $o->{buffer} ||= 0;
68 $o;
69 }
70  
71 sub update {
72 my ($o, $c) = @_;
73
74 my $ln = $c->format_line;
75 my $conf = $c->conf;
76 my $rows = $conf->{rows};
77 my $cols = $conf->{cols};
78  
79 my $s = term->Tputs('cl',1,$c->{io}->fd);
80  
81 # optionally wrap lines longer than $cols XXX
82  
83 # turn off line numbers with
84 # :set number
85  
86 my $output_rows = $rows - 4;
87 if (@{$o->{O}}) {
88 my $cur = $o->{O}[$o->{buffer}];
89 my $O = $cur->{buf};
90 my $max_line = @$O - $output_rows/2;
91 $max_line = 0
92 if $max_line < 0;
93 $cur->{line} = 0
94 if $cur->{line} < 0;
95 $cur->{line} = $max_line
96 if $cur->{line} > $max_line;
97 my $to = $cur->{line} + $output_rows - 1;
98 $to = $#$O if
99 $to > $#$O;
100 for (my $lx= $cur->{line}; $lx <= $to; $lx++) {
101 my $l = $O->[$lx];
102  
103 # snarfed from Carp:
104 $l =~ s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
105 $l =~ s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
106  
107 my $note = '';
108 if ($lx == $cur->{line} or $lx == $to) {
109 $note = " [".($lx+1)."]";
110 } elsif (($lx+1) % 4 == 0) {
111 $note = " .";
112 }
113 my $part = substr $l, 0, $cols - length($note) - 1;
114 if ($note) {
115 $part .= ' ' x ($cols - length($part) - length($note) - 1);
116 $part .= $note;
117 }
118  
119 $part .= "\n"
120 if $part !~ /\n$/;
121 $s .= $part;
122 }
123 $s .= "\n" x ($output_rows + $cur->{line} - $to - 1);
124 } else {
125 $s .= "\n" x $output_rows;
126 }
127 $s .= "\n";
128 $s .= $ln->($o->{error});
129 my $at='';
130 if (@{$o->{Path}}) {
131 my @p = @{$o->{Path}};
132 $at = "\$at=$p[$#p] "
133 }
134 $s .= (join('::', @{$o->{Package}}) || 'main')." $at";
135 $s;
136 }
137  
138 sub add_buffer {
139 my ($o, $lines) = @_;
140 unshift @{$o->{O}}, { line => 0, buf => $lines };
141 $o->{buffer} = 0;
142 }
143  
144 sub cmd {
145 my ($o, $c, $in) = @_;
146
147 my $conf = $c->conf;
148 my $Rows = $conf->{rows};
149 my $Cols = $conf->{cols};
150  
151 # these are invisible to command history
152 if (!$in) {
153 return
154 } elsif ($in =~ m/^(\.+)$/) {
155 $o->{O}[$o->{buffer}]{line} += length($1) * $Rows/2; #XXX
156 return;
157 } elsif ($in =~ m/^(,+)$/) {
158 $o->{O}[$o->{buffer}]{line} -= length($1) * $Rows/2; #XXX
159 return;
160 } elsif ($in =~ m/^(\<+)$/) {
161 $o->{buffer} += length $1;
162 $o->{buffer} = $#{$o->{O}} if
163 $o->{buffer} > $#{$o->{O}};
164 return;
165 } elsif ($in =~ m/^(\>+)$/) {
166 $o->{buffer} -= length $1;
167 $o->{buffer} = 0 if
168 $o->{buffer} < 0;
169 return;
170 } elsif ($in =~ s,^/,,) {
171 if (!$in) {
172 $in = $o->{last_search};
173 } else {
174 $o->{last_search} = $in;
175 }
176 my $cur = $o->{O}[$o->{buffer}];
177 my $buf = $cur->{buf};
178 my $at = $cur->{line};
179 my $ok;
180 for (my $x=$at; $x < @$buf; $x++) {
181 if ($buf->[$x] =~ m/$in/) {
182 next if $cur->{line} == $x; # try to do something
183 $cur->{line} = $x;
184 $ok=1;
185 last;
186 }
187 }
188 if (!$ok) {
189 $o->{error} = "No match for /$in/.";
190 }
191 return;
192 } elsif ($in =~ s/^:\s*//) {
193 if ($in =~ m/^(\d+)$/) {
194 $o->{O}[$o->{buffer}]{line} = $1 - $Rows/2; #XXX
195 } elsif ($in eq 'help') {
196 $o->add_buffer([split /\n/, $Help]);
197 } elsif ($in =~ /^w \s+ (.+)$/x) {
198 my $to = $1;
199 $to = getcwd.'/'.$to if
200 $to !~ m,^/,;
201 my $buf = $o->{O}[$o->{buffer}]{buf};
202 my $fh = gensym;
203 open $fh, ">$to" or return $o->{error} = "open $to: $!";
204 my $char=0;
205 for (@$buf) {
206 $char += 1+length $_;
207 print $fh $_."\n";
208 }
209 close $fh;
210 $o->{error} = "$to ".(0+@$buf)." lines, $char characters";
211 } elsif ($in eq 'history') {
212 my @tmp = @{$o->{I}};
213 $o->add_buffer(\@tmp);
214 } elsif ($in eq 'pwd') {
215 my $Dumper = new_dumper($o->{Path});
216 $Dumper->Maxdepth(1);
217 $o->add_buffer(["Current path:",
218 '',
219 @{$o->{Package}},
220 split(/\n/, $Dumper->Dump)]);
221 } else {
222 $o->{error} = "$in? Try :help for a list of commands.";
223 }
224 return;
225 }
226  
227 push @{$o->{I}}, $in;
228 shift @{$o->{I}} if @{$o->{I}} > 16;
229  
230 if (!@{$o->{Path}} and $in =~ m/^ls (\s+ -[1apfv]+)? $/x) {
231 # call resolve here XXX
232 my %f;
233 if ($1) {
234 my $flags = $1;
235 $flags =~ s/^\s*-//;
236 ++$f{$_} for split / */, $flags;
237 }
238 if ($f{a}) {
239 ++$f{$_} for qw(p f v);
240 }
241 ++$f{p} if keys %f == 0;
242  
243 my $package = (join('::', @{$o->{Package}}) || 'main') . '::';
244 my @got;
245  
246 no strict;
247 local *stab = *{$package};
248 while (my ($key,$val) = each(%stab)) {
249 local(*entry) = $val;
250 if ($f{v} and $key !~ /^_</ and defined $entry) {
251 push @got, "\$$key";
252 }
253 if ($f{v} and $key !~ /^_</ and @entry) {
254 push @got, "\@$key";
255 }
256 if ($key ne "main::" && $key ne "DB::" && %entry
257 && $key !~ /^_</
258 && !($package eq __PACKAGE__ and $key eq "stab")) {
259  
260 if ($key =~ /::$/) {
261 push @got, $key
262 if $f{p};
263 } elsif ($f{v}) {
264 push @got, "\%$key";
265 }
266 }
267 my $fileno;
268 if ($f{v} and defined($fileno = fileno(*entry))) {
269 push @got, "$key=$fileno";
270 }
271 if ($f{f} and defined &entry) {
272 push @got, "\&$key";
273 }
274 }
275 @got = sort { $a cmp $b } @got;
276 if (!$f{1}) {
277 my $maxlen=0;
278 for (@got) {
279 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
280 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
281 $maxlen = length $_
282 if $maxlen < length $_;
283 }
284 ++$maxlen;
285 my $cols = int $Cols / $maxlen;
286 if ($cols > 1) {
287 my $per = int(($cols + @got - 1) / $cols);
288 my @grid;
289 for (my $l=0; $l < $per; $l++) {
290 my $row='';
291 for (my $c=0; $c < $cols; $c++) {
292 my $cel = $got[$l + $c*$per] || '';
293 $cel .= ' 'x($maxlen - length $cel);
294 $row .= $cel;
295 }
296 push @grid, $row;
297 }
298 @got = @grid;
299 }
300 }
301 $o->add_buffer(\@got);
302  
303 } elsif (@{$o->{Path}} and $in eq 'ls') {
304 my @p = @{$o->{Path}};
305
306 my $Dumper = new_dumper([ $p[$#p] ]);
307 $Dumper->Maxdepth(1);
308 $o->add_buffer([split /\n/, $Dumper->Dump]);
309  
310 } elsif ($in =~ m,^cd (\s+/)? $,x) {
311 @{$o->{Package}} = ();
312 @{$o->{Path}} = ();
313
314 } elsif ($in =~ s/^cd\s+//) {
315 my @at = $o->resolve_path($in);
316 return if (@at == 1 and !defined $at[0]);
317 @{$o->{Package}} = ();
318 while (@at and !ref $at[0]) {
319 push @{$o->{Package}}, shift(@at);
320 }
321 @{$o->{Path}} = @at;
322  
323 } else {
324 $in .= "\n" if $in !~ /\n$/;
325 my @warn;
326 local $SIG{__WARN__} = sub {
327 push @warn, @_;
328 };
329 my $pack = join('::', @{$o->{Package}}) || 'main';
330 my @eval = eval "no strict;\n#line 1 \"input\"\npackage $pack;\n$in";
331 if ($@) {
332 $o->add_buffer([split /\n/, "package $pack;\n$in---\n$@"]);
333 } else {
334 my $warns='';
335 $warns = join('', @warn)."---\n"
336 if @warn;
337 my $Dumper = new_dumper(\@eval);
338 $o->add_buffer([split /\n/, "package $pack;\n$in---\n".$warns.
339 $Dumper->Dump]);
340 }
341 }
342 pop @{$o->{O}} if @{$o->{O}} > 16;
343 }
344  
345 sub resolve_path {
346 my ($o, $path) = @_;
347 my @at = (@{$o->{Package}}, @{$o->{Path}});
348 @at=()
349 if $path =~ s,^/,,;
350 if ($path =~ m,^[\w\.\:/-]+$,) {
351 my @step = split m'/+', $path;
352 for my $step (@step) {
353 next if $step eq '.';
354 if ($step eq '..') {
355 pop @at;
356 } else {
357 if (@at and ref $at[$#at]) {
358 my $at = $at[$#at];
359 my $to;
360 if (reftype $at eq 'ARRAY') {
361 $to = $at->[$step];
362 } elsif (reftype $at eq 'HASH') {
363 $to = $at->[$step];
364 } else {
365 $o->{error} = "Can't cd $step through '$at'.";
366 return @at;
367 }
368 if (!ref $to) {
369 $o->{error} = "Can't cd $step into '$to'.";
370 return @at;
371 }
372 push @at, $to;
373 } else {
374 $step =~ s/::$//;
375 push @at, $step;
376 }
377 }
378 }
379 @at;
380 } else {
381 my $package = join('::', @{$o->{Package}}) || 'main';
382 my @r = eval "no strict;\n#line 1 \"input\"\npackage $package;\n$path";
383 if ($@) {
384 $o->{error} = $@;
385 return undef;
386 }
387 if (!@r) {
388 $o->{error} = "Nothing there.";
389 } elsif (@r == 1) {
390 if (ref $r[0]) {
391 return (@at, $r[0])
392 } else {
393 $o->{error} = "Can't cd into '$r[0]'";
394 return undef;
395 }
396 } else {
397 return (@at, \@r);
398 }
399 }
400 }
401  
402 1;