chiark / gitweb /
1bce6c5c53dd54f0bfe03b93afd27b2ab364f3ae
[ypp-sc-tools.db-test.git] / pctb / yppsc-parsedb-updatereceiver
1 #!/usr/bin/perl -w
2 #
3 # This script is invoked when the YPP SC PCTB client phones home to
4 # provide updated character set OCR data or updated screenshot pixmap
5 # interpretation (island name) data.
6 #
7 # The client will also phone home anyway to fetch the latest parsedb
8 # before 
9 #
10 # This allows me (the operator of the SC server) to:
11 #    - review the choices made by the user
12 #    - if they are correct, incorporate them in the next client version
13 #    - if they are wrong, incorporate fixes of them, or contradictions of them,
14 #      in
15
16 # The information reported 
17 # The SC PCTB client does this so that 
18
19 use strict (qw(vars));
20
21 $CGI::POST_MAX= 65536;
22 $CGI::DISABLE_UPLOADS= 1;
23
24 use CGI qw/:standard -private_tempfiles/;
25 use IPC::Open2;
26 use IO::Handle;
27 use File::Temp;
28
29 #---------- pixmaps ----------
30
31 sub parseentryin__pixmap ($) {
32     my ($entry_in) = @_;
33     $entry_in =~
34         m/^(\w+ \- \w[-+'"#! 0-9a-z]*)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die; # ']);
35     my ($def,$w,$h)= ($1, $2+0, $3+0);
36     my @d= grep { m/./ } split /\s+/, $';
37     @d == $w*$h*3 or die "$d[0]|$d[1]|...|$d[$#d-1]|$d[$#d] ?";
38     map {
39         m/\D/ and die "$& ?";
40         $_ += 0;
41         $_ >= 0 or die "$_ ?";
42         $_ <= 255 or die "$_ ?";
43     } @d;
44     my $ppm= "P3\n$w $h\n255\n";
45     my $di=0;
46     for (my $y=0; $y<$h; $y++) {
47         for (my $x=0; $x<$w; $x++, $di+=3) {
48 #print STDERR ">$x,$y,$di,",scalar(@d),"<\n";
49             $ppm .= sprintf "  %3d %3d %3d", @d[$di..$di+2];
50         }
51         $ppm .= "\n";
52     }
53
54     my $summary= pipeval($ppm,
55                          'set -x; cat >&2',
56                          'ppmtopgm',
57                          'pnmscale -width 79',
58                          'pnmnorm -bpercent 40 -wpercent 10',
59                          'pgmtopbm -threshold');
60     print STDERR ">$summary<\n";
61     
62
63 # <ship-ahoy.ppm ppmtopgm | pnmscale -width 79 | pnmnorm -bpercent 40 -wpercent 10 | pgmtopbm -threshold | pbmtoascii | cut -c1-79
64         
65     my $entry= "$def\n$ppm";
66     return ('',$def,$entry);
67 }
68
69 #---------- characters ----------
70
71 #---------- useful stuff ----------
72
73 sub pipeval ($@) {
74     my ($val, @cmds) = @_;
75     my (@pids);
76
77     my $paste_child= open PIPEVAL_PASTE, "-|";
78     defined $paste_child or die $!;
79     if (!$paste_child) { print $val or die $!; exit 0; }
80
81     my $f= 'PIPEVAL_PASTE';
82
83 $_=<$f>;
84 print STDERR ">$_<\n";
85
86     foreach my $cmd (@cmds) {
87         my $newf;
88 print STDERR "$cmd | $f\n";
89         push @pids, open2($newf, "<& $f", $cmd);
90 #       if (@pids>1) { close $f or die $!; }
91 print STDERR "$cmd | $f $newf @pids\n";
92         $f= $newf;
93     }
94
95     $!=0; { local ($/)=undef; $val= <$f>; }
96     defined $val or die $!;
97     $f->error and die $!;  close $f or die $!;
98
99     waitpid($paste_child,0) == $paste_child or die "paste $? $!";
100     $?==0 or $?==13 or die "paste $?";
101     close PIPEVAL_PASTE or die $!;
102
103     foreach my $cmd (@cmds) {
104         my $pid= shift @pids;
105         waitpid($pid,0) == $pid or die "$pid $? $!";
106         $?==0 or $?==13 or die "$cmd $?";
107     }
108     return $val;
109 }
110
111 #---------- main program ----------
112
113 my $path= path_info();
114 my $entry_in= param('entry');
115 defined $entry_in or die;
116
117 my $owner= `whoami`; $? and die $?;
118
119 my $kind;
120
121 if ($path =~ /(pixmap|char)/) {
122     $kind=$1;
123 } else {
124     die "$path ?";
125 }
126
127 my ($ctx,$def,$entry)= &{"parseentryin__$kind"}($entry_in);
128
129 my $summary= <<END
130 To: $owner
131 Subject: yppsc dictionary update
132
133 Context:    $kind $ctx
134 Definition: $def
135
136 END
137     ;