| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Package meta, adds meta database commands to dbish | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | package DBI::Shell::Spool; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | our $VERSION = '11.98'; # VERSION | |||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use vars qw(@ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use IO::Tee; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 4 | my ($self, $sh, @arg) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | $sh->install_options( | |||||||||||||||||||||||||||
| 17 | [ | ||||||||||||||||||||||||||||
| 18 | [ 'spool' => 'off' ], | ||||||||||||||||||||||||||||
| 19 | ]); | ||||||||||||||||||||||||||||
| 20 | 4 | my $com_ref = $sh->{commands}; | |||||||||||||||||||||||||||
| 21 | $com_ref->{spool} = { | ||||||||||||||||||||||||||||
| 22 | 4 | hint => | |||||||||||||||||||||||||||
| 23 | "spool: on/off or file name to send output to", | ||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | return $self; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | #------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | # Start or Stop spooling output. | ||||||||||||||||||||||||||||
| 32 | # The spool support the follow states: | ||||||||||||||||||||||||||||
| 33 | # spool - returns the current state of spooling, if on includes the file name. | ||||||||||||||||||||||||||||
| 34 | # spool on - set the state to on, opens a default name of spool.lst (Yes, the | ||||||||||||||||||||||||||||
| 35 | # Oracle default name). If the spool current state is already on, returns a | ||||||||||||||||||||||||||||
| 36 | # warning message (Already spooling to file X). | ||||||||||||||||||||||||||||
| 37 | # spool /path/file/name - set the state on, attempt to open the file name | ||||||||||||||||||||||||||||
| 38 | # (using the IO::Tee object to allow multiplex output), and set the new IO | ||||||||||||||||||||||||||||
| 39 | # handle to the default handle. | ||||||||||||||||||||||||||||
| 40 | # spool off - set the state to off. If the previous state was on, flush the | ||||||||||||||||||||||||||||
| 41 | # current buffer and close the file handle. If the previous state was off, | ||||||||||||||||||||||||||||
| 42 | # return a warning message (Not current spooling). | ||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||
| 44 | #------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 45 | 4 | ▶ | sub do_spool { | ||||||||||||||||||||||||||
|
Subroutine
do_spool: called CC=14 SCAR=29.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 4 | my ($sh, @args) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Get the current state of spool. | ||||||||||||||||||||||||||||
| 49 | 4 | ▶ | unless(@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | if ($sh->is_spooling) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | return $sh->print_buffer( qq{spooling output to file: }, | ||||||||||||||||||||||||||||
| 52 | 1 | $sh->{spool_file} ); | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 1 | return $sh->print_buffer( qq{not spooling} ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # So what command did I get at this point? | ||||||||||||||||||||||||||||
| 59 | 2 | my $command = shift @args; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if ($command =~ m/\boff/i) { # Turn the spool off (if on). | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if ($sh->is_spooling) { # spool on | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | # The tee object contains the open handles, get a list, shift the | ||||||||||||||||||||||||||||
| 64 | # first (this should be STDOUT), flush. Then for the remainder | ||||||||||||||||||||||||||||
| 65 | # flush each and close. | ||||||||||||||||||||||||||||
| 66 | 1 | my @fhs = $sh->{out_fh}->handles; | |||||||||||||||||||||||||||
| 67 | 1 | $sh->{out_fh} = shift @fhs; select $sh->{out_fh}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | $sh->{out_fh}->flush; | |||||||||||||||||||||||||||
| 70 | 1 | $sh->spool_off; $sh->{spool_file} = undef; | |||||||||||||||||||||||||||
| 71 | 1 | foreach my $fh (@fhs) { | |||||||||||||||||||||||||||
| 72 | 1 | $fh->flush; | |||||||||||||||||||||||||||
| 73 | 1 | $fh->close; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 1 | $sh->{spool_fh} = undef; | |||||||||||||||||||||||||||
| 76 | 1 | return $sh->{out_fh}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 0 | return $sh->print_buffer( qq{not spooling} ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | my $spool_file = undef; | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | if ($command =~ m/on/i) { # Turn the spool off (if on). | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | unless(@args or $args[0] !~ m/!/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@args or not $args[0] =~ /!/
MC/DC0%0/2
@args or not $args[0] =~ /!/
@args
not $args[0] =~ /!/
Truth table0%0/3
A or not(B)
A= @argsB= not $args[0] =~ /!/
| |||||||||||||||||||||||||||||
| 84 | 0 | $spool_file = q{on.lst}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # OK, now we're at the one to open the spool file. How do I handle if the | ||||||||||||||||||||||||||||
| 89 | # file exists? Well, unless the next arg is a !, open the file for append. | ||||||||||||||||||||||||||||
| 90 | 1 | my $mode = q{a+}; | |||||||||||||||||||||||||||
| 91 | 1 | ▶ | if (@args and $args[0] =~ m/!/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@args and $args[0] =~ /!/
MC/DC0%0/2
@args and $args[0] =~ /!/
@args
$args[0] =~ /!/
Truth table33%1/3
A and B
A= @argsB= $args[0] =~ /!/
| |||||||||||||||||||||||||||||
| 92 | 0 | shift @args; | |||||||||||||||||||||||||||
| 93 | 0 | $mode = q{w}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | my $out_fh = $sh->{out_fh}; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | $spool_file = defined $spool_file ? $spool_file : $command; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | ▶ | if (defined $spool_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | ▶ | my $tee_fh = new IO::Tee($out_fh, new IO::File($spool_file, $mode)) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | return $sh->alert(qq{Unable create IO::Tee ($spool_file) handle: $!\n}); | ||||||||||||||||||||||||||||
| 103 | 1 | $sh->{out_fh} = $tee_fh; | |||||||||||||||||||||||||||
| 104 | 1 | $sh->{spool_file} = $spool_file; $sh->spool_on; | |||||||||||||||||||||||||||
| 105 | 1 | $sh->{spool_fh} = ($tee_fh->handles)[1]; | |||||||||||||||||||||||||||
| 106 | 1 | select $tee_fh; | |||||||||||||||||||||||||||
| 107 | 1 | return $sh->print_buffer( qq{spooling $spool_file} ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 0 | return $sh->alert( qq{spool command failed for unknown reason} ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 4 | 1; | |||||||||||||||||||||||||||