chiark / gitweb /
bdc76c26c9748acfb9aa1171f6924ed869a37074
[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 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 #---------- useful stuff ----------
75
76 sub pipeval ($@) {
77     my ($val, @cmds) = @_;
78     my (@pids);
79
80     my $lastpipe;
81     
82     foreach my $cmd ('',@cmds) {
83         my $pipe= new IO::Pipe or die $!;
84         my $pid= fork();  defined $pid or die $!;
85
86         if (!$pid) {
87             $pipe->writer();
88             if (!$lastpipe) {
89                  print $pipe $val or die $!;
90                  exit 0;
91              } else {
92                  open STDIN, '<&', $lastpipe or die $!;
93                  open STDOUT, '>&', $pipe or die $!;
94                  close $lastpipe or die $!;
95                  close $pipe or die $!;
96                  exec $cmd; die $!;
97              }
98         }
99         $pipe->reader();
100         if ($lastpipe) { close $lastpipe or die $!; }
101         $lastpipe= $pipe;
102         push @pids, $pid;
103     }
104
105     $!=0; { local ($/)=undef; $val= <$lastpipe>; }
106     defined $val or die $!;
107     $lastpipe->error and die $!;  close $lastpipe or die $!;
108
109     foreach my $cmd ('(paste)', @cmds) {
110         my $pid= shift @pids;
111         waitpid($pid,0) == $pid or die "$pid $? $!";
112         $?==0 or $?==13 or die "$cmd $?";
113     }
114     return $val;
115 }
116
117 #---------- main program ----------
118
119 my $path= path_info();
120 my $entry_in= param('entry');
121 defined $entry_in or die;
122
123 my $owner= `whoami`; $? and die $?;
124 chomp $owner;
125
126 my $kind;
127
128 if ($path =~ /(pixmap|char)/) {
129     $kind=$1;
130 } else {
131     die "$path ?";
132 }
133
134 my ($ctx,$def,$entry,$icon,$width,$whole)= &{"parseentryin__$kind"}($entry_in);
135
136 $icon =~ s/^/ /mg;
137
138 my $email= <<END
139 To: $owner
140 Subject: yppsc dictionary update
141
142 Context:    $kind $ctx
143 Definition: $def
144
145 $icon
146
147 END
148     ;
149
150 $whole =~ s/(.*)\n/ sprintf "%-${width}s\n", $1 /mge;
151 $whole =~ s/^/|/mg;
152 $whole =~ s/\n/|\n/mg;
153 $whole =~ s/^(.*)/ ",".('_' x $width).".\n".$1 /e;
154 $whole =~ s/(.*)$/ $1."\n\`".('~' x $width)."'\n" /e;
155
156 my $lw= 79;
157
158 while ($whole =~ m/../) {
159     my $lhs= $whole;
160     $lhs =~ s/^(.{0,$lw}).*$/$1/mg;
161     $whole =~ s/^.{1,$lw}//mg;
162 #print STDERR "[[[[[$lhs########$whole]]]]]\n";
163     $email .= $lhs;
164 }
165
166 END
167     ;
168
169 my $cutline= "-8<-\n";
170 $email .= $cutline.$entry.$cutline;
171
172 print $email or die $!;