| 1 | # | ||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | |||||||||||||||
| 5 | package Rex::Interface::Exec::IOReader; | ||||||||||||||
| 6 | |||||||||||||||
| 7 | 60 | ▶ | use v5.14.4; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 60 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 60 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||
| 11 | |||||||||||||||
| 12 | 60 | ▶ | use IO::Select; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | ▶ | sub io_read { | ||||||||||||
|
Subroutine
io_read: called CC=24 SCAR=39.5
Pod
undocumented
| |||||||||||||||
| 15 | 2 | my ( $self, $out_fh, $err_fh, $pid, $option ) = @_; | |||||||||||||
| 16 | 2 | my ( $out, $err, $out_line, $err_line ); | |||||||||||||
| 17 | |||||||||||||||
| 18 | 2 | my $selector = IO::Select->new(); | |||||||||||||
| 19 | 2 | $selector->add($out_fh); | |||||||||||||
| 20 | 2 | $selector->add($err_fh); | |||||||||||||
| 21 | |||||||||||||||
| 22 | 2 | ▶ | my $rex_int_conf = Rex::Commands::get("rex_internals") || {}; | ||||||||||||
|
Condition50%1/2
Rex::Commands::get('rex_internals') || {}
MC/DC0%0/1
Rex::Commands::get('rex_internals') || {}
Rex::Commands::get('rex_internals')
Truth table50%1/2
A
A= Rex::Commands::get('rex_internals')
| |||||||||||||||
| 23 | 2 | my $buffer_size = 1024; | |||||||||||||
| 24 | 2 | ▶ | if ( exists $rex_int_conf->{read_buffer_size} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 0 | $buffer_size = $rex_int_conf->{read_buffer_size}; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 2 | my ( $last_line_stdout, $last_line_stderr ) = ( "", "" ); | |||||||||||||
| 29 | |||||||||||||||
| 30 | 2 | ▶ | while ( my @ready = $selector->can_read ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 27905 | foreach my $fh (@ready) { | |||||||||||||
| 32 | 28144 | my $buf = ""; | |||||||||||||
| 33 | |||||||||||||||
| 34 | 28144 | my $len = sysread $fh, $buf, $buffer_size; | |||||||||||||
| 35 | 28144 | ▶ | $selector->remove($fh) unless $len; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | 28144 | $buf =~ s/\r?\n/\n/g; # normalize EOL characters | |||||||||||||
| 38 | |||||||||||||||
| 39 | # append buffer to the proper overall output | ||||||||||||||
| 40 | 28144 | ▶ | $out .= $buf if $fh == $out_fh; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 28144 | ▶ | $err .= $buf if $fh == $err_fh; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | 28144 | ▶ | if ( $buf =~ /\n/ ) { # buffer has one or more newlines | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 1 | my @line_chunks = split /\n/, $buf; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 1 | my $partial_last_chunk = ''; | |||||||||||||
| 47 | 1 | ▶ | if ( $buf !~ /\n$/ ) { # last chunk is partial | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 0 | $partial_last_chunk = pop @line_chunks; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 1 | foreach my $chunk (@line_chunks) { | |||||||||||||
| 52 | 1 | ▶ | if ( $fh == $out_fh ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 53 | 0 | $out_line .= $chunk; | |||||||||||||
| 54 | $self->execute_line_based_operation( $out_line, $option ) | ||||||||||||||
| 55 | 0 | ▶ | && do { kill( 'KILL', $pid ); goto END_OPEN }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 0 | $out_line = ''; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | elsif ( $fh == $err_fh ) { | ||||||||||||||
| 59 | 1 | $err_line .= $chunk; | |||||||||||||
| 60 | $self->execute_line_based_operation( $err_line, $option ) | ||||||||||||||
| 61 | 0 | ▶ | && do { kill( 'KILL', $pid ); goto END_OPEN }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 1 | $err_line = ''; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 1 | ▶ | if ($partial_last_chunk) { # append partial chunk to line if present | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 0 | ▶ | $out_line .= $partial_last_chunk if $fh == $out_fh; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | ▶ | $err_line .= $partial_last_chunk if $fh == $err_fh; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | } | ||||||||||||||
| 72 | else { # buffer doesn't have any newlines | ||||||||||||||
| 73 | 28143 | ▶ | $out_line .= $buf if $fh == $out_fh; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 28143 | ▶ | $err_line .= $buf if $fh == $err_fh; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | } | ||||||||||||||
| 76 | } | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 2 | ▶ | unless ($out) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 1 | $out = $last_line_stdout; | |||||||||||||
| 81 | $self->execute_line_based_operation( $out, $option ) | ||||||||||||||
| 82 | 0 | ▶ | && do { kill( 'KILL', $pid ); goto END_OPEN }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 2 | ▶ | unless ($err) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | $err = $last_line_stderr; | |||||||||||||
| 87 | $self->execute_line_based_operation( $err, $option ) | ||||||||||||||
| 88 | 0 | ▶ | && do { kill( 'KILL', $pid ); goto END_OPEN }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | END_OPEN: | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 2 | return ( $out, $err ); | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 60 | 1; | |||||||||||||