chiark / gitweb /
WIP yppsc-parsedb-updatereceiver; can convert pixmap to email
[ypp-sc-tools.db-live.git] / pctb / yppsc-parsedb-updatereceiver
index 1bce6c5c53dd54f0bfe03b93afd27b2ab364f3ae..bdc76c26c9748acfb9aa1171f6924ed869a37074 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 ----------
 
@@ -51,19 +50,23 @@ sub parseentryin__pixmap ($) {
        $ppm .= "\n";
     }
 
-    my $summary= pipeval($ppm,
-                        'set -x; cat >&2',
+    my $icon= pipeval($ppm,
                         'ppmtopgm',
-                        'pnmscale -width 79',
-                        'pnmnorm -bpercent 40 -wpercent 10',
-                        'pgmtopbm -threshold');
-    print STDERR ">$summary<\n";
+                        'pnmscale -xysize 156 80',
+                        'pnmnorm -bpercent 40 -wpercent 20',
+                        'pgmtopbm -threshold',
+                        'pnminvert',
+                        'pbmtoascii -2x4');
+
+    my $whole= pipeval($ppm,
+                      'ppmtopgm',
+                      'pnmnorm -bpercent 40 -wpercent 20',
+                      'pgmtopbm -threshold',
+                      'pnminvert',
+                      'pbmtoascii');
     
-
-# <ship-ahoy.ppm ppmtopgm | pnmscale -width 79 | pnmnorm -bpercent 40 -wpercent 10 | pgmtopbm -threshold | pbmtoascii | cut -c1-79
-       
     my $entry= "$def\n$ppm";
-    return ('',$def,$entry);
+    return ('',$def,$entry,$icon,$w,$whole);
 }
 
 #---------- characters ----------
@@ -74,33 +77,36 @@ 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 $?";
@@ -115,6 +121,7 @@ my $entry_in= param('entry');
 defined $entry_in or die;
 
 my $owner= `whoami`; $? and die $?;
+chomp $owner;
 
 my $kind;
 
@@ -124,14 +131,42 @@ if ($path =~ /(pixmap|char)/) {
     die "$path ?";
 }
 
-my ($ctx,$def,$entry)= &{"parseentryin__$kind"}($entry_in);
+my ($ctx,$def,$entry,$icon,$width,$whole)= &{"parseentryin__$kind"}($entry_in);
 
-my $summary= <<END
+$icon =~ s/^/ /mg;
+
+my $email= <<END
 To: $owner
 Subject: yppsc dictionary update
 
 Context:    $kind $ctx
 Definition: $def
 
+$icon
+
 END
     ;
+
+$whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
+$whole =~ s/^/|/mg;
+$whole =~ s/\n/|\n/mg;
+$whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
+$whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
+
+my $lw= 79;
+
+while ($whole =~ m/../) {
+    my $lhs= $whole;
+    $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
+    $whole =~ s/^.{1,$lw}//mg;
+#print STDERR "[[[[[$lhs########$whole]]]]]\n";
+    $email .= $lhs;
+}
+
+END
+    ;
+
+my $cutline= "-8<-\n";
+$email .= $cutline.$entry.$cutline;
+
+print $email or die $!;