| 1 | ## no critic (Documentation::RequirePodAtEnd) | ||
| 2 | ## no critic (Documentation::RequirePodSections) | ||
| 3 | ## no critic (ControlStructures::ProhibitDeepNests) | ||
| 4 | |||
| 5 | package IPC::RunExternal; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use 5.010000; | ||
| 10 | |||
| 11 | # ABSTRACT: Execute an external command conveniently by hiding the details of IPC::Open3. | ||
| 12 | |||
| 13 | our $VERSION = '0.100'; # VERSION: generated by DZP::OurPkgVersion | ||
| 14 | |||
| 15 | |||
| 16 | use English '-no_match_vars'; | ||
| 17 | use Carp 'croak'; | ||
| 18 | use IPC::Open3; | ||
| 19 | use IO::Select; # for select | ||
| 20 | use Symbol 'gensym'; # for gensym | ||
| 21 | |||
| 22 | BEGIN { | ||
| 23 | use Exporter (); | ||
| 24 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); | ||
| 25 | |||
| 26 | @ISA = qw(Exporter DynaLoader); | ||
| 27 | @EXPORT = qw(runexternal); | ||
| 28 | %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], | ||
| 29 | @EXPORT_OK = qw(runexternal); | ||
| 30 | } | ||
| 31 | our @EXPORT_OK; | ||
| 32 | |||
| 33 | # CONSTANTS for this module | ||
| 34 | my $TRUE = 1; | ||
| 35 | my $FALSE = 0; | ||
| 36 | my $EMPTY_STR = q{}; | ||
| 37 | |||
| 38 | my $DEFAULT_PRINT_PROGRESS_INDICATOR = $FALSE; | ||
| 39 | my $DEFAULT_PROGRESS_INDICATOR_CHARACTER = q{.}; | ||
| 40 | my $DEFAULT_EXECUTE_EVERY_SECOND_ROUTINE_POINTER = $FALSE; | ||
| 41 | my $EXIT_STATUS_OK = 1; | ||
| 42 | my $EXIT_STATUS_TIMEOUT = 0; | ||
| 43 | my $EXIT_STATUS_FAILED = -1; | ||
| 44 | my $SIGKILL = 9; | ||
| 45 | |||
| 46 | # GLOBALS | ||
| 47 | # No global variables | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | sub runexternal { | ||
| 53 | |||
| 54 | # Parameters | ||
| 55 | my ($command, $input, $timeout, $parameter_tags) = @_; | ||
| 56 | |||
| 57 | if(!defined $command) { | ||
| 58 | croak('Parameter \'command\' is not initialized!'); | ||
| 59 | } | ||
| 60 | if(!defined $input) { | ||
| 61 | croak('Parameter \'input\' is not initialized!'); | ||
| 62 | } | ||
| 63 | if($timeout < 0) { | ||
| 64 | croak('Parameter \'timeout\' is not valid!'); | ||
| 65 | } | ||
| 66 | |||
| 67 | my $print_progress_indicator = $DEFAULT_PRINT_PROGRESS_INDICATOR; | ||
| 68 | if(exists $parameter_tags->{'print_progress_indicator'}) { | ||
| 69 | if($parameter_tags->{'print_progress_indicator'} == $FALSE || | ||
| 70 | $parameter_tags->{'print_progress_indicator'} == $TRUE) { | ||
| 71 | $print_progress_indicator = $parameter_tags->{'print_progress_indicator'}; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | croak('Parameter \'print_progress_indicator\' is not valid (must be 1/0)!'); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | my $progress_indicator_char = $DEFAULT_PROGRESS_INDICATOR_CHARACTER; | ||
| 79 | if(exists $parameter_tags->{'progress_indicator_char'}) { | ||
| 80 | $progress_indicator_char = $parameter_tags->{'progress_indicator_char'}; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $execute_every_second = $DEFAULT_EXECUTE_EVERY_SECOND_ROUTINE_POINTER; | ||
| 84 | if(exists $parameter_tags->{'execute_every_second'}) { | ||
| 85 | if(ref($parameter_tags->{'execute_every_second'}) eq 'CODE') { | ||
| 86 | $execute_every_second = $parameter_tags->{'execute_every_second'}; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | croak('Parameter execute_every_second is not a code reference!'); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | # Variables | ||
| 94 | my $command_exit_status = $EXIT_STATUS_OK; | ||
| 95 | my $output_std = $EMPTY_STR; | ||
| 96 | my $output_error = $EMPTY_STR; | ||
| 97 | my $output_all = $EMPTY_STR; | ||
| 98 | |||
| 99 | # Validity check | ||
| 100 | if( | ||
| 101 | $command ne $EMPTY_STR | ||
| 102 | #&& defined($input) | ||
| 103 | && $timeout >= 0 | ||
| 104 | ) | ||
| 105 | { | ||
| 106 | $OUTPUT_AUTOFLUSH = $TRUE; # Equals to var $|. Flushes always after writing. | ||
| 107 | my ($infh,$outfh,$errfh); # these are the FHs for our child | ||
| 108 | $errfh = gensym(); # we create a symbol for the errfh | ||
| 109 | # because open3 will not do that for us | ||
| 110 | my $pid; | ||
| 111 | # Read Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval | ||
| 112 | # for an evil eval weakness and a dasdardly difficult eval handling. | ||
| 113 | my $eval_ok = 1; | ||
| 114 | eval { | ||
| 115 | $pid = open3($infh, $outfh, $errfh, $command); | ||
| 116 | # To cover the possiblity that an operation may correctly return a | ||
| 117 | # false value, end the block with "1": | ||
| 118 | 1; | ||
| 119 | } or do { | ||
| 120 | $eval_ok = 0; | ||
| 121 | }; | ||
| 122 | if($eval_ok) { | ||
| 123 | print $infh $input; | ||
| 124 | close $infh; | ||
| 125 | my $sel = new IO::Select; # create a select object to notify | ||
| 126 | # us on reads on our FHs | ||
| 127 | $sel->add($outfh,$errfh); # add the FHs we're interested in | ||
| 128 | my $out_handles_open = 2; | ||
| 129 | for(my $slept_secs = -1; $out_handles_open > 0 && $slept_secs < $timeout; $slept_secs++) { | ||
| 130 | while(my @ready = $sel->can_read(1)) { # read ready, timeout after 1 second. | ||
| 131 | foreach my $fh (@ready) { | ||
| 132 | my $line = <$fh>; # read one line from this fh | ||
| 133 | if( !(defined $line) ){ # EOF on this FH | ||
| 134 | $sel->remove($fh); # remove it from the list | ||
| 135 | $out_handles_open -= 1; | ||
| 136 | next; # and go handle the next FH | ||
| 137 | } | ||
| 138 | if($fh == $outfh) { # if we read from the outfh | ||
| 139 | $output_std .= $line; | ||
| 140 | $output_all .= $line; | ||
| 141 | } elsif($fh == $errfh) { # do the same for errfh | ||
| 142 | $output_error .= $line; | ||
| 143 | $output_all .= $line; | ||
| 144 | } else { # we read from something else?!?! | ||
| 145 | croak "Shouldn't be here!\n"; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | if($timeout == 0) { | ||
| 150 | # No timeout, so we lower the counter by one to keep it forever under 0. | ||
| 151 | # Only the closing of the output handles ($out_handles_open == 0) can break the loop | ||
| 152 | $slept_secs--; | ||
| 153 | } | ||
| 154 | if($print_progress_indicator == $TRUE && $out_handles_open > 0) { | ||
| 155 | print STDOUT $progress_indicator_char; | ||
| 156 | } | ||
| 157 | if($execute_every_second && $out_handles_open > 0) { | ||
| 158 | &$execute_every_second($slept_secs); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | # It is safe to kill in all circumstances. | ||
| 162 | # Anyway, we must reap the child process. | ||
| 163 | my $killed = kill $SIGKILL, $pid; | ||
| 164 | my $command_return_status = $CHILD_ERROR >> 8; | ||
| 165 | if($out_handles_open > 0) { | ||
| 166 | $output_error .= 'Timeout'; | ||
| 167 | $output_all .= 'Timeout'; | ||
| 168 | $command_exit_status = $EXIT_STATUS_TIMEOUT; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | # open3 failed! | ||
| 173 | $output_error .= 'Could not run command'; | ||
| 174 | $output_all .= 'Could not run command'; | ||
| 175 | $command_exit_status = $EXIT_STATUS_FAILED; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | # Parameter check | ||
| 180 | $output_error .= 'Invalid parameters'; | ||
| 181 | $output_all .= 'Invalid parameters'; | ||
| 182 | $command_exit_status = $EXIT_STATUS_FAILED; | ||
| 183 | } | ||
| 184 | |||
| 185 | return ($command_exit_status, $output_std, $output_error, $output_all); | ||
| 186 | } | ||
| 187 | |||
| 188 | 1; | ||
| 189 | |||
| 190 | __END__ |