From: Ian Jackson Date: Fri, 19 Jun 2009 17:04:54 +0000 (+0100) Subject: WIP yppsc-parsedb-updatereceiver; pipeval works X-Git-Tag: 1.9.2~115^2~2 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-live.git;a=commitdiff_plain;h=e9fe421133870b68103e6cfcf1a1e06058edf032 WIP yppsc-parsedb-updatereceiver; pipeval works --- diff --git a/pctb/yppsc-parsedb-updatereceiver b/pctb/yppsc-parsedb-updatereceiver index 1bce6c5..5d5585e 100755 --- a/pctb/yppsc-parsedb-updatereceiver +++ b/pctb/yppsc-parsedb-updatereceiver @@ -22,9 +22,8 @@ $CGI::POST_MAX= 65536; $CGI::DISABLE_UPLOADS= 1; use CGI qw/:standard -private_tempfiles/; -use IPC::Open2; +use IO::Pipe; use IO::Handle; -use File::Temp; #---------- pixmaps ---------- @@ -52,11 +51,12 @@ sub parseentryin__pixmap ($) { } my $summary= pipeval($ppm, - 'set -x; cat >&2', 'ppmtopgm', 'pnmscale -width 79', 'pnmnorm -bpercent 40 -wpercent 10', - 'pgmtopbm -threshold'); + 'pgmtopbm -threshold', + 'pbmtoascii' + ); print STDERR ">$summary<\n"; @@ -74,36 +74,40 @@ sub pipeval ($@) { my ($val, @cmds) = @_; my (@pids); - my $paste_child= open PIPEVAL_PASTE, "-|"; - defined $paste_child or die $!; - if (!$paste_child) { print $val or die $!; exit 0; } - - my $f= 'PIPEVAL_PASTE'; - -$_=<$f>; -print STDERR ">$_<\n"; - - foreach my $cmd (@cmds) { - my $newf; -print STDERR "$cmd | $f\n"; - push @pids, open2($newf, "<& $f", $cmd); -# if (@pids>1) { close $f or die $!; } -print STDERR "$cmd | $f $newf @pids\n"; - $f= $newf; + my $lastpipe; + + foreach my $cmd ('',@cmds) { + my $pipe= new IO::Pipe or die $!; + my $pid= fork(); defined $pid or die $!; + + if (!$pid) { + $pipe->writer(); + if (!$lastpipe) { + print $pipe $val or die $!; + exit 0; + } else { + open STDIN, '<&', $lastpipe or die $!; + open STDOUT, '>&', $pipe or die $!; + close $lastpipe or die $!; + close $pipe or die $!; + exec $cmd; die $!; + } + } + $pipe->reader(); + if ($lastpipe) { close $lastpipe or die $!; } + $lastpipe= $pipe; + push @pids, $pid; } - $!=0; { local ($/)=undef; $val= <$f>; } + $!=0; { local ($/)=undef; $val= <$lastpipe>; } defined $val or die $!; - $f->error and die $!; close $f or die $!; - - waitpid($paste_child,0) == $paste_child or die "paste $? $!"; - $?==0 or $?==13 or die "paste $?"; - close PIPEVAL_PASTE or die $!; + $lastpipe->error and die $!; close $lastpipe or die $!; - foreach my $cmd (@cmds) { + foreach my $cmd ('(paste)', @cmds) { my $pid= shift @pids; waitpid($pid,0) == $pid or die "$pid $? $!"; $?==0 or $?==13 or die "$cmd $?"; +print STDERR "OK $cmd\n"; } return $val; }