chiark / gitweb /
WIP yppsc-parsedb-updatereceiver; pipeval works
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 19 Jun 2009 17:04:54 +0000 (18:04 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Fri, 19 Jun 2009 17:04:54 +0000 (18:04 +0100)
pctb/yppsc-parsedb-updatereceiver

index 1bce6c5c53dd54f0bfe03b93afd27b2ab364f3ae..5d5585ef38bdc4324337c77d2dddb5e2efbc0573 100755 (executable)
@@ -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;
 }