chiark / gitweb /
5d5585ef38bdc4324337c77d2dddb5e2efbc0573
[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 $summary= pipeval($ppm,
54                          'ppmtopgm',
55                          'pnmscale -width 79',
56                          'pnmnorm -bpercent 40 -wpercent 10',
57                          'pgmtopbm -threshold',
58                          'pbmtoascii'
59                          );
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 $lastpipe;
78     
79     foreach my $cmd ('',@cmds) {
80         my $pipe= new IO::Pipe or die $!;
81         my $pid= fork();  defined $pid or die $!;
82
83         if (!$pid) {
84             $pipe->writer();
85             if (!$lastpipe) {
86                  print $pipe $val or die $!;
87                  exit 0;
88              } else {
89                  open STDIN, '<&', $lastpipe or die $!;
90                  open STDOUT, '>&', $pipe or die $!;
91                  close $lastpipe or die $!;
92                  close $pipe or die $!;
93                  exec $cmd; die $!;
94              }
95         }
96         $pipe->reader();
97         if ($lastpipe) { close $lastpipe or die $!; }
98         $lastpipe= $pipe;
99         push @pids, $pid;
100     }
101
102     $!=0; { local ($/)=undef; $val= <$lastpipe>; }
103     defined $val or die $!;
104     $lastpipe->error and die $!;  close $lastpipe or die $!;
105
106     foreach my $cmd ('(paste)', @cmds) {
107         my $pid= shift @pids;
108         waitpid($pid,0) == $pid or die "$pid $? $!";
109         $?==0 or $?==13 or die "$cmd $?";
110 print STDERR "OK $cmd\n";
111     }
112     return $val;
113 }
114
115 #---------- main program ----------
116
117 my $path= path_info();
118 my $entry_in= param('entry');
119 defined $entry_in or die;
120
121 my $owner= `whoami`; $? and die $?;
122
123 my $kind;
124
125 if ($path =~ /(pixmap|char)/) {
126     $kind=$1;
127 } else {
128     die "$path ?";
129 }
130
131 my ($ctx,$def,$entry)= &{"parseentryin__$kind"}($entry_in);
132
133 my $summary= <<END
134 To: $owner
135 Subject: yppsc dictionary update
136
137 Context:    $kind $ctx
138 Definition: $def
139
140 END
141     ;