| 1 | package IPC::Cmd; | ||
| 2 | |||
| 3 | use Params::Check qw[check]; | ||
| 4 | use Module::Load::Conditional qw[can_load]; | ||
| 5 | use Locale::Maketext::Simple Style => 'gettext'; | ||
| 6 | |||
| 7 | use ExtUtils::MakeMaker(); | ||
| 8 | use File::Spec (); | ||
| 9 | use Config; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | |||
| 13 | require Carp; | ||
| 14 | $Carp::CarpLevel = 1; | ||
| 15 | |||
| 16 | BEGIN { | ||
| 17 | use Exporter (); | ||
| 18 | use vars qw[ @ISA $VERSION @EXPORT_OK $VERBOSE | ||
| 19 | $USE_IPC_RUN $USE_IPC_OPEN3 | ||
| 20 | ]; | ||
| 21 | |||
| 22 | $VERSION = '0.22'; | ||
| 23 | $VERBOSE = 0; | ||
| 24 | $USE_IPC_RUN = 1; | ||
| 25 | $USE_IPC_OPEN3 = 1; | ||
| 26 | |||
| 27 | @ISA = qw[Exporter]; | ||
| 28 | @EXPORT_OK = qw[can_run run]; | ||
| 29 | } | ||
| 30 | |||
| 31 | ### check if we can run some command ### | ||
| 32 | sub can_run { | ||
| 33 | my $command = shift; | ||
| 34 | |||
| 35 | if( File::Spec->file_name_is_absolute($command) ) { | ||
| 36 | return MM->maybe_command($command); | ||
| 37 | |||
| 38 | } else { | ||
| 39 | for my $dir (split /$Config{path_sep}/, $ENV{PATH}) { | ||
| 40 | my $abs = File::Spec->catfile($dir, $command); | ||
| 41 | return $abs if $abs = MM->maybe_command($abs); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | ### Execute a command: $cmd may be a scalar or an arrayref of cmd and args | ||
| 48 | ### $bufout is an scalar ref to store outputs, $verbose can override conf | ||
| 49 | sub run { | ||
| 50 | my %hash = @_; | ||
| 51 | |||
| 52 | my $x = ''; | ||
| 53 | my $tmpl = { | ||
| 54 | verbose => { default => $VERBOSE }, | ||
| 55 | command => { required => 1, | ||
| 56 | allow => sub {my $cmd = pop(); | ||
| 57 | !(ref $cmd) or ref $cmd eq 'ARRAY' } | ||
| 58 | }, | ||
| 59 | buffer => { default => \$x }, | ||
| 60 | }; | ||
| 61 | |||
| 62 | my $args = check( $tmpl, \%hash, $VERBOSE ) | ||
| 63 | or ( warn(loc(q[Could not validate input!])), return ); | ||
| 64 | |||
| 65 | ### Kludge! This enables autoflushing for each perl process we launched. | ||
| 66 | local $ENV{PERL5OPT} .= ' -MIPC::Cmd::System=autoflush=1'; | ||
| 67 | |||
| 68 | my $verbose = $args->{verbose}; | ||
| 69 | my $is_win98 = ($^O eq 'MSWin32' and !Win32::IsWinNT()); | ||
| 70 | |||
| 71 | my $err; # error flag | ||
| 72 | my $have_buffer; # to indicate we executed via IPC::Run | ||
| 73 | # or IPC::Open3 only then it makes sence | ||
| 74 | # to return the buffers | ||
| 75 | |||
| 76 | my (@buffer,@buferr,@bufout); | ||
| 77 | |||
| 78 | ### STDOUT message handler | ||
| 79 | my $_out_handler = sub { | ||
| 80 | #sub _out_handler { | ||
| 81 | my $buf = shift; | ||
| 82 | return unless defined $buf; | ||
| 83 | |||
| 84 | print STDOUT $buf if $verbose; | ||
| 85 | push @buffer, $buf; | ||
| 86 | push @bufout, $buf; | ||
| 87 | }; | ||
| 88 | |||
| 89 | ### STDERR message handler | ||
| 90 | my $_err_handler = sub { | ||
| 91 | #sub _err_handler { | ||
| 92 | my $buf = shift; | ||
| 93 | return unless defined $buf; | ||
| 94 | |||
| 95 | print STDERR $buf if $verbose; | ||
| 96 | push @buffer, $buf; | ||
| 97 | push @buferr, $buf; | ||
| 98 | }; | ||
| 99 | |||
| 100 | my $cmd = $args->{command}; | ||
| 101 | my @cmd = ref ($cmd) ? grep(length, @{$cmd}) : $cmd; | ||
| 102 | |||
| 103 | print loc(qq|Running [%1]...\n|,"@cmd") if $verbose; | ||
| 104 | |||
| 105 | ### First, we prefer Barrie Slaymaker's wonderful IPC::Run module. | ||
| 106 | if (!$is_win98 and $USE_IPC_RUN and | ||
| 107 | can_load( | ||
| 108 | modules => { 'IPC::Run' => '0.55' }, | ||
| 109 | verbose => $verbose && ($^O eq 'MSWin32') ) | ||
| 110 | ) { | ||
| 111 | STDOUT->autoflush(1); STDERR->autoflush(1); | ||
| 112 | |||
| 113 | $have_buffer++; | ||
| 114 | |||
| 115 | ### a command like: | ||
| 116 | # [ | ||
| 117 | # '/usr/bin/gzip', | ||
| 118 | # '-cdf', | ||
| 119 | # '/Users/kane/sources/p4/other/archive-extract/t/src/x.tgz', | ||
| 120 | # '|', | ||
| 121 | # '/usr/bin/tar', | ||
| 122 | # '-tf -' | ||
| 123 | # ] | ||
| 124 | ### needs to become: | ||
| 125 | # [ | ||
| 126 | # ['/usr/bin/gzip', '-cdf', | ||
| 127 | # '/Users/kane/sources/p4/other/archive-extract/t/src/x.tgz'] | ||
| 128 | # '|', | ||
| 129 | # ['/usr/bin/tar', '-tf -'] | ||
| 130 | # ] | ||
| 131 | |||
| 132 | my @command; my $special_chars; | ||
| 133 | if( ref $cmd ) { | ||
| 134 | my $aref = []; | ||
| 135 | for my $item (@cmd) { | ||
| 136 | if( $item =~ /[<>|&]/ ) { | ||
| 137 | push @command, $aref, $item; | ||
| 138 | $aref = []; | ||
| 139 | $special_chars++; | ||
| 140 | } else { | ||
| 141 | push @$aref, $item; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | push @command, $aref; | ||
| 145 | } else { | ||
| 146 | @command = map { if( /[<>|&]/ ) { | ||
| 147 | $special_chars++; $_; | ||
| 148 | } else { | ||
| 149 | [ split / +/ ] | ||
| 150 | } | ||
| 151 | } split( /\s*([<>|&])\s*/, $cmd ); | ||
| 152 | } | ||
| 153 | |||
| 154 | ### due to the double '>' construct, stdout buffers are now ending | ||
| 155 | ### up in the stderr buffer. this is a bug in IPC::Run. | ||
| 156 | ### Mailed barries about this early june, no solution yet :( | ||
| 157 | ### update (23-6-04): so this thing with the double > makes | ||
| 158 | ### this command not even fill any buffer: | ||
| 159 | ### perl -lewarn$$ | ||
| 160 | ### so it looks like when there are no 'special' chars in the | ||
| 161 | ### command, like '|' and friends, best not use the '>' construct. | ||
| 162 | if( $special_chars ) { | ||
| 163 | IPC::Run::run(@command, \*STDIN, '>', $_out_handler, | ||
| 164 | '>', $_err_handler) or $err++; | ||
| 165 | } else { | ||
| 166 | IPC::Run::run(@command, \*STDIN, $_out_handler, | ||
| 167 | $_err_handler) or $err++; | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | ### Next, IPC::Open3 is know to fail on Win32, but works on Un*x. | ||
| 172 | } elsif ( $^O !~ /^(?:MSWin32|cygwin)$/ | ||
| 173 | and $USE_IPC_OPEN3 | ||
| 174 | and can_load( | ||
| 175 | modules => { map{$_ => '0.0'} | ||
| 176 | qw|IPC::Open3 IO::Select Symbol| }, | ||
| 177 | verbose => $verbose | ||
| 178 | ) ) { | ||
| 179 | my $rv; | ||
| 180 | ($rv,$err) = _open3_run(\@cmd, $_out_handler, $_err_handler); | ||
| 181 | $have_buffer++; | ||
| 182 | |||
| 183 | |||
| 184 | ### Abandon all hope; falls back to simple system() on verbose calls. | ||
| 185 | } elsif ($verbose) { | ||
| 186 | system(@cmd); | ||
| 187 | $err = $? ? $? : 0; | ||
| 188 | |||
| 189 | ### Non-verbose system() needs to have STDOUT and STDERR muted. | ||
| 190 | } else { | ||
| 191 | local *SAVEOUT; local *SAVEERR; | ||
| 192 | |||
| 193 | open(SAVEOUT, ">&STDOUT") | ||
| 194 | or warn(loc("couldn't dup STDOUT: %1",$!)), return; | ||
| 195 | open(STDOUT, ">".File::Spec->devnull) | ||
| 196 | or warn "couldn't reopen STDOUT: $!", return; | ||
| 197 | |||
| 198 | open(SAVEERR, ">&STDERR") | ||
| 199 | or warn(loc("couldn't dup STDERR: %1",$!)), return; | ||
| 200 | open(STDERR, ">".File::Spec->devnull) | ||
| 201 | or warn(loc("couldn't reopen STDERR: %1",$!)), return; | ||
| 202 | |||
| 203 | system(@cmd); | ||
| 204 | |||
| 205 | open(STDOUT, ">&SAVEOUT") | ||
| 206 | or warn(loc("couldn't restore STDOUT: %1",$!)), return; | ||
| 207 | open(STDERR, ">&SAVEERR") | ||
| 208 | or warn(loc("couldn't restore STDERR: %1",$!)), return; | ||
| 209 | } | ||
| 210 | |||
| 211 | ### unless $err has been set from _open3_run, set it to $? ### | ||
| 212 | $err ||= $?; | ||
| 213 | |||
| 214 | if ( scalar @buffer ) { | ||
| 215 | my $capture = $args->{buffer}; | ||
| 216 | $$capture = join '', @buffer; | ||
| 217 | } | ||
| 218 | |||
| 219 | return wantarray | ||
| 220 | ? $have_buffer | ||
| 221 | ? (!$err, $?, \@buffer, \@bufout, \@buferr) | ||
| 222 | : (!$err, $? ) | ||
| 223 | : !$err | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | ### IPC::Run::run emulator, using IPC::Open3. | ||
| 228 | sub _open3_run { | ||
| 229 | my ($cmdref, $_out_handler, $_err_handler, $verbose) = @_; | ||
| 230 | |||
| 231 | ### in case there are pipes in there; | ||
| 232 | ### IPC::Open3 will call exec and exec will do the right thing ### | ||
| 233 | my $cmd = join " ", @$cmdref; | ||
| 234 | |||
| 235 | ### Following code are adapted from Friar 'abstracts' in the | ||
| 236 | ### Perl Monastery (http://www.perlmonks.org/index.pl?node_id=151886). | ||
| 237 | |||
| 238 | my ($infh, $outfh, $errfh); # open3 handles | ||
| 239 | |||
| 240 | my $pid = eval { | ||
| 241 | IPC::Open3::open3( | ||
| 242 | $infh = Symbol::gensym(), | ||
| 243 | $outfh = Symbol::gensym(), | ||
| 244 | $errfh = Symbol::gensym(), | ||
| 245 | $cmd, | ||
| 246 | ) | ||
| 247 | }; | ||
| 248 | |||
| 249 | |||
| 250 | return (undef, $@) if $@; | ||
| 251 | |||
| 252 | my $sel = IO::Select->new; # create a select object | ||
| 253 | $sel->add($outfh, $errfh); # and add the fhs | ||
| 254 | |||
| 255 | STDOUT->autoflush(1); STDERR->autoflush(1); | ||
| 256 | $outfh->autoflush(1) if UNIVERSAL::can($outfh, 'autoflush'); | ||
| 257 | $errfh->autoflush(1) if UNIVERSAL::can($errfh, 'autoflush'); | ||
| 258 | |||
| 259 | while (my @ready = $sel->can_read) { | ||
| 260 | foreach my $fh (@ready) { # loop through buffered handles | ||
| 261 | # read up to 4096 bytes from this fh. | ||
| 262 | my $len = sysread $fh, my($buf), 4096; | ||
| 263 | |||
| 264 | if (not defined $len){ | ||
| 265 | # There was an error reading | ||
| 266 | warn loc("Error from child: %1",$!); | ||
| 267 | return(undef, $!); | ||
| 268 | } | ||
| 269 | elsif ($len == 0){ | ||
| 270 | $sel->remove($fh); # finished reading | ||
| 271 | next; | ||
| 272 | } | ||
| 273 | elsif ($fh == $outfh) { | ||
| 274 | $_out_handler->($buf); | ||
| 275 | } elsif ($fh == $errfh) { | ||
| 276 | $_err_handler->($buf); | ||
| 277 | } else { | ||
| 278 | warn loc("%1 error", 'IO::Select'); | ||
| 279 | return(undef, $!); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | waitpid $pid, 0; # wait for it to die | ||
| 285 | return 1; | ||
| 286 | } | ||
| 287 | |||
| 288 | 1; | ||
| 289 | |||
| 290 | __END__ |