chiark / gitweb /
Rename yppsc-ocr-resolver to dictionary-manager, and database and charset to dictiona...
[ypp-sc-tools.db-live.git] / pctb / dictionary-update-receiver
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 IO::Pipe;
26 use IO::Handle;
27
28 #---------- pixmaps ----------
29
30 sub parseentryin__pixmap ($) {
31     my ($entry_in) = @_;
32     $entry_in =~
33         m/^(\w+ \- \w[-+'"#! 0-9a-z]*)\nP3\n([1-9]\d{1,3}) ([1-9]\d{1,3})\n255\n/s or die; # ']);
34     my ($def,$w,$h)= ($1, $2+0, $3+0);
35     my @d= grep { m/./ } split /\s+/, $';
36     @d == $w*$h*3 or die "$d[0]|$d[1]|...|$d[$#d-1]|$d[$#d] ?";
37     map {
38         m/\D/ and die "$& ?";
39         $_ += 0;
40         $_ >= 0 or die "$_ ?";
41         $_ <= 255 or die "$_ ?";
42     } @d;
43     my $ppm= "P3\n$w $h\n255\n";
44     my $di=0;
45     for (my $y=0; $y<$h; $y++) {
46         for (my $x=0; $x<$w; $x++, $di+=3) {
47 #print STDERR ">$x,$y,$di,",scalar(@d),"<\n";
48             $ppm .= sprintf "  %3d %3d %3d", @d[$di..$di+2];
49         }
50         $ppm .= "\n";
51     }
52
53     my $icon= pipeval($ppm,
54                          'ppmtopgm',
55                          'pnmscale -xysize 156 80',
56                          'pnmnorm -bpercent 40 -wpercent 20',
57                          'pgmtopbm -threshold',
58                          'pnminvert',
59                          'pbmtoascii -2x4');
60
61     my $whole= pipeval($ppm,
62                        'ppmtopgm',
63                        'pnmnorm -bpercent 40 -wpercent 20',
64                        'pgmtopbm -threshold',
65                        'pnminvert',
66                        'pbmtoascii');
67     
68     my $entry= "$def\n$ppm";
69     return ('',$def,$entry,$icon,$w,$whole);
70 }
71
72 #---------- characters ----------
73
74 sub parseentryin__char ($) {
75     my ($ei) = @_;
76     $ei =~ m/^([1-9]\d{0,2})\n(Digit|Upper|Lower)\n((?:[-&\'A-F0-9a-f ]|\x20)+)\n/s or die;
77     my ($h,$ctx,$str)= ($1+0,$2,$3);
78 #print STDERR ">$'<\n";
79     my @d= grep { m/./ } split /\n/, $';
80 #print STDERR ">@d<\n";
81     die if $h>31;
82     die if @d>400;
83     my $maxval= (1<<$h)-1;
84     map {
85         m/^[0-9a-f]{1,8}$/ or die;
86         $_= hex $_;
87         die "$_ ?" if $_ > $maxval;
88     } @d;
89     my $w= @d;
90     my $ppm= "P2\n$w $h\n1\n";
91     for (my $y=0; $y<$h; $y++) {
92         for (my $x=0; $x<$w; $x++) {
93             $ppm .= sprintf " %d", !!($d[$x] & (1<<$y));
94         }
95         $ppm .= "\n";
96     }
97     my $entry= sprintf "%d\n%s\n%s\n", $h,$ctx,$str;
98     map { $entry .= sprintf "%x\n", $_; } @d;
99     
100 #print STDERR "[[[[\n$ppm\n]]]]";
101
102     my $icon= pipeval($ppm,
103 #                     "pnmscale -xysize 78 $h",
104                       'pgmtopbm -threshold',
105                       'pnminvert',
106                       'pbmtoascii');
107
108     return ("$ctx",$str,$entry, '',$w,$icon);
109 }
110
111 #---------- useful stuff ----------
112
113 sub pipeval ($@) {
114     my ($val, @cmds) = @_;
115     my (@pids);
116
117     my $lastpipe;
118     
119     foreach my $cmd ('',@cmds) {
120         my $pipe= new IO::Pipe or die $!;
121         my $pid= fork();  defined $pid or die $!;
122
123         if (!$pid) {
124             $pipe->writer();
125             if (!$lastpipe) {
126                  print $pipe $val or die $!;
127                  exit 0;
128              } else {
129                  open STDIN, '<&', $lastpipe or die $!;
130                  open STDOUT, '>&', $pipe or die $!;
131                  close $lastpipe or die $!;
132                  close $pipe or die $!;
133                  exec $cmd; die $!;
134              }
135         }
136         $pipe->reader();
137         if ($lastpipe) { close $lastpipe or die $!; }
138         $lastpipe= $pipe;
139         push @pids, $pid;
140     }
141
142     $!=0; { local ($/)=undef; $val= <$lastpipe>; }
143     defined $val or die $!;
144     $lastpipe->error and die $!;  close $lastpipe or die $!;
145
146     foreach my $cmd ('(paste)', @cmds) {
147         my $pid= shift @pids;
148         waitpid($pid,0) == $pid or die "$pid $? $!";
149         $?==0 or $?==13 or die "$cmd $?";
150     }
151     return $val;
152 }
153
154 #---------- main program ----------
155
156 my $path= path_info();
157 my $entry_in= param('entry');
158 defined $entry_in or die;
159
160 my $owner= `whoami`; $? and die $?;
161 chomp $owner;
162
163 my $kind;
164
165 if ($path =~ /(pixmap|char)/) {
166     $kind=$1;
167 } else {
168     die "$path ?";
169 }
170
171 my ($ctx,$def,$entry,$icon,$width,$whole)= &{"parseentryin__$kind"}($entry_in);
172
173 $icon =~ s/^/ /mg;
174
175 my $email= <<END
176 To: $owner
177 Subject: yppsc dictionary update
178
179 Context:    $kind $ctx
180 Definition: $def
181
182 $icon
183
184 END
185     ;
186
187 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
188 $whole =~ s/^/|/mg;
189 $whole =~ s/\n/|\n/mg;
190 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
191 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
192
193 my $lw= 79;
194
195 while ($whole =~ m/../) {
196     my $lhs= $whole;
197     $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
198     $whole =~ s/^.{1,$lw}//mg;
199 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
200     $email .= $lhs;
201 }
202
203 END
204     ;
205
206 my $cutline= "-8<-\n";
207 $email .= $cutline.$entry.$cutline;
208
209 print $email or die $!;